Horizontal layout

This commit is contained in:
Emil Ernerfeldt 2018-12-28 10:39:08 +01:00
parent 702ec23372
commit 1e24095154
4 changed files with 130 additions and 66 deletions

Binary file not shown.

View file

@ -39,24 +39,17 @@ impl GuiSettings for App {
gui.checkbox("checkbox", &mut self.checked); gui.checkbox("checkbox", &mut self.checked);
if gui gui.horizontal(|gui| {
.radio("First alternative", self.selected_alternative == 0) if gui.radio("First", self.selected_alternative == 0).clicked {
.clicked self.selected_alternative = 0;
{ }
self.selected_alternative = 0; if gui.radio("Second", self.selected_alternative == 1).clicked {
} self.selected_alternative = 1;
if gui }
.radio("Second alternative", self.selected_alternative == 1) if gui.radio("Final", self.selected_alternative == 2).clicked {
.clicked self.selected_alternative = 2;
{ }
self.selected_alternative = 1; });
}
if gui
.radio("Final alternative", self.selected_alternative == 2)
.clicked
{
self.selected_alternative = 2;
}
if gui.button("Click me").clicked { if gui.button("Click me").clicked {
self.count += 1; self.count += 1;

View file

@ -1,4 +1,4 @@
use crate::{layout, math::*, style, types::*}; use crate::{layout, style, types::*};
/// Encapsulates input, layout and painting for ease of use. /// Encapsulates input, layout and painting for ease of use.
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -12,14 +12,7 @@ impl Emgui {
pub fn new_frame(&mut self, new_input: RawInput) { pub fn new_frame(&mut self, new_input: RawInput) {
let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input); let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input);
self.last_input = new_input; self.last_input = new_input;
self.layout.new_frae(gui_input);
// TODO: this should be nicer
self.layout.commands.clear();
self.layout.cursor = vec2(0.0, 0.0);
self.layout.input = gui_input;
if !gui_input.mouse_down {
self.layout.memory.active_id = None;
}
} }
pub fn paint(&mut self) -> Vec<PaintCmd> { pub fn paint(&mut self) -> Vec<PaintCmd> {

View file

@ -33,7 +33,7 @@ impl Default for LayoutOptions {
char_size: vec2(7.2, 14.0), char_size: vec2(7.2, 14.0),
item_spacing: vec2(8.0, 4.0), item_spacing: vec2(8.0, 4.0),
indent: 21.0, indent: 21.0,
width: 200.0, width: 250.0,
button_padding: vec2(5.0, 3.0), button_padding: vec2(5.0, 3.0),
start_icon_width: 20.0, start_icon_width: 20.0,
} }
@ -43,9 +43,9 @@ impl Default for LayoutOptions {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Memory { struct Memory {
/// The widget being interacted with (e.g. dragged, in case of a slider). /// The widget being interacted with (e.g. dragged, in case of a slider).
pub active_id: Option<Id>, active_id: Option<Id>,
/// Which foldable regions are open. /// Which foldable regions are open.
open_foldables: HashSet<Id>, open_foldables: HashSet<Id>,
@ -62,16 +62,61 @@ type TextFragments = Vec<TextFragment>;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Clone, Copy, Debug, PartialEq)]
enum Direction {
Horizontal,
Vertical,
}
impl Default for Direction {
fn default() -> Direction {
Direction::Vertical
}
}
// ----------------------------------------------------------------------------
// TODO: give this a better name
#[derive(Clone, Debug, Default)]
struct Layouter {
/// Doesn't change.
dir: Direction,
/// Changes only along self.dir
cursor: Vec2,
/// We keep track of our max-size along the orthogonal to self.dir
size: Vec2,
}
impl Layouter {
/// Reserve this much space and move the cursor.
fn reserve_space(&mut self, size: Vec2) {
if self.dir == Direction::Horizontal {
self.cursor.x += size.x;
self.size.x += size.x;
self.size.y = self.size.y.max(size.y);
} else {
self.cursor.y += size.y;
self.size.y += size.y;
self.size.x = self.size.x.max(size.x);
}
}
}
// ----------------------------------------------------------------------------
type Id = u64; type Id = u64;
/// TODO: non-pub
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Layout { pub struct Layout {
pub options: LayoutOptions, pub options: LayoutOptions, // TODO: remove pub
pub input: GuiInput, input: GuiInput,
pub cursor: Vec2, memory: Memory,
id: Id, id: Id,
pub memory: Memory, layouter: Layouter,
pub commands: Vec<GuiCmd>, pub commands: Vec<GuiCmd>, // TODO: remove pub
} }
impl Layout { impl Layout {
@ -83,15 +128,25 @@ impl Layout {
&self.commands &self.commands
} }
// TODO: move
pub fn new_frae(&mut self, gui_input: GuiInput) {
self.commands.clear();
self.layouter = Default::default();
self.input = gui_input;
if !gui_input.mouse_down {
self.memory.active_id = None;
}
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
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 (text, text_size) = self.layout_text(&text); let (text, text_size) = self.layout_text(&text);
let text_cursor = self.cursor + self.options.button_padding; let text_cursor = self.layouter.cursor + self.options.button_padding;
let (rect, interact) = let (rect, interact) =
self.reserve_space(id, text_size + 2.0 * self.options.button_padding); self.reserve_interactive_space(id, text_size + 2.0 * self.options.button_padding);
self.commands.push(GuiCmd::Button { interact, rect }); self.commands.push(GuiCmd::Button { interact, rect });
self.add_text(text_cursor, text); self.add_text(text_cursor, text);
interact interact
@ -101,9 +156,10 @@ impl Layout {
let text: String = text.into(); let text: String = text.into();
let id = self.get_id(&text); let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text); let (text, text_size) = self.layout_text(&text);
let text_cursor = let text_cursor = self.layouter.cursor
self.cursor + self.options.button_padding + vec2(self.options.start_icon_width, 0.0); + self.options.button_padding
let (rect, interact) = self.reserve_space( + vec2(self.options.start_icon_width, 0.0);
let (rect, interact) = self.reserve_interactive_space(
id, id,
self.options.button_padding self.options.button_padding
+ vec2(self.options.start_icon_width, 0.0) + vec2(self.options.start_icon_width, 0.0)
@ -125,9 +181,8 @@ impl Layout {
pub fn label<S: Into<String>>(&mut self, text: S) { pub fn label<S: Into<String>>(&mut self, text: S) {
let text: String = text.into(); let text: String = text.into();
let (text, text_size) = self.layout_text(&text); let (text, text_size) = self.layout_text(&text);
self.add_text(self.cursor, text); self.add_text(self.layouter.cursor, text);
self.cursor.y += text_size.y; self.reserve_space_default_spacing(text_size);
self.cursor.y += self.options.item_spacing.y;
} }
/// A radio button /// A radio button
@ -135,9 +190,10 @@ impl Layout {
let text: String = text.into(); let text: String = text.into();
let id = self.get_id(&text); let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text); let (text, text_size) = self.layout_text(&text);
let text_cursor = let text_cursor = self.layouter.cursor
self.cursor + self.options.button_padding + vec2(self.options.start_icon_width, 0.0); + self.options.button_padding
let (rect, interact) = self.reserve_space( + vec2(self.options.start_icon_width, 0.0);
let (rect, interact) = self.reserve_interactive_space(
id, id,
self.options.button_padding self.options.button_padding
+ vec2(self.options.start_icon_width, 0.0) + vec2(self.options.start_icon_width, 0.0)
@ -164,9 +220,9 @@ impl Layout {
let text: String = text.into(); let text: String = text.into();
let id = self.get_id(&text); let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&format!("{}: {:.3}", text, value)); let (text, text_size) = self.layout_text(&format!("{}: {:.3}", text, value));
self.add_text(self.cursor, text); self.add_text(self.layouter.cursor, text);
self.cursor.y += text_size.y; self.layouter.reserve_space(text_size);
let (slider_rect, interact) = self.reserve_space( let (slider_rect, interact) = self.reserve_interactive_space(
id, id,
Vec2 { Vec2 {
x: self.options.width, x: self.options.width,
@ -203,11 +259,15 @@ impl Layout {
S: Into<String>, S: Into<String>,
F: FnOnce(&mut Layout), F: FnOnce(&mut Layout),
{ {
assert!(
self.layouter.dir == Direction::Vertical,
"Horizontal foldable is unimplemented"
);
let text: String = text.into(); let text: String = text.into();
let id = self.get_id(&text); let id = self.get_id(&text);
let (text, text_size) = self.layout_text(&text); let (text, text_size) = self.layout_text(&text);
let text_cursor = self.cursor + self.options.button_padding; let text_cursor = self.layouter.cursor + self.options.button_padding;
let (rect, interact) = self.reserve_space( let (rect, interact) = self.reserve_interactive_space(
id, id,
vec2( vec2(
self.options.width, self.options.width,
@ -234,29 +294,46 @@ impl Layout {
if open { if open {
let old_id = self.id; let old_id = self.id;
self.id = id; self.id = id;
let old_x = self.cursor.x; let old_x = self.layouter.cursor.x;
self.cursor.x += self.options.indent; self.layouter.cursor.x += self.options.indent;
add_contents(self); add_contents(self);
self.cursor.x = old_x; self.layouter.cursor.x = old_x;
self.id = old_id; self.id = old_id;
} }
interact interact
} }
// ------------------------------------------------------------------------ /// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, add_contents: F)
fn reserve_space(&mut self, id: Id, size: Vec2) -> (Rect, InteractInfo) { where
let rect = Rect { F: FnOnce(&mut Layout),
pos: self.cursor, {
size, let horizontal_layouter = Layouter {
dir: Direction::Horizontal,
cursor: self.layouter.cursor,
..Default::default()
}; };
let interact = self.interactive_rect(id, &rect); let old_layouter = std::mem::replace(&mut self.layouter, horizontal_layouter);
self.cursor.y += rect.size.y + self.options.item_spacing.y; add_contents(self);
(rect, interact) let horizontal_layouter = std::mem::replace(&mut self.layouter, old_layouter);
self.layouter.reserve_space(horizontal_layouter.size);
} }
fn interactive_rect(&mut self, id: Id, rect: &Rect) -> InteractInfo { // ------------------------------------------------------------------------
fn reserve_space_default_spacing(&mut self, size: Vec2) -> Rect {
let rect = Rect {
pos: self.layouter.cursor,
size,
};
self.layouter
.reserve_space(size + self.options.item_spacing);
rect
}
fn reserve_interactive_space(&mut self, id: Id, size: Vec2) -> (Rect, InteractInfo) {
let rect = self.reserve_space_default_spacing(size);
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;
if clicked { if clicked {
@ -264,11 +341,12 @@ impl Layout {
} }
let active = self.memory.active_id == Some(id); let active = self.memory.active_id == Some(id);
InteractInfo { let interact = InteractInfo {
hovered, hovered,
clicked, clicked,
active, active,
} };
(rect, interact)
} }
fn get_id(&self, id_str: &str) -> Id { fn get_id(&self, id_str: &str) -> Id {