From 49d2b181a1c30538382df56d1e2048abf7732d8c Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 12 Apr 2022 21:46:03 +0200 Subject: [PATCH] Initial logic port of mdr ed's uv editor. --- modules/mesh_data_resource/SCsub | 6 + .../utilities/mdr_ed_mesh_decompose.cpp | 6 +- .../editor/utilities/mdr_ed_mesh_decompose.h | 2 +- .../editor/uv_editor/mdi_ed_uv_editor.cpp | 100 +-- .../editor/uv_editor/mdr_uv_rect_editor.cpp | 4 +- .../editor/uv_editor/mdr_uv_rect_view.cpp | 311 ++++----- .../editor/uv_editor/mdr_uv_rect_view.h | 5 +- .../uv_editor/mdr_uv_rect_view_node.cpp | 653 ++++++++++-------- 8 files changed, 575 insertions(+), 512 deletions(-) diff --git a/modules/mesh_data_resource/SCsub b/modules/mesh_data_resource/SCsub index cac0aaab8..089ecd519 100644 --- a/modules/mesh_data_resource/SCsub +++ b/modules/mesh_data_resource/SCsub @@ -31,6 +31,12 @@ if 'TOOLS_ENABLED' in env["CPPDEFINES"]: module_env.add_source_files(env.modules_sources,"editor/utilities/mdr_ed_mesh_outline.cpp") module_env.add_source_files(env.modules_sources,"editor/utilities/mdr_ed_mesh_utils.cpp") + module_env.add_source_files(env.modules_sources,"editor/uv_editor/mdi_ed_uv_editor.cpp") + module_env.add_source_files(env.modules_sources,"editor/uv_editor/mdr_uv_rect_editor.cpp") + module_env.add_source_files(env.modules_sources,"editor/uv_editor/mdr_uv_rect_view_node.cpp") + module_env.add_source_files(env.modules_sources,"editor/uv_editor/mdr_uv_rect_view_popup.cpp") + module_env.add_source_files(env.modules_sources,"editor/uv_editor/mdr_uv_rect_view.cpp") + module_env.add_source_files(env.modules_sources,"nodes/mesh_data_instance.cpp") module_env.add_source_files(env.modules_sources,"nodes/mesh_data_instance_2d.cpp") diff --git a/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.cpp b/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.cpp index 8cb364319..d7748b578 100644 --- a/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.cpp +++ b/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.cpp @@ -321,8 +321,8 @@ bool MDREDMeshDecompose::pool_int_arr_contains(const PoolIntArray &arr, const in return false; } -Array MDREDMeshDecompose::partition_mesh(Ref mdr) { - Array partitions; +Vector MDREDMeshDecompose::partition_mesh(Ref mdr) { + Vector partitions; Array arrays = mdr->get_array(); @@ -386,7 +386,7 @@ Array MDREDMeshDecompose::partition_mesh(Ref mdr) { } } - partitions.append(partition); + partitions.push_back(partition); } return partitions; diff --git a/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.h b/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.h index 5192b3ae6..23e76f719 100644 --- a/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.h +++ b/modules/mesh_data_resource/editor/utilities/mdr_ed_mesh_decompose.h @@ -46,7 +46,7 @@ public: static Array get_handle_face_to_vertex_map(const Array &arrays); static PoolVector3Array calculate_map_midpoints(Array mesh, Array vertex_map); static bool pool_int_arr_contains(const PoolIntArray &arr, const int val); - static Array partition_mesh(Ref mdr); + static Vector partition_mesh(Ref mdr); }; #endif diff --git a/modules/mesh_data_resource/editor/uv_editor/mdi_ed_uv_editor.cpp b/modules/mesh_data_resource/editor/uv_editor/mdi_ed_uv_editor.cpp index c78c9d756..249874ed5 100644 --- a/modules/mesh_data_resource/editor/uv_editor/mdi_ed_uv_editor.cpp +++ b/modules/mesh_data_resource/editor/uv_editor/mdi_ed_uv_editor.cpp @@ -22,74 +22,84 @@ SOFTWARE. #include "mdi_ed_uv_editor.h" +#include "../../mesh_data_resource.h" +#include "../../nodes/mesh_data_instance.h" +#include "scene/resources/texture.h" + void MDIEdUVEditor::set_mesh_data_resource(Ref a) { - /* - if mesh_data_resource: - mesh_data_resource.disconnect("changed", self, "on_mdr_changed") + if (mesh_data_resource.is_valid()) { + mesh_data_resource->disconnect("changed", this, "on_mdr_changed"); + } - mesh_data_resource = a + mesh_data_resource = a; - if mesh_data_resource: - mesh_data_resource.connect("changed", self, "on_mdr_changed") + if (mesh_data_resource.is_valid()) { + mesh_data_resource->connect("changed", this, "on_mdr_changed"); + } - update() - */ + update(); } + void MDIEdUVEditor::set_mesh_data_instance(MeshDataInstance *a) { - /* + if (!a) { + background_texture.unref(); + return; + } - if !a: - background_texture = null - - background_texture = a.texture - - */ + background_texture = a->get_texture(); } + void MDIEdUVEditor::on_mdr_changed() { update(); } + void MDIEdUVEditor::_draw() { - /* + if (background_texture.is_valid()) { + draw_texture_rect_region(background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), background_texture->get_size())); + } - if background_texture: - draw_texture_rect_region(background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), background_texture.get_size())) + if (!mesh_data_resource.is_valid()) { + return; + } - if !mesh_data_resource: - return + Array arr = mesh_data_resource->get_array(); - if mesh_data_resource.array.size() != ArrayMesh.ARRAY_MAX: - return + if (arr.size() != ArrayMesh::ARRAY_MAX) { + return; + } - var uvs : PoolVector2Array = mesh_data_resource.array[ArrayMesh.ARRAY_TEX_UV] - var indices : PoolIntArray = mesh_data_resource.array[ArrayMesh.ARRAY_INDEX] + PoolVector2Array uvs = arr[ArrayMesh::ARRAY_TEX_UV]; + PoolIntArray indices = arr[ArrayMesh::ARRAY_INDEX]; - if indices.size() % 3 == 0: - for i in range(0, len(indices), 3): - var c : Color = Color(1, 1, 1, 1) + if (indices.size() % 3 == 0) { + for (int i = 0; i < indices.size(); i += 3) { + Color c = Color(1, 1, 1, 1); - if uvs[indices[i]].is_equal_approx(Vector2()) || uvs[indices[i + 1]].is_equal_approx(Vector2()): - c = Color(1, 0, 0, 1) - else: - c = Color(1, 1, 1, 1) + if (uvs[indices[i]].is_equal_approx(Vector2()) || uvs[indices[i + 1]].is_equal_approx(Vector2())) { + c = Color(1, 0, 0, 1); + } else { + c = Color(1, 1, 1, 1); + } - draw_line(uvs[indices[i]] * get_size(), uvs[indices[i + 1]] * get_size(), c, 1, false) + draw_line(uvs[indices[i]] * get_size(), uvs[indices[i + 1]] * get_size(), c, 1, false); - if uvs[indices[i + 1]].is_equal_approx(Vector2()) || uvs[indices[i + 2]].is_equal_approx(Vector2()): - c = Color(1, 0, 0, 1) - else: - c = Color(1, 1, 1, 1) + if (uvs[indices[i + 1]].is_equal_approx(Vector2()) || uvs[indices[i + 2]].is_equal_approx(Vector2())) { + c = Color(1, 0, 0, 1); + } else { + c = Color(1, 1, 1, 1); + } - draw_line(uvs[indices[i + 1]] * get_size(), uvs[indices[i + 2]] * get_size(), c, 1, false) + draw_line(uvs[indices[i + 1]] * get_size(), uvs[indices[i + 2]] * get_size(), c, 1, false); - if uvs[indices[i + 2]].is_equal_approx(Vector2()) || uvs[indices[i]].is_equal_approx(Vector2()): - c = Color(1, 0, 0, 1) - else: - c = Color(1, 1, 1, 1) + if (uvs[indices[i + 2]].is_equal_approx(Vector2()) || uvs[indices[i]].is_equal_approx(Vector2())) { + c = Color(1, 0, 0, 1); + } else { + c = Color(1, 1, 1, 1); + } - draw_line(uvs[indices[i + 2]] * get_size(), uvs[indices[i]] * get_size(), c, 1, false) - - - */ + draw_line(uvs[indices[i + 2]] * get_size(), uvs[indices[i]] * get_size(), c, 1, false); + } + } } MDIEdUVEditor::MDIEdUVEditor() { diff --git a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_editor.cpp b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_editor.cpp index 74ae03caa..d212b7c12 100644 --- a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_editor.cpp +++ b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_editor.cpp @@ -26,9 +26,9 @@ SOFTWARE. #include "../../mesh_data_resource.h" #include "../../nodes/mesh_data_instance.h" -void MDRUVRectEditor::set_plugin(EditorPlugin *plugin) { +//void MDRUVRectEditor::set_plugin(EditorPlugin *plugin) { //$ScrollContainer/MarginContainer/RectView.set_plugin(plugin) -} +//} void MDRUVRectEditor::set_mesh_data_resource(Ref a) { //$ScrollContainer/MarginContainer/RectView.set_mesh_data_resource(a) diff --git a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.cpp b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.cpp index 59cc59b7c..b8bb09e91 100644 --- a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.cpp +++ b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.cpp @@ -22,265 +22,253 @@ SOFTWARE. #include "mdr_uv_rect_view.h" +#include "../../nodes/mesh_data_instance.h" +#include "../utilities/mdr_ed_mesh_decompose.h" +#include "editor/editor_node.h" +#include "editor/editor_plugin.h" +#include "mdr_uv_rect_view_node.h" #include "scene/gui/control.h" void MDRUVRectView::set_plugin(EditorPlugin *plugin) { - /* - _plugin = plugin + _plugin = plugin; - _undo_redo = _plugin.get_undo_redo() - */ + _undo_redo = EditorNode::get_undo_redo(); } void MDRUVRectView::set_mesh_data_resource(Ref a) { - /* -_mdr = a - */ + _mdr = a; } void MDRUVRectView::set_mesh_data_instance(MeshDataInstance *a) { - /* -_background_texture = null + _background_texture.unref(); - if a: - _background_texture = a.texture - */ + if (a) { + _background_texture = a->get_texture(); + } } -void MDRUVRectView::set_selected(Control *node) { - /* -if selected_rect && is_instance_valid(selected_rect): - selected_rect.set_selected(false) +void MDRUVRectView::set_selected(MDRUVRectViewNode *node) { + if (selected_rect && ObjectDB::instance_validate(selected_rect)) { + selected_rect->set_selected(false); + } - selected_rect = node + selected_rect = node; - if selected_rect: - selected_rect.set_selected(true) - */ + if (selected_rect) { + selected_rect->set_selected(true); + } } void MDRUVRectView::store_uvs() { - /* -_stored_uvs.resize(0) + _stored_uvs.resize(0); - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var arrays : Array = _mdr.get_array() + Array arrays = _mdr->get_array(); - if arrays.size() != ArrayMesh.ARRAY_MAX: - return + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return; + } - if arrays[ArrayMesh.ARRAY_TEX_UV] == null: - return + if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) { + return; + } - # Make sure it gets copied - _stored_uvs.append_array(arrays[ArrayMesh.ARRAY_TEX_UV]) - */ + // Make sure it gets copied + _stored_uvs.append_array(arrays[ArrayMesh::ARRAY_TEX_UV]); } PoolVector2Array MDRUVRectView::get_uvs(Ref mdr) { - /* -if !_mdr: - return PoolVector2Array() + if (!_mdr.is_valid()) { + return PoolVector2Array(); + } - var arrays : Array = _mdr.get_array() + Array arrays = _mdr->get_array(); - if arrays.size() != ArrayMesh.ARRAY_MAX: - return PoolVector2Array() + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return PoolVector2Array(); + } - if arrays[ArrayMesh.ARRAY_TEX_UV] == null: - return PoolVector2Array() + if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) { + return PoolVector2Array(); + } - return arrays[ArrayMesh.ARRAY_TEX_UV] - */ + return arrays[ArrayMesh::ARRAY_TEX_UV]; } void MDRUVRectView::apply_uvs(Ref mdr, PoolVector2Array stored_uvs) { - /* -if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var arrays : Array = _mdr.get_array() + Array arrays = _mdr->get_array(); - if arrays.size() != ArrayMesh.ARRAY_MAX: - return + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return; + } - if arrays[ArrayMesh.ARRAY_TEX_UV] == null: - return + if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) { + return; + } - arrays[ArrayMesh.ARRAY_TEX_UV] = stored_uvs + arrays[ArrayMesh::ARRAY_TEX_UV] = stored_uvs; - _mdr.array = arrays - */ + _mdr->set_array(arrays); } void MDRUVRectView::refresh() { - /* -clear() + clear(); - var rect : Rect2 = base_rect - edited_resource_current_size = rect.size - rect.position = rect.position * _rect_scale - rect.size = rect.size * _rect_scale - set_custom_minimum_size(rect.size) + Rect2 rect = base_rect; + edited_resource_current_size = rect.size; + rect.position = rect.position * _rect_scale; + rect.size = rect.size * _rect_scale; + set_custom_minimum_size(rect.size); - apply_zoom() + apply_zoom(); - refresh_rects() - */ + refresh_rects(); } void MDRUVRectView::clear() { } void MDRUVRectView::refresh_rects() { - /* -clear_rects() + clear_rects(); - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var partitions : Array = MeshDecompose.partition_mesh(_mdr) + Vector partitions = MDREDMeshDecompose::partition_mesh(_mdr); - for p in partitions: - var s : Node = rect_editor_node_scene.instance() + for (int i = 0; i < partitions.size(); ++i) { + PoolIntArray p = partitions[i]; - add_child(s) - s.set_editor_rect_scale(_rect_scale) - s.edited_resource_parent_size = edited_resource_current_size - s.set_edited_resource(_mdr, p) + MDRUVRectViewNode *s = memnew(MDRUVRectViewNode); - */ + add_child(s); + s->set_editor_rect_scale(_rect_scale); + s->edited_resource_parent_size = edited_resource_current_size; + s->set_edited_resource(_mdr, p); + } } void MDRUVRectView::clear_rects() { - /* -for c in get_children(): - c.queue_free() - remove_child(c) - */ + while (get_child_count() > 0) { + Node *c = get_child(0); + c->queue_delete(); + remove_child(c); + } + + selected_rect = nullptr; } void MDRUVRectView::_enter_tree() { - /* - - */ } void MDRUVRectView::_draw() { - /* -draw_rect(Rect2(Vector2(), get_size()), Color(0.2, 0.2, 0.2, 1)) + draw_rect(Rect2(Vector2(), get_size()), Color(0.2, 0.2, 0.2, 1)); - if _background_texture: - draw_texture_rect_region(_background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), _background_texture.get_size())) - */ + if (_background_texture.is_valid()) { + draw_texture_rect_region(_background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), _background_texture->get_size())); + } } void MDRUVRectView::on_mirror_horizontal_button_pressed() { - /* - if selected_rect && is_instance_valid(selected_rect) { - selected_rect.mirror_horizontal(); - }*/ + if (selected_rect && ObjectDB::instance_validate(selected_rect)) { + selected_rect->mirror_horizontal(); + } } void MDRUVRectView::on_mirror_vertical_button_pressed() { - /* - if selected_rect && is_instance_valid(selected_rect): - selected_rect.mirror_vertical() - */ + if (selected_rect && ObjectDB::instance_validate(selected_rect)) { + selected_rect->mirror_vertical(); + } } void MDRUVRectView::on_rotate_left_button_button_pressed() { - /* - if selected_rect && is_instance_valid(selected_rect): - selected_rect.rotate_uvs(-rotation_amount) - */ + if (selected_rect && ObjectDB::instance_validate(selected_rect)) { + selected_rect->rotate_uvs(-rotation_amount); + } } void MDRUVRectView::on_rotate_amount_spinbox_changed(float val) { - /* - rotation_amount = val -*/ + rotation_amount = val; } void MDRUVRectView::on_rotate_right_button_button_pressed() { - /* - if selected_rect && is_instance_valid(selected_rect): - selected_rect.rotate_uvs(rotation_amount) -*/ + if (selected_rect && ObjectDB::instance_validate(selected_rect)) { + selected_rect->rotate_uvs(rotation_amount); + } } void MDRUVRectView::on_edited_resource_changed() { - /* - call_deferred("refresh") -*/ + call_deferred("refresh"); } void MDRUVRectView::on_zoom_changed(float zoom) { - /* - _rect_scale = zoom - apply_zoom() -*/ + _rect_scale = zoom; + apply_zoom(); } void MDRUVRectView::on_visibility_changed() { - /* - if is_visible_in_tree(): - store_uvs() - call_deferred("refresh") -*/ + if (is_visible_in_tree()) { + store_uvs(); + call_deferred("refresh"); + } } void MDRUVRectView::ok_pressed() { - /* - _undo_redo.create_action("UV Editor Accept") - _undo_redo.add_do_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) - _undo_redo.add_undo_method(self, "apply_uvs", _mdr, _stored_uvs) - _undo_redo.commit_action() -*/ + _undo_redo->create_action("UV Editor Accept"); + _undo_redo->add_do_method(this, "apply_uvs", _mdr, get_uvs(_mdr)); + _undo_redo->add_undo_method(this, "apply_uvs", _mdr, _stored_uvs); + _undo_redo->commit_action(); } void MDRUVRectView::cancel_pressed() { - /* - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var arrays : Array = _mdr.get_array() + Array arrays = _mdr->get_array(); - if arrays.size() != ArrayMesh.ARRAY_MAX: - return + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return; + } - # Make sure it gets copied - var uvs : PoolVector2Array = PoolVector2Array() - uvs.append_array(_stored_uvs) + // Make sure it gets copied + PoolVector2Array uvs; + uvs.append_array(_stored_uvs); - _undo_redo.create_action("UV Editor Cancel") - _undo_redo.add_do_method(self, "apply_uvs", _mdr, uvs) - _undo_redo.add_undo_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) - _undo_redo.commit_action() + _undo_redo->create_action("UV Editor Cancel"); + _undo_redo->add_do_method(this, "apply_uvs", _mdr, uvs); + _undo_redo->add_undo_method(this, "apply_uvs", _mdr, get_uvs(_mdr)); + _undo_redo->commit_action(); - _stored_uvs.resize(0) -*/ + _stored_uvs.resize(0); } void MDRUVRectView::apply_zoom() { - /* - var rect : Rect2 = base_rect - edited_resource_current_size = rect.size - rect.position = rect.position * _rect_scale - rect.size = rect.size * _rect_scale - set_custom_minimum_size(rect.size) + Rect2 rect = base_rect; + edited_resource_current_size = rect.size; + rect.position = rect.position * _rect_scale; + rect.size = rect.size * _rect_scale; + set_custom_minimum_size(rect.size); - var p : MarginContainer = get_parent() as MarginContainer + MarginContainer *p = Object::cast_to(get_parent()); - p.add_constant_override("margin_left", min(rect.size.x / 4.0, 50 * _rect_scale)) - p.add_constant_override("margin_right", min(rect.size.x / 4.0, 50 * _rect_scale)) - p.add_constant_override("margin_top", min(rect.size.y / 4.0, 50 * _rect_scale)) - p.add_constant_override("margin_bottom", min(rect.size.y / 4.0, 50 * _rect_scale)) + p->add_constant_override("margin_left", MIN(rect.size.x / 4.0, 50 * _rect_scale)); + p->add_constant_override("margin_right", MIN(rect.size.x / 4.0, 50 * _rect_scale)); + p->add_constant_override("margin_top", MIN(rect.size.y / 4.0, 50 * _rect_scale)); + p->add_constant_override("margin_bottom", MIN(rect.size.y / 4.0, 50 * _rect_scale)); - for c in get_children(): - c.set_editor_rect_scale(_rect_scale) -*/ + for (int i = 0; i < get_child_count(); ++i) { + MDRUVRectViewNode *c = Object::cast_to(get_child(i)); + + if (c) { + c->set_editor_rect_scale(_rect_scale); + } + } } MDRUVRectView::MDRUVRectView() { - float _rect_scale = 1; + _rect_scale = 1; - Rect2 base_rect = Rect2(0, 0, 600, 600); + base_rect = Rect2(0, 0, 600, 600); - PoolVector2Array _stored_uvs; + _plugin = nullptr; + _undo_redo = nullptr; - EditorPlugin *_plugin = nullptr; - UndoRedo *_undo_redo = nullptr; - - Control *selected_rect = nullptr; + selected_rect = nullptr; rotation_amount = 45; @@ -321,4 +309,5 @@ MDRUVRectView::~MDRUVRectView() { } void MDRUVRectView::_bind_methods() { + ClassDB::bind_method(D_METHOD("refresh"), &MDRUVRectView::refresh); } diff --git a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.h b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.h index d7d81d592..d5045449a 100644 --- a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.h +++ b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view.h @@ -36,6 +36,7 @@ class UndoRedo; class Button; class EditorZoomWidget; class SpinBox; +class MDRUVRectViewNode; class MDRUVRectView : public Control { GDCLASS(MDRUVRectView, Control); @@ -44,7 +45,7 @@ public: void set_plugin(EditorPlugin *plugin); void set_mesh_data_resource(Ref a); void set_mesh_data_instance(MeshDataInstance *a); - void set_selected(Control *node); + void set_selected(MDRUVRectViewNode *node); void store_uvs(); PoolVector2Array get_uvs(Ref mdr); @@ -104,7 +105,7 @@ public: EditorPlugin *_plugin; UndoRedo *_undo_redo; - Control *selected_rect; + MDRUVRectViewNode *selected_rect; float rotation_amount; }; diff --git a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view_node.cpp b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view_node.cpp index f610838c3..5fb87c58d 100644 --- a/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view_node.cpp +++ b/modules/mesh_data_resource/editor/uv_editor/mdr_uv_rect_view_node.cpp @@ -22,427 +22,495 @@ SOFTWARE. #include "mdr_uv_rect_view_node.h" +#include "../../mesh_data_resource.h" +#include "mdr_uv_rect_view.h" +#include "scene/resources/mesh.h" + void MDRUVRectViewNode::set_edited_resource(Ref mdr, PoolIntArray indices) { - /* - _mdr = mdr - _indices = indices - _uvs.resize(0) + _mdr = mdr; + _indices = indices; + _uvs.resize(0); - var arrays : Array = _mdr.get_array() + if (!mdr.is_valid()) { + return; + } - if arrays.size() != ArrayMesh.ARRAY_MAX: - return + Array arrays = _mdr->get_array(); - if arrays[ArrayMesh.ARRAY_TEX_UV] == null: - return + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return; + } - # Make sure it gets copied - _uvs.append_array(arrays[ArrayMesh.ARRAY_TEX_UV]) + if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) { + return; + } - set_up_base_rect() + PoolVector2Array uvsarr = arrays[ArrayMesh::ARRAY_TEX_UV]; - refresh() - */ + // Make sure it gets copied + _uvs.append_array(uvsarr); + + set_up_base_rect(); + refresh(); } void MDRUVRectViewNode::mirror_horizontal() { - /* - var pia : PoolIntArray = PoolIntArray() - for index in _indices: - var found : bool = false + PoolIntArray pia; - for i in pia: - if i == index: - found = true - break + PoolIntArray::Read r = _indices.read(); - if found: - continue + for (int ii = 0; ii < _indices.size(); ++ii) { + int index = r[ii]; + bool found = false; - pia.append(index) + PoolIntArray::Read pr = _indices.read(); - var uv : Vector2 = _uvs[index] - uv.x = 1.0 - uv.x - _uvs.set(index, uv) + for (int i = 0; i < pia.size(); ++i) { + if (pr[i] == index) { + found = true; + break; + } + } - apply_uv() - update() - */ + pr.release(); + + if (found) { + continue; + } + + pia.append(index); + + Vector2 uv = _uvs[index]; + uv.x = 1.0 - uv.x; + _uvs.set(index, uv); + } + + apply_uv(); + update(); } + void MDRUVRectViewNode::mirror_vertical() { - /* - var pia : PoolIntArray = PoolIntArray() - for index in _indices: - var found : bool = false + PoolIntArray pia; - for i in pia: - if i == index: - found = true - break + PoolIntArray::Read r = _indices.read(); + for (int ii = 0; ii < _indices.size(); ++ii) { + int index = r[ii]; + bool found = false; - if found: - continue + PoolIntArray::Read pr = _indices.read(); - pia.append(index) + for (int i = 0; i < pia.size(); ++i) { + if (pr[i] == index) { + found = true; + break; + } + } - var uv : Vector2 = _uvs[index] - uv.y = 1.0 - uv.y - _uvs.set(index, uv) + pr.release(); - apply_uv() - update() - */ + if (found) { + continue; + } + + pia.append(index); + + Vector2 uv = _uvs[index]; + uv.y = 1.0 - uv.y; + _uvs.set(index, uv); + } + + apply_uv(); + update(); } + void MDRUVRectViewNode::rotate_uvs(float amount) { - /* - var t : Transform2D = Transform2D(deg2rad(amount), Vector2()) + Transform2D t = Transform2D(Math::deg2rad(amount), Vector2()); - var pia : PoolIntArray = PoolIntArray() - for index in _indices: - var found : bool = false + PoolIntArray pia; - for i in pia: - if i == index: - found = true - break + PoolIntArray::Read r = _indices.read(); + for (int ii = 0; ii < _indices.size(); ++ii) { + int index = r[ii]; + bool found = false; - if found: - continue + PoolIntArray::Read pr = _indices.read(); - pia.append(index) + for (int i = 0; i < pia.size(); ++i) { + if (pr[i] == index) { + found = true; + break; + } + } - var uv : Vector2 = _uvs[index] - uv = t.xform(uv) - _uvs.set(index, uv) + pr.release(); + if (found) { + continue; + } - re_normalize_uvs() - apply_uv() - update() - */ + pia.append(index); + + Vector2 uv = _uvs[index]; + uv = t.xform(uv); + _uvs.set(index, uv); + } + + re_normalize_uvs(); + apply_uv(); + update(); } void MDRUVRectViewNode::set_selected(bool val) { - /* - selected = val - update() - */ + selected = val; + update(); } void MDRUVRectViewNode::set_editor_rect_scale(float rect_scale) { - /* - _rect_scale = rect_scale + _rect_scale = rect_scale; - refresh() - */ + refresh(); } void MDRUVRectViewNode::set_up_base_rect() { - /* - _base_rect = Rect2() + _base_rect = Rect2(); - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - if _uvs.size() == 0: - return + if (_uvs.size() == 0) { + return; + } - var vmin : Vector2 = _uvs[_indices[0]] - var vmax : Vector2 = vmin - for i in range(1, _indices.size()): - var uv : Vector2 = _uvs[_indices[i]] + Vector2 vmin = _uvs[_indices[0]]; + Vector2 vmax = vmin; + for (int i = 1; i < _indices.size(); ++i) { + Vector2 uv = _uvs[_indices[i]]; - if uv.x < vmin.x: - vmin.x = uv.x + if (uv.x < vmin.x) { + vmin.x = uv.x; + } - if uv.x > vmax.x: - vmax.x = uv.x + if (uv.x > vmax.x) { + vmax.x = uv.x; + } - if uv.y < vmin.y: - vmin.y = uv.y + if (uv.y < vmin.y) { + vmin.y = uv.y; + } - if uv.y > vmax.y: - vmax.y = uv.y + if (uv.y > vmax.y) { + vmax.y = uv.y; + } + } - _base_rect = Rect2(vmin.x, vmin.y, vmax.x - vmin.x, vmax.y - vmin.y) - _base_rect.position *= edited_resource_parent_size - _base_rect.size *= edited_resource_parent_size + _base_rect = Rect2(vmin.x, vmin.y, vmax.x - vmin.x, vmax.y - vmin.y); + _base_rect.position *= edited_resource_parent_size; + _base_rect.size *= edited_resource_parent_size; - _uv_min = vmin - _uv_max = vmax + _uv_min = vmin; + _uv_max = vmax; - normalize_uvs() - - */ + normalize_uvs(); } void MDRUVRectViewNode::re_normalize_uvs() { - /* - if _uvs.size() == 0: - return + if (_uvs.size() == 0) { + return; + } - var vmin : Vector2 = _uvs[_indices[0]] - var vmax : Vector2 = vmin - for i in range(1, _indices.size()): - var uv : Vector2 = _uvs[_indices[i]] + Vector2 vmin = _uvs[_indices[0]]; + Vector2 vmax = vmin; + for (int i = 1; i < _indices.size(); ++i) { + Vector2 uv = _uvs[_indices[i]]; - if uv.x < vmin.x: - vmin.x = uv.x + if (uv.x < vmin.x) { + vmin.x = uv.x; + } - if uv.x > vmax.x: - vmax.x = uv.x + if (uv.x > vmax.x) { + vmax.x = uv.x; + } - if uv.y < vmin.y: - vmin.y = uv.y + if (uv.y < vmin.y) { + vmin.y = uv.y; + } - if uv.y > vmax.y: - vmax.y = uv.y + if (uv.y > vmax.y) { + vmax.y = uv.y; + } + } - var xmm : float = vmax.x - vmin.x - var ymm : float = vmax.y - vmin.y + float xmm = vmax.x - vmin.x; + float ymm = vmax.y - vmin.y; - if xmm == 0: - xmm = 0.0000001 + if (xmm == 0) { + xmm = 0.0000001; + } - if ymm == 0: - ymm = 0.0000001 + if (ymm == 0) { + ymm = 0.0000001; + } - for i in range(_uvs.size()): - var uv : Vector2 = _uvs[i] + for (int i = 0; i < _uvs.size(); ++i) { + Vector2 uv = _uvs[i]; - uv.x -= vmin.x - uv.x /= xmm + uv.x -= vmin.x; + uv.x /= xmm; - uv.y -= vmin.y - uv.y /= ymm + uv.y -= vmin.y; + uv.y /= ymm; - _uvs[i] = uv - */ + _uvs[i] = uv; + } } void MDRUVRectViewNode::normalize_uvs() { - /* - var xmm : float = _uv_max.x - _uv_min.x - var ymm : float = _uv_max.y - _uv_min.y + float xmm = _uv_max.x - _uv_min.x; + float ymm = _uv_max.y - _uv_min.y; - if xmm == 0: - xmm = 0.0000001 + if (xmm == 0) { + xmm = 0.0000001; + } - if ymm == 0: - ymm = 0.0000001 + if (ymm == 0) { + ymm = 0.0000001; + } - for i in range(_uvs.size()): - var uv : Vector2 = _uvs[i] + for (int i = 0; i < _uvs.size(); ++i) { + Vector2 uv = _uvs[i]; - uv.x -= _uv_min.x - uv.x /= xmm + uv.x -= _uv_min.x; + uv.x /= xmm; - uv.y -= _uv_min.y - uv.y /= ymm + uv.y -= _uv_min.y; + uv.y /= ymm; - _uvs[i] = uv - */ + _uvs[i] = uv; + } } void MDRUVRectViewNode::apply_uv() { - /* - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var rect : Rect2 = get_rect() + Rect2 rect = get_rect(); - #rect needs to be converted back - rect.position /= _rect_scale - rect.size /= _rect_scale - rect.position /= edited_resource_parent_size - rect.size /= edited_resource_parent_size + //rect needs to be converted back + rect.position /= _rect_scale; + rect.size /= _rect_scale; + rect.position /= edited_resource_parent_size; + rect.size /= edited_resource_parent_size; - var arrays : Array = _mdr.get_array() + Array arrays = _mdr->get_array(); - if arrays.size() != ArrayMesh.ARRAY_MAX: - return + if (arrays.size() != ArrayMesh::ARRAY_MAX) { + return; + } - if arrays[ArrayMesh.ARRAY_TEX_UV] == null: - return + if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) { + return; + } - var uvs : PoolVector2Array = arrays[ArrayMesh.ARRAY_TEX_UV] + PoolVector2Array uvs = arrays[ArrayMesh::ARRAY_TEX_UV]; - for index in _indices: - var uv : Vector2 = _uvs[index] + for (int i = 0; i < _uvs.size(); ++i) { + int index = _indices[i]; - uv = uv * rect.size + rect.position + Vector2 uv = _uvs[index]; - uvs[index] = uv + uv = uv * rect.size + rect.position; - _uv_min = rect.position - _uv_max = rect.position + rect.size + uvs[index] = uv; + } - _base_rect = get_rect() + _uv_min = rect.position; + _uv_max = rect.position + rect.size; - arrays[ArrayMesh.ARRAY_TEX_UV] = uvs - _mdr.array = arrays + _base_rect = get_rect(); - */ + arrays[ArrayMesh::ARRAY_TEX_UV] = uvs; + _mdr->set_array(arrays); } void MDRUVRectViewNode::refresh() { - /* - if !_mdr: - return + if (!_mdr.is_valid()) { + return; + } - var rect : Rect2 = _base_rect - rect.position *= _rect_scale - rect.size *= _rect_scale + Rect2 rect = _base_rect; + rect.position *= _rect_scale; + rect.size *= _rect_scale; - rect_position = rect.position - rect_size = rect.size + set_position(rect.position); + set_size(rect.size); - update() - */ + update(); } void MDRUVRectViewNode::_draw() { - /* - if selected: - draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_color) - draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_border_color, false, _editor_rect_border_size) - else: - draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_color) - draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_border_color, false, _editor_rect_border_size) + if (selected) { + draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_color); + draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_border_color, false, _editor_rect_border_size); + } else { + draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_color); + draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_border_color, false, _editor_rect_border_size); + } - if _mdr && _uvs.size() > 0: - var c : Color = _edited_resource_uv_mesh_color + if (_mdr.is_valid() && _uvs.size() > 0) { + Color c = _edited_resource_uv_mesh_color; - for i in range(0, len(_indices), 3): - draw_line(_uvs[_indices[i]] * get_size(), _uvs[_indices[i + 1]] * get_size(), c, 1, false) - draw_line(_uvs[_indices[i + 1]] * get_size(), _uvs[_indices[i + 2]] * get_size(), c, 1, false) - draw_line(_uvs[_indices[i + 2]] * get_size(), _uvs[_indices[i]] * get_size(), c, 1, false) - - */ + for (int i = 0; i < _indices.size(); i += 3) { + draw_line(_uvs[_indices[i]] * get_size(), _uvs[_indices[i + 1]] * get_size(), c, 1, false); + draw_line(_uvs[_indices[i + 1]] * get_size(), _uvs[_indices[i + 2]] * get_size(), c, 1, false); + draw_line(_uvs[_indices[i + 2]] * get_size(), _uvs[_indices[i]] * get_size(), c, 1, false); + } + } } //based on / ported from engine/scene/gui/dialogs.h and .cpp void MDRUVRectViewNode::_gui_input(Ref p_event) { - /* - if (p_event is InputEventMouseButton) && (p_event.get_button_index() == BUTTON_LEFT): - var mb : InputEventMouseButton = p_event as InputEventMouseButton + Ref mb = p_event; - if (mb.is_pressed()): - get_parent().set_selected(self) + if ((mb.is_valid()) && (mb->get_button_index() == BUTTON_LEFT)) { + if (mb->is_pressed()) { + MDRUVRectView *rvp = Object::cast_to(get_parent()); - # Begin a possible dragging operation. - drag_type = _drag_hit_test(Vector2(mb.get_position().x, mb.get_position().y)) + if (rvp) { + rvp->set_selected(this); + } - if (drag_type != DragType.DRAG_NONE): - drag_offset = get_global_mouse_position() - get_position() + // Begin a possible dragging operation. + drag_type = _drag_hit_test(Vector2(mb->get_position().x, mb->get_position().y)); - drag_offset_far = get_position() + get_size() - get_global_mouse_position() + if (drag_type != DragType::DRAG_NONE) { + drag_offset = get_global_mouse_position() - get_position(); + } - elif (drag_type != DragType.DRAG_NONE && !mb.is_pressed()): - # End a dragging operation. + drag_offset_far = get_position() + get_size() - get_global_mouse_position(); - apply_uv() + } else if (drag_type != DragType::DRAG_NONE && !mb->is_pressed()) { + // End a dragging operation. + apply_uv(); - drag_type = DragType.DRAG_NONE + drag_type = DragType::DRAG_NONE; + } - if p_event is InputEventMouseMotion: - var mm : InputEventMouseMotion = p_event as InputEventMouseMotion + return; + } - if (drag_type == DragType.DRAG_NONE): - # Update the cursor while moving along the borders. - var cursor = CURSOR_ARROW + Ref mm = p_event; - var preview_drag_type : int = _drag_hit_test(Vector2(mm.get_position().x, mm.get_position().y)) + if (mm.is_valid()) { + if (drag_type == DragType::DRAG_NONE) { + // Update the cursor while moving along the borders. + Control::CursorShape cursor = CURSOR_ARROW; - var top_left : int = DragType.DRAG_RESIZE_TOP + DragType.DRAG_RESIZE_LEFT - var bottom_right : int = DragType.DRAG_RESIZE_BOTTOM + DragType.DRAG_RESIZE_RIGHT - var top_right : int = DragType.DRAG_RESIZE_TOP + DragType.DRAG_RESIZE_RIGHT - var bottom_left : int = DragType.DRAG_RESIZE_BOTTOM + DragType.DRAG_RESIZE_LEFT + int preview_drag_type = _drag_hit_test(Vector2(mm->get_position().x, mm->get_position().y)); - match (preview_drag_type): - DragType.DRAG_RESIZE_TOP: - cursor = CURSOR_VSIZE - DragType.DRAG_RESIZE_BOTTOM: - cursor = CURSOR_VSIZE - DragType.DRAG_RESIZE_LEFT: - cursor = CURSOR_HSIZE - DragType.DRAG_RESIZE_RIGHT: - cursor = CURSOR_HSIZE - top_left: - cursor = CURSOR_FDIAGSIZE - bottom_right: - cursor = CURSOR_FDIAGSIZE - top_right: - cursor = CURSOR_BDIAGSIZE - bottom_left: - cursor = CURSOR_BDIAGSIZE + switch (preview_drag_type) { + case (DragType::DRAG_RESIZE_TOP): + cursor = CURSOR_VSIZE; + break; + case (DragType::DRAG_RESIZE_BOTTOM): + cursor = CURSOR_VSIZE; + break; + case (DragType::DRAG_RESIZE_LEFT): + cursor = CURSOR_HSIZE; + break; + case (DragType::DRAG_RESIZE_RIGHT): + cursor = CURSOR_HSIZE; + break; + case (DragType::DRAG_RESIZE_TOP + DragType::DRAG_RESIZE_LEFT): + cursor = CURSOR_FDIAGSIZE; + break; + case (DragType::DRAG_RESIZE_BOTTOM + DragType::DRAG_RESIZE_RIGHT): + cursor = CURSOR_FDIAGSIZE; + break; + case (DragType::DRAG_RESIZE_TOP + DragType::DRAG_RESIZE_RIGHT): + cursor = CURSOR_BDIAGSIZE; + break; + case (DragType::DRAG_RESIZE_BOTTOM + DragType::DRAG_RESIZE_LEFT): + cursor = CURSOR_BDIAGSIZE; + break; + default: + break; + } - if (get_cursor_shape() != cursor): + if (get_cursor_shape() != cursor) { set_default_cursor_shape(cursor); + } - else: - # Update while in a dragging operation. - var global_pos : Vector2 = get_global_mouse_position() + } else { + // Update while in a dragging operation. + Vector2 global_pos = get_global_mouse_position(); - var rect : Rect2 = get_rect() - var min_size : Vector2 = get_combined_minimum_size() + Rect2 rect = get_rect(); + Vector2 min_size = get_combined_minimum_size(); - if (drag_type == DragType.DRAG_MOVE): - rect.position = global_pos - drag_offset - else: - if (drag_type & DragType.DRAG_RESIZE_TOP): - var bottom : int = rect.position.y + rect.size.y - var max_y : int = bottom - min_size.y - rect.position.y = min(global_pos.y - drag_offset.y, max_y) - rect.size.y = bottom - rect.position.y - elif (drag_type & DragType.DRAG_RESIZE_BOTTOM): - rect.size.y = global_pos.y - rect.position.y + drag_offset_far.y + if (drag_type == DragType::DRAG_MOVE) { + rect.position = global_pos - drag_offset; + } else { + if (drag_type & DragType::DRAG_RESIZE_TOP) { + int bottom = rect.position.y + rect.size.y; + int max_y = bottom - min_size.y; + rect.position.y = MIN(global_pos.y - drag_offset.y, max_y); + rect.size.y = bottom - rect.position.y; + } else if (drag_type & DragType::DRAG_RESIZE_BOTTOM) { + rect.size.y = global_pos.y - rect.position.y + drag_offset_far.y; + } - if (drag_type & DragType.DRAG_RESIZE_LEFT): - var right : int = rect.position.x + rect.size.x - var max_x : int = right - min_size.x - rect.position.x = min(global_pos.x - drag_offset.x, max_x) - rect.size.x = right - rect.position.x - elif (drag_type & DragType.DRAG_RESIZE_RIGHT): - rect.size.x = global_pos.x - rect.position.x + drag_offset_far.x + if (drag_type & DragType::DRAG_RESIZE_LEFT) { + int right = rect.position.x + rect.size.x; + int max_x = right - min_size.x; + rect.position.x = MIN(global_pos.x - drag_offset.x, max_x); + rect.size.x = right - rect.position.x; + } else if (drag_type & DragType::DRAG_RESIZE_RIGHT) { + rect.size.x = global_pos.x - rect.position.x + drag_offset_far.x; + } + } - set_size(rect.size) - set_position(rect.position) - */ + set_size(rect.size); + set_position(rect.position); + } + } } //based on / ported from engine/scene/gui/dialogs.h and .cpp int MDRUVRectViewNode::_drag_hit_test(Vector2 pos) { - /* - var drag_type : int = DragType.DRAG_NONE + int drag_type = DragType::DRAG_NONE; - var scaleborder_size : int = 5 #get_constant("scaleborder_size", "WindowDialog") + int scaleborder_size = get_constant("scaleborder_size", "WindowDialog"); - var rect : Rect2 = get_rect() + Rect2 rect = get_rect(); - if (pos.y < (scaleborder_size)): - drag_type = DragType.DRAG_RESIZE_TOP - elif (pos.y >= (rect.size.y - scaleborder_size)): - drag_type = DragType.DRAG_RESIZE_BOTTOM + if (pos.y < (scaleborder_size)) { + drag_type = DragType::DRAG_RESIZE_TOP; + } else if (pos.y >= (rect.size.y - scaleborder_size)) { + drag_type = DragType::DRAG_RESIZE_BOTTOM; + } - if (pos.x < scaleborder_size): - drag_type |= DragType.DRAG_RESIZE_LEFT - elif (pos.x >= (rect.size.x - scaleborder_size)): - drag_type |= DragType.DRAG_RESIZE_RIGHT + if (pos.x < scaleborder_size) { + drag_type |= DragType::DRAG_RESIZE_LEFT; + } else if (pos.x >= (rect.size.x - scaleborder_size)) { + drag_type |= DragType::DRAG_RESIZE_RIGHT; + } - if (drag_type == DragType.DRAG_NONE): - drag_type = DragType.DRAG_MOVE + if (drag_type == DragType::DRAG_NONE) { + drag_type = DragType::DRAG_MOVE; + } - return drag_type - */ + return drag_type; } //based on / ported from engine/scene/gui/dialogs.h and .cpp void MDRUVRectViewNode::_notification(int p_what) { - /* - if (p_what == NOTIFICATION_MOUSE_EXIT): - # Reset the mouse cursor when leaving the resizable window border. - if (_mdr && !drag_type): - if (get_default_cursor_shape() != CURSOR_ARROW): - set_default_cursor_shape(CURSOR_ARROW) - */ + if (p_what == NOTIFICATION_MOUSE_EXIT) { + // Reset the mouse cursor when leaving the resizable window border. + if (_mdr.is_valid() && !drag_type) { + if (get_default_cursor_shape() != CURSOR_ARROW) { + set_default_cursor_shape(CURSOR_ARROW); + } + } + } } MDRUVRectViewNode::MDRUVRectViewNode() { @@ -460,19 +528,8 @@ MDRUVRectViewNode::MDRUVRectViewNode() { drag_type = 0; _rect_scale = 1; - /* - [gd_scene load_steps=2 format=2] - - [ext_resource path="res://addons/mesh_data_resource_editor/uv_editor/RectViewNode.gd" type="Script" id=1] - - [node name="RectViewNode" type="MarginContainer"] - margin_right = 1024.0 - margin_bottom = 600.0 - script = ExtResource( 1 ) - __meta__ = { - "_edit_use_anchors_": false - } - */ + set_margin(MARGIN_RIGHT, 1024); + set_margin(MARGIN_BOTTOM, 600); } MDRUVRectViewNode::~MDRUVRectViewNode() {