2017-03-05 14:47:28 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* line_2d_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-03-05 14:47:28 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2016-12-09 01:52:40 +00:00
|
|
|
#include "line_2d_editor_plugin.h"
|
|
|
|
|
|
|
|
#include "canvas_item_editor_plugin.h"
|
2017-03-05 13:21:25 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "os/file_access.h"
|
2016-12-09 01:52:40 +00:00
|
|
|
#include "os/keyboard.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Line2DEditor
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Line2DEditor::_node_removed(Node *p_node) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_node == node) {
|
|
|
|
node = NULL;
|
2016-12-09 01:52:40 +00:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED:
|
|
|
|
// This widget is not a child but should have the same visibility state
|
|
|
|
base_hb->set_visible(is_visible());
|
|
|
|
break;
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Line2DEditor::mouse_to_local_pos(Vector2 gpoint, bool alt) {
|
|
|
|
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
|
2017-03-05 15:44:50 +00:00
|
|
|
return !alt ? canvas_item_editor->snap_point(xform.affine_inverse().xform(gpoint)) : node->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint)));
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Line2DEditor::get_point_index_at(Vector2 gpos) {
|
|
|
|
ERR_FAIL_COND_V(node == 0, -1);
|
|
|
|
|
2017-04-25 12:18:08 +00:00
|
|
|
real_t grab_treshold = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8);
|
2016-12-09 01:52:40 +00:00
|
|
|
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < node->get_point_count(); ++i) {
|
|
|
|
Point2 p = xform.xform(node->get_point_pos(i));
|
|
|
|
if (gpos.distance_to(p) < grab_treshold) {
|
2016-12-09 01:52:40 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
bool Line2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
|
2016-12-09 01:52:40 +00:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!node->is_visible())
|
|
|
|
return false;
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
|
|
|
|
|
|
|
if (mb.is_valid()) {
|
|
|
|
|
2017-06-03 08:54:24 +00:00
|
|
|
Vector2 gpoint = mb->get_position();
|
2017-05-20 15:38:03 +00:00
|
|
|
Vector2 cpoint = mouse_to_local_pos(gpoint, mb->get_alt());
|
|
|
|
|
|
|
|
if (mb->is_pressed() && _dragging == false) {
|
|
|
|
int i = get_point_index_at(gpoint);
|
|
|
|
if (i != -1) {
|
|
|
|
if (mb->get_button_index() == BUTTON_LEFT && !mb->get_shift() && mode == MODE_EDIT) {
|
|
|
|
_dragging = true;
|
|
|
|
action_point = i;
|
|
|
|
moving_from = node->get_point_pos(i);
|
|
|
|
moving_screen_from = gpoint;
|
|
|
|
} else if ((mb->get_button_index() == BUTTON_RIGHT && mode == MODE_EDIT) || (mb->get_button_index() == BUTTON_LEFT && mode == MODE_DELETE)) {
|
|
|
|
undo_redo->create_action(TTR("Remove Point from Line2D"));
|
|
|
|
undo_redo->add_do_method(node, "remove_point", i);
|
|
|
|
undo_redo->add_undo_method(node, "add_point", node->get_point_pos(i), i);
|
|
|
|
undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->commit_action();
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
return true;
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && ((mb->get_command() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
undo_redo->create_action(TTR("Add Point to Line2D"));
|
|
|
|
undo_redo->add_do_method(node, "add_point", cpoint);
|
|
|
|
undo_redo->add_undo_method(node, "remove_point", node->get_point_count());
|
|
|
|
undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->commit_action();
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
_dragging = true;
|
|
|
|
action_point = node->get_point_count() - 1;
|
|
|
|
moving_from = node->get_point_pos(action_point);
|
|
|
|
moving_screen_from = gpoint;
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-12-09 01:52:40 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && _dragging) {
|
|
|
|
undo_redo->create_action(TTR("Move Point in Line2D"));
|
|
|
|
undo_redo->add_do_method(node, "set_point_pos", action_point, cpoint);
|
|
|
|
undo_redo->add_undo_method(node, "set_point_pos", action_point, moving_from);
|
|
|
|
undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
|
|
|
|
undo_redo->commit_action();
|
|
|
|
_dragging = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid()) {
|
|
|
|
|
|
|
|
if (_dragging) {
|
2017-06-03 08:54:24 +00:00
|
|
|
Vector2 cpoint = mouse_to_local_pos(mm->get_position(), mm->get_alt());
|
2017-05-20 15:38:03 +00:00
|
|
|
node->set_point_pos(action_point, cpoint);
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::_canvas_draw() {
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!node->is_visible())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
|
|
|
|
Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
|
|
|
|
Size2 handle_size = handle->get_size();
|
|
|
|
|
|
|
|
int len = node->get_point_count();
|
|
|
|
Control *vpc = canvas_item_editor->get_viewport_control();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < len; ++i) {
|
2016-12-09 01:52:40 +00:00
|
|
|
Vector2 point = xform.xform(node->get_point_pos(i));
|
|
|
|
vpc->draw_texture_rect(handle, Rect2(point - handle_size * 0.5, handle_size), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::_node_visibility_changed() {
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
canvas_item_editor->get_viewport_control()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::edit(Node *p_line2d) {
|
|
|
|
|
|
|
|
if (!canvas_item_editor)
|
|
|
|
canvas_item_editor = CanvasItemEditor::get_singleton();
|
|
|
|
|
|
|
|
if (p_line2d) {
|
|
|
|
node = p_line2d->cast_to<Line2D>();
|
|
|
|
if (!canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
|
|
|
|
canvas_item_editor->get_viewport_control()->connect("draw", this, "_canvas_draw");
|
|
|
|
if (!node->is_connected("visibility_changed", this, "_node_visibility_changed"))
|
|
|
|
node->connect("visibility_changed", this, "_node_visibility_changed");
|
2017-03-05 15:44:50 +00:00
|
|
|
} else {
|
2016-12-09 01:52:40 +00:00
|
|
|
if (canvas_item_editor->get_viewport_control()->is_connected("draw", this, "_canvas_draw"))
|
|
|
|
canvas_item_editor->get_viewport_control()->disconnect("draw", this, "_canvas_draw");
|
|
|
|
// node may have been deleted at this point
|
|
|
|
if (node && node->is_connected("visibility_changed", this, "_node_visibility_changed"))
|
|
|
|
node->disconnect("visibility_changed", this, "_node_visibility_changed");
|
|
|
|
node = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::_bind_methods() {
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_canvas_draw"), &Line2DEditor::_canvas_draw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_node_visibility_changed"), &Line2DEditor::_node_visibility_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_mode_selected"), &Line2DEditor::_mode_selected);
|
2016-12-09 01:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditor::_mode_selected(int p_mode) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (unsigned int i = 0; i < _MODE_COUNT; ++i) {
|
2016-12-09 01:52:40 +00:00
|
|
|
toolbar_buttons[i]->set_pressed(i == p_mode);
|
|
|
|
}
|
|
|
|
mode = Mode(p_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Line2DEditor::Line2DEditor(EditorNode *p_editor) {
|
|
|
|
|
|
|
|
canvas_item_editor = NULL;
|
|
|
|
editor = p_editor;
|
|
|
|
undo_redo = editor->get_undo_redo();
|
|
|
|
|
|
|
|
_dragging = false;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
base_hb = memnew(HBoxContainer);
|
2016-12-09 01:52:40 +00:00
|
|
|
CanvasItemEditor::get_singleton()->add_control_to_menu_panel(base_hb);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
sep = memnew(VSeparator);
|
2016-12-09 01:52:40 +00:00
|
|
|
base_hb->add_child(sep);
|
|
|
|
|
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
ToolButton *b = memnew(ToolButton);
|
2016-12-09 01:52:40 +00:00
|
|
|
b->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
|
|
|
|
b->set_toggle_mode(true);
|
|
|
|
b->set_focus_mode(Control::FOCUS_NONE);
|
|
|
|
b->set_tooltip(
|
2017-03-05 15:44:50 +00:00
|
|
|
TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
|
2016-12-09 01:52:40 +00:00
|
|
|
b->connect("pressed", this, "_mode_selected", varray(MODE_EDIT));
|
|
|
|
toolbar_buttons[MODE_EDIT] = b;
|
|
|
|
base_hb->add_child(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
ToolButton *b = memnew(ToolButton);
|
2016-12-09 01:52:40 +00:00
|
|
|
b->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
|
|
|
|
b->set_toggle_mode(true);
|
|
|
|
b->set_focus_mode(Control::FOCUS_NONE);
|
2017-03-05 15:44:50 +00:00
|
|
|
b->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in line)"));
|
2016-12-09 01:52:40 +00:00
|
|
|
b->connect("pressed", this, "_mode_selected", varray(MODE_CREATE));
|
|
|
|
toolbar_buttons[MODE_CREATE] = b;
|
|
|
|
base_hb->add_child(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
ToolButton *b = memnew(ToolButton);
|
2016-12-09 01:52:40 +00:00
|
|
|
b->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
|
|
|
|
b->set_toggle_mode(true);
|
|
|
|
b->set_focus_mode(Control::FOCUS_NONE);
|
|
|
|
b->set_tooltip(TTR("Delete Point"));
|
|
|
|
b->connect("pressed", this, "_mode_selected", varray(MODE_DELETE));
|
|
|
|
toolbar_buttons[MODE_DELETE] = b;
|
|
|
|
base_hb->add_child(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
base_hb->hide();
|
|
|
|
hide();
|
|
|
|
|
|
|
|
_mode_selected(MODE_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Line2DEditorPlugin
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void Line2DEditorPlugin::edit(Object *p_object) {
|
|
|
|
line2d_editor->edit(p_object->cast_to<Node>());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Line2DEditorPlugin::handles(Object *p_object) const {
|
|
|
|
return p_object->is_class("Line2D");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Line2DEditorPlugin::make_visible(bool p_visible) {
|
|
|
|
line2d_editor->set_visible(p_visible);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_visible == false)
|
2016-12-09 01:52:40 +00:00
|
|
|
line2d_editor->edit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Line2DEditorPlugin::Line2DEditorPlugin(EditorNode *p_node) {
|
2017-03-05 15:44:50 +00:00
|
|
|
editor = p_node;
|
|
|
|
line2d_editor = memnew(Line2DEditor(p_node));
|
2016-12-09 01:52:40 +00:00
|
|
|
CanvasItemEditor::get_singleton()->add_control_to_menu_panel(line2d_editor);
|
|
|
|
line2d_editor->hide();
|
|
|
|
}
|