2018-12-27 18:08:43 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2018-12-26 16:01:46 +00:00
|
|
|
use crate::{math::*, types::*};
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize)]
|
|
|
|
pub struct LayoutOptions {
|
2018-12-27 22:26:05 +00:00
|
|
|
/// The width and height of a single character (including any spacing).
|
|
|
|
/// All text is monospace!
|
|
|
|
pub char_size: Vec2,
|
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
// Horizontal and vertical spacing between widgets
|
|
|
|
pub item_spacing: Vec2,
|
|
|
|
|
2018-12-27 18:08:43 +00:00
|
|
|
/// Indent foldable regions etc by this much.
|
|
|
|
pub indent: f32,
|
|
|
|
|
2018-12-27 22:26:05 +00:00
|
|
|
/// Default width of sliders, foldout categories etc. TODO: percentage of parent?
|
2018-12-26 22:08:50 +00:00
|
|
|
pub width: f32,
|
|
|
|
|
2018-12-27 22:26:05 +00:00
|
|
|
/// Button size is text size plus this on each side
|
|
|
|
pub button_padding: Vec2,
|
2018-12-26 22:08:50 +00:00
|
|
|
|
|
|
|
/// Height of a checkbox and radio button
|
|
|
|
pub checkbox_radio_height: f32,
|
|
|
|
|
|
|
|
/// Height of a slider
|
|
|
|
pub slider_height: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LayoutOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
LayoutOptions {
|
2018-12-27 22:26:05 +00:00
|
|
|
char_size: vec2(7.2, 14.0),
|
|
|
|
item_spacing: vec2(5.0, 3.0),
|
2018-12-27 18:08:43 +00:00
|
|
|
indent: 21.0,
|
2018-12-26 22:08:50 +00:00
|
|
|
width: 200.0,
|
2018-12-27 22:26:05 +00:00
|
|
|
button_padding: vec2(8.0, 8.0),
|
2018-12-26 22:08:50 +00:00
|
|
|
checkbox_radio_height: 24.0,
|
|
|
|
slider_height: 32.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2018-12-27 18:08:43 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2018-12-27 22:26:05 +00:00
|
|
|
pub struct Memory {
|
2018-12-26 14:28:38 +00:00
|
|
|
/// The widget being interacted with (e.g. dragged, in case of a slider).
|
|
|
|
pub active_id: Option<Id>,
|
2018-12-27 18:08:43 +00:00
|
|
|
|
|
|
|
/// Which foldable regions are open.
|
|
|
|
open_foldables: HashSet<Id>,
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-27 22:26:05 +00:00
|
|
|
struct TextFragment {
|
|
|
|
rect: Rect,
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
type TextFragments = Vec<TextFragment>;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
type Id = u64;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Layout {
|
|
|
|
pub layout_options: LayoutOptions,
|
2018-12-27 22:26:05 +00:00
|
|
|
pub input: GuiInput,
|
|
|
|
pub cursor: Vec2,
|
|
|
|
id: Id,
|
|
|
|
pub memory: Memory,
|
|
|
|
pub commands: Vec<GuiCmd>,
|
2018-12-26 14:28:38 +00:00
|
|
|
}
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
impl Layout {
|
2018-12-26 09:46:23 +00:00
|
|
|
pub fn input(&self) -> &GuiInput {
|
|
|
|
&self.input
|
|
|
|
}
|
|
|
|
|
2018-12-26 14:28:38 +00:00
|
|
|
pub fn gui_commands(&self) -> &[GuiCmd] {
|
2018-12-26 09:46:23 +00:00
|
|
|
&self.commands
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub fn button<S: Into<String>>(&mut self, text: S) -> InteractInfo {
|
2018-12-26 14:28:38 +00:00
|
|
|
let text: String = text.into();
|
|
|
|
let id = self.get_id(&text);
|
2018-12-27 22:26:05 +00:00
|
|
|
let (text, text_size) = self.layout_text(&text);
|
|
|
|
let text_cursor = self.cursor + self.layout_options.button_padding;
|
|
|
|
let (rect, interact) =
|
|
|
|
self.reserve_space(id, text_size + 2.0 * self.layout_options.button_padding);
|
|
|
|
self.commands.push(GuiCmd::Button { interact, rect });
|
|
|
|
self.add_text(text_cursor, text);
|
2018-12-26 21:17:33 +00:00
|
|
|
interact
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn checkbox<S: Into<String>>(&mut self, label: S, checked: &mut bool) -> InteractInfo {
|
|
|
|
let label: String = label.into();
|
|
|
|
let id = self.get_id(&label);
|
2018-12-27 18:35:02 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
|
|
|
id,
|
|
|
|
Vec2 {
|
2018-12-26 22:08:50 +00:00
|
|
|
x: self.layout_options.width,
|
|
|
|
y: self.layout_options.checkbox_radio_height,
|
|
|
|
},
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-26 21:17:33 +00:00
|
|
|
if interact.clicked {
|
|
|
|
*checked = !*checked;
|
|
|
|
}
|
|
|
|
self.commands.push(GuiCmd::Checkbox {
|
|
|
|
checked: *checked,
|
|
|
|
interact,
|
|
|
|
rect,
|
|
|
|
text: label,
|
2018-12-26 14:28:38 +00:00
|
|
|
});
|
2018-12-26 13:38:46 +00:00
|
|
|
interact
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn label<S: Into<String>>(&mut self, text: S) {
|
2018-12-26 14:28:38 +00:00
|
|
|
let text: String = text.into();
|
2018-12-27 22:26:05 +00:00
|
|
|
let (text, text_size) = self.layout_text(&text);
|
|
|
|
self.add_text(self.cursor, text);
|
|
|
|
self.cursor.y += text_size.y;
|
2018-12-26 22:08:50 +00:00
|
|
|
self.cursor.y += self.layout_options.item_spacing.y;
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 21:26:15 +00:00
|
|
|
/// A radio button
|
|
|
|
pub fn radio<S: Into<String>>(&mut self, label: S, checked: bool) -> InteractInfo {
|
|
|
|
let label: String = label.into();
|
|
|
|
let id = self.get_id(&label);
|
2018-12-27 18:35:02 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
|
|
|
id,
|
|
|
|
Vec2 {
|
2018-12-26 22:08:50 +00:00
|
|
|
x: self.layout_options.width,
|
|
|
|
y: self.layout_options.checkbox_radio_height,
|
|
|
|
},
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-26 21:26:15 +00:00
|
|
|
self.commands.push(GuiCmd::RadioButton {
|
|
|
|
checked,
|
|
|
|
interact,
|
|
|
|
rect,
|
|
|
|
text: label,
|
|
|
|
});
|
|
|
|
interact
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:01:46 +00:00
|
|
|
pub fn slider_f32<S: Into<String>>(
|
|
|
|
&mut self,
|
|
|
|
label: S,
|
|
|
|
value: &mut f32,
|
|
|
|
min: f32,
|
|
|
|
max: f32,
|
|
|
|
) -> InteractInfo {
|
2018-12-27 18:35:02 +00:00
|
|
|
debug_assert!(min <= max);
|
2018-12-26 16:01:46 +00:00
|
|
|
let label: String = label.into();
|
|
|
|
let id = self.get_id(&label);
|
2018-12-27 18:35:02 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
|
|
|
id,
|
|
|
|
Vec2 {
|
2018-12-26 22:08:50 +00:00
|
|
|
x: self.layout_options.width,
|
|
|
|
y: self.layout_options.slider_height,
|
|
|
|
},
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-26 16:01:46 +00:00
|
|
|
|
|
|
|
if interact.active {
|
|
|
|
*value = remap_clamp(self.input.mouse_pos.x, rect.min().x, rect.max().x, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.commands.push(GuiCmd::Slider {
|
|
|
|
interact,
|
|
|
|
label,
|
|
|
|
max,
|
|
|
|
min,
|
|
|
|
rect,
|
|
|
|
value: *value,
|
|
|
|
});
|
|
|
|
|
|
|
|
interact
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:08:43 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Areas:
|
|
|
|
|
2018-12-27 22:26:05 +00:00
|
|
|
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> InteractInfo
|
2018-12-27 18:08:43 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
F: FnOnce(&mut Layout),
|
|
|
|
{
|
2018-12-27 22:26:05 +00:00
|
|
|
let text: String = text.into();
|
|
|
|
let id = self.get_id(&text);
|
|
|
|
let (text, text_size) = self.layout_text(&text);
|
|
|
|
let text_cursor = self.cursor + self.layout_options.button_padding;
|
2018-12-27 18:35:02 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
|
|
|
id,
|
2018-12-27 22:26:05 +00:00
|
|
|
vec2(
|
|
|
|
self.layout_options.width,
|
|
|
|
text_size.y + 2.0 * self.layout_options.button_padding.y,
|
|
|
|
),
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-27 18:08:43 +00:00
|
|
|
|
|
|
|
if interact.clicked {
|
2018-12-27 22:26:05 +00:00
|
|
|
if self.memory.open_foldables.contains(&id) {
|
|
|
|
self.memory.open_foldables.remove(&id);
|
2018-12-27 18:08:43 +00:00
|
|
|
} else {
|
2018-12-27 22:26:05 +00:00
|
|
|
self.memory.open_foldables.insert(id);
|
2018-12-27 18:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-27 22:26:05 +00:00
|
|
|
let open = self.memory.open_foldables.contains(&id);
|
2018-12-27 18:08:43 +00:00
|
|
|
|
|
|
|
self.commands.push(GuiCmd::FoldableHeader {
|
|
|
|
interact,
|
|
|
|
rect,
|
|
|
|
open,
|
|
|
|
});
|
2018-12-27 22:26:05 +00:00
|
|
|
let icon_width = 16.0; // TODO: this offset is ugly
|
|
|
|
self.add_text(text_cursor + vec2(icon_width, 0.0), text);
|
2018-12-27 18:08:43 +00:00
|
|
|
|
|
|
|
if open {
|
2018-12-27 22:26:05 +00:00
|
|
|
let old_id = self.id;
|
|
|
|
self.id = id;
|
2018-12-27 18:08:43 +00:00
|
|
|
let old_x = self.cursor.x;
|
|
|
|
self.cursor.x += self.layout_options.indent;
|
|
|
|
add_contents(self);
|
|
|
|
self.cursor.x = old_x;
|
2018-12-27 22:26:05 +00:00
|
|
|
self.id = old_id;
|
2018-12-27 18:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interact
|
|
|
|
}
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2018-12-26 14:28:38 +00:00
|
|
|
|
2018-12-27 18:35:02 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:01:46 +00:00
|
|
|
fn interactive_rect(&mut self, id: Id, rect: &Rect) -> InteractInfo {
|
|
|
|
let hovered = rect.contains(self.input.mouse_pos);
|
|
|
|
let clicked = hovered && self.input.mouse_clicked;
|
|
|
|
if clicked {
|
2018-12-27 22:26:05 +00:00
|
|
|
self.memory.active_id = Some(id);
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
2018-12-27 22:26:05 +00:00
|
|
|
let active = self.memory.active_id == Some(id);
|
2018-12-26 16:01:46 +00:00
|
|
|
|
|
|
|
InteractInfo {
|
|
|
|
hovered,
|
|
|
|
clicked,
|
|
|
|
active,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-26 14:28:38 +00:00
|
|
|
fn get_id(&self, id_str: &str) -> Id {
|
|
|
|
use std::hash::Hasher;
|
|
|
|
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
2018-12-27 22:26:05 +00:00
|
|
|
hasher.write_u64(self.id);
|
2018-12-26 14:28:38 +00:00
|
|
|
hasher.write(id_str.as_bytes());
|
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
|
2018-12-27 22:26:05 +00:00
|
|
|
fn layout_text(&self, text: &str) -> (TextFragments, Vec2) {
|
|
|
|
let char_size = self.layout_options.char_size;
|
|
|
|
let mut cursor_y = 0.0;
|
|
|
|
let mut max_width = 0.0;
|
|
|
|
let mut text_fragments = Vec::new();
|
|
|
|
for line in text.split('\n') {
|
|
|
|
// TODO: break long lines
|
|
|
|
let line_width = char_size.x * (line.len() as f32);
|
|
|
|
|
|
|
|
text_fragments.push(TextFragment {
|
|
|
|
rect: Rect::from_min_size(vec2(0.0, cursor_y), vec2(line_width, char_size.y)),
|
|
|
|
text: line.into(),
|
|
|
|
});
|
|
|
|
|
|
|
|
cursor_y += char_size.y;
|
|
|
|
max_width = line_width.max(max_width);
|
|
|
|
}
|
|
|
|
let bounding_size = vec2(max_width, cursor_y);
|
|
|
|
(text_fragments, bounding_size)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_text(&mut self, pos: Vec2, text: Vec<TextFragment>) {
|
|
|
|
for fragment in text {
|
|
|
|
self.commands.push(GuiCmd::Text {
|
|
|
|
pos: pos + fragment.rect.pos,
|
|
|
|
style: TextStyle::Label,
|
|
|
|
text: fragment.text,
|
|
|
|
text_align: TextAlign::Start,
|
|
|
|
});
|
|
|
|
}
|
2018-12-26 14:28:38 +00:00
|
|
|
}
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|