Initial logic port of mdr ed's uv editor.

This commit is contained in:
Relintai 2022-04-12 21:46:03 +02:00
parent 0cc14121fa
commit 49d2b181a1
8 changed files with 575 additions and 512 deletions

View File

@ -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_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/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.cpp")
module_env.add_source_files(env.modules_sources,"nodes/mesh_data_instance_2d.cpp") module_env.add_source_files(env.modules_sources,"nodes/mesh_data_instance_2d.cpp")

View File

@ -321,8 +321,8 @@ bool MDREDMeshDecompose::pool_int_arr_contains(const PoolIntArray &arr, const in
return false; return false;
} }
Array MDREDMeshDecompose::partition_mesh(Ref<MeshDataResource> mdr) { Vector<PoolIntArray> MDREDMeshDecompose::partition_mesh(Ref<MeshDataResource> mdr) {
Array partitions; Vector<PoolIntArray> partitions;
Array arrays = mdr->get_array(); Array arrays = mdr->get_array();
@ -386,7 +386,7 @@ Array MDREDMeshDecompose::partition_mesh(Ref<MeshDataResource> mdr) {
} }
} }
partitions.append(partition); partitions.push_back(partition);
} }
return partitions; return partitions;

View File

@ -46,7 +46,7 @@ public:
static Array get_handle_face_to_vertex_map(const Array &arrays); static Array get_handle_face_to_vertex_map(const Array &arrays);
static PoolVector3Array calculate_map_midpoints(Array mesh, Array vertex_map); static PoolVector3Array calculate_map_midpoints(Array mesh, Array vertex_map);
static bool pool_int_arr_contains(const PoolIntArray &arr, const int val); static bool pool_int_arr_contains(const PoolIntArray &arr, const int val);
static Array partition_mesh(Ref<MeshDataResource> mdr); static Vector<PoolIntArray> partition_mesh(Ref<MeshDataResource> mdr);
}; };
#endif #endif

View File

@ -22,74 +22,84 @@ SOFTWARE.
#include "mdi_ed_uv_editor.h" #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<MeshDataResource> a) { void MDIEdUVEditor::set_mesh_data_resource(Ref<MeshDataResource> a) {
/* if (mesh_data_resource.is_valid()) {
if mesh_data_resource: mesh_data_resource->disconnect("changed", this, "on_mdr_changed");
mesh_data_resource.disconnect("changed", self, "on_mdr_changed") }
mesh_data_resource = a mesh_data_resource = a;
if mesh_data_resource: if (mesh_data_resource.is_valid()) {
mesh_data_resource.connect("changed", self, "on_mdr_changed") mesh_data_resource->connect("changed", this, "on_mdr_changed");
}
update() update();
*/
} }
void MDIEdUVEditor::set_mesh_data_instance(MeshDataInstance *a) { void MDIEdUVEditor::set_mesh_data_instance(MeshDataInstance *a) {
/* if (!a) {
background_texture.unref();
return;
}
if !a: background_texture = a->get_texture();
background_texture = null
background_texture = a.texture
*/
} }
void MDIEdUVEditor::on_mdr_changed() { void MDIEdUVEditor::on_mdr_changed() {
update(); update();
} }
void MDIEdUVEditor::_draw() { 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: if (!mesh_data_resource.is_valid()) {
draw_texture_rect_region(background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), background_texture.get_size())) return;
}
if !mesh_data_resource: Array arr = mesh_data_resource->get_array();
return
if mesh_data_resource.array.size() != ArrayMesh.ARRAY_MAX: if (arr.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
var uvs : PoolVector2Array = mesh_data_resource.array[ArrayMesh.ARRAY_TEX_UV] PoolVector2Array uvs = arr[ArrayMesh::ARRAY_TEX_UV];
var indices : PoolIntArray = mesh_data_resource.array[ArrayMesh.ARRAY_INDEX] PoolIntArray indices = arr[ArrayMesh::ARRAY_INDEX];
if indices.size() % 3 == 0: if (indices.size() % 3 == 0) {
for i in range(0, len(indices), 3): for (int i = 0; i < indices.size(); i += 3) {
var c : Color = Color(1, 1, 1, 1) Color c = Color(1, 1, 1, 1);
if uvs[indices[i]].is_equal_approx(Vector2()) || uvs[indices[i + 1]].is_equal_approx(Vector2()): if (uvs[indices[i]].is_equal_approx(Vector2()) || uvs[indices[i + 1]].is_equal_approx(Vector2())) {
c = Color(1, 0, 0, 1) c = Color(1, 0, 0, 1);
else: } else {
c = Color(1, 1, 1, 1) 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()): if (uvs[indices[i + 1]].is_equal_approx(Vector2()) || uvs[indices[i + 2]].is_equal_approx(Vector2())) {
c = Color(1, 0, 0, 1) c = Color(1, 0, 0, 1);
else: } else {
c = Color(1, 1, 1, 1) 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()): if (uvs[indices[i + 2]].is_equal_approx(Vector2()) || uvs[indices[i]].is_equal_approx(Vector2())) {
c = Color(1, 0, 0, 1) c = Color(1, 0, 0, 1);
else: } else {
c = Color(1, 1, 1, 1) 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() { MDIEdUVEditor::MDIEdUVEditor() {

View File

@ -26,9 +26,9 @@ SOFTWARE.
#include "../../mesh_data_resource.h" #include "../../mesh_data_resource.h"
#include "../../nodes/mesh_data_instance.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) //$ScrollContainer/MarginContainer/RectView.set_plugin(plugin)
} //}
void MDRUVRectEditor::set_mesh_data_resource(Ref<MeshDataResource> a) { void MDRUVRectEditor::set_mesh_data_resource(Ref<MeshDataResource> a) {
//$ScrollContainer/MarginContainer/RectView.set_mesh_data_resource(a) //$ScrollContainer/MarginContainer/RectView.set_mesh_data_resource(a)

View File

@ -22,265 +22,253 @@ SOFTWARE.
#include "mdr_uv_rect_view.h" #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" #include "scene/gui/control.h"
void MDRUVRectView::set_plugin(EditorPlugin *plugin) { 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<MeshDataResource> a) { void MDRUVRectView::set_mesh_data_resource(Ref<MeshDataResource> a) {
/* _mdr = a;
_mdr = a
*/
} }
void MDRUVRectView::set_mesh_data_instance(MeshDataInstance *a) { void MDRUVRectView::set_mesh_data_instance(MeshDataInstance *a) {
/* _background_texture.unref();
_background_texture = null
if a: if (a) {
_background_texture = a.texture _background_texture = a->get_texture();
*/ }
} }
void MDRUVRectView::set_selected(Control *node) { void MDRUVRectView::set_selected(MDRUVRectViewNode *node) {
/* if (selected_rect && ObjectDB::instance_validate(selected_rect)) {
if selected_rect && is_instance_valid(selected_rect): selected_rect->set_selected(false);
selected_rect.set_selected(false) }
selected_rect = node selected_rect = node;
if selected_rect: if (selected_rect) {
selected_rect.set_selected(true) selected_rect->set_selected(true);
*/ }
} }
void MDRUVRectView::store_uvs() { void MDRUVRectView::store_uvs() {
/* _stored_uvs.resize(0);
_stored_uvs.resize(0)
if !_mdr: if (!_mdr.is_valid()) {
return return;
}
var arrays : Array = _mdr.get_array() Array arrays = _mdr->get_array();
if arrays.size() != ArrayMesh.ARRAY_MAX: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
if arrays[ArrayMesh.ARRAY_TEX_UV] == null: if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) {
return return;
}
# Make sure it gets copied // Make sure it gets copied
_stored_uvs.append_array(arrays[ArrayMesh.ARRAY_TEX_UV]) _stored_uvs.append_array(arrays[ArrayMesh::ARRAY_TEX_UV]);
*/
} }
PoolVector2Array MDRUVRectView::get_uvs(Ref<MeshDataResource> mdr) { PoolVector2Array MDRUVRectView::get_uvs(Ref<MeshDataResource> mdr) {
/* if (!_mdr.is_valid()) {
if !_mdr: return PoolVector2Array();
return PoolVector2Array() }
var arrays : Array = _mdr.get_array() Array arrays = _mdr->get_array();
if arrays.size() != ArrayMesh.ARRAY_MAX: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return PoolVector2Array() return PoolVector2Array();
}
if arrays[ArrayMesh.ARRAY_TEX_UV] == null: if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) {
return PoolVector2Array() return PoolVector2Array();
}
return arrays[ArrayMesh.ARRAY_TEX_UV] return arrays[ArrayMesh::ARRAY_TEX_UV];
*/
} }
void MDRUVRectView::apply_uvs(Ref<MeshDataResource> mdr, PoolVector2Array stored_uvs) { void MDRUVRectView::apply_uvs(Ref<MeshDataResource> mdr, PoolVector2Array stored_uvs) {
/* if (!_mdr.is_valid()) {
if !_mdr: return;
return }
var arrays : Array = _mdr.get_array() Array arrays = _mdr->get_array();
if arrays.size() != ArrayMesh.ARRAY_MAX: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
if arrays[ArrayMesh.ARRAY_TEX_UV] == null: if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) {
return 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() { void MDRUVRectView::refresh() {
/* clear();
clear()
var rect : Rect2 = base_rect Rect2 rect = base_rect;
edited_resource_current_size = rect.size edited_resource_current_size = rect.size;
rect.position = rect.position * _rect_scale rect.position = rect.position * _rect_scale;
rect.size = rect.size * _rect_scale rect.size = rect.size * _rect_scale;
set_custom_minimum_size(rect.size) set_custom_minimum_size(rect.size);
apply_zoom() apply_zoom();
refresh_rects() refresh_rects();
*/
} }
void MDRUVRectView::clear() { void MDRUVRectView::clear() {
} }
void MDRUVRectView::refresh_rects() { void MDRUVRectView::refresh_rects() {
/* clear_rects();
clear_rects()
if !_mdr: if (!_mdr.is_valid()) {
return return;
}
var partitions : Array = MeshDecompose.partition_mesh(_mdr) Vector<PoolIntArray> partitions = MDREDMeshDecompose::partition_mesh(_mdr);
for p in partitions: for (int i = 0; i < partitions.size(); ++i) {
var s : Node = rect_editor_node_scene.instance() PoolIntArray p = partitions[i];
add_child(s) MDRUVRectViewNode *s = memnew(MDRUVRectViewNode);
s.set_editor_rect_scale(_rect_scale)
s.edited_resource_parent_size = edited_resource_current_size
s.set_edited_resource(_mdr, p)
*/ 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() { void MDRUVRectView::clear_rects() {
/* while (get_child_count() > 0) {
for c in get_children(): Node *c = get_child(0);
c.queue_free() c->queue_delete();
remove_child(c) remove_child(c);
*/ }
selected_rect = nullptr;
} }
void MDRUVRectView::_enter_tree() { void MDRUVRectView::_enter_tree() {
/*
*/
} }
void MDRUVRectView::_draw() { 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: if (_background_texture.is_valid()) {
draw_texture_rect_region(_background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), _background_texture.get_size())) draw_texture_rect_region(_background_texture, Rect2(Vector2(), get_size()), Rect2(Vector2(), _background_texture->get_size()));
*/ }
} }
void MDRUVRectView::on_mirror_horizontal_button_pressed() { void MDRUVRectView::on_mirror_horizontal_button_pressed() {
/* if (selected_rect && ObjectDB::instance_validate(selected_rect)) {
if selected_rect && is_instance_valid(selected_rect) { selected_rect->mirror_horizontal();
selected_rect.mirror_horizontal(); }
}*/
} }
void MDRUVRectView::on_mirror_vertical_button_pressed() { void MDRUVRectView::on_mirror_vertical_button_pressed() {
/* if (selected_rect && ObjectDB::instance_validate(selected_rect)) {
if selected_rect && is_instance_valid(selected_rect): selected_rect->mirror_vertical();
selected_rect.mirror_vertical() }
*/
} }
void MDRUVRectView::on_rotate_left_button_button_pressed() { void MDRUVRectView::on_rotate_left_button_button_pressed() {
/* if (selected_rect && ObjectDB::instance_validate(selected_rect)) {
if selected_rect && is_instance_valid(selected_rect): selected_rect->rotate_uvs(-rotation_amount);
selected_rect.rotate_uvs(-rotation_amount) }
*/
} }
void MDRUVRectView::on_rotate_amount_spinbox_changed(float val) { void MDRUVRectView::on_rotate_amount_spinbox_changed(float val) {
/* rotation_amount = val;
rotation_amount = val
*/
} }
void MDRUVRectView::on_rotate_right_button_button_pressed() { void MDRUVRectView::on_rotate_right_button_button_pressed() {
/* if (selected_rect && ObjectDB::instance_validate(selected_rect)) {
if selected_rect && is_instance_valid(selected_rect): selected_rect->rotate_uvs(rotation_amount);
selected_rect.rotate_uvs(rotation_amount) }
*/
} }
void MDRUVRectView::on_edited_resource_changed() { void MDRUVRectView::on_edited_resource_changed() {
/* call_deferred("refresh");
call_deferred("refresh")
*/
} }
void MDRUVRectView::on_zoom_changed(float zoom) { void MDRUVRectView::on_zoom_changed(float zoom) {
/* _rect_scale = zoom;
_rect_scale = zoom apply_zoom();
apply_zoom()
*/
} }
void MDRUVRectView::on_visibility_changed() { void MDRUVRectView::on_visibility_changed() {
/* if (is_visible_in_tree()) {
if is_visible_in_tree(): store_uvs();
store_uvs() call_deferred("refresh");
call_deferred("refresh") }
*/
} }
void MDRUVRectView::ok_pressed() { void MDRUVRectView::ok_pressed() {
/* _undo_redo->create_action("UV Editor Accept");
_undo_redo.create_action("UV Editor Accept") _undo_redo->add_do_method(this, "apply_uvs", _mdr, get_uvs(_mdr));
_undo_redo.add_do_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) _undo_redo->add_undo_method(this, "apply_uvs", _mdr, _stored_uvs);
_undo_redo.add_undo_method(self, "apply_uvs", _mdr, _stored_uvs) _undo_redo->commit_action();
_undo_redo.commit_action()
*/
} }
void MDRUVRectView::cancel_pressed() { void MDRUVRectView::cancel_pressed() {
/* if (!_mdr.is_valid()) {
if !_mdr: return;
return }
var arrays : Array = _mdr.get_array() Array arrays = _mdr->get_array();
if arrays.size() != ArrayMesh.ARRAY_MAX: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
# Make sure it gets copied // Make sure it gets copied
var uvs : PoolVector2Array = PoolVector2Array() PoolVector2Array uvs;
uvs.append_array(_stored_uvs) uvs.append_array(_stored_uvs);
_undo_redo.create_action("UV Editor Cancel") _undo_redo->create_action("UV Editor Cancel");
_undo_redo.add_do_method(self, "apply_uvs", _mdr, uvs) _undo_redo->add_do_method(this, "apply_uvs", _mdr, uvs);
_undo_redo.add_undo_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) _undo_redo->add_undo_method(this, "apply_uvs", _mdr, get_uvs(_mdr));
_undo_redo.commit_action() _undo_redo->commit_action();
_stored_uvs.resize(0) _stored_uvs.resize(0);
*/
} }
void MDRUVRectView::apply_zoom() { void MDRUVRectView::apply_zoom() {
/* Rect2 rect = base_rect;
var rect : Rect2 = base_rect edited_resource_current_size = rect.size;
edited_resource_current_size = rect.size rect.position = rect.position * _rect_scale;
rect.position = rect.position * _rect_scale rect.size = rect.size * _rect_scale;
rect.size = rect.size * _rect_scale set_custom_minimum_size(rect.size);
set_custom_minimum_size(rect.size)
var p : MarginContainer = get_parent() as MarginContainer MarginContainer *p = Object::cast_to<MarginContainer>(get_parent());
p.add_constant_override("margin_left", min(rect.size.x / 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_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_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_bottom", MIN(rect.size.y / 4.0, 50 * _rect_scale));
for c in get_children(): for (int i = 0; i < get_child_count(); ++i) {
c.set_editor_rect_scale(_rect_scale) MDRUVRectViewNode *c = Object::cast_to<MDRUVRectViewNode>(get_child(i));
*/
if (c) {
c->set_editor_rect_scale(_rect_scale);
}
}
} }
MDRUVRectView::MDRUVRectView() { 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; selected_rect = nullptr;
UndoRedo *_undo_redo = nullptr;
Control *selected_rect = nullptr;
rotation_amount = 45; rotation_amount = 45;
@ -321,4 +309,5 @@ MDRUVRectView::~MDRUVRectView() {
} }
void MDRUVRectView::_bind_methods() { void MDRUVRectView::_bind_methods() {
ClassDB::bind_method(D_METHOD("refresh"), &MDRUVRectView::refresh);
} }

View File

@ -36,6 +36,7 @@ class UndoRedo;
class Button; class Button;
class EditorZoomWidget; class EditorZoomWidget;
class SpinBox; class SpinBox;
class MDRUVRectViewNode;
class MDRUVRectView : public Control { class MDRUVRectView : public Control {
GDCLASS(MDRUVRectView, Control); GDCLASS(MDRUVRectView, Control);
@ -44,7 +45,7 @@ public:
void set_plugin(EditorPlugin *plugin); void set_plugin(EditorPlugin *plugin);
void set_mesh_data_resource(Ref<MeshDataResource> a); void set_mesh_data_resource(Ref<MeshDataResource> a);
void set_mesh_data_instance(MeshDataInstance *a); void set_mesh_data_instance(MeshDataInstance *a);
void set_selected(Control *node); void set_selected(MDRUVRectViewNode *node);
void store_uvs(); void store_uvs();
PoolVector2Array get_uvs(Ref<MeshDataResource> mdr); PoolVector2Array get_uvs(Ref<MeshDataResource> mdr);
@ -104,7 +105,7 @@ public:
EditorPlugin *_plugin; EditorPlugin *_plugin;
UndoRedo *_undo_redo; UndoRedo *_undo_redo;
Control *selected_rect; MDRUVRectViewNode *selected_rect;
float rotation_amount; float rotation_amount;
}; };

View File

@ -22,427 +22,495 @@ SOFTWARE.
#include "mdr_uv_rect_view_node.h" #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<MeshDataResource> mdr, PoolIntArray indices) { void MDRUVRectViewNode::set_edited_resource(Ref<MeshDataResource> mdr, PoolIntArray indices) {
/* _mdr = mdr;
_mdr = mdr _indices = indices;
_indices = indices _uvs.resize(0);
_uvs.resize(0)
var arrays : Array = _mdr.get_array() if (!mdr.is_valid()) {
return;
}
if arrays.size() != ArrayMesh.ARRAY_MAX: Array arrays = _mdr->get_array();
return
if arrays[ArrayMesh.ARRAY_TEX_UV] == null: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
# Make sure it gets copied if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) {
_uvs.append_array(arrays[ArrayMesh.ARRAY_TEX_UV]) 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() { void MDRUVRectViewNode::mirror_horizontal() {
/* PoolIntArray pia;
var pia : PoolIntArray = PoolIntArray()
for index in _indices:
var found : bool = false
for i in pia: PoolIntArray::Read r = _indices.read();
if i == index:
found = true
break
if found: for (int ii = 0; ii < _indices.size(); ++ii) {
continue int index = r[ii];
bool found = false;
pia.append(index) PoolIntArray::Read pr = _indices.read();
var uv : Vector2 = _uvs[index] for (int i = 0; i < pia.size(); ++i) {
uv.x = 1.0 - uv.x if (pr[i] == index) {
_uvs.set(index, uv) found = true;
break;
}
}
apply_uv() pr.release();
update()
*/ 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() { void MDRUVRectViewNode::mirror_vertical() {
/* PoolIntArray pia;
var pia : PoolIntArray = PoolIntArray()
for index in _indices:
var found : bool = false
for i in pia: PoolIntArray::Read r = _indices.read();
if i == index: for (int ii = 0; ii < _indices.size(); ++ii) {
found = true int index = r[ii];
break bool found = false;
if found: PoolIntArray::Read pr = _indices.read();
continue
pia.append(index) for (int i = 0; i < pia.size(); ++i) {
if (pr[i] == index) {
found = true;
break;
}
}
var uv : Vector2 = _uvs[index] pr.release();
uv.y = 1.0 - uv.y
_uvs.set(index, uv)
apply_uv() if (found) {
update() 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) { void MDRUVRectViewNode::rotate_uvs(float amount) {
/* Transform2D t = Transform2D(Math::deg2rad(amount), Vector2());
var t : Transform2D = Transform2D(deg2rad(amount), Vector2())
var pia : PoolIntArray = PoolIntArray() PoolIntArray pia;
for index in _indices:
var found : bool = false
for i in pia: PoolIntArray::Read r = _indices.read();
if i == index: for (int ii = 0; ii < _indices.size(); ++ii) {
found = true int index = r[ii];
break bool found = false;
if found: PoolIntArray::Read pr = _indices.read();
continue
pia.append(index) for (int i = 0; i < pia.size(); ++i) {
if (pr[i] == index) {
found = true;
break;
}
}
var uv : Vector2 = _uvs[index] pr.release();
uv = t.xform(uv)
_uvs.set(index, uv)
if (found) {
continue;
}
re_normalize_uvs() pia.append(index);
apply_uv()
update() Vector2 uv = _uvs[index];
*/ uv = t.xform(uv);
_uvs.set(index, uv);
}
re_normalize_uvs();
apply_uv();
update();
} }
void MDRUVRectViewNode::set_selected(bool val) { void MDRUVRectViewNode::set_selected(bool val) {
/* selected = val;
selected = val update();
update()
*/
} }
void MDRUVRectViewNode::set_editor_rect_scale(float rect_scale) { 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() { void MDRUVRectViewNode::set_up_base_rect() {
/* _base_rect = Rect2();
_base_rect = Rect2()
if !_mdr: if (!_mdr.is_valid()) {
return return;
}
if _uvs.size() == 0: if (_uvs.size() == 0) {
return return;
}
var vmin : Vector2 = _uvs[_indices[0]] Vector2 vmin = _uvs[_indices[0]];
var vmax : Vector2 = vmin Vector2 vmax = vmin;
for i in range(1, _indices.size()): for (int i = 1; i < _indices.size(); ++i) {
var uv : Vector2 = _uvs[_indices[i]] Vector2 uv = _uvs[_indices[i]];
if uv.x < vmin.x: if (uv.x < vmin.x) {
vmin.x = uv.x vmin.x = uv.x;
}
if uv.x > vmax.x: if (uv.x > vmax.x) {
vmax.x = uv.x vmax.x = uv.x;
}
if uv.y < vmin.y: if (uv.y < vmin.y) {
vmin.y = uv.y vmin.y = uv.y;
}
if uv.y > vmax.y: if (uv.y > vmax.y) {
vmax.y = uv.y vmax.y = uv.y;
}
}
_base_rect = Rect2(vmin.x, vmin.y, vmax.x - vmin.x, vmax.y - vmin.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.position *= edited_resource_parent_size;
_base_rect.size *= edited_resource_parent_size _base_rect.size *= edited_resource_parent_size;
_uv_min = vmin _uv_min = vmin;
_uv_max = vmax _uv_max = vmax;
normalize_uvs() normalize_uvs();
*/
} }
void MDRUVRectViewNode::re_normalize_uvs() { void MDRUVRectViewNode::re_normalize_uvs() {
/* if (_uvs.size() == 0) {
if _uvs.size() == 0: return;
return }
var vmin : Vector2 = _uvs[_indices[0]] Vector2 vmin = _uvs[_indices[0]];
var vmax : Vector2 = vmin Vector2 vmax = vmin;
for i in range(1, _indices.size()): for (int i = 1; i < _indices.size(); ++i) {
var uv : Vector2 = _uvs[_indices[i]] Vector2 uv = _uvs[_indices[i]];
if uv.x < vmin.x: if (uv.x < vmin.x) {
vmin.x = uv.x vmin.x = uv.x;
}
if uv.x > vmax.x: if (uv.x > vmax.x) {
vmax.x = uv.x vmax.x = uv.x;
}
if uv.y < vmin.y: if (uv.y < vmin.y) {
vmin.y = uv.y vmin.y = uv.y;
}
if uv.y > vmax.y: if (uv.y > vmax.y) {
vmax.y = uv.y vmax.y = uv.y;
}
}
var xmm : float = vmax.x - vmin.x float xmm = vmax.x - vmin.x;
var ymm : float = vmax.y - vmin.y float ymm = vmax.y - vmin.y;
if xmm == 0: if (xmm == 0) {
xmm = 0.0000001 xmm = 0.0000001;
}
if ymm == 0: if (ymm == 0) {
ymm = 0.0000001 ymm = 0.0000001;
}
for i in range(_uvs.size()): for (int i = 0; i < _uvs.size(); ++i) {
var uv : Vector2 = _uvs[i] Vector2 uv = _uvs[i];
uv.x -= vmin.x uv.x -= vmin.x;
uv.x /= xmm uv.x /= xmm;
uv.y -= vmin.y uv.y -= vmin.y;
uv.y /= ymm uv.y /= ymm;
_uvs[i] = uv _uvs[i] = uv;
*/ }
} }
void MDRUVRectViewNode::normalize_uvs() { void MDRUVRectViewNode::normalize_uvs() {
/* float xmm = _uv_max.x - _uv_min.x;
var xmm : float = _uv_max.x - _uv_min.x float ymm = _uv_max.y - _uv_min.y;
var ymm : float = _uv_max.y - _uv_min.y
if xmm == 0: if (xmm == 0) {
xmm = 0.0000001 xmm = 0.0000001;
}
if ymm == 0: if (ymm == 0) {
ymm = 0.0000001 ymm = 0.0000001;
}
for i in range(_uvs.size()): for (int i = 0; i < _uvs.size(); ++i) {
var uv : Vector2 = _uvs[i] Vector2 uv = _uvs[i];
uv.x -= _uv_min.x uv.x -= _uv_min.x;
uv.x /= xmm uv.x /= xmm;
uv.y -= _uv_min.y uv.y -= _uv_min.y;
uv.y /= ymm uv.y /= ymm;
_uvs[i] = uv _uvs[i] = uv;
*/ }
} }
void MDRUVRectViewNode::apply_uv() { void MDRUVRectViewNode::apply_uv() {
/* if (!_mdr.is_valid()) {
if !_mdr: return;
return }
var rect : Rect2 = get_rect() Rect2 rect = get_rect();
#rect needs to be converted back //rect needs to be converted back
rect.position /= _rect_scale rect.position /= _rect_scale;
rect.size /= _rect_scale rect.size /= _rect_scale;
rect.position /= edited_resource_parent_size rect.position /= edited_resource_parent_size;
rect.size /= 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: if (arrays.size() != ArrayMesh::ARRAY_MAX) {
return return;
}
if arrays[ArrayMesh.ARRAY_TEX_UV] == null: if (arrays[ArrayMesh::ARRAY_TEX_UV].is_null()) {
return return;
}
var uvs : PoolVector2Array = arrays[ArrayMesh.ARRAY_TEX_UV] PoolVector2Array uvs = arrays[ArrayMesh::ARRAY_TEX_UV];
for index in _indices: for (int i = 0; i < _uvs.size(); ++i) {
var uv : Vector2 = _uvs[index] 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 uvs[index] = uv;
_uv_max = rect.position + rect.size }
_base_rect = get_rect() _uv_min = rect.position;
_uv_max = rect.position + rect.size;
arrays[ArrayMesh.ARRAY_TEX_UV] = uvs _base_rect = get_rect();
_mdr.array = arrays
*/ arrays[ArrayMesh::ARRAY_TEX_UV] = uvs;
_mdr->set_array(arrays);
} }
void MDRUVRectViewNode::refresh() { void MDRUVRectViewNode::refresh() {
/* if (!_mdr.is_valid()) {
if !_mdr: return;
return }
var rect : Rect2 = _base_rect Rect2 rect = _base_rect;
rect.position *= _rect_scale rect.position *= _rect_scale;
rect.size *= _rect_scale rect.size *= _rect_scale;
rect_position = rect.position set_position(rect.position);
rect_size = rect.size set_size(rect.size);
update() update();
*/
} }
void MDRUVRectViewNode::_draw() { void MDRUVRectViewNode::_draw() {
/* if (selected) {
if selected: draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_color);
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);
draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_selected_border_color, false, _editor_rect_border_size) } else {
else: draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_color);
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);
draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_border_color, false, _editor_rect_border_size) }
if _mdr && _uvs.size() > 0: if (_mdr.is_valid() && _uvs.size() > 0) {
var c : Color = _edited_resource_uv_mesh_color Color c = _edited_resource_uv_mesh_color;
for i in range(0, len(_indices), 3): 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]] * 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 + 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) 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 //based on / ported from engine/scene/gui/dialogs.h and .cpp
void MDRUVRectViewNode::_gui_input(Ref<InputEvent> p_event) { void MDRUVRectViewNode::_gui_input(Ref<InputEvent> p_event) {
/* Ref<InputEventMouseButton> mb = p_event;
if (p_event is InputEventMouseButton) && (p_event.get_button_index() == BUTTON_LEFT):
var mb : InputEventMouseButton = p_event as InputEventMouseButton
if (mb.is_pressed()): if ((mb.is_valid()) && (mb->get_button_index() == BUTTON_LEFT)) {
get_parent().set_selected(self) if (mb->is_pressed()) {
MDRUVRectView *rvp = Object::cast_to<MDRUVRectView>(get_parent());
# Begin a possible dragging operation. if (rvp) {
drag_type = _drag_hit_test(Vector2(mb.get_position().x, mb.get_position().y)) rvp->set_selected(this);
}
if (drag_type != DragType.DRAG_NONE): // Begin a possible dragging operation.
drag_offset = get_global_mouse_position() - get_position() 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()): drag_offset_far = get_position() + get_size() - get_global_mouse_position();
# End a dragging operation.
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: return;
var mm : InputEventMouseMotion = p_event as InputEventMouseMotion }
if (drag_type == DragType.DRAG_NONE): Ref<InputEventMouseMotion> mm = p_event;
# Update the cursor while moving along the borders.
var cursor = CURSOR_ARROW
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 int preview_drag_type = _drag_hit_test(Vector2(mm->get_position().x, mm->get_position().y));
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
match (preview_drag_type): switch (preview_drag_type) {
DragType.DRAG_RESIZE_TOP: case (DragType::DRAG_RESIZE_TOP):
cursor = CURSOR_VSIZE cursor = CURSOR_VSIZE;
DragType.DRAG_RESIZE_BOTTOM: break;
cursor = CURSOR_VSIZE case (DragType::DRAG_RESIZE_BOTTOM):
DragType.DRAG_RESIZE_LEFT: cursor = CURSOR_VSIZE;
cursor = CURSOR_HSIZE break;
DragType.DRAG_RESIZE_RIGHT: case (DragType::DRAG_RESIZE_LEFT):
cursor = CURSOR_HSIZE cursor = CURSOR_HSIZE;
top_left: break;
cursor = CURSOR_FDIAGSIZE case (DragType::DRAG_RESIZE_RIGHT):
bottom_right: cursor = CURSOR_HSIZE;
cursor = CURSOR_FDIAGSIZE break;
top_right: case (DragType::DRAG_RESIZE_TOP + DragType::DRAG_RESIZE_LEFT):
cursor = CURSOR_BDIAGSIZE cursor = CURSOR_FDIAGSIZE;
bottom_left: break;
cursor = CURSOR_BDIAGSIZE 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); set_default_cursor_shape(cursor);
}
else: } else {
# Update while in a dragging operation. // Update while in a dragging operation.
var global_pos : Vector2 = get_global_mouse_position() Vector2 global_pos = get_global_mouse_position();
var rect : Rect2 = get_rect() Rect2 rect = get_rect();
var min_size : Vector2 = get_combined_minimum_size() Vector2 min_size = get_combined_minimum_size();
if (drag_type == DragType.DRAG_MOVE): if (drag_type == DragType::DRAG_MOVE) {
rect.position = global_pos - drag_offset rect.position = global_pos - drag_offset;
else: } else {
if (drag_type & DragType.DRAG_RESIZE_TOP): if (drag_type & DragType::DRAG_RESIZE_TOP) {
var bottom : int = rect.position.y + rect.size.y int bottom = rect.position.y + rect.size.y;
var max_y : int = bottom - min_size.y int max_y = bottom - min_size.y;
rect.position.y = min(global_pos.y - drag_offset.y, max_y) rect.position.y = MIN(global_pos.y - drag_offset.y, max_y);
rect.size.y = bottom - rect.position.y rect.size.y = bottom - rect.position.y;
elif (drag_type & DragType.DRAG_RESIZE_BOTTOM): } else if (drag_type & DragType::DRAG_RESIZE_BOTTOM) {
rect.size.y = global_pos.y - rect.position.y + drag_offset_far.y rect.size.y = global_pos.y - rect.position.y + drag_offset_far.y;
}
if (drag_type & DragType.DRAG_RESIZE_LEFT): if (drag_type & DragType::DRAG_RESIZE_LEFT) {
var right : int = rect.position.x + rect.size.x int right = rect.position.x + rect.size.x;
var max_x : int = right - min_size.x int max_x = right - min_size.x;
rect.position.x = min(global_pos.x - drag_offset.x, max_x) rect.position.x = MIN(global_pos.x - drag_offset.x, max_x);
rect.size.x = right - rect.position.x rect.size.x = right - rect.position.x;
elif (drag_type & DragType.DRAG_RESIZE_RIGHT): } else if (drag_type & DragType::DRAG_RESIZE_RIGHT) {
rect.size.x = global_pos.x - rect.position.x + drag_offset_far.x rect.size.x = global_pos.x - rect.position.x + drag_offset_far.x;
}
}
set_size(rect.size) set_size(rect.size);
set_position(rect.position) set_position(rect.position);
*/ }
}
} }
//based on / ported from engine/scene/gui/dialogs.h and .cpp //based on / ported from engine/scene/gui/dialogs.h and .cpp
int MDRUVRectViewNode::_drag_hit_test(Vector2 pos) { int MDRUVRectViewNode::_drag_hit_test(Vector2 pos) {
/* int drag_type = DragType::DRAG_NONE;
var drag_type : int = 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)): if (pos.y < (scaleborder_size)) {
drag_type = DragType.DRAG_RESIZE_TOP drag_type = DragType::DRAG_RESIZE_TOP;
elif (pos.y >= (rect.size.y - scaleborder_size)): } else if (pos.y >= (rect.size.y - scaleborder_size)) {
drag_type = DragType.DRAG_RESIZE_BOTTOM drag_type = DragType::DRAG_RESIZE_BOTTOM;
}
if (pos.x < scaleborder_size): if (pos.x < scaleborder_size) {
drag_type |= DragType.DRAG_RESIZE_LEFT drag_type |= DragType::DRAG_RESIZE_LEFT;
elif (pos.x >= (rect.size.x - scaleborder_size)): } else if (pos.x >= (rect.size.x - scaleborder_size)) {
drag_type |= DragType.DRAG_RESIZE_RIGHT drag_type |= DragType::DRAG_RESIZE_RIGHT;
}
if (drag_type == DragType.DRAG_NONE): if (drag_type == DragType::DRAG_NONE) {
drag_type = DragType.DRAG_MOVE drag_type = DragType::DRAG_MOVE;
}
return drag_type return drag_type;
*/
} }
//based on / ported from engine/scene/gui/dialogs.h and .cpp //based on / ported from engine/scene/gui/dialogs.h and .cpp
void MDRUVRectViewNode::_notification(int p_what) { void MDRUVRectViewNode::_notification(int p_what) {
/* if (p_what == NOTIFICATION_MOUSE_EXIT) {
if (p_what == NOTIFICATION_MOUSE_EXIT): // Reset the mouse cursor when leaving the resizable window border.
# Reset the mouse cursor when leaving the resizable window border. if (_mdr.is_valid() && !drag_type) {
if (_mdr && !drag_type): if (get_default_cursor_shape() != CURSOR_ARROW) {
if (get_default_cursor_shape() != CURSOR_ARROW): set_default_cursor_shape(CURSOR_ARROW);
set_default_cursor_shape(CURSOR_ARROW) }
*/ }
}
} }
MDRUVRectViewNode::MDRUVRectViewNode() { MDRUVRectViewNode::MDRUVRectViewNode() {
@ -460,19 +528,8 @@ MDRUVRectViewNode::MDRUVRectViewNode() {
drag_type = 0; drag_type = 0;
_rect_scale = 1; _rect_scale = 1;
/* set_margin(MARGIN_RIGHT, 1024);
[gd_scene load_steps=2 format=2] set_margin(MARGIN_BOTTOM, 600);
[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
}
*/
} }
MDRUVRectViewNode::~MDRUVRectViewNode() { MDRUVRectViewNode::~MDRUVRectViewNode() {