add button to clone solutions

This commit is contained in:
Crispy 2024-10-13 01:23:56 +02:00
parent 1ce5291777
commit 2720735a58
5 changed files with 42 additions and 29 deletions

View file

@ -203,10 +203,10 @@ impl Game {
solution_y += solution_entry_height + 10;
}
let next_id = get_free_id(solutions, Solution::id);
if simple_button(d, level_list_width + 10, solution_y, entry_width, 30) {
let n = solutions.len();
self.selected_solution = solutions.len();
solutions.push(Solution::new(level, n));
solutions.push(Solution::new(level, next_id));
}
d.draw_text(
"new solution",
@ -227,14 +227,23 @@ impl Game {
24,
true,
);
d.draw_text(solution.id(), column_x, y + 35, 10, Color::GRAY);
let id_text = format!("{}", solution.id());
d.draw_text(&id_text, column_x, y + 35, 10, Color::GRAY);
if simple_button(d, column_x, y + 50, 220, 30) {
let cloned = solution.new_copy(next_id);
self.selected_solution = solutions.len();
solutions.push(cloned);
return;
}
d.draw_text("clone", column_x + 5, y + 55, 20, Color::WHITE);
if simple_button(d, column_x, y + 85, 220, 30) {
let mut editor = Editor::new(solution.clone(), level.clone());
editor.center_view(d);
self.open_editor = Some(editor);
}
d.draw_text("edit", column_x + 5, y + 55, 20, Color::WHITE);
d.draw_text("edit", column_x + 5, y + 90, 20, Color::WHITE);
}
} else {
self.solutions.insert(level.id().to_owned(), Vec::new());
@ -263,7 +272,7 @@ fn get_levels() -> Vec<Level> {
add_level(d.path());
}
}
levels.sort_by_key(Level::sort_order);
levels.sort_unstable_by_key(Level::sort_order);
levels
}
@ -275,7 +284,6 @@ fn get_solutions() -> HashMap<String, Vec<Solution>> {
if dir.path().is_dir() {
let level_name = dir.file_name().to_string_lossy().to_string();
let mut solutions = Vec::new();
if let Ok(files) = read_dir(dir.path()) {
for file in files.flatten() {
let s = read_to_string(file.path())
@ -286,12 +294,11 @@ fn get_solutions() -> HashMap<String, Vec<Solution>> {
solutions.push(solution)
}
}
by_level.insert(level_name, solutions);
solutions.sort_unstable_by_key(Solution::id);
}
by_level.insert(level_name, solutions);
}
}
}
by_level
}