diff options
| author | Arne Dußin | 2020-11-20 20:00:28 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-20 20:00:28 +0100 |
| commit | cf0cbe3fd28b2099b580edc1714b4d68bf7183cd (patch) | |
| tree | b03561c1ae7860cad247d089cb1ad728efa843ec /src/gui/tool_sidebar.rs | |
| parent | f62dabcb390d4808739745c050dfba8e2826b214 (diff) | |
| download | graf_karto-cf0cbe3fd28b2099b580edc1714b4d68bf7183cd.tar.gz graf_karto-cf0cbe3fd28b2099b580edc1714b4d68bf7183cd.zip | |
Add simple tool sidebar gui
Diffstat (limited to 'src/gui/tool_sidebar.rs')
| -rw-r--r-- | src/gui/tool_sidebar.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/gui/tool_sidebar.rs b/src/gui/tool_sidebar.rs new file mode 100644 index 0000000..32d0410 --- /dev/null +++ b/src/gui/tool_sidebar.rs @@ -0,0 +1,58 @@ +use crate::math::{Rect, Vec2}; +use crate::tool::ToolType; +use crate::Editor; +use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle}; +use raylib::core::texture::Texture2D; +use raylib::rgui::RaylibDrawGui; +use raylib::{RaylibHandle, RaylibThread}; +use std::mem; + +pub const BUTTON_FILE: &str = "assets/button/tool_buttons.png"; + +pub struct ToolSidebar { + button_texture: Texture2D, +} + +impl ToolSidebar { + pub fn new(rl: &mut RaylibHandle, rlt: &RaylibThread) -> Self { + let button_texture = rl + .load_texture(rlt, BUTTON_FILE) + .expect("Could not read file containing tool icons."); + + Self { button_texture } + } + + fn panel_rect(screen_height: u16) -> Rect<f32> { + Rect::new(0., 0., 104., screen_height as f32) + } + + /// Check if the mouse is currently being captured by this GUI-element. In that case, + /// everything else that might want to access the mouse will be blocked. + pub fn mouse_captured(screen_height: u16, mouse_pos: Vec2<f32>) -> bool { + Self::panel_rect(screen_height).contains(mouse_pos) + } + + pub fn draw(&self, screen_height: u16, rld: &mut impl RaylibDrawGui, editor: &mut Editor) { + rld.gui_panel(Self::panel_rect(screen_height)); + + let mut active = editor.active(); + for i in 0..ToolType::NumTools as usize { + let is_current_active = active as usize == i; + if rld.gui_image_button_ex( + Rect::new(20., i as f32 * 100. + 20., 64., 64.), + None, + &self.button_texture, + Rect::new( + is_current_active as u8 as f32 * 64., + i as f32 * 64., + 64., + 64., + ), + ) { + active = unsafe { mem::transmute(i as u8) }; + } + } + + editor.set_active(active); + } +} |
