Skip to content

Commit

Permalink
feat: Add layers to IsometricMap and basic features linked (set visib…
Browse files Browse the repository at this point in the history
…ility, add/remove)
  • Loading branch information
piiertho committed Jan 3, 2024
1 parent 9cd95ef commit 6a854c4
Show file tree
Hide file tree
Showing 22 changed files with 740 additions and 49 deletions.
6 changes: 5 additions & 1 deletion demo/dojo/Dojo.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://b4066x3op6sr6"]

[ext_resource type="PackedScene" path="res://maps/dojomap.tscn" id="1"]
[ext_resource type="PackedScene" uid="uid://d2fbdalmi5y2f" path="res://maps/dojomap.tscn" id="1"]
[ext_resource type="Script" path="res://DojoCamera.gd" id="2"]
[ext_resource type="PackedScene" uid="uid://bsdp741c2enwi" path="res://Character.tscn" id="3"]
[ext_resource type="PackedScene" path="res://maps/paradox.tscn" id="4"]
Expand All @@ -20,6 +20,10 @@ zoom = Vector2(2, 2)
[node name="IsometricMap" parent="IsometricMap" instance=ExtResource("4")]
position = Vector2(0, 1635.23)
local_position_3d = Vector3(14, 14, 1)
layers_grid_3d = [0, 0, 0, 0, 0, 0, 0, 0]
layers = {
0: "Default"
}

[node name="CanvasLayer" type="CanvasLayer" parent="."]
script = ExtResource("2")
Expand Down
15 changes: 10 additions & 5 deletions demo/maps/dojomap.tscn

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ config_version=5

config/name="test-isometric-module"
run/main_scene="res://dojo/Dojo.tscn"
config/features=PackedStringArray("4.1")
config/features=PackedStringArray("4.2")
config/icon="res://icon.png"

[input]
Expand Down
6 changes: 6 additions & 0 deletions register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "editor/isometric_editor_plugin.h"
#include "editor/isometric_tile_editor_plugin.h"
#include "editor/positionable_set_editor_plugin.h"
#include "editor/inspector/layers_editor.h"

#endif

#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -55,6 +57,10 @@ void initialize_isometric_maps_module(ModuleInitializationLevel p_level) {
EditorPlugins::add_create_func(isometric_tile_editor_plugin_creator_func);
ClassDB::register_class<editor::inspector::PositionableSelectionPane>();
ClassDB::register_class<editor::inspector::PositionableSetEditor>();
ClassDB::register_class<editor::inspector::LayersEditor>();
ClassDB::register_class<editor::inspector::LayerVisibleCheckBox>();
ClassDB::register_class<editor::inspector::LayerRemoveButton>();
ClassDB::register_class<editor::inspector::CurrentLayerCheckBox>();
}
#endif
}
Expand Down
31 changes: 27 additions & 4 deletions src/containers/grid_3d.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef ISOMETRIC_MAPS_GRID_3D_H
#define ISOMETRIC_MAPS_GRID_3D_H

#include "core/templates/vector.h"
#include <core/templates/vector.h>
#include <core/variant/array.h>

#include <core/math/aabb.h>
#include <core/math/vector3.h>
#include <core/math/vector3i.h>

namespace containers {

Expand Down Expand Up @@ -34,7 +36,7 @@ namespace containers {

void update_array_size(const Vector3& size, bool reset = false);

Vector3 get_dimensions();
Vector3i get_dimensions();

void reset();

Expand Down Expand Up @@ -69,6 +71,9 @@ namespace containers {
void set_internal_array(const Vector<T>& array);

static inline T get_default_value();

Array to_array() const;
void from_array(const Array& array);
};

template<class T, T default_value>
Expand Down Expand Up @@ -104,8 +109,8 @@ namespace containers {
}

template<class T, T default_value>
Vector3 Grid3D<T, default_value>::get_dimensions() {
return {static_cast<float>(width), static_cast<float>(depth), static_cast<float>(height)};
Vector3i Grid3D<T, default_value>::get_dimensions() {
return {width, depth, height};
}

template<class T, T default_value>
Expand Down Expand Up @@ -267,6 +272,24 @@ namespace containers {
return default_value;
}

template<class T, T default_value>
Array Grid3D<T, default_value>::to_array() const {
Array ret;
for (int i = 0; i < internal_array.size(); ++i) {
ret.append(internal_array[i]);
}
return ret;
}

template<class T, T default_value>
void Grid3D<T, default_value>::from_array(const Array& array) {
Vector<T> new_internal_array;
for (int i = 0; i < array.size(); ++i) {
new_internal_array.push_back(array[i]);
}
set_internal_array(new_internal_array);
}

template<class T, T default_value>
Grid3D<T, default_value>::Grid3D() {
update_array_size(Vector3(1, 1, 1));
Expand Down
33 changes: 33 additions & 0 deletions src/editor/commands/add_layer_command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifdef TOOLS_ENABLED

#include "add_layer_command.h"
#include "node/isometric_map.h"
#include "editor/isometric_editor_plugin.h"

using namespace editor::commands;

void AddLayerCommand::redo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->add_layer(layer_name, layer_id);
}

void AddLayerCommand::undo() {
if (layer_id == node::IsometricMap::NO_LAYER_ID) {
IsometricEditorPlugin::get_instance()->get_selected_map()->remove_layer(layer_name);
return;
}
IsometricEditorPlugin::get_instance()->get_selected_map()->remove_layer(layer_id);
}

void AddLayerCommand::set_layer_id(uint32_t p_layer_id) {
layer_id = p_layer_id;
}

void AddLayerCommand::set_layer_name(const String& p_layer_name) {
layer_name = p_layer_name;
}

AddLayerCommand::AddLayerCommand() : layer_id(node::IsometricMap::NO_LAYER_ID) {

}

#endif
31 changes: 31 additions & 0 deletions src/editor/commands/add_layer_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef ISOMETRIC_MAPS_ADD_LAYER_COMMAND_H
#define ISOMETRIC_MAPS_ADD_LAYER_COMMAND_H


#ifdef TOOLS_ENABLED

#include "command.h"

namespace editor {
namespace commands {
class AddLayerCommand : public Command {
public:
void redo() override;
void undo() override;

void set_layer_id(uint32_t p_layer_id);
void set_layer_name(const String& p_layer_name);

AddLayerCommand();
~AddLayerCommand() override = default;

private:
uint32_t layer_id;
String layer_name;
};
}
}
#endif


#endif //ISOMETRIC_MAPS_ADD_LAYER_COMMAND_H
13 changes: 11 additions & 2 deletions src/editor/commands/add_positionable_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
using namespace editor::commands;

void AddPositionableCommand::redo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->add_positionable_if_nothing_present(aabb, positionable_id);
IsometricEditorPlugin::get_instance()->get_selected_map()->add_positionable_if_nothing_present(
aabb,
positionable_id,
layer_id
);
}

void AddPositionableCommand::undo() {
Expand All @@ -21,7 +25,12 @@ void AddPositionableCommand::set_positionable_id(int id) {
positionable_id = id;
}

void AddPositionableCommand::set_layer_id(int p_layer_id) {
layer_id = p_layer_id;
}

AddPositionableCommand::AddPositionableCommand() :
positionable_id(resource::PositionableSet::NONE_POSITIONABLE_ID) {}
positionable_id(resource::PositionableSet::NONE_POSITIONABLE_ID),
layer_id(node::IsometricMap::DEFAULT_LAYER_ID) {}

#endif
2 changes: 2 additions & 0 deletions src/editor/commands/add_positionable_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ namespace editor {

void set_aabb(const AABB& p_aabb);
void set_positionable_id(int id);
void set_layer_id(int p_layer_id);

AddPositionableCommand();
~AddPositionableCommand() override = default;

private:
AABB aabb;
int positionable_id;
uint32_t layer_id;
};
}// namespace commands
}// namespace editor
Expand Down
20 changes: 6 additions & 14 deletions src/editor/commands/emitters/command_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../command.h"
#include "core/input/input_event.h"
#include "editor/editor_undo_redo_manager.h"
#include "command_to_action_transformer.h"
#include <scene/main/node.h>

namespace editor {
Expand All @@ -31,20 +32,11 @@ namespace editor {
return;
}

Vector<Ref<Command>> commands {from_gui_input_to_command(event)};

bool has_valid_command {false};
for (int i = 0; i < commands.size(); ++i) {
Ref<Command> command {commands[i]};
if (command.is_null()) { continue; }
if (!has_valid_command) {
EditorUndoRedoManager::get_singleton()->create_action(action_title, merge_mode, p_context);
has_valid_command = true;
}
command->append_to_undoredo();
}

if (has_valid_command) { EditorUndoRedoManager::get_singleton()->commit_action(); }
CommandToActionTransformer action_transformer;
action_transformer.transform<action_title, merge_mode>(
from_gui_input_to_command(event),
p_context
);
}

template<class Derived, class Evt, const char* action_title, UndoRedo::MergeMode merge_mode>
Expand Down
45 changes: 45 additions & 0 deletions src/editor/commands/emitters/command_to_action_transformer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef ISOMETRIC_MAPS_COMMAND_TO_ACTION_TRANSFORMER_H
#define ISOMETRIC_MAPS_COMMAND_TO_ACTION_TRANSFORMER_H

#ifdef TOOLS_ENABLED

#include <core/templates/vector.h>
#include <core/object/ref_counted.h>
#include <editor/commands/command.h>
#include <scene/main/node.h>

namespace editor {
namespace commands {
namespace emitters {
class CommandToActionTransformer {
public:
template<const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE>
void transform(const Vector<Ref<Command>> commands, Node* p_context);

CommandToActionTransformer() = default;
~CommandToActionTransformer() = default;
};

template<const char* action_title, UndoRedo::MergeMode merge_mode>
void CommandToActionTransformer::transform(const Vector<Ref<Command>> commands, Node* p_context) {
bool has_valid_command {false};
for (int i = 0; i < commands.size(); ++i) {
Ref<Command> command {commands[i]};
if (command.is_null()) { continue; }
if (!has_valid_command) {
EditorUndoRedoManager::get_singleton()->create_action(action_title, merge_mode, p_context);
has_valid_command = true;
}
command->append_to_undoredo();
}

if (has_valid_command) { EditorUndoRedoManager::get_singleton()->commit_action(); }
}
}
}
}

#endif


#endif //ISOMETRIC_MAPS_COMMAND_TO_ACTION_TRANSFORMER_H
3 changes: 2 additions & 1 deletion src/editor/commands/emitters/delete_command_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Vector<Ref<editor::commands::Command>> DeleteCommandEmitter::from_gui_input_to_c

for (int i = 0; i < selected_positions.size(); ++i) {
const Vector3& position {selected_positions[i]};
if (node::IsometricPositionable * current {map->get_positionable_at(position)}) {
if (node::IsometricPositionable* current {map->get_positionable_at(position)}) {
const Vector3& local_position {current->get_local_position_3d()};

Ref<SelectPositionableCommand> select_command;
Expand All @@ -39,6 +39,7 @@ Vector<Ref<editor::commands::Command>> DeleteCommandEmitter::from_gui_input_to_c

Ref<AddPositionableCommand> add_command;
add_command.instantiate();
add_command->set_layer_id(map->get_layer_id_at(position));
add_command->set_aabb({local_position, current->get_size()});
add_command->set_positionable_id(map->get_positionable_id_for_position(local_position));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Vector<Ref<editor::commands::Command>> DragAndDropCommandEmitter::from_gui_input
add_command.instantiate();
add_command->set_aabb({all_positions[i], positionable_size});
add_command->set_positionable_id(selected_tile_id);
add_command->set_layer_id(IsometricEditorPlugin::get_instance()->get_selected_layer());
commands.push_back(add_command);
}

Expand Down
1 change: 1 addition & 0 deletions src/editor/commands/emitters/painting_command_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Vector<Ref<editor::commands::Command>> PaintingCommandEmitter::from_gui_input_to
add_command.instantiate();
add_command->set_aabb(aabb);
add_command->set_positionable_id(selected_tile_id);
add_command->set_layer_id(IsometricEditorPlugin::get_instance()->get_selected_layer());
commands.push_back(add_command);
return commands;
}
Expand Down
28 changes: 28 additions & 0 deletions src/editor/commands/set_layer_visibility_command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifdef TOOLS_ENABLED

#include "set_layer_visibility_command.h"
#include <editor/isometric_editor_plugin.h>

using namespace editor::commands;

void SetLayerVisibilityCommand::redo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->set_layer_visible(layer_id, is_visible);
}

void SetLayerVisibilityCommand::undo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->set_layer_visible(layer_id, !is_visible);
}

void SetLayerVisibilityCommand::set_layer_id(uint32_t p_layer_id) {
layer_id = p_layer_id;
}

void SetLayerVisibilityCommand::set_visible(bool p_is_visible) {
is_visible = p_is_visible;
}

SetLayerVisibilityCommand::SetLayerVisibilityCommand() : layer_id(node::IsometricMap::DEFAULT_LAYER_ID), is_visible() {

}

#endif
30 changes: 30 additions & 0 deletions src/editor/commands/set_layer_visibility_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef ISOMETRIC_MAPS_SET_LAYER_VISIBILITY_COMMAND_H
#define ISOMETRIC_MAPS_SET_LAYER_VISIBILITY_COMMAND_H

#ifdef TOOLS_ENABLED

#include "command.h"

namespace editor {
namespace commands {
class SetLayerVisibilityCommand : public Command {
public:
void redo() override;
void undo() override;

void set_layer_id(uint32_t p_layer_id);
void set_visible(bool p_is_visible);

SetLayerVisibilityCommand();
~SetLayerVisibilityCommand() override = default;

private:
uint32_t layer_id;
bool is_visible;
};
}
}

#endif

#endif //ISOMETRIC_MAPS_SET_LAYER_VISIBILITY_COMMAND_H
Loading

0 comments on commit 6a854c4

Please sign in to comment.