Skip to content

Commit

Permalink
Merge pull request #168 from goostengine/debug-2d-grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Xrayez authored Dec 18, 2021
2 parents 15a75fa + 626565a commit 154398d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 15 deletions.
13 changes: 13 additions & 0 deletions doc/Debug2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@
Returns [DebugCapture] object to manage history of draw commands.
</description>
</method>
<method name="get_grid" qualifiers="const">
<return type="GridRect" />
<description>
Returns the default [GridRect] used to draw an infinite grid at run-time.
The grid drawing is disabled by default, so if you want to use it, you have to go into project settings and enable it manually. You can change the grid properties both by configuring project settings at [code]debug/draw/2d/grid[/code] and via code:
[codeblock]
func _ready():
var grid: GridRect = Debug2D.get_grid()
grid.show()
[/codeblock]
If you use [Camera2D] or change [member Viewport.canvas_transform], the grid's position and scale are going to be updated automatically to simulate an infinite grid. Note that [GridRect] doesn't currently support rotated grid lines.
</description>
</method>
<method name="update">
<return type="void" />
<description>
Expand Down
2 changes: 1 addition & 1 deletion goost.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def add_depencency(self, goost_class):
# If so, define them here explicitly so that they're automatically enabled.
class_dependencies = {
"CommandLineParser": ["CommandLineOption", "CommandLineHelpFormat"],
"Debug2D": ["DebugCapture", "GoostGeometry2D"],
"Debug2D": ["DebugCapture", "GoostGeometry2D", "GridRect"],
"GoostEngine" : "InvokeState",
"GoostGeometry2D" : ["PolyBoolean2D", "PolyDecomp2D", "PolyOffset2D"],
"LightTexture" : "GradientTexture2D",
Expand Down
33 changes: 32 additions & 1 deletion scene/2d/debug_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "goost/core/math/geometry/2d/goost_geometry_2d.h"

#include "scene/main/viewport.h"
#include "scene/resources/theme.h"
#include "scene/scene_string_names.h"

Expand Down Expand Up @@ -183,6 +184,23 @@ Variant Debug2D::_option_get_value(const String &p_option, const Variant &p_valu
return ret;
}

void Debug2D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_PROCESS: {
if (grid_rect->is_visible()) {
const Transform2D &ct = get_viewport()->get_canvas_transform();

if (grid_rect->get_origin_offset() != ct.get_origin()) {
grid_rect->set_origin_offset(ct.get_origin());
}
if (grid_rect->get_origin_scale() != ct.get_scale()) {
grid_rect->set_origin_scale(ct.get_scale());
}
}
} break;
}
}

void Debug2D::_push_command(DrawCommand &p_command) {
p_command.canvas_item = canvas_item;
commands.push_back(p_command);
Expand Down Expand Up @@ -449,13 +467,25 @@ Debug2D::Debug2D() {
ERR_FAIL_COND_MSG(singleton != nullptr, "Singleton already exists");
singleton = this;
state.instance();
set_process(true);

// Base for drawing.
base = memnew(Node2D);
set_canvas_item(base);

base->set_name("Canvas");
add_child(base);

// Grid layer.
grid_layer = memnew(CanvasLayer);
grid_layer->set_name("GridLayer");
grid_layer->set_layer(GLOBAL_GET("debug/draw/2d/grid/layer"));
add_child(grid_layer);

grid_rect = memnew(GridRect);
grid_rect->set_name("Grid");
grid_layer->add_child(grid_rect);
grid_rect->set_anchors_and_margins_preset(Control::PRESET_WIDE);

draw_reset();

default_value["color"] = GLOBAL_GET("debug/draw/2d/color");
Expand All @@ -479,6 +509,7 @@ void Debug2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_canvas_item", "canvas_item"), &Debug2D::set_canvas_item);
ClassDB::bind_method(D_METHOD("get_canvas_item"), &Debug2D::get_canvas_item);
ClassDB::bind_method(D_METHOD("get_base"), &Debug2D::get_base);
ClassDB::bind_method(D_METHOD("get_grid"), &Debug2D::get_grid);

ClassDB::bind_method(D_METHOD("draw", "method", "args"), &Debug2D::draw, DEFVAL(Variant()));

Expand Down
9 changes: 9 additions & 0 deletions scene/2d/debug_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "core/engine.h"
#include "core/resource.h"
#include "scene/2d/node_2d.h"
#include "scene/main/canvas_layer.h"

#include "scene/gui/grid_rect.h"

class DebugCapture;

Expand All @@ -15,6 +18,10 @@ class Debug2D : public Node {
bool enabled = true;

Node2D *base = nullptr;

CanvasLayer *grid_layer = nullptr;
GridRect *grid_rect = nullptr;

ObjectID canvas_item; // Currently used item passed to draw commands.

Dictionary draw_override;
Expand Down Expand Up @@ -48,6 +55,7 @@ class Debug2D : public Node {

protected:
static void _bind_methods();
void _notification(int p_what);

void _on_canvas_item_draw(Object *p_item);
void _push_command(DrawCommand &p_command);
Expand All @@ -66,6 +74,7 @@ class Debug2D : public Node {
void set_canvas_item(Object *p_canvas_item);
Object *get_canvas_item() const;
Object *get_base() const { return base; } // The default canvas item.
GridRect *get_grid() const { return grid_rect; }

// Custom drawing using one of the `CanvasItem.draw_*` methods.
void draw(const StringName &p_method, const Array &p_args = Array());
Expand Down
2 changes: 1 addition & 1 deletion scene/gui/grid_rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class GridRect : public Control {
int divisions_horizontal = 8;
int divisions_vertical = 8;
float divisions_line_width = 1.0;

Vector2 origin_offset;
Vector2 origin_scale = Vector2(1, 1);

bool origin_centered = false;
bool origin_axes_visible = false;
float origin_axes_line_width = 1.0;
Expand Down
90 changes: 78 additions & 12 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,43 @@ static void _debug_2d_add_to_scene_tree() {
if (_debug_2d_added) {
return;
}
Debug2D::get_singleton()->set_name("Debug2D");
auto debug_2d = Debug2D::get_singleton();
bool editor = Engine::get_singleton()->is_editor_hint();
debug_2d->set_name("Debug2D");
debug_2d->set_enabled(GLOBAL_GET("debug/draw/2d/enabled"));

if (Engine::get_singleton()->is_editor_hint()) {
if (editor) {
#ifdef TOOLS_ENABLED
EditorNode::get_singleton()->get_scene_root()->add_child(Debug2D::get_singleton());
EditorNode::get_singleton()->get_scene_root()->add_child(debug_2d);
debug_2d->get_grid()->hide();
#endif
} else {
if (!SceneTree::get_singleton()) {
return;
}
SceneTree::get_singleton()->get_root()->add_child(Debug2D::get_singleton());
SceneTree::get_singleton()->get_root()->add_child(debug_2d);

GridRect *grid = debug_2d->get_grid();
grid->set_visible(GLOBAL_GET("debug/draw/2d/grid/visible"));

List<PropertyInfo> grid_props;
ClassDB::get_property_list("GridRect", &grid_props, true);
for (List<PropertyInfo>::Element *E = grid_props.front(); E; E = E->next()) {
const PropertyInfo &p = E->get();
if (p.usage & PROPERTY_USAGE_GROUP) {
continue;
}
if (p.name == "origin_offset" || p.name == "origin_scale" || p.name == "origin_centered") {
continue;
}
grid->set(p.name, GLOBAL_GET("debug/draw/2d/grid/" + p.name));
}
grid->set("custom_colors/line_axis_x", GLOBAL_GET("debug/draw/2d/grid/origin_axis_x_color"));
grid->set("custom_colors/line_axis_y", GLOBAL_GET("debug/draw/2d/grid/origin_axis_y_color"));
grid->set("custom_colors/line_cell", GLOBAL_GET("debug/draw/2d/grid/cell_line_color"));
grid->set("custom_colors/line_division", GLOBAL_GET("debug/draw/2d/grid/divisions_line_color"));
grid->set("custom_colors/background", GLOBAL_GET("debug/draw/2d/grid/background_color"));
}
Debug2D::get_singleton()->set_enabled(GLOBAL_GET("debug/draw/2d/enabled"));

_debug_2d_added = true;
}
#endif
Expand All @@ -62,31 +85,74 @@ void register_scene_types() {
ClassDB::register_class<GradientTexture2D>();
ClassDB::register_class<LightTexture>();

#ifdef GOOST_GUI_ENABLED
// Before Debug2D, need to access default values, property hints etc.
ClassDB::register_class<GridRect>();
#endif

#if defined(GOOST_GEOMETRY_ENABLED) && defined(GOOST_Debug2D)
// Define project settings before registering classes.
GLOBAL_DEF("debug/draw/2d/enabled", true);
GLOBAL_DEF("debug/draw/2d/color", Color(0.0, 0.6, 0.7, 1));
GLOBAL_DEF("debug/draw/2d/filled", true);
GLOBAL_DEF("debug/draw/2d/line_width", 1.0);
ProjectSettings::get_singleton()->set_custom_property_info("debug/draw/2d/line_width", PropertyInfo(Variant::REAL, "debug/draw/2d/line_width", PROPERTY_HINT_RANGE, "0.1,5.0,0.1,or_greater"));
ProjectSettings::get_singleton()->set_custom_property_info("debug/draw/2d/line_width",
PropertyInfo(Variant::REAL, "debug/draw/2d/line_width", PROPERTY_HINT_RANGE, "0.1,5.0,0.1,or_greater"));
GLOBAL_DEF("debug/draw/2d/antialiased", false);

// Grid layer.
List<PropertyInfo> grid_props;
ClassDB::get_property_list("GridRect", &grid_props, true);

GLOBAL_DEF("debug/draw/2d/grid/visible", false);
GLOBAL_DEF("debug/draw/2d/grid/layer", 1); // Show on top by default.

auto g = memnew(GridRect);
for (List<PropertyInfo>::Element *E = grid_props.front(); E; E = E->next()) {
const PropertyInfo &p = E->get();
if (p.usage & PROPERTY_USAGE_GROUP) {
continue;
}
if (p.name == "origin_offset" || p.name == "origin_scale" || p.name == "origin_centered") {
continue;
}
const String &setting = "debug/draw/2d/grid/" + p.name;

if (p.name == "cell_line_width") {
GLOBAL_DEF(setting, g->get(p.name));
ProjectSettings::get_singleton()->set_custom_property_info(setting, p);
GLOBAL_DEF("debug/draw/2d/grid/cell_line_color", Color(1.0, 1.0, 1.0, 0.07)); // From GraphEdit in godot/editor/editor_themes.cpp

} else if (p.name == "divisions_line_width") {
GLOBAL_DEF(setting, g->get(p.name));
ProjectSettings::get_singleton()->set_custom_property_info(setting, p);
GLOBAL_DEF("debug/draw/2d/grid/divisions_line_color", Color(1.0, 1.0, 1.0, 0.15)); // From GraphEdit in godot/editor/editor_themes.cpp

} else if (p.name == "origin_axes_visible") {
GLOBAL_DEF(setting, true);
GLOBAL_DEF("debug/draw/2d/grid/origin_axis_x_color", Color(0.96, 0.20, 0.32)); // From godot/editor/editor_themes.cpp
GLOBAL_DEF("debug/draw/2d/grid/origin_axis_y_color", Color(0.53, 0.84, 0.01)); // From godot/editor/editor_themes.cpp
} else {
GLOBAL_DEF(setting, g->get(p.name));
ProjectSettings::get_singleton()->set_custom_property_info(setting, p);
}
}
GLOBAL_DEF("debug/draw/2d/grid/background_color", Color(0, 0, 0, 0));
memdelete(g);

// End grid setup.

ClassDB::register_virtual_class<Debug2D>();
ClassDB::register_virtual_class<DebugCapture>();

_debug_2d = memnew(Debug2D);
Engine::get_singleton()->add_singleton(Engine::Singleton("Debug2D", Debug2D::get_singleton()));
SceneTree::add_idle_callback(&_debug_2d_add_to_scene_tree);

#endif
#ifdef GOOST_AUDIO_ENABLED
register_audio_types();
#endif

#ifdef GOOST_GUI_ENABLED
ClassDB::register_class<GridRect>();
#endif

#if defined(TOOLS_ENABLED) && defined(GOOST_EDITOR_ENABLED)
#if defined(GOOST_GEOMETRY_ENABLED) && defined(GOOST_PolyNode2D)
EditorPlugins::add_by_type<PolyNode2DEditorPlugin>();
Expand Down

0 comments on commit 154398d

Please sign in to comment.