make more ui disableable

This commit is contained in:
Crispy 2024-12-23 00:53:59 +01:00
parent 6fc41bdb17
commit b5600b301d
3 changed files with 38 additions and 32 deletions

View file

@ -638,6 +638,7 @@ impl Editor {
let mut text_selected = is_selected && self.blueprint_name_selected; let mut text_selected = is_selected && self.blueprint_name_selected;
text_input( text_input(
d, d,
&self.mouse,
Rectangle::new(42., y as f32, 200., 32.), Rectangle::new(42., y as f32, 200., 32.),
&mut b.name, &mut b.name,
&mut text_selected, &mut text_selected,
@ -648,7 +649,16 @@ impl Editor {
self.blueprint_name_selected = text_selected; self.blueprint_name_selected = text_selected;
} }
self.tooltip.add(42 + 205, y, 32, 32, "Select"); 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( d.draw_texture_ex(
textures.get("blueprint"), textures.get("blueprint"),
@ -846,6 +856,7 @@ impl Editor {
} }
if text_input( if text_input(
d, d,
&self.mouse,
Rectangle::new(input_x as f32, 5., (width - input_x - 5) as f32, 30.), Rectangle::new(input_x as f32, 5., (width - input_x - 5) as f32, 30.),
&mut input_text, &mut input_text,
&mut self.input_text_selected, &mut self.input_text_selected,
@ -899,6 +910,7 @@ impl Editor {
hide_tile_tools = true; hide_tile_tools = true;
text_input( text_input(
d, d,
&self.mouse,
Rectangle::new(100., footer_top + 10., 240., 30.), Rectangle::new(100., footer_top + 10., 240., 30.),
&mut self.new_blueprint_name, &mut self.new_blueprint_name,
&mut self.blueprint_name_selected, &mut self.blueprint_name_selected,
@ -942,7 +954,6 @@ impl Editor {
draw_scaled_texture(d, textures.get("eraser"), 236, y + 4, 2.); draw_scaled_texture(d, textures.get("eraser"), 236, y + 4, 2.);
} }
let mouse_pos = d.get_mouse_position();
let mut tool_button = let mut tool_button =
|(row, col): (i32, i32), texture: &str, tooltip: &'static str, tool_option: Tool| { |(row, col): (i32, i32), texture: &str, tooltip: &'static str, tool_option: Tool| {
let border = 4.; let border = 4.;
@ -954,22 +965,20 @@ impl Editor {
x: 100. + col as f32 * grid_size - if col < 0 { 10. } else { 0. }, x: 100. + col as f32 * grid_size - if col < 0 { 10. } else { 0. },
y: footer_top + 5. + row as f32 * grid_size, 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, d,
&self.mouse,
pos, pos,
textures.get(texture), textures.get(texture),
tool_option, tool_option,
&mut self.active_tool, &mut self.active_tool,
tex_size, tex_size,
border, 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((0, -2), "eraser", "Eraser", Tool::Erase);
tool_button( tool_button(

View file

@ -207,6 +207,7 @@ impl Game {
for (solution_index, solution) in solutions.iter().enumerate() { for (solution_index, solution) in solutions.iter().enumerate() {
if simple_option_button( if simple_option_button(
d, d,
&mouse,
level_list_width + 10, level_list_width + 10,
solution_y, solution_y,
entry_width, entry_width,
@ -263,6 +264,7 @@ impl Game {
let bounds = Rectangle::new(column_x as f32, y as f32, 220., 30.); let bounds = Rectangle::new(column_x as f32, y as f32, 220., 30.);
if text_input( if text_input(
d, d,
&mouse,
bounds, bounds,
&mut solution.name, &mut solution.name,
&mut self.editing_solution_name, &mut self.editing_solution_name,

View file

@ -1,6 +1,6 @@
use std::ops::Range; use std::ops::Range;
use crate::{draw_scaled_texture, theme::*, MouseInput, Textures}; use crate::{draw_scaled_texture, theme::*, MouseInput, Scroll, Textures};
use raylib::prelude::*; use raylib::prelude::*;
#[derive(Debug)] #[derive(Debug)]
@ -120,7 +120,7 @@ impl Tooltip {
} }
} }
pub fn reset(&mut self){ pub fn reset(&mut self) {
self.text = None; self.text = None;
} }
@ -180,6 +180,7 @@ pub fn simple_button(
pub fn simple_option_button<T>( pub fn simple_option_button<T>(
d: &mut RaylibDrawHandle, d: &mut RaylibDrawHandle,
mouse: &MouseInput,
x: i32, x: i32,
y: i32, y: i32,
width: i32, width: i32,
@ -192,12 +193,8 @@ where
{ {
let bounds = Rectangle::new(x as f32, y as f32, width as f32, height as f32); 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)); d.draw_rectangle_rec(bounds, widget_bg(&option == current));
let mouse_pos = d.get_mouse_position();
let mut changed = false; let mut changed = false;
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) if mouse.left_click() && mouse.is_over(bounds) && current != &option {
&& bounds.check_collision_point_rec(mouse_pos)
&& current != &option
{
*current = option; *current = option;
changed = true; changed = true;
} }
@ -206,6 +203,7 @@ where
pub fn text_input( pub fn text_input(
d: &mut RaylibDrawHandle, d: &mut RaylibDrawHandle,
mouse: &MouseInput,
bounds: Rectangle, bounds: Rectangle,
text: &mut String, text: &mut String,
is_selected: &mut bool, is_selected: &mut bool,
@ -235,11 +233,7 @@ pub fn text_input(
20, 20,
Color::WHITE, Color::WHITE,
); );
let mouse_pos = d.get_mouse_position(); if editable && mouse.left_click() && (mouse.is_over(bounds) || *is_selected) {
if editable
&& d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
&& (bounds.check_collision_point_rec(mouse_pos) || *is_selected)
{
*is_selected = !*is_selected; *is_selected = !*is_selected;
} }
@ -267,16 +261,17 @@ pub fn text_input(
changed changed
} }
pub fn texture_option_button<T>( pub fn scrollable_texture_option_button<T>(
d: &mut RaylibDrawHandle, d: &mut RaylibDrawHandle,
mouse: &MouseInput,
pos: Vector2, pos: Vector2,
texture: &Texture2D, texture: &Texture2D,
option: T, option: T,
current: &mut T, current: &mut T,
tex_size: f32, tex_size: f32,
border: f32, border: f32,
// tooltip ) -> Option<Scroll>
) where where
T: PartialEq, T: PartialEq,
{ {
let bounds = Rectangle { let bounds = Rectangle {
@ -300,13 +295,13 @@ pub fn texture_option_button<T>(
tex_size / texture.width as f32, tex_size / texture.width as f32,
Color::WHITE, Color::WHITE,
); );
if mouse.is_over(bounds) {
let mouse_pos = d.get_mouse_position(); if mouse.left_click() {
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
&& bounds.check_collision_point_rec(mouse_pos)
{
*current = option; *current = option;
} }
return mouse.scroll();
}
None
} }
pub fn draw_usize( pub fn draw_usize(