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