Skip to content

Commit

Permalink
drawing modes for tile maps (wip)
Browse files Browse the repository at this point in the history
- various api improvements
  • Loading branch information
mrDIMAS committed Jul 21, 2024
1 parent f824866 commit 2577c9c
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 21 deletions.
Binary file added editor/resources/brush.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/resources/eraser.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/resources/fill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified editor/resources/grid-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/resources/pipette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added editor/resources/rect_fill.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion editor/src/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,17 @@ pub trait InteractionMode: BaseInteractionMode {

fn on_drop(&mut self, _engine: &mut Engine) {}

fn on_hot_key(
fn on_hot_key_pressed(
&mut self,
#[allow(unused_variables)] hotkey: &HotKey,
#[allow(unused_variables)] controller: &mut dyn SceneController,
#[allow(unused_variables)] engine: &mut Engine,
#[allow(unused_variables)] settings: &Settings,
) -> bool {
false
}

fn on_hot_key_released(
&mut self,
#[allow(unused_variables)] hotkey: &HotKey,
#[allow(unused_variables)] controller: &mut dyn SceneController,
Expand Down Expand Up @@ -283,6 +293,16 @@ impl InteractionModeContainer {
.map(|mode| &mut **mode)
}

pub fn of_type<T: InteractionMode + TypeUuidProvider>(&self) -> Option<&T> {
self.get(&T::type_uuid())
.and_then(|mode| mode.as_any().downcast_ref())
}

pub fn of_type_mut<T: InteractionMode + TypeUuidProvider>(&mut self) -> Option<&mut T> {
self.get_mut(&T::type_uuid())
.and_then(|mode| mode.as_any_mut().downcast_mut())
}

pub fn drain(&mut self) -> impl Iterator<Item = Box<dyn InteractionMode>> + '_ {
self.try_notify_changed();
self.container.drain(..)
Expand Down
2 changes: 1 addition & 1 deletion editor/src/interaction/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl InteractionMode for TerrainInteractionMode {
));
}

fn on_hot_key(
fn on_hot_key_pressed(
&mut self,
hotkey: &HotKey,
_controller: &mut dyn SceneController,
Expand Down
21 changes: 20 additions & 1 deletion editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ impl Editor {
.current_interaction_mode
.and_then(|current_mode| scene.interaction_modes.get_mut(&current_mode))
{
processed |= current_interaction_mode.on_hot_key(
processed |= current_interaction_mode.on_hot_key_pressed(
&hot_key,
&mut *scene.controller,
engine,
Expand Down Expand Up @@ -1125,6 +1125,25 @@ impl Editor {
}
}
}
} else if let Some(WidgetMessage::KeyUp(key)) = message.data() {
let hot_key = HotKey::Some {
code: *key,
modifiers,
};

if let Some(scene) = self.scenes.current_scene_entry_mut() {
if let Some(current_interaction_mode) = scene
.current_interaction_mode
.and_then(|current_mode| scene.interaction_modes.get_mut(&current_mode))
{
current_interaction_mode.on_hot_key_released(
&hot_key,
&mut *scene.controller,
engine,
&self.settings,
);
}
}
}
}

Expand Down
109 changes: 100 additions & 9 deletions editor/src/plugins/tilemap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod preview;
pub mod tile_set_import;
pub mod tileset;

use crate::plugins::tilemap::preview::TileSetPreview;
use crate::{
command::SetPropertyCommand,
fyrox::{
Expand All @@ -27,19 +26,24 @@ use crate::{
scene::{
debug::Line,
node::Node,
tilemap::tileset::TileSet,
tilemap::{brush::TileMapBrush, TileMap, Tiles},
Scene,
},
},
interaction::{make_interaction_mode_button, InteractionMode},
message::MessageSender,
plugin::EditorPlugin,
plugins::tilemap::{palette::PaletteMessage, panel::TileMapPanel, tileset::TileSetEditor},
plugins::tilemap::{
palette::PaletteMessage, panel::TileMapPanel, preview::TileSetPreview,
tileset::TileSetEditor,
},
scene::{commands::GameSceneContext, controller::SceneController, GameScene, Selection},
settings::Settings,
Editor, Message,
};
use fyrox::scene::tilemap::tileset::TileSet;
use fyrox::gui::key::HotKey;
use fyrox::gui::message::KeyCode;
use std::sync::Arc;

fn make_button(
Expand All @@ -60,19 +64,32 @@ fn make_button(
.build(ctx)
}

pub enum DrawingMode {
Draw,
Erase,
FloodFill,
Pick {
click_grid_position: Option<Vector2<i32>>,
},
RectFill {
click_grid_position: Option<Vector2<i32>>,
},
}

struct InteractionContext {
previous_tiles: Tiles,
}

#[derive(TypeUuidProvider)]
#[type_uuid(id = "33fa8ef9-a29c-45d4-a493-79571edd870a")]
pub struct TileMapInteractionMode {
#[allow(dead_code)]
tile_map: Handle<Node>,
brush: Arc<Mutex<TileMapBrush>>,
brush_position: Vector2<i32>,
interaction_context: Option<InteractionContext>,
sender: MessageSender,
#[allow(dead_code)]
drawing_mode: DrawingMode,
}

impl TileMapInteractionMode {
Expand Down Expand Up @@ -286,6 +303,70 @@ impl InteractionMode for TileMapInteractionMode {
fn uuid(&self) -> Uuid {
Self::type_uuid()
}

fn on_hot_key_pressed(
&mut self,
hotkey: &HotKey,
_controller: &mut dyn SceneController,
_engine: &mut Engine,
_settings: &Settings,
) -> bool {
if let HotKey::Some { code, .. } = hotkey {
match *code {
KeyCode::AltLeft => {
self.drawing_mode = DrawingMode::Pick {
click_grid_position: None,
};
return true;
}
KeyCode::ShiftLeft => {
self.drawing_mode = DrawingMode::Erase;
return true;
}
KeyCode::ControlLeft => {
self.drawing_mode = DrawingMode::RectFill {
click_grid_position: None,
};
return true;
}
_ => (),
}
}
false
}

fn on_hot_key_released(
&mut self,
hotkey: &HotKey,
_controller: &mut dyn SceneController,
_engine: &mut Engine,
_settings: &Settings,
) -> bool {
if let HotKey::Some { code, .. } = hotkey {
match *code {
KeyCode::AltLeft => {
if matches!(self.drawing_mode, DrawingMode::Pick { .. }) {
self.drawing_mode = DrawingMode::Draw;
return true;
}
}
KeyCode::ShiftLeft => {
if matches!(self.drawing_mode, DrawingMode::Erase) {
self.drawing_mode = DrawingMode::Draw;
return true;
}
}
KeyCode::ControlLeft => {
if matches!(self.drawing_mode, DrawingMode::RectFill { .. }) {
self.drawing_mode = DrawingMode::Draw;
return true;
}
}
_ => (),
}
}
false
}
}

#[derive(Default)]
Expand Down Expand Up @@ -359,10 +440,11 @@ impl EditorPlugin for TileMapEditorPlugin {
}
}

let tile_map = editor
.scenes
.current_scene_entry_mut()
.and_then(|entry| entry.controller.downcast_mut::<GameScene>())
let editor_scene_entry = editor.scenes.current_scene_entry_mut();

let tile_map = editor_scene_entry
.as_ref()
.and_then(|entry| entry.controller.downcast_ref::<GameScene>())
.and_then(|scene| {
editor.engine.scenes[scene.scene]
.graph
Expand All @@ -375,14 +457,22 @@ impl EditorPlugin for TileMapEditorPlugin {
self.tile_map,
tile_map,
&editor.message_sender,
editor_scene_entry,
);
}
}

fn on_update(&mut self, _editor: &mut Editor) {
fn on_update(&mut self, editor: &mut Editor) {
if let Some(tile_set_editor) = self.tile_set_editor.as_mut() {
tile_set_editor.update();
}

if let Some(panel) = self.panel.as_mut() {
panel.update(
editor.engine.user_interfaces.first(),
editor.scenes.current_scene_entry_ref(),
);
}
}

fn on_message(&mut self, message: &Message, editor: &mut Editor) {
Expand Down Expand Up @@ -430,6 +520,7 @@ impl EditorPlugin for TileMapEditorPlugin {
brush_position: Default::default(),
interaction_context: None,
sender: editor.message_sender.clone(),
drawing_mode: DrawingMode::Draw,
});

self.panel = Some(TileMapPanel::new(
Expand Down
Loading

0 comments on commit 2577c9c

Please sign in to comment.