Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change draw order via drag & drop #15

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ clap = { version = "4.5.23", features = ["derive"] }
confy = "0.6.1"
eframe = {version = "0.31.0", features = ["wgpu"]}
egui_extras = { version = "0.31.0", features = ["all_loaders", "serde"] }
egui_dnd = "0.12.0"
egui-file-dialog = "0.9.0"
egui_kittest = { version = "0.31.0", features = ["wgpu", "snapshot", "eframe"] }
egui_tiles = "0.12.0"
Expand Down
4 changes: 4 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use crate::grid_options::GridOptions;
pub use crate::lens::LensOptions;
pub use crate::value_colormap::ColorMap;

use crate::draw_order::DrawOrder;
use crate::map_state::MapState;
use crate::meta::Meta;
use crate::persistence::{save_app_options, PersistenceOptions};
Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct StatusInfo {
pub hover_position: Option<egui::Pos2>,
pub quit_modal_active: bool,
pub debug_window_active: bool,
pub draw_order_edit_active: bool,
pub unsaved_changes: bool,
pub quit_after_save: bool,
pub move_action: Option<String>,
Expand All @@ -78,6 +80,8 @@ pub struct StatusInfo {
#[derive(Default, Serialize, Deserialize)]
pub struct SessionData {
pub maps: BTreeMap<String, MapState>,
#[serde(skip)]
pub draw_order: DrawOrder,
pub grid_lenses: HashMap<String, egui::Pos2>,
}

Expand Down
13 changes: 9 additions & 4 deletions src/app_impl/central_panel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use eframe::egui;
use log::debug;
use log::{debug, error};
use uuid::Uuid;

use crate::app::{ActiveTool, AppState, ViewMode};
Expand Down Expand Up @@ -41,7 +41,12 @@ impl AppState {
),
);
self.status.active_tool = None;
for (name, map) in self.data.maps.iter_mut() {
for name in self.data.draw_order.keys() {
let Some(map) = self.data.maps.get_mut(name) else {
error!("Unknown draw order key: {}", name);
continue;
};

if !map.visible {
continue;
}
Expand Down Expand Up @@ -94,7 +99,7 @@ impl AppState {
}

let grid = Grid::new(ui, "main_grid", options.scale).with_origin_offset(options.offset);
grid.show_maps(ui, &mut self.data.maps, &options);
grid.show_maps(ui, &mut self.data.maps, &options, &self.data.draw_order);
if options.lines_visible {
grid.draw(ui, options, LineType::Main);
}
Expand Down Expand Up @@ -195,7 +200,7 @@ impl AppState {
window.show(ui.ctx(), |ui| {
if let Some(center_pos) = center_pos {
let mini_grid = Grid::new(ui, id, grid_lens_scale).centered_at(center_pos);
mini_grid.show_maps(ui, &mut self.data.maps, &options);
mini_grid.show_maps(ui, &mut self.data.maps, &options, &self.data.draw_order);
if options.lines_visible {
mini_grid.draw(ui, options, LineType::Main);
}
Expand Down
2 changes: 2 additions & 0 deletions src/app_impl/load_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl AppState {
use_value_interpretation: use_interpretation,
},
);
self.data.draw_order.add(name.clone());
info!("Loaded map: {}", name);
self.status.unsaved_changes = true;
Ok(())
Expand All @@ -142,6 +143,7 @@ impl AppState {
for name in to_delete {
info!("Removing {}", name);
self.data.maps.remove(name);
self.data.draw_order.remove(name);
self.tile_manager.remove_pane(name);
if let Some(active_tool) = &self.status.active_tool {
if active_tool == name {
Expand Down
8 changes: 7 additions & 1 deletion src/app_impl/menu_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ impl AppState {
egui::CollapsingHeader::new("List")
.default_open(true)
.show(ui, |ui| {
self.maps_list(ui);
ui.toggle_value(&mut self.status.draw_order_edit_active, "⬆⬇")
.on_hover_text("Click to view and edit the draw order via drag and drop.");
if self.status.draw_order_edit_active {
self.data.draw_order.ui(ui);
} else {
self.maps_list(ui);
}
});

if self.data.maps.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions src/app_impl/pose_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct PoseEditOptions {
pub selected_map: String,
pub edit_root_frame: bool,
pub edit_map_frame: bool,
#[serde(skip)]
pub movable_amounts: MovableAmounts,
}

Expand Down
33 changes: 33 additions & 0 deletions src/draw_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use eframe::egui;
use egui_dnd::dnd;
use serde::{Deserialize, Serialize};

#[derive(Default, Serialize, Deserialize)]
pub struct DrawOrder {
keys: Vec<String>,
}

impl DrawOrder {
pub fn add(&mut self, name: String) {
self.keys.push(name);
}

pub fn remove(&mut self, name: &str) {
self.keys.retain(|x| x != name);
}

pub fn keys(&self) -> &Vec<String> {
self.keys.as_ref()
}

pub fn ui(&mut self, ui: &mut egui::Ui) {
dnd(ui, "draw_order").show_vec(&mut self.keys, |ui, item, handle, state| {
ui.horizontal(|ui| {
handle.ui(ui, |ui| {
ui.label(egui::RichText::new(state.index.to_string()).strong());
ui.label(item.clone());
});
});
});
}
}
11 changes: 9 additions & 2 deletions src/grid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::BTreeMap;

use eframe::egui;
use log::error;

use crate::draw_order::DrawOrder;
use crate::grid_options::{GridLineDimension, GridOptions, LineType};
use crate::map_pose::MapPose;
use crate::map_state::MapState;
Expand Down Expand Up @@ -142,9 +144,14 @@ impl Grid {
ui: &mut egui::Ui,
maps: &mut BTreeMap<String, MapState>,
options: &GridOptions,
draw_order: &DrawOrder,
) {
for (name, map) in maps.iter_mut() {
self.show_map(ui, map, name, options);
for name in draw_order.keys() {
if let Some(map) = maps.get_mut(name) {
self.show_map(ui, map, name, options);
} else {
error!("Unknown draw order key: {}", name);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod app;
mod app_impl;
mod draw_order;
mod grid;
mod grid_options;
mod image;
Expand Down