Skip to content
Open
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
35 changes: 30 additions & 5 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
Language: Cpp
UseCRLF: false
Standard: c++20
UseTab: Always
UseTab: ForIndentation
TabWidth: 4
IndentWidth: 4
ColumnLimit: 128
Expand Down Expand Up @@ -39,7 +38,7 @@ FixNamespaceComments: false
EmptyLineBeforeAccessModifier: LogicalBlock
Cpp11BracedListStyle: false
CompactNamespaces: false
BreakConstructorInitializers: BeforeColon
BreakConstructorInitializers: AfterColon
BreakBeforeBraces: Attach
AlwaysBreakTemplateDeclarations: Yes
AlwaysBreakAfterReturnType: None
Expand Down Expand Up @@ -82,7 +81,33 @@ IncludeCategories:
Priority: 4
- Regex: ^<openvic-simulation/
Priority: 5
- Regex: ^"openvic-extension/
- Regex: ^<lexy-vdf/
Priority: 6
- Regex: .*
- Regex: ^<boost/
Priority: 7
- Regex: ^<dryad/
Priority: 8
- Regex: ^<fmt/
Priority: 9
- Regex: ^<foonathan
Priority: 10
- Regex: ^<function2/
Priority: 11
- Regex: ^<plf_colony.h>
Priority: 12
- Regex: ^<range
Priority: 13
- Regex: ^<spdlog/
Priority: 14
- Regex: ^<tsl/
Priority: 15
- Regex: ^<type_safe/
Priority: 16
- Regex: ^<XoshiroCpp.hpp>
Priority: 17
- Regex: ^<gli/
Priority: 18
- Regex: ^"openvic-extension/
Priority: 19
- Regex: .*
Priority: 20
25 changes: 12 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ exclude: |
)

repos:
# Waiting on Hop311 to approve clang-format
# - repo: https://github.com/pre-commit/mirrors-clang-format
# rev: v19.1.3
# hooks:
# - id: clang-format
# files: \.(c|h|cpp|hpp|inc)$
# types_or: [text]
# # exclude: |
# # (?x)^(
# # )
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v22.1.5
hooks:
- id: clang-format
files: \.(c|h|cpp|hpp|inc)$
types_or: [text]
exclude: |
(?x)^(
extension/src/gen/.*
)

- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
Expand All @@ -31,7 +31,7 @@ repos:
stages: [manual] # Not automatically triggered, invoked via `pre-commit run --hook-stage manual clang-tidy`

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.2
rev: v0.15.18
hooks:
- id: ruff-check
args: [--fix]
Expand All @@ -42,7 +42,7 @@ repos:
types_or: [text]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
rev: v2.1.0
hooks:
- id: mypy
files: \.py$
Expand All @@ -69,7 +69,6 @@ repos:
game/addons/.*
)


- repo: local
hooks:
- id: make-rst
Expand Down
46 changes: 19 additions & 27 deletions extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void GFXCorneredTileSupportingTexture::draw_rect_cornered(RID const& to_canvas_i
if (rendering_server != nullptr) {
const Size2 size = get_size();
rendering_server->canvas_item_add_nine_patch(
to_canvas_item, rect, { {}, size }, get_rid(),
cornered_tile_border_size, size - cornered_tile_border_size
to_canvas_item, rect, { {}, size }, get_rid(), cornered_tile_border_size, size - cornered_tile_border_size
);
}
} else {
Expand All @@ -53,38 +52,37 @@ void GFXButtonStateTexture::_bind_methods() {
GFXButtonStateTexture::GFXButtonStateTexture() {}

Ref<GFXButtonStateTexture> GFXButtonStateTexture::make_gfx_button_state_texture(
ButtonState button_state, Ref<Image> const& source_image, Rect2i const& region, Vector2i const& cornered_tile_border_size
ButtonState button_state, Ref<Image> const& source_image, Rect2i const& region, Vector2i const& cornered_tile_border_size
) {
Ref<GFXButtonStateTexture> button_state_texture;
button_state_texture.instantiate();
ERR_FAIL_NULL_V(button_state_texture, nullptr);
button_state_texture->set_button_state(button_state);
if (source_image.is_valid()) {
ERR_FAIL_COND_V(
button_state_texture->generate_state_image(source_image, region, cornered_tile_border_size) != OK, nullptr
button_state_texture->generate_state_image(source_image, region, cornered_tile_border_size) != OK, nullptr
);
}
return button_state_texture;
}

void GFXButtonStateTexture::set_button_state(ButtonState new_button_state) {
ERR_FAIL_COND(
new_button_state != HOVER && new_button_state != PRESSED &&
new_button_state != DISABLED && new_button_state != SELECTED
new_button_state != HOVER && new_button_state != PRESSED && new_button_state != DISABLED && new_button_state != SELECTED
);
button_state = new_button_state;
}

Error GFXButtonStateTexture::generate_state_image(
Ref<Image> const& source_image, Rect2i const& region, Vector2i const& new_cornered_tile_border_size
Ref<Image> const& source_image, Rect2i const& region, Vector2i const& new_cornered_tile_border_size
) {
ERR_FAIL_COND_V(source_image.is_null() || source_image->is_empty(), FAILED);
const Rect2i source_image_rect { {}, source_image->get_size() };
ERR_FAIL_COND_V(!region.has_area() || !source_image_rect.encloses(region), FAILED);
/* Whether we've already set the ImageTexture to an image of the right dimensions and format,
* and so can update it without creating and setting a new image, or not. */
bool can_update = state_image.is_valid() && state_image->get_size() == region.get_size()
&& state_image->get_format() == source_image->get_format();
* and so can update it without creating and setting a new image, or not. */
bool can_update = state_image.is_valid() && state_image->get_size() == region.get_size() &&
state_image->get_format() == source_image->get_format();
if (!can_update) {
state_image = Image::create(region.size.width, region.size.height, false, source_image->get_format());
ERR_FAIL_NULL_V(state_image, FAILED);
Expand Down Expand Up @@ -114,11 +112,10 @@ Error GFXButtonStateTexture::generate_state_image(
return { std::max(colour.r - 0.3f, 0.0f), std::max(colour.g - 0.3f, 0.0f), std::max(colour.b - 0.3f, 0.0f), colour.a };
};

const auto colour_func =
button_state == HOVER ? hover_colour :
button_state == PRESSED ? pressed_colour :
button_state == DISABLED ? disabled_colour :
selected_colour;
const auto colour_func = button_state == HOVER ? hover_colour
: button_state == PRESSED ? pressed_colour
: button_state == DISABLED ? disabled_colour
: selected_colour;

for (Vector2i point { 0, 0 }; point.y < state_image->get_height(); ++point.y) {
for (point.x = 0; point.x < state_image->get_width(); ++point.x) {
Expand All @@ -141,16 +138,11 @@ StringName const& GFXButtonStateTexture::button_state_to_name(ButtonState button
static const StringName name_selected = "selected";
static const StringName name_error = "INVALID BUTTON STATE";
switch (button_state) {
case HOVER:
return name_hover;
case PRESSED:
return name_pressed;
case DISABLED:
return name_disabled;
case SELECTED:
return name_selected;
default:
return name_error;
case HOVER: return name_hover;
case PRESSED: return name_pressed;
case DISABLED: return name_disabled;
case SELECTED: return name_selected;
default: return name_error;
}
}

Expand Down Expand Up @@ -181,15 +173,15 @@ void GFXButtonStateHavingTexture::_clear_button_states() {

GFXButtonStateHavingTexture::GFXButtonStateHavingTexture() : button_state_textures {} {}

Ref<GFXButtonStateTexture> GFXButtonStateHavingTexture::get_button_state_texture(
Ref<GFXButtonStateTexture> GFXButtonStateHavingTexture::get_button_state_texture( //
GFXButtonStateTexture::ButtonState button_state
) {
const size_t button_state_index = button_state;
ERR_FAIL_COND_V(button_state_index >= button_state_textures.size(), nullptr);
Ref<GFXButtonStateTexture>& button_state_texture = button_state_textures[button_state_index];
if (button_state_texture.is_null()) {
button_state_texture = GFXButtonStateTexture::make_gfx_button_state_texture(
button_state, button_image, get_region(), cornered_tile_border_size
button_state, button_image, get_region(), cornered_tile_border_size
);
ERR_FAIL_NULL_V(button_state_texture, nullptr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace OpenVic {
GDCLASS(GFXButtonStateTexture, GFXCorneredTileSupportingTexture)

public:
enum ButtonState {
enum ButtonState { //
HOVER,
PRESSED,
DISABLED,
Expand All @@ -49,8 +49,8 @@ namespace OpenVic {

/* Create a GFXButtonStateTexture using the specified godot::Image. Returns nullptr if generate_state_image fails. */
static godot::Ref<GFXButtonStateTexture> make_gfx_button_state_texture(
ButtonState button_state, godot::Ref<godot::Image> const& source_image, godot::Rect2i const& region,
godot::Vector2i const& cornered_tile_border_size
ButtonState button_state, godot::Ref<godot::Image> const& source_image, godot::Rect2i const& region,
godot::Vector2i const& cornered_tile_border_size
);

/* Set the ButtonState to be generated by this class (calling this does not trigger state image generation). */
Expand All @@ -59,8 +59,8 @@ namespace OpenVic {
/* Generate a modified version of the given region of source_image
* and update the underlying godot::ImageTexture to use it. */
godot::Error generate_state_image(
godot::Ref<godot::Image> const& source_image, godot::Rect2i const& region,
godot::Vector2i const& new_cornered_tile_border_size
godot::Ref<godot::Image> const& source_image, godot::Rect2i const& region,
godot::Vector2i const& new_cornered_tile_border_size
);

static godot::StringName const& button_state_to_name(ButtonState button_state);
Expand Down
57 changes: 26 additions & 31 deletions extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "GFXMaskedFlagTexture.hpp"

#include "openvic-extension/core/Bind.hpp"
#include "openvic-extension/core/Convert.hpp"
#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/core/Bind.hpp"
#include "openvic-extension/utility/UITools.hpp"
#include "openvic-extension/utility/Utilities.hpp"

Expand All @@ -14,11 +14,11 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
ERR_FAIL_NULL_V(overlay_image, FAILED);
/* Whether we've already set the ImageTexture to an image of the right dimensions and format,
* and so can update it without creating and setting a new image, or not. */
bool can_update = button_image.is_valid() && button_image->get_size() == overlay_image->get_size()
&& button_image->get_format() == overlay_image->get_format();
bool can_update = button_image.is_valid() && button_image->get_size() == overlay_image->get_size() &&
button_image->get_format() == overlay_image->get_format();
if (!can_update) {
button_image = Image::create(
overlay_image->get_width(), overlay_image->get_height(), false, overlay_image->get_format()
overlay_image->get_width(), overlay_image->get_height(), false, overlay_image->get_format() //
);
ERR_FAIL_NULL_V(button_image, FAILED);
}
Expand All @@ -35,27 +35,24 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
const Vector2i centre_translation = (mask_image->get_size() - button_image->get_size()) / 2;

for (
Vector2i combined_image_point { 0, 0 };
combined_image_point.y < button_image->get_height();
++combined_image_point.y
Vector2i combined_image_point { 0, 0 }; combined_image_point.y < button_image->get_height();
++combined_image_point.y //
) {

for (combined_image_point.x = 0; combined_image_point.x < button_image->get_width(); ++combined_image_point.x) {

const Color overlay_image_colour = overlay_image->get_pixelv(combined_image_point);

// Translate to mask_image coordinates, keeping the centres of each image aligned.
const Vector2i mask_image_point = combined_image_point + centre_translation;

if (
0 <= mask_image_point.x && mask_image_point.x < mask_image->get_width() &&
0 <= mask_image_point.y && mask_image_point.y < mask_image->get_height()
0 <= mask_image_point.x && mask_image_point.x < mask_image->get_width() && 0 <= mask_image_point.y &&
mask_image_point.y < mask_image->get_height() //
) {
const Color mask_image_colour = mask_image->get_pixelv(mask_image_point);

// Rescale from mask_image to flag_image coordinates.
const Vector2i flag_image_point =
flag_image_rect.position + mask_image_point * flag_image_rect.size / mask_image->get_size();
flag_image_rect.position + mask_image_point * flag_image_rect.size / mask_image->get_size();

Color flag_image_colour = flag_image->get_pixelv(flag_image_point);
flag_image_colour.a = mask_image_colour.a;
Expand Down Expand Up @@ -153,11 +150,11 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl

GFX::MaskedFlag const* new_masked_flag = sprite->cast_to<GFX::MaskedFlag>();
ERR_FAIL_NULL_V_MSG(
new_masked_flag, FAILED, Utilities::format(
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_masked_flag_name,
convert_to<String>(sprite->get_type()),
convert_to<String>(GFX::MaskedFlag::get_type_static())
)
new_masked_flag, FAILED,
Utilities::format(
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_masked_flag_name, convert_to<String>(sprite->get_type()),
convert_to<String>(GFX::MaskedFlag::get_type_static())
)
);

return set_gfx_masked_flag(new_masked_flag);
Expand All @@ -168,7 +165,7 @@ String GFXMaskedFlagTexture::get_gfx_masked_flag_name() const {
}

Error GFXMaskedFlagTexture::set_flag_country_and_type(
CountryDefinition const* new_flag_country, StringName const& new_flag_type
CountryDefinition const* new_flag_country, StringName const& new_flag_type
) {
if (flag_country == new_flag_country && flag_type == new_flag_type) {
return OK;
Expand All @@ -195,7 +192,7 @@ Error GFXMaskedFlagTexture::set_flag_country_and_type(
}

Error GFXMaskedFlagTexture::set_flag_country_name_and_type(
String const& new_flag_country_name, StringName const& new_flag_type
String const& new_flag_country_name, StringName const& new_flag_type
) {
if (new_flag_country_name.is_empty()) {
return set_flag_country_and_type(nullptr, {});
Expand All @@ -204,11 +201,10 @@ Error GFXMaskedFlagTexture::set_flag_country_name_and_type(
GameSingleton const* game_singleton = GameSingleton::get_singleton();
ERR_FAIL_NULL_V(game_singleton, FAILED);

CountryDefinition const* new_flag_country = game_singleton->get_definition_manager()
.get_country_definition_manager()
.get_country_definition_by_identifier(
convert_to<std::string>(new_flag_country_name)
);
CountryDefinitionManager const& manager = game_singleton->get_definition_manager().get_country_definition_manager();
CountryDefinition const* new_flag_country = manager.get_country_definition_by_identifier( //
convert_to<std::string>(new_flag_country_name)
);
ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, Utilities::format("Country not found: %s", new_flag_country_name));

return set_flag_country_and_type(new_flag_country, new_flag_type);
Expand All @@ -221,9 +217,9 @@ Error GFXMaskedFlagTexture::set_flag_country(CountryInstance* new_flag_country)

GovernmentType const* government_type = new_flag_country->flag_government_type.get_untracked();

const StringName new_flag_type = government_type != nullptr
? StringName { convert_to<String>(government_type->get_flag_type()) }
: StringName {};
const StringName new_flag_type = government_type != nullptr //
? StringName { convert_to<String>(government_type->get_flag_type()) }
: StringName {};

return set_flag_country_and_type(&new_flag_country->country_definition, new_flag_type);
}
Expand All @@ -239,10 +235,9 @@ Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country
InstanceManager* instance_manager = game_singleton->get_instance_manager();
ERR_FAIL_NULL_V(instance_manager, FAILED);

CountryInstance* new_flag_country = instance_manager->get_country_instance_manager()
.get_country_instance_by_identifier(
convert_to<std::string>(new_flag_country_name)
);
CountryInstance* new_flag_country = instance_manager->get_country_instance_manager().get_country_instance_by_identifier(
convert_to<std::string>(new_flag_country_name)
);
ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, Utilities::format("Country not found: %s", new_flag_country_name));

return set_flag_country(new_flag_country);
Expand Down
Loading
Loading