forked from ceu-lang/ceu-sdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui-timemachine.ceu
192 lines (180 loc) · 6.38 KB
/
ui-timemachine.ceu
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
#include "sdl.ceu"
#include "sdl-gfx.ceu"
input _SDL_MouseButtonEvent* SDL_MOUSEBUTTONDOWN_;
input _SDL_MouseButtonEvent* SDL_MOUSEBUTTONUP_;
input _SDL_MouseMotionEvent* SDL_MOUSEMOTION_;
native do
/* must be assigned externally */
#define UITM_TEX_N 6
SDL_Texture* UITM_TEX_FIRST = NULL;
SDL_Texture* UITM_TEX_REWIND = NULL;
SDL_Texture* UITM_TEX_PAUSE = NULL;
SDL_Texture* UITM_TEX_PLAY = NULL;
SDL_Texture* UITM_TEX_FORWARD = NULL;
SDL_Texture* UITM_TEX_LAST = NULL;
SDL_Texture* UITM_TEXS[UITM_TEX_N];
end
#ifndef _UITM_REN
#error "_UITM_REN is not defined!"
#endif
finalize
_UITM_TEX_FIRST = _IMG_LoadTexture(_UITM_REN, "tm/first.png");
with
_SDL_DestroyTexture(_UITM_TEX_FIRST);
end
finalize
_UITM_TEX_REWIND = _IMG_LoadTexture(_UITM_REN, "tm/rewind.png");
with
_SDL_DestroyTexture(_UITM_TEX_REWIND);
end
finalize
_UITM_TEX_PAUSE = _IMG_LoadTexture(_UITM_REN, "tm/pause.png");
with
_SDL_DestroyTexture(_UITM_TEX_PAUSE);
end
finalize
_UITM_TEX_PLAY = _IMG_LoadTexture(_UITM_REN, "tm/play.png");
with
_SDL_DestroyTexture(_UITM_TEX_PLAY);
end
finalize
_UITM_TEX_FORWARD = _IMG_LoadTexture(_UITM_REN, "tm/forward.png");
with
_SDL_DestroyTexture(_UITM_TEX_FORWARD);
end
finalize
_UITM_TEX_LAST = _IMG_LoadTexture(_UITM_REN, "tm/last.png");
with
_SDL_DestroyTexture(_UITM_TEX_LAST);
end
_assert(_UITM_TEX_FIRST != null);
_assert(_UITM_TEX_REWIND != null);
_assert(_UITM_TEX_PAUSE != null);
_assert(_UITM_TEX_PLAY != null);
_assert(_UITM_TEX_FORWARD != null);
_assert(_UITM_TEX_LAST != null);
_UITM_TEXS[0] = _UITM_TEX_FIRST;
_UITM_TEXS[1] = _UITM_TEX_REWIND;
_UITM_TEXS[2] = _UITM_TEX_PAUSE;
_UITM_TEXS[3] = _UITM_TEX_PLAY;
_UITM_TEXS[4] = _UITM_TEX_FORWARD;
_UITM_TEXS[5] = _UITM_TEX_LAST;
class UITimeMachine with
var TimeMachine& tm;
event void go_on, go_off;
event void ok_restart;
event void go_redraw;
do
var int ren_w, ren_h;
_SDL_GetRendererOutputSize(_UITM_REN, &ren_w, &ren_h);
var int x = ren_w/6;
var int w = ren_w - x*2;
var int h = ren_h/80;
var int y = ren_h - h*4;
var SDL_Rect r = SDL_Rect(x,y, w,h);
var int cw = w*2 / 3;
var SDL_Rect rc = SDL_Rect(ren_w/2 - cw/2,
y - ren_h/10 - ren_h/80,
cw,
ren_h/10);
var SDL_Rect[_UITM_TEX_N] rcs;
do
var int dx = rc.w / (_UITM_TEX_N+1);
loop i in _UITM_TEX_N do
var SDL_Rect ri;
_SDL_QueryTexture(_UITM_TEXS[i], null, null, &ri.w, &ri.h);
ri.x = rc.x + (i+1)*dx - ri.w/2;
ri.y = rc.y + (rc.h-ri.h)/2;
rcs[i] = ri;
end
end
loop do
await this.go_on;
var int bk_speed = -4;
var int fw_speed = -4;
par/or do
await this.go_off;
with
loop do
var _SDL_MouseButtonEvent* e = await SDL_MOUSEBUTTONDOWN_
until _SDL_Rect_vs_Mouse((_SDL_Rect*)&r,e);
emit tm.go_seek => (tm.time_total * (e:x-x) / (float)w);
bk_speed = -4;
fw_speed = -4;
watching SDL_MOUSEBUTTONUP_ do
every SDL_MOUSEMOTION_ do
var int x_,y_;
_SDL_GetMouseState(&x_, &y_);
do
var int to = (tm.time_total * (x_-x) / (float)w);
if to < 0 then
to = 0;
else/if to > tm.time_total then
to = tm.time_total;
end
emit tm.go_seek => to;
end
end
end
end
with
loop do
var _SDL_MouseButtonEvent* e = await SDL_MOUSEBUTTONDOWN_;
var int i = -1;
loop ii in _UITM_TEX_N do
if _SDL_Rect_vs_Mouse((_SDL_Rect*)&rcs[ii], e) then
i = ii;
break;
end
end
e = await SDL_MOUSEBUTTONUP_;
if _SDL_Rect_vs_Mouse((_SDL_Rect*)&rcs[i], e) then
if i == 0 then // FIRST
emit tm.go_seek => 0;
else/if i == 1 then // REWIND
emit tm.go_backward => bk_speed;
bk_speed = bk_speed + 1;
if bk_speed == 0 then
bk_speed = 1; // skip seek speed 0
end
else/if i == 2 then // PAUSE
emit tm.go_pause;
else/if i == 3 then // PLAY
emit tm.go_forward => 1;
else/if i == 4 then // FORWARD
emit tm.go_forward => fw_speed;
fw_speed = fw_speed + 1;
if fw_speed == 0 then
fw_speed = 1; // skip seek speed 0
end
else/if i == 5 then // LAST
emit tm.go_forward => 0;
end
if i != 1 then
bk_speed = -4; // reset fw_speed for other clicks
end
if i != 4 then
fw_speed = -4; // reset fw_speed for other clicks
end
end
end
with
every this.go_redraw do
do
var int dx = tm.time_now/(float)tm.time_total * w;
_boxRGBA(_UITM_REN, x, y, x+w, y+h,
0x88,0x88,0x88,0x88);
_filledCircleRGBA(_UITM_REN, x+dx-h/2, y+h/2, h,
0xFF,0xFF,0xFF,0x88);
end
do
_boxRGBA(_UITM_REN, rc.x, rc.y, rc.x+rc.w, rc.y+rc.h,
0xAA,0xAA,0xAA,0xAA);
loop i in _UITM_TEX_N do
_SDL_RenderCopy(_UITM_REN, _UITM_TEXS[i], null, (_SDL_Rect*)&rcs[i]);
end
end
end
end
end
end