Skip to content

Commit

Permalink
Free removed child nodes + godot:: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Jul 26, 2024
1 parent 46c3009 commit 164db4e
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
5 changes: 4 additions & 1 deletion extension/src/openvic-extension/classes/GUIListBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ void GUIListBox::clear() {
clear_children();
if (scrollbar != nullptr) {
remove_child(scrollbar);
scrollbar->queue_free();
scrollbar = nullptr;
}
}
Expand All @@ -192,7 +193,9 @@ void GUIListBox::clear_children(int32_t remaining_child_count) {
int32_t child_index = get_child_count();

while (child_index > remaining_child_count) {
remove_child(get_child(--child_index));
Node* child = get_child(--child_index);
remove_child(child);
child->queue_free();
}

if (scrollbar != nullptr) {
Expand Down
34 changes: 33 additions & 1 deletion extension/src/openvic-extension/classes/GUINode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ void GUINode::_bind_methods() {
OV_BIND_METHOD(GUINode::hide_node, { "path" });
OV_BIND_METHOD(GUINode::hide_nodes, { "paths" });

OV_BIND_METHOD(GUINode::remove_node, { "path" });
OV_BIND_METHOD(GUINode::remove_nodes, { "paths" });

OV_BIND_SMETHOD(int_to_string_suffixed, { "val" });
OV_BIND_SMETHOD(float_to_string_suffixed, { "val" });
OV_BIND_SMETHOD(float_to_string_dp, { "val", "decimal_places" });
Expand Down Expand Up @@ -207,17 +210,46 @@ APPLY_TO_TEXTURE_TYPES(TEXTURE_GET_FUNCTIONS)
Error GUINode::hide_node(NodePath const& path) const {
CanvasItem* node = _cast_node<CanvasItem>(get_node_internal(path));
ERR_FAIL_NULL_V(node, FAILED);

node->hide();

return OK;
}

Error GUINode::hide_nodes(TypedArray<NodePath> const& paths) const {
Error ret = OK;

for (int32_t i = 0; i < paths.size(); ++i) {
if (hide_node(paths[i]) != OK) {
ret = FAILED;
}
}

return ret;
}

Error GUINode::remove_node(NodePath const& path) const {
Node* node = get_node_internal(path);
ERR_FAIL_NULL_V(node, FAILED);

Node* parent = node->get_parent();
ERR_FAIL_NULL_V(parent, FAILED);

parent->remove_child(node);
node->queue_free();

return OK;
}

Error GUINode::remove_nodes(TypedArray<NodePath> const& paths) const {
Error ret = OK;

for (int32_t i = 0; i < paths.size(); ++i) {
if (remove_node(paths[i]) != OK) {
ret = FAILED;
}
}

return ret;
}

Expand Down Expand Up @@ -331,7 +363,7 @@ void GUINode::set_click_mask_from_nodepaths(TypedArray<NodePath> const& paths) {
update_click_mask();
}

bool GUINode::_has_point(godot::Vector2 const& p_point) const {
bool GUINode::_has_point(Vector2 const& p_point) const {
if (!_click_mask.is_valid()) {
return Control::_has_point(p_point);
}
Expand Down
3 changes: 3 additions & 0 deletions extension/src/openvic-extension/classes/GUINode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ namespace OpenVic {
godot::Error hide_node(godot::NodePath const& path) const;
godot::Error hide_nodes(godot::TypedArray<godot::NodePath> const& paths) const;

godot::Error remove_node(godot::NodePath const& path) const;
godot::Error remove_nodes(godot::TypedArray<godot::NodePath> const& paths) const;

static godot::String int_to_string_suffixed(int64_t val);
static godot::String float_to_string_suffixed(float val);
static godot::String float_to_string_dp(float val, int32_t decimal_places);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ Error GUIOverlappingElementsBox::set_child_count(int32_t new_count) {
return OK;
} else if (child_count > new_count) {
do {
remove_child(get_child(--child_count));
Node* child = get_child(--child_count);
remove_child(child);
child->queue_free();
} while (child_count > new_count);
return OK;
} else {
Expand Down
2 changes: 1 addition & 1 deletion extension/src/openvic-extension/classes/MapMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ AABB MapMesh::get_core_aabb() const {
return AABB { size * -0.5f, size };
}

bool MapMesh::is_valid_uv_coord(godot::Vector2 const& uv) const {
bool MapMesh::is_valid_uv_coord(Vector2 const& uv) const {
return 0.0f <= uv.y && uv.y <= 1.0f;
}

Expand Down
4 changes: 2 additions & 2 deletions extension/src/openvic-extension/singletons/Checksum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ Checksum::~Checksum() {
/* REQUIREMENTS:
* DAT-8
*/
godot::String Checksum::get_checksum_text() {
return godot::String("1234abcd");
String Checksum::get_checksum_text() {
return String("1234abcd");
}
4 changes: 2 additions & 2 deletions extension/src/openvic-extension/singletons/GameSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Ref<ImageTexture> GameSingleton::get_flag_sheet_texture() const {
return flag_sheet_texture;
}

int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, godot::StringName const& flag_type) const {
int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, StringName const& flag_type) const {
ERR_FAIL_COND_V_MSG(
country_index < 0 ||
country_index >= get_definition_manager().get_country_definition_manager().get_country_definition_count(),
Expand All @@ -207,7 +207,7 @@ Rect2i GameSingleton::get_flag_sheet_rect(int32_t flag_index) const {
return { Vector2i { flag_index % flag_sheet_dims.x, flag_index / flag_sheet_dims.x } * flag_dims, flag_dims };
}

Rect2i GameSingleton::get_flag_sheet_rect(int32_t country_index, godot::StringName const& flag_type) const {
Rect2i GameSingleton::get_flag_sheet_rect(int32_t country_index, StringName const& flag_type) const {
return get_flag_sheet_rect(get_flag_sheet_index(country_index, flag_type));
}

Expand Down
13 changes: 6 additions & 7 deletions extension/src/openvic-extension/utility/UITools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_view_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string_name;

GFX::Sprite const* UITools::get_gfx_sprite(godot::String const& gfx_sprite) {
GFX::Sprite const* UITools::get_gfx_sprite(String const& gfx_sprite) {
GameSingleton* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, nullptr);
GFX::Sprite const* sprite = game_singleton->get_definition_manager().get_ui_manager().get_sprite_by_identifier(
Expand All @@ -38,7 +38,7 @@ GFX::Sprite const* UITools::get_gfx_sprite(godot::String const& gfx_sprite) {
return sprite;
}

GUI::Element const* UITools::get_gui_element(godot::String const& gui_scene, godot::String const& gui_element) {
GUI::Element const* UITools::get_gui_element(String const& gui_scene, String const& gui_element) {
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, nullptr);
GUI::Scene const* scene =
Expand All @@ -65,13 +65,12 @@ GUI::Position const* UITools::get_gui_position(String const& gui_scene, String c
namespace OpenVic {
struct generate_gui_args_t {
GUI::Element const& element;
godot::String const& name;
String const& name;
AssetManager& asset_manager;
godot::Control*& result;
Control*& result;

constexpr generate_gui_args_t(
GUI::Element const& new_element, godot::String const& new_name, AssetManager& new_asset_manager,
godot::Control*& new_result
GUI::Element const& new_element, String const& new_name, AssetManager& new_asset_manager, Control*& new_result
) : element { new_element }, name { new_name }, asset_manager { new_asset_manager }, result { new_result } {}
};
}
Expand Down Expand Up @@ -690,7 +689,7 @@ bool UITools::generate_gui_element(
}

bool UITools::generate_gui_element(
godot::String const& gui_scene, godot::String const& gui_element, godot::String const& name, godot::Control*& result
String const& gui_scene, String const& gui_element, String const& name, Control*& result
) {
return generate_gui_element(get_gui_element(gui_scene, gui_element), name, result);
}
4 changes: 3 additions & 1 deletion game/src/Game/GameSession/MapText.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func _clear_children() -> void:
var child_count : int = get_child_count()
while child_count > 0:
child_count -= 1
remove_child(get_child(child_count))
var child : Node = get_child(child_count)
remove_child(child)
child.queue_free()

func generate_map_names() -> void:
_clear_children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func _generate_province_list_row(index : int, type : MenuSingleton.ProvinceListE

if _province_list_panels[index]:
_province_listbox.remove_child(_province_list_panels[index])
_province_list_panels[index].queue_free()

_province_list_types[index] = MenuSingleton.LIST_ENTRY_NONE
_province_list_indices[index] = -1
Expand Down Expand Up @@ -540,7 +541,7 @@ func _update_distributions():
child.set_mouse_filter(Control.MOUSE_FILTER_IGNORE)
list.add_child(child)

for list_index in min(list.get_child_count(), distribution_info.size()):
for list_index : int in min(list.get_child_count(), distribution_info.size()):

var child : Panel = list.get_child(list_index)

Expand Down

0 comments on commit 164db4e

Please sign in to comment.