aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2024-02-19 23:35:27 +0100
committer GitHub <noreply@github.com>2024-02-19 23:35:27 +0100
commit5d7c6eafe35e2c6e952bc0b3f91d27d760c8e75e (patch)
tree1912fb231991b53dd638a295bb8d8f84b587885b /extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
parent275cfbb62fe69828aeb9968110ad822447322a4e (diff)
parentc48d14ca66d47ea7c25bb9a36c3d51f76d8351fc (diff)
Merge pull request #208 from OpenVicProject/sprite-texture
Added multipurpose GFXSpriteTexture + reworked GFXButtonStateTexture
Diffstat (limited to 'extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp')
-rw-r--r--extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp61
1 files changed, 47 insertions, 14 deletions
diff --git a/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp b/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
index e6dff1f..dcee965 100644
--- a/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
+++ b/extension/src/openvic-extension/classes/GFXButtonStateTexture.cpp
@@ -1,5 +1,7 @@
#include "GFXButtonStateTexture.hpp"
+#include <godot_cpp/variant/utility_functions.hpp>
+
#include "openvic-extension/utility/ClassBindings.hpp"
using namespace OpenVic;
@@ -9,8 +11,6 @@ void GFXButtonStateTexture::_bind_methods() {
OV_BIND_METHOD(GFXButtonStateTexture::set_button_state, { "new_button_state" });
OV_BIND_METHOD(GFXButtonStateTexture::get_button_state);
- OV_BIND_SMETHOD(get_generate_state_image_func_name);
-
OV_BIND_SMETHOD(button_state_to_theme_name, { "button_state" });
OV_BIND_METHOD(GFXButtonStateTexture::get_button_state_theme);
@@ -24,14 +24,14 @@ void GFXButtonStateTexture::_bind_methods() {
GFXButtonStateTexture::GFXButtonStateTexture() : button_state { HOVER } {}
Ref<GFXButtonStateTexture> GFXButtonStateTexture::make_gfx_button_state_texture(
- ButtonState button_state, Ref<Image> const& source_image
+ ButtonState button_state, Ref<Image> const& source_image, Rect2i const& region
) {
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) != OK, nullptr);
+ ERR_FAIL_COND_V(button_state_texture->generate_state_image(source_image, region) != OK, nullptr);
}
return button_state_texture;
}
@@ -41,14 +41,16 @@ void GFXButtonStateTexture::set_button_state(ButtonState new_button_state) {
button_state = new_button_state;
}
-Error GFXButtonStateTexture::generate_state_image(Ref<Image> const& source_image) {
+Error GFXButtonStateTexture::generate_state_image(Ref<Image> const& source_image, Rect2i const& region) {
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. */
- const bool can_update = state_image.is_valid() && state_image->get_size() == source_image->get_size()
+ const 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(source_image->get_width(), source_image->get_height(), false, source_image->get_format());
+ state_image = Image::create(region.size.width, region.size.height, false, source_image->get_format());
ERR_FAIL_NULL_V(state_image, FAILED);
}
@@ -67,7 +69,7 @@ Error GFXButtonStateTexture::generate_state_image(Ref<Image> const& source_image
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) {
- state_image->set_pixelv(point, colour_func(source_image->get_pixelv(point)));
+ state_image->set_pixelv(point, colour_func(source_image->get_pixelv(region.position + point)));
}
}
@@ -79,16 +81,11 @@ Error GFXButtonStateTexture::generate_state_image(Ref<Image> const& source_image
return OK;
}
-StringName const& GFXButtonStateTexture::get_generate_state_image_func_name() {
- static const StringName generate_state_image_func_name = "generate_state_image";
- return generate_state_image_func_name;
-}
-
StringName const& GFXButtonStateTexture::button_state_to_theme_name(ButtonState button_state) {
static const StringName theme_name_hover = "hover";
static const StringName theme_name_pressed = "pressed";
static const StringName theme_name_disabled = "disabled";
- static const StringName theme_name_error = "";
+ static const StringName theme_name_error = "INVALID BUTTON STATE";
switch (button_state) {
case HOVER:
return theme_name_hover;
@@ -104,3 +101,39 @@ StringName const& GFXButtonStateTexture::button_state_to_theme_name(ButtonState
StringName const& GFXButtonStateTexture::get_button_state_theme() const {
return button_state_to_theme_name(button_state);
}
+
+void GFXButtonStateHavingTexture::_bind_methods() {
+ OV_BIND_METHOD(GFXButtonStateHavingTexture::get_button_state_texture, { "button_state" });
+}
+
+void GFXButtonStateHavingTexture::_update_button_states() {
+ for (Ref<GFXButtonStateTexture>& button_state_texture : button_state_textures) {
+ if (button_state_texture.is_valid()) {
+ button_state_texture->generate_state_image(button_image, get_region());
+ }
+ }
+}
+
+void GFXButtonStateHavingTexture::_clear_button_states() {
+ set_atlas(nullptr);
+ set_region({});
+ button_image.unref();
+ for (Ref<GFXButtonStateTexture>& button_state_texture : button_state_textures) {
+ button_state_texture.unref();
+ }
+}
+
+GFXButtonStateHavingTexture::GFXButtonStateHavingTexture() : button_image {}, button_state_textures {} {}
+
+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());
+ ERR_FAIL_NULL_V(button_state_texture, nullptr);
+ }
+ return button_state_texture;
+}