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-27 16:47:32 +00:00
|
|
|
/// 0-255 sRGBA
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Serialize)]
|
|
|
|
pub struct Color {
|
|
|
|
pub r: u8,
|
|
|
|
pub g: u8,
|
|
|
|
pub b: u8,
|
|
|
|
pub a: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn srgba(r: u8, g: u8, b: u8, a: u8) -> Color {
|
|
|
|
Color { r, g, b, a }
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
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-27 18:08:43 +00:00
|
|
|
// The header for a foldable region
|
|
|
|
FoldableHeader {
|
|
|
|
interact: InteractInfo,
|
|
|
|
open: bool,
|
|
|
|
rect: Rect,
|
|
|
|
label: String,
|
|
|
|
},
|
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
|
|
|
#[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
|
|
|
}
|
|
|
|
|
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,
|
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-23 19:06:40 +00:00
|
|
|
Clear {
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color,
|
2018-12-23 19:06:40 +00:00
|
|
|
},
|
2018-12-26 21:17:33 +00:00
|
|
|
Line {
|
|
|
|
points: Vec<Vec2>,
|
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>,
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: Vec2,
|
|
|
|
size: Vec2,
|
|
|
|
},
|
|
|
|
Text {
|
2018-12-27 16:47:32 +00:00
|
|
|
fill_color: Color,
|
2018-12-27 17:19:06 +00:00
|
|
|
/// Name, e.g. Palatino
|
|
|
|
font_name: String,
|
|
|
|
/// Height in pixels, e.g. 12
|
|
|
|
font_size: f32,
|
2018-12-23 19:06:40 +00:00
|
|
|
pos: Vec2,
|
|
|
|
text: String,
|
|
|
|
text_align: TextAlign,
|
|
|
|
},
|
|
|
|
}
|