egui/emgui/src/emgui.rs

64 lines
1.9 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2019-01-06 15:34:01 +00:00
use crate::{font::Font, layout, style, types::GuiInput, Frame, Painter, RawInput};
2018-12-26 22:08:50 +00:00
/// Encapsulates input, layout and painting for ease of use.
pub struct Emgui {
pub last_input: RawInput,
2019-01-07 07:54:37 +00:00
pub data: Arc<layout::Data>,
2018-12-26 22:08:50 +00:00
pub style: style::Style,
2019-01-06 15:34:01 +00:00
pub painter: Painter,
2018-12-26 22:08:50 +00:00
}
impl Emgui {
pub fn new(font: Arc<Font>) -> Emgui {
2019-01-05 14:28:07 +00:00
Emgui {
last_input: Default::default(),
2019-01-07 07:54:37 +00:00
data: Arc::new(layout::Data::new(font.clone())),
2019-01-05 14:28:07 +00:00
style: Default::default(),
2019-01-06 15:34:01 +00:00
painter: Painter::new(font),
2019-01-05 14:28:07 +00:00
}
}
2019-01-06 15:34:01 +00:00
pub fn texture(&self) -> (u16, u16, &[u8]) {
self.painter.texture()
}
2018-12-26 22:08:50 +00:00
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;
2019-01-07 07:54:37 +00:00
let mut new_data = (*self.data).clone();
new_data.new_frame(gui_input);
self.data = Arc::new(new_data);
2019-01-06 15:34:01 +00:00
}
pub fn whole_screen_region(&mut self) -> layout::Region {
let size = self.data.input.screen_size;
2019-01-06 15:34:01 +00:00
layout::Region {
2019-01-07 07:54:37 +00:00
data: self.data.clone(),
2019-01-06 15:34:01 +00:00
id: Default::default(),
dir: layout::Direction::Vertical,
cursor: Default::default(),
bounding_size: Default::default(),
available_space: size,
2019-01-06 15:34:01 +00:00
}
}
2019-01-07 07:54:37 +00:00
pub fn options(&self) -> &layout::LayoutOptions {
&self.data.options
}
2019-01-06 15:34:01 +00:00
pub fn set_options(&mut self, options: layout::LayoutOptions) {
2019-01-07 07:54:37 +00:00
let mut new_data = (*self.data).clone();
new_data.options = options;
self.data = Arc::new(new_data);
2018-12-26 22:08:50 +00:00
}
2019-01-06 15:34:01 +00:00
pub fn paint(&mut self) -> Frame {
2019-01-07 07:54:37 +00:00
let gui_commands = self.data.graphics.lock().unwrap().drain();
2019-01-06 15:34:01 +00:00
let paint_commands = style::into_paint_commands(gui_commands, &self.style);
self.painter.paint(&paint_commands)
2018-12-26 22:08:50 +00:00
}
}