Skip to content

Commit

Permalink
feat(editor): Add BoxMesh with layer color as child of positionable i…
Browse files Browse the repository at this point in the history
…n editor to create 3D debug view
  • Loading branch information
piiertho committed Mar 25, 2024
1 parent e2924eb commit 76c2f8e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
15 changes: 7 additions & 8 deletions src/editor/inspector/layers_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ void LayersEditor::refresh() {
layer_controls_container->add_child(current_layer_name_label);
LayerColorPickerButton* color_picker_button {memnew(LayerColorPickerButton)};
color_picker_button->set_layer_id(layer_id);
color_picker_button->set_pick_color(
current_map->get_meta(
vformat(node::IsometricMap::LAYER_COLOR_META_NAME_FORMAT, layer_id),
Color()
)
);
const Variant& layer_color {
current_map->get_meta(vformat(node::IsometricMap::LAYER_COLOR_META_NAME_FORMAT, layer_id), Color())
};
color_picker_button->set_pick_color(layer_color);
current_map->set_layer_color(layer_id, layer_color);
layer_controls_container->add_child(color_picker_button);
LayerVisibleCheckBox* visible_check_box {memnew(LayerVisibleCheckBox)};
visible_check_box->set_layer_id(layer_id);
Expand Down Expand Up @@ -279,9 +278,9 @@ void LayerColorPickerButton::set_layer_id(const uint32_t p_layer_id) {
layer_id = p_layer_id;
}

void LayerColorPickerButton::on_color_changed(const Color& color) { // NOLINT(*-make-member-function-const)
void LayerColorPickerButton::on_color_changed(const Color& p_color) { // NOLINT(*-make-member-function-const)
if (node::IsometricMap* map{IsometricEditorPlugin::get_instance()->get_selected_map()}) {
map->set_meta(vformat(node::IsometricMap::LAYER_COLOR_META_NAME_FORMAT, layer_id), color);
map->set_layer_color(layer_id, p_color);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/editor/inspector/layers_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace editor {

public:
void set_layer_id(uint32_t p_layer_id);
void on_color_changed(const Color& color);
void on_color_changed(const Color& p_color);

LayerColorPickerButton();

Expand Down
24 changes: 24 additions & 0 deletions src/node/isometric_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ void IsometricMap::set_layer_visible(uint32_t p_layer_id, bool is_visible) {
}
}

void IsometricMap::set_layer_color(const uint32_t p_layer_id, const Color& p_color) {
set_meta(vformat(node::IsometricMap::LAYER_COLOR_META_NAME_FORMAT, p_layer_id), p_color);

if (!Engine::get_singleton()->is_editor_hint()) return;

const Vector<IsometricPositionable*>& instances {instances_grid_3d.get_internal_array()};
const Vector<uint32_t>& layer_assignations {layers_grid_3d.get_internal_array()};
for (int i = 0; i < instances.size(); ++i) {
if (IsometricPositionable* instance {instances[i]}) {
if (layer_assignations[i] != p_layer_id) continue;

instance->update_debug_mesh_color(p_color);
}
}
}

#endif

void IsometricMap::_enter_tree() {
Expand Down Expand Up @@ -204,6 +220,14 @@ void IsometricMap::add_positionable_as_child(int positionable_id, const Vector3&
add_child(positionable);

instances_grid_3d.insert_box({p_position, positionable->get_size()}, positionable);

#ifdef TOOLS_ENABLED
if (!Engine::get_singleton()->is_editor_hint()) return;

positionable->update_debug_mesh_color(
get_meta(vformat(LAYER_COLOR_META_NAME_FORMAT, layer_id),Color())
);
#endif
}
}

Expand Down
1 change: 1 addition & 0 deletions src/node/isometric_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ namespace node {
uint32_t get_last_layer_id() const;
void set_last_layer_id(uint32_t p_last_layer_id);
void set_layer_visible(uint32_t p_layer_id, bool is_visible);
void set_layer_color(uint32_t p_layer_id, const Color& p_color);
#endif

IsometricMap();
Expand Down
67 changes: 63 additions & 4 deletions src/node/isometric_positionable.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "isometric_positionable.h"

#include "editor/outline_drawer.h"
#include "isometric_map.h"
#include "isometric_server.h"
#include "utils/isometric_maths.h"
#include "isometric_string_names.h"
#include "utils/isometric_maths.h"


using namespace node;

Expand All @@ -14,6 +16,10 @@ IsometricPositionable::IsometricPositionable() :
collision_object(nullptr),
world_owner(false),
is_container {false}
#ifdef TOOLS_ENABLED
,
debug_mesh_instance_3d(nullptr)
#endif
{
set_process(true);
}
Expand Down Expand Up @@ -53,6 +59,23 @@ void IsometricPositionable::_enter_tree() {
ISOMETRIC_SERVER->space_attach_isometric_element(world, self);
}
update_position();

#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint() && !Object::cast_to<IsometricMap>(this)) {
debug_mesh_material.instantiate();
debug_mesh_material->set_albedo(Color());

debug_mesh.instantiate();
debug_mesh->set_material(debug_mesh_material);

debug_mesh_instance_3d = memnew(MeshInstance3D);
debug_mesh_instance_3d->set_mesh(debug_mesh);

add_child(debug_mesh_instance_3d);

_rebind_debug_mesh_instance();
}
#endif
}

void IsometricPositionable::_ready() {
Expand Down Expand Up @@ -84,9 +107,9 @@ void IsometricPositionable::update_position() {
const data::IsometricParameters* params = ISOMETRIC_SERVER->space_get_configuration(world);
Vector2 position2D = utils::from_3D_to_screen(*params, local_position);

Transform2D transform = get_transform();
transform.set_origin(position2D);
set_transform(transform);
Transform2D updated_transform = get_transform();
updated_transform.set_origin(position2D);
set_transform(updated_transform);
}
}

Expand All @@ -101,6 +124,10 @@ void IsometricPositionable::set_local_position_3d(Vector3 p_local) {
}
update_position();
_rebind_collision_object_position();

#ifdef TOOLS_ENABLED
_rebind_debug_mesh_instance();
#endif
}

Vector3 IsometricPositionable::get_global_position_3d() const {
Expand Down Expand Up @@ -131,6 +158,8 @@ void IsometricPositionable::set_size(Vector3 s) {
_rebind_collision_object_position();

#ifdef TOOLS_ENABLED
_rebind_debug_mesh_instance();

emit_signal(IsometricStringNames::get_singleton()->size_changed_signal);
#endif
}
Expand Down Expand Up @@ -202,6 +231,24 @@ void IsometricPositionable::_rebind_collision_object_position() const {
);
}

#ifdef TOOLS_ENABLED
void IsometricPositionable::_rebind_debug_mesh_instance() const {
if (!Engine::get_singleton()->is_editor_hint() || !debug_mesh_instance_3d) return;

const Vector3& global_position {get_global_position_3d()};
debug_mesh->set_size(size);
debug_mesh_instance_3d->set_global_transform(
{
{1, 0, 0, 0, 1, 0, 0, 0, 1},
{
Vector3(global_position.x, global_position.z, global_position.y) +
(size - Vector3(1, 1, 1)) * 0.5
}
}
);
}
#endif

RID IsometricPositionable::get_rid() const {
return self;
}
Expand All @@ -225,6 +272,12 @@ void IsometricPositionable::set_debug_view(bool p_debug) {
queue_redraw();
}

void IsometricPositionable::update_debug_mesh_color(const Color& p_color) const {
if (debug_mesh_material.is_null()) return;

debug_mesh_material->set_albedo(p_color);
}

#endif

void IsometricPositionable::_bind_methods() {
Expand Down Expand Up @@ -254,4 +307,10 @@ void IsometricPositionable::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_debug_view"), &IsometricPositionable::set_debug_view);
ADD_SIGNAL(MethodInfo("size_changed"));
#endif
}

IsometricPositionable::~IsometricPositionable() {
#ifdef TOOLS_ENABLED
debug_mesh_instance_3d = nullptr;
#endif
}
11 changes: 10 additions & 1 deletion src/node/isometric_positionable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "editor/outline_data.h"
#include "scene/2d/node_2d.h"
#include "scene/3d/collision_object_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/resources/primitive_meshes.h"

namespace node {
class IsometricPositionable : public Node2D {
Expand Down Expand Up @@ -39,6 +41,12 @@ namespace node {
#ifdef TOOLS_ENABLED
bool debug_view = false;
editor::OutlineData outline_data;

MeshInstance3D* debug_mesh_instance_3d;
Ref<BoxMesh> debug_mesh;
Ref<StandardMaterial3D> debug_mesh_material;

void _rebind_debug_mesh_instance() const;
#endif

protected:
Expand All @@ -57,7 +65,7 @@ namespace node {

public:
IsometricPositionable();
~IsometricPositionable() override = default;
~IsometricPositionable() override;

Vector3 get_local_position_3d() const;
void set_local_position_3d(Vector3 p_local);
Expand All @@ -79,6 +87,7 @@ namespace node {
void set_debug_view(bool p_debug);

editor::OutlineData& get_outline_data();
void update_debug_mesh_color(const Color& p_color) const;
#endif

protected:
Expand Down

0 comments on commit 76c2f8e

Please # to comment.