aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/classes/GUIButton.cpp
blob: 323b03cc2df509497276022518f3bbefb01e11d0 (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
#include "GUIButton.hpp"

#include <array>

#include <godot_cpp/variant/utility_functions.hpp>

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

using namespace godot;
using namespace OpenVic;

void GUIButton::_bind_methods() {}

Error GUIButton::set_gfx_button_state_having_texture(Ref<GFXButtonStateHavingTexture> const& texture) {
   ERR_FAIL_NULL_V(texture, FAILED);

   Error err = OK;

   set_custom_minimum_size(texture->get_size());

   {
      Ref<StyleBoxTexture> stylebox = AssetManager::make_stylebox_texture(texture);

      if (stylebox.is_valid()) {
         static const StringName normal_theme = "normal";

         add_theme_stylebox_override(normal_theme, stylebox);
      } else {
         UtilityFunctions::push_error("Failed to make StyleBoxTexture for GUIButton ", get_name());

         err = FAILED;
      }
   }

   using enum GFXButtonStateTexture::ButtonState;

   for (GFXButtonStateTexture::ButtonState button_state : { HOVER, PRESSED, DISABLED }) {
      Ref<GFXButtonStateTexture> button_state_texture = texture->get_button_state_texture(button_state);

      if (button_state_texture.is_valid()) {
         Ref<StyleBoxTexture> stylebox = AssetManager::make_stylebox_texture(button_state_texture);

         if (stylebox.is_valid()) {
            add_theme_stylebox_override(button_state_texture->get_button_state_name(), stylebox);
         } else {
            UtilityFunctions::push_error(
               "Failed to make ", GFXButtonStateTexture::button_state_to_name(button_state),
               " StyleBoxTexture for GUIButton ", get_name()
            );

            err = FAILED;
         }
      } else {
         UtilityFunctions::push_error(
            "Failed to make ", GFXButtonStateTexture::button_state_to_name(button_state),
            " GFXButtonStateTexture for GUIButton ", get_name()
         );

         err = FAILED;
      }
   }

   return err;
}

Error GUIButton::set_gfx_font(GFX::Font const* gfx_font) {
   ERR_FAIL_NULL_V(gfx_font, FAILED);

   AssetManager* asset_manager = AssetManager::get_singleton();
   ERR_FAIL_NULL_V(asset_manager, FAILED);

   Error err = OK;

   const StringName font_file = Utilities::std_to_godot_string(gfx_font->get_fontname());
   const Ref<Font> font = asset_manager->get_font(font_file);

   if (font.is_valid()) {
      static const StringName font_theme = "font";

      add_theme_font_override(font_theme, font);
   } else {
      UtilityFunctions::push_error("Failed to load font \"", font_file, "\" for GUIButton ", get_name());

      err = FAILED;
   }

   static const std::array<StringName, 5> button_font_themes {
      "font_color", "font_hover_color", "font_hover_pressed_color", "font_pressed_color", "font_disabled_color"
   };

   const Color colour = Utilities::to_godot_color(gfx_font->get_colour());

   for (StringName const& theme_name : button_font_themes) {
      add_theme_color_override(theme_name, colour);
   }

   return err;
}