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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::caches::TextureCache;
use crate::event::PushrodEvent;
use crate::event::PushrodEvent::{
DrawFrame, MouseButton, WidgetMouseEntered, WidgetMouseExited, WidgetSelected,
};
use crate::primitives::draw_base;
use crate::properties::{
WidgetProperties, PROPERTY_BORDER_WIDTH, PROPERTY_ID, PROPERTY_TEXT_JUSTIFICATION,
PROPERTY_TOGGLED, TEXT_JUSTIFY_CENTER, TEXT_JUSTIFY_LEFT, TEXT_JUSTIFY_RIGHT,
};
use crate::texture_store::TextureStore;
use crate::widget::Widget;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
const PROPERTY_BUTTON_ACTIVE: u32 = 20000;
const PROPERTY_BUTTON_IN_BOUNDS: u32 = 20001;
const PROPERTY_BUTTON_ORIGINATED: u32 = 20002;
const PROPERTY_BUTTON_HOVERED: u32 = 20003;
#[derive(Default)]
pub struct ToggleButtonWidget {
texture_store: TextureStore,
properties: WidgetProperties,
}
impl ToggleButtonWidget {
fn set_hovered(&mut self) {
self.properties.set_bool(PROPERTY_BUTTON_HOVERED);
self.invalidate();
}
fn set_unhovered(&mut self) {
self.properties.delete(PROPERTY_BUTTON_HOVERED);
self.invalidate();
}
fn toggle_selected(&mut self) {
if self.properties.get_bool(PROPERTY_TOGGLED) {
self.properties.delete(PROPERTY_TOGGLED);
} else {
self.properties.set_bool(PROPERTY_TOGGLED);
}
}
fn is_selected(&self) -> bool {
self.properties.get_bool(PROPERTY_TOGGLED)
}
}
impl Widget for ToggleButtonWidget {
widget_default_impl!();
fn draw(&mut self, c: &mut Canvas<Window>, t: &mut TextureCache) -> Option<&Texture> {
if self.invalidated() {
let bounds = self.properties.get_bounds();
let text_color = if self.properties.get_bool(PROPERTY_BUTTON_HOVERED) {
if self.is_selected() {
Some(Color::BLACK)
} else {
Some(Color::WHITE)
}
} else if self.is_selected() {
Some(Color::WHITE)
} else {
None
};
let (font_texture, width, height) = t.render_text(c, &mut self.properties, text_color);
let text_justification = self.properties.get_value(PROPERTY_TEXT_JUSTIFICATION);
let border_width = self.properties.get_value(PROPERTY_BORDER_WIDTH);
let widget_w = bounds.0;
let texture_x: i32 = match text_justification {
TEXT_JUSTIFY_LEFT => 0,
TEXT_JUSTIFY_CENTER => (widget_w - width) as i32 / 2,
TEXT_JUSTIFY_RIGHT => (widget_w - width) as i32,
_ => 0,
};
self.texture_store
.create_or_resize_texture(c, bounds.0, bounds.1);
let cloned_properties = self.properties.clone();
let back_color = if self.properties.get_bool(PROPERTY_BUTTON_HOVERED) {
if self.is_selected() {
Some(Color::WHITE)
} else {
Some(Color::BLACK)
}
} else if self.is_selected() {
Some(Color::BLACK)
} else {
None
};
c.with_texture_canvas(self.texture_store.get_mut_ref(), |texture| {
draw_base(texture, &cloned_properties, back_color);
texture
.copy(
&font_texture,
None,
Rect::new(
texture_x + border_width,
((bounds.1 / 2) - (height / 2) - (border_width / 2) as u32) as i32,
width,
height,
),
)
.unwrap();
})
.unwrap();
}
self.texture_store.get_optional_ref()
}
fn handle_event(&mut self, event: PushrodEvent) -> Option<PushrodEvent> {
match event {
WidgetMouseEntered { .. } => {
if self.properties.get_bool(PROPERTY_BUTTON_ACTIVE) {
self.set_hovered();
}
self.properties.set_bool(PROPERTY_BUTTON_IN_BOUNDS);
}
WidgetMouseExited { .. } => {
if self.properties.get_bool(PROPERTY_BUTTON_ACTIVE) {
self.set_unhovered();
}
self.properties.delete(PROPERTY_BUTTON_IN_BOUNDS);
}
MouseButton {
widget_id,
button,
state,
} => {
let current_widget_id = self.properties.get_value(PROPERTY_ID);
if button == 1 {
if state {
self.set_hovered();
self.properties.set_bool(PROPERTY_BUTTON_ACTIVE);
self.properties.set_bool(PROPERTY_BUTTON_ORIGINATED);
} else {
let had_bounds = self.properties.get_bool(PROPERTY_BUTTON_ACTIVE);
self.set_unhovered();
self.properties.delete(PROPERTY_BUTTON_ACTIVE);
if self.properties.get_bool(PROPERTY_BUTTON_IN_BOUNDS)
&& had_bounds
&& self.properties.get_bool(PROPERTY_BUTTON_ORIGINATED)
&& current_widget_id as u32 == widget_id
{
self.properties.delete(PROPERTY_BUTTON_ORIGINATED);
self.toggle_selected();
return Some(WidgetSelected {
widget_id: current_widget_id as u32,
state: self.is_selected(),
});
}
self.properties.delete(PROPERTY_BUTTON_ORIGINATED);
}
}
}
DrawFrame { .. } => {}
_ => eprintln!("ButtonWidget Event: {:?}", event),
}
None
}
}