egui/src/types.rs

168 lines
3.9 KiB
Rust
Raw Normal View History

2018-12-26 09:46:23 +00:00
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
2018-12-23 19:06:40 +00:00
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
2018-12-26 09:46:23 +00:00
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
2018-12-23 19:06:40 +00:00
pub struct Rect {
pub pos: Vec2,
pub size: Vec2,
}
impl Rect {
2018-12-26 09:46:23 +00:00
pub fn contains(&self, p: Vec2) -> bool {
2018-12-23 19:06:40 +00:00
self.pos.x <= p.x
&& p.x <= self.pos.x + self.size.x
&& self.pos.y <= p.y
&& p.y <= self.pos.y + self.size.y
}
2018-12-26 09:46:23 +00:00
pub fn center(&self) -> Vec2 {
Vec2 {
x: self.pos.x + self.size.x / 2.0,
y: self.pos.y + self.size.y / 2.0,
}
}
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
#[derive(Clone, Copy, Debug)]
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 14:28:38 +00:00
/// Names taken from Dear ImGui
#[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct LayoutOptions {
// Horizontal and vertical spacing between widgets
item_spacing: Vec2,
/// Padding within a framed rectangle (used by most widgets)
frame_padding: Vec2,
}
impl LayoutOptions {
pub fn new() -> Self {
// Values taken from Dear ImGui
LayoutOptions {
item_spacing: Vec2 { x: 8.0, y: 4.0 },
frame_padding: Vec2 { x: 4.0, y: 3.0 },
}
}
}
// ----------------------------------------------------------------------------
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 RectStyle {
Button,
}
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TextStyle {
Button,
Label,
}
#[derive(Clone, Debug, Serialize)]
pub enum GuiCmd {
Rect {
rect: Rect,
style: RectStyle,
interact: InteractInfo,
},
Text {
pos: Vec2,
text: String,
text_align: TextAlign,
style: TextStyle,
},
}
// ----------------------------------------------------------------------------
#[derive(Clone, Debug, Serialize)] // TODO: copy
2018-12-23 19:06:40 +00:00
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum PaintCmd {
Clear {
fill_style: String,
},
RoundedRect {
fill_style: String,
pos: Vec2,
size: Vec2,
corner_radius: f32,
},
Text {
fill_style: String,
font: String,
pos: Vec2,
text: String,
text_align: TextAlign,
},
}