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