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,
|
2020-04-18 23:05:49 +00:00
|
|
|
math::{Pos2, Rect, Vec2},
|
2020-04-19 21:34:34 +00:00
|
|
|
mesher::{Mesh, Path},
|
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.
|
2020-04-22 18:01:49 +00:00
|
|
|
/// All coordinates in emigui is in point/logical coordinates.
|
2018-12-26 09:46:23 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Deserialize)]
|
2020-04-22 18:01:49 +00:00
|
|
|
#[serde(default)]
|
2018-12-26 09:46:23 +00:00
|
|
|
pub struct RawInput {
|
|
|
|
/// Is the button currently down?
|
|
|
|
pub mouse_down: bool,
|
|
|
|
|
|
|
|
/// Current position of the mouse in points.
|
2020-04-18 23:05:49 +00:00
|
|
|
pub mouse_pos: Option<Pos2>,
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
/// How many pixels the user scrolled
|
|
|
|
pub scroll_delta: Vec2,
|
|
|
|
|
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,
|
2020-04-21 12:46:42 +00:00
|
|
|
|
|
|
|
/// Time in seconds. Relative to whatever. Used for animation.
|
|
|
|
pub time: f64,
|
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?
|
2020-04-21 18:43:47 +00:00
|
|
|
/// true the frame when it is pressed,
|
|
|
|
/// false the frame it is released.
|
2018-12-26 09:46:23 +00:00
|
|
|
pub mouse_down: bool,
|
|
|
|
|
|
|
|
/// The mouse went from !down to down
|
2020-04-21 18:43:47 +00:00
|
|
|
pub mouse_pressed: bool,
|
2018-12-26 09:46:23 +00:00
|
|
|
|
|
|
|
/// The mouse went from down to !down
|
|
|
|
pub mouse_released: bool,
|
|
|
|
|
|
|
|
/// Current position of the mouse in points.
|
2020-04-18 23:05:49 +00:00
|
|
|
pub mouse_pos: Option<Pos2>,
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2020-04-17 12:26:36 +00:00
|
|
|
/// How much the mouse moved compared to last frame, in points.
|
|
|
|
pub mouse_move: Vec2,
|
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
/// How many pixels the user scrolled
|
|
|
|
pub scroll_delta: 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,
|
2020-04-21 12:46:42 +00:00
|
|
|
|
|
|
|
/// Time in seconds. Relative to whatever. Used for animation.
|
|
|
|
pub time: f64,
|
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 {
|
2020-04-17 12:26:36 +00:00
|
|
|
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,
|
2020-04-21 18:43:47 +00:00
|
|
|
mouse_pressed: !last.mouse_down && new.mouse_down,
|
2018-12-26 09:46:23 +00:00
|
|
|
mouse_released: last.mouse_down && !new.mouse_down,
|
|
|
|
mouse_pos: new.mouse_pos,
|
2020-04-17 12:26:36 +00:00
|
|
|
mouse_move,
|
2020-04-22 18:01:49 +00:00
|
|
|
scroll_delta: new.scroll_delta,
|
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,
|
2020-04-21 12:46:42 +00:00
|
|
|
time: new.time,
|
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 {
|
2020-04-21 18:43:47 +00:00
|
|
|
/// The mouse is hovering above this thing
|
2018-12-26 13:38:46 +00:00
|
|
|
pub hovered: bool,
|
2018-12-26 14:28:38 +00:00
|
|
|
|
2020-04-21 18:43:47 +00:00
|
|
|
/// The mouse pressed this thing ealier, and now released on this thing too.
|
2018-12-26 13:38:46 +00:00
|
|
|
pub clicked: bool,
|
2018-12-26 14:28:38 +00:00
|
|
|
|
2020-04-21 18:43:47 +00:00
|
|
|
/// The mouse is interacting with this thing (e.g. dragging it or holding it)
|
2018-12-26 14:28:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-19 09:11:41 +00:00
|
|
|
impl Outline {
|
|
|
|
pub fn new(width: impl Into<f32>, color: impl Into<Color>) -> Self {
|
|
|
|
Self {
|
|
|
|
width: width.into(),
|
|
|
|
color: color.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:34:34 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2018-12-23 19:06:40 +00:00
|
|
|
pub enum PaintCmd {
|
2018-12-26 21:26:15 +00:00
|
|
|
Circle {
|
2020-04-18 23:05:49 +00:00
|
|
|
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 {
|
2020-04-18 23:05:49 +00:00
|
|
|
points: Vec<Pos2>,
|
2018-12-27 16:47:32 +00:00
|
|
|
color: Color,
|
2018-12-26 21:17:33 +00:00
|
|
|
width: f32,
|
|
|
|
},
|
2020-04-19 21:34:34 +00:00
|
|
|
Path {
|
|
|
|
path: Path,
|
|
|
|
closed: bool,
|
|
|
|
fill_color: Option<Color>,
|
|
|
|
outline: Option<Outline>,
|
|
|
|
},
|
2018-12-26 16:32:58 +00:00
|
|
|
Rect {
|
2020-04-20 22:17:02 +00:00
|
|
|
rect: Rect,
|
2018-12-26 16:32:58 +00:00
|
|
|
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>,
|
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.
|
2020-04-18 23:05:49 +00:00
|
|
|
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
|
|
|
}
|