egui/emigui/src/types.rs

128 lines
3.3 KiB
Rust
Raw Normal View History

2019-01-12 23:55:56 +00:00
use crate::{
2019-04-25 16:07:36 +00:00
color::Color,
2019-01-12 23:55:56 +00:00
fonts::TextStyle,
math::{Pos2, Rect, Vec2},
2019-03-10 20:00:28 +00:00
mesher::Mesh,
2019-01-12 23:55:56 +00:00
};
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: Option<Pos2>,
2018-12-26 09:46:23 +00:00
/// Size of the screen in points.
2018-12-23 19:06:40 +00:00
pub screen_size: Vec2,
2019-01-19 16:10:28 +00:00
/// Also known as device pixel ratio, > 1 for HDPI screens.
pub pixels_per_point: f32,
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.
pub mouse_pos: Option<Pos2>,
2018-12-26 09:46:23 +00:00
/// How much the mouse moved compared to last frame, in points.
pub mouse_move: Vec2,
2018-12-26 09:46:23 +00:00
/// Size of the screen in points.
pub screen_size: Vec2,
2019-01-19 16:10:28 +00:00
/// Also known as device pixel ratio, > 1 for HDPI screens.
pub pixels_per_point: f32,
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 {
let mouse_move = new
.mouse_pos
.and_then(|new| last.mouse_pos.map(|last| new - last))
.unwrap_or_default();
2018-12-26 09:46:23 +00:00
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,
mouse_move,
2018-12-26 09:46:23 +00:00
screen_size: new.screen_size,
2019-01-19 16:10:28 +00:00
pixels_per_point: new.pixels_per_point,
2018-12-26 09:46:23 +00:00
}
}
}
// ----------------------------------------------------------------------------
2018-12-26 13:38:46 +00:00
#[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct InteractInfo {
2018-12-28 22:53:15 +00:00
/// The mouse is hovering above this
2018-12-26 13:38:46 +00:00
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,
2019-01-14 13:26:02 +00:00
/// The region of the screen we are talking about
pub rect: Rect,
2018-12-26 13:38:46 +00:00
}
// ----------------------------------------------------------------------------
2018-12-26 16:32:58 +00:00
#[derive(Clone, Debug, Serialize)]
pub struct Outline {
pub width: f32,
2018-12-27 16:47:32 +00:00
pub color: Color,
2018-12-26 16:32:58 +00:00
}
2019-11-02 08:50:49 +00:00
#[derive(Clone, Debug, Serialize)]
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: Pos2,
2018-12-27 16:47:32 +00:00
fill_color: Option<Color>,
2018-12-26 21:26:15 +00:00
outline: Option<Outline>,
radius: f32,
},
2018-12-26 21:17:33 +00:00
Line {
points: Vec<Pos2>,
2018-12-27 16:47:32 +00:00
color: Color,
2018-12-26 21:17:33 +00:00
width: f32,
},
2018-12-26 16:32:58 +00:00
Rect {
corner_radius: f32,
2018-12-27 16:47:32 +00:00
fill_color: Option<Color>,
2018-12-26 16:32:58 +00:00
outline: Option<Outline>,
2019-01-14 13:26:02 +00:00
rect: Rect,
2018-12-23 19:06:40 +00:00
},
2019-01-05 14:28:07 +00:00
/// Paint a single line of text
2018-12-23 19:06:40 +00:00
Text {
2019-01-05 14:28:07 +00:00
color: Color,
/// Top left corner of the first character.
pos: Pos2,
2018-12-23 19:06:40 +00:00
text: String,
2019-01-12 23:55:56 +00:00
text_style: TextStyle,
2019-01-05 14:28:07 +00:00
/// Start each character in the text, as offset from pos.
x_offsets: Vec<f32>,
// TODO: font info
2018-12-23 19:06:40 +00:00
},
2019-03-11 14:30:32 +00:00
/// Low-level triangle mesh
Mesh(Mesh),
2018-12-23 19:06:40 +00:00
}