Skip to content

Commit

Permalink
Merge pull request #22225 from groud/fix_draw_on_viewport
Browse files Browse the repository at this point in the history
Fixes drawing of the 2D plugins on the 3D view
  • Loading branch information
akien-mga authored Sep 20, 2018
2 parents f93a69c + 5172642 commit 76ca46d
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 70 deletions.
22 changes: 18 additions & 4 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5898,17 +5898,31 @@ bool EditorPluginList::forward_spatial_gui_input(Camera *p_camera, const Ref<Inp
return discard;
}

void EditorPluginList::forward_draw_over_viewport(Control *p_overlay) {
void EditorPluginList::forward_canvas_draw_over_viewport(Control *p_overlay) {

for (int i = 0; i < plugins_list.size(); i++) {
plugins_list[i]->forward_draw_over_viewport(p_overlay);
plugins_list[i]->forward_canvas_draw_over_viewport(p_overlay);
}
}

void EditorPluginList::forward_force_draw_over_viewport(Control *p_overlay) {
void EditorPluginList::forward_canvas_force_draw_over_viewport(Control *p_overlay) {

for (int i = 0; i < plugins_list.size(); i++) {
plugins_list[i]->forward_force_draw_over_viewport(p_overlay);
plugins_list[i]->forward_canvas_force_draw_over_viewport(p_overlay);
}
}

void EditorPluginList::forward_spatial_draw_over_viewport(Control *p_overlay) {

for (int i = 0; i < plugins_list.size(); i++) {
plugins_list[i]->forward_spatial_draw_over_viewport(p_overlay);
}
}

void EditorPluginList::forward_spatial_force_draw_over_viewport(Control *p_overlay) {

for (int i = 0; i < plugins_list.size(); i++) {
plugins_list[i]->forward_spatial_force_draw_over_viewport(p_overlay);
}
}

Expand Down
6 changes: 4 additions & 2 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,11 @@ class EditorPluginList : public Object {
void make_visible(bool p_visible);
void edit(Object *p_object);
bool forward_gui_input(const Ref<InputEvent> &p_event);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void forward_canvas_force_draw_over_viewport(Control *p_overlay);
bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled);
void forward_draw_over_viewport(Control *p_overlay);
void forward_force_draw_over_viewport(Control *p_overlay);
void forward_spatial_draw_over_viewport(Control *p_overlay);
void forward_spatial_force_draw_over_viewport(Control *p_overlay);
void add_plugin(EditorPlugin *p_plugin);
void clear();
bool empty();
Expand Down
30 changes: 22 additions & 8 deletions editor/editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,17 @@ bool EditorPlugin::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
return false;
}

void EditorPlugin::forward_draw_over_viewport(Control *p_overlay) {
void EditorPlugin::forward_canvas_draw_over_viewport(Control *p_overlay) {

if (get_script_instance() && get_script_instance()->has_method("forward_draw_over_viewport")) {
get_script_instance()->call("forward_draw_over_viewport", p_overlay);
if (get_script_instance() && get_script_instance()->has_method("forward_canvas_draw_over_viewport")) {
get_script_instance()->call("forward_canvas_draw_over_viewport", p_overlay);
}
}

void EditorPlugin::forward_force_draw_over_viewport(Control *p_overlay) {
void EditorPlugin::forward_canvas_force_draw_over_viewport(Control *p_overlay) {

if (get_script_instance() && get_script_instance()->has_method("forward_force_draw_over_viewport")) {
get_script_instance()->call("forward_force_draw_over_viewport", p_overlay);
if (get_script_instance() && get_script_instance()->has_method("forward_canvas_force_draw_over_viewport")) {
get_script_instance()->call("forward_canvas_force_draw_over_viewport", p_overlay);
}
}

Expand Down Expand Up @@ -545,6 +545,20 @@ bool EditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEv

return false;
}

void EditorPlugin::forward_spatial_draw_over_viewport(Control *p_overlay) {

if (get_script_instance() && get_script_instance()->has_method("forward_spatial_draw_over_viewport")) {
get_script_instance()->call("forward_spatial_draw_over_viewport", p_overlay);
}
}

void EditorPlugin::forward_spatial_force_draw_over_viewport(Control *p_overlay) {

if (get_script_instance() && get_script_instance()->has_method("forward_spatial_force_draw_over_viewport")) {
get_script_instance()->call("forward_spatial_force_draw_over_viewport", p_overlay);
}
}
String EditorPlugin::get_name() const {

if (get_script_instance() && get_script_instance()->has_method("get_plugin_name")) {
Expand Down Expand Up @@ -769,8 +783,8 @@ void EditorPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_script_create_dialog"), &EditorPlugin::get_script_create_dialog);

ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_canvas_gui_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_spatial_gui_input", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_plugin_name"));
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::OBJECT, "get_plugin_icon"));
Expand Down
8 changes: 6 additions & 2 deletions editor/editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ class EditorPlugin : public Node {
void notify_resource_saved(const Ref<Resource> &p_resource);

virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
virtual void forward_draw_over_viewport(Control *p_overlay);
virtual void forward_force_draw_over_viewport(Control *p_overlay);
virtual void forward_canvas_draw_over_viewport(Control *p_overlay);
virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay);

virtual bool forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event);
virtual void forward_spatial_draw_over_viewport(Control *p_overlay);
virtual void forward_spatial_force_draw_over_viewport(Control *p_overlay);

virtual String get_name() const;
virtual const Ref<Texture> get_icon() const;
virtual bool has_main_screen() const;
Expand Down
12 changes: 5 additions & 7 deletions editor/plugins/abstract_polygon_2d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,10 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
return false;
}

void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
void AbstractPolygon2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
if (!_get_node())
return;

Control *vpc = canvas_item_editor->get_viewport_control();

Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_global_transform();
const Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");

Expand Down Expand Up @@ -562,7 +560,7 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
Vector2 point = xform.xform(p);
Vector2 next_point = xform.xform(p2);

vpc->draw_line(point, next_point, col, 2 * EDSCALE);
p_overlay->draw_line(point, next_point, col, 2 * EDSCALE);
}
}

Expand All @@ -586,7 +584,7 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
p2 = points[(i + 1) % n_points] + offset;

const Vector2 next_point = xform.xform(p2);
vpc->draw_line(point, next_point, col, 2 * EDSCALE);
p_overlay->draw_line(point, next_point, col, 2 * EDSCALE);
}
}

Expand All @@ -598,14 +596,14 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
const Vector2 point = xform.xform(p);

const Color modulate = vertex == active_point ? Color(0.5, 1, 2) : Color(1, 1, 1);
vpc->draw_texture(handle, point - handle->get_size() * 0.5, modulate);
p_overlay->draw_texture(handle, point - handle->get_size() * 0.5, modulate);
}
}

if (edge_point.valid()) {

Ref<Texture> add_handle = get_icon("EditorHandleAdd", "EditorIcons");
vpc->draw_texture(add_handle, edge_point.pos - add_handle->get_size() * 0.5);
p_overlay->draw_texture(add_handle, edge_point.pos - add_handle->get_size() * 0.5);
}
}

Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/abstract_polygon_2d_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class AbstractPolygon2DEditor : public HBoxContainer {

public:
bool forward_gui_input(const Ref<InputEvent> &p_event);
void forward_draw_over_viewport(Control *p_overlay);
void forward_canvas_draw_over_viewport(Control *p_overlay);

void edit(Node *p_polygon);
AbstractPolygon2DEditor(EditorNode *p_editor, bool p_wip_destructive = true);
Expand All @@ -153,7 +153,7 @@ class AbstractPolygon2DEditorPlugin : public EditorPlugin {

public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return polygon_editor->forward_gui_input(p_event); }
virtual void forward_draw_over_viewport(Control *p_overlay) { polygon_editor->forward_draw_over_viewport(p_overlay); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }

bool has_main_screen() const { return false; }
virtual String get_name() const { return klass; }
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/animation_player_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ void AnimationPlayerEditor::edit(AnimationPlayer *p_player) {
}
}

void AnimationPlayerEditor::forward_force_draw_over_viewport(Control *p_overlay) {
void AnimationPlayerEditor::forward_canvas_force_draw_over_viewport(Control *p_overlay) {

if (!onion.can_overlay)
return;
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/animation_player_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class AnimationPlayerEditor : public VBoxContainer {

void set_undo_redo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }
void edit(AnimationPlayer *p_player);
void forward_force_draw_over_viewport(Control *p_overlay);
void forward_canvas_force_draw_over_viewport(Control *p_overlay);

AnimationPlayerEditor(EditorNode *p_editor, AnimationPlayerEditorPlugin *p_plugin);
};
Expand All @@ -271,7 +271,7 @@ class AnimationPlayerEditorPlugin : public EditorPlugin {
virtual bool handles(Object *p_object) const;
virtual void make_visible(bool p_visible);

virtual void forward_force_draw_over_viewport(Control *p_overlay) { anim_editor->forward_force_draw_over_viewport(p_overlay); }
virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay) { anim_editor->forward_canvas_force_draw_over_viewport(p_overlay); }

AnimationPlayerEditorPlugin(EditorNode *p_node);
~AnimationPlayerEditorPlugin();
Expand Down
5 changes: 3 additions & 2 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,6 +3026,7 @@ bool CanvasItemEditor::_build_bones_list(Node *p_node) {
}

void CanvasItemEditor::_draw_viewport() {

// Update the transform
transform = Transform2D();
transform.scale_basis(Size2(zoom, zoom));
Expand Down Expand Up @@ -3074,11 +3075,11 @@ void CanvasItemEditor::_draw_viewport() {

EditorPluginList *over_plugin_list = editor->get_editor_plugins_over();
if (!over_plugin_list->empty()) {
over_plugin_list->forward_draw_over_viewport(viewport);
over_plugin_list->forward_canvas_draw_over_viewport(viewport);
}
EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
if (!force_over_plugin_list->empty()) {
force_over_plugin_list->forward_force_draw_over_viewport(viewport);
force_over_plugin_list->forward_canvas_force_draw_over_viewport(viewport);
}

_draw_bones();
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/collision_shape_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void CollisionShape2DEditor::_get_current_shape_type() {
canvas_item_editor->get_viewport_control()->update();
}

void CollisionShape2DEditor::forward_draw_over_viewport(Control *p_overlay) {
void CollisionShape2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {

if (!node) {
return;
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/collision_shape_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CollisionShape2DEditor : public Control {

public:
bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
void forward_draw_over_viewport(Control *p_overlay);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void edit(Node *p_node);

CollisionShape2DEditor(EditorNode *p_editor);
Expand All @@ -89,7 +89,7 @@ class CollisionShape2DEditorPlugin : public EditorPlugin {

public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return collision_shape_2d_editor->forward_canvas_gui_input(p_event); }
virtual void forward_draw_over_viewport(Control *p_overlay) { return collision_shape_2d_editor->forward_draw_over_viewport(p_overlay); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { return collision_shape_2d_editor->forward_canvas_draw_over_viewport(p_overlay); }

virtual String get_name() const { return "CollisionShape2D"; }
bool has_main_screen() const { return false; }
Expand Down
8 changes: 3 additions & 5 deletions editor/plugins/light_occluder_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,11 @@ bool LightOccluder2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
return false;
}

void LightOccluder2DEditor::forward_draw_over_viewport(Control *p_overlay) {
void LightOccluder2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {

if (!node || !node->get_occluder_polygon().is_valid())
return;

Control *vpc = canvas_item_editor->get_viewport_control();

Vector<Vector2> poly;

if (wip_active)
Expand Down Expand Up @@ -353,9 +351,9 @@ void LightOccluder2DEditor::forward_draw_over_viewport(Control *p_overlay) {
if (i == poly.size() - 1 && (!node->get_occluder_polygon()->is_closed() || wip_active)) {

} else {
vpc->draw_line(point, next_point, col, 2);
p_overlay->draw_line(point, next_point, col, 2);
}
vpc->draw_texture(handle, point - handle->get_size() * 0.5);
p_overlay->draw_texture(handle, point - handle->get_size() * 0.5);
}
}

Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/light_occluder_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class LightOccluder2DEditor : public HBoxContainer {

public:
Vector2 snap_point(const Vector2 &p_point) const;
void forward_draw_over_viewport(Control *p_overlay);
void forward_canvas_draw_over_viewport(Control *p_overlay);
bool forward_gui_input(const Ref<InputEvent> &p_event);
void edit(Node *p_collision_polygon);
LightOccluder2DEditor(EditorNode *p_editor);
Expand All @@ -98,7 +98,7 @@ class LightOccluder2DEditorPlugin : public EditorPlugin {

public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return light_occluder_editor->forward_gui_input(p_event); }
virtual void forward_draw_over_viewport(Control *p_overlay) { return light_occluder_editor->forward_draw_over_viewport(p_overlay); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { return light_occluder_editor->forward_canvas_draw_over_viewport(p_overlay); }

virtual String get_name() const { return "LightOccluder2D"; }
bool has_main_screen() const { return false; }
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/path_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
return false;
}

void Path2DEditor::forward_draw_over_viewport(Control *p_overlay) {
void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {

if (!node)
return;
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/path_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Path2DEditor : public HBoxContainer {

public:
bool forward_gui_input(const Ref<InputEvent> &p_event);
void forward_draw_over_viewport(Control *p_overlay);
void forward_canvas_draw_over_viewport(Control *p_overlay);
void edit(Node *p_path2d);
Path2DEditor(EditorNode *p_editor);
};
Expand All @@ -121,7 +121,7 @@ class Path2DEditorPlugin : public EditorPlugin {

public:
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return path2d_editor->forward_gui_input(p_event); }
virtual void forward_draw_over_viewport(Control *p_overlay) { return path2d_editor->forward_draw_over_viewport(p_overlay); }
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { return path2d_editor->forward_canvas_draw_over_viewport(p_overlay); }

virtual String get_name() const { return "Path2D"; }
bool has_main_screen() const { return false; }
Expand Down
5 changes: 2 additions & 3 deletions editor/plugins/spatial_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2312,12 +2312,12 @@ void SpatialEditorViewport::_draw() {

EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
if (!over_plugin_list->empty()) {
over_plugin_list->forward_draw_over_viewport(surface);
over_plugin_list->forward_spatial_draw_over_viewport(surface);
}

EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
if (!force_over_plugin_list->empty()) {
force_over_plugin_list->forward_force_draw_over_viewport(surface);
force_over_plugin_list->forward_spatial_force_draw_over_viewport(surface);
}

if (surface->has_focus()) {
Expand Down Expand Up @@ -2346,7 +2346,6 @@ void SpatialEditorViewport::_draw() {
Point2 center = _point_to_screen(_edit.center);
VisualServer::get_singleton()->canvas_item_add_line(ci, _edit.mouse_pos, center, Color(0.4, 0.7, 1.0, 0.8));
}

if (previewing) {

Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"));
Expand Down
Loading

0 comments on commit 76ca46d

Please sign in to comment.