1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 23:13:13 -04:00

persitent interaction with buttons

This commit is contained in:
Emil Ernerfeldt
2018-12-26 15:28:38 +01:00
parent 094f8216c5
commit 7c3aa61c02
6 changed files with 105 additions and 53 deletions

View File

@@ -3,63 +3,58 @@ use crate::types::*;
// TODO: implement Gui on this so we can add children to a widget
// pub struct Widget {}
pub struct Gui {
commands: Vec<GuiCmd>,
input: GuiInput,
type Id = u64;
cursor: Vec2,
#[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>,
}
pub struct Gui {
pub commands: Vec<GuiCmd>,
pub cursor: Vec2,
pub input: GuiInput,
pub state: GuiState,
}
impl Gui {
pub fn new(input: GuiInput) -> Self {
Gui {
commands: vec![],
input,
cursor: Vec2 { x: 32.0, y: 32.0 },
}
}
pub fn input(&self) -> &GuiInput {
&self.input
}
pub fn into_commands(self) -> Vec<GuiCmd> {
self.commands
}
pub fn paint_commands(&self) -> &[GuiCmd] {
pub fn gui_commands(&self) -> &[GuiCmd] {
&self.commands
}
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,
});
interact
}
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,
});
}
// ------------------------------------------------------------------------
pub fn button<S: Into<String>>(&mut self, text: S) -> InteractInfo {
let text: String = text.into();
let id = self.get_id(&text);
let rect = Rect {
pos: self.cursor,
size: Vec2 { x: 200.0, y: 32.0 }, // TODO: get from some settings
};
let interact = self.rect(rect, RectStyle::Button);
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);
let interact = InteractInfo {
hovered,
clicked,
active,
};
self.commands.push(GuiCmd::Rect {
interact,
rect,
style: RectStyle::Button,
});
// TODO: clip-rect of text
self.text(
@@ -75,7 +70,8 @@ impl Gui {
}
pub fn label<S: Into<String>>(&mut self, text: S) {
for line in text.into().split("\n") {
let text: String = text.into();
for line in text.split('\n') {
self.text(self.cursor, TextStyle::Label, line);
self.cursor.y += 16.0;
}
@@ -83,4 +79,20 @@ impl Gui {
}
// ------------------------------------------------------------------------
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,
});
}
}