mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -04:00
Break into two crates
This commit is contained in:
12
emgui/Cargo.toml
Normal file
12
emgui/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "emgui"
|
||||
version = "0.1.0"
|
||||
authors = ["Emil Ernerfeldt <emilernerfeldt@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
|
||||
[dependencies]
|
||||
# palette = "0.4"
|
||||
serde = "1"
|
||||
serde_derive = "1"
|
||||
21
emgui/src/emgui.rs
Normal file
21
emgui/src/emgui.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::{layout, style, types::*};
|
||||
|
||||
/// Encapsulates input, layout and painting for ease of use.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Emgui {
|
||||
pub last_input: RawInput,
|
||||
pub layout: layout::Layout,
|
||||
pub style: style::Style,
|
||||
}
|
||||
|
||||
impl Emgui {
|
||||
pub fn new_frame(&mut self, new_input: RawInput) {
|
||||
let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input);
|
||||
self.last_input = new_input;
|
||||
self.layout.new_frame(gui_input);
|
||||
}
|
||||
|
||||
pub fn paint(&mut self) -> Vec<PaintCmd> {
|
||||
style::into_paint_commands(self.layout.gui_commands(), &self.style)
|
||||
}
|
||||
}
|
||||
486
emgui/src/layout.rs
Normal file
486
emgui/src/layout.rs
Normal file
@@ -0,0 +1,486 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::{math::*, types::*};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
pub struct LayoutOptions {
|
||||
/// The width and height of a single character (including any spacing).
|
||||
/// All text is monospace!
|
||||
pub char_size: Vec2,
|
||||
|
||||
/// Horizontal and vertical padding within a window frame.
|
||||
pub window_padding: Vec2,
|
||||
|
||||
/// Horizontal and vertical spacing between widgets
|
||||
pub item_spacing: Vec2,
|
||||
|
||||
/// Indent foldable regions etc by this much.
|
||||
pub indent: f32,
|
||||
|
||||
/// Default width of sliders, foldout categories etc. TODO: percentage of parent?
|
||||
pub width: f32,
|
||||
|
||||
/// Button size is text size plus this on each side
|
||||
pub button_padding: Vec2,
|
||||
|
||||
/// Checkboxed, radio button and foldables have an icon at the start.
|
||||
/// The text starts after this many pixels.
|
||||
pub start_icon_width: f32,
|
||||
}
|
||||
|
||||
impl Default for LayoutOptions {
|
||||
fn default() -> Self {
|
||||
LayoutOptions {
|
||||
char_size: vec2(7.2, 14.0),
|
||||
item_spacing: vec2(8.0, 4.0),
|
||||
window_padding: vec2(6.0, 6.0),
|
||||
indent: 21.0,
|
||||
width: 250.0,
|
||||
button_padding: vec2(5.0, 3.0),
|
||||
start_icon_width: 20.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// TODO: rename
|
||||
pub struct GuiResponse<'a> {
|
||||
/// The mouse is hovering above this
|
||||
pub hovered: bool,
|
||||
|
||||
/// The mouse went got pressed on this thing this frame
|
||||
pub clicked: bool,
|
||||
|
||||
/// The mouse is interacting with this thing (e.g. dragging it)
|
||||
pub active: bool,
|
||||
|
||||
layout: &'a mut Layout,
|
||||
}
|
||||
|
||||
impl<'a> GuiResponse<'a> {
|
||||
/// Show some stuff if the item was hovered
|
||||
pub fn tooltip<F>(self, add_contents: F) -> Self
|
||||
where
|
||||
F: FnOnce(&mut Layout),
|
||||
{
|
||||
if self.hovered {
|
||||
let window_pos = self.layout.input.mouse_pos + vec2(16.0, 16.0);
|
||||
self.layout.show_popup(window_pos, add_contents);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Show this text if the item was hovered
|
||||
pub fn tooltip_text<S: Into<String>>(self, text: S) -> Self {
|
||||
self.tooltip(|popup| {
|
||||
popup.label(text);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Memory {
|
||||
/// The widget being interacted with (e.g. dragged, in case of a slider).
|
||||
active_id: Option<Id>,
|
||||
|
||||
/// Which foldable regions are open.
|
||||
open_foldables: HashSet<Id>,
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
struct TextFragment {
|
||||
rect: Rect,
|
||||
text: String,
|
||||
}
|
||||
|
||||
type TextFragments = Vec<TextFragment>;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Direction {
|
||||
Horizontal,
|
||||
Vertical,
|
||||
}
|
||||
|
||||
impl Default for Direction {
|
||||
fn default() -> Direction {
|
||||
Direction::Vertical
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// TODO: give this a better name
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Layouter {
|
||||
/// Doesn't change.
|
||||
dir: Direction,
|
||||
|
||||
/// Changes only along self.dir
|
||||
cursor: Vec2,
|
||||
|
||||
/// We keep track of our max-size along the orthogonal to self.dir
|
||||
size: Vec2,
|
||||
}
|
||||
|
||||
impl Layouter {
|
||||
/// Reserve this much space and move the cursor.
|
||||
fn reserve_space(&mut self, size: Vec2) {
|
||||
if self.dir == Direction::Horizontal {
|
||||
self.cursor.x += size.x;
|
||||
self.size.x += size.x;
|
||||
self.size.y = self.size.y.max(size.y);
|
||||
} else {
|
||||
self.cursor.y += size.y;
|
||||
self.size.y += size.y;
|
||||
self.size.x = self.size.x.max(size.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
type Id = u64;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Layout {
|
||||
options: LayoutOptions,
|
||||
input: GuiInput,
|
||||
memory: Memory,
|
||||
id: Id,
|
||||
layouter: Layouter,
|
||||
graphics: Vec<GuiCmd>,
|
||||
hovering_graphics: Vec<GuiCmd>,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
pub fn input(&self) -> &GuiInput {
|
||||
&self.input
|
||||
}
|
||||
|
||||
pub fn gui_commands(&self) -> impl Iterator<Item = &GuiCmd> {
|
||||
self.graphics.iter().chain(self.hovering_graphics.iter())
|
||||
}
|
||||
|
||||
pub fn options(&self) -> &LayoutOptions {
|
||||
&self.options
|
||||
}
|
||||
|
||||
pub fn set_options(&mut self, options: LayoutOptions) {
|
||||
self.options = options;
|
||||
}
|
||||
|
||||
// TODO: move
|
||||
pub fn new_frame(&mut self, gui_input: GuiInput) {
|
||||
self.graphics.clear();
|
||||
self.hovering_graphics.clear();
|
||||
self.layouter = Default::default();
|
||||
self.input = gui_input;
|
||||
if !gui_input.mouse_down {
|
||||
self.memory.active_id = None;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
pub fn button<S: Into<String>>(&mut self, text: S) -> GuiResponse {
|
||||
let text: String = text.into();
|
||||
let id = self.get_id(&text);
|
||||
let (text, text_size) = self.layout_text(&text);
|
||||
let text_cursor = self.layouter.cursor + self.options.button_padding;
|
||||
let (rect, interact) =
|
||||
self.reserve_space(text_size + 2.0 * self.options.button_padding, Some(id));
|
||||
self.graphics.push(GuiCmd::Button { interact, rect });
|
||||
self.add_text(text_cursor, text);
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
pub fn checkbox<S: Into<String>>(&mut self, text: S, checked: &mut bool) -> GuiResponse {
|
||||
let text: String = text.into();
|
||||
let id = self.get_id(&text);
|
||||
let (text, text_size) = self.layout_text(&text);
|
||||
let text_cursor = self.layouter.cursor
|
||||
+ self.options.button_padding
|
||||
+ vec2(self.options.start_icon_width, 0.0);
|
||||
let (rect, interact) = self.reserve_space(
|
||||
self.options.button_padding
|
||||
+ vec2(self.options.start_icon_width, 0.0)
|
||||
+ text_size
|
||||
+ self.options.button_padding,
|
||||
Some(id),
|
||||
);
|
||||
if interact.clicked {
|
||||
*checked = !*checked;
|
||||
}
|
||||
self.graphics.push(GuiCmd::Checkbox {
|
||||
checked: *checked,
|
||||
interact,
|
||||
rect,
|
||||
});
|
||||
self.add_text(text_cursor, text);
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
pub fn label<S: Into<String>>(&mut self, text: S) -> GuiResponse {
|
||||
let text: String = text.into();
|
||||
let (text, text_size) = self.layout_text(&text);
|
||||
self.add_text(self.layouter.cursor, text);
|
||||
let (_, interact) = self.reserve_space(text_size, None);
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
/// A radio button
|
||||
pub fn radio<S: Into<String>>(&mut self, text: S, checked: bool) -> GuiResponse {
|
||||
let text: String = text.into();
|
||||
let id = self.get_id(&text);
|
||||
let (text, text_size) = self.layout_text(&text);
|
||||
let text_cursor = self.layouter.cursor
|
||||
+ self.options.button_padding
|
||||
+ vec2(self.options.start_icon_width, 0.0);
|
||||
let (rect, interact) = self.reserve_space(
|
||||
self.options.button_padding
|
||||
+ vec2(self.options.start_icon_width, 0.0)
|
||||
+ text_size
|
||||
+ self.options.button_padding,
|
||||
Some(id),
|
||||
);
|
||||
self.graphics.push(GuiCmd::RadioButton {
|
||||
checked,
|
||||
interact,
|
||||
rect,
|
||||
});
|
||||
self.add_text(text_cursor, text);
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
pub fn slider_f32<S: Into<String>>(
|
||||
&mut self,
|
||||
text: S,
|
||||
value: &mut f32,
|
||||
min: f32,
|
||||
max: f32,
|
||||
) -> GuiResponse {
|
||||
debug_assert!(min <= max);
|
||||
let text: String = text.into();
|
||||
let id = self.get_id(&text);
|
||||
let (text, text_size) = self.layout_text(&format!("{}: {:.3}", text, value));
|
||||
self.add_text(self.layouter.cursor, text);
|
||||
self.layouter.reserve_space(text_size);
|
||||
let (slider_rect, interact) = self.reserve_space(
|
||||
Vec2 {
|
||||
x: self.options.width,
|
||||
y: self.options.char_size.y,
|
||||
},
|
||||
Some(id),
|
||||
);
|
||||
|
||||
if interact.active {
|
||||
*value = remap_clamp(
|
||||
self.input.mouse_pos.x,
|
||||
slider_rect.min().x,
|
||||
slider_rect.max().x,
|
||||
min,
|
||||
max,
|
||||
);
|
||||
}
|
||||
|
||||
self.graphics.push(GuiCmd::Slider {
|
||||
interact,
|
||||
max,
|
||||
min,
|
||||
rect: slider_rect,
|
||||
value: *value,
|
||||
});
|
||||
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Areas:
|
||||
|
||||
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
|
||||
where
|
||||
S: Into<String>,
|
||||
F: FnOnce(&mut Layout),
|
||||
{
|
||||
assert!(
|
||||
self.layouter.dir == Direction::Vertical,
|
||||
"Horizontal foldable is unimplemented"
|
||||
);
|
||||
let text: String = text.into();
|
||||
let id = self.get_id(&text);
|
||||
let (text, text_size) = self.layout_text(&text);
|
||||
let text_cursor = self.layouter.cursor + self.options.button_padding;
|
||||
let (rect, interact) = self.reserve_space(
|
||||
vec2(
|
||||
self.options.width,
|
||||
text_size.y + 2.0 * self.options.button_padding.y,
|
||||
),
|
||||
Some(id),
|
||||
);
|
||||
|
||||
if interact.clicked {
|
||||
if self.memory.open_foldables.contains(&id) {
|
||||
self.memory.open_foldables.remove(&id);
|
||||
} else {
|
||||
self.memory.open_foldables.insert(id);
|
||||
}
|
||||
}
|
||||
let open = self.memory.open_foldables.contains(&id);
|
||||
|
||||
self.graphics.push(GuiCmd::FoldableHeader {
|
||||
interact,
|
||||
rect,
|
||||
open,
|
||||
});
|
||||
self.add_text(text_cursor + vec2(self.options.start_icon_width, 0.0), text);
|
||||
|
||||
if open {
|
||||
let old_id = self.id;
|
||||
self.id = id;
|
||||
let old_x = self.layouter.cursor.x;
|
||||
self.layouter.cursor.x += self.options.indent;
|
||||
add_contents(self);
|
||||
self.layouter.cursor.x = old_x;
|
||||
self.id = old_id;
|
||||
}
|
||||
|
||||
self.response(interact)
|
||||
}
|
||||
|
||||
/// Start a region with horizontal layout
|
||||
pub fn horizontal<F>(&mut self, add_contents: F)
|
||||
where
|
||||
F: FnOnce(&mut Layout),
|
||||
{
|
||||
let horizontal_layouter = Layouter {
|
||||
dir: Direction::Horizontal,
|
||||
cursor: self.layouter.cursor,
|
||||
..Default::default()
|
||||
};
|
||||
let old_layouter = std::mem::replace(&mut self.layouter, horizontal_layouter);
|
||||
add_contents(self);
|
||||
let horizontal_layouter = std::mem::replace(&mut self.layouter, old_layouter);
|
||||
self.layouter.reserve_space(horizontal_layouter.size);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Free painting. It is up to the caller to make sure there is room for these.
|
||||
pub fn add_paint_command(&mut self, cmd: GuiCmd) {
|
||||
self.graphics.push(cmd);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/// Show a pop-over window
|
||||
pub fn show_popup<F>(&mut self, window_pos: Vec2, add_contents: F)
|
||||
where
|
||||
F: FnOnce(&mut Layout),
|
||||
{
|
||||
// TODO: less copying
|
||||
let mut popup_layout = Layout {
|
||||
options: self.options,
|
||||
input: self.input,
|
||||
memory: self.memory.clone(), // TODO: Arc
|
||||
id: self.id,
|
||||
layouter: Default::default(),
|
||||
graphics: vec![],
|
||||
hovering_graphics: vec![],
|
||||
};
|
||||
popup_layout.layouter.cursor = window_pos + self.options.window_padding;
|
||||
|
||||
add_contents(&mut popup_layout);
|
||||
|
||||
// TODO: handle the last item_spacing in a nicer way
|
||||
let inner_size = popup_layout.layouter.size - self.options.item_spacing;
|
||||
let outer_size = inner_size + 2.0 * self.options.window_padding;
|
||||
|
||||
let rect = Rect::from_min_size(window_pos, outer_size);
|
||||
self.hovering_graphics.push(GuiCmd::Window { rect });
|
||||
self.hovering_graphics
|
||||
.extend(popup_layout.gui_commands().cloned());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
fn reserve_space(&mut self, size: Vec2, interaction_id: Option<Id>) -> (Rect, InteractInfo) {
|
||||
let rect = Rect {
|
||||
pos: self.layouter.cursor,
|
||||
size,
|
||||
};
|
||||
self.layouter
|
||||
.reserve_space(size + self.options.item_spacing);
|
||||
let hovered = rect.contains(self.input.mouse_pos);
|
||||
let clicked = hovered && self.input.mouse_clicked;
|
||||
let active = if interaction_id.is_some() {
|
||||
if clicked {
|
||||
self.memory.active_id = interaction_id;
|
||||
}
|
||||
self.memory.active_id == interaction_id
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let interact = InteractInfo {
|
||||
hovered,
|
||||
clicked,
|
||||
active,
|
||||
};
|
||||
(rect, interact)
|
||||
}
|
||||
|
||||
fn get_id(&self, id_str: &str) -> Id {
|
||||
use std::hash::Hasher;
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
hasher.write_u64(self.id);
|
||||
hasher.write(id_str.as_bytes());
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
fn layout_text(&self, text: &str) -> (TextFragments, Vec2) {
|
||||
let char_size = self.options.char_size;
|
||||
let mut cursor_y = 0.0;
|
||||
let mut max_width = 0.0;
|
||||
let mut text_fragments = Vec::new();
|
||||
for line in text.split('\n') {
|
||||
// TODO: break long lines
|
||||
let line_width = char_size.x * (line.len() as f32);
|
||||
|
||||
text_fragments.push(TextFragment {
|
||||
rect: Rect::from_min_size(vec2(0.0, cursor_y), vec2(line_width, char_size.y)),
|
||||
text: line.into(),
|
||||
});
|
||||
|
||||
cursor_y += char_size.y;
|
||||
max_width = line_width.max(max_width);
|
||||
}
|
||||
let bounding_size = vec2(max_width, cursor_y);
|
||||
(text_fragments, bounding_size)
|
||||
}
|
||||
|
||||
fn add_text(&mut self, pos: Vec2, text: Vec<TextFragment>) {
|
||||
for fragment in text {
|
||||
self.graphics.push(GuiCmd::Text {
|
||||
pos: pos + vec2(fragment.rect.pos.x, fragment.rect.center().y),
|
||||
style: TextStyle::Label,
|
||||
text: fragment.text,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn response(&mut self, interact: InteractInfo) -> GuiResponse {
|
||||
GuiResponse {
|
||||
hovered: interact.hovered,
|
||||
clicked: interact.clicked,
|
||||
active: interact.active,
|
||||
layout: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
16
emgui/src/lib.rs
Normal file
16
emgui/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
extern crate serde;
|
||||
|
||||
#[macro_use] // TODO: get rid of this
|
||||
extern crate serde_derive;
|
||||
|
||||
mod emgui;
|
||||
mod layout;
|
||||
pub mod math;
|
||||
mod style;
|
||||
pub mod types;
|
||||
|
||||
pub use crate::{
|
||||
emgui::Emgui, layout::Layout, layout::LayoutOptions, style::Style, types::RawInput,
|
||||
};
|
||||
104
emgui/src/math.rs
Normal file
104
emgui/src/math.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct Vec2 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
impl std::ops::Add for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn add(self, rhs: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Sub for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn sub(self, rhs: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<f32> for Vec2 {
|
||||
type Output = Vec2;
|
||||
fn mul(self, factor: f32) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x * factor,
|
||||
y: self.y * factor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<Vec2> for f32 {
|
||||
type Output = Vec2;
|
||||
fn mul(self, vec: Vec2) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self * vec.x,
|
||||
y: self * vec.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vec2(x: f32, y: f32) -> Vec2 {
|
||||
Vec2 { x, y }
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct Rect {
|
||||
pub pos: Vec2,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub fn from_min_size(min: Vec2, size: Vec2) -> Self {
|
||||
Rect { pos: min, size }
|
||||
}
|
||||
|
||||
pub fn from_center_size(center: Vec2, size: Vec2) -> Self {
|
||||
Rect {
|
||||
pos: center - size * 0.5,
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn contains(&self, p: Vec2) -> bool {
|
||||
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
|
||||
}
|
||||
|
||||
pub fn center(&self) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.pos.x + self.size.x / 2.0,
|
||||
y: self.pos.y + self.size.y / 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn min(&self) -> Vec2 {
|
||||
self.pos
|
||||
}
|
||||
pub fn max(&self) -> Vec2 {
|
||||
self.pos + self.size
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lerp(min: f32, max: f32, t: f32) -> f32 {
|
||||
(1.0 - t) * min + t * max
|
||||
}
|
||||
|
||||
pub fn remap_clamp(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 {
|
||||
let t = if from <= from_min {
|
||||
0.0
|
||||
} else if from >= from_max {
|
||||
1.0
|
||||
} else {
|
||||
(from - from_min) / (from_max - from_min)
|
||||
};
|
||||
lerp(to_min, to_max, t)
|
||||
}
|
||||
285
emgui/src/style.rs
Normal file
285
emgui/src/style.rs
Normal file
@@ -0,0 +1,285 @@
|
||||
use crate::{math::*, types::*};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Style {
|
||||
/// Show rectangles around each widget
|
||||
pub debug_rects: bool,
|
||||
|
||||
/// For stuff like check marks in check boxes.
|
||||
pub line_width: f32,
|
||||
|
||||
pub font_name: String,
|
||||
|
||||
/// Height in pixels of most text.
|
||||
pub font_size: f32,
|
||||
}
|
||||
|
||||
impl Default for Style {
|
||||
fn default() -> Style {
|
||||
Style {
|
||||
debug_rects: false,
|
||||
line_width: 2.0,
|
||||
// font_name: "Palatino".to_string(),
|
||||
font_name: "Courier".to_string(),
|
||||
// font_name: "Courier New".to_string(),
|
||||
font_size: 12.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Style {
|
||||
/// e.g. the background of the slider
|
||||
fn background_fill_color(&self) -> Color {
|
||||
srgba(34, 34, 34, 200)
|
||||
}
|
||||
|
||||
fn text_color(&self) -> Color {
|
||||
srgba(255, 255, 255, 187)
|
||||
}
|
||||
|
||||
/// Fill color of the interactive part of a component (button, slider grab, checkbox, ...)
|
||||
fn interact_fill_color(&self, interact: &InteractInfo) -> Color {
|
||||
if interact.active {
|
||||
srgba(136, 136, 136, 255)
|
||||
} else if interact.hovered {
|
||||
srgba(100, 100, 100, 255)
|
||||
} else {
|
||||
srgba(68, 68, 68, 220)
|
||||
}
|
||||
}
|
||||
|
||||
/// Stroke and text color of the interactive part of a component (button, slider grab, checkbox, ...)
|
||||
fn interact_stroke_color(&self, interact: &InteractInfo) -> Color {
|
||||
if interact.active {
|
||||
srgba(255, 255, 255, 255)
|
||||
} else if interact.hovered {
|
||||
srgba(255, 255, 255, 200)
|
||||
} else {
|
||||
srgba(255, 255, 255, 170)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns small icon rectangle and big icon rectangle
|
||||
fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) {
|
||||
let box_side = 16.0;
|
||||
let big_icon_rect = Rect::from_center_size(
|
||||
vec2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y),
|
||||
vec2(box_side, box_side),
|
||||
);
|
||||
|
||||
let small_icon_rect = Rect::from_center_size(big_icon_rect.center(), vec2(10.0, 10.0));
|
||||
|
||||
(small_icon_rect, big_icon_rect)
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_rect(rect: Rect) -> PaintCmd {
|
||||
PaintCmd::Rect {
|
||||
corner_radius: 0.0,
|
||||
fill_color: None,
|
||||
outline: Some(Outline {
|
||||
color: srgba(255, 255, 255, 255),
|
||||
width: 1.0,
|
||||
}),
|
||||
pos: rect.pos,
|
||||
size: rect.size,
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO: a Style struct which defines colors etc
|
||||
fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
|
||||
match cmd {
|
||||
GuiCmd::PaintCommands(mut commands) => out_commands.append(&mut commands),
|
||||
GuiCmd::Button { interact, rect } => {
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 5.0,
|
||||
fill_color: Some(style.interact_fill_color(&interact)),
|
||||
outline: None,
|
||||
pos: rect.pos,
|
||||
size: rect.size,
|
||||
});
|
||||
if style.debug_rects {
|
||||
out_commands.push(debug_rect(rect));
|
||||
}
|
||||
}
|
||||
GuiCmd::Checkbox {
|
||||
checked,
|
||||
interact,
|
||||
rect,
|
||||
} => {
|
||||
let (small_icon_rect, big_icon_rect) = style.icon_rectangles(&rect);
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 3.0,
|
||||
fill_color: Some(style.interact_fill_color(&interact)),
|
||||
outline: None,
|
||||
pos: big_icon_rect.pos,
|
||||
size: big_icon_rect.size,
|
||||
});
|
||||
|
||||
let stroke_color = style.interact_stroke_color(&interact);
|
||||
|
||||
if checked {
|
||||
out_commands.push(PaintCmd::Line {
|
||||
points: vec![
|
||||
vec2(small_icon_rect.min().x, small_icon_rect.center().y),
|
||||
vec2(small_icon_rect.center().x, small_icon_rect.max().y),
|
||||
vec2(small_icon_rect.max().x, small_icon_rect.min().y),
|
||||
],
|
||||
color: stroke_color,
|
||||
width: style.line_width,
|
||||
});
|
||||
}
|
||||
|
||||
if style.debug_rects {
|
||||
out_commands.push(debug_rect(rect));
|
||||
}
|
||||
}
|
||||
GuiCmd::FoldableHeader {
|
||||
interact,
|
||||
open,
|
||||
rect,
|
||||
} => {
|
||||
let fill_color = style.interact_fill_color(&interact);
|
||||
let stroke_color = style.interact_stroke_color(&interact);
|
||||
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 3.0,
|
||||
fill_color: Some(fill_color),
|
||||
outline: None,
|
||||
pos: rect.pos,
|
||||
size: rect.size,
|
||||
});
|
||||
|
||||
// TODO: paint a little triangle or arrow or something instead of this
|
||||
|
||||
let (small_icon_rect, _) = style.icon_rectangles(&rect);
|
||||
// Draw a minus:
|
||||
out_commands.push(PaintCmd::Line {
|
||||
points: vec![
|
||||
vec2(small_icon_rect.min().x, small_icon_rect.center().y),
|
||||
vec2(small_icon_rect.max().x, small_icon_rect.center().y),
|
||||
],
|
||||
color: stroke_color,
|
||||
width: style.line_width,
|
||||
});
|
||||
if !open {
|
||||
// Draw it as a plus:
|
||||
out_commands.push(PaintCmd::Line {
|
||||
points: vec![
|
||||
vec2(small_icon_rect.center().x, small_icon_rect.min().y),
|
||||
vec2(small_icon_rect.center().x, small_icon_rect.max().y),
|
||||
],
|
||||
color: stroke_color,
|
||||
width: style.line_width,
|
||||
});
|
||||
}
|
||||
}
|
||||
GuiCmd::RadioButton {
|
||||
checked,
|
||||
interact,
|
||||
rect,
|
||||
} => {
|
||||
let fill_color = style.interact_fill_color(&interact);
|
||||
let stroke_color = style.interact_stroke_color(&interact);
|
||||
|
||||
let (small_icon_rect, big_icon_rect) = style.icon_rectangles(&rect);
|
||||
|
||||
out_commands.push(PaintCmd::Circle {
|
||||
center: big_icon_rect.center(),
|
||||
fill_color: Some(fill_color),
|
||||
outline: None,
|
||||
radius: big_icon_rect.size.x / 2.0,
|
||||
});
|
||||
|
||||
if checked {
|
||||
out_commands.push(PaintCmd::Circle {
|
||||
center: small_icon_rect.center(),
|
||||
fill_color: Some(stroke_color),
|
||||
outline: None,
|
||||
radius: small_icon_rect.size.x / 2.0,
|
||||
});
|
||||
}
|
||||
|
||||
if style.debug_rects {
|
||||
out_commands.push(debug_rect(rect));
|
||||
}
|
||||
}
|
||||
GuiCmd::Slider {
|
||||
interact,
|
||||
max,
|
||||
min,
|
||||
rect,
|
||||
value,
|
||||
} => {
|
||||
let thin_rect = Rect::from_center_size(rect.center(), vec2(rect.size.x, 6.0));
|
||||
let marker_center_x = remap_clamp(value, min, max, rect.min().x, rect.max().x);
|
||||
|
||||
let marker_rect = Rect::from_center_size(
|
||||
vec2(marker_center_x, thin_rect.center().y),
|
||||
vec2(16.0, 16.0),
|
||||
);
|
||||
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 2.0,
|
||||
fill_color: Some(style.background_fill_color()),
|
||||
outline: None,
|
||||
pos: thin_rect.pos,
|
||||
size: thin_rect.size,
|
||||
});
|
||||
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 3.0,
|
||||
fill_color: Some(style.interact_fill_color(&interact)),
|
||||
outline: None,
|
||||
pos: marker_rect.pos,
|
||||
size: marker_rect.size,
|
||||
});
|
||||
|
||||
if style.debug_rects {
|
||||
out_commands.push(debug_rect(rect));
|
||||
}
|
||||
}
|
||||
GuiCmd::Text {
|
||||
pos,
|
||||
text,
|
||||
style: text_style,
|
||||
} => {
|
||||
let fill_color = match text_style {
|
||||
TextStyle::Label => style.text_color(),
|
||||
};
|
||||
out_commands.push(PaintCmd::Text {
|
||||
fill_color,
|
||||
font_name: style.font_name.clone(),
|
||||
font_size: style.font_size,
|
||||
pos,
|
||||
text,
|
||||
});
|
||||
}
|
||||
GuiCmd::Window { rect } => {
|
||||
out_commands.push(PaintCmd::Rect {
|
||||
corner_radius: 5.0,
|
||||
fill_color: Some(style.background_fill_color()),
|
||||
outline: Some(Outline {
|
||||
color: srgba(255, 255, 255, 255), // TODO
|
||||
width: 1.0,
|
||||
}),
|
||||
pos: rect.pos,
|
||||
size: rect.size,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_paint_commands<'a, GuiCmdIterator>(
|
||||
gui_commands: GuiCmdIterator,
|
||||
style: &Style,
|
||||
) -> Vec<PaintCmd>
|
||||
where
|
||||
GuiCmdIterator: Iterator<Item = &'a GuiCmd>,
|
||||
{
|
||||
let mut paint_commands = vec![];
|
||||
for gui_cmd in gui_commands {
|
||||
translate_cmd(&mut paint_commands, style, gui_cmd.clone())
|
||||
}
|
||||
paint_commands
|
||||
}
|
||||
173
emgui/src/types.rs
Normal file
173
emgui/src/types.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
use crate::math::{Rect, Vec2};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// 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.
|
||||
pub screen_size: Vec2,
|
||||
}
|
||||
|
||||
/// What the gui maintains
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
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: Vec2,
|
||||
|
||||
/// Size of the screen in points.
|
||||
pub screen_size: Vec2,
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// 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 }
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Serialize)]
|
||||
pub struct InteractInfo {
|
||||
/// The mouse is hovering above this
|
||||
pub hovered: bool,
|
||||
|
||||
/// The mouse went got pressed on this thing this frame
|
||||
pub clicked: bool,
|
||||
|
||||
/// The mouse is interacting with this thing (e.g. dragging it)
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum TextStyle {
|
||||
Label,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub enum GuiCmd {
|
||||
PaintCommands(Vec<PaintCmd>),
|
||||
/// The background for a button
|
||||
Button {
|
||||
interact: InteractInfo,
|
||||
rect: Rect,
|
||||
},
|
||||
Checkbox {
|
||||
checked: bool,
|
||||
interact: InteractInfo,
|
||||
rect: Rect,
|
||||
},
|
||||
/// The header button background for a foldable region
|
||||
FoldableHeader {
|
||||
interact: InteractInfo,
|
||||
open: bool,
|
||||
rect: Rect,
|
||||
},
|
||||
RadioButton {
|
||||
checked: bool,
|
||||
interact: InteractInfo,
|
||||
rect: Rect,
|
||||
},
|
||||
Slider {
|
||||
interact: InteractInfo,
|
||||
max: f32,
|
||||
min: f32,
|
||||
rect: Rect,
|
||||
value: f32,
|
||||
},
|
||||
/// Paint a single line of mono-space text.
|
||||
/// The text should start at the given position and flow to the right.
|
||||
/// The text should be vertically centered at the given position.
|
||||
Text {
|
||||
pos: Vec2,
|
||||
style: TextStyle,
|
||||
text: String,
|
||||
},
|
||||
/// Background of e.g. a popup
|
||||
Window {
|
||||
rect: Rect,
|
||||
},
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Outline {
|
||||
pub width: f32,
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)] // TODO: copy
|
||||
#[serde(rename_all = "snake_case", tag = "kind")]
|
||||
pub enum PaintCmd {
|
||||
Circle {
|
||||
center: Vec2,
|
||||
fill_color: Option<Color>,
|
||||
outline: Option<Outline>,
|
||||
radius: f32,
|
||||
},
|
||||
Clear {
|
||||
fill_color: Color,
|
||||
},
|
||||
Line {
|
||||
points: Vec<Vec2>,
|
||||
color: Color,
|
||||
width: f32,
|
||||
},
|
||||
Rect {
|
||||
corner_radius: f32,
|
||||
fill_color: Option<Color>,
|
||||
outline: Option<Outline>,
|
||||
pos: Vec2,
|
||||
size: Vec2,
|
||||
},
|
||||
/// Paint a single line of mono-space text.
|
||||
/// The text should start at the given position and flow to the right.
|
||||
/// The text should be vertically centered at the given position.
|
||||
Text {
|
||||
fill_color: Color,
|
||||
/// Name, e.g. Palatino
|
||||
font_name: String,
|
||||
/// Height in pixels, e.g. 12
|
||||
font_size: f32,
|
||||
pos: Vec2,
|
||||
text: String,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user