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.
|
2020-04-28 21:05:22 +00:00
|
|
|
#[derive(Clone, 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,
|
2020-04-28 21:05:22 +00:00
|
|
|
|
|
|
|
/// Files has been dropped into the window.
|
|
|
|
pub dropped_files: Vec<std::path::PathBuf>,
|
|
|
|
|
|
|
|
/// Someone is threatening to drop these on us.
|
|
|
|
pub hovered_files: Vec<std::path::PathBuf>,
|
2020-04-29 19:25:49 +00:00
|
|
|
|
|
|
|
/// In-order events received this frame
|
|
|
|
pub events: Vec<Event>,
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 19:25:49 +00:00
|
|
|
/// What emigui maintains
|
2020-04-28 21:05:22 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2018-12-26 09:46:23 +00:00
|
|
|
pub struct GuiInput {
|
2020-04-29 19:58:41 +00:00
|
|
|
// TODO: mouse: Mouse as separate
|
|
|
|
//
|
2018-12-26 09:46:23 +00:00
|
|
|
/// 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-23 20:08:42 +00:00
|
|
|
/// None for touch screens when finger is not down.
|
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,
|
2020-04-28 21:05:22 +00:00
|
|
|
|
|
|
|
/// Files has been dropped into the window.
|
|
|
|
pub dropped_files: Vec<std::path::PathBuf>,
|
|
|
|
|
|
|
|
/// Someone is threatening to drop these on us.
|
|
|
|
pub hovered_files: Vec<std::path::PathBuf>,
|
2020-04-29 19:25:49 +00:00
|
|
|
|
|
|
|
/// In-order events received this frame
|
|
|
|
pub events: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum Event {
|
|
|
|
Copy,
|
|
|
|
Cut,
|
|
|
|
/// Text input, e.g. via keyboard or paste action
|
|
|
|
Text(String),
|
|
|
|
Key {
|
|
|
|
key: Key,
|
|
|
|
pressed: bool,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum Key {
|
|
|
|
Alt,
|
|
|
|
Backspace,
|
|
|
|
Control,
|
|
|
|
Delete,
|
|
|
|
Down,
|
|
|
|
End,
|
|
|
|
Escape,
|
|
|
|
Home,
|
|
|
|
Insert,
|
|
|
|
Left,
|
|
|
|
/// Windows key or Mac Command key
|
|
|
|
Logo,
|
|
|
|
PageDown,
|
|
|
|
PageUp,
|
|
|
|
Return,
|
|
|
|
Right,
|
|
|
|
Shift,
|
|
|
|
// Space,
|
|
|
|
Tab,
|
|
|
|
Up,
|
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 {
|
2020-04-23 20:08:42 +00:00
|
|
|
mouse_down: new.mouse_down && new.mouse_pos.is_some(),
|
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,
|
2020-04-28 21:05:22 +00:00
|
|
|
dropped_files: new.dropped_files.clone(),
|
|
|
|
hovered_files: new.hovered_files.clone(),
|
2020-04-29 19:25:49 +00:00
|
|
|
events: new.events.clone(),
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
#[derive(Clone, Default, Serialize)]
|
|
|
|
pub struct Output {
|
|
|
|
pub cursor_icon: CursorIcon,
|
2020-04-29 19:25:49 +00:00
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
/// If set, open this url.
|
|
|
|
pub open_url: Option<String>,
|
2020-04-29 19:25:49 +00:00
|
|
|
|
|
|
|
/// Response to Event::Copy or Event::Cut. Ignore if empty.
|
|
|
|
pub copied_text: String,
|
2020-04-23 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Serialize)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum CursorIcon {
|
|
|
|
Default,
|
|
|
|
/// Pointing hand, used for e.g. web links
|
|
|
|
PointingHand,
|
|
|
|
ResizeNwSe,
|
2020-04-29 19:25:49 +00:00
|
|
|
Text,
|
2020-04-23 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CursorIcon {
|
|
|
|
fn default() -> Self {
|
|
|
|
CursorIcon::Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-04-25 12:37:39 +00:00
|
|
|
|
|
|
|
impl PaintCmd {
|
|
|
|
pub fn line_segment(seg: (Pos2, Pos2), color: Color, width: f32) -> Self {
|
|
|
|
Self::Line {
|
|
|
|
points: vec![seg.0, seg.1],
|
|
|
|
color,
|
|
|
|
width,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|