Skip to content
Merged
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
14 changes: 14 additions & 0 deletions extension/doc_classes/GUIScrollbar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<description>
</description>
</method>
<method name="get_value_scaled" qualifiers="const">
<return type="float" />
<description>
</description>
</method>
<method name="increment_value">
<return type="void" />
<param index="0" name="signal" type="bool" default="true" />
Expand Down Expand Up @@ -118,6 +123,15 @@
<description>
</description>
</method>
<method name="set_range_limits_and_value">
<return type="int" enum="Error" />
<param index="0" name="new_range_limit_min" type="int" />
<param index="1" name="new_range_limit_max" type="int" />
<param index="2" name="new_value" type="int" />
<param index="3" name="signal" type="bool" default="true" />
<description>
</description>
</method>
<method name="set_tooltip_string_and_substitution_dict">
<return type="void" />
<param index="0" name="new_tooltip_string" type="String" />
Expand Down
7 changes: 7 additions & 0 deletions extension/doc_classes/MenuSingleton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<tutorials>
</tutorials>
<methods>
<method name="calculate_trade_menu_stockpile_cutoff_amount" qualifiers="static">
<return type="float" />
<param index="0" name="slider" type="GUIScrollbar" />
<description>
</description>
</method>
<method name="can_decrease_speed" qualifiers="const">
<return type="bool" />
<description>
Expand Down Expand Up @@ -183,6 +189,7 @@
<method name="get_trade_menu_trade_details_info" qualifiers="const">
<return type="Dictionary" />
<param index="0" name="trade_detail_good_index" type="int" />
<param index="1" name="stockpile_cutoff_slider" type="GUIScrollbar" />
<description>
</description>
</method>
Expand Down
91 changes: 76 additions & 15 deletions extension/src/openvic-extension/classes/GUIScrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

#include <openvic-simulation/types/SliderValue.hpp>

#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/UITools.hpp"
#include "openvic-extension/utility/Utilities.hpp"
Expand Down Expand Up @@ -40,11 +42,15 @@ void GUIScrollbar::_bind_methods() {
OV_BIND_METHOD(GUIScrollbar::increment_value, { "signal" }, DEFVAL(true));
OV_BIND_METHOD(GUIScrollbar::decrement_value, { "signal" }, DEFVAL(true));
OV_BIND_METHOD(GUIScrollbar::set_value_as_ratio, { "new_ratio", "signal" }, DEFVAL(true));
OV_BIND_METHOD(GUIScrollbar::get_value_scaled);

OV_BIND_METHOD(GUIScrollbar::is_range_limited);
OV_BIND_METHOD(GUIScrollbar::get_range_limit_min);
OV_BIND_METHOD(GUIScrollbar::get_range_limit_max);
OV_BIND_METHOD(GUIScrollbar::set_range_limits, { "new_range_limit_min", "new_range_limit_max", "signal" }, DEFVAL(true));
OV_BIND_METHOD(GUIScrollbar::set_range_limits_and_value, {
"new_range_limit_min", "new_range_limit_max", "new_value", "signal"
}, DEFVAL(true));
OV_BIND_METHOD(GUIScrollbar::set_limits, { "new_min_value", "new_max_value", "signal" }, DEFVAL(true));

OV_BIND_METHOD(GUIScrollbar::get_length_override);
Expand Down Expand Up @@ -426,20 +432,9 @@ Error GUIScrollbar::set_gui_scrollbar(GUI::Scrollbar const* new_gui_scrollbar) {

_calculate_rects();

fixed_point_t step_size = gui_scrollbar->get_step_size();
if (step_size <= 0) {
UtilityFunctions::push_error(
"Invalid step size ", Utilities::fixed_point_to_string_dp(step_size, -1), " for GUIScrollbar ",
gui_scrollbar_name, " - not positive! Defaulting to 1."
);
step_size = 1;
ret = false;
}
min_value = gui_scrollbar->get_min_value() / step_size;
max_value = gui_scrollbar->get_max_value() / step_size;

ret &= _constrain_limits() == OK;
ret &= reset() == OK;
ret &= set_step_size_and_limits_fp(
gui_scrollbar->get_step_size(), gui_scrollbar->get_min_value(), gui_scrollbar->get_max_value()
) == OK;

return ERR(ret);
}
Expand Down Expand Up @@ -472,6 +467,14 @@ void GUIScrollbar::set_value(int32_t new_value, bool signal) {
}
}

void GUIScrollbar::set_value_fp(fixed_point_t new_value, bool signal) {
return set_value(new_value / step_size, signal);
}

void GUIScrollbar::set_value_from_slider_value(SliderValue const& slider_value, int32_t scale, bool signal) {
set_value_fp(slider_value.get_value() * scale, signal);
}

void GUIScrollbar::increment_value(bool signal) {
set_value(value + 1, signal);
}
Expand All @@ -488,12 +491,26 @@ void GUIScrollbar::set_value_as_ratio(float new_ratio, bool signal) {
set_value(min_value + (max_value - min_value) * new_ratio, signal);
}

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

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

Error GUIScrollbar::set_range_limits(int32_t new_range_limit_min, int32_t new_range_limit_max, bool signal) {
return set_range_limits_and_value(new_range_limit_min, new_range_limit_max, value, signal);
}

Error GUIScrollbar::set_range_limits_and_value(
int32_t new_range_limit_min, int32_t new_range_limit_max, int32_t new_value, bool signal
) {
ERR_FAIL_COND_V_MSG(!range_limited, FAILED, "Cannot set range limits of non-range-limited GUIScrollbar!");
range_limit_min = new_range_limit_min;
range_limit_max = new_range_limit_max;
const Error err = _constrain_range_limits();
set_value(value, signal);
set_value(new_value, signal);
return err;
}

Expand All @@ -506,6 +523,50 @@ Error GUIScrollbar::set_limits(int32_t new_min_value, int32_t new_max_value, boo
return ERR(ret);
}

Error GUIScrollbar::set_range_limits_and_value_fp(
fixed_point_t new_range_limit_min, fixed_point_t new_range_limit_max, fixed_point_t new_value, bool signal
) {
return set_range_limits_and_value(
new_range_limit_min / step_size,
new_range_limit_max / step_size,
new_value / step_size,
signal
);
}

Error GUIScrollbar::set_range_limits_and_value_from_slider_value(
SliderValue const& slider_value, int32_t scale, bool signal
) {
return set_range_limits_and_value_fp(
slider_value.get_min() * scale,
slider_value.get_max() * scale,
slider_value.get_value() * scale,
signal
);
}

Error GUIScrollbar::set_step_size_and_limits_fp(fixed_point_t new_step_size, int32_t new_min_value, int32_t new_max_value) {
bool ret = true;

step_size = new_step_size;
if (step_size <= 0) {
UtilityFunctions::push_error(
"Invalid step size ", Utilities::fixed_point_to_string_dp(step_size, -1), " for GUIScrollbar ",
get_name(), " - not positive! Defaulting to 1."
);
step_size = fixed_point_t::_1();
ret = false;
}

min_value = new_min_value / step_size;
Comment thread
Hop311 marked this conversation as resolved.
max_value = new_max_value / step_size;

ret &= _constrain_limits() == OK;
ret &= reset() == OK;

return ERR(ret);
}

void GUIScrollbar::set_length_override(real_t new_length_override) {
ERR_FAIL_COND_MSG(
length_override < 0, vformat("Invalid GUIScrollbar length override: %f - cannot be negative!", length_override)
Expand Down
25 changes: 23 additions & 2 deletions extension/src/openvic-extension/classes/GUIScrollbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "openvic-extension/classes/GUIHasTooltip.hpp"

namespace OpenVic {
struct SliderValue;

class GUIScrollbar : public godot::Control {
GDCLASS(GUIScrollbar, godot::Control)

Expand Down Expand Up @@ -36,11 +38,12 @@ namespace OpenVic {
godot::Orientation PROPERTY(orientation, godot::HORIZONTAL);
real_t PROPERTY(length_override, 0.0);

fixed_point_t PROPERTY(step_size, fixed_point_t::_1());
int32_t PROPERTY(value, 0);
int32_t PROPERTY(min_value, 0);
int32_t PROPERTY(max_value, 0);

bool PROPERTY_CUSTOM_PREFIX(range_limited, is);
bool PROPERTY_CUSTOM_PREFIX(range_limited, is, false);
int32_t PROPERTY(range_limit_min, 0);
int32_t PROPERTY(range_limit_max, 0);

Expand Down Expand Up @@ -97,15 +100,33 @@ namespace OpenVic {
godot::String get_gui_scrollbar_name() const;

void set_value(int32_t new_value, bool signal = true);
void set_value_fp(fixed_point_t new_value, bool signal = true);
void set_value_from_slider_value(SliderValue const& slider_value, int32_t scale, bool signal = true);
void increment_value(bool signal = true);
void decrement_value(bool signal = true);

float get_value_as_ratio() const;
void set_value_as_ratio(float new_ratio, bool signal = true);

godot::Error set_range_limits(int32_t new_range_limit_min, int32_t new_range_limit_max, bool signal = true);
fixed_point_t get_value_scaled_fp() const;
float get_value_scaled() const;

godot::Error set_range_limits(
int32_t new_range_limit_min, int32_t new_range_limit_max, bool signal = true
);
godot::Error set_range_limits_and_value(
int32_t new_range_limit_min, int32_t new_range_limit_max, int32_t new_value, bool signal = true
);
godot::Error set_limits(int32_t new_min_value, int32_t new_max_value, bool signal = true);

godot::Error set_range_limits_and_value_fp(
fixed_point_t new_range_limit_min, fixed_point_t new_range_limit_max, fixed_point_t new_value, bool signal = true
);
godot::Error set_range_limits_and_value_from_slider_value(
SliderValue const& slider_value, int32_t scale, bool signal = true
);
godot::Error set_step_size_and_limits_fp(fixed_point_t new_step_size, int32_t new_min_value, int32_t new_max_value);

/* Override the main dimension of gui_scollbar's size with the specified length. */
void set_length_override(real_t new_length_override);
};
Expand Down
3 changes: 2 additions & 1 deletion extension/src/openvic-extension/singletons/MenuSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ void MenuSingleton::_bind_methods() {

/* TRADE MENU */
OV_BIND_METHOD(MenuSingleton::get_trade_menu_good_categories_info);
OV_BIND_METHOD(MenuSingleton::get_trade_menu_trade_details_info, { "trade_detail_good_index" });
OV_BIND_METHOD(MenuSingleton::get_trade_menu_trade_details_info, { "trade_detail_good_index", "stockpile_cutoff_slider" });
OV_BIND_METHOD(MenuSingleton::get_trade_menu_tables_info);
OV_BIND_SMETHOD(calculate_trade_menu_stockpile_cutoff_amount, { "slider" });

BIND_ENUM_CONSTANT(TRADE_SETTING_NONE);
BIND_ENUM_CONSTANT(TRADE_SETTING_AUTOMATED);
Expand Down
10 changes: 9 additions & 1 deletion extension/src/openvic-extension/singletons/MenuSingleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace OpenVic {
struct ModifierSum;
struct RuleSet;
struct LeaderInstance;
struct GUIScrollbar;

class MenuSingleton : public godot::Object {
GDCLASS(MenuSingleton, godot::Object)
Expand Down Expand Up @@ -239,8 +240,15 @@ namespace OpenVic {

/* TRADE MENU */
godot::Dictionary get_trade_menu_good_categories_info() const;
godot::Dictionary get_trade_menu_trade_details_info(int32_t trade_detail_good_index) const;
godot::Dictionary get_trade_menu_trade_details_info(
int32_t trade_detail_good_index, GUIScrollbar* stockpile_cutoff_slider
) const;
godot::Dictionary get_trade_menu_tables_info() const;
static constexpr fixed_point_t calculate_trade_menu_stockpile_cutoff_amount_fp(fixed_point_t value) {
// TODO - replace this with: pow(2001, value / 2000) - 1
return value;
}
static float calculate_trade_menu_stockpile_cutoff_amount(GUIScrollbar const* slider);

/* MILITARY MENU */
godot::Dictionary make_leader_dict(LeaderInstance const& leader);
Expand Down
27 changes: 23 additions & 4 deletions extension/src/openvic-extension/singletons/TradeMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <godot_cpp/variant/utility_functions.hpp>

#include "openvic-extension/classes/GUILabel.hpp"
#include "openvic-extension/classes/GUIScrollbar.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/singletons/PlayerSingleton.hpp"
#include "openvic-extension/utility/Utilities.hpp"
Expand Down Expand Up @@ -105,14 +106,15 @@ Dictionary MenuSingleton::get_trade_menu_good_categories_info() const {
return ret;
}

Dictionary MenuSingleton::get_trade_menu_trade_details_info(int32_t trade_detail_good_index) const {
Dictionary MenuSingleton::get_trade_menu_trade_details_info(
int32_t trade_detail_good_index, GUIScrollbar* stockpile_cutoff_slider
) const {
static const StringName trade_detail_good_name_key = "trade_detail_good_name";
static const StringName trade_detail_good_price_key = "trade_detail_good_price";
static const StringName trade_detail_good_base_price_key = "trade_detail_good_base_price";
static const StringName trade_detail_price_history_key = "trade_detail_price_history";
static const StringName trade_detail_is_automated_key = "trade_detail_is_automated";
static const StringName trade_detail_is_selling_key = "trade_detail_is_selling"; // or buying (false)
static const StringName trade_detail_slider_value_key = "trade_detail_slider_value"; // linear slider value
static const StringName trade_detail_slider_amount_key = "trade_detail_slider_amount"; // exponential good amount
static const StringName trade_detail_government_needs_key = "trade_detail_government_needs";
static const StringName trade_detail_army_needs_key = "trade_detail_army_needs";
Expand Down Expand Up @@ -163,8 +165,19 @@ Dictionary MenuSingleton::get_trade_menu_trade_details_info(int32_t trade_detail

ret[trade_detail_is_automated_key] = good_data.is_automated;
ret[trade_detail_is_selling_key] = good_data.is_selling;
// TODO - use exponential formula!
ret[trade_detail_slider_value_key] = (good_data.stockpile_cutoff / 2000).to_int32_t();
if (stockpile_cutoff_slider != nullptr) {
int32_t index = 0;

while (index < stockpile_cutoff_slider->get_max_value() && calculate_trade_menu_stockpile_cutoff_amount_fp(
index * stockpile_cutoff_slider->get_step_size()
) < good_data.stockpile_cutoff) {
++index;
}

// TODO - use a more efficient algorithm, e.g. some kind of binary search
Comment thread
Hop311 marked this conversation as resolved.

stockpile_cutoff_slider->set_value(index, false);
}
ret[trade_detail_slider_amount_key] = good_data.stockpile_cutoff.to_float();
ret[trade_detail_government_needs_key] = good_data.government_needs.to_float();
ret[trade_detail_army_needs_key] = good_data.army_needs.to_float();
Expand Down Expand Up @@ -317,3 +330,9 @@ Dictionary MenuSingleton::get_trade_menu_tables_info() const {

return ret;
}

float MenuSingleton::calculate_trade_menu_stockpile_cutoff_amount(GUIScrollbar const* slider) {
ERR_FAIL_NULL_V(slider, 0.0f);

return calculate_trade_menu_stockpile_cutoff_amount_fp(slider->get_value_scaled_fp());
}
Loading