| 1 | module main |
| 2 | |
| 3 | import gg |
| 4 | import math.easing |
| 5 | |
| 6 | const all = { |
| 7 | 'in_functions': { |
| 8 | 'linear': voidptr(easing.linear) |
| 9 | 'in_sine': easing.in_sine |
| 10 | 'in_quad': easing.in_quad |
| 11 | 'in_cubic': easing.in_cubic |
| 12 | 'in_quart': easing.in_quart |
| 13 | 'in_quint': easing.in_quint |
| 14 | 'in_expo': easing.in_expo |
| 15 | 'in_circ': easing.in_circ |
| 16 | 'in_back': easing.in_back |
| 17 | 'in_elastic': easing.in_elastic |
| 18 | 'in_bounce': easing.in_bounce |
| 19 | } |
| 20 | 'out_functions': { |
| 21 | 'linear': voidptr(easing.linear) |
| 22 | 'out_sine': easing.out_sine |
| 23 | 'out_quad': easing.out_quad |
| 24 | 'out_cubic': easing.out_cubic |
| 25 | 'out_quart': easing.out_quart |
| 26 | 'out_quint': easing.out_quint |
| 27 | 'out_expo': easing.out_expo |
| 28 | 'out_circ': easing.out_circ |
| 29 | 'out_back': easing.out_back |
| 30 | 'out_elastic': easing.out_elastic |
| 31 | 'out_bounce': easing.out_bounce |
| 32 | } |
| 33 | 'in_out_functions': { |
| 34 | 'linear': voidptr(easing.linear) |
| 35 | 'in_out_sine': easing.in_out_sine |
| 36 | 'in_out_quad': easing.in_out_quad |
| 37 | 'in_out_cubic': easing.in_out_cubic |
| 38 | 'in_out_quart': easing.in_out_quart |
| 39 | 'in_out_quint': easing.in_out_quint |
| 40 | 'in_out_expo': easing.in_out_expo |
| 41 | 'in_out_circ': easing.in_out_circ |
| 42 | 'in_out_back': easing.in_out_back |
| 43 | 'in_out_elastic': easing.in_out_elastic |
| 44 | 'in_out_bounce': easing.in_out_bounce |
| 45 | } |
| 46 | } |
| 47 | const all_keys = all.keys() |
| 48 | |
| 49 | @[heap] |
| 50 | struct App { |
| 51 | mut: |
| 52 | gg &gg.Context = unsafe { nil } |
| 53 | x f64 |
| 54 | t f64 |
| 55 | h f64 |
| 56 | kind string = all_keys.first() |
| 57 | } |
| 58 | |
| 59 | fn (mut app App) draw_circle(label string, f easing.EasingFN) { |
| 60 | offset := 30 |
| 61 | app.gg.draw_text_def(int(app.x) - 30, 5, label) |
| 62 | app.gg.draw_line(f32(app.x), offset, f32(app.x), f32(app.h + offset), gg.gray) |
| 63 | app.gg.draw_circle_filled(f32(app.x), f32(offset + f(app.t) * app.h), 10, gg.rgb(0, 0, 255)) |
| 64 | app.x += 120 |
| 65 | } |
| 66 | |
| 67 | fn (mut app App) frame() { |
| 68 | size := gg.window_size() |
| 69 | period := u64(240) |
| 70 | app.t = f32_min(1.0, f32(app.gg.frame % period) / f32(period - 30)) |
| 71 | app.x = 80 |
| 72 | app.h = size.height - 100 |
| 73 | app.gg.begin() |
| 74 | app.gg.draw_line(0, f32(app.h + 30), size.width, f32(app.h + 30), gg.gray) |
| 75 | current_map := unsafe { all[app.kind].clone() } |
| 76 | for k, e in current_map { |
| 77 | app.draw_circle(k, e) |
| 78 | } |
| 79 | app.gg.draw_text_def(50, int(app.h + 50), |
| 80 | 'Note: use left and right arrows to change functions. Frame: ${app.gg.frame:010} | t: ${app.t:6.3f} | kind: ${app.kind}.') |
| 81 | app.gg.end() |
| 82 | } |
| 83 | |
| 84 | fn (mut app App) change_functions(direction int) { |
| 85 | idx := (all_keys.len + all_keys.index(app.kind) + direction) % all_keys.len |
| 86 | app.kind = all_keys[idx] |
| 87 | } |
| 88 | |
| 89 | fn (mut app App) on_event(ev &gg.Event, _x voidptr) { |
| 90 | if ev.typ != .key_down { |
| 91 | return |
| 92 | } |
| 93 | if ev.key_code == .left { |
| 94 | app.change_functions(-1) |
| 95 | } else if ev.key_code == .right { |
| 96 | app.change_functions(1) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | mut app := &App{} |
| 101 | app.gg = gg.new_context( |
| 102 | bg_color: gg.rgb(174, 198, 255) |
| 103 | width: 1350 |
| 104 | height: 800 |
| 105 | window_title: 'Easing functions' |
| 106 | frame_fn: app.frame |
| 107 | event_fn: app.on_event |
| 108 | user_data: app |
| 109 | ) |
| 110 | app.gg.run() |
| 111 | |