Clean up layout.rs

This commit is contained in:
Emil Ernerfeldt 2018-12-27 19:35:02 +01:00
parent 4e03a607c6
commit 641b72d6b1

View file

@ -76,50 +76,40 @@ impl Layout {
pub fn button<S: Into<String>>(&mut self, text: S) -> InteractInfo { pub fn button<S: Into<String>>(&mut self, text: S) -> InteractInfo {
let text: String = text.into(); let text: String = text.into();
let id = self.get_id(&text); let id = self.get_id(&text);
let rect = Rect { let (rect, interact) = self.reserve_space(
pos: self.cursor, id,
size: Vec2 { Vec2 {
x: self.layout_options.width, x: self.layout_options.width,
y: self.layout_options.button_height, y: self.layout_options.button_height,
}, },
}; );
let interact = self.interactive_rect(id, &rect);
self.commands.push(GuiCmd::Button { self.commands.push(GuiCmd::Button {
interact, interact,
rect, rect,
text, text,
}); });
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
interact interact
} }
pub fn checkbox<S: Into<String>>(&mut self, label: S, checked: &mut bool) -> InteractInfo { pub fn checkbox<S: Into<String>>(&mut self, label: S, checked: &mut bool) -> InteractInfo {
let label: String = label.into(); let label: String = label.into();
let id = self.get_id(&label); let id = self.get_id(&label);
let rect = Rect { let (rect, interact) = self.reserve_space(
pos: self.cursor, id,
size: Vec2 { Vec2 {
x: self.layout_options.width, x: self.layout_options.width,
y: self.layout_options.checkbox_radio_height, y: self.layout_options.checkbox_radio_height,
}, },
}; );
let interact = self.interactive_rect(id, &rect);
if interact.clicked { if interact.clicked {
*checked = !*checked; *checked = !*checked;
} }
self.commands.push(GuiCmd::Checkbox { self.commands.push(GuiCmd::Checkbox {
checked: *checked, checked: *checked,
interact, interact,
rect, rect,
text: label, text: label,
}); });
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
interact interact
} }
@ -136,24 +126,19 @@ impl Layout {
pub fn radio<S: Into<String>>(&mut self, label: S, checked: bool) -> InteractInfo { pub fn radio<S: Into<String>>(&mut self, label: S, checked: bool) -> InteractInfo {
let label: String = label.into(); let label: String = label.into();
let id = self.get_id(&label); let id = self.get_id(&label);
let rect = Rect { let (rect, interact) = self.reserve_space(
pos: self.cursor, id,
size: Vec2 { Vec2 {
x: self.layout_options.width, x: self.layout_options.width,
y: self.layout_options.checkbox_radio_height, y: self.layout_options.checkbox_radio_height,
}, },
}; );
let interact = self.interactive_rect(id, &rect);
self.commands.push(GuiCmd::RadioButton { self.commands.push(GuiCmd::RadioButton {
checked, checked,
interact, interact,
rect, rect,
text: label, text: label,
}); });
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
interact interact
} }
@ -164,18 +149,16 @@ impl Layout {
min: f32, min: f32,
max: f32, max: f32,
) -> InteractInfo { ) -> InteractInfo {
debug_assert!(min <= max);
let label: String = label.into(); let label: String = label.into();
let id = self.get_id(&label); let id = self.get_id(&label);
let rect = Rect { let (rect, interact) = self.reserve_space(
pos: self.cursor, id,
size: Vec2 { Vec2 {
x: self.layout_options.width, x: self.layout_options.width,
y: self.layout_options.slider_height, y: self.layout_options.slider_height,
}, },
}; );
let interact = self.interactive_rect(id, &rect);
debug_assert!(min <= max);
if interact.active { if interact.active {
*value = remap_clamp(self.input.mouse_pos.x, rect.min().x, rect.max().x, min, max); *value = remap_clamp(self.input.mouse_pos.x, rect.min().x, rect.max().x, min, max);
@ -190,8 +173,6 @@ impl Layout {
value: *value, value: *value,
}); });
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
interact interact
} }
@ -205,17 +186,13 @@ impl Layout {
{ {
let label: String = label.into(); let label: String = label.into();
let id = self.get_id(&label); let id = self.get_id(&label);
let (rect, interact) = self.reserve_space(
let rect = Rect { id,
pos: self.cursor, Vec2 {
size: Vec2 {
x: self.layout_options.width, x: self.layout_options.width,
y: self.layout_options.button_height, y: self.layout_options.button_height,
}, },
}; );
let interact = self.interactive_rect(id, &rect);
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
if interact.clicked { if interact.clicked {
if self.state.open_foldables.contains(&id) { if self.state.open_foldables.contains(&id) {
@ -248,6 +225,16 @@ impl Layout {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
fn reserve_space(&mut self, id: Id, size: Vec2) -> (Rect, InteractInfo) {
let rect = Rect {
pos: self.cursor,
size,
};
let interact = self.interactive_rect(id, &rect);
self.cursor.y += rect.size.y + self.layout_options.item_spacing.y;
(rect, interact)
}
fn interactive_rect(&mut self, id: Id, rect: &Rect) -> InteractInfo { fn interactive_rect(&mut self, id: Id, rect: &Rect) -> InteractInfo {
let hovered = rect.contains(self.input.mouse_pos); let hovered = rect.contains(self.input.mouse_pos);
let clicked = hovered && self.input.mouse_clicked; let clicked = hovered && self.input.mouse_clicked;