egui/src/gui.rs

138 lines
3.5 KiB
Rust
Raw Normal View History

2018-12-26 16:01:46 +00:00
use crate::{math::*, types::*};
2018-12-26 09:46:23 +00:00
// TODO: implement Gui on this so we can add children to a widget
// pub struct Widget {}
2018-12-26 14:28:38 +00:00
type Id = u64;
2018-12-26 09:46:23 +00:00
2018-12-26 14:28:38 +00:00
#[derive(Clone, Copy, Debug, Default)]
pub struct GuiState {
/// The widget being interacted with (e.g. dragged, in case of a slider).
pub active_id: Option<Id>,
2018-12-26 09:46:23 +00:00
}
2018-12-26 14:28:38 +00:00
pub struct Gui {
pub commands: Vec<GuiCmd>,
pub cursor: Vec2,
pub input: GuiInput,
pub state: GuiState,
}
2018-12-26 09:46:23 +00:00
2018-12-26 14:28:38 +00:00
impl Gui {
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-26 09:46:23 +00:00
let rect = Rect {
pos: self.cursor,
size: Vec2 { x: 200.0, y: 32.0 }, // TODO: get from some settings
};
2018-12-26 14:28:38 +00:00
2018-12-26 16:01:46 +00:00
let interact = self.interactive_rect(id, &rect);
2018-12-26 14:28:38 +00:00
self.commands.push(GuiCmd::Rect {
interact,
rect,
style: RectStyle::Button,
});
2018-12-26 13:38:46 +00:00
// TODO: clip-rect of text
2018-12-26 09:46:23 +00:00
self.text(
Vec2 {
2018-12-26 13:38:46 +00:00
x: rect.pos.x + 8.0,
2018-12-26 09:46:23 +00:00
y: rect.center().y + 14.0 / 2.0,
},
2018-12-26 13:38:46 +00:00
TextStyle::Button,
2018-12-26 09:46:23 +00:00
text,
);
self.cursor.y += rect.size.y + 16.0;
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();
for line in text.split('\n') {
2018-12-26 13:38:46 +00:00
self.text(self.cursor, TextStyle::Label, line);
2018-12-26 09:46:23 +00:00
self.cursor.y += 16.0;
}
self.cursor.y += 16.0; // Padding
}
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 {
let label: String = label.into();
let id = self.get_id(&label);
let rect = Rect {
pos: self.cursor,
size: Vec2 { x: 200.0, y: 24.0 }, // TODO: get from some settings
};
let interact = self.interactive_rect(id, &rect);
debug_assert!(min <= max);
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,
});
self.cursor.y += rect.size.y + 16.0;
interact
}
2018-12-26 09:46:23 +00:00
// ------------------------------------------------------------------------
2018-12-26 14:28:38 +00:00
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 {
self.state.active_id = Some(id);
}
let active = self.state.active_id == Some(id);
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();
hasher.write(id_str.as_bytes());
hasher.finish()
}
fn text<S: Into<String>>(&mut self, pos: Vec2, style: TextStyle, text: S) {
self.commands.push(GuiCmd::Text {
pos,
style,
text: text.into(),
text_align: TextAlign::Start,
});
}
2018-12-26 09:46:23 +00:00
}