Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/windowing/StarButtonWidget.hpp
blob: bace69b63fe7735be5ac15e705ffd16b3021c900 (plain)
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
#pragma once

#include "StarButtonGroup.hpp"

namespace Star {

STAR_CLASS(ButtonWidget);

class ButtonWidget : public Widget {
public:
  ButtonWidget();
  ButtonWidget(WidgetCallbackFunc callback,
      String const& baseImage,
      String const& hoverImage = "",
      String const& pressedImage = "",
      String const& disabledImage = "");
  virtual ~ButtonWidget();

  virtual bool sendEvent(InputEvent const& event) override;
  virtual void mouseOver() override;
  virtual void mouseOut() override;
  virtual void mouseReturnStillDown() override;
  virtual void hide() override;

  // Callback is called when the checked / pressed state is changed.
  void setCallback(WidgetCallbackFunc callback);

  ButtonGroupPtr buttonGroup() const;
  // Sets the button group for this widget, and adds it to the button group if
  // it is not already added.  Additionally, sets the button as checkable.
  void setButtonGroup(ButtonGroupPtr buttonGroup, int id = ButtonGroup::NoButton);
  // If a button group is set, returns this button's id in the button group.
  int buttonGroupId();

  bool isHovered() const;

  bool isPressed() const;
  void setPressed(bool pressed);

  bool isCheckable() const;
  void setCheckable(bool checkable);

  bool isHighlighted() const;
  void setHighlighted(bool highlighted);

  bool isChecked() const;
  void setChecked(bool checked);
  // Either checks a button, or toggles the state, depending on whether the
  // button is part of an exclusive group or not.
  void check();

  bool sustainCallbackOnDownHold();
  void setSustainCallbackOnDownHold(bool sustain);

  void setImages(String const& baseImage,
      String const& hoverImage = "",
      String const& pressedImage = "",
      String const& disabledImage = "");
  void setCheckedImages(String const& baseImage,
      String const& hoverImage = "",
      String const& pressedImage = "",
      String const& disabledImage = "");
  void setOverlayImage(String const& overlayImage = "");

  // Used to offset drawing when the button is being pressed / checked
  Vec2I const& pressedOffset() const;
  void setPressedOffset(Vec2I const& offset);

  virtual String const& getText() const;
  virtual void setText(String const& text);
  virtual void setFontSize(int size);
  virtual void setFontDirectives(String directives);
  virtual void setTextOffset(Vec2I textOffset);

  void setTextAlign(HorizontalAnchor hAnchor);
  void setFontColor(Color color);
  void setFontColorDisabled(Color color);
  void setFontColorChecked(Color color);

  virtual WidgetPtr getChildAt(Vec2I const& pos) override;

  void disable();
  void enable();
  void setEnabled(bool enabled);

  void setInvisible(bool invisible);

protected:
  virtual RectI getScissorRect() const override;
  virtual void renderImpl() override;

  void drawButtonPart(String const& image, Vec2F const& position);
  void updateSize();

  WidgetCallbackFunc m_callback;
  ButtonGroupPtr m_buttonGroup;

  bool m_hovered;
  bool m_pressed;
  bool m_checkable;
  bool m_checked;

  bool m_disabled;
  bool m_highlighted;

  String m_baseImage;
  String m_hoverImage;
  String m_pressedImage;
  String m_disabledImage;

  bool m_hasCheckedImages;
  String m_baseImageChecked;
  String m_hoverImageChecked;
  String m_pressedImageChecked;
  String m_disabledImageChecked;

  String m_overlayImage;

  bool m_invisible;

  Vec2I m_pressedOffset;
  Vec2U m_buttonBoundSize;

  TextStyle m_textStyle;
  String m_text;
  Vec2I m_textOffset;

  StringList m_clickSounds;
  StringList m_releaseSounds;
  StringList m_hoverSounds;
  StringList m_hoverOffSounds;

  bool m_sustain;

private:
  HorizontalAnchor m_hTextAnchor;
  Color m_fontColor;
  Color m_fontColorDisabled;
  Maybe<Color> m_fontColorChecked;
};

}