From b5600b301d84b68d01a53c14d5d9733130deb9d7 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 23 Dec 2024 00:53:59 +0100 Subject: [PATCH] make more ui disableable --- src/editor.rs | 31 ++++++++++++++++++++----------- src/main.rs | 2 ++ src/ui.rs | 37 ++++++++++++++++--------------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 54b5b42..bc6d6dc 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -638,6 +638,7 @@ impl Editor { let mut text_selected = is_selected && self.blueprint_name_selected; text_input( d, + &self.mouse, Rectangle::new(42., y as f32, 200., 32.), &mut b.name, &mut text_selected, @@ -648,7 +649,16 @@ impl Editor { self.blueprint_name_selected = text_selected; } self.tooltip.add(42 + 205, y, 32, 32, "Select"); - simple_option_button(d, 42 + 205, y, 32, 32, i, &mut self.selected_blueprint); + simple_option_button( + d, + &self.mouse, + 42 + 205, + y, + 32, + 32, + i, + &mut self.selected_blueprint, + ); d.draw_texture_ex( textures.get("blueprint"), @@ -846,6 +856,7 @@ impl Editor { } if text_input( d, + &self.mouse, Rectangle::new(input_x as f32, 5., (width - input_x - 5) as f32, 30.), &mut input_text, &mut self.input_text_selected, @@ -899,6 +910,7 @@ impl Editor { hide_tile_tools = true; text_input( d, + &self.mouse, Rectangle::new(100., footer_top + 10., 240., 30.), &mut self.new_blueprint_name, &mut self.blueprint_name_selected, @@ -942,7 +954,6 @@ impl Editor { draw_scaled_texture(d, textures.get("eraser"), 236, y + 4, 2.); } - let mouse_pos = d.get_mouse_position(); let mut tool_button = |(row, col): (i32, i32), texture: &str, tooltip: &'static str, tool_option: Tool| { let border = 4.; @@ -954,22 +965,20 @@ impl Editor { x: 100. + col as f32 * grid_size - if col < 0 { 10. } else { 0. }, y: footer_top + 5. + row as f32 * grid_size, }; - texture_option_button( + self.tooltip.add_rec( + Rectangle::new(pos.x, pos.y, button_size, button_size), + tooltip, + ); + scrollable_texture_option_button( d, + &self.mouse, pos, textures.get(texture), tool_option, &mut self.active_tool, tex_size, border, - ); - let bounds = Rectangle::new(pos.x, pos.y, button_size, button_size); - self.tooltip.add_rec(bounds, tooltip); - if bounds.check_collision_point_rec(mouse_pos) { - get_scroll(d) - } else { - None - } + ) }; tool_button((0, -2), "eraser", "Eraser", Tool::Erase); tool_button( diff --git a/src/main.rs b/src/main.rs index e19cb33..0cf3a8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -207,6 +207,7 @@ impl Game { for (solution_index, solution) in solutions.iter().enumerate() { if simple_option_button( d, + &mouse, level_list_width + 10, solution_y, entry_width, @@ -263,6 +264,7 @@ impl Game { let bounds = Rectangle::new(column_x as f32, y as f32, 220., 30.); if text_input( d, + &mouse, bounds, &mut solution.name, &mut self.editing_solution_name, diff --git a/src/ui.rs b/src/ui.rs index d3d1f97..6c18c9c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,6 +1,6 @@ use std::ops::Range; -use crate::{draw_scaled_texture, theme::*, MouseInput, Textures}; +use crate::{draw_scaled_texture, theme::*, MouseInput, Scroll, Textures}; use raylib::prelude::*; #[derive(Debug)] @@ -120,7 +120,7 @@ impl Tooltip { } } - pub fn reset(&mut self){ + pub fn reset(&mut self) { self.text = None; } @@ -180,6 +180,7 @@ pub fn simple_button( pub fn simple_option_button( d: &mut RaylibDrawHandle, + mouse: &MouseInput, x: i32, y: i32, width: i32, @@ -192,12 +193,8 @@ where { let bounds = Rectangle::new(x as f32, y as f32, width as f32, height as f32); d.draw_rectangle_rec(bounds, widget_bg(&option == current)); - let mouse_pos = d.get_mouse_position(); let mut changed = false; - if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) - && bounds.check_collision_point_rec(mouse_pos) - && current != &option - { + if mouse.left_click() && mouse.is_over(bounds) && current != &option { *current = option; changed = true; } @@ -206,6 +203,7 @@ where pub fn text_input( d: &mut RaylibDrawHandle, + mouse: &MouseInput, bounds: Rectangle, text: &mut String, is_selected: &mut bool, @@ -235,11 +233,7 @@ pub fn text_input( 20, Color::WHITE, ); - let mouse_pos = d.get_mouse_position(); - if editable - && d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) - && (bounds.check_collision_point_rec(mouse_pos) || *is_selected) - { + if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) { *is_selected = !*is_selected; } @@ -267,16 +261,17 @@ pub fn text_input( changed } -pub fn texture_option_button( +pub fn scrollable_texture_option_button( d: &mut RaylibDrawHandle, + mouse: &MouseInput, pos: Vector2, texture: &Texture2D, option: T, current: &mut T, tex_size: f32, border: f32, - // tooltip -) where +) -> Option +where T: PartialEq, { let bounds = Rectangle { @@ -300,13 +295,13 @@ pub fn texture_option_button( tex_size / texture.width as f32, Color::WHITE, ); - - let mouse_pos = d.get_mouse_position(); - if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) - && bounds.check_collision_point_rec(mouse_pos) - { - *current = option; + if mouse.is_over(bounds) { + if mouse.left_click() { + *current = option; + } + return mouse.scroll(); } + None } pub fn draw_usize(