Skip to content

Commit

Permalink
fixed canvas switching
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Jan 19, 2023
1 parent 0e8195b commit 1e5ba73
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 47 deletions.
3 changes: 2 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func _process(delta: float) -> void:
# Testing the rendering layers by showing the image from the second camera inside the 2D panel
DebugDraw.geometry_render_layers = 1 if !Input.is_key_pressed(KEY_SHIFT) else 0b10010
$Panel.visible = Input.is_key_pressed(KEY_SHIFT)
DebugDraw.custom_canvas = $CustomCanvas if Input.is_key_pressed(KEY_SHIFT) else null

# More property toggles
DebugDraw.freeze_3d_render = Input.is_key_pressed(KEY_ENTER)
Expand Down Expand Up @@ -298,7 +299,7 @@ func _graph_test():
#DebugDraw.get_graph(&"fps11").line_position = DebugDrawGraph.LINE_BOTTOM
DebugDraw.get_graph(&"fps11").offset = Vector2i(16, 0)
DebugDraw.get_graph(&"fps6").offset = Vector2i(0, 32)
DebugDraw.get_graph(&"fps").offset = Vector2i(16, 32)
DebugDraw.get_graph(&"fps").offset = Vector2i(16, 72)

DebugDraw.get_graph(&"fps9").enabled = graph_is_enabled
if Engine.is_editor_hint():
Expand Down
15 changes: 14 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func _on_Button_pressed() -> void:
process_priority = 1
script = ExtResource("1")
custom_font = ExtResource("2_aedbq")
test_text = false
text_groups_position = 2
text_groups_default_font_size = 15
text_groups_title_font_size = 20
Expand Down Expand Up @@ -457,6 +456,20 @@ libraries = {
"": SubResource("AnimationLibrary_a7f1a")
}
[node name="CustomCanvas" type="Control" parent="."]
layout_mode = 3
anchors_preset = 11
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -639.0
offset_top = 36.0
offset_right = -36.0
offset_bottom = -36.0
grow_horizontal = 0
grow_vertical = 2
mouse_filter = 2
[node name="Settings" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
Expand Down
54 changes: 19 additions & 35 deletions src/debug_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,14 @@ void DebugDraw::enter_tree() {
void DebugDraw::exit_tree() {
_font.unref();

if (default_canvas && default_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)))) {
if (UtilityFunctions::is_instance_valid(default_canvas) && default_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)))) {
default_canvas->disconnect("draw", Callable(this, TEXT(_on_canvas_item_draw)));
current_draw_canvas = nullptr;
default_canvas->queue_redraw();
}
if (custom_canvas && custom_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)))) {
if (UtilityFunctions::is_instance_valid(custom_canvas) && custom_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)))) {
custom_canvas->disconnect("draw", Callable(this, TEXT(_on_canvas_item_draw)));
current_draw_canvas = nullptr;
}

// Clear editor canvas
if (custom_canvas)
custom_canvas->queue_redraw();
}
}

void DebugDraw::ready() {
Expand Down Expand Up @@ -225,15 +221,14 @@ void DebugDraw::ready() {

Node *n = res->get_child(0)->get_child(0);
n->set_meta("UseParentSize", true);
set_custom_canvas((CanvasItem *)n);
default_canvas = (Control *)n;
// if (res) {
// res->add_child(_canvasLayer);
// _canvasLayer->add_child(default_canvas);
// } else {
// ERR_FAIL_MSG("\"Node3DEditorViewportContainer\" not found in editor tree!");
// }


/*
// Used to explore the editor tree.
auto f = FileAccess::open("user://tree.txt", FileAccess::WRITE);
Expand All @@ -251,12 +246,8 @@ void DebugDraw::ready() {
base_node->add_child(_canvas_layer);
_canvas_layer->add_child(default_canvas);
base_node->move_child(_canvas_layer, 0);

if (!custom_canvas) { // && godot_is_instance_valid(custom_canvas))
default_canvas->connect("draw", Callable(this, TEXT(_on_canvas_item_draw))); // TODO: use bind()
current_draw_canvas = default_canvas;
}
}
set_custom_canvas(custom_canvas);
_set_base_world_node(SCENE_ROOT());
}

Expand All @@ -269,9 +260,9 @@ void DebugDraw::process(double delta) {

// Update overlay
if (_canvas_need_update) {
if (!custom_canvas) // && godot_is_instance_valid(custom_canvas))
if (!UtilityFunctions::is_instance_valid(custom_canvas) && UtilityFunctions::is_instance_valid(default_canvas))
default_canvas->queue_redraw();
else
else if (UtilityFunctions::is_instance_valid(custom_canvas))
custom_canvas->queue_redraw();

// reset some values
Expand All @@ -290,13 +281,8 @@ void DebugDraw::process(double delta) {
}
}

// TODO: use version with pointer
// void DebugDraw::_on_canvas_item_draw(CanvasItem *ci) {
void DebugDraw::_on_canvas_item_draw() {
// TODO: remove it
CanvasItem *ci = current_draw_canvas;

Vector2 vp_size = ci->has_meta("UseParentSize") ? Object::cast_to<Control>(ci->get_parent())->get_rect().size : ci->get_viewport_rect().size;
void DebugDraw::_on_canvas_item_draw(Control *ci) {
Vector2 vp_size = ci->has_meta("UseParentSize") ? Object::cast_to<Control>(ci->get_parent())->get_rect().size : ci->get_rect().size;

grouped_text->draw(ci, _font, vp_size);
data_graphs->draw(ci, _font, vp_size, ci->get_process_delta_time());
Expand Down Expand Up @@ -481,34 +467,32 @@ Viewport *DebugDraw::get_custom_viewport() {
return custom_viewport;
}

void DebugDraw::set_custom_canvas(CanvasItem *_canvas) {
bool connected_internal = default_canvas && default_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)));
bool connected_custom = custom_canvas && custom_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)));
void DebugDraw::set_custom_canvas(Control *_canvas) {
bool connected_internal = UtilityFunctions::is_instance_valid(default_canvas) && default_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)));
bool connected_custom = UtilityFunctions::is_instance_valid(custom_canvas) && custom_canvas->is_connected("draw", Callable(this, TEXT(_on_canvas_item_draw)));

if (!_canvas) {
if (!connected_internal) {
default_canvas->connect("draw", Callable(this, TEXT(_on_canvas_item_draw))); // TODO: use bind()
current_draw_canvas = default_canvas;
if (!connected_internal && UtilityFunctions::is_instance_valid(default_canvas)) {
default_canvas->connect("draw", Callable(this, TEXT(_on_canvas_item_draw)).bindv(Array::make(default_canvas)));
}
if (connected_custom) {
custom_canvas->disconnect("draw", Callable(this, TEXT(_on_canvas_item_draw)));
current_draw_canvas = nullptr;
custom_canvas->queue_redraw();
}
} else {
if (connected_internal) {
default_canvas->disconnect("draw", Callable(this, TEXT(_on_canvas_item_draw)));
current_draw_canvas = nullptr;
default_canvas->queue_redraw();
}
if (!connected_custom) {
_canvas->connect("draw", Callable(this, TEXT(_on_canvas_item_draw))); // TODO: use bind()
current_draw_canvas = _canvas;
_canvas->connect("draw", Callable(this, TEXT(_on_canvas_item_draw)).bindv(Array::make(_canvas)));
}
}

custom_canvas = _canvas;
}

CanvasItem *DebugDraw::get_custom_canvas() {
Control *DebugDraw::get_custom_canvas() {
return custom_canvas;
}

Expand Down
15 changes: 5 additions & 10 deletions src/debug_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ class DebugDraw : public Object {
bool _canvas_need_update = true;
Ref<Font> _font;

CanvasItem *default_canvas = nullptr;

// TODO: remove it and use Callable::bind in connect methods
CanvasItem *current_draw_canvas = nullptr;
Control *default_canvas = nullptr;

// Text
std::unique_ptr<GroupedText> grouped_text;
Expand All @@ -73,10 +70,8 @@ class DebugDraw : public Object {
DebugDraw *get_singleton_gdscript() {
return singleton;
};
// TODO: use pointer to CanvasItem from Callable::bind
// void _on_canvas_item_draw(CanvasItem *ci);
void _on_canvas_item_draw();

void _on_canvas_item_draw(Control *ci);
void _set_base_world_node(Node *world_base);

void enter_tree();
Expand Down Expand Up @@ -138,7 +133,7 @@ class DebugDraw : public Object {
/// Custom 'Viewport' to use for frustum culling.
Viewport *custom_viewport = nullptr;
/// Custom 'CanvasItem' to draw on it. Set to 'null' to disable.
CanvasItem *custom_canvas = nullptr;
Control *custom_canvas = nullptr;
#pragma endregion // Exposed Parameter Values

protected:
Expand Down Expand Up @@ -214,8 +209,8 @@ class DebugDraw : public Object {
void set_custom_viewport(Viewport *_viewport);
Viewport *get_custom_viewport();

void set_custom_canvas(CanvasItem *_canvas);
CanvasItem *get_custom_canvas();
void set_custom_canvas(Control *_canvas);
Control *get_custom_canvas();
#pragma endregion // Exposed Parametes

#pragma region Exposed Draw Functions
Expand Down

0 comments on commit 1e5ba73

Please sign in to comment.