-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton.odin
More file actions
143 lines (122 loc) · 3.99 KB
/
button.odin
File metadata and controls
143 lines (122 loc) · 3.99 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package syl
import "core:fmt"
Button_State :: enum {
Default,
Hover,
Press,
}
Button :: struct {
using box: Layout_Box,
text: ^Text,
style: ^Button_Styles_Override,
button_state: Button_State,
on_click: any,
on_mouse_over: any,
}
button_destroy:: proc(button: ^Button) {
button_deinit(button)
free(button)
}
button_deinit :: proc(button: ^Button) {
if button == nil do return
layout_box_deinit(button)
if button.on_click != nil {
free(button.on_click.data)
}
if button.on_mouse_over != nil {
free(button.on_mouse_over.data)
}
}
button_change_state :: proc(button: ^Button, state: Button_State) {
button.button_state = state
if button.style_sheet == nil do return
hover := &button.style_sheet.button.hover
press := &button.style_sheet.button.press
if button.style != nil {
hover = &button.style.hover
press = &button.style.press
}
switch state {
case .Default:
button_set_style(button, nil)
if button.text != nil do text_set_style_from_box_style(button.text, nil)
case .Hover:
button_set_style(button, hover)
if button.text != nil do text_set_style_from_box_style(button.text, hover)
case .Press:
button_set_style(button, press)
if button.text != nil do text_set_style_from_box_style(button.text, press)
}
}
button_dispatch :: proc(button: ^Button, message: any) {
if message != nil {
if button.owner != nil {
if h, ok := button.owner.handler.?; ok {
h.handler(h.element, message.data)
}
}
}
}
Rectangle :: struct {
x: f32,
y: f32,
width: f32,
height: f32,
}
check_collision_point_rect :: proc(point: [2]f32, rect: Rectangle) -> bool {
return point.x >= rect.x && point.x <= (rect.x + rect.width) &&
point.y >= rect.y && point.y <= (rect.y + rect.height)
}
update_button :: proc(button: ^Button) {
box_rect := Rectangle{button.global_position.x, button.global_position.y, button.size.x, button.size.y}
collide := check_collision_point_rect(ctx.mouse_pos, box_rect)
if collide && ctx.mouse_pressed_bits == {.LEFT} {
if button.button_state != .Press {
button_dispatch(button, button.on_click)
button_change_state(button, .Press)
}
} else if collide {
if button.button_state == .Default {
button_dispatch(button, button.on_mouse_over)
button_change_state(button, .Hover)
} else if button.button_state == .Press {
// Transition from Press to Hover without dispatching
button_change_state(button, .Hover)
}
} else {
if button.button_state != .Default {
button_change_state(button, .Default)
}
}
for child in button.children do element_update(child)
}
// Style ______________________________________________________________________
Button_Styles :: struct {
normal: Box_Style,
hover: Box_Style_Override,
press: Box_Style_Override
}
Button_Styles_Override :: struct {
normal: Box_Style_Override,
hover: Box_Style_Override,
press: Box_Style_Override,
}
button_set_style_default :: proc(button: ^Button, style: ^Box_Style_Override, use_transitions: bool = true) {
default := button.style_sheet.button.normal
fallback: ^Box_Style_Override
if button.style != nil {
fallback = &button.style.normal
}
layout_box_set_style(button, style, fallback, default, use_transitions)
}
button_set_style :: proc(button: ^Button, style: ^Box_Style_Override, use_transitions: bool = true) {
default := button.style_sheet.button.normal
fallback: ^Box_Style_Override
if button.style != nil {
fallback = &button.style.normal
}
layout_box_set_style(button, style, fallback, default, use_transitions)
}
button_apply_style :: proc(button: ^Button, style: Box_Style_Override, use_transitions: bool = true) {
layout_box_apply_style(button, style, use_transitions)
}