| 1 | import term.ui as tui |
| 2 | |
| 3 | const colors = [ |
| 4 | tui.Color{33, 150, 243}, |
| 5 | tui.Color{0, 150, 136}, |
| 6 | tui.Color{205, 220, 57}, |
| 7 | tui.Color{255, 152, 0}, |
| 8 | tui.Color{244, 67, 54}, |
| 9 | tui.Color{156, 39, 176}, |
| 10 | ] |
| 11 | |
| 12 | struct Point { |
| 13 | x int |
| 14 | y int |
| 15 | } |
| 16 | |
| 17 | struct App { |
| 18 | mut: |
| 19 | tui &tui.Context = unsafe { nil } |
| 20 | points []Point |
| 21 | color tui.Color = colors[0] |
| 22 | color_idx int |
| 23 | cut_rate f64 = 5 |
| 24 | } |
| 25 | |
| 26 | fn frame(mut app App) { |
| 27 | app.tui.clear() |
| 28 | |
| 29 | if app.points.len > 0 { |
| 30 | app.tui.set_bg_color(app.color) |
| 31 | mut last := app.points[0] |
| 32 | for point in app.points { |
| 33 | // if the cursor moves quickly enough, different events are not |
| 34 | // necessarily touching, so we need to draw a line between them |
| 35 | app.tui.draw_line(last.x, last.y, point.x, point.y) |
| 36 | last = point |
| 37 | } |
| 38 | app.tui.reset() |
| 39 | |
| 40 | l := int(app.points.len / app.cut_rate) + 1 |
| 41 | app.points = app.points[l..].clone() |
| 42 | } |
| 43 | |
| 44 | ww := app.tui.window_width |
| 45 | |
| 46 | app.tui.bold() |
| 47 | app.tui.draw_text(ww / 6, 2, 'V term.input: cursor chaser demo') |
| 48 | app.tui.draw_text((ww - ww / 6) - 14, 2, 'cut rate: ${(100 / app.cut_rate):3.0f}%') |
| 49 | app.tui.horizontal_separator(3) |
| 50 | app.tui.reset() |
| 51 | app.tui.flush() |
| 52 | } |
| 53 | |
| 54 | fn event(e &tui.Event, mut app App) { |
| 55 | match e.typ { |
| 56 | .key_down { |
| 57 | match e.code { |
| 58 | .escape { |
| 59 | exit(0) |
| 60 | } |
| 61 | .space, .enter { |
| 62 | app.color_idx++ |
| 63 | if app.color_idx == colors.len { |
| 64 | app.color_idx = 0 |
| 65 | } |
| 66 | app.color = colors[app.color_idx] |
| 67 | } |
| 68 | else {} |
| 69 | } |
| 70 | } |
| 71 | .mouse_move, .mouse_drag, .mouse_down { |
| 72 | app.points << Point{e.x, e.y} |
| 73 | } |
| 74 | .mouse_scroll { |
| 75 | d := if e.direction == .up { 0.1 } else { -0.1 } |
| 76 | app.cut_rate += d |
| 77 | if app.cut_rate < 1 { |
| 78 | app.cut_rate = 1 |
| 79 | } |
| 80 | } |
| 81 | else {} |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | type EventFn = fn (&tui.Event, voidptr) |
| 86 | |
| 87 | type FrameFn = fn (voidptr) |
| 88 | |
| 89 | fn main() { |
| 90 | mut app := &App{} |
| 91 | app.tui = tui.init( |
| 92 | user_data: app |
| 93 | frame_fn: FrameFn(frame) |
| 94 | event_fn: EventFn(event) |
| 95 | hide_cursor: true |
| 96 | ) |
| 97 | app.tui.run()! |
| 98 | } |
| 99 | |