-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_canvas.v
106 lines (92 loc) · 1.78 KB
/
widget_canvas.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2020-2022 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module ui
import gg
pub type DrawFn = fn (ctx &gg.Context, state voidptr, c &Canvas) // x_offset int, y_offset int)
[heap]
pub struct Canvas {
pub mut:
id string
width int
height int
x int
y int
offset_x int
offset_y int
z_index int
ui &UI = 0
hidden bool
// component state for composable widget
component voidptr
mut:
parent Layout = empty_stack
draw_fn DrawFn = voidptr(0)
gg &gg.Context = 0
}
[params]
pub struct CanvasParams {
id string
width int
height int
z_index int
text string
draw_fn DrawFn = voidptr(0)
}
pub fn canvas(c CanvasParams) &Canvas {
mut canvas := &Canvas{
id: c.id
width: c.width
height: c.height
z_index: c.z_index
draw_fn: c.draw_fn
}
return canvas
}
fn (mut c Canvas) init(parent Layout) {
c.parent = parent
c.gg = parent.get_ui().gg
}
[manualfree]
pub fn (mut c Canvas) cleanup() {
unsafe { c.free() }
}
[unsafe]
pub fn (c &Canvas) free() {
$if free ? {
print('canvas $c.id')
}
unsafe {
c.id.free()
free(c)
}
$if free ? {
println(' -> freed')
}
}
fn (mut c Canvas) set_pos(x int, y int) {
c.x = x
c.y = y
}
fn (mut c Canvas) size() (int, int) {
return c.width, c.height
}
fn (mut c Canvas) propose_size(w int, h int) (int, int) {
c.width = w
c.height = h
return c.width, c.height
}
fn (mut c Canvas) draw() {
offset_start(mut c)
state := c.parent.get_state()
if c.draw_fn != voidptr(0) {
c.draw_fn(c.gg, state, c)
}
offset_end(mut c)
}
fn (mut c Canvas) set_visible(state bool) {
c.hidden = !state
}
fn (c &Canvas) point_inside(x f64, y f64) bool {
return point_inside(c, x, y)
}