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-28 22:29:24 +00:00
|
|
|
/// Horizontal and vertical padding within a window frame.
|
|
|
|
pub window_padding: Vec2,
|
|
|
|
|
|
|
|
/// Horizontal and vertical spacing between widgets
|
2018-12-26 22:08:50 +00:00
|
|
|
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
|
|
|
|
2018-12-27 22:55:16 +00:00
|
|
|
/// Checkboxed, radio button and foldables have an icon at the start.
|
|
|
|
/// The text starts after this many pixels.
|
|
|
|
pub start_icon_width: f32,
|
2018-12-26 22:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LayoutOptions {
|
|
|
|
fn default() -> Self {
|
|
|
|
LayoutOptions {
|
2018-12-27 22:26:05 +00:00
|
|
|
char_size: vec2(7.2, 14.0),
|
2018-12-27 22:55:16 +00:00
|
|
|
item_spacing: vec2(8.0, 4.0),
|
2018-12-28 22:29:24 +00:00
|
|
|
window_padding: vec2(6.0, 6.0),
|
2018-12-27 18:08:43 +00:00
|
|
|
indent: 21.0,
|
2018-12-28 09:39:08 +00:00
|
|
|
width: 250.0,
|
2018-12-27 22:55:16 +00:00
|
|
|
button_padding: vec2(5.0, 3.0),
|
|
|
|
start_icon_width: 20.0,
|
2018-12-26 22:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
// TODO: rename
|
|
|
|
pub struct GuiResponse<'a> {
|
|
|
|
/// The mouse is hovering above this
|
|
|
|
pub hovered: bool,
|
|
|
|
|
|
|
|
/// The mouse went got pressed on this thing this frame
|
|
|
|
pub clicked: bool,
|
|
|
|
|
|
|
|
/// The mouse is interacting with this thing (e.g. dragging it)
|
|
|
|
pub active: bool,
|
|
|
|
|
|
|
|
layout: &'a mut Layout,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> GuiResponse<'a> {
|
|
|
|
/// Show some stuff if the item was hovered
|
|
|
|
pub fn tooltip<F>(self, add_contents: F) -> Self
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Layout),
|
|
|
|
{
|
|
|
|
if self.hovered {
|
|
|
|
let window_pos = self.layout.input.mouse_pos + vec2(16.0, 16.0);
|
|
|
|
self.layout.show_popup(window_pos, add_contents);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show this text if the item was hovered
|
|
|
|
pub fn tooltip_text<S: Into<String>>(self, text: S) -> Self {
|
|
|
|
self.tooltip(|popup| {
|
|
|
|
popup.label(text);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-27 18:08:43 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2018-12-28 09:39:08 +00:00
|
|
|
struct Memory {
|
2018-12-26 14:28:38 +00:00
|
|
|
/// The widget being interacted with (e.g. dragged, in case of a slider).
|
2018-12-28 09:39:08 +00:00
|
|
|
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-28 09:39:08 +00:00
|
|
|
#[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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2018-12-26 22:08:50 +00:00
|
|
|
type Id = u64;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Layout {
|
2018-12-28 22:29:24 +00:00
|
|
|
options: LayoutOptions,
|
2018-12-28 09:39:08 +00:00
|
|
|
input: GuiInput,
|
|
|
|
memory: Memory,
|
2018-12-27 22:26:05 +00:00
|
|
|
id: Id,
|
2018-12-28 09:39:08 +00:00
|
|
|
layouter: Layouter,
|
2018-12-28 22:29:24 +00:00
|
|
|
graphics: Vec<GuiCmd>,
|
|
|
|
hovering_graphics: 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-28 22:29:24 +00:00
|
|
|
pub fn gui_commands(&self) -> impl Iterator<Item = &GuiCmd> {
|
|
|
|
self.graphics.iter().chain(self.hovering_graphics.iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn options(&self) -> &LayoutOptions {
|
|
|
|
&self.options
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_options(&mut self, options: LayoutOptions) {
|
|
|
|
self.options = options;
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 09:39:08 +00:00
|
|
|
// TODO: move
|
2018-12-28 22:29:24 +00:00
|
|
|
pub fn new_frame(&mut self, gui_input: GuiInput) {
|
|
|
|
self.graphics.clear();
|
|
|
|
self.hovering_graphics.clear();
|
2018-12-28 09:39:08 +00:00
|
|
|
self.layouter = Default::default();
|
|
|
|
self.input = gui_input;
|
|
|
|
if !gui_input.mouse_down {
|
|
|
|
self.memory.active_id = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
pub fn button<S: Into<String>>(&mut self, text: S) -> GuiResponse {
|
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);
|
2018-12-28 09:39:08 +00:00
|
|
|
let text_cursor = self.layouter.cursor + self.options.button_padding;
|
2018-12-27 22:26:05 +00:00
|
|
|
let (rect, interact) =
|
2018-12-28 22:53:15 +00:00
|
|
|
self.reserve_space(text_size + 2.0 * self.options.button_padding, Some(id));
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::Button { interact, rect });
|
2018-12-27 22:26:05 +00:00
|
|
|
self.add_text(text_cursor, text);
|
2018-12-28 22:53:15 +00:00
|
|
|
self.response(interact)
|
2018-12-26 21:17:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
pub fn checkbox<S: Into<String>>(&mut self, text: S, checked: &mut bool) -> GuiResponse {
|
2018-12-27 22:55:16 +00:00
|
|
|
let text: String = text.into();
|
|
|
|
let id = self.get_id(&text);
|
|
|
|
let (text, text_size) = self.layout_text(&text);
|
2018-12-28 09:39:08 +00:00
|
|
|
let text_cursor = self.layouter.cursor
|
|
|
|
+ self.options.button_padding
|
|
|
|
+ vec2(self.options.start_icon_width, 0.0);
|
2018-12-28 22:53:15 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
2018-12-27 22:55:16 +00:00
|
|
|
self.options.button_padding
|
|
|
|
+ vec2(self.options.start_icon_width, 0.0)
|
|
|
|
+ text_size
|
|
|
|
+ self.options.button_padding,
|
2018-12-28 22:53:15 +00:00
|
|
|
Some(id),
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-26 21:17:33 +00:00
|
|
|
if interact.clicked {
|
|
|
|
*checked = !*checked;
|
|
|
|
}
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::Checkbox {
|
2018-12-26 21:17:33 +00:00
|
|
|
checked: *checked,
|
|
|
|
interact,
|
|
|
|
rect,
|
2018-12-26 14:28:38 +00:00
|
|
|
});
|
2018-12-27 22:55:16 +00:00
|
|
|
self.add_text(text_cursor, text);
|
2018-12-28 22:53:15 +00:00
|
|
|
self.response(interact)
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
pub fn label<S: Into<String>>(&mut self, text: S) -> GuiResponse {
|
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);
|
2018-12-28 09:39:08 +00:00
|
|
|
self.add_text(self.layouter.cursor, text);
|
2018-12-28 22:53:15 +00:00
|
|
|
let (_, interact) = self.reserve_space(text_size, None);
|
|
|
|
self.response(interact)
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 21:26:15 +00:00
|
|
|
/// A radio button
|
2018-12-28 22:53:15 +00:00
|
|
|
pub fn radio<S: Into<String>>(&mut self, text: S, checked: bool) -> GuiResponse {
|
2018-12-27 22:55:16 +00:00
|
|
|
let text: String = text.into();
|
|
|
|
let id = self.get_id(&text);
|
|
|
|
let (text, text_size) = self.layout_text(&text);
|
2018-12-28 09:39:08 +00:00
|
|
|
let text_cursor = self.layouter.cursor
|
|
|
|
+ self.options.button_padding
|
|
|
|
+ vec2(self.options.start_icon_width, 0.0);
|
2018-12-28 22:53:15 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
2018-12-27 22:55:16 +00:00
|
|
|
self.options.button_padding
|
|
|
|
+ vec2(self.options.start_icon_width, 0.0)
|
|
|
|
+ text_size
|
|
|
|
+ self.options.button_padding,
|
2018-12-28 22:53:15 +00:00
|
|
|
Some(id),
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::RadioButton {
|
2018-12-26 21:26:15 +00:00
|
|
|
checked,
|
|
|
|
interact,
|
|
|
|
rect,
|
|
|
|
});
|
2018-12-27 22:55:16 +00:00
|
|
|
self.add_text(text_cursor, text);
|
2018-12-28 22:53:15 +00:00
|
|
|
self.response(interact)
|
2018-12-26 21:26:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 16:01:46 +00:00
|
|
|
pub fn slider_f32<S: Into<String>>(
|
|
|
|
&mut self,
|
2018-12-27 22:55:16 +00:00
|
|
|
text: S,
|
2018-12-26 16:01:46 +00:00
|
|
|
value: &mut f32,
|
|
|
|
min: f32,
|
|
|
|
max: f32,
|
2018-12-28 22:53:15 +00:00
|
|
|
) -> GuiResponse {
|
2018-12-27 18:35:02 +00:00
|
|
|
debug_assert!(min <= max);
|
2018-12-27 22:55:16 +00:00
|
|
|
let text: String = text.into();
|
|
|
|
let id = self.get_id(&text);
|
|
|
|
let (text, text_size) = self.layout_text(&format!("{}: {:.3}", text, value));
|
2018-12-28 09:39:08 +00:00
|
|
|
self.add_text(self.layouter.cursor, text);
|
|
|
|
self.layouter.reserve_space(text_size);
|
2018-12-28 22:53:15 +00:00
|
|
|
let (slider_rect, interact) = self.reserve_space(
|
2018-12-27 18:35:02 +00:00
|
|
|
Vec2 {
|
2018-12-27 22:55:16 +00:00
|
|
|
x: self.options.width,
|
|
|
|
y: self.options.char_size.y,
|
2018-12-26 22:08:50 +00:00
|
|
|
},
|
2018-12-28 22:53:15 +00:00
|
|
|
Some(id),
|
2018-12-27 18:35:02 +00:00
|
|
|
);
|
2018-12-26 16:01:46 +00:00
|
|
|
|
|
|
|
if interact.active {
|
2018-12-27 22:55:16 +00:00
|
|
|
*value = remap_clamp(
|
|
|
|
self.input.mouse_pos.x,
|
|
|
|
slider_rect.min().x,
|
|
|
|
slider_rect.max().x,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
);
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::Slider {
|
2018-12-26 16:01:46 +00:00
|
|
|
interact,
|
|
|
|
max,
|
|
|
|
min,
|
2018-12-27 22:55:16 +00:00
|
|
|
rect: slider_rect,
|
2018-12-26 16:01:46 +00:00
|
|
|
value: *value,
|
|
|
|
});
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
self.response(interact)
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 18:08:43 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Areas:
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
|
2018-12-27 18:08:43 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
F: FnOnce(&mut Layout),
|
|
|
|
{
|
2018-12-28 09:39:08 +00:00
|
|
|
assert!(
|
|
|
|
self.layouter.dir == Direction::Vertical,
|
|
|
|
"Horizontal foldable is unimplemented"
|
|
|
|
);
|
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);
|
2018-12-28 09:39:08 +00:00
|
|
|
let text_cursor = self.layouter.cursor + self.options.button_padding;
|
2018-12-28 22:53:15 +00:00
|
|
|
let (rect, interact) = self.reserve_space(
|
2018-12-27 22:26:05 +00:00
|
|
|
vec2(
|
2018-12-27 22:55:16 +00:00
|
|
|
self.options.width,
|
|
|
|
text_size.y + 2.0 * self.options.button_padding.y,
|
2018-12-27 22:26:05 +00:00
|
|
|
),
|
2018-12-28 22:53:15 +00:00
|
|
|
Some(id),
|
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
|
|
|
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::FoldableHeader {
|
2018-12-27 18:08:43 +00:00
|
|
|
interact,
|
|
|
|
rect,
|
|
|
|
open,
|
|
|
|
});
|
2018-12-27 22:55:16 +00:00
|
|
|
self.add_text(text_cursor + vec2(self.options.start_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-28 09:39:08 +00:00
|
|
|
let old_x = self.layouter.cursor.x;
|
|
|
|
self.layouter.cursor.x += self.options.indent;
|
2018-12-27 18:08:43 +00:00
|
|
|
add_contents(self);
|
2018-12-28 09:39:08 +00:00
|
|
|
self.layouter.cursor.x = old_x;
|
2018-12-27 22:26:05 +00:00
|
|
|
self.id = old_id;
|
2018-12-27 18:08:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
self.response(interact)
|
2018-12-27 18:08:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 09:39:08 +00:00
|
|
|
/// Start a region with horizontal layout
|
|
|
|
pub fn horizontal<F>(&mut self, add_contents: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Layout),
|
|
|
|
{
|
|
|
|
let horizontal_layouter = Layouter {
|
|
|
|
dir: Direction::Horizontal,
|
|
|
|
cursor: self.layouter.cursor,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let old_layouter = std::mem::replace(&mut self.layouter, horizontal_layouter);
|
|
|
|
add_contents(self);
|
|
|
|
let horizontal_layouter = std::mem::replace(&mut self.layouter, old_layouter);
|
|
|
|
self.layouter.reserve_space(horizontal_layouter.size);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:29:24 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Free painting. It is up to the caller to make sure there is room for these.
|
|
|
|
pub fn add_paint_command(&mut self, cmd: GuiCmd) {
|
|
|
|
self.graphics.push(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
/// Show a pop-over window
|
|
|
|
pub fn show_popup<F>(&mut self, window_pos: Vec2, add_contents: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Layout),
|
|
|
|
{
|
2018-12-28 22:29:24 +00:00
|
|
|
// TODO: less copying
|
|
|
|
let mut popup_layout = Layout {
|
|
|
|
options: self.options,
|
|
|
|
input: self.input,
|
|
|
|
memory: self.memory.clone(), // TODO: Arc
|
|
|
|
id: self.id,
|
|
|
|
layouter: Default::default(),
|
|
|
|
graphics: vec![],
|
|
|
|
hovering_graphics: vec![],
|
|
|
|
};
|
|
|
|
popup_layout.layouter.cursor = window_pos + self.options.window_padding;
|
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
add_contents(&mut popup_layout);
|
2018-12-28 22:29:24 +00:00
|
|
|
|
|
|
|
// TODO: handle the last item_spacing in a nicer way
|
|
|
|
let inner_size = popup_layout.layouter.size - self.options.item_spacing;
|
|
|
|
let outer_size = inner_size + 2.0 * self.options.window_padding;
|
|
|
|
|
|
|
|
let rect = Rect::from_min_size(window_pos, outer_size);
|
|
|
|
self.hovering_graphics.push(GuiCmd::Window { rect });
|
|
|
|
self.hovering_graphics
|
|
|
|
.extend(popup_layout.gui_commands().cloned());
|
|
|
|
}
|
|
|
|
|
2018-12-26 09:46:23 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2018-12-26 14:28:38 +00:00
|
|
|
|
2018-12-28 22:53:15 +00:00
|
|
|
fn reserve_space(&mut self, size: Vec2, interaction_id: Option<Id>) -> (Rect, InteractInfo) {
|
2018-12-27 18:35:02 +00:00
|
|
|
let rect = Rect {
|
2018-12-28 09:39:08 +00:00
|
|
|
pos: self.layouter.cursor,
|
2018-12-27 18:35:02 +00:00
|
|
|
size,
|
|
|
|
};
|
2018-12-28 09:39:08 +00:00
|
|
|
self.layouter
|
|
|
|
.reserve_space(size + self.options.item_spacing);
|
2018-12-26 16:01:46 +00:00
|
|
|
let hovered = rect.contains(self.input.mouse_pos);
|
|
|
|
let clicked = hovered && self.input.mouse_clicked;
|
2018-12-28 22:53:15 +00:00
|
|
|
let active = if interaction_id.is_some() {
|
|
|
|
if clicked {
|
|
|
|
self.memory.active_id = interaction_id;
|
|
|
|
}
|
|
|
|
self.memory.active_id == interaction_id
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2018-12-26 16:01:46 +00:00
|
|
|
|
2018-12-28 09:39:08 +00:00
|
|
|
let interact = InteractInfo {
|
2018-12-26 16:01:46 +00:00
|
|
|
hovered,
|
|
|
|
clicked,
|
|
|
|
active,
|
2018-12-28 09:39:08 +00:00
|
|
|
};
|
|
|
|
(rect, interact)
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-12-27 22:55:16 +00:00
|
|
|
let char_size = self.options.char_size;
|
2018-12-27 22:26:05 +00:00
|
|
|
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 {
|
2018-12-28 22:29:24 +00:00
|
|
|
self.graphics.push(GuiCmd::Text {
|
2018-12-27 22:55:16 +00:00
|
|
|
pos: pos + vec2(fragment.rect.pos.x, fragment.rect.center().y),
|
2018-12-27 22:26:05 +00:00
|
|
|
style: TextStyle::Label,
|
|
|
|
text: fragment.text,
|
|
|
|
});
|
|
|
|
}
|
2018-12-26 14:28:38 +00:00
|
|
|
}
|
2018-12-28 22:53:15 +00:00
|
|
|
|
|
|
|
fn response(&mut self, interact: InteractInfo) -> GuiResponse {
|
|
|
|
GuiResponse {
|
|
|
|
hovered: interact.hovered,
|
|
|
|
clicked: interact.clicked,
|
|
|
|
active: interact.active,
|
|
|
|
layout: self,
|
|
|
|
}
|
|
|
|
}
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|