-
-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathui.rs
More file actions
287 lines (259 loc) · 10.4 KB
/
Copy pathui.rs
File metadata and controls
287 lines (259 loc) · 10.4 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use macroquad::prelude::*;
use macroquad::ui::widgets::{ProgressBar, Slider};
use macroquad::ui::{
hash, root_ui,
widgets::{self, Group},
Drag, Ui,
};
pub struct Slot {
id: u64,
item: Option<String>,
}
impl Slot {
fn new(id: u64) -> Slot {
Slot { id, item: None }
}
}
pub enum FittingCommand {
/// Remove item from this slot
Unfit { target_slot: u64 },
/// Fit item from inventory to slot
Fit { target_slot: u64, item: String },
/// Move item from one slot to another
Refit { target_slot: u64, origin_slot: u64 },
}
pub struct Data {
inventory: Vec<String>,
item_dragging: bool,
slots: Vec<(&'static str, Slot)>,
fit_command: Option<FittingCommand>,
}
impl Data {
pub fn new() -> Data {
Data {
inventory: vec![],
item_dragging: false,
slots: vec![
("Left Mouse Button", Slot::new(hash!())),
("Right Mouse Button", Slot::new(hash!())),
("Middle Mouse Button", Slot::new(hash!())),
("Space", Slot::new(hash!())),
("\"1\"", Slot::new(hash!())),
("\"2\"", Slot::new(hash!())),
("\"3\"", Slot::new(hash!())),
],
fit_command: None,
}
}
fn slots(&mut self, ui: &mut Ui) {
let item_dragging = &mut self.item_dragging;
let fit_command = &mut self.fit_command;
for (label, slot) in self.slots.iter_mut() {
Group::new(hash!("grp", slot.id, &label), Vec2::new(210., 55.)).ui(ui, |ui| {
let drag = Group::new(slot.id, Vec2::new(50., 50.))
// slot without item is not draggable
.draggable(slot.item.is_some())
// but could be a target of drag
.hoverable(*item_dragging)
// and is highlighted with other color when some item is dragging
.highlight(*item_dragging)
.ui(ui, |ui| {
if let Some(ref item) = slot.item {
ui.label(Vec2::new(5., 10.), &item);
}
});
match drag {
// there is some item in this slot and it was dragged to another slot
Drag::Dropped(_, Some(id)) if slot.item.is_some() => {
*fit_command = Some(FittingCommand::Refit {
target_slot: id,
origin_slot: slot.id,
});
}
// there is some item in this slot and it was dragged out - unfit it
Drag::Dropped(_, None) if slot.item.is_some() => {
*fit_command = Some(FittingCommand::Unfit {
target_slot: slot.id,
});
}
// there is no item in this slot
// this is impossible - slots without items are non-draggable
Drag::Dropped(_, _) => unreachable!(),
Drag::Dragging(pos, id) => {
debug!("slots: pos: {:?}, id {:?}", pos, id);
*item_dragging = true;
}
Drag::No => {}
}
ui.label(Vec2::new(60., 20.), label);
});
}
}
fn inventory(&mut self, ui: &mut Ui) {
let item_dragging = &mut self.item_dragging;
for (n, item) in self.inventory.iter().enumerate() {
let drag = Group::new(hash!("inventory", n), Vec2::new(50., 50.))
.draggable(true)
.ui(ui, |ui| {
ui.label(Vec2::new(5., 10.), &item);
});
match drag {
Drag::Dropped(_, Some(id)) => {
self.fit_command = Some(FittingCommand::Fit {
target_slot: id,
item: item.clone(),
});
*item_dragging = false;
}
Drag::Dropped(_, _) => {
*item_dragging = false;
}
Drag::Dragging(pos, id) => {
debug!("inventory: pos: {:?}, id {:?}", pos, id);
*item_dragging = true;
}
_ => {}
}
}
}
fn set_item(&mut self, id: u64, item: Option<String>) {
if let Some(slot) = self.slots.iter_mut().find(|(_, slot)| slot.id == id) {
slot.1.item = item;
}
}
}
#[macroquad::main("UI showcase")]
async fn main() {
let mut data = Data::new();
let mut data0 = String::new();
let mut data1 = String::new();
let mut text0 = String::new();
let mut text1 = String::new();
let mut number0 = 0.;
let mut number1 = 0.;
let mut number2 = 0.;
let texture: Texture2D = load_texture("examples/ferris.png").await.unwrap();
loop {
clear_background(WHITE);
widgets::Window::new(hash!(), vec2(400., 200.), vec2(320., 400.))
.label("Shop")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
for i in 0..30 {
Group::new(hash!("shop", i), Vec2::new(300., 80.)).ui(ui, |ui| {
ui.label(Vec2::new(10., 10.), &format!("Item N {i}"));
ui.label(Vec2::new(260., 40.), "10/10");
ui.label(Vec2::new(200., 58.), &format!("{} kr", 800));
if ui.button(Vec2::new(260., 55.), "buy") {
data.inventory.push(format!("Item {i}"));
}
});
}
});
widgets::Window::new(hash!(), vec2(100., 220.), vec2(542., 430.))
.label("Fitting window")
.titlebar(true)
.ui(&mut *root_ui(), |ui| {
Group::new(hash!(), Vec2::new(230., 400.)).ui(ui, |ui| {
data.slots(ui);
});
Group::new(hash!(), Vec2::new(280., 400.)).ui(ui, |ui| {
data.inventory(ui);
});
});
widgets::Window::new(hash!(), vec2(470., 50.), vec2(300., 300.))
.label("Megaui Showcase Window")
.ui(&mut *root_ui(), |ui| {
ui.tree_node(hash!(), "input", |ui| {
ui.label(None, "Some random text");
if ui.button(None, "click me") {
println!("hi");
}
ui.separator();
ui.label(None, "Some other random text");
if ui.button(None, "other button") {
println!("hi2");
}
ui.separator();
ui.input_text(hash!(), "<- input text 1", &mut data0);
ui.input_text(hash!(), "<- input text 2", &mut data1);
ui.label(None, &format!("Text entered: \"{data0}\" and \"{data1}\""));
ui.separator();
});
ui.tree_node(hash!(), "buttons", |ui| {
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
ui.same_line(0.);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
widgets::Button::new("Button").size(vec2(120., 70.)).ui(ui);
ui.same_line(0.);
widgets::Button::new(texture.clone())
.size(vec2(120., 70.))
.ui(ui);
});
ui.tree_node(hash!(), "sliders and bars", |ui| {
let range0 = -10f32..10f32;
ui.slider(hash!(), "[-10 .. 10]", range0.clone(), &mut number0);
let progress0 = number0.remap(range0.start, range0.end, 0., 1.);
ProgressBar::new().label("first bar").ui(
ui,
progress0,
format!("{:.0}%", progress0 * 100.).as_str(),
);
let range1 = 0f32..100f32;
ui.slider(hash!(), "[0 .. 100]", range1.clone(), &mut number1);
let progress1 = number1.remap(range1.start, range1.end, 0., 1.);
ProgressBar::new().label("second bar").ui(
ui,
progress1,
format!("{:.1}/{:.0}", number1, range1.end).as_str(),
);
let range2 = 0f32..1f32;
Slider::new(hash!(), range2.clone())
.label_width(200.)
.label("slider with a long label")
.ui(ui, &mut number2);
let progress2 = number2;
ProgressBar::new()
.label("bar with a really long label")
.label_width(240.)
.ui(ui, progress2, "");
});
ui.tree_node(hash!(), "editbox 1", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text0);
});
ui.tree_node(hash!(), "editbox 2", |ui| {
ui.label(None, "This is editbox!");
ui.editbox(hash!(), vec2(285., 165.), &mut text1);
});
});
match data.fit_command.take() {
Some(FittingCommand::Unfit { target_slot }) => data.set_item(target_slot, None),
Some(FittingCommand::Fit { target_slot, item }) => {
data.set_item(target_slot, Some(item));
}
Some(FittingCommand::Refit {
target_slot,
origin_slot,
}) => {
let origin_item = data
.slots
.iter()
.find_map(|(_, slot)| {
if slot.id == origin_slot {
Some(slot.item.clone())
} else {
None
}
})
.flatten();
data.set_item(target_slot, origin_item);
data.set_item(origin_slot, None);
}
None => {}
};
next_frame().await;
}
}