Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extension/doc_classes/MenuSingleton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@
<description>
</description>
</method>
<method name="update_budget_menu_cpp" qualifiers="const">
<return type="void" />
<description>
</description>
</method>
<method name="update_search_results">
<return type="void" />
<param index="0" name="text" type="String" />
Expand Down
17 changes: 13 additions & 4 deletions extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,19 @@ Error GFXMaskedFlagTexture::set_flag_country_name_and_type(

Error GFXMaskedFlagTexture::set_flag_country(CountryInstance* new_flag_country) {
if (new_flag_country == nullptr) {
flag_government_type_connection = {};
return set_flag_country_and_type(nullptr, {});
}

GovernmentType const* government_type = new_flag_country->flag_government_type.get_untracked();
GovernmentType const* const government_type = new_flag_country->flag_government_type.get(
[this,new_flag_country](signal<>& changed) mutable -> void {
flag_government_type_connection = changed.connect_scoped(
[this,new_flag_country]() -> void {
set_flag_country(new_flag_country);
}
);
}
);

const StringName new_flag_type = government_type != nullptr
? StringName { Utilities::std_to_godot_string(government_type->get_flag_type()) }
Expand All @@ -232,13 +241,13 @@ Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country
return set_flag_country(nullptr);
}

GameSingleton* game_singleton = GameSingleton::get_singleton();
GameSingleton* const game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);

InstanceManager* instance_manager = game_singleton->get_instance_manager();
InstanceManager* const instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, FAILED);

CountryInstance* new_flag_country = instance_manager->get_country_instance_manager()
CountryInstance* const new_flag_country = instance_manager->get_country_instance_manager()
.get_country_instance_by_identifier(
Utilities::godot_to_std_string(new_flag_country_name)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <openvic-simulation/interface/GFXSprite.hpp>
#include <openvic-simulation/types/Signal.hpp>

#include "openvic-extension/classes/GFXButtonStateTexture.hpp"

Expand All @@ -9,8 +10,11 @@ namespace OpenVic {
struct CountryInstance;

class GFXMaskedFlagTexture : public GFXButtonStateHavingTexture {
private:
GDCLASS(GFXMaskedFlagTexture, GFXButtonStateHavingTexture)

scoped_connection flag_government_type_connection;

GFX::MaskedFlag const* PROPERTY(gfx_masked_flag, nullptr);
CountryDefinition const* PROPERTY(flag_country, nullptr);
godot::StringName PROPERTY(flag_type);
Expand Down
47 changes: 29 additions & 18 deletions extension/src/openvic-extension/classes/GUILabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ using namespace OpenVic;
using namespace godot;
using namespace OpenVic::Utilities::literals;

GUILabel::~GUILabel() {
disconnect_all();
}

GUILabel::string_segment_t::string_segment_t(String&& new_text, Color const& new_colour, real_t new_width)
: text { std::move(new_text) }, colour { new_colour }, width { new_width } {}

Expand Down Expand Up @@ -594,7 +598,7 @@ std::pair<String, GUILabel::colour_instructions_t> GUILabel::generate_display_te

std::vector<GUILabel::line_t> GUILabel::generate_lines_and_segments(
String const& display_text, colour_instructions_t const& colour_instructions
) const {
) {
static constexpr char RESET_COLOUR_CODE = '!';

std::vector<line_t> unwrapped_lines;
Expand Down Expand Up @@ -641,7 +645,7 @@ std::vector<GUILabel::line_t> GUILabel::generate_lines_and_segments(

void GUILabel::separate_lines(
String const& string, Color const& colour, std::vector<line_t>& unwrapped_lines
) const {
) {
static const String NEWLINE_MARKER = "\n";

int64_t start_pos = 0;
Expand All @@ -664,7 +668,7 @@ void GUILabel::separate_lines(

void GUILabel::separate_currency_segments(
String const& string, Color const& colour, line_t& line
) const {
) {
int64_t start_pos = 0;
int64_t marker_pos;

Expand Down Expand Up @@ -694,28 +698,30 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {

InstanceManager* instance_manager = game_singleton.get_instance_manager();

if (instance_manager != nullptr) {
CountryInstance* country_instance =
instance_manager->get_country_instance_manager().get_country_instance_by_identifier(
if (instance_manager == nullptr) {
CountryDefinition const* country_definition =
game_singleton.get_definition_manager().get_country_definition_manager().get_country_definition_by_identifier(
Utilities::godot_to_std_string(identifier)
);

if (country_instance != nullptr) {
country_index = country_instance->get_country_definition().get_index();

GovernmentType const* government_type = country_instance->flag_government_type.get_untracked();
if (government_type != nullptr) {
flag_type = Utilities::std_to_godot_string(government_type->get_flag_type());
}
if (country_definition != nullptr) {
country_index = country_definition->get_index();
}
} else {
CountryDefinition const* country_definition =
game_singleton.get_definition_manager().get_country_definition_manager().get_country_definition_by_identifier(
CountryInstance* const country_instance = instance_manager->get_country_instance_manager()
.get_country_instance_by_identifier(
Utilities::godot_to_std_string(identifier)
);

if (country_definition != nullptr) {
country_index = country_definition->get_index();
if (country_instance != nullptr) {
country_index = country_instance->get_country_definition().get_index();
DerivedState<GovernmentType const*>& flag_government_type_state = country_instance->flag_government_type;
GovernmentType const* const flag_government_type = flag_government_type_state.get([this](signal<>& changed) mutable -> void {
changed.connect(&GUILabel::on_flag_government_type_changed, this);
});
if (flag_government_type != nullptr) {
flag_type = Utilities::std_to_godot_string(flag_government_type->get_flag_type());
}
}
}

Expand All @@ -740,9 +746,14 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
return flag_segment;
}

void GUILabel::on_flag_government_type_changed() {
disconnect_all();
_queue_line_update();
}

void GUILabel::separate_flag_segments(
String const& string, Color const& colour, line_t& line
) const {
) {
const auto push_string_segment = [this, &string, &colour, &line](int64_t start, int64_t end) -> void {
String substring = string.substr(start, end - start);
const real_t width = get_string_width(substring);
Expand Down
17 changes: 11 additions & 6 deletions extension/src/openvic-extension/classes/GUILabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <godot_cpp/classes/style_box_texture.hpp>

#include <openvic-simulation/interface/GUI.hpp>
#include <openvic-simulation/types/Signal.hpp>

#include "openvic-extension/classes/GFXSpriteTexture.hpp"
#include "openvic-extension/classes/GUIHasTooltip.hpp"
Expand All @@ -17,7 +18,9 @@ namespace godot {
}

namespace OpenVic {
class GUILabel : public godot::Control {
struct GovernmentType;

class GUILabel : public godot::Control, public observer {
GDCLASS(GUILabel, godot::Control)

GUI_TOOLTIP_DEFINITIONS
Expand Down Expand Up @@ -81,6 +84,7 @@ namespace OpenVic {
);

GUILabel();
~GUILabel() override;

/* Reset gui_text to nullptr and reset current text. */
void clear();
Expand Down Expand Up @@ -124,24 +128,25 @@ namespace OpenVic {

void _queue_line_update();
void _update_lines();
void on_flag_government_type_changed();

godot::String generate_substituted_text(godot::String const& base_text) const;
std::pair<godot::String, colour_instructions_t> generate_display_text_and_colour_instructions(
godot::String const& substituted_text
) const;
std::vector<line_t> generate_lines_and_segments(
godot::String const& display_text, colour_instructions_t const& colour_instructions
) const;
);
void separate_lines(
godot::String const& string, godot::Color const& colour, std::vector<line_t>& lines
) const;
);
void separate_currency_segments(
godot::String const& string, godot::Color const& colour, line_t& line
) const;
static flag_segment_t make_flag_segment(godot::String const& identifier);
);
flag_segment_t make_flag_segment(godot::String const& identifier);
void separate_flag_segments(
godot::String const& string, godot::Color const& colour, line_t& line
) const;
);
std::vector<line_t> wrap_lines(std::vector<line_t>& unwrapped_lines) const;
void adjust_to_content_size();
};
Expand Down
29 changes: 19 additions & 10 deletions extension/src/openvic-extension/classes/GUIScrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ void GUIScrollbar::emit_value_changed() {
return;
}
emit_signal(signal_value_changed(), value);
value_changed();
scaled_value.set(_get_scaled_value(value));
}

void GUIScrollbar::reset() {
Expand Down Expand Up @@ -522,12 +522,8 @@ void GUIScrollbar::set_value_as_ratio(float new_ratio) {
set_value(step_count * new_ratio);
}

fixed_point_t GUIScrollbar::get_value_scaled_fp() const {
return _get_scaled_value(value);
}

float GUIScrollbar::get_value_scaled() const {
return get_value_scaled_fp().to_float();
return _get_scaled_value(value).to_float();
}

void GUIScrollbar::set_step_count(const int32_t new_step_count) {
Expand Down Expand Up @@ -592,12 +588,25 @@ void GUIScrollbar::set_range_limits_fp(
: std::optional<fixed_point_t>{}
);
}
void GUIScrollbar::set_range_limits_and_value_from_slider_value(ReadOnlyClampedValue& slider_value) {
void GUIScrollbar::link_to(ReadOnlyClampedValue& clamped_value) {
clamped_value_connection.disconnect();
set_range_limits_fp(
slider_value.get_min(),
slider_value.get_max()
clamped_value.get_min(),
clamped_value.get_max()
);
set_scaled_value(slider_value.get_value_untracked());
set_scaled_value(clamped_value.get_value(
[this](signal<fixed_point_t>& dependency_changed) mutable -> void {
clamped_value_connection = std::move(dependency_changed.connect(
&GUIScrollbar::set_scaled_value,
this
));
}
));
}

void GUIScrollbar::unlink() {
clamped_value_connection.disconnect();
clamped_value_connection={};
}

void GUIScrollbar::set_length_override(real_t new_length_override) {
Expand Down
13 changes: 7 additions & 6 deletions extension/src/openvic-extension/classes/GUIScrollbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include <godot_cpp/classes/input_event.hpp>

#include <openvic-simulation/interface/GUI.hpp>
#include <openvic-simulation/types/Signal.hpp>
#include <openvic-simulation/types/fixed_point/FixedPoint.hpp>
#include <openvic-simulation/utility/reactive/MutableState.hpp>

#include "openvic-extension/classes/GFXSpriteTexture.hpp"
#include "openvic-extension/classes/GUIHasTooltip.hpp"

namespace OpenVic {
struct SliderValue;
struct ReadOnlyClampedValue;

class GUIScrollbar : public godot::Control {
GDCLASS(GUIScrollbar, godot::Control)
Expand Down Expand Up @@ -47,6 +47,9 @@ namespace OpenVic {
int32_t scale_denominator = 1;
int32_t PROPERTY(step_count, 1);
int32_t PROPERTY(value, 0);
STATE_PROPERTY(fixed_point_t, scaled_value);

scoped_connection clamped_value_connection;

bool PROPERTY_CUSTOM_PREFIX(range_limited, is, false);
std::optional<int32_t> upper_range_limit;
Expand Down Expand Up @@ -91,7 +94,6 @@ namespace OpenVic {

public:
static godot::StringName const& signal_value_changed();
mutable signal_property<GUIScrollbar> value_changed;

GUIScrollbar();

Expand All @@ -114,8 +116,6 @@ namespace OpenVic {

float get_value_as_ratio() const;
void set_value_as_ratio(float new_ratio);

fixed_point_t get_value_scaled_fp() const;
float get_value_scaled() const;

void set_step_count(const int32_t new_step_count);
Expand All @@ -132,9 +132,10 @@ namespace OpenVic {
const std::optional<fixed_point_t> new_lower_range_limit,
const std::optional<fixed_point_t> new_upper_range_limit
);
void set_range_limits_and_value_from_slider_value(
void link_to(
ReadOnlyClampedValue& slider_value
);
void unlink();

/* Override the main dimension of gui_scollbar's size with the specified length. */
void set_length_override(real_t new_length_override);
Expand Down
48 changes: 48 additions & 0 deletions extension/src/openvic-extension/components/ReactiveComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "ReactiveComponent.hpp"

using namespace OpenVic;

void ReactiveComponent::add_connection(connection conn) {
const std::lock_guard<std::mutex> lock_guard { connections_lock };
connections.emplace_back(std::move(conn));
}

void ReactiveComponent::disconnect_all() {
const std::lock_guard<std::mutex> lock_guard { connections_lock };
connections.clear();
}

void ReactiveComponent::update_if_dirty() {
if (!is_initialised) {
initialise();
is_initialised = true;
}

if (!is_dirty) {
return;
}

const std::lock_guard<std::mutex> lock_guard { is_dirty_lock };
if (!is_dirty) {
return;
}

update();

is_dirty = false;
}

void ReactiveComponent::mark_dirty() {
if (is_dirty) {
return;
}

const std::lock_guard<std::mutex> lock_guard { is_dirty_lock };
if (is_dirty) {
return;
}

disconnect_all();
is_dirty = true;
marked_dirty();
}
Loading
Loading