edit solution names

This commit is contained in:
Crispy 2024-10-06 13:12:05 +02:00
parent ed5084d0fd
commit 44b7d63cde
2 changed files with 39 additions and 8 deletions

View file

@ -24,6 +24,7 @@ struct Game {
textures: Textures, textures: Textures,
selected_level: usize, selected_level: usize,
selected_solution: usize, selected_solution: usize,
editing_solution_name: bool,
} }
fn main() { fn main() {
@ -56,6 +57,7 @@ impl Game {
textures, textures,
selected_level: 0, selected_level: 0,
selected_solution: 0, selected_solution: 0,
editing_solution_name: false,
} }
} }
@ -73,7 +75,8 @@ impl Game {
fn draw(&mut self, d: &mut RaylibDrawHandle) { fn draw(&mut self, d: &mut RaylibDrawHandle) {
d.clear_background(Color::new(64, 64, 64, 255)); d.clear_background(Color::new(64, 64, 64, 255));
let level_list_width = 320; // let level_list_width = 240;
let level_list_width = d.get_screen_width() / 4; // woah! Reactive UI! so fancy
let screen_height = d.get_screen_height(); let screen_height = d.get_screen_height();
d.draw_rectangle(0, 0, level_list_width, screen_height, Color::GRAY); d.draw_rectangle(0, 0, level_list_width, screen_height, Color::GRAY);
// let (a, b, c) = d.gui_scroll_panel( // let (a, b, c) = d.gui_scroll_panel(
@ -103,6 +106,7 @@ impl Game {
}; };
if clicked && bounds.check_collision_point_rec(mouse_pos) && self.selected_level != i { if clicked && bounds.check_collision_point_rec(mouse_pos) && self.selected_level != i {
self.selected_solution = 0; self.selected_solution = 0;
self.editing_solution_name = false;
self.selected_level = i; self.selected_level = i;
} }
if self.selected_level == i { if self.selected_level == i {
@ -130,16 +134,19 @@ impl Game {
let mut y = 60; let mut y = 60;
if let Some(solutions) = self.solutions.get_mut(level.id()) { if let Some(solutions) = self.solutions.get_mut(level.id()) {
let solution_entry_height = 40; let solution_entry_height = 40;
let solution_entry_width = 200;
for (solution_index, solution) in solutions.iter().enumerate() { for (solution_index, solution) in solutions.iter().enumerate() {
simple_option_button( if simple_option_button(
d, d,
level_list_width + 10, level_list_width + 10,
y, y,
200, solution_entry_width,
solution_entry_height, solution_entry_height,
solution_index, solution_index,
&mut self.selected_solution, &mut self.selected_solution,
); ) {
self.editing_solution_name = false;
}
let name_color = if solution.score.is_some() { let name_color = if solution.score.is_some() {
Color::LIME Color::LIME
} else { } else {
@ -156,8 +163,7 @@ impl Game {
y += solution_entry_height + 10; y += solution_entry_height + 10;
} }
// d.gui_button(bounds, text) if simple_button(d, level_list_width + 10, y, solution_entry_width, 30) {
if simple_button(d, level_list_width + 10, y, 200, 30) {
let n = solutions.len(); let n = solutions.len();
solutions.push(Solution::new(level.id().to_owned(), n)); solutions.push(Solution::new(level.id().to_owned(), n));
} }
@ -168,6 +174,21 @@ impl Game {
20, 20,
Color::WHITE, Color::WHITE,
); );
if let Some(solution) = solutions.get_mut(self.selected_solution) {
let bounds = Rectangle {
x: (level_list_width + 10 + solution_entry_width + 10) as f32,
y: 60.,
width: 240.,
height: 30.,
};
text_input(
d,
bounds,
&mut solution.name,
&mut self.editing_solution_name,
);
}
} else { } else {
self.solutions.insert(level.id().to_owned(), Vec::new()); self.solutions.insert(level.id().to_owned(), Vec::new());
} }

View file

@ -57,7 +57,8 @@ pub fn simple_option_button<T>(
height: i32, height: i32,
option: T, option: T,
current: &mut T, current: &mut T,
) where ) -> bool
where
T: PartialEq, T: PartialEq,
{ {
let color = if &option == current { let color = if &option == current {
@ -72,12 +73,16 @@ pub fn simple_option_button<T>(
height: height as f32, height: height as f32,
}; };
let mouse_pos = d.get_mouse_position(); let mouse_pos = d.get_mouse_position();
let mut changed = false;
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
&& bounds.check_collision_point_rec(mouse_pos) && bounds.check_collision_point_rec(mouse_pos)
&& current != &option
{ {
*current = option; *current = option;
changed = true;
} }
d.draw_rectangle_rec(bounds, color); d.draw_rectangle_rec(bounds, color);
changed
} }
pub fn text_input( pub fn text_input(
@ -94,8 +99,13 @@ pub fn text_input(
}; };
d.draw_rectangle_rec(bounds, border); d.draw_rectangle_rec(bounds, border);
d.draw_rectangle_rec(shrink_rec(bounds, 2.), bg); d.draw_rectangle_rec(shrink_rec(bounds, 2.), bg);
let drawn_text = if *is_selected {
&format!("{text}_")
} else {
&text
};
d.draw_text( d.draw_text(
text, drawn_text,
bounds.x as i32 + 4, bounds.x as i32 + 4,
bounds.y as i32 + 4, bounds.y as i32 + 4,
20, 20,