diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-10-05 18:56:44 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-10-05 18:56:44 +0200 |
| commit | 5380cea34fb544de9374efafd846be7a930a0b88 (patch) | |
| tree | abf379c58a43c6d5174e7b7534443350650a6ce2 /demo | |
| parent | 2e31cbeb70d93bb0f5512064f181990db189d1f6 (diff) | |
| download | hare-termbox2-5380cea34fb544de9374efafd846be7a930a0b88.tar.gz hare-termbox2-5380cea34fb544de9374efafd846be7a930a0b88.zip | |
Added mouse demo
Diffstat (limited to 'demo')
| -rw-r--r-- | demo/mouse/mouse.ha | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/demo/mouse/mouse.ha b/demo/mouse/mouse.ha new file mode 100644 index 0000000..13026c6 --- /dev/null +++ b/demo/mouse/mouse.ha | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | use fmt; | ||
| 2 | use tb = termbox2; | ||
| 3 | |||
| 4 | def bg = tb::COLOR.DEFAULT; | ||
| 5 | def fg = tb::COLOR.BLACK; | ||
| 6 | def paint_colors = [ | ||
| 7 | (tb::COLOR.BLACK, "BLACK "), | ||
| 8 | (tb::COLOR.RED, "RED "), | ||
| 9 | (tb::COLOR.GREEN, "GREEN "), | ||
| 10 | (tb::COLOR.YELLOW, "YELLOW "), | ||
| 11 | (tb::COLOR.BLUE, "BLUE "), | ||
| 12 | (tb::COLOR.MAGENTA, "MAGENTA"), | ||
| 13 | (tb::COLOR.CYAN, "CYAN "), | ||
| 14 | ]; | ||
| 15 | def help_text = [ | ||
| 16 | "Left-click to paint", | ||
| 17 | "Right-click to change color", | ||
| 18 | "Press q to exit", | ||
| 19 | ]; | ||
| 20 | def exit_keys = [ | ||
| 21 | tb::KEY.ESC, | ||
| 22 | tb::KEY.CTRL_Z, | ||
| 23 | tb::KEY.CTRL_C, | ||
| 24 | tb::KEY.CTRL_D, | ||
| 25 | ]; | ||
| 26 | def exit_chars = [ | ||
| 27 | 'q', | ||
| 28 | 'Q', | ||
| 29 | ]; | ||
| 30 | |||
| 31 | let ii: size = 0; // Current color index | ||
| 32 | |||
| 33 | fn handle_mouse_event(me: tb::mouse_event) bool = { | ||
| 34 | switch (me.key) { | ||
| 35 | case tb::KEY.MOUSE_LEFT => tb::set_cell(me.x, me.y, ' ', | ||
| 36 | tb::COLOR.DEFAULT, paint_colors[ii].0)!; | ||
| 37 | case tb::KEY.MOUSE_RIGHT => ii = (ii + 1) % len(paint_colors); | ||
| 38 | case => return false; | ||
| 39 | }; | ||
| 40 | |||
| 41 | return false; | ||
| 42 | }; | ||
| 43 | |||
| 44 | fn is_exit_key(ke: tb::key_event) bool = { | ||
| 45 | match (ke.key_or_ch) { | ||
| 46 | case let key: tb::key => for (let k .. exit_keys) | ||
| 47 | if (key == k) return true; | ||
| 48 | case let ch: rune => for (let c .. exit_chars) | ||
| 49 | if (ch == c) return true; | ||
| 50 | }; | ||
| 51 | return false; | ||
| 52 | }; | ||
| 53 | |||
| 54 | fn handle_events_and_exit() bool = { | ||
| 55 | let e = tb::poll_event(); | ||
| 56 | if (e is tb::err) | ||
| 57 | return false; | ||
| 58 | |||
| 59 | return match (e as tb::event) { | ||
| 60 | case tb::resize_event => yield false; | ||
| 61 | case let me: tb::mouse_event => yield handle_mouse_event(me); | ||
| 62 | case let ke: tb::key_event => yield is_exit_key(ke); | ||
| 63 | }; | ||
| 64 | }; | ||
| 65 | |||
| 66 | fn print_help_text() void = { | ||
| 67 | let y = (tb::height()! - len(help_text)): int; | ||
| 68 | let len0 = tb::print(0, y, fg, bg, help_text[0])!; | ||
| 69 | let (c, ctext) = paint_colors[ii]; | ||
| 70 | tb::print(len0: int + 1, y, c, bg, ctext)!; | ||
| 71 | for (let i = 1z; i < len(help_text); i += 1) | ||
| 72 | tb::print(0, y + i: int, fg, bg, help_text[i])!; | ||
| 73 | }; | ||
| 74 | |||
| 75 | export fn main() void = { | ||
| 76 | let e = tb::init(); | ||
| 77 | if (e is tb::err) | ||
| 78 | fmt::fatalf(tb::strerror(e as tb::err)); | ||
| 79 | defer tb::shutdown()!; | ||
| 80 | |||
| 81 | tb::set_input_mode(tb::INPUT.ESC | tb::INPUT.MOUSE)!; | ||
| 82 | tb::set_clear_attrs(fg, bg)!; | ||
| 83 | |||
| 84 | for (true) { | ||
| 85 | print_help_text(); | ||
| 86 | tb::present()!; | ||
| 87 | if (handle_events_and_exit()) break; | ||
| 88 | }; | ||
| 89 | return; | ||
| 90 | }; | ||
