aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp
blob: fdc1a1056adb0caa5381f5d79745d4324ba2082e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "GFXMaskedFlagTexture.hpp"

#include "openvic-extension/singletons/AssetManager.hpp"
#include "openvic-extension/singletons/GameSingleton.hpp"
#include "openvic-extension/utility/ClassBindings.hpp"
#include "openvic-extension/utility/UITools.hpp"
#include "openvic-extension/utility/Utilities.hpp"

using namespace godot;
using namespace OpenVic;

using OpenVic::Utilities::godot_to_std_string;
using OpenVic::Utilities::std_view_to_godot_string;
using OpenVic::Utilities::std_view_to_godot_string_name;

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();
   if (!can_update) {
      button_image = Image::create(
         overlay_image->get_width(), overlay_image->get_height(), false, overlay_image->get_format()
      );
      ERR_FAIL_NULL_V(button_image, FAILED);
   }

   if (combined_texture.is_null()) {
      can_update = false;
      combined_texture.instantiate();
      ERR_FAIL_NULL_V(combined_texture, FAILED);
      set_atlas(combined_texture);
      set_region({ {}, button_image->get_size() });
   }

   if (mask_image.is_valid() && flag_image.is_valid() && flag_image_rect.has_area()) {
      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
      ) {

         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()
            ) {
               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();

               Color flag_image_colour = flag_image->get_pixelv(flag_image_point);
               flag_image_colour.a = mask_image_colour.a;

               button_image->set_pixelv(combined_image_point, flag_image_colour.blend(overlay_image_colour));
            } else {
               button_image->set_pixelv(combined_image_point, overlay_image_colour);
            }
         }
      }
   } else {
      button_image->blit_rect(overlay_image, overlay_image->get_used_rect(), {});
   }

   if (can_update) {
      combined_texture->update(button_image);
   } else {
      combined_texture->set_image(button_image);
   }
   _update_button_states();
   return OK;
}

void GFXMaskedFlagTexture::_bind_methods() {
   OV_BIND_METHOD(GFXMaskedFlagTexture::clear);

   OV_BIND_METHOD(GFXMaskedFlagTexture::set_gfx_masked_flag_name, { "gfx_masked_flag_name" });
   OV_BIND_METHOD(GFXMaskedFlagTexture::get_gfx_masked_flag_name);

   OV_BIND_METHOD(GFXMaskedFlagTexture::set_flag_country_name_and_type, { "new_flag_country_name", "new_flag_type" });
   OV_BIND_METHOD(GFXMaskedFlagTexture::set_flag_country_name, { "new_flag_country_name" });
   OV_BIND_METHOD(GFXMaskedFlagTexture::get_flag_country_name);
   OV_BIND_METHOD(GFXMaskedFlagTexture::get_flag_type);
}

GFXMaskedFlagTexture::GFXMaskedFlagTexture() : gfx_masked_flag { nullptr }, flag_country { nullptr } {}

Ref<GFXMaskedFlagTexture> GFXMaskedFlagTexture::make_gfx_masked_flag_texture(GFX::MaskedFlag const* gfx_masked_flag) {
   Ref<GFXMaskedFlagTexture> masked_flag_texture;
   masked_flag_texture.instantiate();
   ERR_FAIL_NULL_V(masked_flag_texture, nullptr);
   ERR_FAIL_COND_V(masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) != OK, nullptr);
   return masked_flag_texture;
}

void GFXMaskedFlagTexture::clear() {
   gfx_masked_flag = nullptr;
   flag_country = nullptr;
   flag_type = String {};

   _clear_button_states();

   overlay_image.unref();
   mask_image.unref();
   flag_image.unref();
}

Error GFXMaskedFlagTexture::set_gfx_masked_flag(GFX::MaskedFlag const* new_gfx_masked_flag) {
   if (gfx_masked_flag == new_gfx_masked_flag) {
      return OK;
   }
   if (new_gfx_masked_flag == nullptr) {
      clear();
      return OK;
   }
   AssetManager* asset_manager = AssetManager::get_singleton();
   ERR_FAIL_NULL_V(asset_manager, FAILED);

   const StringName overlay_file = std_view_to_godot_string_name(new_gfx_masked_flag->get_overlay_file());
   const Ref<Image> new_overlay_image = asset_manager->get_image(overlay_file);
   ERR_FAIL_NULL_V_MSG(new_overlay_image, FAILED, vformat("Failed to load flag overlay image: %s", overlay_file));

   const StringName mask_file = std_view_to_godot_string_name(new_gfx_masked_flag->get_mask_file());
   const Ref<Image> new_mask_image = asset_manager->get_image(mask_file);
   ERR_FAIL_NULL_V_MSG(new_mask_image, FAILED, vformat("Failed to load flag mask image: %s", mask_file));

   gfx_masked_flag = new_gfx_masked_flag;
   overlay_image = new_overlay_image;
   mask_image = new_mask_image;

   return _generate_combined_image();
}

Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_flag_name) {
   if (gfx_masked_flag_name.is_empty()) {
      return set_gfx_masked_flag(nullptr);
   }
   GFX::Sprite const* sprite = UITools::get_gfx_sprite(gfx_masked_flag_name);
   ERR_FAIL_NULL_V(sprite, FAILED);
   GFX::MaskedFlag const* new_masked_flag = sprite->cast_to<GFX::MaskedFlag>();
   ERR_FAIL_NULL_V_MSG(
      new_masked_flag, FAILED, vformat(
         "Invalid type for GFX sprite %s: %s (expected %s)", gfx_masked_flag_name,
         std_view_to_godot_string(sprite->get_type()), std_view_to_godot_string(GFX::MaskedFlag::get_type_static())
      )
   );
   return set_gfx_masked_flag(new_masked_flag);
}

String GFXMaskedFlagTexture::get_gfx_masked_flag_name() const {
   return gfx_masked_flag != nullptr ? std_view_to_godot_string(gfx_masked_flag->get_name()) : String {};
}

Error GFXMaskedFlagTexture::set_flag_country_and_type(Country const* new_flag_country, StringName const& new_flag_type) {
   if (flag_country == new_flag_country && flag_type == new_flag_type) {
      return OK;
   }
   if (new_flag_country != nullptr) {
      GameSingleton* game_singleton = GameSingleton::get_singleton();
      ERR_FAIL_NULL_V(game_singleton, FAILED);

      flag_image_rect = game_singleton->get_flag_sheet_rect(new_flag_country->get_index(), new_flag_type);
      ERR_FAIL_COND_V(!flag_image_rect.has_area(), FAILED);

      flag_country = new_flag_country;
      flag_type = new_flag_type;
      flag_image = game_singleton->get_flag_sheet_image();
   } else {
      // TODO - use REB flag as default/error flag
      flag_country = nullptr;
      flag_type = String {};
      flag_image.unref();
   }
   return _generate_combined_image();
}

Error GFXMaskedFlagTexture::set_flag_country_name_and_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, {});
   }
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, FAILED);
   Country const* new_flag_country = game_singleton->get_definition_manager().get_country_manager().get_country_by_identifier(
      godot_to_std_string(new_flag_country_name)
   );
   ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, vformat("Country not found: %s", new_flag_country_name));
   return set_flag_country_and_type(new_flag_country, new_flag_type);
}

Error GFXMaskedFlagTexture::set_flag_country(Country const* new_flag_country) {
   // TODO - get country's current flag type from the game state
   return set_flag_country_and_type( new_flag_country, {});
}

Error GFXMaskedFlagTexture::set_flag_country_name(String const& new_flag_country_name) {
   if (new_flag_country_name.is_empty()) {
      return set_flag_country(nullptr);
   }
   GameSingleton* game_singleton = GameSingleton::get_singleton();
   ERR_FAIL_NULL_V(game_singleton, FAILED);
   Country const* new_flag_country = game_singleton->get_definition_manager().get_country_manager().get_country_by_identifier(
      godot_to_std_string(new_flag_country_name)
   );
   ERR_FAIL_NULL_V_MSG(new_flag_country, FAILED, vformat("Country not found: %s", new_flag_country_name));
   return set_flag_country(new_flag_country);
}

String GFXMaskedFlagTexture::get_flag_country_name() const {
   return flag_country != nullptr ? std_view_to_godot_string(flag_country->get_identifier()) : String {};
}