2020-11-12 14:12:51 +03:00
|
|
|
import term.ui as tui
|
|
|
|
import rand
|
|
|
|
|
|
|
|
struct Rect {
|
|
|
|
mut:
|
|
|
|
c tui.Color
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
x2 int
|
|
|
|
y2 int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
tui &tui.Context = unsafe { nil }
|
2020-11-12 14:12:51 +03:00
|
|
|
rects []Rect
|
|
|
|
cur_rect Rect
|
|
|
|
is_drag bool
|
|
|
|
redraw bool
|
|
|
|
}
|
|
|
|
|
|
|
|
fn random_color() tui.Color {
|
2021-07-20 11:17:08 +03:00
|
|
|
return tui.Color{
|
2022-04-15 14:58:56 +03:00
|
|
|
r: rand.u8()
|
|
|
|
g: rand.u8()
|
|
|
|
b: rand.u8()
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
.mouse_down {
|
|
|
|
app.is_drag = true
|
2021-07-20 11:17:08 +03:00
|
|
|
app.cur_rect = Rect{
|
2020-11-12 14:12:51 +03:00
|
|
|
c: random_color()
|
2021-01-26 17:43:10 +03:00
|
|
|
x: e.x
|
|
|
|
y: e.y
|
2020-11-12 14:12:51 +03:00
|
|
|
x2: e.x
|
|
|
|
y2: e.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mouse_drag {
|
|
|
|
app.cur_rect.x2 = e.x
|
|
|
|
app.cur_rect.y2 = e.y
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
.mouse_up {
|
2020-11-12 14:12:51 +03:00
|
|
|
app.rects << app.cur_rect
|
|
|
|
app.is_drag = false
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
.key_down {
|
|
|
|
if e.code == .c {
|
|
|
|
app.rects.clear()
|
|
|
|
} else if e.code == .escape {
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
app.redraw = true
|
|
|
|
}
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
fn frame(mut app App) {
|
2021-01-26 17:43:10 +03:00
|
|
|
if !app.redraw {
|
|
|
|
return
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
|
|
|
app.tui.clear()
|
|
|
|
|
|
|
|
for rect in app.rects {
|
|
|
|
app.tui.set_bg_color(rect.c)
|
|
|
|
app.tui.draw_rect(rect.x, rect.y, rect.x2, rect.y2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.is_drag {
|
|
|
|
r := app.cur_rect
|
|
|
|
app.tui.set_bg_color(r.c)
|
|
|
|
app.tui.draw_empty_rect(r.x, r.y, r.x2, r.y2)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.tui.reset_bg_color()
|
|
|
|
app.tui.flush()
|
|
|
|
app.redraw = false
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:34:49 +03:00
|
|
|
fn main() {
|
|
|
|
mut app := &App{}
|
|
|
|
app.tui = tui.init(
|
|
|
|
user_data: app
|
|
|
|
event_fn: event
|
|
|
|
frame_fn: frame
|
|
|
|
hide_cursor: true
|
|
|
|
frame_rate: 60
|
|
|
|
)
|
2023-07-02 09:38:33 +03:00
|
|
|
app.tui.run()!
|
2021-02-04 18:34:49 +03:00
|
|
|
}
|