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
23static const char *const CURVE_TYPE_NAMES[] = {"Step", "Linear", "Slow", "Fast", "Smooth", "Bezier"};
24static const char *const CURVE_TYPE_NAMES_SHORT[] = {"N", "L", "S", "F", "M", "B"};
25static_assert(std::size(CURVE_TYPE_NAMES) == NUM_CURVETYPES);
26static_assert(std::size(CURVE_TYPE_NAMES_SHORT) == NUM_CURVETYPES);
27
28static 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
37static 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
46class CTimeStep
47{
48public:
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
105private:
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
121void 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
132void 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
146void 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 if(Input()->ModifierIsPressed())
558 {
559 Ui()->SetMouseSlow(true);
560 }
561
562 Map()->m_EnvelopeEvaluator.m_AnimateTime += ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) / Map()->m_EnvelopeEvaluator.m_AnimateSpeed;
563 Map()->m_EnvelopeEvaluator.m_AnimateTime = std::max(a: Map()->m_EnvelopeEvaluator.m_AnimateTime, b: 0.0f);
564 }
565
566 if(!Ui()->MouseButton(Index: 0))
567 {
568 Ui()->SetActiveItem(nullptr);
569 m_Operation = EEnvelopeEditorOp::NONE;
570 }
571
572 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
573 BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.8f);
574 str_copy(dst&: Editor()->m_aTooltip, src: "Timebar. Press left-click to drag. Hold ctrl to be more precise.");
575 }
576 else if(Ui()->HotItem() == &Map()->m_EnvelopeEvaluator.m_AnimateTime)
577 {
578 if(Ui()->MouseButton(Index: 0))
579 {
580 Ui()->SetActiveItem(&Map()->m_EnvelopeEvaluator.m_AnimateTime);
581 m_Operation = EEnvelopeEditorOp::SELECT;
582 m_MouseStart = Ui()->MousePos();
583 }
584
585 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
586 BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.8f);
587 str_copy(dst&: Editor()->m_aTooltip, src: "Timebar. Press left-click to drag. Hold ctrl to be more precise.");
588 }
589 else
590 BarColor = ColorRGBA(1.0f, 1.0f, 0.0f, 0.5f);
591
592 Ui()->ClipEnable(pRect: &View);
593 float Time = Map()->m_EnvelopeEvaluator.m_AnimateTime * Map()->m_EnvelopeEvaluator.m_AnimateSpeed;
594 const float BarWidth = 1.5f;
595 CUIRect TimeBar{
596 .x: EnvelopeToScreenX(View, x: Time) - BarWidth / 2.0f,
597 .y: View.y,
598 .w: BarWidth,
599 .h: View.h,
600 };
601 TimeBar.Draw(Color: BarColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
602
603 if(Time > pEnvelope->EndTime())
604 {
605 float LoopedTime = std::fmod(x: Time, y: pEnvelope->EndTime());
606 TimeBar.x = EnvelopeToScreenX(View, x: LoopedTime) - BarWidth / 2.0f;
607 TimeBar.Draw(Color: BarColor, Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
608 }
609 Ui()->ClipDisable();
610 }
611
612 {
613 using namespace std::chrono_literals;
614 CTimeStep UnitsPerLineX = 1ms;
615 static const CTimeStep UNIT_PER_LINE_OPTIONS_X[] = {5ms, 10ms, 25ms, 50ms, 100ms, 250ms, 500ms, 1s, 2s, 5s, 10s, 15s, 30s, 1min};
616 for(CTimeStep Value : UNIT_PER_LINE_OPTIONS_X)
617 {
618 if(Value.AsSeconds() / State.m_ZoomX.GetValue() * View.w < 160.0f)
619 UnitsPerLineX = Value;
620 }
621 int NumLinesX = State.m_ZoomX.GetValue() / UnitsPerLineX.AsSeconds() + 1;
622
623 Ui()->ClipEnable(pRect: &View);
624 Graphics()->TextureClear();
625 Graphics()->LinesBegin();
626 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.2f);
627
628 CTimeStep BaseValue = UnitsPerLineX * static_cast<int>(State.m_Offset.x * State.m_ZoomX.GetValue() / UnitsPerLineX.AsSeconds());
629 for(int i = 0; i <= NumLinesX; i++)
630 {
631 float Value = UnitsPerLineX.AsSeconds() * i - BaseValue.AsSeconds();
632 IGraphics::CLineItem LineItem(EnvelopeToScreenX(View, x: Value), View.y, EnvelopeToScreenX(View, x: Value), View.y + View.h);
633 Graphics()->LinesDraw(pArray: &LineItem, Num: 1);
634 }
635
636 Graphics()->LinesEnd();
637
638 Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
639 for(int i = 0; i <= NumLinesX; i++)
640 {
641 CTimeStep Value = UnitsPerLineX * i - BaseValue;
642 if(Value.AsSeconds() >= 0)
643 {
644 char aValueBuffer[16];
645 Value.Format(pBuffer: aValueBuffer, BufferSize: sizeof(aValueBuffer));
646
647 Ui()->TextRender()->Text(x: EnvelopeToScreenX(View, x: Value.AsSeconds()) + 1.0f, y: View.y + View.h - 8.0f, Size: 8.0f, pText: aValueBuffer);
648 }
649 }
650 Ui()->TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 1.0f);
651 Ui()->ClipDisable();
652 }
653
654 // render lines
655 {
656 float EndX = View.x + View.w;
657 float StartX = std::clamp(val: View.x + View.w * State.m_Offset.x, lo: View.x, hi: View.x + View.w);
658
659 float EndTime = ScreenToEnvelopeX(View, x: EndX);
660 float StartTime = ScreenToEnvelopeX(View, x: StartX);
661
662 Ui()->ClipEnable(pRect: &View);
663 Graphics()->TextureClear();
664 IGraphics::CLineItemBatch LineItemBatch;
665 for(int c = 0; c < pEnvelope->GetChannels(); c++)
666 {
667 Graphics()->LinesBatchBegin(pBatch: &LineItemBatch);
668 if(State.m_ActiveChannels & (1 << c))
669 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1);
670 else
671 Graphics()->SetColor(r: aColors[c].r * 0.5f, g: aColors[c].g * 0.5f, b: aColors[c].b * 0.5f, a: 1);
672
673 const int Steps = static_cast<int>(((EndX - StartX) / Ui()->Screen()->w) * Graphics()->ScreenWidth());
674 const float StepTime = (EndTime - StartTime) / static_cast<float>(Steps);
675 const float StepSize = (EndX - StartX) / static_cast<float>(Steps);
676
677 ColorRGBA Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
678 pEnvelope->Eval(Time: StartTime, Result&: Channels, Channels: c + 1);
679 float PrevTime = StartTime;
680 float PrevX = StartX;
681 float PrevY = EnvelopeToScreenY(View, y: Channels[c]);
682 for(int Step = 1; Step <= Steps; Step++)
683 {
684 float CurrentTime = StartTime + Step * StepTime;
685 if(CurrentTime >= EndTime)
686 {
687 CurrentTime = EndTime - 0.001f;
688 if(CurrentTime <= PrevTime)
689 break;
690 }
691
692 Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
693 pEnvelope->Eval(Time: CurrentTime, Result&: Channels, Channels: c + 1);
694 const float CurrentX = StartX + Step * StepSize;
695 const float CurrentY = EnvelopeToScreenY(View, y: Channels[c]);
696
697 const IGraphics::CLineItem Item = IGraphics::CLineItem(PrevX, PrevY, CurrentX, CurrentY);
698 Graphics()->LinesBatchDraw(pBatch: &LineItemBatch, pArray: &Item, Num: 1);
699
700 PrevTime = CurrentTime;
701 PrevX = CurrentX;
702 PrevY = CurrentY;
703 }
704 Graphics()->LinesBatchEnd(pBatch: &LineItemBatch);
705 }
706 Ui()->ClipDisable();
707 }
708
709 CUIRect InactiveRegionLeft{
710 .x: View.x,
711 .y: View.y,
712 .w: std::clamp(val: EnvelopeToScreenX(View, x: 0.0f) - View.x, lo: 0.0f, hi: View.w),
713 .h: View.h,
714 };
715 const float EndX = EnvelopeToScreenX(View, x: pEnvelope->EndTime());
716 CUIRect InactiveRegionRight{
717 .x: std::max(a: View.x, b: EndX),
718 .y: View.y,
719 .w: std::clamp(val: View.x + View.w - EndX, lo: 0.0f, hi: View.w),
720 .h: View.h,
721 };
722 InactiveRegionLeft.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
723 InactiveRegionRight.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.5f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
724
725 // render tangents for bezier curves
726 {
727 Ui()->ClipEnable(pRect: &View);
728 Graphics()->TextureClear();
729 Graphics()->LinesBegin();
730 for(int c = 0; c < pEnvelope->GetChannels(); c++)
731 {
732 if(!(State.m_ActiveChannels & (1 << c)))
733 continue;
734
735 for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++)
736 {
737 float PosX = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds());
738 float PosY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c]));
739
740 // Out-Tangent
741 if(i < (int)pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER)
742 {
743 float TangentX = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds());
744 float TangentY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c]));
745
746 if(Map()->IsTangentOutPointSelected(Index: i, Channel: c))
747 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
748 else
749 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 0.4f);
750
751 IGraphics::CLineItem LineItem(TangentX, TangentY, PosX, PosY);
752 Graphics()->LinesDraw(pArray: &LineItem, Num: 1);
753 }
754
755 // In-Tangent
756 if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER)
757 {
758 float TangentX = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds());
759 float TangentY = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c]));
760
761 if(Map()->IsTangentInPointSelected(Index: i, Channel: c))
762 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: 0.4f);
763 else
764 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 0.4f);
765
766 IGraphics::CLineItem LineItem(TangentX, TangentY, PosX, PosY);
767 Graphics()->LinesDraw(pArray: &LineItem, Num: 1);
768 }
769 }
770 }
771 Graphics()->LinesEnd();
772 Ui()->ClipDisable();
773 }
774
775 // render curve options
776 {
777 for(int i = 0; i < (int)pEnvelope->m_vPoints.size() - 1; i++)
778 {
779 float t0 = pEnvelope->m_vPoints[i].m_Time.AsSeconds();
780 float t1 = pEnvelope->m_vPoints[i + 1].m_Time.AsSeconds();
781
782 CUIRect CurveButton;
783 CurveButton.x = EnvelopeToScreenX(View, x: t0 + (t1 - t0) * 0.5f);
784 CurveButton.y = CurveBar.y;
785 CurveButton.h = CurveBar.h;
786 CurveButton.w = CurveBar.h;
787 CurveButton.x -= CurveButton.w / 2.0f;
788 const void *pId = &pEnvelope->m_vPoints[i].m_Curvetype;
789
790 if(CurveButton.x >= View.x)
791 {
792 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).");
793 if(ButtonResult == 1)
794 {
795 const int PrevCurve = pEnvelope->m_vPoints[i].m_Curvetype;
796 const int Direction = Input()->ShiftIsPressed() ? -1 : 1;
797 pEnvelope->m_vPoints[i].m_Curvetype = (pEnvelope->m_vPoints[i].m_Curvetype + Direction + NUM_CURVETYPES) % NUM_CURVETYPES;
798
799 Map()->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEditPoint>(args: Map(),
800 args&: Map()->m_SelectedEnvelope, args&: i, args: 0, args: CEditorActionEnvelopeEditPoint::EEditType::CURVE_TYPE, args: PrevCurve, args&: pEnvelope->m_vPoints[i].m_Curvetype));
801 Map()->OnModify();
802 }
803 else if(ButtonResult == 2)
804 {
805 m_PopupEnvelopeCurveType.m_pEnvelopeEditor = this;
806 m_PopupEnvelopeCurveType.m_SelectedPoint = i;
807 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);
808 }
809 }
810 }
811 }
812
813 // render colorbar
814 if(ShowColorBar)
815 {
816 RenderColorBar(ColorBar, pEnvelope);
817 }
818
819 // render handles
820 if(CurrentEnvelopeSwitched)
821 {
822 Map()->DeselectEnvPoints();
823 State.m_ResetZoom = true;
824 }
825
826 {
827 const auto &&ShowPopupEnvPoint = [&]() {
828 m_PopupEnvelopePoint.m_pEnvelopeEditor = this;
829 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);
830 };
831
832 if(m_Operation == EEnvelopeEditorOp::NONE)
833 {
834 UpdateHotEnvelopeObject(View, pEnvelope: pEnvelope.get(), ActiveChannels: State.m_ActiveChannels);
835 if(!Ui()->MouseButton(Index: 0))
836 Map()->m_EnvOpTracker.Stop(Switch: false);
837 }
838 else
839 {
840 Map()->m_EnvOpTracker.Begin(Operation: m_Operation);
841 }
842
843 Ui()->ClipEnable(pRect: &View);
844 Graphics()->TextureClear();
845 Graphics()->QuadsBegin();
846 for(int c = 0; c < pEnvelope->GetChannels(); c++)
847 {
848 if(!(State.m_ActiveChannels & (1 << c)))
849 continue;
850
851 for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++)
852 {
853 // point handle
854 {
855 CUIRect Final;
856 Final.x = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds());
857 Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c]));
858 Final.x -= 2.0f;
859 Final.y -= 2.0f;
860 Final.w = 4.0f;
861 Final.h = 4.0f;
862
863 const void *pId = &pEnvelope->m_vPoints[i].m_aValues[c];
864
865 if(Map()->IsEnvPointSelected(Index: i, Channel: c))
866 {
867 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
868 CUIRect Background = {
869 .x: Final.x - 0.2f * Final.w,
870 .y: Final.y - 0.2f * Final.h,
871 .w: Final.w * 1.4f,
872 .h: Final.h * 1.4f};
873 IGraphics::CQuadItem QuadItem(Background.x, Background.y, Background.w, Background.h);
874 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
875 }
876
877 if(Ui()->CheckActiveItem(pId))
878 {
879 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
880
881 if(m_Operation == EEnvelopeEditorOp::SELECT)
882 {
883 if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f)
884 {
885 m_Operation = EEnvelopeEditorOp::DRAG_POINT;
886
887 if(!Map()->IsEnvPointSelected(Index: i, Channel: c))
888 Map()->SelectEnvPoint(Index: i, Channel: c);
889 }
890 }
891
892 if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_X || m_Operation == EEnvelopeEditorOp::DRAG_POINT_Y)
893 {
894 if(Input()->ModifierIsPressed())
895 {
896 Ui()->SetMouseSlow(true);
897 }
898
899 if(Input()->ShiftIsPressed())
900 {
901 if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_Y)
902 {
903 m_Operation = EEnvelopeEditorOp::DRAG_POINT_X;
904 m_vAccurateDragValuesX.clear();
905 for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints)
906 m_vAccurateDragValuesX.push_back(x: pEnvelope->m_vPoints[SelectedIndex].m_Time.GetInternal());
907 }
908 else
909 {
910 float DeltaX = ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * 1000.0f;
911
912 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
913 {
914 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
915 CFixedTime BoundLow = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x));
916 CFixedTime BoundHigh = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w));
917 for(int j = 0; j < SelectedIndex; j++)
918 {
919 if(!Map()->IsEnvPointSelected(Index: j))
920 BoundLow = std::max(a: pEnvelope->m_vPoints[j].m_Time + CFixedTime(1), b: BoundLow);
921 }
922 for(int j = SelectedIndex + 1; j < (int)pEnvelope->m_vPoints.size(); j++)
923 {
924 if(!Map()->IsEnvPointSelected(Index: j))
925 BoundHigh = std::min(a: pEnvelope->m_vPoints[j].m_Time - CFixedTime(1), b: BoundHigh);
926 }
927
928 DeltaX = ClampDelta(Val: m_vAccurateDragValuesX[k], Delta: DeltaX, Min: BoundLow.GetInternal(), Max: BoundHigh.GetInternal());
929 }
930 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
931 {
932 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
933 m_vAccurateDragValuesX[k] += DeltaX;
934 pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: m_vAccurateDragValuesX[k]));
935 }
936 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
937 {
938 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
939 if(SelectedIndex == 0 && pEnvelope->m_vPoints[SelectedIndex].m_Time != CFixedTime(0))
940 {
941 RemoveTimeOffsetEnvelope(pEnvelope);
942 float Offset = m_vAccurateDragValuesX[k];
943 for(auto &Value : m_vAccurateDragValuesX)
944 Value -= Offset;
945 break;
946 }
947 }
948 }
949 }
950 else
951 {
952 if(m_Operation == EEnvelopeEditorOp::DRAG_POINT || m_Operation == EEnvelopeEditorOp::DRAG_POINT_X)
953 {
954 m_Operation = EEnvelopeEditorOp::DRAG_POINT_Y;
955 m_vAccurateDragValuesY.clear();
956 for(auto [SelectedIndex, SelectedChannel] : Map()->m_vSelectedEnvelopePoints)
957 m_vAccurateDragValuesY.push_back(x: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel]);
958 }
959 else
960 {
961 float DeltaY = ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * 1024.0f;
962 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
963 {
964 auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k];
965 m_vAccurateDragValuesY[k] -= DeltaY;
966 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vAccurateDragValuesY[k]);
967
968 if(pEnvelope->GetChannels() == 1 || pEnvelope->GetChannels() == 4)
969 {
970 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::clamp(val: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel], lo: 0, hi: 1024);
971 m_vAccurateDragValuesY[k] = std::clamp<float>(val: m_vAccurateDragValuesY[k], lo: 0, hi: 1024);
972 }
973 }
974 }
975 }
976 }
977
978 if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU)
979 {
980 if(!Ui()->MouseButton(Index: 1))
981 {
982 if(Map()->m_vSelectedEnvelopePoints.size() == 1)
983 {
984 Map()->m_UpdateEnvPointInfo = true;
985 ShowPopupEnvPoint();
986 }
987 else if(Map()->m_vSelectedEnvelopePoints.size() > 1)
988 {
989 m_PopupEnvelopePointMulti.m_pEnvelopeEditor = this;
990 Ui()->DoPopupMenu(pId: &m_PopupEnvelopePointMulti, X: Ui()->MouseX(), Y: Ui()->MouseY(), Width: 100, Height: 24, pContext: &m_PopupEnvelopePointMulti, pfnFunc: CPopupEnvelopePointMulti::Render);
991 }
992 Ui()->SetActiveItem(nullptr);
993 m_Operation = EEnvelopeEditorOp::NONE;
994 }
995 }
996 else if(!Ui()->MouseButton(Index: 0))
997 {
998 Ui()->SetActiveItem(nullptr);
999 Map()->m_SelectedQuadEnvelope = -1;
1000
1001 if(m_Operation == EEnvelopeEditorOp::SELECT)
1002 {
1003 if(Input()->ShiftIsPressed())
1004 Map()->ToggleEnvPoint(Index: i, Channel: c);
1005 else
1006 Map()->SelectEnvPoint(Index: i, Channel: c);
1007 }
1008
1009 m_Operation = EEnvelopeEditorOp::NONE;
1010 Map()->OnModify();
1011 }
1012
1013 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1014 }
1015 else if(Ui()->HotItem() == pId)
1016 {
1017 if(Ui()->MouseButton(Index: 0))
1018 {
1019 Ui()->SetActiveItem(pId);
1020 m_Operation = EEnvelopeEditorOp::SELECT;
1021 Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope;
1022 m_MouseStart = Ui()->MousePos();
1023 }
1024 else if(Ui()->MouseButtonClicked(Index: 1))
1025 {
1026 if(Input()->ShiftIsPressed())
1027 {
1028 Map()->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionDeleteEnvelopePoint>(args: Map(), args&: Map()->m_SelectedEnvelope, args&: i));
1029 }
1030 else
1031 {
1032 m_Operation = EEnvelopeEditorOp::CONTEXT_MENU;
1033 if(!Map()->IsEnvPointSelected(Index: i, Channel: c))
1034 Map()->SelectEnvPoint(Index: i, Channel: c);
1035 Ui()->SetActiveItem(pId);
1036 }
1037 }
1038
1039 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1040 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1041 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.");
1042 Editor()->m_pUiGotContext = pId;
1043 }
1044 else
1045 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f);
1046
1047 IGraphics::CQuadItem QuadItem(Final.x, Final.y, Final.w, Final.h);
1048 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
1049 }
1050
1051 // tangent handles for bezier curves
1052 if(i >= 0 && i < (int)pEnvelope->m_vPoints.size())
1053 {
1054 // Out-Tangent handle
1055 if(i < (int)pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER)
1056 {
1057 CUIRect Final;
1058 Final.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds());
1059 Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c]));
1060 Final.x -= 2.0f;
1061 Final.y -= 2.0f;
1062 Final.w = 4.0f;
1063 Final.h = 4.0f;
1064
1065 // handle logic
1066 const void *pId = &pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c];
1067
1068 if(Map()->IsTangentOutPointSelected(Index: i, Channel: c))
1069 {
1070 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1071 IGraphics::CFreeformItem FreeformItem(
1072 Final.x + Final.w / 2.0f,
1073 Final.y - 1,
1074 Final.x + Final.w / 2.0f,
1075 Final.y - 1,
1076 Final.x + Final.w + 1,
1077 Final.y + Final.h + 1,
1078 Final.x - 1,
1079 Final.y + Final.h + 1);
1080 Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1);
1081 }
1082
1083 if(Ui()->CheckActiveItem(pId))
1084 {
1085 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1086
1087 if(m_Operation == EEnvelopeEditorOp::SELECT)
1088 {
1089 if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f)
1090 {
1091 m_Operation = EEnvelopeEditorOp::DRAG_POINT;
1092
1093 m_vAccurateDragValuesX = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c].GetInternal())};
1094 m_vAccurateDragValuesY = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c])};
1095
1096 if(!Map()->IsTangentOutPointSelected(Index: i, Channel: c))
1097 Map()->SelectTangentOutPoint(Index: i, Channel: c);
1098 }
1099 }
1100
1101 if(m_Operation == EEnvelopeEditorOp::DRAG_POINT)
1102 {
1103 if(Input()->ModifierIsPressed())
1104 {
1105 Ui()->SetMouseSlow(true);
1106 }
1107
1108 m_vAccurateDragValuesX[0] += ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * 1000.0f;
1109 m_vAccurateDragValuesY[0] -= ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * 1024.0f;
1110
1111 pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c] = CFixedTime(std::round(x: m_vAccurateDragValuesX[0]));
1112 pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c] = std::round(x: m_vAccurateDragValuesY[0]);
1113
1114 // clamp time value
1115 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);
1116 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());
1117 }
1118
1119 if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU)
1120 {
1121 if(!Ui()->MouseButton(Index: 1))
1122 {
1123 if(Map()->IsTangentOutPointSelected(Index: i, Channel: c))
1124 {
1125 Map()->m_UpdateEnvPointInfo = true;
1126 ShowPopupEnvPoint();
1127 }
1128 Ui()->SetActiveItem(nullptr);
1129 m_Operation = EEnvelopeEditorOp::NONE;
1130 }
1131 }
1132 else if(!Ui()->MouseButton(Index: 0))
1133 {
1134 Ui()->SetActiveItem(nullptr);
1135 Map()->m_SelectedQuadEnvelope = -1;
1136
1137 if(m_Operation == EEnvelopeEditorOp::SELECT)
1138 Map()->SelectTangentOutPoint(Index: i, Channel: c);
1139
1140 m_Operation = EEnvelopeEditorOp::NONE;
1141 Map()->OnModify();
1142 }
1143
1144 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1145 }
1146 else if(Ui()->HotItem() == pId)
1147 {
1148 if(Ui()->MouseButton(Index: 0))
1149 {
1150 Ui()->SetActiveItem(pId);
1151 m_Operation = EEnvelopeEditorOp::SELECT;
1152 Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope;
1153 m_MouseStart = Ui()->MousePos();
1154 }
1155 else if(Ui()->MouseButtonClicked(Index: 1))
1156 {
1157 if(Input()->ShiftIsPressed())
1158 {
1159 Map()->SelectTangentOutPoint(Index: i, Channel: c);
1160 pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c] = CFixedTime(0);
1161 pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c] = 0.0f;
1162 Map()->OnModify();
1163 }
1164 else
1165 {
1166 m_Operation = EEnvelopeEditorOp::CONTEXT_MENU;
1167 Map()->SelectTangentOutPoint(Index: i, Channel: c);
1168 Ui()->SetActiveItem(pId);
1169 }
1170 }
1171
1172 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1173 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1174 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.");
1175 Editor()->m_pUiGotContext = pId;
1176 }
1177 else
1178 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f);
1179
1180 // draw triangle
1181 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);
1182 Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1);
1183 }
1184
1185 // In-Tangent handle
1186 if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER)
1187 {
1188 CUIRect Final;
1189 Final.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds());
1190 Final.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c]));
1191 Final.x -= 2.0f;
1192 Final.y -= 2.0f;
1193 Final.w = 4.0f;
1194 Final.h = 4.0f;
1195
1196 // handle logic
1197 const void *pId = &pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c];
1198
1199 if(Map()->IsTangentInPointSelected(Index: i, Channel: c))
1200 {
1201 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1202 IGraphics::CFreeformItem FreeformItem(
1203 Final.x + Final.w / 2.0f,
1204 Final.y - 1,
1205 Final.x + Final.w / 2.0f,
1206 Final.y - 1,
1207 Final.x + Final.w + 1,
1208 Final.y + Final.h + 1,
1209 Final.x - 1,
1210 Final.y + Final.h + 1);
1211 Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1);
1212 }
1213
1214 if(Ui()->CheckActiveItem(pId))
1215 {
1216 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1217
1218 if(m_Operation == EEnvelopeEditorOp::SELECT)
1219 {
1220 if(distance_squared(a: m_MouseStart, b: Ui()->MousePos()) > 20.0f)
1221 {
1222 m_Operation = EEnvelopeEditorOp::DRAG_POINT;
1223
1224 m_vAccurateDragValuesX = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c].GetInternal())};
1225 m_vAccurateDragValuesY = {static_cast<float>(pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c])};
1226
1227 if(!Map()->IsTangentInPointSelected(Index: i, Channel: c))
1228 Map()->SelectTangentInPoint(Index: i, Channel: c);
1229 }
1230 }
1231
1232 if(m_Operation == EEnvelopeEditorOp::DRAG_POINT)
1233 {
1234 if(Input()->ModifierIsPressed())
1235 {
1236 Ui()->SetMouseSlow(true);
1237 }
1238
1239 m_vAccurateDragValuesX[0] += ScreenToEnvelopeDeltaX(View, DeltaX: Ui()->MouseDeltaX()) * 1000.0f;
1240 m_vAccurateDragValuesY[0] -= ScreenToEnvelopeDeltaY(View, DeltaY: Ui()->MouseDeltaY()) * 1024.0f;
1241
1242 pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c] = CFixedTime(std::round(x: m_vAccurateDragValuesX[0]));
1243 pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c] = std::round(x: m_vAccurateDragValuesY[0]);
1244
1245 // clamp time value
1246 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));
1247 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);
1248 }
1249
1250 if(m_Operation == EEnvelopeEditorOp::CONTEXT_MENU)
1251 {
1252 if(!Ui()->MouseButton(Index: 1))
1253 {
1254 if(Map()->IsTangentInPointSelected(Index: i, Channel: c))
1255 {
1256 Map()->m_UpdateEnvPointInfo = true;
1257 ShowPopupEnvPoint();
1258 }
1259 Ui()->SetActiveItem(nullptr);
1260 m_Operation = EEnvelopeEditorOp::NONE;
1261 }
1262 }
1263 else if(!Ui()->MouseButton(Index: 0))
1264 {
1265 Ui()->SetActiveItem(nullptr);
1266 Map()->m_SelectedQuadEnvelope = -1;
1267
1268 if(m_Operation == EEnvelopeEditorOp::SELECT)
1269 Map()->SelectTangentInPoint(Index: i, Channel: c);
1270
1271 m_Operation = EEnvelopeEditorOp::NONE;
1272 Map()->OnModify();
1273 }
1274
1275 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1276 }
1277 else if(Ui()->HotItem() == pId)
1278 {
1279 if(Ui()->MouseButton(Index: 0))
1280 {
1281 Ui()->SetActiveItem(pId);
1282 m_Operation = EEnvelopeEditorOp::SELECT;
1283 Map()->m_SelectedQuadEnvelope = Map()->m_SelectedEnvelope;
1284 m_MouseStart = Ui()->MousePos();
1285 }
1286 else if(Ui()->MouseButtonClicked(Index: 1))
1287 {
1288 if(Input()->ShiftIsPressed())
1289 {
1290 Map()->SelectTangentInPoint(Index: i, Channel: c);
1291 pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c] = CFixedTime(0);
1292 pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c] = 0.0f;
1293 Map()->OnModify();
1294 }
1295 else
1296 {
1297 m_Operation = EEnvelopeEditorOp::CONTEXT_MENU;
1298 Map()->SelectTangentInPoint(Index: i, Channel: c);
1299 Ui()->SetActiveItem(pId);
1300 }
1301 }
1302
1303 Editor()->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1304 Graphics()->SetColor(r: 1, g: 1, b: 1, a: 1);
1305 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.");
1306 Editor()->m_pUiGotContext = pId;
1307 }
1308 else
1309 Graphics()->SetColor(r: aColors[c].r, g: aColors[c].g, b: aColors[c].b, a: 1.0f);
1310
1311 // draw triangle
1312 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);
1313 Graphics()->QuadsDrawFreeform(pArray: &FreeformItem, Num: 1);
1314 }
1315 }
1316 }
1317 }
1318 Graphics()->QuadsEnd();
1319 Ui()->ClipDisable();
1320 }
1321
1322 // handle scaling
1323 if(m_Operation == EEnvelopeEditorOp::NONE && !m_NameInput.IsActive() && Input()->KeyIsPressed(Key: KEY_S) && !Input()->ModifierIsPressed() && !Map()->m_vSelectedEnvelopePoints.empty())
1324 {
1325 m_Operation = EEnvelopeEditorOp::SCALE;
1326 m_ScaleFactor.x = 1.0f;
1327 m_ScaleFactor.y = 1.0f;
1328 auto [FirstPointIndex, FirstPointChannel] = Map()->m_vSelectedEnvelopePoints.front();
1329
1330 float MaximumX = pEnvelope->m_vPoints[FirstPointIndex].m_Time.GetInternal();
1331 float MinimumX = MaximumX;
1332 m_vInitialPositionsX.clear();
1333 for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints)
1334 {
1335 float Value = pEnvelope->m_vPoints[SelectedIndex].m_Time.GetInternal();
1336 m_vInitialPositionsX.push_back(x: Value);
1337 MaximumX = std::max(a: MaximumX, b: Value);
1338 MinimumX = std::min(a: MinimumX, b: Value);
1339 }
1340 m_Midpoint.x = (MaximumX - MinimumX) / 2.0f + MinimumX;
1341
1342 float MaximumY = pEnvelope->m_vPoints[FirstPointIndex].m_aValues[FirstPointChannel];
1343 float MinimumY = MaximumY;
1344 m_vInitialPositionsY.clear();
1345 for(auto [SelectedIndex, SelectedChannel] : Map()->m_vSelectedEnvelopePoints)
1346 {
1347 float Value = pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel];
1348 m_vInitialPositionsY.push_back(x: Value);
1349 MaximumY = std::max(a: MaximumY, b: Value);
1350 MinimumY = std::min(a: MinimumY, b: Value);
1351 }
1352 m_Midpoint.y = (MaximumY - MinimumY) / 2.0f + MinimumY;
1353 }
1354
1355 if(m_Operation == EEnvelopeEditorOp::SCALE)
1356 {
1357 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.");
1358
1359 if(Input()->ModifierIsPressed())
1360 {
1361 Ui()->SetMouseSlow(true);
1362 }
1363
1364 if(Input()->ShiftIsPressed())
1365 {
1366 m_ScaleFactor.x += Ui()->MouseDeltaX() / Graphics()->ScreenWidth() * 10.0f;
1367 float Midpoint = Input()->AltIsPressed() ? m_Midpoint.x : 0.0f;
1368 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
1369 {
1370 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
1371 CFixedTime BoundLow = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x));
1372 CFixedTime BoundHigh = CFixedTime::FromSeconds(Seconds: ScreenToEnvelopeX(View, x: View.x + View.w));
1373 for(int j = 0; j < SelectedIndex; j++)
1374 {
1375 if(!Map()->IsEnvPointSelected(Index: j))
1376 BoundLow = std::max(a: pEnvelope->m_vPoints[j].m_Time + CFixedTime(1), b: BoundLow);
1377 }
1378 for(int j = SelectedIndex + 1; j < (int)pEnvelope->m_vPoints.size(); j++)
1379 {
1380 if(!Map()->IsEnvPointSelected(Index: j))
1381 BoundHigh = std::min(a: pEnvelope->m_vPoints[j].m_Time - CFixedTime(1), b: BoundHigh);
1382 }
1383
1384 float Value = m_vInitialPositionsX[k];
1385 float ScaleBoundLow = (BoundLow.GetInternal() - Midpoint) / (Value - Midpoint);
1386 float ScaleBoundHigh = (BoundHigh.GetInternal() - Midpoint) / (Value - Midpoint);
1387 float ScaleBoundMin = std::min(a: ScaleBoundLow, b: ScaleBoundHigh);
1388 float ScaleBoundMax = std::max(a: ScaleBoundLow, b: ScaleBoundHigh);
1389 m_ScaleFactor.x = std::clamp(val: m_ScaleFactor.x, lo: ScaleBoundMin, hi: ScaleBoundMax);
1390 }
1391
1392 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
1393 {
1394 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
1395 float ScaleMinimum = m_vInitialPositionsX[k] - Midpoint > CFixedTime(1).AsSeconds() ? CFixedTime(1).AsSeconds() / (m_vInitialPositionsX[k] - Midpoint) : 0.0f;
1396 float ScaleFactor = std::max(a: ScaleMinimum, b: m_ScaleFactor.x);
1397 pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: (m_vInitialPositionsX[k] - Midpoint) * ScaleFactor + Midpoint));
1398 }
1399 for(size_t k = 1; k < pEnvelope->m_vPoints.size(); k++)
1400 {
1401 if(pEnvelope->m_vPoints[k].m_Time <= pEnvelope->m_vPoints[k - 1].m_Time)
1402 pEnvelope->m_vPoints[k].m_Time = pEnvelope->m_vPoints[k - 1].m_Time + CFixedTime(1);
1403 }
1404 for(auto [SelectedIndex, _] : Map()->m_vSelectedEnvelopePoints)
1405 {
1406 if(SelectedIndex == 0 && pEnvelope->m_vPoints[SelectedIndex].m_Time != CFixedTime(0))
1407 {
1408 float Offset = pEnvelope->m_vPoints[0].m_Time.GetInternal();
1409 RemoveTimeOffsetEnvelope(pEnvelope);
1410 m_Midpoint.x -= Offset;
1411 for(auto &Value : m_vInitialPositionsX)
1412 Value -= Offset;
1413 break;
1414 }
1415 }
1416 }
1417 else
1418 {
1419 m_ScaleFactor.y -= Ui()->MouseDeltaY() / Graphics()->ScreenHeight() * 10.0f;
1420 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
1421 {
1422 auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k];
1423 if(Input()->AltIsPressed())
1424 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: (m_vInitialPositionsY[k] - m_Midpoint.y) * m_ScaleFactor.y + m_Midpoint.y);
1425 else
1426 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vInitialPositionsY[k] * m_ScaleFactor.y);
1427
1428 if(pEnvelope->GetChannels() == 1 || pEnvelope->GetChannels() == 4)
1429 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::clamp(val: pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel], lo: 0, hi: 1024);
1430 }
1431 }
1432
1433 if(Ui()->MouseButton(Index: 0))
1434 {
1435 m_Operation = EEnvelopeEditorOp::NONE;
1436 Map()->m_EnvOpTracker.Stop(Switch: false);
1437 }
1438 else if(Ui()->MouseButton(Index: 1) || Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ESCAPE))
1439 {
1440 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
1441 {
1442 int SelectedIndex = Map()->m_vSelectedEnvelopePoints[k].first;
1443 pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(std::round(x: m_vInitialPositionsX[k]));
1444 }
1445 for(size_t k = 0; k < Map()->m_vSelectedEnvelopePoints.size(); k++)
1446 {
1447 auto [SelectedIndex, SelectedChannel] = Map()->m_vSelectedEnvelopePoints[k];
1448 pEnvelope->m_vPoints[SelectedIndex].m_aValues[SelectedChannel] = std::round(x: m_vInitialPositionsY[k]);
1449 }
1450 RemoveTimeOffsetEnvelope(pEnvelope);
1451 m_Operation = EEnvelopeEditorOp::NONE;
1452 }
1453 }
1454
1455 // handle box selection
1456 if(m_Operation == EEnvelopeEditorOp::BOX_SELECT)
1457 {
1458 Ui()->ClipEnable(pRect: &View);
1459 CUIRect SelectionRect;
1460 SelectionRect.x = m_MouseStart.x;
1461 SelectionRect.y = m_MouseStart.y;
1462 SelectionRect.w = Ui()->MouseX() - m_MouseStart.x;
1463 SelectionRect.h = Ui()->MouseY() - m_MouseStart.y;
1464 SelectionRect.DrawOutline(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f));
1465 Ui()->ClipDisable();
1466
1467 if(!Ui()->MouseButton(Index: 0))
1468 {
1469 m_Operation = EEnvelopeEditorOp::NONE;
1470 Ui()->SetActiveItem(nullptr);
1471
1472 float TimeStart = ScreenToEnvelopeX(View, x: m_MouseStart.x);
1473 float TimeEnd = ScreenToEnvelopeX(View, x: Ui()->MouseX());
1474 float ValueStart = ScreenToEnvelopeY(View, y: m_MouseStart.y);
1475 float ValueEnd = ScreenToEnvelopeY(View, y: Ui()->MouseY());
1476
1477 float TimeMin = std::min(a: TimeStart, b: TimeEnd);
1478 float TimeMax = std::max(a: TimeStart, b: TimeEnd);
1479 float ValueMin = std::min(a: ValueStart, b: ValueEnd);
1480 float ValueMax = std::max(a: ValueStart, b: ValueEnd);
1481
1482 if(!Input()->ShiftIsPressed())
1483 Map()->DeselectEnvPoints();
1484
1485 for(int i = 0; i < (int)pEnvelope->m_vPoints.size(); i++)
1486 {
1487 for(int c = 0; c < pEnvelope->GetChannels(); c++)
1488 {
1489 if(!(State.m_ActiveChannels & (1 << c)))
1490 continue;
1491
1492 float Time = pEnvelope->m_vPoints[i].m_Time.AsSeconds();
1493 float Value = fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c]);
1494
1495 if(in_range(a: Time, lower: TimeMin, upper: TimeMax) && in_range(a: Value, lower: ValueMin, upper: ValueMax))
1496 Map()->ToggleEnvPoint(Index: i, Channel: c);
1497 }
1498 }
1499 }
1500 }
1501 }
1502}
1503
1504void CEnvelopeEditor::RenderColorBar(CUIRect ColorBar, const std::shared_ptr<CEnvelope> &pEnvelope)
1505{
1506 if(pEnvelope->m_vPoints.size() < 2)
1507 {
1508 return;
1509 }
1510 const float ViewStartTime = ScreenToEnvelopeX(View: ColorBar, x: ColorBar.x);
1511 const float ViewEndTime = ScreenToEnvelopeX(View: ColorBar, x: ColorBar.x + ColorBar.w);
1512 if(ViewEndTime < 0.0f || ViewStartTime > pEnvelope->EndTime())
1513 {
1514 return;
1515 }
1516 const float StartX = std::max(a: EnvelopeToScreenX(View: ColorBar, x: 0.0f), b: ColorBar.x);
1517 const float TotalWidth = std::min(a: EnvelopeToScreenX(View: ColorBar, x: pEnvelope->EndTime()) - StartX, b: ColorBar.x + ColorBar.w - StartX);
1518
1519 Ui()->ClipEnable(pRect: &ColorBar);
1520 CUIRect ColorBarBackground = CUIRect{.x: StartX, .y: ColorBar.y, .w: TotalWidth, .h: ColorBar.h};
1521 Editor()->RenderBackground(View: ColorBarBackground, Texture: Editor()->m_CheckerTexture, Size: ColorBarBackground.h, Brightness: 1.0f);
1522 Graphics()->TextureClear();
1523 Graphics()->QuadsBegin();
1524
1525 int PointBeginIndex = pEnvelope->FindPointIndex(Time: CFixedTime::FromSeconds(Seconds: ViewStartTime));
1526 if(PointBeginIndex == -1)
1527 {
1528 PointBeginIndex = 0;
1529 }
1530 int PointEndIndex = pEnvelope->FindPointIndex(Time: CFixedTime::FromSeconds(Seconds: ViewEndTime));
1531 if(PointEndIndex == -1)
1532 {
1533 PointEndIndex = (int)pEnvelope->m_vPoints.size() - 2;
1534 }
1535 for(int PointIndex = PointBeginIndex; PointIndex <= PointEndIndex; PointIndex++)
1536 {
1537 const auto &PointStart = pEnvelope->m_vPoints[PointIndex];
1538 const auto &PointEnd = pEnvelope->m_vPoints[PointIndex + 1];
1539 const float PointStartTime = PointStart.m_Time.AsSeconds();
1540 const float PointEndTime = PointEnd.m_Time.AsSeconds();
1541
1542 int Steps;
1543 if(PointStart.m_Curvetype == CURVETYPE_LINEAR || PointStart.m_Curvetype == CURVETYPE_STEP)
1544 {
1545 Steps = 1; // let the GPU do the work
1546 }
1547 else
1548 {
1549 const float ClampedPointStartX = std::max(a: EnvelopeToScreenX(View: ColorBar, x: PointStartTime), b: ColorBar.x);
1550 const float ClampedPointEndX = std::min(a: EnvelopeToScreenX(View: ColorBar, x: PointEndTime), b: ColorBar.x + ColorBar.w);
1551 Steps = std::clamp(val: (int)std::sqrt(x: 5.0f * (ClampedPointEndX - ClampedPointStartX)), lo: 1, hi: 250);
1552 }
1553 const float OverallSectionStartTime = Steps == 1 ? PointStartTime : std::max(a: PointStartTime, b: ViewStartTime);
1554 const float OverallSectionEndTime = Steps == 1 ? PointEndTime : std::min(a: PointEndTime, b: ViewEndTime);
1555 float SectionStartTime = OverallSectionStartTime;
1556 float SectionStartX = EnvelopeToScreenX(View: ColorBar, x: SectionStartTime);
1557 for(int Step = 1; Step <= Steps; Step++)
1558 {
1559 const float SectionEndTime = OverallSectionStartTime + (OverallSectionEndTime - OverallSectionStartTime) * (Step / (float)Steps);
1560 const float SectionEndX = EnvelopeToScreenX(View: ColorBar, x: SectionEndTime);
1561
1562 ColorRGBA StartColor;
1563 if(Step == 1 && OverallSectionStartTime == PointStartTime)
1564 {
1565 StartColor = PointStart.ColorValue();
1566 }
1567 else
1568 {
1569 StartColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
1570 pEnvelope->Eval(Time: SectionStartTime, Result&: StartColor, Channels: 4);
1571 }
1572
1573 ColorRGBA EndColor;
1574 if(PointStart.m_Curvetype == CURVETYPE_STEP)
1575 {
1576 EndColor = StartColor;
1577 }
1578 else if(Step == Steps && OverallSectionEndTime == PointEndTime)
1579 {
1580 EndColor = PointEnd.ColorValue();
1581 }
1582 else
1583 {
1584 EndColor = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
1585 pEnvelope->Eval(Time: SectionEndTime, Result&: EndColor, Channels: 4);
1586 }
1587
1588 Graphics()->SetColor4(TopLeft: StartColor, TopRight: EndColor, BottomLeft: StartColor, BottomRight: EndColor);
1589 const IGraphics::CQuadItem QuadItem(SectionStartX, ColorBar.y, SectionEndX - SectionStartX, ColorBar.h);
1590 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
1591
1592 SectionStartTime = SectionEndTime;
1593 SectionStartX = SectionEndX;
1594 }
1595 }
1596 Graphics()->QuadsEnd();
1597 Ui()->ClipDisable();
1598 ColorBarBackground.h -= Ui()->Screen()->h / Graphics()->ScreenHeight(); // hack to fix alignment of bottom border
1599 ColorBarBackground.DrawOutline(Color: ColorRGBA(0.7f, 0.7f, 0.7f, 1.0f));
1600}
1601
1602void CEnvelopeEditor::UpdateHotEnvelopeObject(const CUIRect &View, const CEnvelope *pEnvelope, int ActiveChannels)
1603{
1604 if(!Ui()->MouseInside(pRect: &View))
1605 return;
1606
1607 const vec2 MousePos = Ui()->MousePos();
1608
1609 float MinDist = 200.0f;
1610 const void *pMinPointId = nullptr;
1611
1612 const auto UpdateMinimum = [&](vec2 Position, const void *pId) {
1613 const float CurrDist = distance_squared(a: Position, b: MousePos);
1614 if(CurrDist < MinDist)
1615 {
1616 MinDist = CurrDist;
1617 pMinPointId = pId;
1618 }
1619 };
1620
1621 for(size_t i = 0; i < pEnvelope->m_vPoints.size(); i++)
1622 {
1623 for(int c = pEnvelope->GetChannels() - 1; c >= 0; c--)
1624 {
1625 if(!(ActiveChannels & (1 << c)))
1626 continue;
1627
1628 if(i > 0 && pEnvelope->m_vPoints[i - 1].m_Curvetype == CURVETYPE_BEZIER)
1629 {
1630 vec2 Position;
1631 Position.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]).AsSeconds());
1632 Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaY[c]));
1633 UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_Bezier.m_aInTangentDeltaX[c]);
1634 }
1635
1636 if(i < pEnvelope->m_vPoints.size() - 1 && pEnvelope->m_vPoints[i].m_Curvetype == CURVETYPE_BEZIER)
1637 {
1638 vec2 Position;
1639 Position.x = EnvelopeToScreenX(View, x: (pEnvelope->m_vPoints[i].m_Time + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]).AsSeconds());
1640 Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c] + pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaY[c]));
1641 UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_Bezier.m_aOutTangentDeltaX[c]);
1642 }
1643
1644 vec2 Position;
1645 Position.x = EnvelopeToScreenX(View, x: pEnvelope->m_vPoints[i].m_Time.AsSeconds());
1646 Position.y = EnvelopeToScreenY(View, y: fx2f(v: pEnvelope->m_vPoints[i].m_aValues[c]));
1647 UpdateMinimum(Position, &pEnvelope->m_vPoints[i].m_aValues[c]);
1648 }
1649 }
1650
1651 if(pMinPointId != nullptr)
1652 {
1653 Ui()->SetHotItem(pMinPointId);
1654 }
1655 else if(!Map()->m_EnvelopeEvaluator.m_Animate)
1656 {
1657 float Time = Map()->m_EnvelopeEvaluator.m_AnimateTime * Map()->m_EnvelopeEvaluator.m_AnimateSpeed;
1658 float LoopedTime = std::fmod(x: Time, y: pEnvelope->EndTime());
1659 if(absolute(a: EnvelopeToScreenX(View, x: Time) - MousePos.x) < 20.0f || absolute(a: EnvelopeToScreenX(View, x: LoopedTime) - MousePos.x) < 20.0f)
1660 {
1661 Ui()->SetHotItem(&Map()->m_EnvelopeEvaluator.m_AnimateTime);
1662 }
1663 }
1664}
1665
1666void CEnvelopeEditor::ZoomAdaptOffsetX(float ZoomFactor, const CUIRect &View)
1667{
1668 float PosX = g_Config.m_EdZoomTarget ? (Ui()->MouseX() - View.x) / View.w : 0.5f;
1669 Map()->m_EnvelopeEditorState.m_Offset.x = PosX - (PosX - Map()->m_EnvelopeEditorState.m_Offset.x) * ZoomFactor;
1670}
1671
1672void CEnvelopeEditor::UpdateZoomEnvelopeX(const CUIRect &View)
1673{
1674 float OldZoom = Map()->m_EnvelopeEditorState.m_ZoomX.GetValue();
1675 if(Map()->m_EnvelopeEditorState.m_ZoomX.UpdateValue())
1676 ZoomAdaptOffsetX(ZoomFactor: OldZoom / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue(), View);
1677}
1678
1679void CEnvelopeEditor::ZoomAdaptOffsetY(float ZoomFactor, const CUIRect &View)
1680{
1681 float PosY = g_Config.m_EdZoomTarget ? 1.0f - (Ui()->MouseY() - View.y) / View.h : 0.5f;
1682 Map()->m_EnvelopeEditorState.m_Offset.y = PosY - (PosY - Map()->m_EnvelopeEditorState.m_Offset.y) * ZoomFactor;
1683}
1684
1685void CEnvelopeEditor::UpdateZoomEnvelopeY(const CUIRect &View)
1686{
1687 float OldZoom = Map()->m_EnvelopeEditorState.m_ZoomY.GetValue();
1688 if(Map()->m_EnvelopeEditorState.m_ZoomY.UpdateValue())
1689 ZoomAdaptOffsetY(ZoomFactor: OldZoom / Map()->m_EnvelopeEditorState.m_ZoomY.GetValue(), View);
1690}
1691
1692void CEnvelopeEditor::ResetZoomEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope, int ActiveChannels)
1693{
1694 auto [Bottom, Top] = pEnvelope->GetValueRange(ChannelMask: ActiveChannels);
1695 float EndTime = pEnvelope->EndTime();
1696 float ValueRange = absolute(a: Top - Bottom);
1697 CState &State = Map()->m_EnvelopeEditorState;
1698
1699 if(ValueRange < State.m_ZoomY.GetMinValue())
1700 {
1701 // Set view to some sane default if range is too small
1702 State.m_Offset.y = 0.5f - ValueRange / State.m_ZoomY.GetMinValue() / 2.0f - Bottom / State.m_ZoomY.GetMinValue();
1703 State.m_ZoomY.SetValueInstant(State.m_ZoomY.GetMinValue());
1704 }
1705 else if(ValueRange > State.m_ZoomY.GetMaxValue())
1706 {
1707 State.m_Offset.y = -Bottom / State.m_ZoomY.GetMaxValue();
1708 State.m_ZoomY.SetValueInstant(State.m_ZoomY.GetMaxValue());
1709 }
1710 else
1711 {
1712 // calculate biggest possible spacing
1713 float SpacingFactor = std::min(a: 1.25f, b: State.m_ZoomY.GetMaxValue() / ValueRange);
1714 State.m_ZoomY.SetValueInstant(SpacingFactor * ValueRange);
1715 float Space = 1.0f / SpacingFactor;
1716 float Spacing = (1.0f - Space) / 2.0f;
1717
1718 if(Top >= 0 && Bottom >= 0)
1719 State.m_Offset.y = Spacing - Bottom / State.m_ZoomY.GetValue();
1720 else if(Top <= 0 && Bottom <= 0)
1721 State.m_Offset.y = Spacing - Bottom / State.m_ZoomY.GetValue();
1722 else
1723 State.m_Offset.y = Spacing + Space * absolute(a: Bottom) / ValueRange;
1724 }
1725
1726 if(EndTime < State.m_ZoomX.GetMinValue())
1727 {
1728 State.m_Offset.x = 0.5f - EndTime / State.m_ZoomX.GetMinValue();
1729 State.m_ZoomX.SetValueInstant(State.m_ZoomX.GetMinValue());
1730 }
1731 else if(EndTime > State.m_ZoomX.GetMaxValue())
1732 {
1733 State.m_Offset.x = 0.0f;
1734 State.m_ZoomX.SetValueInstant(State.m_ZoomX.GetMaxValue());
1735 }
1736 else
1737 {
1738 float SpacingFactor = std::min(a: 1.25f, b: State.m_ZoomX.GetMaxValue() / EndTime);
1739 State.m_ZoomX.SetValueInstant(SpacingFactor * EndTime);
1740 float Space = 1.0f / SpacingFactor;
1741 float Spacing = (1.0f - Space) / 2.0f;
1742 State.m_Offset.x = Spacing;
1743 }
1744}
1745
1746float CEnvelopeEditor::ScreenToEnvelopeX(const CUIRect &View, float x) const
1747{
1748 return (x - View.x - View.w * Map()->m_EnvelopeEditorState.m_Offset.x) / View.w * Map()->m_EnvelopeEditorState.m_ZoomX.GetValue();
1749}
1750
1751float CEnvelopeEditor::EnvelopeToScreenX(const CUIRect &View, float x) const
1752{
1753 return View.x + View.w * Map()->m_EnvelopeEditorState.m_Offset.x + x / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue() * View.w;
1754}
1755
1756float CEnvelopeEditor::ScreenToEnvelopeY(const CUIRect &View, float y) const
1757{
1758 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();
1759}
1760
1761float CEnvelopeEditor::EnvelopeToScreenY(const CUIRect &View, float y) const
1762{
1763 return View.y + View.h - y / Map()->m_EnvelopeEditorState.m_ZoomY.GetValue() * View.h - Map()->m_EnvelopeEditorState.m_Offset.y * View.h;
1764}
1765
1766float CEnvelopeEditor::ScreenToEnvelopeDeltaX(const CUIRect &View, float DeltaX)
1767{
1768 return DeltaX / Graphics()->ScreenWidth() * Ui()->Screen()->w / View.w * Map()->m_EnvelopeEditorState.m_ZoomX.GetValue();
1769}
1770
1771float CEnvelopeEditor::ScreenToEnvelopeDeltaY(const CUIRect &View, float DeltaY)
1772{
1773 return DeltaY / Graphics()->ScreenHeight() * Ui()->Screen()->h / View.h * Map()->m_EnvelopeEditorState.m_ZoomY.GetValue();
1774}
1775
1776void CEnvelopeEditor::RemoveTimeOffsetEnvelope(const std::shared_ptr<CEnvelope> &pEnvelope)
1777{
1778 CFixedTime TimeOffset = pEnvelope->m_vPoints[0].m_Time;
1779 for(auto &Point : pEnvelope->m_vPoints)
1780 Point.m_Time -= TimeOffset;
1781
1782 Map()->m_EnvelopeEditorState.m_Offset.x += TimeOffset.AsSeconds() / Map()->m_EnvelopeEditorState.m_ZoomX.GetValue();
1783}
1784
1785CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePoint::Render(void *pContext, CUIRect View, bool Active)
1786{
1787 CPopupEnvelopePoint *pPopupEnvelopePointContext = static_cast<CPopupEnvelopePoint *>(pContext);
1788 CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointContext->m_pEnvelopeEditor;
1789 CEditorMap *pMap = pEnvelopeEditor->Map();
1790 CEditor *pEditor = pEnvelopeEditor->Editor();
1791
1792 if(pMap->m_SelectedEnvelope < 0 || pMap->m_SelectedEnvelope >= (int)pMap->m_vpEnvelopes.size())
1793 {
1794 return CUi::POPUP_CLOSE_CURRENT;
1795 }
1796
1797 const float RowHeight = 12.0f;
1798 CUIRect Row, Label, EditBox;
1799
1800 pEditor->m_ActiveEnvelopePreview = CEditor::EEnvelopePreview::SELECTED;
1801
1802 std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes[pMap->m_SelectedEnvelope];
1803
1804 if(pEnvelope->GetChannels() == 4 && !pMap->IsTangentSelected())
1805 {
1806 View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View);
1807 View.HSplitTop(Cut: 4.0f, pTop: nullptr, pBottom: &View);
1808 Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row);
1809 Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox);
1810 pEditor->Ui()->DoLabel(pRect: &Label, pText: "Color:", Size: RowHeight - 2.0f, Align: TEXTALIGN_ML);
1811
1812 const auto SelectedPoint = pMap->m_vSelectedEnvelopePoints.front();
1813 const int SelectedIndex = SelectedPoint.first;
1814 auto *pValues = pEnvelope->m_vPoints[SelectedIndex].m_aValues;
1815 const ColorRGBA Color = pEnvelope->m_vPoints[SelectedIndex].ColorValue();
1816 const auto &&SetColor = [&](ColorRGBA NewColor) {
1817 if(Color == NewColor && pEditor->m_ColorPickerPopupContext.m_State == EEditState::EDITING)
1818 return;
1819
1820 if(pEditor->m_ColorPickerPopupContext.m_State == EEditState::START || pEditor->m_ColorPickerPopupContext.m_State == EEditState::ONE_GO)
1821 {
1822 for(int Channel = 0; Channel < 4; ++Channel)
1823 pPopupEnvelopePointContext->m_aValues[Channel] = pValues[Channel];
1824 }
1825
1826 pEnvelope->m_vPoints[SelectedIndex].SetColorValue(NewColor);
1827
1828 if(pEditor->m_ColorPickerPopupContext.m_State == EEditState::END || pEditor->m_ColorPickerPopupContext.m_State == EEditState::ONE_GO)
1829 {
1830 std::vector<std::shared_ptr<IEditorAction>> vpActions(4);
1831
1832 for(int Channel = 0; Channel < 4; ++Channel)
1833 {
1834 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]));
1835 }
1836
1837 char aDisplay[256];
1838 str_format(buffer: aDisplay, buffer_size: sizeof(aDisplay), format: "Edit color of point %d of envelope %d", SelectedIndex, pMap->m_SelectedEnvelope);
1839 pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args&: pMap, args&: vpActions, args&: aDisplay));
1840 }
1841
1842 pMap->m_UpdateEnvPointInfo = true;
1843 pMap->OnModify();
1844 };
1845 pEditor->DoColorPickerButton(pId: &pPopupEnvelopePointContext->m_ColorPickerButtonId, pRect: &EditBox, Color, SetColor);
1846 }
1847
1848 if(pMap->m_UpdateEnvPointInfo)
1849 {
1850 pMap->m_UpdateEnvPointInfo = false;
1851
1852 const auto &[CurrentTime, CurrentValue] = pMap->SelectedEnvelopeTimeAndValue();
1853
1854 // update displayed text
1855 pPopupEnvelopePointContext->m_ValueInput.SetFloat(fx2f(v: CurrentValue));
1856 pPopupEnvelopePointContext->m_TimeInput.SetFloat(CurrentTime.AsSeconds());
1857
1858 pPopupEnvelopePointContext->m_CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat();
1859 pPopupEnvelopePointContext->m_CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat();
1860 }
1861
1862 View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View);
1863 Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row);
1864 Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox);
1865 pEditor->Ui()->DoLabel(pRect: &Label, pText: "Value:", Size: RowHeight - 2.0f, Align: TEXTALIGN_ML);
1866 pEditor->DoEditBox(pLineInput: &pPopupEnvelopePointContext->m_ValueInput, pRect: &EditBox, FontSize: RowHeight - 2.0f, Corners: IGraphics::CORNER_ALL, pToolTip: "The value of the selected envelope point.");
1867
1868 View.HSplitTop(Cut: 4.0f, pTop: nullptr, pBottom: &View);
1869 View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View);
1870 Row.VSplitLeft(Cut: 60.0f, pLeft: &Label, pRight: &Row);
1871 Row.VSplitLeft(Cut: 10.0f, pLeft: nullptr, pRight: &EditBox);
1872 pEditor->Ui()->DoLabel(pRect: &Label, pText: "Time (in s):", Size: RowHeight - 2.0f, Align: TEXTALIGN_ML);
1873 pEditor->DoEditBox(pLineInput: &pPopupEnvelopePointContext->m_TimeInput, pRect: &EditBox, FontSize: RowHeight - 2.0f, Corners: IGraphics::CORNER_ALL, pToolTip: "The time of the selected envelope point.");
1874
1875 if(pEditor->Input()->KeyIsPressed(Key: KEY_RETURN) || pEditor->Input()->KeyIsPressed(Key: KEY_KP_ENTER))
1876 {
1877 float CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat();
1878 float CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat();
1879 if(!(absolute(a: CurrentTime - pPopupEnvelopePointContext->m_CurrentTime) < 0.0001f && absolute(a: CurrentValue - pPopupEnvelopePointContext->m_CurrentValue) < 0.0001f))
1880 {
1881 const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue();
1882
1883 if(pMap->IsTangentInSelected())
1884 {
1885 auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentInPoint;
1886
1887 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)));
1888 CurrentTime = (pEnvelope->m_vPoints[SelectedIndex].m_Time + pEnvelope->m_vPoints[SelectedIndex].m_Bezier.m_aInTangentDeltaX[SelectedChannel]).AsSeconds();
1889 }
1890 else if(pMap->IsTangentOutSelected())
1891 {
1892 auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentOutPoint;
1893
1894 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)));
1895 CurrentTime = (pEnvelope->m_vPoints[SelectedIndex].m_Time + pEnvelope->m_vPoints[SelectedIndex].m_Bezier.m_aOutTangentDeltaX[SelectedChannel]).AsSeconds();
1896 }
1897 else
1898 {
1899 auto [SelectedIndex, SelectedChannel] = pMap->m_vSelectedEnvelopePoints.front();
1900 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)));
1901
1902 if(SelectedIndex != 0)
1903 {
1904 CurrentTime = pEnvelope->m_vPoints[SelectedIndex].m_Time.AsSeconds();
1905 }
1906 else
1907 {
1908 CurrentTime = 0.0f;
1909 pEnvelope->m_vPoints[SelectedIndex].m_Time = CFixedTime(0);
1910 }
1911 }
1912
1913 pPopupEnvelopePointContext->m_TimeInput.SetFloat(CFixedTime::FromSeconds(Seconds: CurrentTime).AsSeconds());
1914 pPopupEnvelopePointContext->m_ValueInput.SetFloat(fx2f(v: f2fx(v: CurrentValue)));
1915
1916 pPopupEnvelopePointContext->m_CurrentTime = pPopupEnvelopePointContext->m_TimeInput.GetFloat();
1917 pPopupEnvelopePointContext->m_CurrentValue = pPopupEnvelopePointContext->m_ValueInput.GetFloat();
1918 }
1919 }
1920
1921 View.HSplitTop(Cut: 6.0f, pTop: nullptr, pBottom: &View);
1922 View.HSplitTop(Cut: RowHeight, pTop: &Row, pBottom: &View);
1923 const char *pButtonText = pMap->IsTangentSelected() ? "Reset" : "Delete";
1924 const char *pTooltip = pMap->IsTangentSelected() ? "Reset tangent point to default value." : "Delete current envelope point in all channels.";
1925 if(pEditor->DoButton_Editor(pId: &pPopupEnvelopePointContext->m_DeleteButtonId, pText: pButtonText, Checked: 0, pRect: &Row, Flags: BUTTONFLAG_LEFT, pToolTip: pTooltip))
1926 {
1927 if(pMap->IsTangentInSelected())
1928 {
1929 auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentInPoint;
1930 const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue();
1931 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));
1932 }
1933 else if(pMap->IsTangentOutSelected())
1934 {
1935 auto [SelectedIndex, SelectedChannel] = pMap->m_SelectedTangentOutPoint;
1936 const auto &[OldTime, OldValue] = pMap->SelectedEnvelopeTimeAndValue();
1937 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));
1938 }
1939 else
1940 {
1941 auto [SelectedIndex, SelectedChannel] = pMap->m_vSelectedEnvelopePoints.front();
1942 pMap->m_EnvelopeEditorHistory.Execute(pAction: std::make_shared<CEditorActionDeleteEnvelopePoint>(args&: pMap, args&: pMap->m_SelectedEnvelope, args&: SelectedIndex));
1943 }
1944
1945 return CUi::POPUP_CLOSE_CURRENT;
1946 }
1947
1948 return CUi::POPUP_KEEP_OPEN;
1949}
1950
1951CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePointMulti::Render(void *pContext, CUIRect View, bool Active)
1952{
1953 CPopupEnvelopePointMulti *pPopupEnvelopePointMultiContext = static_cast<CPopupEnvelopePointMulti *>(pContext);
1954 CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointMultiContext->m_pEnvelopeEditor;
1955 CEditor *pEditor = pEnvelopeEditor->Editor();
1956
1957 CUIRect ProjectOntoButton;
1958 View.HSplitTop(Cut: 14.0f, pTop: &ProjectOntoButton, pBottom: &View);
1959 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."))
1960 {
1961 pEnvelopeEditor->m_PopupEnvelopePointCurveType.m_pEnvelopeEditor = pEnvelopeEditor;
1962 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);
1963 }
1964
1965 return CUi::POPUP_KEEP_OPEN;
1966}
1967
1968CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopePointCurveType::Render(void *pContext, CUIRect View, bool Active)
1969{
1970 CPopupEnvelopePointCurveType *pPopupEnvelopePointCurveTypeContext = static_cast<CPopupEnvelopePointCurveType *>(pContext);
1971 CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopePointCurveTypeContext->m_pEnvelopeEditor;
1972 CEditorMap *pMap = pEnvelopeEditor->Map();
1973 CEditor *pEditor = pEnvelopeEditor->Editor();
1974
1975 int SelectedCurveType = -1;
1976 for(int Type = 0; Type <= CURVETYPE_SMOOTH; Type++)
1977 {
1978 CUIRect Button;
1979 View.HSplitTop(Cut: 14.0f, pTop: &Button, pBottom: &View);
1980 if(pEditor->DoButton_MenuItem(pId: &pPopupEnvelopePointCurveTypeContext->m_aButtonIds[Type], pText: CURVE_TYPE_NAMES[Type], Checked: 0, pRect: &Button))
1981 {
1982 SelectedCurveType = Type;
1983 }
1984 }
1985 if(SelectedCurveType < 0)
1986 {
1987 return CUi::POPUP_KEEP_OPEN;
1988 }
1989
1990 std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes.at(n: pMap->m_SelectedEnvelope);
1991 std::vector<std::shared_ptr<IEditorAction>> vpActions;
1992 for(int Channel = 0; Channel < pEnvelope->GetChannels(); Channel++)
1993 {
1994 int FirstSelectedIndex = pEnvelope->m_vPoints.size();
1995 int LastSelectedIndex = -1;
1996 for(auto [SelectedIndex, SelectedChannel] : pMap->m_vSelectedEnvelopePoints)
1997 {
1998 if(SelectedChannel == Channel)
1999 {
2000 FirstSelectedIndex = std::min(a: FirstSelectedIndex, b: SelectedIndex);
2001 LastSelectedIndex = std::max(a: LastSelectedIndex, b: SelectedIndex);
2002 }
2003 }
2004
2005 if(FirstSelectedIndex < (int)pEnvelope->m_vPoints.size() && LastSelectedIndex >= 0 && FirstSelectedIndex != LastSelectedIndex)
2006 {
2007 CEnvPoint FirstPoint = pEnvelope->m_vPoints[FirstSelectedIndex];
2008 CEnvPoint LastPoint = pEnvelope->m_vPoints[LastSelectedIndex];
2009
2010 CEnvelope HelperEnvelope(1);
2011 HelperEnvelope.AddPoint(Time: FirstPoint.m_Time, aValues: {FirstPoint.m_aValues[Channel], 0, 0, 0});
2012 HelperEnvelope.AddPoint(Time: LastPoint.m_Time, aValues: {LastPoint.m_aValues[Channel], 0, 0, 0});
2013 HelperEnvelope.m_vPoints[0].m_Curvetype = SelectedCurveType;
2014
2015 for(auto [SelectedIndex, SelectedChannel] : pMap->m_vSelectedEnvelopePoints)
2016 {
2017 if(SelectedChannel == Channel &&
2018 SelectedIndex != FirstSelectedIndex &&
2019 SelectedIndex != LastSelectedIndex)
2020 {
2021 CEnvPoint &CurrentPoint = pEnvelope->m_vPoints[SelectedIndex];
2022 ColorRGBA Channels = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
2023 HelperEnvelope.Eval(Time: CurrentPoint.m_Time.AsSeconds(), Result&: Channels, Channels: 1);
2024 int PrevValue = CurrentPoint.m_aValues[Channel];
2025 CurrentPoint.m_aValues[Channel] = f2fx(v: Channels.r);
2026 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]));
2027 }
2028 }
2029 }
2030 }
2031
2032 if(!vpActions.empty())
2033 {
2034 pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionBulk>(args&: pMap, args&: vpActions, args: "Project points"));
2035 }
2036
2037 pMap->OnModify();
2038 return CUi::POPUP_CLOSE_CURRENT;
2039}
2040
2041CUi::EPopupMenuFunctionResult CEnvelopeEditor::CPopupEnvelopeCurveType::Render(void *pContext, CUIRect View, bool Active)
2042{
2043 CPopupEnvelopeCurveType *pPopupEnvelopeCurveTypeContext = static_cast<CPopupEnvelopeCurveType *>(pContext);
2044 CEnvelopeEditor *pEnvelopeEditor = pPopupEnvelopeCurveTypeContext->m_pEnvelopeEditor;
2045 CEditorMap *pMap = pEnvelopeEditor->Map();
2046 CEditor *pEditor = pEnvelopeEditor->Editor();
2047
2048 if(pMap->m_SelectedEnvelope < 0 || pMap->m_SelectedEnvelope >= (int)pMap->m_vpEnvelopes.size())
2049 {
2050 return CUi::POPUP_CLOSE_CURRENT;
2051 }
2052 std::shared_ptr<CEnvelope> pEnvelope = pMap->m_vpEnvelopes[pMap->m_SelectedEnvelope];
2053
2054 if(pPopupEnvelopeCurveTypeContext->m_SelectedPoint < 0 || pPopupEnvelopeCurveTypeContext->m_SelectedPoint >= (int)pEnvelope->m_vPoints.size())
2055 {
2056 return CUi::POPUP_CLOSE_CURRENT;
2057 }
2058 CEnvPoint_runtime &SelectedPoint = pEnvelope->m_vPoints[pPopupEnvelopeCurveTypeContext->m_SelectedPoint];
2059
2060 for(int Type = 0; Type < NUM_CURVETYPES; Type++)
2061 {
2062 CUIRect Button;
2063 View.HSplitTop(Cut: 14.0f, pTop: &Button, pBottom: &View);
2064
2065 if(pEditor->DoButton_MenuItem(pId: &pPopupEnvelopeCurveTypeContext->m_aButtonIds[Type], pText: CURVE_TYPE_NAMES[Type], Checked: Type == SelectedPoint.m_Curvetype, pRect: &Button))
2066 {
2067 const int PrevCurve = SelectedPoint.m_Curvetype;
2068 if(PrevCurve != Type)
2069 {
2070 SelectedPoint.m_Curvetype = Type;
2071 pMap->m_EnvelopeEditorHistory.RecordAction(pAction: std::make_shared<CEditorActionEnvelopeEditPoint>(
2072 args&: pMap, args&: pMap->m_SelectedEnvelope, args&: pPopupEnvelopeCurveTypeContext->m_SelectedPoint, args: 0,
2073 args: CEditorActionEnvelopeEditPoint::EEditType::CURVE_TYPE, args: PrevCurve, args&: SelectedPoint.m_Curvetype));
2074 pMap->OnModify();
2075 return CUi::POPUP_CLOSE_CURRENT;
2076 }
2077 }
2078 }
2079
2080 return CUi::POPUP_KEEP_OPEN;
2081}
2082