Skip to content

Commit 0773387

Browse files
authoredFeb 25, 2021
gg.Event instead of sapp.event, starting tap event (vlang#304)
1 parent b476246 commit 0773387

37 files changed

+985
-222
lines changed
 

‎button.v

+34-13
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import os
1010
const (
1111
button_bg_color = gx.rgb(28, 28, 28)
1212
button_border_color = gx.rgb(200, 200, 200)
13-
btn_text_cfg = gx.TextCfg{ // color: gx.white, {
14-
color: gx.rgb(38, 38, 38)
15-
align: gx.align_left
16-
}
1713
button_horizontal_padding = 26
1814
button_vertical_padding = 8
1915
)
@@ -31,6 +27,9 @@ pub struct ButtonConfig {
3127
onclick ButtonClickFn
3228
height int = 20
3329
width int
30+
z_index int
31+
text_cfg gx.TextCfg
32+
text_size f64
3433
}
3534

3635
[heap]
@@ -42,6 +41,7 @@ pub mut:
4241
state ButtonState
4342
height int
4443
width int
44+
z_index int
4545
x int
4646
y int
4747
parent Layout
@@ -52,6 +52,9 @@ pub mut:
5252
icon_path string
5353
image gg.Image
5454
use_icon bool
55+
text_cfg gx.TextCfg
56+
text_size f64
57+
fixed_text bool
5558
}
5659

5760
fn (mut b Button) init(parent Layout) {
@@ -61,6 +64,16 @@ fn (mut b Button) init(parent Layout) {
6164
if b.use_icon {
6265
b.image = b.ui.gg.create_image(b.icon_path)
6366
}
67+
if is_empty_text_cfg(b.text_cfg) {
68+
b.text_cfg = b.ui.window.text_cfg
69+
}
70+
if b.text_size > 0 {
71+
_, win_height := b.ui.window.size()
72+
b.text_cfg = gx.TextCfg{
73+
...b.text_cfg
74+
size: text_size_as_int(b.text_size, win_height)
75+
}
76+
}
6477
mut subscriber := parent.get_subscriber()
6578
subscriber.subscribe_method(events.on_mouse_down, btn_click, b)
6679
subscriber.subscribe_method(events.on_click, btn_click, b)
@@ -70,10 +83,13 @@ pub fn button(c ButtonConfig) &Button {
7083
mut b := &Button{
7184
width: c.width
7285
height: c.height
86+
z_index: c.z_index
7387
text: c.text
7488
icon_path: c.icon_path
7589
use_icon: c.icon_path != ''
7690
onclick: c.onclick
91+
text_cfg: c.text_cfg
92+
text_size: c.text_size
7793
ui: 0
7894
}
7995
if b.use_icon && !os.exists(c.icon_path) {
@@ -103,7 +119,7 @@ fn (mut b Button) set_pos(x int, y int) {
103119
}
104120

105121
fn (mut b Button) size() (int, int) {
106-
if b.width == 0 && b.height == 0 {
122+
if b.width == 0 || b.height == 0 {
107123
b.set_text_size()
108124
}
109125
return b.width, b.height
@@ -118,7 +134,7 @@ fn (mut b Button) propose_size(w int, h int) (int, int) {
118134
b.height = h
119135
}
120136
// b.height = h
121-
// b.width = b.ui.ft.text_width(b.text) + button_horizontal_padding
137+
// b.width = b.ui.ft.text_width(b.text) + ui.button_horizontal_padding
122138
// b.height = 20 // vertical padding
123139
return b.width, b.height
124140
}
@@ -145,15 +161,20 @@ fn (mut b Button) draw() {
145161
b.ui.gg.draw_empty_rect(b.x, b.y, b.width, b.height, ui.button_border_color)
146162
mut y := bcenter_y - h2 - 1
147163
// if b.ui.gg.scale == 2 {
148-
$if macos { // TODO
149-
y -= 2
150-
}
164+
// $if macos { // TODO
165+
// y -= 2
166+
// }
151167
if b.use_icon {
152168
b.ui.gg.draw_image(b.x, b.y, b.width, b.height, b.image)
153169
} else {
154-
b.ui.gg.draw_text(bcenter_x - w2, y, b.text, ui.btn_text_cfg)
170+
// b.ui.gg.draw_text(bcenter_x - w2, y, b.text, b.text_cfg.as_text_cfg())
171+
b.draw_text(bcenter_x - w2, y, b.text)
172+
}
173+
$if bb ? {
174+
draw_bb(b, b.ui)
175+
draw_text_bb(bcenter_x - w2, y, b.text_width, b.text_height, b.ui)
155176
}
156-
// b.ui.gg.draw_empty_rect(bcenter_x-w2, bcenter_y-h2, text_width, text_height, button_border_color)
177+
// b.ui.gg.draw_empty_rect(bcenter_x-w2, bcenter_y-h2, text_width, text_height, ui.button_border_color)
157178
}
158179

159180
fn (mut b Button) set_text_size() {
@@ -164,8 +185,8 @@ fn (mut b Button) set_text_size() {
164185
// if b.text_width == 0 || b.text_height == 0 {
165186
else if b.ui != 0 {
166187
b.text_width, b.text_height = b.ui.gg.text_size(b.text)
167-
b.text_width = int(f32(b.text_width) * b.ui.gg.scale) // * b.ui.gg.scale)
168-
b.text_height = int(f32(b.text_height) * b.ui.gg.scale) // * b.ui.gg.scale)
188+
b.text_width = int(f32(b.text_width) * b.ui.gg.scale * b.ui.gg.scale)
189+
b.text_height = int(f32(b.text_height) * b.ui.gg.scale * b.ui.gg.scale)
169190
if b.width == 0 {
170191
b.width = b.text_width + ui.button_horizontal_padding
171192
}

‎canvas.v

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ pub type DrawFn = fn (ctx &gg.Context, state voidptr, c &Canvas) // x_offset int
99

1010
pub struct Canvas {
1111
pub mut:
12-
width int
13-
height int
14-
x int
15-
y int
12+
width int
13+
height int
14+
x int
15+
y int
16+
z_index int
1617
mut:
1718
parent Layout
1819
draw_fn DrawFn = voidptr(0)
@@ -22,6 +23,7 @@ mut:
2223
pub struct CanvasConfig {
2324
width int
2425
height int
26+
z_index int
2527
text string
2628
draw_fn DrawFn = voidptr(0)
2729
}
@@ -35,6 +37,7 @@ pub fn canvas(c CanvasConfig) &Canvas {
3537
mut canvas := &Canvas{
3638
width: c.width
3739
height: c.height
40+
z_index: c.z_index
3841
draw_fn: c.draw_fn
3942
}
4043
return canvas

‎checkbox.v

+11-3
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ pub mut:
2727
width int
2828
x int
2929
y int
30+
z_index int
3031
parent Layout
3132
is_focused bool
3233
checked bool
3334
ui &UI
3435
on_check_changed CheckChangedFn
3536
text string
3637
disabled bool
38+
text_cfg gx.TextCfg
3739
}
3840

3941
pub struct CheckBoxConfig {
4042
x int
4143
y int
44+
z_index int
4245
parent Layout
4346
text string
4447
on_check_changed CheckChangedFn
@@ -48,16 +51,17 @@ pub struct CheckBoxConfig {
4851

4952
fn (mut cb CheckBox) init(parent Layout) {
5053
cb.parent = parent
51-
pui := parent.get_ui()
52-
cb.ui = pui
54+
cb.ui = parent.get_ui()
5355
cb.width = cb.ui.gg.text_width(cb.text) + 5 + ui.check_mark_size
56+
cb.text_cfg = cb.ui.window.text_cfg
5457
mut subscriber := parent.get_subscriber()
5558
subscriber.subscribe_method(events.on_click, cb_click, cb)
5659
}
5760

5861
pub fn checkbox(c CheckBoxConfig) &CheckBox {
5962
mut cb := &CheckBox{
6063
height: 20 // TODO
64+
z_index: c.z_index
6165
ui: 0
6266
text: c.text
6367
on_check_changed: c.on_check_changed
@@ -70,6 +74,7 @@ pub fn checkbox(c CheckBoxConfig) &CheckBox {
7074
fn cb_click(mut cb CheckBox, e &MouseEvent, window &Window) {
7175
if cb.point_inside(e.x, e.y) { // && e.action == 0 {
7276
cb.checked = !cb.checked
77+
// println("checked: $cb.checked")
7378
if cb.on_check_changed != voidptr(0) {
7479
cb.on_check_changed(window.state, cb.checked)
7580
}
@@ -113,7 +118,10 @@ fn (mut cb CheckBox) draw() {
113118
cb.ui.gg.draw_image(cb.x + 3, cb.y + 3, 8, 8, cb.ui.cb_image)
114119
}
115120
// Text
116-
cb.ui.gg.draw_text(cb.x + ui.check_mark_size + 5, cb.y, cb.text, btn_text_cfg)
121+
cb.ui.gg.draw_text(cb.x + ui.check_mark_size + 5, cb.y, cb.text, cb.text_cfg)
122+
$if bb ? {
123+
draw_bb(cb, cb.ui)
124+
}
117125
}
118126

119127
fn (cb &CheckBox) point_inside(x f64, y f64) bool {

‎dropdown.v

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mut:
2222
parent Layout
2323
x int
2424
y int
25+
z_index int
2526
ui &UI
2627
items []DropdownItem
2728
open bool
@@ -36,6 +37,7 @@ pub struct DropdownConfig {
3637
x int
3738
y int
3839
width int
40+
z_index int = 10
3941
parent Layout
4042
selected_index int = -1
4143
on_selection_changed SelectionChangedFn
@@ -59,6 +61,7 @@ fn (mut dd Dropdown) init(parent Layout) {
5961
pub fn dropdown(c DropdownConfig, items []DropdownItem) &Dropdown {
6062
mut dd := &Dropdown{
6163
width: c.width
64+
z_index: c.z_index
6265
items: items
6366
selected_index: c.selected_index
6467
on_selection_changed: c.on_selection_changed
@@ -83,7 +86,7 @@ fn (mut dd Dropdown) propose_size(w int, h int) (int, int) {
8386
return w, ui.dropdown_height
8487
}
8588

86-
fn (mut dd Dropdown) draw() {
89+
fn (dd &Dropdown) draw() {
8790
gg := dd.ui.gg
8891
// draw the main dropdown
8992
gg.draw_rect(dd.x, dd.y, dd.width, ui.dropdown_height, ui.dropdown_color)
@@ -93,8 +96,15 @@ fn (mut dd Dropdown) draw() {
9396
} else {
9497
gg.draw_text_def(dd.x + 5, dd.y + 5, dd.def_text)
9598
}
99+
dd.draw_open()
100+
// draw the arrow
101+
gg.draw_image(dd.x + (dd.width - 28), dd.y - 3, 28, 28, dd.ui.down_arrow)
102+
}
103+
104+
fn (dd &Dropdown) draw_open() {
96105
// draw the drawer
97106
if dd.open {
107+
gg := dd.ui.gg
98108
gg.draw_rect(dd.x, dd.y + ui.dropdown_height, dd.width, dd.items.len * ui.dropdown_height,
99109
ui.drawer_color)
100110
gg.draw_empty_rect(dd.x, dd.y + ui.dropdown_height, dd.width, dd.items.len * ui.dropdown_height,
@@ -109,8 +119,6 @@ fn (mut dd Dropdown) draw() {
109119
gg.draw_text_def(dd.x + 5, y + i * ui.dropdown_height + 5, item.text)
110120
}
111121
}
112-
// draw the arrow
113-
gg.draw_image(dd.x + (dd.width - 28), dd.y - 3, 28, 28, dd.ui.down_arrow)
114122
}
115123

116124
pub fn (mut dd Dropdown) add_item(text string) {

‎examples/android_window.v

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import ui
2+
import sokol.sapp
3+
4+
struct App {
5+
mut:
6+
window &ui.Window
7+
text string
8+
}
9+
10+
fn main() {
11+
mut app := &App{
12+
window: 0
13+
text: 'size= $sapp.dpi_scale() $sapp.width() $sapp.height()'
14+
}
15+
window := ui.window({
16+
title: 'V Android Test'
17+
width: 800
18+
height: 600
19+
state: app
20+
mode: .max_size
21+
}, [
22+
ui.column({
23+
widths: ui.stretch
24+
}, [
25+
ui.textbox(
26+
text: &app.text
27+
placeholder: '0'
28+
// width: 135
29+
read_only: true
30+
),
31+
]),
32+
])
33+
app.window = window
34+
app.text = 'size= ($window.width,$window.height) $sapp.dpi_scale() $sapp.width() $sapp.height()'
35+
ui.run(window)
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.