2020-11-12 14:12:51 +03:00
|
|
|
import term.ui as tui
|
|
|
|
|
|
|
|
const (
|
|
|
|
colors = [
|
2021-01-26 17:43:10 +03:00
|
|
|
tui.Color{33, 150, 243},
|
|
|
|
tui.Color{0, 150, 136},
|
|
|
|
tui.Color{205, 220, 57},
|
|
|
|
tui.Color{255, 152, 0},
|
|
|
|
tui.Color{244, 67, 54},
|
|
|
|
tui.Color{156, 39, 176},
|
2020-11-12 14:12:51 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-11-14 22:05:22 +03:00
|
|
|
struct Point {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
struct App {
|
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
tui &tui.Context = unsafe { nil }
|
2020-11-12 14:12:51 +03:00
|
|
|
points []Point
|
|
|
|
color tui.Color = colors[0]
|
|
|
|
color_idx int
|
|
|
|
cut_rate f64 = 5
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn frame(mut app App) {
|
2020-11-12 14:12:51 +03:00
|
|
|
app.tui.clear()
|
|
|
|
|
|
|
|
if app.points.len > 0 {
|
|
|
|
app.tui.set_bg_color(app.color)
|
|
|
|
mut last := app.points[0]
|
2020-11-14 22:05:22 +03:00
|
|
|
for point in app.points {
|
|
|
|
// if the cursor moves quickly enough, different events are not
|
2020-11-12 14:12:51 +03:00
|
|
|
// necessarily touching, so we need to draw a line between them
|
2020-11-14 22:05:22 +03:00
|
|
|
app.tui.draw_line(last.x, last.y, point.x, point.y)
|
|
|
|
last = point
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
app.tui.reset()
|
|
|
|
|
|
|
|
l := int(app.points.len / app.cut_rate) + 1
|
|
|
|
app.points = app.points[l..].clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
ww := app.tui.window_width
|
|
|
|
|
|
|
|
app.tui.bold()
|
|
|
|
app.tui.draw_text(ww / 6, 2, 'V term.input: cursor chaser demo')
|
|
|
|
app.tui.draw_text((ww - ww / 6) - 14, 2, 'cut rate: ${(100 / app.cut_rate):3.0f}%')
|
|
|
|
app.tui.horizontal_separator(3)
|
|
|
|
app.tui.reset()
|
|
|
|
app.tui.flush()
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn event(e &tui.Event, mut app App) {
|
2020-11-12 14:12:51 +03:00
|
|
|
match e.typ {
|
|
|
|
.key_down {
|
|
|
|
match e.code {
|
|
|
|
.escape {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
.space, .enter {
|
|
|
|
app.color_idx++
|
2021-01-26 17:43:10 +03:00
|
|
|
if app.color_idx == colors.len {
|
|
|
|
app.color_idx = 0
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
app.color = colors[app.color_idx]
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
.mouse_move, .mouse_drag, .mouse_down {
|
|
|
|
app.points << Point{e.x, e.y}
|
|
|
|
}
|
|
|
|
.mouse_scroll {
|
2020-11-12 14:12:51 +03:00
|
|
|
d := if e.direction == .up { 0.1 } else { -0.1 }
|
|
|
|
app.cut_rate += d
|
2021-01-26 17:43:10 +03:00
|
|
|
if app.cut_rate < 1 {
|
|
|
|
app.cut_rate = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:34:49 +03:00
|
|
|
fn main() {
|
|
|
|
mut app := &App{}
|
|
|
|
app.tui = tui.init(
|
|
|
|
user_data: app
|
|
|
|
frame_fn: frame
|
|
|
|
event_fn: event
|
|
|
|
hide_cursor: true
|
|
|
|
)
|
2023-07-02 09:38:33 +03:00
|
|
|
app.tui.run()!
|
2021-02-04 18:34:49 +03:00
|
|
|
}
|