egui/src/types.rs

154 lines
3.4 KiB
Rust
Raw Normal View History

2018-12-26 16:01:46 +00:00
use crate::math::{Rect, Vec2};
2018-12-23 19:06:40 +00:00
2018-12-26 09:46:23 +00:00
// ----------------------------------------------------------------------------
/// What the integration gives to the gui.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
pub struct RawInput {
/// Is the button currently down?
pub mouse_down: bool,
/// Current position of the mouse in points.
pub mouse_pos: Vec2,
/// Size of the screen in points.
2018-12-23 19:06:40 +00:00
pub screen_size: Vec2,
2018-12-26 09:46:23 +00:00
}
/// What the gui maintains
2018-12-26 22:08:50 +00:00
#[derive(Clone, Copy, Debug, Default)]
2018-12-26 09:46:23 +00:00
pub struct GuiInput {
/// Is the button currently down?
pub mouse_down: bool,
/// The mouse went from !down to down
pub mouse_clicked: bool,
/// The mouse went from down to !down
pub mouse_released: bool,
/// Current position of the mouse in points.
2018-12-23 19:06:40 +00:00
pub mouse_pos: Vec2,
2018-12-26 09:46:23 +00:00
/// Size of the screen in points.
pub screen_size: Vec2,
2018-12-23 19:06:40 +00:00
}
2018-12-26 09:46:23 +00:00
impl GuiInput {
pub fn from_last_and_new(last: &RawInput, new: &RawInput) -> GuiInput {
GuiInput {
mouse_down: new.mouse_down,
mouse_clicked: !last.mouse_down && new.mouse_down,
mouse_released: last.mouse_down && !new.mouse_down,
mouse_pos: new.mouse_pos,
screen_size: new.screen_size,
}
}
}
// ----------------------------------------------------------------------------
2018-12-26 13:38:46 +00:00
#[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct InteractInfo {
pub hovered: bool,
2018-12-26 14:28:38 +00:00
/// The mouse went got pressed on this thing this frame
2018-12-26 13:38:46 +00:00
pub clicked: bool,
2018-12-26 14:28:38 +00:00
/// The mouse is interacting with this thing (e.g. dragging it)
pub active: bool,
2018-12-26 13:38:46 +00:00
}
2018-12-23 23:15:18 +00:00
#[derive(Clone, Copy, Debug, Serialize)]
2018-12-23 19:06:40 +00:00
#[serde(rename_all = "snake_case")]
pub enum TextAlign {
2018-12-23 23:15:18 +00:00
Start, // Test with arabic text
2018-12-23 19:06:40 +00:00
Center,
End,
}
2018-12-26 13:38:46 +00:00
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TextStyle {
Label,
}
#[derive(Clone, Debug, Serialize)]
pub enum GuiCmd {
2018-12-26 16:32:58 +00:00
PaintCommands(Vec<PaintCmd>),
2018-12-26 21:17:33 +00:00
Button {
interact: InteractInfo,
2018-12-26 13:38:46 +00:00
rect: Rect,
2018-12-26 21:17:33 +00:00
text: String,
},
Checkbox {
checked: bool,
2018-12-26 13:38:46 +00:00
interact: InteractInfo,
2018-12-26 21:17:33 +00:00
rect: Rect,
text: String,
2018-12-26 13:38:46 +00:00
},
2018-12-26 21:26:15 +00:00
RadioButton {
checked: bool,
interact: InteractInfo,
rect: Rect,
text: String,
},
2018-12-26 16:01:46 +00:00
Slider {
interact: InteractInfo,
label: String,
max: f32,
min: f32,
rect: Rect,
value: f32,
},
2018-12-26 16:32:58 +00:00
Text {
pos: Vec2,
style: TextStyle,
text: String,
text_align: TextAlign,
},
2018-12-26 13:38:46 +00:00
}
// ----------------------------------------------------------------------------
2018-12-26 16:32:58 +00:00
pub type Style = String;
#[derive(Clone, Debug, Serialize)]
pub struct Outline {
pub width: f32,
pub style: Style,
}
2018-12-26 13:38:46 +00:00
#[derive(Clone, Debug, Serialize)] // TODO: copy
2018-12-23 19:06:40 +00:00
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum PaintCmd {
2018-12-26 21:26:15 +00:00
Circle {
center: Vec2,
fill_style: Option<Style>,
outline: Option<Outline>,
radius: f32,
},
2018-12-23 19:06:40 +00:00
Clear {
2018-12-26 16:32:58 +00:00
fill_style: Style,
2018-12-23 19:06:40 +00:00
},
2018-12-26 21:17:33 +00:00
Line {
points: Vec<Vec2>,
style: Style,
width: f32,
},
2018-12-26 16:32:58 +00:00
Rect {
corner_radius: f32,
fill_style: Option<Style>,
outline: Option<Outline>,
2018-12-23 19:06:40 +00:00
pos: Vec2,
size: Vec2,
},
Text {
2018-12-26 16:32:58 +00:00
fill_style: Style,
2018-12-23 19:06:40 +00:00
font: String,
pos: Vec2,
text: String,
text_align: TextAlign,
},
}