| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | |
| 4 | #include "envelope_editor.h" |
| 5 | |
| 6 | #include <base/color.h> |
| 7 | #include <base/math.h> |
| 8 | #include <base/str.h> |
| 9 | #include <base/vmath.h> |
| 10 | |
| 11 | #include <engine/font_icons.h> |
| 12 | #include <engine/graphics.h> |
| 13 | #include <engine/input.h> |
| 14 | #include <engine/keys.h> |
| 15 | #include <engine/shared/config.h> |
| 16 | |
| 17 | #include <game/editor/editor.h> |
| 18 | #include <game/editor/editor_actions.h> |
| 19 | #include <game/editor/editor_trackers.h> |
| 20 | #include <game/editor/mapitems/envelope.h> |
| 21 | #include <game/editor/mapitems/map.h> |
| 22 | |
| 23 | static const char *const CURVE_TYPE_NAMES[] = {"Step" , "Linear" , "Slow" , "Fast" , "Smooth" , "Bezier" }; |
| 24 | static const char *const CURVE_TYPE_NAMES_SHORT[] = {"N" , "L" , "S" , "F" , "M" , "B" }; |
| 25 | static_assert(std::size(CURVE_TYPE_NAMES) == NUM_CURVETYPES); |
| 26 | static_assert(std::size(CURVE_TYPE_NAMES_SHORT) == NUM_CURVETYPES); |
| 27 | |
| 28 | static const char *CurveTypeNameShort(int CurveType) |
| 29 | { |
| 30 | if(in_range<int>(a: CurveType, lower: 0, upper: std::size(CURVE_TYPE_NAMES_SHORT) - 1)) |
| 31 | { |
| 32 | return CURVE_TYPE_NAMES_SHORT[CurveType]; |
| 33 | } |
| 34 | return "!?" ; |
| 35 | } |
| 36 | |
| 37 | static float ClampDelta(float Val, float Delta, float Min, float Max) |
| 38 | { |
| 39 | if(Val + Delta <= Min) |
| 40 | return Min - Val; |
| 41 | if(Val + Delta >= Max) |
| 42 | return Max - Val; |
| 43 | return Delta; |
| 44 | } |
| 45 | |
| 46 | class CTimeStep |
| 47 | { |
| 48 | public: |
| 49 | template<class T> |
| 50 | CTimeStep(T t) |
| 51 | { |
| 52 | if constexpr(std::is_same_v<T, std::chrono::milliseconds>) |
| 53 | m_Unit = ETimeUnit::MILLISECONDS; |
| 54 | else if constexpr(std::is_same_v<T, std::chrono::seconds>) |
| 55 | m_Unit = ETimeUnit::SECONDS; |
| 56 | else |
| 57 | m_Unit = ETimeUnit::MINUTES; |
| 58 | |
| 59 | m_Value = t; |
| 60 | } |
| 61 | |
| 62 | CTimeStep operator*(int k) const |
| 63 | { |
| 64 | return CTimeStep(m_Value * k, m_Unit); |
| 65 | } |
| 66 | |
| 67 | CTimeStep operator-(const CTimeStep &Other) |
| 68 | { |
| 69 | return CTimeStep(m_Value - Other.m_Value, m_Unit); |
| 70 | } |
| 71 | |
| 72 | void Format(char *pBuffer, size_t BufferSize) |
| 73 | { |
| 74 | int Milliseconds = m_Value.count() % 1000; |
| 75 | int Seconds = std::chrono::duration_cast<std::chrono::seconds>(d: m_Value).count() % 60; |
| 76 | int Minutes = std::chrono::duration_cast<std::chrono::minutes>(d: m_Value).count(); |
| 77 | |
| 78 | switch(m_Unit) |
| 79 | { |
| 80 | case ETimeUnit::MILLISECONDS: |
| 81 | if(Minutes != 0) |
| 82 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%d:%02d.%03dmin" , Minutes, Seconds, Milliseconds); |
| 83 | else if(Seconds != 0) |
| 84 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%d.%03ds" , Seconds, Milliseconds); |
| 85 | else |
| 86 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%dms" , Milliseconds); |
| 87 | break; |
| 88 | case ETimeUnit::SECONDS: |
| 89 | if(Minutes != 0) |
| 90 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%d:%02dmin" , Minutes, Seconds); |
| 91 | else |
| 92 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%ds" , Seconds); |
| 93 | break; |
| 94 | case ETimeUnit::MINUTES: |
| 95 | str_format(buffer: pBuffer, buffer_size: BufferSize, format: "%dmin" , Minutes); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | float AsSeconds() const |
| 101 | { |
| 102 | return std::chrono::duration_cast<std::chrono::duration<float>>(d: m_Value).count(); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | enum class ETimeUnit |
| 107 | { |
| 108 | MILLISECONDS, |
| 109 | SECONDS, |
| 110 | MINUTES, |
| 111 | } m_Unit; |
| 112 | std::chrono::milliseconds m_Value; |
| 113 | |
| 114 | CTimeStep(std::chrono::milliseconds Value, ETimeUnit Unit) |
| 115 | { |
| 116 | m_Value = Value; |
| 117 | m_Unit = Unit; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | void CEnvelopeEditor::CState::Reset(CEditor *pEditor) |
| 122 | { |
| 123 | m_ZoomX = CSmoothValue(1.0f, 0.1f, 600.0f); |
| 124 | m_ZoomY = CSmoothValue(640.0f, 0.1f, 32000.0f); |
| 125 | m_ZoomX.OnInit(pEditor); |
| 126 | m_ZoomY.OnInit(pEditor); |
| 127 | m_ResetZoom = true; |
| 128 | m_Offset = vec2(0.1f, 0.5f); |
| 129 | m_ActiveChannels = 0b1111; |
| 130 | } |
| 131 | |
| 132 | void CEnvelopeEditor::OnReset() |
| 133 | { |
| 134 | m_NameInput.SetBuffer(pStr: nullptr, MaxSize: 0, MaxChars: 0); |
| 135 | m_EnvelopeEditorButtonUsed = -1; |
| 136 | m_Operation = EEnvelopeEditorOp::NONE; |
| 137 | m_vAccurateDragValuesX.clear(); |
| 138 | m_vAccurateDragValuesY.clear(); |
| 139 | m_MouseStart = vec2(0.0f, 0.0f); |
| 140 | m_ScaleFactor = vec2(1.0f, 1.0f); |
| 141 | m_Midpoint = vec2(0.0f, 0.0f); |
| 142 | m_vInitialPositionsX.clear(); |
| 143 | m_vInitialPositionsY.clear(); |
| 144 | } |
| 145 | |
| 146 | void CEnvelopeEditor::Render(CUIRect View) |
| 147 | { |
| 148 | Map()->m_SelectedEnvelope = Map()->m_vpEnvelopes.empty() ? -1 : std::clamp(val: Map()->m_SelectedEnvelope, lo: 0, hi: (int)Map()->m_vpEnvelopes.size() - 1); |
| 149 | std::shared_ptr<CEnvelope> pEnvelope = Map()->m_vpEnvelopes.empty() ? nullptr : Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 150 | CState &State = Map()->m_EnvelopeEditorState; |
| 151 | |
| 152 | CUIRect ToolBar, CurveBar, ColorBar, DragBar; |
| 153 | View.HSplitTop(Cut: 30.0f, pTop: &DragBar, pBottom: nullptr); |
| 154 | DragBar.y -= 2.0f; |
| 155 | DragBar.w += 2.0f; |
| 156 | DragBar.h += 4.0f; |
| 157 | Editor()->DoEditorDragBar(View, pDragBar: &DragBar, Side: CEditor::EDragSide::TOP, pValue: &Editor()->m_aExtraEditorSplits[CEditor::EXTRAEDITOR_ENVELOPES]); |
| 158 | View.HSplitTop(Cut: 15.0f, pTop: &ToolBar, pBottom: &View); |
| 159 | View.HSplitTop(Cut: 15.0f, pTop: &CurveBar, pBottom: &View); |
| 160 | ToolBar.Margin(Cut: 2.0f, pOtherRect: &ToolBar); |
| 161 | CurveBar.Margin(Cut: 2.0f, pOtherRect: &CurveBar); |
| 162 | |
| 163 | bool CurrentEnvelopeSwitched = false; |
| 164 | |
| 165 | // do the toolbar |
| 166 | { |
| 167 | CUIRect Button; |
| 168 | |
| 169 | // redo button |
| 170 | ToolBar.VSplitRight(Cut: 25.0f, pLeft: &ToolBar, pRight: &Button); |
| 171 | if(Editor()->DoButton_FontIcon(pId: &m_RedoButtonId, pText: FontIcon::REDO, Checked: Map()->m_EnvelopeEditorHistory.CanRedo() ? 0 : -1, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "[Ctrl+Y] Redo the last action." , Corners: IGraphics::CORNER_R, FontSize: 11.0f) == 1) |
| 172 | { |
| 173 | Map()->m_EnvelopeEditorHistory.Redo(); |
| 174 | } |
| 175 | |
| 176 | // undo button |
| 177 | ToolBar.VSplitRight(Cut: 25.0f, pLeft: &ToolBar, pRight: &Button); |
| 178 | ToolBar.VSplitRight(Cut: 10.0f, pLeft: &ToolBar, pRight: nullptr); |
| 179 | if(Editor()->DoButton_FontIcon(pId: &m_UndoButtonId, pText: FontIcon::UNDO, Checked: Map()->m_EnvelopeEditorHistory.CanUndo() ? 0 : -1, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "[Ctrl+Z] Undo the last action." , Corners: IGraphics::CORNER_L, FontSize: 11.0f) == 1) |
| 180 | { |
| 181 | Map()->m_EnvelopeEditorHistory.Undo(); |
| 182 | } |
| 183 | |
| 184 | ToolBar.VSplitRight(Cut: 50.0f, pLeft: &ToolBar, pRight: &Button); |
| 185 | if(Editor()->DoButton_Editor(pId: &m_NewSoundEnvelopeButtonId, pText: "Sound+" , Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Create a new sound envelope." )) |
| 186 | { |
| 187 | Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEnvelopeAdd>(args: Map(), args: CEnvelope::EType::SOUND)); |
| 188 | pEnvelope = Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 189 | CurrentEnvelopeSwitched = true; |
| 190 | } |
| 191 | |
| 192 | ToolBar.VSplitRight(Cut: 5.0f, pLeft: &ToolBar, pRight: nullptr); |
| 193 | ToolBar.VSplitRight(Cut: 50.0f, pLeft: &ToolBar, pRight: &Button); |
| 194 | if(Editor()->DoButton_Editor(pId: &m_NewColorEnvelopeButtonId, pText: "Color+" , Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Create a new color envelope." )) |
| 195 | { |
| 196 | Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEnvelopeAdd>(args: Map(), args: CEnvelope::EType::COLOR)); |
| 197 | pEnvelope = Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 198 | CurrentEnvelopeSwitched = true; |
| 199 | } |
| 200 | |
| 201 | ToolBar.VSplitRight(Cut: 5.0f, pLeft: &ToolBar, pRight: nullptr); |
| 202 | ToolBar.VSplitRight(Cut: 50.0f, pLeft: &ToolBar, pRight: &Button); |
| 203 | if(Editor()->DoButton_Editor(pId: &m_NewPositionEnvelopeButtonId, pText: "Pos.+" , Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Create a new position envelope." )) |
| 204 | { |
| 205 | Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEnvelopeAdd>(args: Map(), args: CEnvelope::EType::POSITION)); |
| 206 | pEnvelope = Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 207 | CurrentEnvelopeSwitched = true; |
| 208 | } |
| 209 | |
| 210 | if(Map()->m_SelectedEnvelope >= 0) |
| 211 | { |
| 212 | // Delete button |
| 213 | ToolBar.VSplitRight(Cut: 10.0f, pLeft: &ToolBar, pRight: nullptr); |
| 214 | ToolBar.VSplitRight(Cut: 25.0f, pLeft: &ToolBar, pRight: &Button); |
| 215 | if(Editor()->DoButton_Editor(pId: &m_DeleteButtonId, pText: "✗" , Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Delete this envelope." )) |
| 216 | { |
| 217 | auto vpObjectReferences = Map()->DeleteEnvelope(Index: Map()->m_SelectedEnvelope); |
| 218 | Map()->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeDelete>(args: Map(), args&: Map()->m_SelectedEnvelope, args&: vpObjectReferences, args&: pEnvelope)); |
| 219 | |
| 220 | Map()->m_SelectedEnvelope = Map()->m_vpEnvelopes.empty() ? -1 : std::clamp(val: Map()->m_SelectedEnvelope, lo: 0, hi: (int)Map()->m_vpEnvelopes.size() - 1); |
| 221 | pEnvelope = Map()->m_vpEnvelopes.empty() ? nullptr : Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 222 | Map()->OnModify(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // check again, because the last envelope might has been deleted |
| 227 | if(Map()->m_SelectedEnvelope >= 0) |
| 228 | { |
| 229 | // Move right button |
| 230 | ToolBar.VSplitRight(Cut: 5.0f, pLeft: &ToolBar, pRight: nullptr); |
| 231 | ToolBar.VSplitRight(Cut: 25.0f, pLeft: &ToolBar, pRight: &Button); |
| 232 | if(Editor()->DoButton_Ex(pId: &m_MoveRightButtonId, pText: "→" , Checked: (Map()->m_SelectedEnvelope >= (int)Map()->m_vpEnvelopes.size() - 1 ? -1 : 0), pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Move this envelope to the right." , Corners: IGraphics::CORNER_R)) |
| 233 | { |
| 234 | int MoveTo = Map()->m_SelectedEnvelope + 1; |
| 235 | int MoveFrom = Map()->m_SelectedEnvelope; |
| 236 | Map()->m_SelectedEnvelope = Map()->MoveEnvelope(IndexFrom: MoveFrom, IndexTo: MoveTo); |
| 237 | if(Map()->m_SelectedEnvelope != MoveFrom) |
| 238 | { |
| 239 | Map()->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEdit>(args: Map(), args&: Map()->m_SelectedEnvelope, args: CEditorActionEnvelopeEdit::EEditType::ORDER, args&: MoveFrom, args&: Map()->m_SelectedEnvelope)); |
| 240 | pEnvelope = Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 241 | Map()->OnModify(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Move left button |
| 246 | ToolBar.VSplitRight(Cut: 25.0f, pLeft: &ToolBar, pRight: &Button); |
| 247 | if(Editor()->DoButton_Ex(pId: &m_MoveLeftButtonId, pText: "←" , Checked: (Map()->m_SelectedEnvelope <= 0 ? -1 : 0), pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Move this envelope to the left." , Corners: IGraphics::CORNER_L)) |
| 248 | { |
| 249 | int MoveTo = Map()->m_SelectedEnvelope - 1; |
| 250 | int MoveFrom = Map()->m_SelectedEnvelope; |
| 251 | Map()->m_SelectedEnvelope = Map()->MoveEnvelope(IndexFrom: MoveFrom, IndexTo: MoveTo); |
| 252 | if(Map()->m_SelectedEnvelope != MoveFrom) |
| 253 | { |
| 254 | Map()->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEdit>(args: Map(), args&: Map()->m_SelectedEnvelope, args: CEditorActionEnvelopeEdit::EEditType::ORDER, args&: MoveFrom, args&: Map()->m_SelectedEnvelope)); |
| 255 | pEnvelope = Map()->m_vpEnvelopes[Map()->m_SelectedEnvelope]; |
| 256 | Map()->OnModify(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if(pEnvelope) |
| 261 | { |
| 262 | ToolBar.VSplitRight(Cut: 5.0f, pLeft: &ToolBar, pRight: nullptr); |
| 263 | ToolBar.VSplitRight(Cut: 20.0f, pLeft: &ToolBar, pRight: &Button); |
| 264 | if(Editor()->DoButton_FontIcon(pId: &m_ZoomOutButtonId, pText: FontIcon::MINUS, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "[NumPad-] Zoom out horizontally, hold shift to zoom vertically." , Corners: IGraphics::CORNER_R, FontSize: 9.0f)) |
| 265 | { |
| 266 | if(Input()->ShiftIsPressed()) |
| 267 | State.m_ZoomY.ScaleValue(Factor: 1.1f); |
| 268 | else |
| 269 | State.m_ZoomX.ScaleValue(Factor: 1.1f); |
| 270 | } |
| 271 | |
| 272 | ToolBar.VSplitRight(Cut: 20.0f, pLeft: &ToolBar, pRight: &Button); |
| 273 | if(Editor()->DoButton_FontIcon(pId: &m_ResetZoomButtonId, pText: FontIcon::MAGNIFYING_GLASS, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "[NumPad*] Reset zoom to default value." , Corners: IGraphics::CORNER_NONE, FontSize: 9.0f)) |
| 274 | ResetZoomEnvelope(pEnvelope, ActiveChannels: State.m_ActiveChannels); |
| 275 | |
| 276 | ToolBar.VSplitRight(Cut: 20.0f, pLeft: &ToolBar, pRight: &Button); |
| 277 | if(Editor()->DoButton_FontIcon(pId: &m_ZoomInButtonId, pText: FontIcon::PLUS, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "[NumPad+] Zoom in horizontally, hold shift to zoom vertically." , Corners: IGraphics::CORNER_L, FontSize: 9.0f)) |
| 278 | { |
| 279 | if(Input()->ShiftIsPressed()) |
| 280 | State.m_ZoomY.ScaleValue(Factor: 1.0f / 1.1f); |
| 281 | else |
| 282 | State.m_ZoomX.ScaleValue(Factor: 1.0f / 1.1f); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // Margin on the right side |
| 287 | ToolBar.VSplitRight(Cut: 7.0f, pLeft: &ToolBar, pRight: nullptr); |
| 288 | } |
| 289 | |
| 290 | CUIRect Shifter, Inc, Dec; |
| 291 | ToolBar.VSplitLeft(Cut: 60.0f, pLeft: &Shifter, pRight: &ToolBar); |
| 292 | Shifter.VSplitRight(Cut: 15.0f, pLeft: &Shifter, pRight: &Inc); |
| 293 | Shifter.VSplitLeft(Cut: 15.0f, pLeft: &Dec, pRight: &Shifter); |
| 294 | char aBuf[64]; |
| 295 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d/%d" , Map()->m_SelectedEnvelope + 1, (int)Map()->m_vpEnvelopes.size()); |
| 296 | |
| 297 | ColorRGBA EnvColor = ColorRGBA(1, 1, 1, 0.5f); |
| 298 | if(!Map()->m_vpEnvelopes.empty()) |
| 299 | { |
| 300 | EnvColor = Map()->IsEnvelopeUsed(EnvelopeIndex: Map()->m_SelectedEnvelope) ? ColorRGBA(1, 0.7f, 0.7f, 0.5f) : ColorRGBA(0.7f, 1, 0.7f, 0.5f); |
| 301 | } |
| 302 | |
| 303 | auto NewValueRes = Editor()->UiDoValueSelector(pId: &m_EnvelopeSelectorId, pRect: &Shifter, pLabel: aBuf, Current: Map()->m_SelectedEnvelope + 1, Min: 1, Max: Map()->m_vpEnvelopes.size(), Step: 1, Scale: 1.0f, pToolTip: "Select the envelope." , IsDegree: false, IsHex: false, Corners: IGraphics::CORNER_NONE, pColor: &EnvColor, ShowValue: false); |
| 304 | int NewValue = NewValueRes.m_Value; |
| 305 | if(NewValue - 1 != Map()->m_SelectedEnvelope) |
| 306 | { |
| 307 | Map()->m_SelectedEnvelope = NewValue - 1; |
| 308 | CurrentEnvelopeSwitched = true; |
| 309 | } |
| 310 | |
| 311 | if(Editor()->DoButton_FontIcon(pId: &m_PrevEnvelopeButtonId, pText: FontIcon::MINUS, Checked: 0, pRect: &Dec, Flags: BUTTONFLAG_LEFT, pToolTip: "Select previous envelope." , Corners: IGraphics::CORNER_L, FontSize: 7.0f)) |
| 312 | { |
| 313 | Map()->m_SelectedEnvelope--; |
| 314 | if(Map()->m_SelectedEnvelope < 0) |
| 315 | Map()->m_SelectedEnvelope = Map()->m_vpEnvelopes.size() - 1; |
| 316 | CurrentEnvelopeSwitched = true; |
| 317 | } |
| 318 | |
| 319 | if(Editor()->DoButton_FontIcon(pId: &m_NextEnvelopeButtonId, pText: FontIcon::PLUS, Checked: 0, pRect: &Inc, Flags: BUTTONFLAG_LEFT, pToolTip: "Select next envelope." , Corners: IGraphics::CORNER_R, FontSize: 7.0f)) |
| 320 | { |
| 321 | Map()->m_SelectedEnvelope++; |
| 322 | if(Map()->m_SelectedEnvelope >= (int)Map()->m_vpEnvelopes.size()) |
| 323 | Map()->m_SelectedEnvelope = 0; |
| 324 | CurrentEnvelopeSwitched = true; |
| 325 | } |
| 326 | |
| 327 | if(pEnvelope) |
| 328 | { |
| 329 | ToolBar.VSplitLeft(Cut: 15.0f, pLeft: nullptr, pRight: &ToolBar); |
| 330 | ToolBar.VSplitLeft(Cut: 40.0f, pLeft: &Button, pRight: &ToolBar); |
| 331 | Ui()->DoLabel(pRect: &Button, pText: "Name:" , Size: 10.0f, Align: TEXTALIGN_MR); |
| 332 | |
| 333 | ToolBar.VSplitLeft(Cut: 3.0f, pLeft: nullptr, pRight: &ToolBar); |
| 334 | ToolBar.VSplitLeft(Cut: ToolBar.w > ToolBar.h * 40 ? 80.0f : 60.0f, pLeft: &Button, pRight: &ToolBar); |
| 335 | |
| 336 | m_NameInput.SetBuffer(pStr: pEnvelope->m_aName, MaxSize: sizeof(pEnvelope->m_aName)); |
| 337 | if(Editor()->DoEditBox(pLineInput: &m_NameInput, pRect: &Button, FontSize: 10.0f, Corners: IGraphics::CORNER_ALL, pToolTip: "The name of the selected envelope." )) |
| 338 | { |
| 339 | Map()->OnModify(); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | const bool ShowColorBar = pEnvelope && pEnvelope->GetChannels() == 4; |
| 345 | if(ShowColorBar) |
| 346 | { |
| 347 | View.HSplitTop(Cut: 20.0f, pTop: &ColorBar, pBottom: &View); |
| 348 | ColorBar.HMargin(Cut: 2.0f, pOtherRect: &ColorBar); |
| 349 | } |
| 350 | |
| 351 | Editor()->RenderBackground(View, Texture: Editor()->m_CheckerTexture, Size: 32.0f, Brightness: 0.1f); |
| 352 | |
| 353 | if(pEnvelope) |
| 354 | { |
| 355 | if(State.m_ResetZoom) |
| 356 | { |
| 357 | State.m_ResetZoom = false; |
| 358 | ResetZoomEnvelope(pEnvelope, ActiveChannels: State.m_ActiveChannels); |
| 359 | } |
| 360 | |
| 361 | ColorRGBA aColors[] = {ColorRGBA(1, 0.2f, 0.2f), ColorRGBA(0.2f, 1, 0.2f), ColorRGBA(0.2f, 0.2f, 1), ColorRGBA(1, 1, 0.2f)}; |
| 362 | |
| 363 | CUIRect Button; |
| 364 | ToolBar.VSplitLeft(Cut: 15.0f, pLeft: &Button, pRight: &ToolBar); |
| 365 | |
| 366 | static const char *const CHANNEL_NAMES[CEnvPoint::MAX_CHANNELS][CEnvPoint::MAX_CHANNELS] = { |
| 367 | {"V" , "" , "" , "" }, |
| 368 | {"" , "" , "" , "" }, |
| 369 | {"X" , "Y" , "R" , "" }, |
| 370 | {"R" , "G" , "B" , "A" }, |
| 371 | }; |
| 372 | |
| 373 | static const char *const CHANNEL_DESCRIPTIONS[CEnvPoint::MAX_CHANNELS][CEnvPoint::MAX_CHANNELS] = { |
| 374 | {"Volume of the envelope." , "" , "" , "" }, |
| 375 | {"" , "" , "" , "" }, |
| 376 | {"X-axis of the envelope." , "Y-axis of the envelope." , "Rotation of the envelope." , "" }, |
| 377 | {"Red value of the envelope." , "Green value of the envelope." , "Blue value of the envelope." , "Alpha value of the envelope." }, |
| 378 | }; |
| 379 | |
| 380 | int Bit = 1; |
| 381 | for(int i = 0; i < CEnvPoint::MAX_CHANNELS; i++, Bit <<= 1) |
| 382 | { |
| 383 | ToolBar.VSplitLeft(Cut: 15.0f, pLeft: &Button, pRight: &ToolBar); |
| 384 | if(i < pEnvelope->GetChannels()) |
| 385 | { |
| 386 | int Corners = IGraphics::CORNER_NONE; |
| 387 | if(pEnvelope->GetChannels() == 1) |
| 388 | Corners = IGraphics::CORNER_ALL; |
| 389 | else if(i == 0) |
| 390 | Corners = IGraphics::CORNER_L; |
| 391 | else if(i == pEnvelope->GetChannels() - 1) |
| 392 | Corners = IGraphics::CORNER_R; |
| 393 | |
| 394 | if(Editor()->DoButton_Env(pId: &m_aChannelButtonIds[i], pText: CHANNEL_NAMES[pEnvelope->GetChannels() - 1][i], Checked: State.m_ActiveChannels & Bit, pRect: &Button, pToolTip: CHANNEL_DESCRIPTIONS[pEnvelope->GetChannels() - 1][i], Color: aColors[i], Corners)) |
| 395 | State.m_ActiveChannels ^= Bit; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | ToolBar.VSplitLeft(Cut: 15.0f, pLeft: nullptr, pRight: &ToolBar); |
| 400 | ToolBar.VSplitLeft(Cut: 40.0f, pLeft: &Button, pRight: &ToolBar); |
| 401 | |
| 402 | const bool ShouldPan = m_Operation == EEnvelopeEditorOp::NONE && (Ui()->MouseButton(Index: 2) || (Ui()->MouseButton(Index: 0) && Input()->ModifierIsPressed())); |
| 403 | if(Editor()->m_pContainerPanned == &m_EnvelopeEditorId) |
| 404 | { |
| 405 | if(!ShouldPan) |
| 406 | Editor()->m_pContainerPanned = nullptr; |
| 407 | else |
| 408 | { |
| 409 | State.m_Offset.x += Ui()->MouseDeltaX() / Graphics()->ScreenWidth() * Ui()->Screen()->w / View.w; |
| 410 | State.m_Offset.y -= Ui()->MouseDeltaY() / Graphics()->ScreenHeight() * Ui()->Screen()->h / View.h; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if(Ui()->MouseInside(pRect: &View) && Editor()->m_Dialog == DIALOG_NONE) |
| 415 | { |
| 416 | Ui()->SetHotItem(&m_EnvelopeEditorId); |
| 417 | |
| 418 | if(ShouldPan && Editor()->m_pContainerPanned == nullptr) |
| 419 | Editor()->m_pContainerPanned = &m_EnvelopeEditorId; |
| 420 | |
| 421 | if(Input()->KeyPress(Key: KEY_KP_MULTIPLY) && CLineInput::GetActiveInput() == nullptr) |
| 422 | ResetZoomEnvelope(pEnvelope, ActiveChannels: State.m_ActiveChannels); |
| 423 | if(Input()->ShiftIsPressed()) |
| 424 | { |
| 425 | if(Input()->KeyPress(Key: KEY_KP_MINUS) && CLineInput::GetActiveInput() == nullptr) |
| 426 | State.m_ZoomY.ScaleValue(Factor: 1.1f); |
| 427 | if(Input()->KeyPress(Key: KEY_KP_PLUS) && CLineInput::GetActiveInput() == nullptr) |
| 428 | State.m_ZoomY.ScaleValue(Factor: 1.0f / 1.1f); |
| 429 | if(Input()->KeyPress(Key: KEY_MOUSE_WHEEL_DOWN)) |
| 430 | State.m_ZoomY.ScaleValue(Factor: 1.1f); |
| 431 | if(Input()->KeyPress(Key: KEY_MOUSE_WHEEL_UP)) |
| 432 | State.m_ZoomY.ScaleValue(Factor: 1.0f / 1.1f); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | if(Input()->KeyPress(Key: KEY_KP_MINUS) && CLineInput::GetActiveInput() == nullptr) |
| 437 | State.m_ZoomX.ScaleValue(Factor: 1.1f); |
| 438 | if(Input()->KeyPress(Key: KEY_KP_PLUS) && CLineInput::GetActiveInput() == nullptr) |
| 439 | State.m_ZoomX.ScaleValue(Factor: 1.0f / 1.1f); |
| 440 | if(Input()->KeyPress(Key: KEY_MOUSE_WHEEL_DOWN)) |
| 441 | State.m_ZoomX.ScaleValue(Factor: 1.1f); |
| 442 | if(Input()->KeyPress(Key: KEY_MOUSE_WHEEL_UP)) |
| 443 | State.m_ZoomX.ScaleValue(Factor: 1.0f / 1.1f); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if(Ui()->HotItem() == &m_EnvelopeEditorId) |
| 448 | { |
| 449 | // do stuff |
| 450 | if(Ui()->MouseButton(Index: 0)) |
| 451 | { |
| 452 | m_EnvelopeEditorButtonUsed = 0; |
| 453 | if(m_Operation != EEnvelopeEditorOp::BOX_SELECT && !Input()->ModifierIsPressed()) |
| 454 | { |
| 455 | m_Operation = EEnvelopeEditorOp::BOX_SELECT; |
| 456 | m_MouseStart = Ui()->MousePos(); |
| 457 | } |
| 458 | } |
| 459 | else if(m_EnvelopeEditorButtonUsed == 0) |
| 460 | { |
| 461 | if(Ui()->DoDoubleClickLogic(pId: &m_EnvelopeEditorId) && !Input()->ModifierIsPressed()) |
| 462 | { |
| 463 | // add point |
| 464 | float Time = ScreenToEnvelopeX(View, x: Ui()->MouseX()); |
| 465 | ColorRGBA Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 466 | pEnvelope->Eval(Time: std::clamp(val: Time, lo: 0.0f, hi: pEnvelope->EndTime()), Result&: Channels, Channels: 4); |
| 467 | |
| 468 | const CFixedTime FixedTime = CFixedTime::FromSeconds(Seconds: Time); |
| 469 | bool TimeFound = false; |
| 470 | for(CEnvPoint &Point : pEnvelope->m_vPoints) |
| 471 | { |
| 472 | if(Point.m_Time == FixedTime) |
| 473 | TimeFound = true; |
| 474 | } |
| 475 | |
| 476 | if(!TimeFound) |
| 477 | Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionAddEnvelopePoint>(args: Map(), args&: Map()->m_SelectedEnvelope, args: FixedTime, args&: Channels)); |
| 478 | |
| 479 | if(FixedTime < CFixedTime(0)) |
| 480 | RemoveTimeOffsetEnvelope(pEnvelope); |
| 481 | Map()->OnModify(); |
| 482 | } |
| 483 | m_EnvelopeEditorButtonUsed = -1; |
| 484 | } |
| 485 | |
| 486 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 487 | str_copy(dst&: Editor()->m_aTooltip, src: "Double click to create a new point. Use shift to change the zoom axis. Press S to scale selected envelope points." ); |
| 488 | } |
| 489 | |
| 490 | UpdateZoomEnvelopeX(View); |
| 491 | UpdateZoomEnvelopeY(View); |
| 492 | |
| 493 | { |
| 494 | float UnitsPerLineY = 0.001f; |
| 495 | static const float UNIT_PER_LINE_OPTIONS_Y[] = {0.005f, 0.01f, 0.025f, 0.05f, 0.1f, 0.25f, 0.5f, 1.0f, 2.0f, 4.0f, 8.0f, 16.0f, 32.0f, 2 * 32.0f, 5 * 32.0f, 10 * 32.0f, 20 * 32.0f, 50 * 32.0f, 100 * 32.0f}; |
| 496 | for(float Value : UNIT_PER_LINE_OPTIONS_Y) |
| 497 | { |
| 498 | if(Value / State.m_ZoomY.GetValue() * View.h < 40.0f) |
| 499 | UnitsPerLineY = Value; |
| 500 | } |
| 501 | int NumLinesY = State.m_ZoomY.GetValue() / UnitsPerLineY + 1; |
| 502 | |
| 503 | Ui()->ClipEnable(pRect: &View); |
| 504 | Graphics()->TextureClear(); |
| 505 | Graphics()->LinesBegin(); |
| 506 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.2f); |
| 507 | |
| 508 | float BaseValue = static_cast<int>(State.m_Offset.y * State.m_ZoomY.GetValue() / UnitsPerLineY) * UnitsPerLineY; |
| 509 | for(int i = 0; i <= NumLinesY; i++) |
| 510 | { |
| 511 | float Value = UnitsPerLineY * i - BaseValue; |
| 512 | IGraphics::CLineItem LineItem(View.x, EnvelopeToScreenY(View, y: Value), View.x + View.w, EnvelopeToScreenY(View, y: Value)); |
| 513 | Graphics()->LinesDraw(pArray: &LineItem, Num: 1); |
| 514 | } |
| 515 | |
| 516 | Graphics()->LinesEnd(); |
| 517 | |
| 518 | Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f); |
| 519 | for(int i = 0; i <= NumLinesY; i++) |
| 520 | { |
| 521 | float Value = UnitsPerLineY * i - BaseValue; |
| 522 | char aValueBuffer[16]; |
| 523 | if(UnitsPerLineY >= 1.0f) |
| 524 | { |
| 525 | str_format(buffer: aValueBuffer, buffer_size: sizeof(aValueBuffer), format: "%d" , static_cast<int>(Value)); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | str_format(buffer: aValueBuffer, buffer_size: sizeof(aValueBuffer), format: "%.3f" , Value); |
| 530 | } |
| 531 | Ui()->TextRender()->Text(x: View.x, y: EnvelopeToScreenY(View, y: Value) + 4.0f, Size: 8.0f, pText: aValueBuffer); |
| 532 | } |
| 533 | Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f); |
| 534 | Ui()->ClipDisable(); |
| 535 | } |
| 536 | |
| 537 | // handle time bar |
| 538 | { |
| 539 | if(m_Operation == EEnvelopeEditorOp::NONE) |
| 540 | { |
| 541 | UpdateHotEnvelopeObject(View, pEnvelope: pEnvelope.get(), ActiveChannels: State.m_ActiveChannels); |
| 542 | } |
| 543 | |
| 544 | ColorRGBA BarColor; |
| 545 | if(Ui()->CheckActiveItem(pId: &Map()->m_EnvelopeEvaluator.m_AnimateTime)) |
| 546 | { |
| 547 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 548 | { |
| 549 | if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f) |
| 550 | { |
| 551 | m_Operation = EEnvelopeEditorOp::DRAG_TIME_BAR; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if(m_Operation == EEnvelopeEditorOp::DRAG_TIME_BAR) |
| 556 | { |
| 557 | float DeltaX = ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * (Input()->ModifierIsPressed() ? 0.05f : 1.0f); |
| 558 | Map()->m_EnvelopeEvaluator.m_AnimateTime += DeltaX / Map()->m_EnvelopeEvaluator.m_AnimateSpeed; |
| 559 | Map()->m_EnvelopeEvaluator.m_AnimateTime = std::max(a: Map()->m_EnvelopeEvaluator.m_AnimateTime, b: 0.0f); |
| 560 | } |
| 561 | |
| 562 | if(!Ui()->MouseButton(Index: 0)) |
| 563 | { |
| 564 | Ui()->SetActiveItem(nullptr); |
| 565 | m_Operation = EEnvelopeEditorOp::NONE; |
| 566 | } |
| 567 | |
| 568 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 569 | BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.8f); |
| 570 | str_copy(dst&: Editor()->m_aTooltip, src: "Timebar. Press left-click to drag. Hold ctrl to be more precise." ); |
| 571 | } |
| 572 | else if(Ui()->HotItem() == &Map()->m_EnvelopeEvaluator.m_AnimateTime) |
| 573 | { |
| 574 | if(Ui()->MouseButton(Index: 0)) |
| 575 | { |
| 576 | Ui()->SetActiveItem(&Map()->m_EnvelopeEvaluator.m_AnimateTime); |
| 577 | m_Operation = EEnvelopeEditorOp::SELECT; |
| 578 | m_MouseStart = Ui()->MousePos(); |
| 579 | } |
| 580 | |
| 581 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 582 | BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.8f); |
| 583 | str_copy(dst&: Editor()->m_aTooltip, src: "Timebar. Press left-click to drag. Hold ctrl to be more precise." ); |
| 584 | } |
| 585 | else |
| 586 | BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.5f); |
| 587 | |
| 588 | Ui()->ClipEnable(pRect: &View); |
| 589 | float Time = Map()->m_EnvelopeEvaluator.m_AnimateTime * Map()->m_EnvelopeEvaluator.m_AnimateSpeed; |
| 590 | const float BarWidth = 1.5f; |
| 591 | CUIRect TimeBar{ |
| 592 | .x: EnvelopeToScreenX(View, x: Time) - BarWidth / 2.0f, |
| 593 | .y: View.y, |
| 594 | .w: BarWidth, |
| 595 | .h: View.h, |
| 596 | }; |
| 597 | TimeBar.Draw(Color: BarColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f); |
| 598 | |
| 599 | if(Time > pEnvelope->EndTime()) |
| 600 | { |
| 601 | float LoopedTime = std::fmod(x: Time, y: pEnvelope->EndTime()); |
| 602 | TimeBar.x = EnvelopeToScreenX(View, x: LoopedTime) - BarWidth / 2.0f; |
| 603 | TimeBar.Draw(Color: BarColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f); |
| 604 | } |
| 605 | Ui()->ClipDisable(); |
| 606 | } |
| 607 | |
| 608 | { |
| 609 | using namespace std::chrono_literals; |
| 610 | CTimeStep UnitsPerLineX = 1ms; |
| 611 | static const CTimeStep UNIT_PER_LINE_OPTIONS_X[] = {5ms, 10ms, 25ms, 50ms, 100ms, 250ms, 500ms, 1s, 2s, 5s, 10s, 15s, 30s, 1min}; |
| 612 | for(CTimeStep Value : UNIT_PER_LINE_OPTIONS_X) |
| 613 | { |
| 614 | if(Value.AsSeconds() / State.m_ZoomX.GetValue() * View.w < 160.0f) |
| 615 | UnitsPerLineX = Value; |
| 616 | } |
| 617 | int NumLinesX = State.m_ZoomX.GetValue() / UnitsPerLineX.AsSeconds() + 1; |
| 618 | |
| 619 | Ui()->ClipEnable(pRect: &View); |
| 620 | Graphics()->TextureClear(); |
| 621 | Graphics()->LinesBegin(); |
| 622 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.2f); |
| 623 | |
| 624 | CTimeStep BaseValue = UnitsPerLineX * static_cast<int>(State.m_Offset.x * State.m_ZoomX.GetValue() / UnitsPerLineX.AsSeconds()); |
| 625 | for(int i = 0; i <= NumLinesX; i++) |
| 626 | { |
| 627 | float Value = UnitsPerLineX.AsSeconds() * i - BaseValue.AsSeconds(); |
| 628 | IGraphics::CLineItem LineItem(EnvelopeToScreenX(View, x: Value), View.y, EnvelopeToScreenX(View, x: Value), View.y + View.h); |
| 629 | Graphics()->LinesDraw(pArray: &LineItem, Num: 1); |
| 630 | } |
| 631 | |
| 632 | Graphics()->LinesEnd(); |
| 633 | |
| 634 | Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f); |
| 635 | for(int i = 0; i <= NumLinesX; i++) |
| 636 | { |
| 637 | CTimeStep Value = UnitsPerLineX * i - BaseValue; |
| 638 | if(Value.AsSeconds() >= 0) |
| 639 | { |
| 640 | char aValueBuffer[16]; |
| 641 | Value.Format(pBuffer: aValueBuffer, BufferSize: sizeof(aValueBuffer)); |
| 642 | |
| 643 | Ui()->TextRender()->Text(x: EnvelopeToScreenX(View, x: Value.AsSeconds()) + 1.0f, y: View.y + View.h - 8.0f, Size: 8.0f, pText: aValueBuffer); |
| 644 | } |
| 645 | } |
| 646 | Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f); |
| 647 | Ui()->ClipDisable(); |
| 648 | } |
| 649 | |
| 650 | // render lines |
| 651 | { |
| 652 | float EndX = View.x + View.w; |
| 653 | float StartX = std::clamp(val: View.x + View.w * State.m_Offset.x, lo: View.x, hi: View.x + View.w); |
| 654 | |
| 655 | float EndTime = ScreenToEnvelopeX(View, x: EndX); |
| 656 | float StartTime = ScreenToEnvelopeX(View, x: StartX); |
| 657 | |
| 658 | Ui()->ClipEnable(pRect: &View); |
| 659 | Graphics()->TextureClear(); |
| 660 | IGraphics::CLineItemBatch LineItemBatch; |
| 661 | for(int c = 0; c < pEnvelope->GetChannels(); c++) |
| 662 | { |
| 663 | Graphics()->LinesBatchBegin(pBatch: &LineItemBatch); |
| 664 | if(State.m_ActiveChannels & (1 << c)) |
| 665 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1); |
| 666 | else |
| 667 | Graphics()->SetColor(r: aColors[c].r * 0.5f, g: aColors[c].g * 0.5f, b: aColors[c].b * 0.5f, a: 1); |
| 668 | |
| 669 | const int Steps = static_cast<int>(((EndX - StartX) / Ui()->Screen()->w) * Graphics()->ScreenWidth()); |
| 670 | const float StepTime = (EndTime - StartTime) / static_cast<float>(Steps); |
| 671 | const float StepSize = (EndX - StartX) / static_cast<float>(Steps); |
| 672 | |
| 673 | ColorRGBA Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 674 | pEnvelope->Eval(Time: StartTime, Result&: Channels, Channels: c + 1); |
| 675 | float PrevTime = StartTime; |
| 676 | float PrevX = StartX; |
| 677 | float PrevY = EnvelopeToScreenY(View, y: Channels[c]); |
| 678 | for(int Step = 1; Step <= Steps; Step++) |
| 679 | { |
| 680 | float CurrentTime = StartTime + Step * StepTime; |
| 681 | if(CurrentTime >= EndTime) |
| 682 | { |
| 683 | CurrentTime = EndTime - 0.001f; |
| 684 | if(CurrentTime <= PrevTime) |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 689 | pEnvelope->Eval(Time: CurrentTime, Result&: Channels, Channels: c + 1); |
| 690 | const float CurrentX = StartX + Step * StepSize; |
| 691 | const float CurrentY = EnvelopeToScreenY(View, y: Channels[c]); |
| 692 | |
| 693 | const IGraphics::CLineItem Item = IGraphics::CLineItem(PrevX, PrevY, CurrentX, CurrentY); |
| 694 | Graphics()->LinesBatchDraw(pBatch: &LineItemBatch, pArray: &Item, Num: 1); |
| 695 | |
| 696 | PrevTime = CurrentTime; |
| 697 | PrevX = CurrentX; |
| 698 | PrevY = CurrentY; |
| 699 | } |
| 700 | Graphics()->LinesBatchEnd(pBatch: &LineItemBatch); |
| 701 | } |
| 702 | Ui()->ClipDisable(); |
| 703 | } |
| 704 | |
| 705 | CUIRect InactiveRegionLeft{ |
| 706 | .x: View.x, |
| 707 | .y: View.y, |
| 708 | .w: std::clamp(val: EnvelopeToScreenX(View, x: 0.0f) - View.x, lo: 0.0f, hi: View.w), |
| 709 | .h: View.h, |
| 710 | }; |
| 711 | const float EndX = EnvelopeToScreenX(View, x: pEnvelope->EndTime()); |
| 712 | CUIRect InactiveRegionRight{ |
| 713 | .x: std::max(a: View.x, b: EndX), |
| 714 | .y: View.y, |
| 715 | .w: std::clamp(val: View.x + View.w - EndX, lo: 0.0f, hi: View.w), |
| 716 | .h: View.h, |
| 717 | }; |
| 718 | InactiveRegionLeft.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f); |
| 719 | InactiveRegionRight.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f); |
| 720 | |
| 721 | // render tangents for bezier curves |
| 722 | { |
| 723 | Ui()->ClipEnable(pRect: &View); |
| 724 | Graphics()->TextureClear(); |
| 725 | Graphics()->LinesBegin(); |
| 726 | for(int c = 0; c < pEnvelope->GetChannels(); c++) |
| 727 | { |
| 728 | if(!(State.m_ActiveChannels & (1 << c))) |
| 729 | continue; |
| 730 | |
| 731 | for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++) |
| 732 | { |
| 733 | float PosX = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds()); |
| 734 | float PosY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c])); |
| 735 | |
| 736 | // Out-Tangent |
| 737 | if(i < (int)pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER) |
| 738 | { |
| 739 | float TangentX = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds()); |
| 740 | float TangentY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c])); |
| 741 | |
| 742 | if(Map()->IsTangentOutPointSelected(Index: i, Channel: c)) |
| 743 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f); |
| 744 | else |
| 745 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 0.4f); |
| 746 | |
| 747 | IGraphics::CLineItem LineItem(TangentX, TangentY, PosX, PosY); |
| 748 | Graphics()->LinesDraw(pArray: &LineItem, Num: 1); |
| 749 | } |
| 750 | |
| 751 | // In-Tangent |
| 752 | if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER) |
| 753 | { |
| 754 | float TangentX = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds()); |
| 755 | float TangentY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c])); |
| 756 | |
| 757 | if(Map()->IsTangentInPointSelected(Index: i, Channel: c)) |
| 758 | Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f); |
| 759 | else |
| 760 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 0.4f); |
| 761 | |
| 762 | IGraphics::CLineItem LineItem(TangentX, TangentY, PosX, PosY); |
| 763 | Graphics()->LinesDraw(pArray: &LineItem, Num: 1); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | Graphics()->LinesEnd(); |
| 768 | Ui()->ClipDisable(); |
| 769 | } |
| 770 | |
| 771 | // render curve options |
| 772 | { |
| 773 | for(int i = 0; i < (int)pEnvelope->m_vPoints.size() - 1; i++) |
| 774 | { |
| 775 | float t0 = pEnvelope->m_vPoints[i].m_Time.AsSeconds(); |
| 776 | float t1 = pEnvelope->m_vPoints[i + 1].m_Time.AsSeconds(); |
| 777 | |
| 778 | CUIRect CurveButton; |
| 779 | CurveButton.x = EnvelopeToScreenX(View, x: t0 + (t1 - t0) * 0.5f); |
| 780 | CurveButton.y = CurveBar.y; |
| 781 | CurveButton.h = CurveBar.h; |
| 782 | CurveButton.w = CurveBar.h; |
| 783 | CurveButton.x -= CurveButton.w / 2.0f; |
| 784 | const void *pId = &pEnvelope->m_vPoints[i].m_Curvetype; |
| 785 | |
| 786 | if(CurveButton.x >= View.x) |
| 787 | { |
| 788 | const int ButtonResult = Editor()->DoButton_Editor(pId, pText: CurveTypeNameShort(CurveType: pEnvelope->m_vPoints[i].m_Curvetype), Checked: 0, pRect: &CurveButton, Flags: BUTTONFLAG_LEFT | BUTTONFLAG_RIGHT, pToolTip: "Switch curve type (N = step, L = linear, S = slow, F = fast, M = smooth, B = bezier)." ); |
| 789 | if(ButtonResult == 1) |
| 790 | { |
| 791 | const int PrevCurve = pEnvelope->m_vPoints[i].m_Curvetype; |
| 792 | const int Direction = Input()->ShiftIsPressed() ? -1 : 1; |
| 793 | pEnvelope->m_vPoints[i].m_Curvetype = (pEnvelope->m_vPoints[i].m_Curvetype + Direction + NUM_CURVETYPES) % NUM_CURVETYPES; |
| 794 | |
| 795 | Map()->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEditPoint>(args: Map(), |
| 796 | args&: Map()->m_SelectedEnvelope, args&: i, args: 0, args: CEditorActionEnvelopeEditPoint::EEditType::CURVE_TYPE, args: PrevCurve, args&: pEnvelope->m_vPoints[i].m_Curvetype)); |
| 797 | Map()->OnModify(); |
| 798 | } |
| 799 | else if(ButtonResult == 2) |
| 800 | { |
| 801 | m_PopupEnvelopeCurveType.m_pEnvelopeEditor = this; |
| 802 | m_PopupEnvelopeCurveType.m_SelectedPoint = i; |
| 803 | Ui()->DoPopupMenu(pId: &m_PopupEnvelopeCurveType, X: Ui()->MouseX(), Y: Ui()->MouseY(), Width: 80, Height: (float)NUM_CURVETYPES * 14.0f + 10.0f, pContext: &m_PopupEnvelopeCurveType, pfnFunc: CPopupEnvelopeCurveType::Render); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // render colorbar |
| 810 | if(ShowColorBar) |
| 811 | { |
| 812 | RenderColorBar(ColorBar, pEnvelope); |
| 813 | } |
| 814 | |
| 815 | // render handles |
| 816 | if(CurrentEnvelopeSwitched) |
| 817 | { |
| 818 | Map()->DeselectEnvPoints(); |
| 819 | State.m_ResetZoom = true; |
| 820 | } |
| 821 | |
| 822 | { |
| 823 | const auto && = [&]() { |
| 824 | m_PopupEnvelopePoint.m_pEnvelopeEditor = this; |
| 825 | Ui()->DoPopupMenu(pId: &m_PopupEnvelopePoint, X: Ui()->MouseX(), Y: Ui()->MouseY(), Width: 150, Height: 56 + (pEnvelope->GetChannels() == 4 && !Map()->IsTangentSelected() ? 16.0f : 0.0f), pContext: &m_PopupEnvelopePoint, pfnFunc: CPopupEnvelopePoint::Render); |
| 826 | }; |
| 827 | |
| 828 | if(m_Operation == EEnvelopeEditorOp::NONE) |
| 829 | { |
| 830 | UpdateHotEnvelopeObject(View, pEnvelope: pEnvelope.get(), ActiveChannels: State.m_ActiveChannels); |
| 831 | if(!Ui()->MouseButton(Index: 0)) |
| 832 | Map()->m_EnvOpTracker.Stop(Switch: false); |
| 833 | } |
| 834 | else |
| 835 | { |
| 836 | Map()->m_EnvOpTracker.Begin(Operation: m_Operation); |
| 837 | } |
| 838 | |
| 839 | Ui()->ClipEnable(pRect: &View); |
| 840 | Graphics()->TextureClear(); |
| 841 | Graphics()->QuadsBegin(); |
| 842 | for(int c = 0; c < pEnvelope->GetChannels(); c++) |
| 843 | { |
| 844 | if(!(State.m_ActiveChannels & (1 << c))) |
| 845 | continue; |
| 846 | |
| 847 | for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++) |
| 848 | { |
| 849 | // point handle |
| 850 | { |
| 851 | CUIRect Final; |
| 852 | Final.x = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds()); |
| 853 | Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c])); |
| 854 | Final.x -= 2.0f; |
| 855 | Final.y -= 2.0f; |
| 856 | Final.w = 4.0f; |
| 857 | Final.h = 4.0f; |
| 858 | |
| 859 | const void *pId = &pEnvelope->m_vPoints[i].m_aValues[c]; |
| 860 | |
| 861 | if(Map()->IsEnvPointSelected(Index: i, Channel: c)) |
| 862 | { |
| 863 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 864 | CUIRect Background = { |
| 865 | .x: Final.x - 0.2f * Final.w, |
| 866 | .y: Final.y - 0.2f * Final.h, |
| 867 | .w: Final.w * 1.4f, |
| 868 | .h: Final.h * 1.4f}; |
| 869 | IGraphics::CQuadItem QuadItem(Background.x, Background.y, Background.w, Background.h); |
| 870 | Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1); |
| 871 | } |
| 872 | |
| 873 | if(Ui()->CheckActiveItem(pId)) |
| 874 | { |
| 875 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 876 | |
| 877 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 878 | { |
| 879 | if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f) |
| 880 | { |
| 881 | m_Operation = EEnvelopeEditorOp::DRAG_POINT; |
| 882 | |
| 883 | if(!Map()->IsEnvPointSelected(Index: i, Channel: c)) |
| 884 | Map()->SelectEnvPoint(Index: i, Channel: c); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_X || m_Operation == EEnvelopeEditorOp::DRAG_POINT_Y) |
| 889 | { |
| 890 | if(Input()->ShiftIsPressed()) |
| 891 | { |
| 892 | if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_Y) |
| 893 | { |
| 894 | m_Operation = EEnvelopeEditorOp::DRAG_POINT_X; |
| 895 | m_vAccurateDragValuesX.clear(); |
| 896 | for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints) |
| 897 | m_vAccurateDragValuesX.push_back(x: pEnvelope->m_vPoints[SelectedIndex].m_Time.GetInternal()); |
| 898 | } |
| 899 | else |
| 900 | { |
| 901 | float DeltaX = ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * (Input()->ModifierIsPressed() ? 50.0f : 1000.0f); |
| 902 | |
| 903 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 904 | { |
| 905 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 906 | CFixedTime BoundLow = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x)); |
| 907 | CFixedTime BoundHigh = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w)); |
| 908 | for(int j = 0; j < SelectedIndex; j++) |
| 909 | { |
| 910 | if(!Map()->IsEnvPointSelected(Index: j)) |
| 911 | BoundLow = std::max(a: pEnvelope->m_vPoints[j].m_Time + CFixedTime(1), b: BoundLow); |
| 912 | } |
| 913 | for(int j = SelectedIndex + 1; j < (int)pEnvelope->m_vPoints.size(); j++) |
| 914 | { |
| 915 | if(!Map()->IsEnvPointSelected(Index: j)) |
| 916 | BoundHigh = std::min(a: pEnvelope->m_vPoints[j].m_Time - CFixedTime(1), b: BoundHigh); |
| 917 | } |
| 918 | |
| 919 | DeltaX = ClampDelta(Val: m_vAccurateDragValuesX[k], Delta: DeltaX, Min: BoundLow.GetInternal(), Max: BoundHigh.GetInternal()); |
| 920 | } |
| 921 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 922 | { |
| 923 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 924 | m_vAccurateDragValuesX[k] += DeltaX; |
| 925 | pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: m_vAccurateDragValuesX[k])); |
| 926 | } |
| 927 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 928 | { |
| 929 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 930 | if(SelectedIndex == 0 && pEnvelope->m_vPoints[SelectedIndex].m_Time != CFixedTime(0)) |
| 931 | { |
| 932 | RemoveTimeOffsetEnvelope(pEnvelope); |
| 933 | float Offset = m_vAccurateDragValuesX[k]; |
| 934 | for(auto &Value : m_vAccurateDragValuesX) |
| 935 | Value -= Offset; |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_X) |
| 944 | { |
| 945 | m_Operation = EEnvelopeEditorOp::DRAG_POINT_Y; |
| 946 | m_vAccurateDragValuesY.clear(); |
| 947 | for(auto [SelectedIndex, SelectedChannel] : Map()->m_vSelectedEnvelopePoints) |
| 948 | m_vAccurateDragValuesY.push_back(x: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel]); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | float DeltaY = ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * (Input()->ModifierIsPressed() ? 51.2f : 1024.0f); |
| 953 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 954 | { |
| 955 | auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k]; |
| 956 | m_vAccurateDragValuesY[k] -= DeltaY; |
| 957 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vAccurateDragValuesY[k]); |
| 958 | |
| 959 | if(pEnvelope->GetChannels() == 1 || pEnvelope->GetChannels() == 4) |
| 960 | { |
| 961 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::clamp(val: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel], lo: 0, hi: 1024); |
| 962 | m_vAccurateDragValuesY[k] = std::clamp<float>(val: m_vAccurateDragValuesY[k], lo: 0, hi: 1024); |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU) |
| 970 | { |
| 971 | if(!Ui()->MouseButton(Index: 1)) |
| 972 | { |
| 973 | if(Map()->m_vSelectedEnvelopePoints.size() == 1) |
| 974 | { |
| 975 | Map()->m_UpdateEnvPointInfo = true; |
| 976 | ShowPopupEnvPoint(); |
| 977 | } |
| 978 | else if(Map()->m_vSelectedEnvelopePoints.size() > 1) |
| 979 | { |
| 980 | m_PopupEnvelopePointMulti.m_pEnvelopeEditor = this; |
| 981 | Ui()->DoPopupMenu(pId: &m_PopupEnvelopePointMulti, X: Ui()->MouseX(), Y: Ui()->MouseY(), Width: 100, Height: 24, pContext: &m_PopupEnvelopePointMulti, pfnFunc: CPopupEnvelopePointMulti::Render); |
| 982 | } |
| 983 | Ui()->SetActiveItem(nullptr); |
| 984 | m_Operation = EEnvelopeEditorOp::NONE; |
| 985 | } |
| 986 | } |
| 987 | else if(!Ui()->MouseButton(Index: 0)) |
| 988 | { |
| 989 | Ui()->SetActiveItem(nullptr); |
| 990 | Map()->m_SelectedQuadEnvelope = -1; |
| 991 | |
| 992 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 993 | { |
| 994 | if(Input()->ShiftIsPressed()) |
| 995 | Map()->ToggleEnvPoint(Index: i, Channel: c); |
| 996 | else |
| 997 | Map()->SelectEnvPoint(Index: i, Channel: c); |
| 998 | } |
| 999 | |
| 1000 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1001 | Map()->OnModify(); |
| 1002 | } |
| 1003 | |
| 1004 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1005 | } |
| 1006 | else if(Ui()->HotItem() == pId) |
| 1007 | { |
| 1008 | if(Ui()->MouseButton(Index: 0)) |
| 1009 | { |
| 1010 | Ui()->SetActiveItem(pId); |
| 1011 | m_Operation = EEnvelopeEditorOp::SELECT; |
| 1012 | Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope; |
| 1013 | m_MouseStart = Ui()->MousePos(); |
| 1014 | } |
| 1015 | else if(Ui()->MouseButtonClicked(Index: 1)) |
| 1016 | { |
| 1017 | if(Input()->ShiftIsPressed()) |
| 1018 | { |
| 1019 | Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionDeleteEnvelopePoint>(args: Map(), args&: Map()->m_SelectedEnvelope, args&: i)); |
| 1020 | } |
| 1021 | else |
| 1022 | { |
| 1023 | m_Operation = EEnvelopeEditorOp::CONTEXT_MENU; |
| 1024 | if(!Map()->IsEnvPointSelected(Index: i, Channel: c)) |
| 1025 | Map()->SelectEnvPoint(Index: i, Channel: c); |
| 1026 | Ui()->SetActiveItem(pId); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1031 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1032 | str_copy(dst&: Editor()->m_aTooltip, src: "Envelope point. Left mouse to drag. Hold ctrl to be more precise. Hold shift to alter time. Shift+right click to delete." ); |
| 1033 | Editor()->m_pUiGotContext = pId; |
| 1034 | } |
| 1035 | else |
| 1036 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f); |
| 1037 | |
| 1038 | IGraphics::CQuadItem QuadItem(Final.x, Final.y, Final.w, Final.h); |
| 1039 | Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1); |
| 1040 | } |
| 1041 | |
| 1042 | // tangent handles for bezier curves |
| 1043 | if(i >= 0 && i < (int)pEnvelope->m_vPoints.size()) |
| 1044 | { |
| 1045 | // Out-Tangent handle |
| 1046 | if(i < (int)pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER) |
| 1047 | { |
| 1048 | CUIRect Final; |
| 1049 | Final.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds()); |
| 1050 | Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c])); |
| 1051 | Final.x -= 2.0f; |
| 1052 | Final.y -= 2.0f; |
| 1053 | Final.w = 4.0f; |
| 1054 | Final.h = 4.0f; |
| 1055 | |
| 1056 | // handle logic |
| 1057 | const void *pId = &pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]; |
| 1058 | |
| 1059 | if(Map()->IsTangentOutPointSelected(Index: i, Channel: c)) |
| 1060 | { |
| 1061 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1062 | IGraphics::CFreeformItem FreeformItem( |
| 1063 | Final.x + Final.w / 2.0f, |
| 1064 | Final.y - 1, |
| 1065 | Final.x + Final.w / 2.0f, |
| 1066 | Final.y - 1, |
| 1067 | Final.x + Final.w + 1, |
| 1068 | Final.y + Final.h + 1, |
| 1069 | Final.x - 1, |
| 1070 | Final.y + Final.h + 1); |
| 1071 | Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1); |
| 1072 | } |
| 1073 | |
| 1074 | if(Ui()->CheckActiveItem(pId)) |
| 1075 | { |
| 1076 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1077 | |
| 1078 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 1079 | { |
| 1080 | if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f) |
| 1081 | { |
| 1082 | m_Operation = EEnvelopeEditorOp::DRAG_POINT; |
| 1083 | |
| 1084 | m_vAccurateDragValuesX = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c].GetInternal())}; |
| 1085 | m_vAccurateDragValuesY = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c])}; |
| 1086 | |
| 1087 | if(!Map()->IsTangentOutPointSelected(Index: i, Channel: c)) |
| 1088 | Map()->SelectTangentOutPoint(Index: i, Channel: c); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | if(m_Operation == EEnvelopeEditorOp::DRAG_POINT) |
| 1093 | { |
| 1094 | float DeltaX = ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * (Input()->ModifierIsPressed() ? 50.0f : 1000.0f); |
| 1095 | float DeltaY = ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * (Input()->ModifierIsPressed() ? 51.2f : 1024.0f); |
| 1096 | m_vAccurateDragValuesX[0] += DeltaX; |
| 1097 | m_vAccurateDragValuesY[0] -= DeltaY; |
| 1098 | |
| 1099 | pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c] = CFixedTime(std::round(x: m_vAccurateDragValuesX[0])); |
| 1100 | pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c] = std::round(x: m_vAccurateDragValuesY[0]); |
| 1101 | |
| 1102 | // clamp time value |
| 1103 | pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c] = std::clamp(val: pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c], lo: CFixedTime(0), hi: CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w)) - pEnvelope->m_vPoints[i].m_Time); |
| 1104 | m_vAccurateDragValuesX[0] = std::clamp<float>(val: m_vAccurateDragValuesX[0], lo: 0, hi: (CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w)) - pEnvelope->m_vPoints[i].m_Time).GetInternal()); |
| 1105 | } |
| 1106 | |
| 1107 | if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU) |
| 1108 | { |
| 1109 | if(!Ui()->MouseButton(Index: 1)) |
| 1110 | { |
| 1111 | if(Map()->IsTangentOutPointSelected(Index: i, Channel: c)) |
| 1112 | { |
| 1113 | Map()->m_UpdateEnvPointInfo = true; |
| 1114 | ShowPopupEnvPoint(); |
| 1115 | } |
| 1116 | Ui()->SetActiveItem(nullptr); |
| 1117 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1118 | } |
| 1119 | } |
| 1120 | else if(!Ui()->MouseButton(Index: 0)) |
| 1121 | { |
| 1122 | Ui()->SetActiveItem(nullptr); |
| 1123 | Map()->m_SelectedQuadEnvelope = -1; |
| 1124 | |
| 1125 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 1126 | Map()->SelectTangentOutPoint(Index: i, Channel: c); |
| 1127 | |
| 1128 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1129 | Map()->OnModify(); |
| 1130 | } |
| 1131 | |
| 1132 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1133 | } |
| 1134 | else if(Ui()->HotItem() == pId) |
| 1135 | { |
| 1136 | if(Ui()->MouseButton(Index: 0)) |
| 1137 | { |
| 1138 | Ui()->SetActiveItem(pId); |
| 1139 | m_Operation = EEnvelopeEditorOp::SELECT; |
| 1140 | Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope; |
| 1141 | m_MouseStart = Ui()->MousePos(); |
| 1142 | } |
| 1143 | else if(Ui()->MouseButtonClicked(Index: 1)) |
| 1144 | { |
| 1145 | if(Input()->ShiftIsPressed()) |
| 1146 | { |
| 1147 | Map()->SelectTangentOutPoint(Index: i, Channel: c); |
| 1148 | pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c] = CFixedTime(0); |
| 1149 | pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c] = 0.0f; |
| 1150 | Map()->OnModify(); |
| 1151 | } |
| 1152 | else |
| 1153 | { |
| 1154 | m_Operation = EEnvelopeEditorOp::CONTEXT_MENU; |
| 1155 | Map()->SelectTangentOutPoint(Index: i, Channel: c); |
| 1156 | Ui()->SetActiveItem(pId); |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1161 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1162 | str_copy(dst&: Editor()->m_aTooltip, src: "Bezier out-tangent. Left mouse to drag. Hold ctrl to be more precise. Shift+right click to reset." ); |
| 1163 | Editor()->m_pUiGotContext = pId; |
| 1164 | } |
| 1165 | else |
| 1166 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f); |
| 1167 | |
| 1168 | // draw triangle |
| 1169 | IGraphics::CFreeformItem FreeformItem(Final.x + Final.w / 2.0f, Final.y, Final.x + Final.w / 2.0f, Final.y, Final.x + Final.w, Final.y + Final.h, Final.x, Final.y + Final.h); |
| 1170 | Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1); |
| 1171 | } |
| 1172 | |
| 1173 | // In-Tangent handle |
| 1174 | if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER) |
| 1175 | { |
| 1176 | CUIRect Final; |
| 1177 | Final.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds()); |
| 1178 | Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c])); |
| 1179 | Final.x -= 2.0f; |
| 1180 | Final.y -= 2.0f; |
| 1181 | Final.w = 4.0f; |
| 1182 | Final.h = 4.0f; |
| 1183 | |
| 1184 | // handle logic |
| 1185 | const void *pId = &pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]; |
| 1186 | |
| 1187 | if(Map()->IsTangentInPointSelected(Index: i, Channel: c)) |
| 1188 | { |
| 1189 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1190 | IGraphics::CFreeformItem FreeformItem( |
| 1191 | Final.x + Final.w / 2.0f, |
| 1192 | Final.y - 1, |
| 1193 | Final.x + Final.w / 2.0f, |
| 1194 | Final.y - 1, |
| 1195 | Final.x + Final.w + 1, |
| 1196 | Final.y + Final.h + 1, |
| 1197 | Final.x - 1, |
| 1198 | Final.y + Final.h + 1); |
| 1199 | Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1); |
| 1200 | } |
| 1201 | |
| 1202 | if(Ui()->CheckActiveItem(pId)) |
| 1203 | { |
| 1204 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1205 | |
| 1206 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 1207 | { |
| 1208 | if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f) |
| 1209 | { |
| 1210 | m_Operation = EEnvelopeEditorOp::DRAG_POINT; |
| 1211 | |
| 1212 | m_vAccurateDragValuesX = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c].GetInternal())}; |
| 1213 | m_vAccurateDragValuesY = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c])}; |
| 1214 | |
| 1215 | if(!Map()->IsTangentInPointSelected(Index: i, Channel: c)) |
| 1216 | Map()->SelectTangentInPoint(Index: i, Channel: c); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if(m_Operation == EEnvelopeEditorOp::DRAG_POINT) |
| 1221 | { |
| 1222 | float DeltaX = ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * (Input()->ModifierIsPressed() ? 50.0f : 1000.0f); |
| 1223 | float DeltaY = ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * (Input()->ModifierIsPressed() ? 51.2f : 1024.0f); |
| 1224 | m_vAccurateDragValuesX[0] += DeltaX; |
| 1225 | m_vAccurateDragValuesY[0] -= DeltaY; |
| 1226 | |
| 1227 | pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c] = CFixedTime(std::round(x: m_vAccurateDragValuesX[0])); |
| 1228 | pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c] = std::round(x: m_vAccurateDragValuesY[0]); |
| 1229 | |
| 1230 | // clamp time value |
| 1231 | pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c] = std::clamp(val: pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c], lo: CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x)) - pEnvelope->m_vPoints[i].m_Time, hi: CFixedTime(0)); |
| 1232 | m_vAccurateDragValuesX[0] = std::clamp<float>(val: m_vAccurateDragValuesX[0], lo: (CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x)) - pEnvelope->m_vPoints[i].m_Time).GetInternal(), hi: 0); |
| 1233 | } |
| 1234 | |
| 1235 | if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU) |
| 1236 | { |
| 1237 | if(!Ui()->MouseButton(Index: 1)) |
| 1238 | { |
| 1239 | if(Map()->IsTangentInPointSelected(Index: i, Channel: c)) |
| 1240 | { |
| 1241 | Map()->m_UpdateEnvPointInfo = true; |
| 1242 | ShowPopupEnvPoint(); |
| 1243 | } |
| 1244 | Ui()->SetActiveItem(nullptr); |
| 1245 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1246 | } |
| 1247 | } |
| 1248 | else if(!Ui()->MouseButton(Index: 0)) |
| 1249 | { |
| 1250 | Ui()->SetActiveItem(nullptr); |
| 1251 | Map()->m_SelectedQuadEnvelope = -1; |
| 1252 | |
| 1253 | if(m_Operation == EEnvelopeEditorOp::SELECT) |
| 1254 | Map()->SelectTangentInPoint(Index: i, Channel: c); |
| 1255 | |
| 1256 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1257 | Map()->OnModify(); |
| 1258 | } |
| 1259 | |
| 1260 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1261 | } |
| 1262 | else if(Ui()->HotItem() == pId) |
| 1263 | { |
| 1264 | if(Ui()->MouseButton(Index: 0)) |
| 1265 | { |
| 1266 | Ui()->SetActiveItem(pId); |
| 1267 | m_Operation = EEnvelopeEditorOp::SELECT; |
| 1268 | Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope; |
| 1269 | m_MouseStart = Ui()->MousePos(); |
| 1270 | } |
| 1271 | else if(Ui()->MouseButtonClicked(Index: 1)) |
| 1272 | { |
| 1273 | if(Input()->ShiftIsPressed()) |
| 1274 | { |
| 1275 | Map()->SelectTangentInPoint(Index: i, Channel: c); |
| 1276 | pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c] = CFixedTime(0); |
| 1277 | pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c] = 0.0f; |
| 1278 | Map()->OnModify(); |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | m_Operation = EEnvelopeEditorOp::CONTEXT_MENU; |
| 1283 | Map()->SelectTangentInPoint(Index: i, Channel: c); |
| 1284 | Ui()->SetActiveItem(pId); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1289 | Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1); |
| 1290 | str_copy(dst&: Editor()->m_aTooltip, src: "Bezier in-tangent. Left mouse to drag. Hold ctrl to be more precise. Shift+right click to reset." ); |
| 1291 | Editor()->m_pUiGotContext = pId; |
| 1292 | } |
| 1293 | else |
| 1294 | Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f); |
| 1295 | |
| 1296 | // draw triangle |
| 1297 | IGraphics::CFreeformItem FreeformItem(Final.x + Final.w / 2.0f, Final.y, Final.x + Final.w / 2.0f, Final.y, Final.x + Final.w, Final.y + Final.h, Final.x, Final.y + Final.h); |
| 1298 | Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1); |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | Graphics()->QuadsEnd(); |
| 1304 | Ui()->ClipDisable(); |
| 1305 | } |
| 1306 | |
| 1307 | // handle scaling |
| 1308 | if(m_Operation == EEnvelopeEditorOp::NONE && !m_NameInput.IsActive() && Input()->KeyIsPressed(Key: KEY_S) && !Input()->ModifierIsPressed() && !Map()->m_vSelectedEnvelopePoints.empty()) |
| 1309 | { |
| 1310 | m_Operation = EEnvelopeEditorOp::SCALE; |
| 1311 | m_ScaleFactor.x = 1.0f; |
| 1312 | m_ScaleFactor.y = 1.0f; |
| 1313 | auto [FirstPointIndex, FirstPointChannel] = Map()->m_vSelectedEnvelopePoints.front(); |
| 1314 | |
| 1315 | float MaximumX = pEnvelope->m_vPoints[FirstPointIndex].m_Time.GetInternal(); |
| 1316 | float MinimumX = MaximumX; |
| 1317 | m_vInitialPositionsX.clear(); |
| 1318 | for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints) |
| 1319 | { |
| 1320 | float Value = pEnvelope->m_vPoints[SelectedIndex].m_Time.GetInternal(); |
| 1321 | m_vInitialPositionsX.push_back(x: Value); |
| 1322 | MaximumX = std::max(a: MaximumX, b: Value); |
| 1323 | MinimumX = std::min(a: MinimumX, b: Value); |
| 1324 | } |
| 1325 | m_Midpoint.x = (MaximumX - MinimumX) / 2.0f + MinimumX; |
| 1326 | |
| 1327 | float MaximumY = pEnvelope->m_vPoints[FirstPointIndex].m_aValues[FirstPointChannel]; |
| 1328 | float MinimumY = MaximumY; |
| 1329 | m_vInitialPositionsY.clear(); |
| 1330 | for(auto [SelectedIndex, SelectedChannel] : Map()->m_vSelectedEnvelopePoints) |
| 1331 | { |
| 1332 | float Value = pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel]; |
| 1333 | m_vInitialPositionsY.push_back(x: Value); |
| 1334 | MaximumY = std::max(a: MaximumY, b: Value); |
| 1335 | MinimumY = std::min(a: MinimumY, b: Value); |
| 1336 | } |
| 1337 | m_Midpoint.y = (MaximumY - MinimumY) / 2.0f + MinimumY; |
| 1338 | } |
| 1339 | |
| 1340 | if(m_Operation == EEnvelopeEditorOp::SCALE) |
| 1341 | { |
| 1342 | str_copy(dst&: Editor()->m_aTooltip, src: "Press shift to scale the time. Press alt to scale along midpoint. Press ctrl to be more precise." ); |
| 1343 | |
| 1344 | if(Input()->ShiftIsPressed()) |
| 1345 | { |
| 1346 | m_ScaleFactor.x += Ui()->MouseDeltaX() / Graphics()->ScreenWidth() * (Input()->ModifierIsPressed() ? 0.5f : 10.0f); |
| 1347 | float Midpoint = Input()->AltIsPressed() ? m_Midpoint.x : 0.0f; |
| 1348 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 1349 | { |
| 1350 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 1351 | CFixedTime BoundLow = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x)); |
| 1352 | CFixedTime BoundHigh = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w)); |
| 1353 | for(int j = 0; j < SelectedIndex; j++) |
| 1354 | { |
| 1355 | if(!Map()->IsEnvPointSelected(Index: j)) |
| 1356 | BoundLow = std::max(a: pEnvelope->m_vPoints[j].m_Time + CFixedTime(1), b: BoundLow); |
| 1357 | } |
| 1358 | for(int j = SelectedIndex + 1; j < (int)pEnvelope->m_vPoints.size(); j++) |
| 1359 | { |
| 1360 | if(!Map()->IsEnvPointSelected(Index: j)) |
| 1361 | BoundHigh = std::min(a: pEnvelope->m_vPoints[j].m_Time - CFixedTime(1), b: BoundHigh); |
| 1362 | } |
| 1363 | |
| 1364 | float Value = m_vInitialPositionsX[k]; |
| 1365 | float ScaleBoundLow = (BoundLow.GetInternal() - Midpoint) / (Value - Midpoint); |
| 1366 | float ScaleBoundHigh = (BoundHigh.GetInternal() - Midpoint) / (Value - Midpoint); |
| 1367 | float ScaleBoundMin = std::min(a: ScaleBoundLow, b: ScaleBoundHigh); |
| 1368 | float ScaleBoundMax = std::max(a: ScaleBoundLow, b: ScaleBoundHigh); |
| 1369 | m_ScaleFactor.x = std::clamp(val: m_ScaleFactor.x, lo: ScaleBoundMin, hi: ScaleBoundMax); |
| 1370 | } |
| 1371 | |
| 1372 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 1373 | { |
| 1374 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 1375 | float ScaleMinimum = m_vInitialPositionsX[k] - Midpoint > CFixedTime(1).AsSeconds() ? CFixedTime(1).AsSeconds() / (m_vInitialPositionsX[k] - Midpoint) : 0.0f; |
| 1376 | float ScaleFactor = std::max(a: ScaleMinimum, b: m_ScaleFactor.x); |
| 1377 | pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: (m_vInitialPositionsX[k] - Midpoint) * ScaleFactor + Midpoint)); |
| 1378 | } |
| 1379 | for(size_t k = 1; k < pEnvelope->m_vPoints.size(); k++) |
| 1380 | { |
| 1381 | if(pEnvelope->m_vPoints[k].m_Time <= pEnvelope->m_vPoints[k - 1].m_Time) |
| 1382 | pEnvelope->m_vPoints[k].m_Time = pEnvelope->m_vPoints[k - 1].m_Time + CFixedTime(1); |
| 1383 | } |
| 1384 | for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints) |
| 1385 | { |
| 1386 | if(SelectedIndex == 0 && pEnvelope->m_vPoints[SelectedIndex].m_Time != CFixedTime(0)) |
| 1387 | { |
| 1388 | float Offset = pEnvelope->m_vPoints[0].m_Time.GetInternal(); |
| 1389 | RemoveTimeOffsetEnvelope(pEnvelope); |
| 1390 | m_Midpoint.x -= Offset; |
| 1391 | for(auto &Value : m_vInitialPositionsX) |
| 1392 | Value -= Offset; |
| 1393 | break; |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | else |
| 1398 | { |
| 1399 | m_ScaleFactor.y -= Ui()->MouseDeltaY() / Graphics()->ScreenHeight() * (Input()->ModifierIsPressed() ? 0.5f : 10.0f); |
| 1400 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 1401 | { |
| 1402 | auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k]; |
| 1403 | if(Input()->AltIsPressed()) |
| 1404 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: (m_vInitialPositionsY[k] - m_Midpoint.y) * m_ScaleFactor.y + m_Midpoint.y); |
| 1405 | else |
| 1406 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vInitialPositionsY[k] * m_ScaleFactor.y); |
| 1407 | |
| 1408 | if(pEnvelope->GetChannels() == 1 || pEnvelope->GetChannels() == 4) |
| 1409 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::clamp(val: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel], lo: 0, hi: 1024); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if(Ui()->MouseButton(Index: 0)) |
| 1414 | { |
| 1415 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1416 | Map()->m_EnvOpTracker.Stop(Switch: false); |
| 1417 | } |
| 1418 | else if(Ui()->MouseButton(Index: 1) || Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ESCAPE)) |
| 1419 | { |
| 1420 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 1421 | { |
| 1422 | int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first; |
| 1423 | pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: m_vInitialPositionsX[k])); |
| 1424 | } |
| 1425 | for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++) |
| 1426 | { |
| 1427 | auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k]; |
| 1428 | pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vInitialPositionsY[k]); |
| 1429 | } |
| 1430 | RemoveTimeOffsetEnvelope(pEnvelope); |
| 1431 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | // handle box selection |
| 1436 | if(m_Operation == EEnvelopeEditorOp::BOX_SELECT) |
| 1437 | { |
| 1438 | Ui()->ClipEnable(pRect: &View); |
| 1439 | CUIRect SelectionRect; |
| 1440 | SelectionRect.x = m_MouseStart.x; |
| 1441 | SelectionRect.y = m_MouseStart.y; |
| 1442 | SelectionRect.w = Ui()->MouseX() - m_MouseStart.x; |
| 1443 | SelectionRect.h = Ui()->MouseY() - m_MouseStart.y; |
| 1444 | SelectionRect.DrawOutline(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f)); |
| 1445 | Ui()->ClipDisable(); |
| 1446 | |
| 1447 | if(!Ui()->MouseButton(Index: 0)) |
| 1448 | { |
| 1449 | m_Operation = EEnvelopeEditorOp::NONE; |
| 1450 | Ui()->SetActiveItem(nullptr); |
| 1451 | |
| 1452 | float TimeStart = ScreenToEnvelopeX(View, x: m_MouseStart.x); |
| 1453 | float TimeEnd = ScreenToEnvelopeX(View, x: Ui()->MouseX()); |
| 1454 | float ValueStart = ScreenToEnvelopeY(View, y: m_MouseStart.y); |
| 1455 | float ValueEnd = ScreenToEnvelopeY(View, y: Ui()->MouseY()); |
| 1456 | |
| 1457 | float TimeMin = std::min(a: TimeStart, b: TimeEnd); |
| 1458 | float TimeMax = std::max(a: TimeStart, b: TimeEnd); |
| 1459 | float ValueMin = std::min(a: ValueStart, b: ValueEnd); |
| 1460 | float ValueMax = std::max(a: ValueStart, b: ValueEnd); |
| 1461 | |
| 1462 | if(!Input()->ShiftIsPressed()) |
| 1463 | Map()->DeselectEnvPoints(); |
| 1464 | |
| 1465 | for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++) |
| 1466 | { |
| 1467 | for(int c = 0; c < pEnvelope->GetChannels(); c++) |
| 1468 | { |
| 1469 | if(!(State.m_ActiveChannels & (1 << c))) |
| 1470 | continue; |
| 1471 | |
| 1472 | float Time = pEnvelope->m_vPoints[i].m_Time.AsSeconds(); |
| 1473 | float Value = fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c]); |
| 1474 | |
| 1475 | if(in_range(a: Time, lower: TimeMin, upper: TimeMax) && in_range(a: Value, lower: ValueMin, upper: ValueMax)) |
| 1476 | Map()->ToggleEnvPoint(Index: i, Channel: c); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | void CEnvelopeEditor::RenderColorBar(CUIRect ColorBar, const std::shared_ptr<CEnvelope> &pEnvelope) |
| 1485 | { |
| 1486 | if(pEnvelope->m_vPoints.size() < 2) |
| 1487 | { |
| 1488 | return; |
| 1489 | } |
| 1490 | const float ViewStartTime = ScreenToEnvelopeX(View: ColorBar, x: ColorBar.x); |
| 1491 | const float ViewEndTime = ScreenToEnvelopeX(View: ColorBar, x: ColorBar.x + ColorBar.w); |
| 1492 | if(ViewEndTime < 0.0f || ViewStartTime > pEnvelope->EndTime()) |
| 1493 | { |
| 1494 | return; |
| 1495 | } |
| 1496 | const float StartX = std::max(a: EnvelopeToScreenX(View: ColorBar, x: 0.0f), b: ColorBar.x); |
| 1497 | const float TotalWidth = std::min(a: EnvelopeToScreenX(View: ColorBar, x: pEnvelope->EndTime()) - StartX, b: ColorBar.x + ColorBar.w - StartX); |
| 1498 | |
| 1499 | Ui()->ClipEnable(pRect: &ColorBar); |
| 1500 | CUIRect ColorBarBackground = CUIRect{.x: StartX, .y: ColorBar.y, .w: TotalWidth, .h: ColorBar.h}; |
| 1501 | Editor()->RenderBackground(View: ColorBarBackground, Texture: Editor()->m_CheckerTexture, Size: ColorBarBackground.h, Brightness: 1.0f); |
| 1502 | Graphics()->TextureClear(); |
| 1503 | Graphics()->QuadsBegin(); |
| 1504 | |
| 1505 | int PointBeginIndex = pEnvelope->FindPointIndex(Time: CFixedTime::FromSeconds(Seconds: ViewStartTime)); |
| 1506 | if(PointBeginIndex == -1) |
| 1507 | { |
| 1508 | PointBeginIndex = 0; |
| 1509 | } |
| 1510 | int PointEndIndex = pEnvelope->FindPointIndex(Time: CFixedTime::FromSeconds(Seconds: ViewEndTime)); |
| 1511 | if(PointEndIndex == -1) |
| 1512 | { |
| 1513 | PointEndIndex = (int)pEnvelope->m_vPoints.size() - 2; |
| 1514 | } |
| 1515 | for(int PointIndex = PointBeginIndex; PointIndex <= PointEndIndex; PointIndex++) |
| 1516 | { |
| 1517 | const auto &PointStart = pEnvelope->m_vPoints[PointIndex]; |
| 1518 | const auto &PointEnd = pEnvelope->m_vPoints[PointIndex + 1]; |
| 1519 | const float PointStartTime = PointStart.m_Time.AsSeconds(); |
| 1520 | const float PointEndTime = PointEnd.m_Time.AsSeconds(); |
| 1521 | |
| 1522 | int Steps; |
| 1523 | if(PointStart.m_Curvetype == CURVETYPE_LINEAR || PointStart.m_Curvetype == CURVETYPE_STEP) |
| 1524 | { |
| 1525 | Steps = 1; // let the GPU do the work |
| 1526 | } |
| 1527 | else |
| 1528 | { |
| 1529 | const float ClampedPointStartX = std::max(a: EnvelopeToScreenX(View: ColorBar, x: PointStartTime), b: ColorBar.x); |
| 1530 | const float ClampedPointEndX = std::min(a: EnvelopeToScreenX(View: ColorBar, x: PointEndTime), b: ColorBar.x + ColorBar.w); |
| 1531 | Steps = std::clamp(val: (int)std::sqrt(x: 5.0f * (ClampedPointEndX - ClampedPointStartX)), lo: 1, hi: 250); |
| 1532 | } |
| 1533 | const float OverallSectionStartTime = Steps == 1 ? PointStartTime : std::max(a: PointStartTime, b: ViewStartTime); |
| 1534 | const float OverallSectionEndTime = Steps == 1 ? PointEndTime : std::min(a: PointEndTime, b: ViewEndTime); |
| 1535 | float SectionStartTime = OverallSectionStartTime; |
| 1536 | float SectionStartX = EnvelopeToScreenX(View: ColorBar, x: SectionStartTime); |
| 1537 | for(int Step = 1; Step <= Steps; Step++) |
| 1538 | { |
| 1539 | const float SectionEndTime = OverallSectionStartTime + (OverallSectionEndTime - OverallSectionStartTime) * (Step / (float)Steps); |
| 1540 | const float SectionEndX = EnvelopeToScreenX(View: ColorBar, x: SectionEndTime); |
| 1541 | |
| 1542 | ColorRGBA StartColor; |
| 1543 | if(Step == 1 && OverallSectionStartTime == PointStartTime) |
| 1544 | { |
| 1545 | StartColor = PointStart.ColorValue(); |
| 1546 | } |
| 1547 | else |
| 1548 | { |
| 1549 | StartColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 1550 | pEnvelope->Eval(Time: SectionStartTime, Result&: StartColor, Channels: 4); |
| 1551 | } |
| 1552 | |
| 1553 | ColorRGBA EndColor; |
| 1554 | if(PointStart.m_Curvetype == CURVETYPE_STEP) |
| 1555 | { |
| 1556 | EndColor = StartColor; |
| 1557 | } |
| 1558 | else if(Step == Steps && OverallSectionEndTime == PointEndTime) |
| 1559 | { |
| 1560 | EndColor = PointEnd.ColorValue(); |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | EndColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 1565 | pEnvelope->Eval(Time: SectionEndTime, Result&: EndColor, Channels: 4); |
| 1566 | } |
| 1567 | |
| 1568 | Graphics()->SetColor4(TopLeft: StartColor, TopRight: EndColor, BottomLeft: StartColor, BottomRight: EndColor); |
| 1569 | const IGraphics::CQuadItem QuadItem(SectionStartX, ColorBar.y, SectionEndX - SectionStartX, ColorBar.h); |
| 1570 | Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1); |
| 1571 | |
| 1572 | SectionStartTime = SectionEndTime; |
| 1573 | SectionStartX = SectionEndX; |
| 1574 | } |
| 1575 | } |
| 1576 | Graphics()->QuadsEnd(); |
| 1577 | Ui()->ClipDisable(); |
| 1578 | ColorBarBackground.h -= Ui()->Screen()->h / Graphics()->ScreenHeight(); // hack to fix alignment of bottom border |
| 1579 | ColorBarBackground.DrawOutline(Color: ColorRGBA(0.7f, 0.7f, 0.7f, 1.0f)); |
| 1580 | } |
| 1581 | |
| 1582 | void CEnvelopeEditor::UpdateHotEnvelopeObject(const CUIRect &View, const CEnvelope *pEnvelope, int ActiveChannels) |
| 1583 | { |
| 1584 | if(!Ui()->MouseInside(pRect: &View)) |
| 1585 | return; |
| 1586 | |
| 1587 | const vec2 MousePos = Ui()->MousePos(); |
| 1588 | |
| 1589 | float MinDist = 200.0f; |
| 1590 | const void *pMinPointId = nullptr; |
| 1591 | |
| 1592 | const auto UpdateMinimum = [&](vec2 Position, const void *pId) { |
| 1593 | const float CurrDist = distance_squared(a: Position, b: MousePos); |
| 1594 | if(CurrDist < MinDist) |
| 1595 | { |
| 1596 | MinDist = CurrDist; |
| 1597 | pMinPointId = pId; |
| 1598 | } |
| 1599 | }; |
| 1600 | |
| 1601 | for(size_t i = 0; i < pEnvelope->m_vPoints.size(); i++) |
| 1602 | { |
| 1603 | for(int c = pEnvelope->GetChannels() - 1; c >= 0; c--) |
| 1604 | { |
| 1605 | if(!(ActiveChannels & (1 << c))) |
| 1606 | continue; |
| 1607 | |
| 1608 | if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER) |
| 1609 | { |
| 1610 | vec2 Position; |
| 1611 | Position.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds()); |
| 1612 | Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c])); |
| 1613 | UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]); |
| 1614 | } |
| 1615 | |
| 1616 | if(i < pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER) |
| 1617 | { |
| 1618 | vec2 Position; |
| 1619 | Position.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds()); |
| 1620 | Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c])); |
| 1621 | UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]); |
| 1622 | } |
| 1623 | |
| 1624 | vec2 Position; |
| 1625 | Position.x = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds()); |
| 1626 | Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c])); |
| 1627 | UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_aValues[c]); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | if(pMinPointId != nullptr) |
| 1632 | { |
| 1633 | Ui()->SetHotItem(pMinPointId); |
| 1634 | } |
| 1635 | else if(!Map()->m_EnvelopeEvaluator.m_Animate) |
| 1636 | { |
| 1637 | float Time = Map()->m_EnvelopeEvaluator.m_AnimateTime * Map()->m_EnvelopeEvaluator.m_AnimateSpeed; |
| 1638 | float LoopedTime = std::fmod(x: Time, y: pEnvelope->EndTime()); |
| 1639 | if(absolute(a: EnvelopeToScreenX(View, x: Time) - MousePos.x) < 20.0f || absolute(a: EnvelopeToScreenX(View, x: LoopedTime) - MousePos.x) < 20.0f) |
| 1640 | { |
| 1641 | Ui()->SetHotItem(&Map()->m_EnvelopeEvaluator.m_AnimateTime); |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | void CEnvelopeEditor::ZoomAdaptOffsetX(float ZoomFactor, const CUIRect &View) |
| 1647 | { |
| 1648 | float PosX = g_Config.m_EdZoomTarget ? (Ui()->MouseX() - View.x) / View.w : 0.5f; |
| 1649 | Map()->m_EnvelopeEditorState.m_Offset.x = PosX - (PosX - Map()->m_EnvelopeEditorState.m_Offset.x) * ZoomFactor; |
| 1650 | } |
| 1651 | |
| 1652 | void CEnvelopeEditor::UpdateZoomEnvelopeX(const CUIRect &View) |
| 1653 | { |
| 1654 | float OldZoom = Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(); |
| 1655 | if(Map()->m_EnvelopeEditorState.m_ZoomX.UpdateValue()) |
| 1656 | ZoomAdaptOffsetX(ZoomFactor: OldZoom / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(), View); |
| 1657 | } |
| 1658 | |
| 1659 | void CEnvelopeEditor::ZoomAdaptOffsetY(float ZoomFactor, const CUIRect &View) |
| 1660 | { |
| 1661 | float PosY = g_Config.m_EdZoomTarget ? 1.0f - (Ui()->MouseY() - View.y) / View.h : 0.5f; |
| 1662 | Map()->m_EnvelopeEditorState.m_Offset.y = PosY - (PosY - Map()->m_EnvelopeEditorState.m_Offset.y) * ZoomFactor; |
| 1663 | } |
| 1664 | |
| 1665 | void CEnvelopeEditor::UpdateZoomEnvelopeY(const CUIRect &View) |
| 1666 | { |
| 1667 | float OldZoom = Map()->m_EnvelopeEditorState.m_ZoomY.GetValue(); |
| 1668 | if(Map()->m_EnvelopeEditorState.m_ZoomY.UpdateValue()) |
| 1669 | ZoomAdaptOffsetY(ZoomFactor: OldZoom / Map()->m_EnvelopeEditorState.m_ZoomY.GetValue(), View); |
| 1670 | } |
| 1671 | |
| 1672 | void CEnvelopeEditor::ResetZoomEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope, int ActiveChannels) |
| 1673 | { |
| 1674 | auto [Bottom, Top] = pEnvelope->GetValueRange(ChannelMask: ActiveChannels); |
| 1675 | float EndTime = pEnvelope->EndTime(); |
| 1676 | float ValueRange = absolute(a: Top - Bottom); |
| 1677 | CState &State = Map()->m_EnvelopeEditorState; |
| 1678 | |
| 1679 | if(ValueRange < State.m_ZoomY.GetMinValue()) |
| 1680 | { |
| 1681 | // Set view to some sane default if range is too small |
| 1682 | State.m_Offset.y = 0.5f - ValueRange / State.m_ZoomY.GetMinValue() / 2.0f - Bottom / State.m_ZoomY.GetMinValue(); |
| 1683 | State.m_ZoomY.SetValueInstant(State.m_ZoomY.GetMinValue()); |
| 1684 | } |
| 1685 | else if(ValueRange > State.m_ZoomY.GetMaxValue()) |
| 1686 | { |
| 1687 | State.m_Offset.y = -Bottom / State.m_ZoomY.GetMaxValue(); |
| 1688 | State.m_ZoomY.SetValueInstant(State.m_ZoomY.GetMaxValue()); |
| 1689 | } |
| 1690 | else |
| 1691 | { |
| 1692 | // calculate biggest possible spacing |
| 1693 | float SpacingFactor = std::min(a: 1.25f, b: State.m_ZoomY.GetMaxValue() / ValueRange); |
| 1694 | State.m_ZoomY.SetValueInstant(SpacingFactor * ValueRange); |
| 1695 | float Space = 1.0f / SpacingFactor; |
| 1696 | float Spacing = (1.0f - Space) / 2.0f; |
| 1697 | |
| 1698 | if(Top >= 0 && Bottom >= 0) |
| 1699 | State.m_Offset.y = Spacing - Bottom / State.m_ZoomY.GetValue(); |
| 1700 | else if(Top <= 0 && Bottom <= 0) |
| 1701 | State.m_Offset.y = Spacing - Bottom / State.m_ZoomY.GetValue(); |
| 1702 | else |
| 1703 | State.m_Offset.y = Spacing + Space * absolute(a: Bottom) / ValueRange; |
| 1704 | } |
| 1705 | |
| 1706 | if(EndTime < State.m_ZoomX.GetMinValue()) |
| 1707 | { |
| 1708 | State.m_Offset.x = 0.5f - EndTime / State.m_ZoomX.GetMinValue(); |
| 1709 | State.m_ZoomX.SetValueInstant(State.m_ZoomX.GetMinValue()); |
| 1710 | } |
| 1711 | else if(EndTime > State.m_ZoomX.GetMaxValue()) |
| 1712 | { |
| 1713 | State.m_Offset.x = 0.0f; |
| 1714 | State.m_ZoomX.SetValueInstant(State.m_ZoomX.GetMaxValue()); |
| 1715 | } |
| 1716 | else |
| 1717 | { |
| 1718 | float SpacingFactor = std::min(a: 1.25f, b: State.m_ZoomX.GetMaxValue() / EndTime); |
| 1719 | State.m_ZoomX.SetValueInstant(SpacingFactor * EndTime); |
| 1720 | float Space = 1.0f / SpacingFactor; |
| 1721 | float Spacing = (1.0f - Space) / 2.0f; |
| 1722 | State.m_Offset.x = Spacing; |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | float CEnvelopeEditor::ScreenToEnvelopeX(const CUIRect &View, float x) const |
| 1727 | { |
| 1728 | return (x - View.x - View.w * Map()->m_EnvelopeEditorState.m_Offset.x) / View.w * Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(); |
| 1729 | } |
| 1730 | |
| 1731 | float CEnvelopeEditor::EnvelopeToScreenX(const CUIRect &View, float x) const |
| 1732 | { |
| 1733 | return View.x + View.w * Map()->m_EnvelopeEditorState.m_Offset.x + x / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue() * View.w; |
| 1734 | } |
| 1735 | |
| 1736 | float CEnvelopeEditor::ScreenToEnvelopeY(const CUIRect &View, float y) const |
| 1737 | { |
| 1738 | return (View.h - y + View.y) / View.h * Map()->m_EnvelopeEditorState.m_ZoomY.GetValue() - Map()->m_EnvelopeEditorState.m_Offset.y * Map()->m_EnvelopeEditorState.m_ZoomY.GetValue(); |
| 1739 | } |
| 1740 | |
| 1741 | float CEnvelopeEditor::EnvelopeToScreenY(const CUIRect &View, float y) const |
| 1742 | { |
| 1743 | return View.y + View.h - y / Map()->m_EnvelopeEditorState.m_ZoomY.GetValue() * View.h - Map()->m_EnvelopeEditorState.m_Offset.y * View.h; |
| 1744 | } |
| 1745 | |
| 1746 | float CEnvelopeEditor::ScreenToEnvelopeDeltaX(const CUIRect &View, float DeltaX) |
| 1747 | { |
| 1748 | return DeltaX / Graphics()->ScreenWidth() * Ui()->Screen()->w / View.w * Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(); |
| 1749 | } |
| 1750 | |
| 1751 | float CEnvelopeEditor::ScreenToEnvelopeDeltaY(const CUIRect &View, float DeltaY) |
| 1752 | { |
| 1753 | return DeltaY / Graphics()->ScreenHeight() * Ui()->Screen()->h / View.h * Map()->m_EnvelopeEditorState.m_ZoomY.GetValue(); |
| 1754 | } |
| 1755 | |
| 1756 | void CEnvelopeEditor::RemoveTimeOffsetEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope) |
| 1757 | { |
| 1758 | CFixedTime TimeOffset = pEnvelope->m_vPoints[0].m_Time; |
| 1759 | for(auto &Point : pEnvelope->m_vPoints) |
| 1760 | Point.m_Time -= TimeOffset; |
| 1761 | |
| 1762 | Map()->m_EnvelopeEditorState.m_Offset.x += TimeOffset.AsSeconds() / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(); |
| 1763 | } |
| 1764 | |
| 1765 | CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePoint::(void *pContext, CUIRect View, bool Active) |
| 1766 | { |
| 1767 | CPopupEnvelopePoint * = static_cast<CPopupEnvelopePoint *>(pContext); |
| 1768 | CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointContext->m_pEnvelopeEditor; |
| 1769 | CEditorMap *pMap = pEnvelopeEditor->Map(); |
| 1770 | CEditor *pEditor = pEnvelopeEditor->Editor(); |
| 1771 | |
| 1772 | if(pMap->m_SelectedEnvelope < 0 || pMap->m_SelectedEnvelope >= (int)pMap->m_vpEnvelopes.size()) |
| 1773 | { |
| 1774 | return CUi::POPUP_CLOSE_CURRENT; |
| 1775 | } |
| 1776 | |
| 1777 | const float RowHeight = 12.0f; |
| 1778 | CUIRect Row, Label, EditBox; |
| 1779 | |
| 1780 | pEditor->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED; |
| 1781 | |
| 1782 | std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes[pMap->m_SelectedEnvelope]; |
| 1783 | |
| 1784 | if(pEnvelope->GetChannels() == 4 && !pMap->IsTangentSelected()) |
| 1785 | { |
| 1786 | View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View); |
| 1787 | View.HSplitTop(Cut: 4.0f, pTop: nullptr, pBottom: &View); |
| 1788 | Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row); |
| 1789 | Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox); |
| 1790 | pEditor->Ui()->DoLabel(pRect: &Label, pText: "Color:" , Size: RowHeight - 2.0f, Align: TEXTALIGN_ML); |
| 1791 | |
| 1792 | const auto SelectedPoint = pMap->m_vSelectedEnvelopePoints.front(); |
| 1793 | const int SelectedIndex = SelectedPoint.first; |
| 1794 | auto *pValues = pEnvelope->m_vPoints[SelectedIndex].m_aValues; |
| 1795 | const ColorRGBA Color = pEnvelope->m_vPoints[SelectedIndex].ColorValue(); |
| 1796 | const auto &&SetColor = [&](ColorRGBA NewColor) { |
| 1797 | if(Color == NewColor && pEditor->m_ColorPickerPopupContext.m_State == EEditState::EDITING) |
| 1798 | return; |
| 1799 | |
| 1800 | if(pEditor->m_ColorPickerPopupContext.m_State == EEditState::START || pEditor->m_ColorPickerPopupContext.m_State == EEditState::ONE_GO) |
| 1801 | { |
| 1802 | for(int Channel = 0; Channel < 4; ++Channel) |
| 1803 | pPopupEnvelopePointContext->m_aValues[Channel] = pValues[Channel]; |
| 1804 | } |
| 1805 | |
| 1806 | pEnvelope->m_vPoints[SelectedIndex].SetColorValue(NewColor); |
| 1807 | |
| 1808 | if(pEditor->m_ColorPickerPopupContext.m_State == EEditState::END || pEditor->m_ColorPickerPopupContext.m_State == EEditState::ONE_GO) |
| 1809 | { |
| 1810 | std::vector<std::shared_ptr<IEditorAction>> vpActions(4); |
| 1811 | |
| 1812 | for(int Channel = 0; Channel < 4; ++Channel) |
| 1813 | { |
| 1814 | vpActions[Channel] = std::make_shared<CEditorActionEnvelopeEditPoint>(args&: pMap, args&: pMap->m_SelectedEnvelope, args: SelectedIndex, args&: Channel, args: CEditorActionEnvelopeEditPoint::EEditType::VALUE, args&: pPopupEnvelopePointContext->m_aValues[Channel], args: f2fx(v: NewColor[Channel])); |
| 1815 | } |
| 1816 | |
| 1817 | char aDisplay[256]; |
| 1818 | str_format(buffer: aDisplay, buffer_size: sizeof(aDisplay), format: "Edit color of point %d of envelope %d" , SelectedIndex, pMap->m_SelectedEnvelope); |
| 1819 | pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args&: pMap, args&: vpActions, args&: aDisplay)); |
| 1820 | } |
| 1821 | |
| 1822 | pMap->m_UpdateEnvPointInfo = true; |
| 1823 | pMap->OnModify(); |
| 1824 | }; |
| 1825 | pEditor->DoColorPickerButton(pId: &pPopupEnvelopePointContext->m_ColorPickerButtonId, pRect: &EditBox, Color, SetColor); |
| 1826 | } |
| 1827 | |
| 1828 | if(pMap->m_UpdateEnvPointInfo) |
| 1829 | { |
| 1830 | pMap->m_UpdateEnvPointInfo = false; |
| 1831 | |
| 1832 | const auto &[CurrentTime, CurrentValue] = pMap->SelectedEnvelopeTimeAndValue(); |
| 1833 | |
| 1834 | // update displayed text |
| 1835 | pPopupEnvelopePointContext->m_ValueInput.SetFloat(fx2f(v: CurrentValue)); |
| 1836 | pPopupEnvelopePointContext->m_TimeInput.SetFloat(CurrentTime.AsSeconds()); |
| 1837 | |
| 1838 | pPopupEnvelopePointContext->m_CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat(); |
| 1839 | pPopupEnvelopePointContext->m_CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat(); |
| 1840 | } |
| 1841 | |
| 1842 | View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View); |
| 1843 | Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row); |
| 1844 | Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox); |
| 1845 | pEditor->Ui()->DoLabel(pRect: &Label, pText: "Value:" , Size: RowHeight - 2.0f, Align: TEXTALIGN_ML); |
| 1846 | pEditor->DoEditBox(pLineInput: &pPopupEnvelopePointContext->m_ValueInput, pRect: &EditBox, FontSize: RowHeight - 2.0f, Corners: IGraphics::CORNER_ALL, pToolTip: "The value of the selected envelope point." ); |
| 1847 | |
| 1848 | View.HSplitTop(Cut: 4.0f, pTop: nullptr, pBottom: &View); |
| 1849 | View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View); |
| 1850 | Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row); |
| 1851 | Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox); |
| 1852 | pEditor->Ui()->DoLabel(pRect: &Label, pText: "Time (in s):" , Size: RowHeight - 2.0f, Align: TEXTALIGN_ML); |
| 1853 | pEditor->DoEditBox(pLineInput: &pPopupEnvelopePointContext->m_TimeInput, pRect: &EditBox, FontSize: RowHeight - 2.0f, Corners: IGraphics::CORNER_ALL, pToolTip: "The time of the selected envelope point." ); |
| 1854 | |
| 1855 | if(pEditor->Input()->KeyIsPressed(Key: KEY_RETURN) || pEditor->Input()->KeyIsPressed(Key: KEY_KP_ENTER)) |
| 1856 | { |
| 1857 | float CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat(); |
| 1858 | float CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat(); |
| 1859 | if(!(absolute(a: CurrentTime - pPopupEnvelopePointContext->m_CurrentTime) < 0.0001f && absolute(a: CurrentValue - pPopupEnvelopePointContext->m_CurrentValue) < 0.0001f)) |
| 1860 | { |
| 1861 | const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue(); |
| 1862 | |
| 1863 | if(pMap->IsTangentInSelected()) |
| 1864 | { |
| 1865 | auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentInPoint; |
| 1866 | |
| 1867 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEditEnvelopePointValue>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: CEditorActionEditEnvelopePointValue::EType::TANGENT_IN, args: OldTime, args: OldValue, args: CFixedTime::FromSeconds(Seconds: CurrentTime), args: f2fx(v: CurrentValue))); |
| 1868 | CurrentTime = (pEnvelope->m_vPoints[SelectedIndex].m_Time + pEnvelope->m_vPoints[SelectedIndex].m_Bezier.m_aInTangentDeltaX[SelectedChannel]).AsSeconds(); |
| 1869 | } |
| 1870 | else if(pMap->IsTangentOutSelected()) |
| 1871 | { |
| 1872 | auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentOutPoint; |
| 1873 | |
| 1874 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEditEnvelopePointValue>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: CEditorActionEditEnvelopePointValue::EType::TANGENT_OUT, args: OldTime, args: OldValue, args: CFixedTime::FromSeconds(Seconds: CurrentTime), args: f2fx(v: CurrentValue))); |
| 1875 | CurrentTime = (pEnvelope->m_vPoints[SelectedIndex].m_Time + pEnvelope->m_vPoints[SelectedIndex].m_Bezier.m_aOutTangentDeltaX[SelectedChannel]).AsSeconds(); |
| 1876 | } |
| 1877 | else |
| 1878 | { |
| 1879 | auto [SelectedIndex, SelectedChannel] = pMap->m_vSelectedEnvelopePoints.front(); |
| 1880 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionEditEnvelopePointValue>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: CEditorActionEditEnvelopePointValue::EType::POINT, args: OldTime, args: OldValue, args: CFixedTime::FromSeconds(Seconds: CurrentTime), args: f2fx(v: CurrentValue))); |
| 1881 | |
| 1882 | if(SelectedIndex != 0) |
| 1883 | { |
| 1884 | CurrentTime = pEnvelope->m_vPoints[SelectedIndex].m_Time.AsSeconds(); |
| 1885 | } |
| 1886 | else |
| 1887 | { |
| 1888 | CurrentTime = 0.0f; |
| 1889 | pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(0); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | pPopupEnvelopePointContext->m_TimeInput.SetFloat(CFixedTime::FromSeconds(Seconds: CurrentTime).AsSeconds()); |
| 1894 | pPopupEnvelopePointContext->m_ValueInput.SetFloat(fx2f(v: f2fx(v: CurrentValue))); |
| 1895 | |
| 1896 | pPopupEnvelopePointContext->m_CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat(); |
| 1897 | pPopupEnvelopePointContext->m_CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat(); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | View.HSplitTop(Cut: 6.0f, pTop: nullptr, pBottom: &View); |
| 1902 | View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View); |
| 1903 | const char *pButtonText = pMap->IsTangentSelected() ? "Reset" : "Delete" ; |
| 1904 | const char *pTooltip = pMap->IsTangentSelected() ? "Reset tangent point to default value." : "Delete current envelope point in all channels." ; |
| 1905 | if(pEditor->DoButton_Editor(pId: &pPopupEnvelopePointContext->m_DeleteButtonId, pText: pButtonText, Checked: 0, pRect: &Row, Flags: BUTTONFLAG_LEFT, pToolTip: pTooltip)) |
| 1906 | { |
| 1907 | if(pMap->IsTangentInSelected()) |
| 1908 | { |
| 1909 | auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentInPoint; |
| 1910 | const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue(); |
| 1911 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionResetEnvelopePointTangent>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: true, args: OldTime, args: OldValue)); |
| 1912 | } |
| 1913 | else if(pMap->IsTangentOutSelected()) |
| 1914 | { |
| 1915 | auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentOutPoint; |
| 1916 | const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue(); |
| 1917 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionResetEnvelopePointTangent>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: false, args: OldTime, args: OldValue)); |
| 1918 | } |
| 1919 | else |
| 1920 | { |
| 1921 | auto [SelectedIndex, SelectedChannel] = pMap->m_vSelectedEnvelopePoints.front(); |
| 1922 | pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionDeleteEnvelopePoint>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex)); |
| 1923 | } |
| 1924 | |
| 1925 | return CUi::POPUP_CLOSE_CURRENT; |
| 1926 | } |
| 1927 | |
| 1928 | return CUi::POPUP_KEEP_OPEN; |
| 1929 | } |
| 1930 | |
| 1931 | CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePointMulti::(void *pContext, CUIRect View, bool Active) |
| 1932 | { |
| 1933 | CPopupEnvelopePointMulti * = static_cast<CPopupEnvelopePointMulti *>(pContext); |
| 1934 | CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointMultiContext->m_pEnvelopeEditor; |
| 1935 | CEditor *pEditor = pEnvelopeEditor->Editor(); |
| 1936 | |
| 1937 | CUIRect ProjectOntoButton; |
| 1938 | View.HSplitTop(Cut: 14.0f, pTop: &ProjectOntoButton, pBottom: &View); |
| 1939 | if(pEditor->DoButton_MenuItem(pId: &pPopupEnvelopePointMultiContext->m_ProjectOntoButtonId, pText: "Project onto" , Checked: 0, pRect: &ProjectOntoButton, Flags: BUTTONFLAG_LEFT, pToolTip: "Project all selected envelopes onto the curve between the first and last selected envelope." )) |
| 1940 | { |
| 1941 | pEnvelopeEditor->m_PopupEnvelopePointCurveType.m_pEnvelopeEditor = pEnvelopeEditor; |
| 1942 | pEditor->Ui()->DoPopupMenu(pId: &pEnvelopeEditor->m_PopupEnvelopePointCurveType, X: pEditor->Ui()->MouseX(), Y: pEditor->Ui()->MouseY(), Width: 80, Height: 80, pContext: &pEnvelopeEditor->m_PopupEnvelopePointCurveType, pfnFunc: CEnvelopeEditor::CPopupEnvelopePointCurveType::Render); |
| 1943 | } |
| 1944 | |
| 1945 | return CUi::POPUP_KEEP_OPEN; |
| 1946 | } |
| 1947 | |
| 1948 | CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePointCurveType::(void *pContext, CUIRect View, bool Active) |
| 1949 | { |
| 1950 | CPopupEnvelopePointCurveType * = static_cast<CPopupEnvelopePointCurveType *>(pContext); |
| 1951 | CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointCurveTypeContext->m_pEnvelopeEditor; |
| 1952 | CEditorMap *pMap = pEnvelopeEditor->Map(); |
| 1953 | CEditor *pEditor = pEnvelopeEditor->Editor(); |
| 1954 | |
| 1955 | int SelectedCurveType = -1; |
| 1956 | for(int Type = 0; Type <= CURVETYPE_SMOOTH; Type++) |
| 1957 | { |
| 1958 | CUIRect Button; |
| 1959 | View.HSplitTop(Cut: 14.0f, pTop: &Button, pBottom: &View); |
| 1960 | if(pEditor->DoButton_MenuItem(pId: &pPopupEnvelopePointCurveTypeContext->m_aButtonIds[Type], pText: CURVE_TYPE_NAMES[Type], Checked: 0, pRect: &Button)) |
| 1961 | { |
| 1962 | SelectedCurveType = Type; |
| 1963 | } |
| 1964 | } |
| 1965 | if(SelectedCurveType < 0) |
| 1966 | { |
| 1967 | return CUi::POPUP_KEEP_OPEN; |
| 1968 | } |
| 1969 | |
| 1970 | std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes.at(n: pMap->m_SelectedEnvelope); |
| 1971 | std::vector<std::shared_ptr<IEditorAction>> vpActions; |
| 1972 | for(int Channel = 0; Channel < pEnvelope->GetChannels(); Channel++) |
| 1973 | { |
| 1974 | int FirstSelectedIndex = pEnvelope->m_vPoints.size(); |
| 1975 | int LastSelectedIndex = -1; |
| 1976 | for(auto [SelectedIndex, SelectedChannel] : pMap->m_vSelectedEnvelopePoints) |
| 1977 | { |
| 1978 | if(SelectedChannel == Channel) |
| 1979 | { |
| 1980 | FirstSelectedIndex = std::min(a: FirstSelectedIndex, b: SelectedIndex); |
| 1981 | LastSelectedIndex = std::max(a: LastSelectedIndex, b: SelectedIndex); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | if(FirstSelectedIndex < (int)pEnvelope->m_vPoints.size() && LastSelectedIndex >= 0 && FirstSelectedIndex != LastSelectedIndex) |
| 1986 | { |
| 1987 | CEnvPoint FirstPoint = pEnvelope->m_vPoints[FirstSelectedIndex]; |
| 1988 | CEnvPoint LastPoint = pEnvelope->m_vPoints[LastSelectedIndex]; |
| 1989 | |
| 1990 | CEnvelope HelperEnvelope(1); |
| 1991 | HelperEnvelope.AddPoint(Time: FirstPoint.m_Time, aValues: {FirstPoint.m_aValues[Channel], 0, 0, 0}); |
| 1992 | HelperEnvelope.AddPoint(Time: LastPoint.m_Time, aValues: {LastPoint.m_aValues[Channel], 0, 0, 0}); |
| 1993 | HelperEnvelope.m_vPoints[0].m_Curvetype = SelectedCurveType; |
| 1994 | |
| 1995 | for(auto [SelectedIndex, SelectedChannel] : pMap->m_vSelectedEnvelopePoints) |
| 1996 | { |
| 1997 | if(SelectedChannel == Channel && |
| 1998 | SelectedIndex != FirstSelectedIndex && |
| 1999 | SelectedIndex != LastSelectedIndex) |
| 2000 | { |
| 2001 | CEnvPoint &CurrentPoint = pEnvelope->m_vPoints[SelectedIndex]; |
| 2002 | ColorRGBA Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f); |
| 2003 | HelperEnvelope.Eval(Time: CurrentPoint.m_Time.AsSeconds(), Result&: Channels, Channels: 1); |
| 2004 | int PrevValue = CurrentPoint.m_aValues[Channel]; |
| 2005 | CurrentPoint.m_aValues[Channel] = f2fx(v: Channels.r); |
| 2006 | vpActions.push_back(x: std::make_shared<CEditorActionEnvelopeEditPoint>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex, args&: SelectedChannel, args: CEditorActionEnvelopeEditPoint::EEditType::VALUE, args&: PrevValue, args&: CurrentPoint.m_aValues[Channel])); |
| 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | if(!vpActions.empty()) |
| 2013 | { |
| 2014 | pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args&: pMap, args&: vpActions, args: "Project points" )); |
| 2015 | } |
| 2016 | |
| 2017 | pMap->OnModify(); |
| 2018 | return CUi::POPUP_CLOSE_CURRENT; |
| 2019 | } |
| 2020 | |
| 2021 | CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopeCurveType::(void *pContext, CUIRect View, bool Active) |
| 2022 | { |
| 2023 | CPopupEnvelopeCurveType * = static_cast<CPopupEnvelopeCurveType *>(pContext); |
| 2024 | CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopeCurveTypeContext->m_pEnvelopeEditor; |
| 2025 | CEditorMap *pMap = pEnvelopeEditor->Map(); |
| 2026 | CEditor *pEditor = pEnvelopeEditor->Editor(); |
| 2027 | |
| 2028 | if(pMap->m_SelectedEnvelope < 0 || pMap->m_SelectedEnvelope >= (int)pMap->m_vpEnvelopes.size()) |
| 2029 | { |
| 2030 | return CUi::POPUP_CLOSE_CURRENT; |
| 2031 | } |
| 2032 | std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes[pMap->m_SelectedEnvelope]; |
| 2033 | |
| 2034 | if(pPopupEnvelopeCurveTypeContext->m_SelectedPoint < 0 || pPopupEnvelopeCurveTypeContext->m_SelectedPoint >= (int)pEnvelope->m_vPoints.size()) |
| 2035 | { |
| 2036 | return CUi::POPUP_CLOSE_CURRENT; |
| 2037 | } |
| 2038 | CEnvPoint_runtime &SelectedPoint = pEnvelope->m_vPoints[pPopupEnvelopeCurveTypeContext->m_SelectedPoint]; |
| 2039 | |
| 2040 | for(int Type = 0; Type < NUM_CURVETYPES; Type++) |
| 2041 | { |
| 2042 | CUIRect Button; |
| 2043 | View.HSplitTop(Cut: 14.0f, pTop: &Button, pBottom: &View); |
| 2044 | |
| 2045 | if(pEditor->DoButton_MenuItem(pId: &pPopupEnvelopeCurveTypeContext->m_aButtonIds[Type], pText: CURVE_TYPE_NAMES[Type], Checked: Type == SelectedPoint.m_Curvetype, pRect: &Button)) |
| 2046 | { |
| 2047 | const int PrevCurve = SelectedPoint.m_Curvetype; |
| 2048 | if(PrevCurve != Type) |
| 2049 | { |
| 2050 | SelectedPoint.m_Curvetype = Type; |
| 2051 | pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEditPoint>( |
| 2052 | args&: pMap, args&: pMap->m_SelectedEnvelope, args&: pPopupEnvelopeCurveTypeContext->m_SelectedPoint, args: 0, |
| 2053 | args: CEditorActionEnvelopeEditPoint::EEditType::CURVE_TYPE, args: PrevCurve, args&: SelectedPoint.m_Curvetype)); |
| 2054 | pMap->OnModify(); |
| 2055 | return CUi::POPUP_CLOSE_CURRENT; |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | return CUi::POPUP_KEEP_OPEN; |
| 2061 | } |
| 2062 | |