2020-11-26 02:28:57 +03:00
|
|
|
|
// Copyright (c) 2020 Raúl Hernández. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
|
// that can be found in the LICENSE file.
|
2020-11-12 14:12:51 +03:00
|
|
|
|
module main
|
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
|
import term.ui
|
2020-11-12 14:12:51 +03:00
|
|
|
|
|
|
|
|
|
// The color palette, taken from Google's Material design
|
|
|
|
|
const (
|
|
|
|
|
colors = [
|
|
|
|
|
[
|
2021-01-26 17:43:10 +03:00
|
|
|
|
ui.Color{239, 154, 154},
|
|
|
|
|
ui.Color{244, 143, 177},
|
|
|
|
|
ui.Color{206, 147, 216},
|
|
|
|
|
ui.Color{179, 157, 219},
|
|
|
|
|
ui.Color{159, 168, 218},
|
|
|
|
|
ui.Color{144, 202, 249},
|
|
|
|
|
ui.Color{129, 212, 250},
|
|
|
|
|
ui.Color{128, 222, 234},
|
|
|
|
|
ui.Color{128, 203, 196},
|
|
|
|
|
ui.Color{165, 214, 167},
|
|
|
|
|
ui.Color{197, 225, 165},
|
|
|
|
|
ui.Color{230, 238, 156},
|
|
|
|
|
ui.Color{255, 245, 157},
|
|
|
|
|
ui.Color{255, 224, 130},
|
|
|
|
|
ui.Color{255, 204, 128},
|
|
|
|
|
ui.Color{255, 171, 145},
|
|
|
|
|
ui.Color{188, 170, 164},
|
|
|
|
|
ui.Color{238, 238, 238},
|
|
|
|
|
ui.Color{176, 190, 197},
|
2020-11-13 12:49:09 +03:00
|
|
|
|
],
|
|
|
|
|
[
|
2021-01-26 17:43:10 +03:00
|
|
|
|
ui.Color{244, 67, 54},
|
|
|
|
|
ui.Color{233, 30, 99},
|
|
|
|
|
ui.Color{156, 39, 176},
|
|
|
|
|
ui.Color{103, 58, 183},
|
|
|
|
|
ui.Color{63, 81, 181},
|
|
|
|
|
ui.Color{33, 150, 243},
|
|
|
|
|
ui.Color{3, 169, 244},
|
|
|
|
|
ui.Color{0, 188, 212},
|
|
|
|
|
ui.Color{0, 150, 136},
|
|
|
|
|
ui.Color{76, 175, 80},
|
|
|
|
|
ui.Color{139, 195, 74},
|
|
|
|
|
ui.Color{205, 220, 57},
|
|
|
|
|
ui.Color{255, 235, 59},
|
|
|
|
|
ui.Color{255, 193, 7},
|
|
|
|
|
ui.Color{255, 152, 0},
|
|
|
|
|
ui.Color{255, 87, 34},
|
|
|
|
|
ui.Color{121, 85, 72},
|
|
|
|
|
ui.Color{120, 120, 120},
|
|
|
|
|
ui.Color{96, 125, 139},
|
2020-11-13 12:49:09 +03:00
|
|
|
|
],
|
|
|
|
|
[
|
2021-01-26 17:43:10 +03:00
|
|
|
|
ui.Color{198, 40, 40},
|
|
|
|
|
ui.Color{173, 20, 87},
|
|
|
|
|
ui.Color{106, 27, 154},
|
|
|
|
|
ui.Color{69, 39, 160},
|
|
|
|
|
ui.Color{40, 53, 147},
|
|
|
|
|
ui.Color{21, 101, 192},
|
|
|
|
|
ui.Color{2, 119, 189},
|
|
|
|
|
ui.Color{0, 131, 143},
|
|
|
|
|
ui.Color{0, 105, 92},
|
|
|
|
|
ui.Color{46, 125, 50},
|
|
|
|
|
ui.Color{85, 139, 47},
|
|
|
|
|
ui.Color{158, 157, 36},
|
|
|
|
|
ui.Color{249, 168, 37},
|
|
|
|
|
ui.Color{255, 143, 0},
|
|
|
|
|
ui.Color{239, 108, 0},
|
|
|
|
|
ui.Color{216, 67, 21},
|
|
|
|
|
ui.Color{78, 52, 46},
|
|
|
|
|
ui.Color{33, 33, 33},
|
|
|
|
|
ui.Color{55, 71, 79},
|
2020-11-13 12:49:09 +03:00
|
|
|
|
],
|
2020-11-12 14:12:51 +03:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2020-11-13 12:49:09 +03:00
|
|
|
|
frame_rate = 30 // fps
|
2020-11-12 14:12:51 +03:00
|
|
|
|
msg_display_time = 5 * frame_rate
|
2020-11-13 12:49:09 +03:00
|
|
|
|
w = 200
|
|
|
|
|
h = 100
|
|
|
|
|
space = ' '
|
|
|
|
|
spaces = ' '
|
|
|
|
|
select_color = 'Select color: '
|
|
|
|
|
select_size = 'Size: + -'
|
|
|
|
|
help_1 = '╭────────╮'
|
|
|
|
|
help_2 = '│ HELP │'
|
|
|
|
|
help_3 = '╰────────╯'
|
2020-11-12 14:12:51 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
|
mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
|
ui &ui.Context = unsafe { nil }
|
2020-11-14 22:05:22 +03:00
|
|
|
|
header_text []string
|
|
|
|
|
mouse_pos Point
|
|
|
|
|
msg string
|
|
|
|
|
msg_hide_tick int
|
2021-01-26 17:43:10 +03:00
|
|
|
|
primary_color ui.Color = colors[1][6]
|
|
|
|
|
secondary_color ui.Color = colors[1][9]
|
|
|
|
|
primary_color_idx int = 25
|
|
|
|
|
secondary_color_idx int = 28
|
|
|
|
|
bg_color ui.Color = ui.Color{0, 0, 0}
|
|
|
|
|
drawing [][]ui.Color = [][]ui.Color{len: h, init: []ui.Color{len: w}}
|
|
|
|
|
size int = 1
|
2020-11-14 22:05:22 +03:00
|
|
|
|
should_redraw bool = true
|
|
|
|
|
is_dragging bool
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Point {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
mut:
|
2020-11-12 14:12:51 +03:00
|
|
|
|
x int
|
|
|
|
|
y int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
mut app := &App{}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui = ui.init(
|
2020-11-12 14:12:51 +03:00
|
|
|
|
user_data: app
|
|
|
|
|
frame_fn: frame
|
|
|
|
|
event_fn: event
|
|
|
|
|
frame_rate: frame_rate
|
|
|
|
|
hide_cursor: true
|
2020-11-13 16:30:47 +03:00
|
|
|
|
window_title: 'V terminal pixelart drawing app'
|
2021-01-26 17:43:10 +03:00
|
|
|
|
)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.mouse_pos.x = 40
|
|
|
|
|
app.mouse_pos.y = 15
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.clear()
|
2022-05-13 06:56:21 +03:00
|
|
|
|
app.ui.run()?
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
|
fn frame(mut app App) {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
mut redraw := app.should_redraw
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if app.msg != '' && app.ui.frame_count >= app.msg_hide_tick {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.msg = ''
|
|
|
|
|
redraw = true
|
|
|
|
|
}
|
|
|
|
|
if redraw {
|
|
|
|
|
app.render(false)
|
|
|
|
|
app.should_redraw = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 09:28:01 +03:00
|
|
|
|
fn event(event &ui.Event, mut app App) {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
match event.typ {
|
|
|
|
|
.mouse_down {
|
|
|
|
|
app.is_dragging = true
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if app.ui.window_height - event.y < 5 {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.footer_click(event)
|
|
|
|
|
} else {
|
|
|
|
|
app.paint(event)
|
|
|
|
|
}
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
.mouse_up {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.is_dragging = false
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
.mouse_drag {
|
2021-07-20 11:17:08 +03:00
|
|
|
|
app.mouse_pos = Point{
|
2020-11-13 12:49:09 +03:00
|
|
|
|
x: event.x
|
|
|
|
|
y: event.y
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.paint(event)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
.mouse_move {
|
2021-07-20 11:17:08 +03:00
|
|
|
|
app.mouse_pos = Point{
|
2020-11-13 12:49:09 +03:00
|
|
|
|
x: event.x
|
|
|
|
|
y: event.y
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.mouse_scroll {
|
2021-07-20 11:17:08 +03:00
|
|
|
|
app.mouse_pos = Point{
|
2020-11-14 10:14:54 +03:00
|
|
|
|
x: event.x
|
|
|
|
|
y: event.y
|
|
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
|
d := event.direction == .down
|
2021-02-21 17:07:49 +03:00
|
|
|
|
if event.modifiers.has(.ctrl) {
|
|
|
|
|
p := !event.modifiers.has(.shift)
|
2020-11-14 22:05:22 +03:00
|
|
|
|
c := if d {
|
|
|
|
|
if p { app.primary_color_idx - 1 } else { app.secondary_color_idx - 1 }
|
|
|
|
|
} else {
|
|
|
|
|
if p { app.primary_color_idx + 1 } else { app.secondary_color_idx + 1 }
|
|
|
|
|
}
|
|
|
|
|
app.select_color(p, c)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
} else {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if d {
|
|
|
|
|
app.inc_size()
|
|
|
|
|
} else {
|
|
|
|
|
app.dec_size()
|
|
|
|
|
}
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.key_down {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
match event.code {
|
2020-11-13 14:08:28 +03:00
|
|
|
|
.f1, ._1 {
|
|
|
|
|
oevent := *event
|
2021-01-26 17:43:10 +03:00
|
|
|
|
nevent := ui.Event{
|
|
|
|
|
...oevent
|
|
|
|
|
button: ui.MouseButton.left
|
|
|
|
|
x: app.mouse_pos.x
|
|
|
|
|
y: app.mouse_pos.y
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
app.paint(nevent)
|
|
|
|
|
}
|
|
|
|
|
.f2, ._2 {
|
2020-11-14 10:14:54 +03:00
|
|
|
|
oevent := *event
|
2021-01-26 17:43:10 +03:00
|
|
|
|
nevent := ui.Event{
|
|
|
|
|
...oevent
|
|
|
|
|
button: ui.MouseButton.right
|
|
|
|
|
x: app.mouse_pos.x
|
|
|
|
|
y: app.mouse_pos.y
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
app.paint(nevent)
|
|
|
|
|
}
|
2021-02-21 17:07:49 +03:00
|
|
|
|
.space, .enter {
|
2020-11-13 14:08:28 +03:00
|
|
|
|
oevent := *event
|
2021-01-26 17:43:10 +03:00
|
|
|
|
nevent := ui.Event{
|
|
|
|
|
...oevent
|
2021-02-21 17:07:49 +03:00
|
|
|
|
button: .left
|
|
|
|
|
x: app.mouse_pos.x
|
|
|
|
|
y: app.mouse_pos.y
|
|
|
|
|
}
|
|
|
|
|
app.paint(nevent)
|
|
|
|
|
}
|
|
|
|
|
.delete, .backspace {
|
|
|
|
|
oevent := *event
|
|
|
|
|
nevent := ui.Event{
|
|
|
|
|
...oevent
|
|
|
|
|
button: .middle
|
2021-01-26 17:43:10 +03:00
|
|
|
|
x: app.mouse_pos.x
|
|
|
|
|
y: app.mouse_pos.y
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
app.paint(nevent)
|
|
|
|
|
}
|
|
|
|
|
.j, .down {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
if event.modifiers.has(.shift) {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
|
|
|
|
|
}
|
|
|
|
|
app.mouse_pos.y++
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
.k, .up {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
if event.modifiers.has(.shift) {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
|
|
|
|
|
}
|
|
|
|
|
app.mouse_pos.y--
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
.h, .left {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
if event.modifiers.has(.shift) {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
|
|
|
|
|
}
|
|
|
|
|
app.mouse_pos.x -= 2
|
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
.l, .right {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
if event.modifiers.has(.shift) {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.set_pixel((1 + app.mouse_pos.x) / 2, app.mouse_pos.y, app.primary_color)
|
|
|
|
|
}
|
|
|
|
|
app.mouse_pos.x += 2
|
|
|
|
|
}
|
|
|
|
|
.t {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
p := !event.modifiers.has(.alt)
|
|
|
|
|
c := if event.modifiers.has(.shift) {
|
2020-11-14 22:05:22 +03:00
|
|
|
|
if p { app.primary_color_idx - 19 } else { app.secondary_color_idx - 19 }
|
|
|
|
|
} else {
|
|
|
|
|
if p { app.primary_color_idx + 19 } else { app.secondary_color_idx + 19 }
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
|
app.select_color(p, c)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
.r {
|
2021-02-21 17:07:49 +03:00
|
|
|
|
p := !event.modifiers.has(.alt)
|
|
|
|
|
c := if event.modifiers.has(.shift) {
|
2020-11-14 22:05:22 +03:00
|
|
|
|
if p { app.primary_color_idx - 1 } else { app.secondary_color_idx - 1 }
|
|
|
|
|
} else {
|
|
|
|
|
if p { app.primary_color_idx + 1 } else { app.secondary_color_idx + 1 }
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
|
app.select_color(p, c)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
.plus {
|
|
|
|
|
app.inc_size()
|
|
|
|
|
}
|
|
|
|
|
.minus {
|
|
|
|
|
app.dec_size()
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
.c {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.drawing = [][]ui.Color{len: h, init: []ui.Color{len: w}}
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2020-11-13 14:08:28 +03:00
|
|
|
|
.q, .escape {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.render(true)
|
|
|
|
|
exit(0)
|
|
|
|
|
}
|
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
app.should_redraw = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) render(paint_only bool) {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.clear()
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.draw_header()
|
|
|
|
|
app.draw_content()
|
|
|
|
|
if !paint_only {
|
|
|
|
|
app.draw_footer()
|
|
|
|
|
app.draw_cursor()
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.flush()
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 22:05:22 +03:00
|
|
|
|
fn (mut app App) select_color(primary bool, idx int) {
|
|
|
|
|
c := (idx + 57) % 57
|
|
|
|
|
cx := c % 19
|
|
|
|
|
cy := (c / 19) % 3
|
|
|
|
|
color := colors[cy][cx]
|
|
|
|
|
if primary {
|
|
|
|
|
app.primary_color_idx = c % (19 * 3)
|
|
|
|
|
app.primary_color = color
|
|
|
|
|
} else {
|
|
|
|
|
app.secondary_color_idx = c % (19 * 3)
|
|
|
|
|
app.secondary_color = color
|
|
|
|
|
}
|
|
|
|
|
c_str := if primary { 'primary' } else { 'secondary' }
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.show_msg('set ${c_str} color idx: ${idx}', 1)
|
2020-11-14 22:05:22 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
|
fn (mut app App) set_pixel(x_ int, y_ int, c ui.Color) {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
// Term coords start at 1, and adjust for the header
|
|
|
|
|
x, y := x_ - 1, y_ - 4
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if y < 0 || app.ui.window_height - y < 3 {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if y >= app.drawing.len || x < 0 || x >= app.drawing[0].len {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.drawing[y][x] = c
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
|
fn (mut app App) paint(event &ui.Event) {
|
|
|
|
|
if event.y < 4 || app.ui.window_height - event.y < 4 {
|
2020-11-14 22:05:22 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
x_start, y_start := int(f32((event.x - 1) / 2) - app.size / 2 + 1), event.y - app.size / 2
|
2020-11-13 14:08:28 +03:00
|
|
|
|
color := match event.button {
|
2020-11-14 10:14:54 +03:00
|
|
|
|
.left { app.primary_color }
|
|
|
|
|
.right { app.secondary_color }
|
2020-11-13 14:08:28 +03:00
|
|
|
|
else { app.bg_color }
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
for x in x_start .. x_start + app.size {
|
|
|
|
|
for y in y_start .. y_start + app.size {
|
|
|
|
|
app.set_pixel(x, y, color)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) draw_content() {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
w_, mut h_ := app.ui.window_width / 2, app.ui.window_height - 8
|
2021-01-10 23:41:29 +03:00
|
|
|
|
if h_ > app.drawing.len {
|
|
|
|
|
h_ = app.drawing.len
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
2021-01-10 23:41:29 +03:00
|
|
|
|
for row_idx, row in app.drawing[..h_] {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_cursor_position(0, row_idx + 4)
|
|
|
|
|
mut last := ui.Color{0, 0, 0}
|
2021-01-10 23:41:29 +03:00
|
|
|
|
for cell in row[..w_] {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
if cell.r == 0 && cell.g == 0 && cell.b == 0 {
|
|
|
|
|
if !(cell.r == last.r && cell.g == last.g && cell.b == last.b) {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset()
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if !(cell.r == last.r && cell.g == last.g && cell.b == last.b) {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_bg_color(cell)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.write(spaces)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
last = cell
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset()
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) draw_cursor() {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if app.mouse_pos.y in [3, app.ui.window_height - 5] {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
// inside the horizontal separators
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
cursor_color := if app.is_dragging { ui.Color{220, 220, 220} } else { ui.Color{160, 160, 160} }
|
|
|
|
|
app.ui.set_bg_color(cursor_color)
|
|
|
|
|
if app.mouse_pos.y >= 3 && app.mouse_pos.y <= app.ui.window_height - 4 {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
// inside the main content
|
|
|
|
|
mut x_start := int(f32((app.mouse_pos.x - 1) / 2) - app.size / 2 + 1) * 2 - 1
|
|
|
|
|
mut y_start := app.mouse_pos.y - app.size / 2
|
|
|
|
|
mut x_end := x_start + app.size * 2 - 1
|
|
|
|
|
mut y_end := y_start + app.size - 1
|
2020-11-13 12:49:09 +03:00
|
|
|
|
if x_start < 1 {
|
|
|
|
|
x_start = 1
|
|
|
|
|
}
|
|
|
|
|
if y_start < 4 {
|
|
|
|
|
y_start = 4
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if x_end > app.ui.window_width {
|
|
|
|
|
x_end = app.ui.window_width
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if y_end > app.ui.window_height - 5 {
|
|
|
|
|
y_end = app.ui.window_height - 5
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.draw_rect(x_start, y_start, x_end, y_end)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
} else {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.draw_text(app.mouse_pos.x, app.mouse_pos.y, space)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset()
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) draw_header() {
|
|
|
|
|
if app.msg != '' {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_color(
|
2020-11-13 12:49:09 +03:00
|
|
|
|
r: 0
|
|
|
|
|
g: 0
|
|
|
|
|
b: 0
|
2021-01-26 17:43:10 +03:00
|
|
|
|
)
|
|
|
|
|
app.ui.set_bg_color(
|
2020-11-13 12:49:09 +03:00
|
|
|
|
r: 220
|
|
|
|
|
g: 220
|
|
|
|
|
b: 220
|
2021-01-26 17:43:10 +03:00
|
|
|
|
)
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.ui.draw_text(0, 0, ' ${app.msg} ')
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset()
|
2020-11-14 10:14:54 +03:00
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
//'tick: $app.ui.frame_count | ' +
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.ui.draw_text(3, 2, 'terminal size: (${app.ui.window_width}, ${app.ui.window_height}) | primary color: ${app.primary_color.hex()} | secondary color: ${app.secondary_color.hex()}')
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.horizontal_separator(3)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) draw_footer() {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
_, wh := app.ui.window_width, app.ui.window_height
|
|
|
|
|
app.ui.horizontal_separator(wh - 4)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
for i, color_row in colors {
|
|
|
|
|
for j, color in color_row {
|
|
|
|
|
x := j * 3 + 19
|
|
|
|
|
y := wh - 3 + i
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_bg_color(color)
|
2020-11-14 22:05:22 +03:00
|
|
|
|
if app.primary_color_idx == j + (i * 19) {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_color(r: 0, g: 0, b: 0)
|
|
|
|
|
app.ui.draw_text(x, y, '><')
|
|
|
|
|
app.ui.reset_color()
|
2020-11-14 22:05:22 +03:00
|
|
|
|
} else if app.secondary_color_idx == j + (i * 19) {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.set_color(r: 255, g: 255, b: 255)
|
|
|
|
|
app.ui.draw_text(x, y, '><')
|
|
|
|
|
app.ui.reset_color()
|
2020-11-13 12:49:09 +03:00
|
|
|
|
} else {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.draw_rect(x, y, x + 1, y)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset_bg_color()
|
|
|
|
|
app.ui.draw_text(3, wh - 3, select_color)
|
|
|
|
|
app.ui.bold()
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.ui.draw_text(3, wh - 1, '${select_size} ${app.size}')
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.ui.reset()
|
2020-11-14 10:14:54 +03:00
|
|
|
|
|
2020-11-13 16:30:47 +03:00
|
|
|
|
// TODO: help button
|
|
|
|
|
// if ww >= 90 {
|
2021-01-26 17:43:10 +03:00
|
|
|
|
// app.ui.draw_text(80, wh - 3, help_1)
|
|
|
|
|
// app.ui.draw_text(80, wh - 2, help_2)
|
|
|
|
|
// app.ui.draw_text(80, wh - 1, help_3)
|
2020-11-13 16:30:47 +03:00
|
|
|
|
// }
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
|
fn (mut app App) inc_size() {
|
2020-11-14 22:05:22 +03:00
|
|
|
|
if app.size < 30 {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
app.size++
|
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.show_msg('inc. size: ${app.size}', 1)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
|
fn (mut app App) dec_size() {
|
2020-11-13 12:49:09 +03:00
|
|
|
|
if app.size > 1 {
|
|
|
|
|
app.size--
|
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
|
app.show_msg('dec. size: ${app.size}', 1)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
|
fn (mut app App) footer_click(event &ui.Event) {
|
|
|
|
|
footer_y := 3 - (app.ui.window_height - event.y)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
match event.x {
|
|
|
|
|
8...11 {
|
|
|
|
|
app.inc_size()
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
12...15 {
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.dec_size()
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
18...75 {
|
|
|
|
|
if (event.x % 3) == 0 {
|
|
|
|
|
// Inside the gap between tiles
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
idx := footer_y * 19 - 6 + event.x / 3
|
2021-01-26 17:43:10 +03:00
|
|
|
|
if idx < 0 || idx > 56 {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
|
app.select_color(event.button == .left, idx)
|
2020-11-13 12:49:09 +03:00
|
|
|
|
}
|
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut app App) show_msg(text string, time int) {
|
|
|
|
|
frames := time * frame_rate
|
2021-01-26 17:43:10 +03:00
|
|
|
|
app.msg_hide_tick = if time > 0 { int(app.ui.frame_count) + frames } else { -1 }
|
2020-11-12 14:12:51 +03:00
|
|
|
|
app.msg = text
|
|
|
|
|
}
|