Skip to content

Commit 126f5c2

Browse files
committed
examples: add more control keys to tunnel.v
1 parent fa4e61c commit 126f5c2

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

examples/hot_reload/tunnel.v

+46-10
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ fn main() {
1111
width: 1024
1212
height: 768
1313
frame_fn: unsafe { state.draw }
14-
event_fn: fn (event &gg.Event, ctx voidptr) {
15-
if event.typ == .char && event.char_code == `f` {
16-
gg.toggle_fullscreen()
17-
return
18-
}
19-
}
14+
event_fn: unsafe { state.on_event }
2015
)
2116
}
2217

@@ -35,6 +30,7 @@ struct State {
3530
mut:
3631
t f32
3732
real_t f32
33+
drt f32 = 0.1
3834
tparams gg.DrawTextParams
3935
rects []BigRect
4036
}
@@ -45,7 +41,9 @@ const colors = []gg.Color{len: 1024, init: gg.Color{u8(100 +
4541

4642
fn init_state() State {
4743
mut state := State{
48-
real_t: 1300
44+
t: 0
45+
real_t: 120
46+
drt: 0.1
4947
tparams: gg.DrawTextParams{
5048
x: 40
5149
size: 20
@@ -69,6 +67,41 @@ fn init_state() State {
6967
return state
7068
}
7169

70+
fn (mut state State) on_event(e &gg.Event, ctx voidptr) {
71+
if e.typ == .char {
72+
if e.char_code == `f` {
73+
gg.toggle_fullscreen()
74+
return
75+
}
76+
if e.char_code == `1` {
77+
state.real_t -= 100
78+
}
79+
if e.char_code == `2` {
80+
state.real_t += 100
81+
}
82+
if e.char_code == `3` {
83+
state.t -= 100
84+
}
85+
if e.char_code == `4` {
86+
state.t += 100
87+
}
88+
if e.char_code == `5` {
89+
state.rects[0].w += 10
90+
}
91+
if e.char_code == `6` {
92+
state.rects[0].w -= 10
93+
}
94+
if e.char_code == `r` {
95+
state.real_t = 120
96+
state.t = 0
97+
state.rects[0].w = 30
98+
}
99+
if e.char_code == ` ` {
100+
state.drt = if state.drt > 0.0001 { f32(0.0) } else { 0.1 }
101+
}
102+
}
103+
}
104+
72105
fn (mut state State) draw(ctx &gg.Context) {
73106
update(mut state)
74107
ctx.begin()
@@ -80,7 +113,7 @@ fn (mut state State) draw(ctx &gg.Context) {
80113
ctx.draw_text2(gg.DrawTextParams{
81114
...state.tparams
82115
y: 0
83-
text: 'real_t: ${state.real_t:7.3f}, r: ${state.t:7.3f}, r0.x: ${r0.x:7.3f}, r0.y: ${r0.y:7.3f}'
116+
text: 'rt[1,2]: ${state.real_t:08.3f}, t[3,4]: ${state.t:08.3f}, r0.x: ${r0.x:07.3f}, r0.y: ${r0.y:07.3f}, r0.w[5,6]: ${r0.w:4.1f}, [f]-toggle fs, [r]-reset, [ ]-pause'
84117
})
85118
draw_center_point(ctx, r0, state.t)
86119
ctx.show_fps()
@@ -89,8 +122,11 @@ fn (mut state State) draw(ctx &gg.Context) {
89122

90123
@[live]
91124
fn update(mut state State) {
92-
state.real_t += 0.1
93-
state.t += 0.5 * math.sinf(state.real_t / 7000)
125+
if state.drt <= 0.0001 {
126+
return
127+
}
128+
state.real_t += state.drt
129+
state.t += 0.5 * math.sinf(state.real_t / 1000)
94130
mut r0 := &state.rects[0]
95131
r0.t = state.t
96132
r0.x = 550 + 450 * math.sinf(state.t / 10) * math.sinf(state.t / 50)

0 commit comments

Comments
 (0)