egui/src/gui.rs

87 lines
2.2 KiB
Rust
Raw Normal View History

2018-12-26 13:38:46 +00:00
use crate::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 {}
pub struct Gui {
2018-12-26 13:38:46 +00:00
commands: Vec<GuiCmd>,
2018-12-26 09:46:23 +00:00
input: GuiInput,
cursor: Vec2,
}
impl Gui {
pub fn new(input: GuiInput) -> Self {
Gui {
2018-12-26 13:38:46 +00:00
commands: vec![],
2018-12-26 09:46:23 +00:00
input,
cursor: Vec2 { x: 32.0, y: 32.0 },
}
}
pub fn input(&self) -> &GuiInput {
&self.input
}
2018-12-26 13:38:46 +00:00
pub fn into_commands(self) -> Vec<GuiCmd> {
2018-12-26 09:46:23 +00:00
self.commands
}
2018-12-26 13:38:46 +00:00
pub fn paint_commands(&self) -> &[GuiCmd] {
2018-12-26 09:46:23 +00:00
&self.commands
}
2018-12-26 13:38:46 +00:00
fn rect(&mut self, rect: Rect, style: RectStyle) -> InteractInfo {
let hovered = rect.contains(self.input.mouse_pos);
let clicked = hovered && self.input.mouse_clicked;
let interact = InteractInfo { hovered, clicked };
self.commands.push(GuiCmd::Rect {
interact,
rect,
style,
2018-12-26 09:46:23 +00:00
});
2018-12-26 13:38:46 +00:00
interact
2018-12-26 09:46:23 +00:00
}
2018-12-26 13:38:46 +00:00
fn text<S: Into<String>>(&mut self, pos: Vec2, style: TextStyle, text: S) {
self.commands.push(GuiCmd::Text {
2018-12-26 09:46:23 +00:00
pos,
2018-12-26 13:38:46 +00:00
style,
2018-12-26 09:46:23 +00:00
text: text.into(),
text_align: TextAlign::Start,
});
}
// ------------------------------------------------------------------------
pub fn button<S: Into<String>>(&mut self, text: S) -> InteractInfo {
let rect = Rect {
pos: self.cursor,
size: Vec2 { x: 200.0, y: 32.0 }, // TODO: get from some settings
};
2018-12-26 13:38:46 +00:00
let interact = self.rect(rect, RectStyle::Button);
// 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) {
for line in text.into().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
}
// ------------------------------------------------------------------------
}