-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.h
355 lines (304 loc) · 12 KB
/
timeline.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#ifndef RAYGUI_IMPLEMENTATION
#pragma message("DEPENDS ON RAYGUI_IMPLEMENTATION in same .C file")
#else
typedef struct
{
const char *name;
int nKeys;
float *keys;
} track_t;
typedef struct
{
float frame;
float minValue;
float maxValue;
Rectangle bounds;
int nTracks;
track_t *tracks;
bool isPlaying;
bool isEditingFrameSlider;
bool isDropdownActive;
int selected;
int cellHeight;
Rectangle dropDownRect;
} timeline_t;
#ifdef _TIMELINE_IMPL_
static float sliderPos;
static float GuiSliderProOwning(Rectangle bounds, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue, int sliderWidth, bool editMode)
{
GuiState state = (GuiState)GuiGetState();
float tempValue = *value;
bool pressed = false;
int sliderValue = (int)(((tempValue - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH)));
Rectangle slider = {
bounds.x,
bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, SLIDER_PADDING),
0,
bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, SLIDER_PADDING)
};
if (sliderWidth > 0) // Slider
{
slider.x += (sliderValue - sliderWidth/2);
slider.width = (float)sliderWidth;
}
else if (sliderWidth == 0) // SliderBar
{
slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH);
slider.width = (float)sliderValue;
}
// Update control
//--------------------------------------------------------------------
if ((state != STATE_DISABLED) && (editMode || !guiLocked))
{
Vector2 mousePoint = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
if (CheckCollisionPointRec(mousePoint, bounds))
{
pressed = true;
}
}
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && editMode)
{
pressed = true;
}
if (editMode)
{
state = STATE_PRESSED;
tempValue = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + sliderWidth/2)))/(float)(bounds.width - sliderWidth) + minValue;
if (sliderWidth > 0) slider.x = mousePoint.x - slider.width/2; // Slider
else if (sliderWidth == 0) slider.width = (float)sliderValue; // SliderBar
}
else if (CheckCollisionPointRec(mousePoint, bounds))
{
state = STATE_FOCUSED;
}
if (tempValue > maxValue) tempValue = maxValue;
else if (tempValue < minValue) tempValue = minValue;
}
// Bar limits check
if (sliderWidth > 0) // Slider
{
if (slider.x <= (bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH))) slider.x = bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH);
else if ((slider.x + slider.width) >= (bounds.x + bounds.width)) slider.x = bounds.x + bounds.width - slider.width - GuiGetStyle(SLIDER, BORDER_WIDTH);
}
else if (sliderWidth == 0) // SliderBar
{
if (slider.width > bounds.width) slider.width = bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH);
}
//--------------------------------------------------------------------
// Draw control
//--------------------------------------------------------------------
GuiDrawRectangle(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(SLIDER, (state != STATE_DISABLED)? BASE_COLOR_NORMAL : BASE_COLOR_DISABLED)), guiAlpha));
// Draw slider internal bar (depends on state)
if ((state == STATE_NORMAL) || (state == STATE_PRESSED))
GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_PRESSED)), guiAlpha));
else if (state == STATE_FOCUSED)
GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, TEXT_COLOR_FOCUSED)), guiAlpha));
sliderPos = slider.x;
// Draw left/right text if provided
if (textLeft != NULL)
{
Rectangle textBounds = { 0 };
textBounds.width = (float)GetTextWidth(textLeft);
textBounds.height = (float)GuiGetStyle(DEFAULT, TEXT_SIZE);
textBounds.x = bounds.x - textBounds.width - GuiGetStyle(SLIDER, TEXT_PADDING);
textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
GuiDrawText(textLeft, textBounds, TEXT_ALIGN_RIGHT, Fade(GetColor(GuiGetStyle(SLIDER, TEXT + (state*3))), guiAlpha));
}
if (textRight != NULL)
{
Rectangle textBounds = { 0 };
textBounds.width = (float)GetTextWidth(textRight);
textBounds.height = (float)GuiGetStyle(DEFAULT, TEXT_SIZE);
textBounds.x = bounds.x + bounds.width + GuiGetStyle(SLIDER, TEXT_PADDING);
textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
GuiDrawText(textRight, textBounds, TEXT_ALIGN_LEFT, Fade(GetColor(GuiGetStyle(SLIDER, TEXT + (state*3))), guiAlpha));
}
//--------------------------------------------------------------------
*value = tempValue;
return pressed;
}
static bool GuiSliderOwning(Rectangle bounds, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue, bool editMode)
{
return GuiSliderProOwning(bounds, textLeft, textRight, value, minValue, maxValue, GuiGetStyle(SLIDER, SLIDER_WIDTH), editMode);
}
#define TEXTBLOCK 96
void TimelineTrack(timeline_t *timeline,const char *label,Rectangle bounds,float *keys,int kcount)
{
//
int sliderWidth = GuiGetStyle(SLIDER, SLIDER_WIDTH);
Vector2 mousePoint = GetMousePosition();
// draw label
GuiDisableTooltip();
if (GuiButton((Rectangle){ bounds.x, bounds.y, timeline->cellHeight, timeline->cellHeight }, GuiIconText(ICON_LOCK_OPEN, "")))
{
}
if (GuiButton((Rectangle){ bounds.x+timeline->cellHeight, bounds.y, timeline->cellHeight, timeline->cellHeight }, GuiIconText(ICON_PLAYER_PLAY, "")))
{
}
GuiEnableTooltip();
GuiLabel((Rectangle){bounds.x+(timeline->cellHeight*2),bounds.y,GetTextWidth(label),timeline->cellHeight},label);
// if we're not pressing buttons then selected is reset
if ((!IsMouseButtonDown(MOUSE_LEFT_BUTTON)) && (!IsMouseButtonDown(MOUSE_BUTTON_RIGHT)))
timeline->selected = -1;
// if mouse over
if (CheckCollisionPointRec(mousePoint, bounds))
{
// GuiDrawRectangle(bounds, 0, BLANK, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_FOCUSED)), guiAlpha));
}
// draw the keys
bounds.x+=TEXTBLOCK;
bounds.width-=TEXTBLOCK;
int over = -1;
for (int q=0;q<kcount;q++)
{
float value;
GuiState state = GuiGetState();
;
value = keys[q];
// slidervalue for this keyframe
float sliderValue = (float)(((value - timeline->minValue)/(timeline->maxValue - timeline->minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH)));
// get rect
Rectangle slider = { bounds.x, bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, SLIDER_PADDING),
0, bounds.height - 2.0f*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2.0f*GuiGetStyle(SLIDER, SLIDER_PADDING) };
// set normal color
Color circle_color = GetColor(GuiGetStyle(SLIDER, TEXT_COLOR_NORMAL));
if (timeline->selected==(int)keys+q)
{
// move value based on movement
value = ((timeline->maxValue - timeline->minValue)*(mousePoint.x - (float)(bounds.x + (float)sliderWidth/2.0f)))/(float)(bounds.width - sliderWidth) + timeline->minValue;
if (value > timeline->maxValue) value = timeline->maxValue;
else if (value < timeline->minValue) value = timeline->minValue;
// change to red
circle_color=RED;
// set the key to new value
keys[q]=value;
}
sliderValue = (float)(((value - timeline->minValue)/(timeline->maxValue - timeline->minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH)));
if (sliderWidth > 0) // Slider
{
slider.x += (sliderValue - sliderWidth/2);
slider.width = (float)sliderWidth;
}
else if (sliderWidth == 0) // SliderBar
{
slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH);
slider.width = (float)sliderValue;
}
float offsetx = slider.width/2.0f;
float offsety = slider.height/2.0f;
// if we can edit
if ((state != STATE_DISABLED) && !guiLocked && timeline->isEditingFrameSlider==false)
{
if (CheckCollisionPointRec(mousePoint, slider))
{
state=STATE_FOCUSED;
over = (int)keys+q;
// if nothing selected then select this one if mouse pressed
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
if (timeline->selected==-1)
timeline->selected = (int)keys+q;
}
// drop down if wanted
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
{
timeline->dropDownRect=(Rectangle){ mousePoint.x, mousePoint.y, 125, 32 };
timeline->isDropdownActive=true;
}
}
}
// if we're over or have a selection for this key show the tooltip
if ((over==(int)keys+q) || (timeline->selected==(int)keys+q))
{
GuiSetTooltip(TextFormat("%d",(int)value));
GuiTooltip(slider);
}
// draw the key
DrawCircle(slider.x+offsetx,slider.y+offsety,slider.height/2,Fade(circle_color, guiAlpha));
}
}
void Timeline(timeline_t *timeline)
{
float sy = 0;
static int scroll = 0;
float step = 0;
int y = timeline->bounds.y;
Rectangle *block =&timeline->bounds;
GuiDisableTooltip();
timeline->bounds.height=((5+timeline->nTracks)*timeline->cellHeight);
GuiDrawRectangle(timeline->bounds,0,BLACK,(Color){0,0,0,230});
int bs = timeline->cellHeight*2;
// draw control buttons
if (GuiButton((Rectangle){ block->x+(0*bs), block->y, bs, bs }, GuiIconText(ICON_PLAYER_PREVIOUS, "")))
{
}
if (GuiButton((Rectangle){ block->x+(1*bs), block->y, bs, bs }, GuiIconText(ICON_PLAYER_PLAY_BACK, "")))
{
}
if (GuiButton((Rectangle){ block->x+(2*bs), block->y, bs, bs }, GuiIconText(timeline->isPlaying ? ICON_PLAYER_PAUSE:ICON_PLAYER_PLAY , "")))
{
timeline->isPlaying=!timeline->isPlaying;
}
if (GuiButton((Rectangle){ block->x+(3*bs), block->y, bs, bs }, GuiIconText(ICON_PLAYER_NEXT, "")))
{
}
if (GuiButton((Rectangle){ block->x+(4*bs), block->y, bs, bs }, GuiIconText(ICON_HELP, "")))
{
}
GuiEnableTooltip();
float scale = timeline->maxValue/((block->width-TEXTBLOCK)/32.0f);
step = (float)scroll;
if (timeline->isPlaying==true)
{
timeline->frame+=1;
}
y+=bs;
timeline->frame = fmod(timeline->frame,timeline->maxValue+1.0f);
if (GuiSliderOwning((Rectangle){ block->x+TEXTBLOCK, y, block->width-TEXTBLOCK, timeline->cellHeight }, NULL, NULL, &timeline->frame, timeline->minValue, timeline->maxValue, timeline->isEditingFrameSlider))
{
timeline->isEditingFrameSlider = !timeline->isEditingFrameSlider;
}
for (int x=TEXTBLOCK;x<block->width;x+=32)
{
DrawLine((int)block->x+x,y,(int)block->x+x,y+bs,WHITE);
GuiDrawText(TextFormat("%03d",(int)step), (Rectangle){block->x+x,y+timeline->cellHeight,32,timeline->cellHeight},
TEXT_ALIGN_LEFT, WHITE);
step+=scale;
}
float rpos;
rpos = sliderPos;
y+=timeline->cellHeight*2;
int slidery=y+timeline->cellHeight;
GuiLabel((Rectangle){block->x,y,block->width,timeline->cellHeight},TextFormat("%d",(int)timeline->frame));
y+=timeline->cellHeight;
for (int q=0;q<timeline->nTracks;q++)
{
if (((int)q)&1)
DrawRectangle(block->x+TEXTBLOCK,y,block->width-TEXTBLOCK,timeline->cellHeight,(Color){96,96,96,128});
else
DrawRectangle(block->x+TEXTBLOCK,y,block->width-TEXTBLOCK,timeline->cellHeight,(Color){64,64,64,128});
TimelineTrack(timeline,timeline->tracks[q].name,(Rectangle){block->x,y,block->width,timeline->cellHeight},&timeline->tracks[q].keys[0],timeline->tracks[q].nKeys);
y+=timeline->cellHeight;
}
DrawLine(rpos+4,slidery,rpos+4,y,RED);
// scroll = GuiSlider((Rectangle){block->x,y+12,block->width,12},scroll,0,255,GuiGetStyle(SLIDER, SLIDER_WIDTH),1.0f);
if (timeline->dropDownRect.x!=-1)
{
GuiDisableTooltip();
if (GuiButton(timeline->dropDownRect,"Delete"))
{
// }
// if (GuiDropdownBox(timeline->dropDownRect, "Step;Lerp;Delete", &dropdownBox000Active, timeline->isDropdownActive))
// {
timeline->dropDownRect.x=-1;
timeline->isDropdownActive=false;
}
GuiEnableTooltip();
}
}
#else
void Timeline(timeline_t *timeline);
#endif
#endif