Skip to content

Commit

Permalink
Added DDScopedConfig3D for future scoped settings
Browse files Browse the repository at this point in the history
Use `PROPERTY_USAGE_NONE` to avoid the warning in the editor
Updated printing macros
Updated logging function
  • Loading branch information
DmitriySalnikov committed Nov 23, 2023
1 parent f904248 commit 4e1699e
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ void DebugDraw2D::_bind_methods() {

#pragma region Parameters

REG_PROP(empty_color, Variant::COLOR);
REG_PROP_BOOL(debug_enabled);
REG_PROP(empty_color, Variant::COLOR, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);
REG_PROP_BOOL(debug_enabled, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

REG_PROP(config, Variant::OBJECT);
REG_PROP(config, Variant::OBJECT, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

REG_PROP(custom_canvas, Variant::OBJECT);
REG_PROP(custom_canvas, Variant::OBJECT, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

#pragma endregion
#undef REG_CLASS_NAME
Expand Down
53 changes: 45 additions & 8 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,36 @@ GODOT_WARNING_RESTORE()

DebugDraw3D *DebugDraw3D::singleton = nullptr;

void DDScopedConfig3D::_bind_methods() {
#define REG_CLASS_NAME DDScopedConfig3D
REG_PROP(empty_color, Variant::COLOR);
#undef REG_CLASS_NAME
}

DDScopedConfig3D::DDScopedConfig3D() {
// TODO check for the first create() for properties defaults
// TODO add this check for DD3D and DD2D to avoid warnings
DEV_PRINT_STD_ERR(NAMEOF(DDScopedConfig3D) " must be called with arguments!\n");
}

DDScopedConfig3D::DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent) {
}

DDScopedConfig3D::~DDScopedConfig3D() {
}

void DebugDraw3D::_bind_methods() {
#define REG_CLASS_NAME DebugDraw3D

#pragma region Parameters

REG_PROP(empty_color, Variant::COLOR);
REG_PROP_BOOL(debug_enabled);
// HACK PROPERTY_USAGE_NONE disable "Instantiated OBJECT used as default value" warning
REG_PROP(empty_color, Variant::COLOR, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);
REG_PROP_BOOL(debug_enabled, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

REG_PROP(config, Variant::OBJECT);
REG_PROP(config, Variant::OBJECT, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

REG_PROP(custom_viewport, Variant::OBJECT);
REG_PROP(custom_viewport, Variant::OBJECT, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE);

#pragma endregion
#undef REG_CLASS_NAME
Expand Down Expand Up @@ -95,9 +114,7 @@ void DebugDraw3D::init(DebugDrawManager *root) {
_load_materials();

#ifndef DISABLE_DEBUG_RENDERING
#ifdef DEV_ENABLED
DEV_PRINT_STD_F("To regenerate the 3D geometry press the \"%s\" action\n", reload_action_name);
#endif
DEV_PRINT_STD("To regenerate the 3D geometry press the \"%s\" action\n", reload_action_name);

dgc = std::make_unique<DebugGeometryContainer>(this);
#endif
Expand Down Expand Up @@ -141,6 +158,26 @@ void DebugDraw3D::process(double delta) {
#endif
}

Ref<DDScopedConfig3D> DebugDraw3D::new_scoped_config() {
static std::atomic<uint64_t> create_counter = 0;
create_counter++;

return Ref<DDScopedConfig3D>();
}

DDScopedConfig3D *DebugDraw3D::get_transform_for_current_thread() {
return nullptr;
}

void DebugDraw3D::register_scoped_config(uint64_t guard_id, uint64_t thread_id, DDScopedConfig3D *cfg) {
}

void DebugDraw3D::unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) {
}

void DebugDraw3D::clear_scoped_configs() {
}

void DebugDraw3D::_load_materials() {
#ifndef DISABLE_DEBUG_RENDERING
shader_unshaded_code.instantiate();
Expand Down Expand Up @@ -670,4 +707,4 @@ void DebugDraw3D::draw_camera_frustum_planes(const Array &camera_frustum, const
#endif

#undef IS_DEFAULT_COLOR
#undef NEED_LEAVE
#undef NEED_LEAVE
39 changes: 38 additions & 1 deletion src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,34 @@ class DebugDrawConfig3D;
class DebugDrawStats3D;
class DebugGeometryContainer;

class DebugDraw3D : public Object {
class DDScopedConfig3D : public RefCounted {
GDCLASS(DDScopedConfig3D, RefCounted)

protected:
static void _bind_methods();
uint64_t thread_id = 0;
uint64_t guard_id = 0;
void set_empty_color(const Color &_col){};
Color get_empty_color() const { return Color(); };

public:
DDScopedConfig3D();
DDScopedConfig3D(const uint64_t &p_thread_id, const uint64_t &p_guard_id, const DDScopedConfig3D *parent);
~DDScopedConfig3D();
};

template <class TCfgStorage>
class ScopedStorage {
private:
virtual TCfgStorage *get_transform_for_current_thread() = 0;

public:
virtual void register_scoped_config(uint64_t guard_id, uint64_t thread_id, TCfgStorage *cfg) = 0;
virtual void unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) = 0;
virtual void clear_scoped_configs() = 0;
};

class DebugDraw3D : public Object, public ScopedStorage<DDScopedConfig3D> {
GDCLASS(DebugDraw3D, Object)

friend DebugDrawManager;
Expand All @@ -32,6 +59,9 @@ class DebugDraw3D : public Object {
std::vector<SubViewport *> custom_editor_viewports;
DebugDrawManager *root_node = nullptr;

// Inherited via ScopedStorage
DDScopedConfig3D *get_transform_for_current_thread() override;

#ifndef DISABLE_DEBUG_RENDERING
#ifdef DEV_ENABLED
const char *reload_action_name = "ui_end";
Expand Down Expand Up @@ -82,6 +112,13 @@ class DebugDraw3D : public Object {
return singleton;
};

Ref<DDScopedConfig3D> new_scoped_config();

// Inherited via ScopedStorage
void register_scoped_config(uint64_t guard_id, uint64_t thread_id, DDScopedConfig3D *cfg) override;
void unregister_scoped_config(uint64_t guard_id, uint64_t thread_id) override;
void clear_scoped_configs() override;

Node *get_root_node();
void set_custom_editor_viewport(std::vector<SubViewport *> _viewports);
std::vector<SubViewport *> get_custom_editor_viewports();
Expand Down
18 changes: 3 additions & 15 deletions src/debug_draw_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const char *DebugDrawManager::s_extension_unloading = "extension_unloading";
void DebugDrawManager::_bind_methods() {
#define REG_CLASS_NAME DebugDrawManager

// TODO USE CALLABLE_MP!
// ClassDB::bind_method(D_METHOD(NAMEOF(get_title)), &DebugDrawGraph::get_title);
ClassDB::bind_method(D_METHOD(NAMEOF(_add_to_tree)), &DebugDrawManager::_add_to_tree);
// TODO use CALLABLE_MP in 4.2!
ClassDB::bind_method(D_METHOD(NAMEOF(_integrate_into_engine)), &DebugDrawManager::_integrate_into_engine);
ClassDB::bind_method(D_METHOD(NAMEOF(_on_scene_changed)), &DebugDrawManager::_on_scene_changed);

Expand Down Expand Up @@ -81,7 +79,7 @@ void DebugDrawManager::_on_scene_changed(bool _is_scene_null) {
#endif
}

void DebugDrawManager::_add_to_tree() {
void DebugDrawManager::_integrate_into_engine() {
// Assigned here because it is created inside the `GDExtension Initialize` function.
// 1. Create in Initialize and assign Singleton
// 2. Call class constructor after Initialize to get default values of properties
Expand All @@ -92,13 +90,8 @@ void DebugDrawManager::_add_to_tree() {
SCENE_ROOT()->add_child(this);
SCENE_ROOT()->move_child(this, 0);

// Then wait for `_ready` to continue
}

void DebugDrawManager::_integrate_into_engine() {
set_process(true);

// Need to be call 'deferred'
debug_draw_3d_singleton->init(this);
debug_draw_2d_singleton->init(this);

Expand Down Expand Up @@ -226,7 +219,7 @@ void DebugDrawManager::init() {
Engine::get_singleton()->register_singleton(NAMEOF(DebugDraw2D), debug_draw_2d_singleton);
Engine::get_singleton()->register_singleton("Dbg2", debug_draw_2d_singleton);

call_deferred(NAMEOF(_add_to_tree));
call_deferred(NAMEOF(_integrate_into_engine));
}

void DebugDrawManager::_process(double delta) {
Expand Down Expand Up @@ -254,11 +247,6 @@ void DebugDrawManager::_process(double delta) {
}
}

void DebugDrawManager::_ready() {
// HACK Call deferred to avoid setting the instance of `config` as a standard parameter value
call_deferred(NAMEOF(_integrate_into_engine));
}

void DebugDrawManager::_exit_tree() {
is_closing = true;

Expand Down
3 changes: 0 additions & 3 deletions src/debug_draw_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ class DebugDrawManager : public CanvasLayer {
void _connect_scene_changed();
void _on_scene_changed(bool _is_scene_null);

void _add_to_tree();
void _integrate_into_engine();

#ifdef TOOLS_ENABLED
void _try_to_update_cs_bindings();
#endif

public:

static const char *s_extension_unloading;

DebugDrawManager();
Expand All @@ -53,6 +51,5 @@ class DebugDrawManager : public CanvasLayer {

void init();
virtual void _process(double delta) override;
virtual void _ready() override;
virtual void _exit_tree() override;
};
16 changes: 8 additions & 8 deletions src/editor/asset_library_update_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void AssetLibraryUpdateChecker::init() {

err = http->poll();
if (err != Error::OK) {
PRINT_ERROR(FMT_STR("DebugDraw Updater: Failed to initialize connection. Error: {0}", UtilityFunctions::error_string(err)));
PRINT_ERROR("DebugDraw Updater: Failed to initialize connection. Error: {0}", UtilityFunctions::error_string(err));
return;
}
} else {
Expand All @@ -118,7 +118,7 @@ void AssetLibraryUpdateChecker::init() {
while (http.is_valid() && !is_thread_closing) {
err = http->poll();
if (err != Error::OK) {
PRINT_ERROR(FMT_STR("DebugDraw Updater: Failed to connect. Error: {0}", UtilityFunctions::error_string(err)));
PRINT_ERROR("DebugDraw Updater: Failed to connect. Error: {0}", UtilityFunctions::error_string(err));
return;
}

Expand All @@ -129,15 +129,15 @@ void AssetLibraryUpdateChecker::init() {
case godot::HTTPClient::STATUS_CANT_RESOLVE:
case godot::HTTPClient::STATUS_CANT_CONNECT:
case godot::HTTPClient::STATUS_TLS_HANDSHAKE_ERROR:
PRINT_ERROR(FMT_STR("DebugDraw Updater: Connection error: {0}", status));
PRINT_ERROR("DebugDraw Updater: Connection error: {0}", status);
return;
case godot::HTTPClient::STATUS_RESOLVING:
case godot::HTTPClient::STATUS_CONNECTING:
case godot::HTTPClient::STATUS_REQUESTING:
case godot::HTTPClient::STATUS_BODY:
default:
if (status != prev_status) {
DEV_PRINT(FMT_STR("DebugDraw Updater: Connecting status: {0}", status));
DEV_PRINT_STD("DebugDraw Updater: Connecting status: %d\n", status);
}
break;
case godot::HTTPClient::STATUS_CONNECTED:
Expand All @@ -156,15 +156,15 @@ void AssetLibraryUpdateChecker::init() {
String request_url = "/asset-library/api/asset/" + String::num_int64(addon_id);
err = http->request(HTTPClient::METHOD_GET, request_url, PackedStringArray());
if (err != Error::OK) {
PRINT_ERROR(FMT_STR("DebugDraw Updater: Failed to create a request. Error: {0}", UtilityFunctions::error_string(err)));
PRINT_ERROR("DebugDraw Updater: Failed to create a request. Error: {0}", UtilityFunctions::error_string(err));
return;
}

while (http.is_valid() && !is_thread_closing) {

err = http->poll();
if (err != Error::OK) {
PRINT_ERROR(FMT_STR("DebugDraw Updater: Failed to get a response from \"{0}\". Error: {1}", godot_domain + request_url, UtilityFunctions::error_string(err)));
PRINT_ERROR("DebugDraw Updater: Failed to get a response from \"{0}\". Error: {1}", godot_domain + request_url, UtilityFunctions::error_string(err));
return;
}

Expand All @@ -184,15 +184,15 @@ void AssetLibraryUpdateChecker::init() {
return;
} else {
if (code != 0) {
PRINT_ERROR(FMT_STR("DebugDraw Updater: Failed to get a response from \"{0}\". Code: {1}", godot_domain + request_url, code));
PRINT_ERROR("DebugDraw Updater: Failed to get a response from \"{0}\". Code: {1}", godot_domain + request_url, code);
return;
}
}

std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

DEV_PRINT(FMT_STR("DebugDraw Updater: Thread finished"));
DEV_PRINT_STD("DebugDraw Updater: Thread finished\n");
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/editor/editor_menu_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ inline String DebugDrawMenuExtensionPlugin::_get_plugin_name() const {
}

void DebugDrawMenuExtensionPlugin::_enter_tree() {
DEV_PRINT(NAMEOF(DebugDrawMenuExtensionPlugin) " _enter_tree");
DEV_PRINT_STD(NAMEOF(DebugDrawMenuExtensionPlugin) " _enter_tree\n");

PopupMenu *menu = memnew(PopupMenu);
menu->connect("id_pressed", Callable(this, NAMEOF(_on_id_pressed)));
Expand Down Expand Up @@ -69,13 +69,13 @@ void DebugDrawMenuExtensionPlugin::_on_id_pressed(MenuItemId id) {
}

DebugDrawMenuExtensionPlugin::DebugDrawMenuExtensionPlugin() {
DEV_PRINT(NAMEOF(DebugDrawMenuExtensionPlugin) " constructor");
DEV_PRINT_STD(NAMEOF(DebugDrawMenuExtensionPlugin) " constructor\n");

menu_item_name = "Debug Draw";
}

DebugDrawMenuExtensionPlugin::~DebugDrawMenuExtensionPlugin() {
// DEV_PRINT(NAMEOF(DebugDrawMenuExtensionPlugin) " deconstructor");
// DEV_PRINT_STD(NAMEOF(DebugDrawMenuExtensionPlugin) " deconstructor\n");
// remove_tool_menu_item(menu_item_name);
}

Expand Down
1 change: 1 addition & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void initialize_debug_draw_3d_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<DebugDraw3D>();
ClassDB::register_class<DebugDrawStats3D>();
ClassDB::register_class<DebugDrawConfig3D>();
ClassDB::register_class<DDScopedConfig3D>();

ClassDB::register_class<DebugDrawManager>();

Expand Down
8 changes: 4 additions & 4 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ void Utils::print_logs() {
auto &l = log_buffer.front();
if (l.repeat > 1) {
if (l.is_error) {
fprintf(stderr, "[x%d] %s", l.repeat, l.text.c_str());
fprintf(stderr, "[Error] [x%d] %s", l.repeat, l.text.c_str());
} else {
printf("[x%d] %s", l.repeat, l.text.c_str());
printf("[Info] [x%d] %s", l.repeat, l.text.c_str());
// fflush(stdout);
}
} else {
if (l.is_error) {
fprintf(stderr, l.text.c_str());
fprintf(stderr, "[Error] %s", l.text.c_str());
} else {
printf(l.text.c_str());
printf("[Info] %s", l.text.c_str());
// fflush(stdout);
}
}
Expand Down
Loading

0 comments on commit 4e1699e

Please sign in to comment.