aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp
blob: d97ca2bfeed107dd5d9ae9be966443fb6c312cb3 (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
#pragma once

#include <godot_cpp/classes/atlas_texture.hpp>
#include <godot_cpp/classes/image_texture.hpp>

#include <openvic-simulation/utility/Getters.hpp>

namespace OpenVic {
   class GFXCorneredTileSupportingTexture : public godot::AtlasTexture {
      GDCLASS(GFXCorneredTileSupportingTexture, godot::AtlasTexture)

   protected:
      godot::Vector2i PROPERTY_ACCESS(cornered_tile_border_size, protected);

      static void _bind_methods();

   public:
      GFXCorneredTileSupportingTexture();

      /* Returns true if the texture has a non-zero 9 patch border. */
      bool is_cornered_tile_texture() const;

      /* Equivalent to draw_rect, but draws a 9 patch texture if this is a cornered tile texture. */
      void draw_rect_cornered(godot::RID const& to_canvas_item, godot::Rect2 const& rect) const;
   };

   class GFXButtonStateTexture : public GFXCorneredTileSupportingTexture {
      GDCLASS(GFXButtonStateTexture, GFXCorneredTileSupportingTexture)

   public:
      enum ButtonState {
         HOVER,
         PRESSED,
         DISABLED,
         SELECTED,
         BUTTON_STATE_COUNT
      };

   private:
      ButtonState PROPERTY(button_state);
      godot::Ref<godot::Image> state_image;
      godot::Ref<godot::ImageTexture> state_texture;

   protected:
      static void _bind_methods();

   public:
      GFXButtonStateTexture();

      /* 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
      );

      /* Set the ButtonState to be generated by this class (calling this does not trigger state image generation). */
      void set_button_state(ButtonState new_button_state);

      /* 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
      );

      static godot::StringName const& button_state_to_name(ButtonState button_state);
      godot::StringName const& get_button_state_name() const;
   };

   class GFXButtonStateHavingTexture : public GFXCorneredTileSupportingTexture {
      GDCLASS(GFXButtonStateHavingTexture, GFXCorneredTileSupportingTexture)

      std::array<godot::Ref<GFXButtonStateTexture>, GFXButtonStateTexture::BUTTON_STATE_COUNT> button_state_textures;

   protected:
      static void _bind_methods();

      godot::Ref<godot::Image> button_image;

      void _update_button_states();
      void _clear_button_states();

      GFXButtonStateHavingTexture();

   public:
      godot::Ref<GFXButtonStateTexture> get_button_state_texture(GFXButtonStateTexture::ButtonState button_state);
   };
}

VARIANT_ENUM_CAST(OpenVic::GFXButtonStateTexture::ButtonState);