ManaPlus
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes

#include <button.h>

Inheritance diagram for Button:
Widget MouseListener KeyListener FocusListener WidgetListener Widget2

Public Types

enum  {
  BUTTON_STANDARD = 0, BUTTON_HIGHLIGHTED, BUTTON_PRESSED, BUTTON_DISABLED,
  BUTTON_COUNT
}
 

Public Member Functions

 Button (const Widget2 *const widget)
 
 Button (const Widget2 *const widget, const std::string &caption, const std::string &actionEventId, ActionListener *const listener)
 
 Button (const Widget2 *const widget, const std::string &caption, const std::string &imageName, const int imageWidth, const int imageHeight, const std::string &actionEventId, ActionListener *const listener)
 
 Button (const Widget2 *const widget, const std::string &imageName, const int imageWidth, const int imageHeight, const std::string &actionEventId, ActionListener *const listener)
 
 Button (const Widget2 *const widget, const std::string &imageName, const std::string &caption, const std::string &actionEventId, ActionListener *const listener)
 
 ~Button ()
 
void draw (Graphics *const graphics)
 
void safeDraw (Graphics *const graphics)
 
void mouseReleased (MouseEvent &event)
 
void setDescription (const std::string &text)
 
std::string getDescription () const
 
int getClickCount () const
 
void setTag (int tag)
 
int getTag () const
 
void setStick (bool b)
 
void setPressed (bool b)
 
void widgetResized (const Event &event)
 
void widgetMoved (const Event &event)
 
void widgetHidden (const Event &event)
 
void loadImage (const std::string &imageName)
 
void loadImageSet (const std::string &imageName)
 
void adjustSize ()
 
void keyPressed (KeyEvent &event)
 
void keyReleased (KeyEvent &event)
 
bool isPressed2 () const
 
void setCaption (const std::string &caption)
 
const std::string & getCaption () const
 
void setAlignment (Graphics::Alignment alignment)
 
Graphics::Alignment getAlignment () const
 
void focusLost (const Event &event)
 
void mousePressed (MouseEvent &event)
 
void mouseEntered (MouseEvent &event)
 
void mouseExited (MouseEvent &event)
 
void mouseDragged (MouseEvent &event)
 
void setParent (Widget *widget)
 
void setWindow (Widget *const widget)
 
void setImageWidth (const int width)
 
void setImageHeight (const int height)
 
- Public Member Functions inherited from Widget
 Widget (const Widget2 *const widget)
 
 ~Widget ()
 
virtual void drawFrame (Graphics *graphics)
 
virtual void safeDrawFrame (Graphics *graphics)
 
void setFrameSize (const unsigned int frameSize)
 
unsigned int getFrameSize () const
 
virtual void logic ()
 
WidgetgetParent () const
 
void setWidth (const int width)
 
int getWidth () const
 
void setHeight (const int height)
 
int getHeight () const
 
void setSize (const int width, const int height)
 
void setX (const int x)
 
int getX () const
 
void setY (const int y)
 
int getY () const
 
void setPosition (const int x, const int y)
 
void setDimension (const Rect &dimension)
 
const RectgetDimension () const
 
void setFocusable (const bool focusable)
 
bool isFocusable () const
 
virtual bool isFocused () const
 
void setEnabled (const bool enabled)
 
bool isEnabled () const
 
void setVisible (Visible visible)
 
bool isVisible () const
 
void setBaseColor (const Color &color)
 
const ColorgetBaseColor () const
 
void setForegroundColor (const Color &color)
 
const ColorgetForegroundColor () const
 
void setBackgroundColor (const Color &color)
 
const ColorgetBackgroundColor () const
 
virtual void requestFocus ()
 
virtual void requestMoveToTop ()
 
virtual void requestMoveToBottom ()
 
virtual void setFocusHandler (FocusHandler *const focusHandler)
 
FocusHandlergetFocusHandler ()
 
void addActionListener (ActionListener *const actionListener)
 
void removeActionListener (ActionListener *const actionListener)
 
void addDeathListener (WidgetDeathListener *const deathListener)
 
void removeDeathListener (WidgetDeathListener *const deathListener)
 
void addMouseListener (MouseListener *const mouseListener)
 
void removeMouseListener (MouseListener *const mouseListener)
 
void addKeyListener (KeyListener *const keyListener)
 
void removeKeyListener (KeyListener *const keyListener)
 
void addFocusListener (FocusListener *const focusListener)
 
void removeFocusListener (FocusListener *const focusListener)
 
void addWidgetListener (WidgetListener *const widgetListener)
 
void removeWidgetListener (WidgetListener *const widgetListener)
 
void setActionEventId (const std::string &actionEventId)
 
const std::string & getActionEventId () const
 
virtual void getAbsolutePosition (int &x, int &y) const
 
FontgetFont () const
 
void setFont (Font *const font)
 
virtual void fontChanged ()
 
bool isTabInEnabled () const
 
void setTabInEnabled (const bool enabled)
 
bool isTabOutEnabled () const
 
void setTabOutEnabled (const bool enabled)
 
void requestModalFocus ()
 
virtual void requestModalMouseInputFocus ()
 
virtual void releaseModalFocus ()
 
virtual void releaseModalMouseInputFocus ()
 
virtual bool isModalFocused () const
 
virtual bool isModalMouseInputFocused () const
 
virtual WidgetgetWidgetAt (int x, int y)
 
const std::list< MouseListener * > & getMouseListeners () const A_CONST
 
const std::list< KeyListener * > & getKeyListeners () const A_CONST
 
const std::list< FocusListener * > & getFocusListeners () const A_CONST
 
virtual Rect getChildrenArea ()
 
virtual FocusHandlergetInternalFocusHandler ()
 
void setInternalFocusHandler (FocusHandler *const internalFocusHandler)
 
virtual void moveToTop (Widget *widget)
 
virtual void moveToBottom (Widget *widget)
 
virtual void focusNext ()
 
virtual void focusPrevious ()
 
virtual void showWidgetPart (Widget *const widget, const Rect &area)
 
void setId (const std::string &id)
 
const std::string & getId () const
 
virtual void showPart (const Rect &rectangle)
 
bool isAllowLogic () const
 
void setMouseConsume (const bool b)
 
bool isMouseConsume () const
 
void setRedraw (const bool b)
 
virtual bool isSelectable () const
 
void setSelectable (const bool selectable)
 
void windowResized ()
 
virtual void postInit ()
 
- Public Member Functions inherited from Widget2
virtual ~Widget2 ()
 
const ColorgetThemeColor (const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
 
const ColorgetThemeCharColor (const signed char c, bool &valid) const A_INLINE
 
virtual void setWidget2 (const Widget2 *const widget)
 
void setPalette (int palette)
 
void checkPalette ()
 
void setForegroundColor2 (const Color &color)
 
WidgetgetWindow () const
 
- Public Member Functions inherited from MouseListener
virtual ~MouseListener ()
 
virtual void mouseClicked (MouseEvent &event)
 
virtual void mouseWheelMovedUp (MouseEvent &event)
 
virtual void mouseWheelMovedDown (MouseEvent &event)
 
virtual void mouseMoved (MouseEvent &event)
 
- Public Member Functions inherited from KeyListener
virtual ~KeyListener ()
 
- Public Member Functions inherited from FocusListener
virtual ~FocusListener ()
 
virtual void focusGained (const Event &event)
 
- Public Member Functions inherited from WidgetListener
virtual ~WidgetListener ()
 
virtual void widgetShown (const Event &event)
 

Static Public Member Functions

static void updateAlpha ()
 
- Static Public Member Functions inherited from Widget
static void setGlobalFont (Font *const font)
 
static FontgetGloablFont ()
 
static void cleanGlobalFont ()
 
static bool widgetExists (const Widget *const widget)
 
static void distributeWindowResizeEvent ()
 
static WidgetcallPostInit (Widget *const widget)
 

Private Member Functions

bool isPressed () const
 
void init ()
 

Private Attributes

std::string mCaption
 
std::string mDescription
 
TextChunk mTextChunk
 
ImageCollectionmVertexes2
 
Color mEnabledColor
 
Color mEnabledColor2
 
Color mDisabledColor
 
Color mDisabledColor2
 
Color mHighlightedColor
 
Color mHighlightedColor2
 
Color mPressedColor
 
Color mPressedColor2
 
Image ** mImages
 
ImageSetmImageSet
 
Graphics::Alignment mAlignment
 
int mClickCount
 
int mSpacing [BUTTON_COUNT]
 
int mTag
 
int mMode
 
int mXOffset
 
int mYOffset
 
int mImageWidth
 
int mImageHeight
 
bool mHasMouse
 
bool mKeyPressed
 
bool mMousePressed
 
bool mStick
 
bool mPressed
 
bool mTextChanged
 

Static Private Attributes

static Skinbutton [BUTTON_COUNT]
 
static int mInstances = 0
 
static float mAlpha = 1.0
 

Additional Inherited Members

- Data Fields inherited from Widget
Visible mVisible
 
- Protected Types inherited from Widget
typedef std::list< MouseListener * > MouseListenerList
 
typedef MouseListenerList::iterator MouseListenerIterator
 
typedef std::list< KeyListener * > KeyListenerList
 
typedef KeyListenerList::iterator KeyListenerIterator
 
typedef std::list< ActionListener * > ActionListenerList
 
typedef ActionListenerList::iterator ActionListenerIterator
 
typedef std::list< WidgetDeathListener * > WidgetDeathListenerList
 
typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator
 
typedef std::list< FocusListener * > FocusListenerList
 
typedef FocusListenerList::iterator FocusListenerIterator
 
typedef std::list< WidgetListener * > WidgetListenerList
 
typedef WidgetListenerList::iterator WidgetListenerIterator
 
- Protected Member Functions inherited from Widget
void distributeActionEvent ()
 
void distributeResizedEvent ()
 
void distributeMovedEvent ()
 
void distributeHiddenEvent ()
 
void distributeShownEvent ()
 
- Protected Member Functions inherited from Widget2
 Widget2 (const Widget2 *const widget)
 
- Protected Member Functions inherited from MouseListener
 MouseListener ()
 
- Protected Member Functions inherited from KeyListener
 KeyListener ()
 
- Protected Member Functions inherited from FocusListener
 FocusListener ()
 
- Protected Member Functions inherited from WidgetListener
 WidgetListener ()
 
- Protected Attributes inherited from Widget
MouseListenerList mMouseListeners
 
KeyListenerList mKeyListeners
 
ActionListenerList mActionListeners
 
WidgetDeathListenerList mDeathListeners
 
FocusListenerList mFocusListeners
 
WidgetListenerList mWidgetListeners
 
Color mForegroundColor
 
Color mBackgroundColor
 
Color mBaseColor
 
Rect mDimension
 
std::string mActionEventId
 
std::string mId
 
FocusHandlermFocusHandler
 
FocusHandlermInternalFocusHandler
 
WidgetmParent
 
FontmCurrentFont
 
unsigned int mFrameSize
 
bool mFocusable
 
bool mTabIn
 
bool mTabOut
 
bool mEnabled
 
bool mAllowLogic
 
bool mMouseConsume
 
bool mRedraw
 
bool mSelectable
 
- Protected Attributes inherited from Widget2
int mPaletteOffset
 
WidgetmWindow
 
Color mForegroundColor2
 
- Static Protected Attributes inherited from Widget
static FontmGlobalFont = 0
 

Detailed Description

Button widget. Same as the Guichan button but with custom look.

Definition at line 94 of file button.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
BUTTON_STANDARD 
BUTTON_HIGHLIGHTED 
BUTTON_PRESSED 
BUTTON_DISABLED 
BUTTON_COUNT 

Definition at line 264 of file button.h.

265  {
266  BUTTON_STANDARD = 0, // 0
267  BUTTON_HIGHLIGHTED, // 1
268  BUTTON_PRESSED, // 2
269  BUTTON_DISABLED, // 3
270  BUTTON_COUNT // 4 - Must be last.
271  };

Constructor & Destructor Documentation

◆ Button() [1/5]

Button::Button ( const Widget2 *const  widget)
explicit

Default constructor.

Definition at line 101 of file button.cpp.

References adjustSize(), and init().

101  :
102  Widget(widget),
103  MouseListener(),
104  KeyListener(),
105  FocusListener(),
106  WidgetListener(),
107  mCaption(),
108  mDescription(),
109  mTextChunk(),
115  255U)),
121  mImages(nullptr),
122  mImageSet(nullptr),
124  mClickCount(0),
125  mSpacing(),
126  mTag(0),
127  mMode(0),
128  mXOffset(0),
129  mYOffset(0),
130  mImageWidth(0),
131  mImageHeight(0),
132  mHasMouse(false),
133  mKeyPressed(false),
134  mMousePressed(false),
135  mStick(false),
136  mPressed(false),
137  mTextChanged(true)
138 {
139  init();
140  adjustSize();
141 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Image ** mImages
Definition: button.h:306
bool mStick
Definition: button.h:342
Color mDisabledColor2
Definition: button.h:301
bool mKeyPressed
Definition: button.h:335
TextChunk mTextChunk
Definition: button.h:295
Color mHighlightedColor
Definition: button.h:302
int mMode
Definition: button.h:322
ImageSet * mImageSet
Definition: button.h:307
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
void adjustSize()
Definition: button.cpp:829
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:291
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mMousePressed
Definition: button.h:340
ImageCollection * mVertexes2
Definition: button.h:297
bool mPressed
Definition: button.h:343
int mTag
Definition: button.h:321
Color mEnabledColor2
Definition: button.h:299
Color mPressedColor2
Definition: button.h:305
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int mXOffset
Definition: button.h:323
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void init()
Definition: button.cpp:346
std::string mDescription
Definition: button.h:293

◆ Button() [2/5]

Button::Button ( const Widget2 *const  widget,
const std::string &  caption,
const std::string &  actionEventId,
ActionListener *const  listener 
)

Constructor, sets the caption of the button to the given string and adds the given action listener.

Definition at line 143 of file button.cpp.

References Widget::addActionListener(), adjustSize(), init(), and Widget::setActionEventId().

146  :
147  Widget(widget),
148  MouseListener(),
149  KeyListener(),
150  FocusListener(),
151  WidgetListener(),
152  mCaption(caption),
153  mDescription(),
154  mTextChunk(),
160  255U)),
166  mImages(nullptr),
167  mImageSet(nullptr),
169  mClickCount(0),
170  mSpacing(),
171  mTag(0),
172  mMode(0),
173  mXOffset(0),
174  mYOffset(0),
175  mImageWidth(0),
176  mImageHeight(0),
177  mHasMouse(false),
178  mKeyPressed(false),
179  mMousePressed(false),
180  mStick(false),
181  mPressed(false),
182  mTextChanged(true)
183 {
184  init();
185  adjustSize();
186  setActionEventId(actionEventId);
187 
188  if (listener != nullptr)
189  addActionListener(listener);
190 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Image ** mImages
Definition: button.h:306
bool mStick
Definition: button.h:342
Color mDisabledColor2
Definition: button.h:301
bool mKeyPressed
Definition: button.h:335
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
TextChunk mTextChunk
Definition: button.h:295
Color mHighlightedColor
Definition: button.h:302
int mMode
Definition: button.h:322
ImageSet * mImageSet
Definition: button.h:307
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
void adjustSize()
Definition: button.cpp:829
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:291
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mMousePressed
Definition: button.h:340
ImageCollection * mVertexes2
Definition: button.h:297
bool mPressed
Definition: button.h:343
int mTag
Definition: button.h:321
Color mEnabledColor2
Definition: button.h:299
Color mPressedColor2
Definition: button.h:305
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int mXOffset
Definition: button.h:323
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void init()
Definition: button.cpp:346
std::string mDescription
Definition: button.h:293

◆ Button() [3/5]

Button::Button ( const Widget2 *const  widget,
const std::string &  caption,
const std::string &  imageName,
const int  imageWidth,
const int  imageHeight,
const std::string &  actionEventId,
ActionListener *const  listener 
)

Constructor, sets the caption of the button to the given string and adds the given action listener.

Definition at line 192 of file button.cpp.

References Widget::addActionListener(), adjustSize(), init(), loadImageSet(), and Widget::setActionEventId().

197  :
198  Widget(widget),
199  MouseListener(),
200  KeyListener(),
201  FocusListener(),
202  WidgetListener(),
203  mCaption(caption),
204  mDescription(),
205  mTextChunk(),
211  255U)),
217  mImages(nullptr),
218  mImageSet(nullptr),
220  mClickCount(0),
221  mSpacing(),
222  mTag(0),
223  mMode(0),
224  mXOffset(0),
225  mYOffset(0),
226  mImageWidth(imageWidth),
227  mImageHeight(imageHeight),
228  mHasMouse(false),
229  mKeyPressed(false),
230  mMousePressed(false),
231  mStick(false),
232  mPressed(false),
233  mTextChanged(true)
234 {
235  init();
236  loadImageSet(imageName);
237  adjustSize();
238  setActionEventId(actionEventId);
239 
240  if (listener != nullptr)
241  addActionListener(listener);
242 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
void loadImageSet(const std::string &imageName)
Definition: button.cpp:425
Image ** mImages
Definition: button.h:306
bool mStick
Definition: button.h:342
Color mDisabledColor2
Definition: button.h:301
bool mKeyPressed
Definition: button.h:335
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
TextChunk mTextChunk
Definition: button.h:295
Color mHighlightedColor
Definition: button.h:302
int mMode
Definition: button.h:322
ImageSet * mImageSet
Definition: button.h:307
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
void adjustSize()
Definition: button.cpp:829
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:291
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mMousePressed
Definition: button.h:340
ImageCollection * mVertexes2
Definition: button.h:297
bool mPressed
Definition: button.h:343
int mTag
Definition: button.h:321
Color mEnabledColor2
Definition: button.h:299
Color mPressedColor2
Definition: button.h:305
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int mXOffset
Definition: button.h:323
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void init()
Definition: button.cpp:346
std::string mDescription
Definition: button.h:293

◆ Button() [4/5]

Button::Button ( const Widget2 *const  widget,
const std::string &  imageName,
const int  imageWidth,
const int  imageHeight,
const std::string &  actionEventId,
ActionListener *const  listener 
)

Constructor, sets the caption of the button to the given string and adds the given action listener.

Definition at line 244 of file button.cpp.

References Widget::addActionListener(), adjustSize(), init(), loadImageSet(), and Widget::setActionEventId().

248  :
249  Widget(widget),
250  MouseListener(),
251  KeyListener(),
252  FocusListener(),
253  WidgetListener(),
254  mCaption(),
255  mDescription(),
256  mTextChunk(),
262  255U)),
268  mImages(nullptr),
269  mImageSet(nullptr),
271  mClickCount(0),
272  mSpacing(),
273  mTag(0),
274  mMode(0),
275  mXOffset(0),
276  mYOffset(0),
277  mImageWidth(imageWidth),
278  mImageHeight(imageHeight),
279  mHasMouse(false),
280  mKeyPressed(false),
281  mMousePressed(false),
282  mStick(false),
283  mPressed(false),
284  mTextChanged(true)
285 {
286  init();
287  loadImageSet(imageName);
288  adjustSize();
289  setActionEventId(actionEventId);
290 
291  if (listener != nullptr)
292  addActionListener(listener);
293 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
void loadImageSet(const std::string &imageName)
Definition: button.cpp:425
Image ** mImages
Definition: button.h:306
bool mStick
Definition: button.h:342
Color mDisabledColor2
Definition: button.h:301
bool mKeyPressed
Definition: button.h:335
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
TextChunk mTextChunk
Definition: button.h:295
Color mHighlightedColor
Definition: button.h:302
int mMode
Definition: button.h:322
ImageSet * mImageSet
Definition: button.h:307
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
void adjustSize()
Definition: button.cpp:829
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:291
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mMousePressed
Definition: button.h:340
ImageCollection * mVertexes2
Definition: button.h:297
bool mPressed
Definition: button.h:343
int mTag
Definition: button.h:321
Color mEnabledColor2
Definition: button.h:299
Color mPressedColor2
Definition: button.h:305
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int mXOffset
Definition: button.h:323
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void init()
Definition: button.cpp:346
std::string mDescription
Definition: button.h:293

◆ Button() [5/5]

Button::Button ( const Widget2 *const  widget,
const std::string &  imageName,
const std::string &  caption,
const std::string &  actionEventId,
ActionListener *const  listener 
)

Constructor, sets the caption of the button to the given string and adds the given action listener.

Definition at line 295 of file button.cpp.

References Widget::addActionListener(), adjustSize(), init(), loadImage(), and Widget::setActionEventId().

299  :
300  Widget(widget),
301  MouseListener(),
302  KeyListener(),
303  FocusListener(),
304  WidgetListener(),
305  mCaption(caption),
306  mDescription(),
307  mTextChunk(),
313  255U)),
319  mImages(nullptr),
320  mImageSet(nullptr),
322  mClickCount(0),
323  mSpacing(),
324  mTag(0),
325  mMode(0),
326  mXOffset(0),
327  mYOffset(0),
328  mImageWidth(0),
329  mImageHeight(0),
330  mHasMouse(false),
331  mKeyPressed(false),
332  mMousePressed(false),
333  mStick(false),
334  mPressed(false),
335  mTextChanged(true)
336 {
337  init();
338  loadImage(imageName);
339  adjustSize();
340  setActionEventId(actionEventId);
341 
342  if (listener != nullptr)
343  addActionListener(listener);
344 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Image ** mImages
Definition: button.h:306
bool mStick
Definition: button.h:342
Color mDisabledColor2
Definition: button.h:301
bool mKeyPressed
Definition: button.h:335
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
TextChunk mTextChunk
Definition: button.h:295
Color mHighlightedColor
Definition: button.h:302
int mMode
Definition: button.h:322
ImageSet * mImageSet
Definition: button.h:307
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
void adjustSize()
Definition: button.cpp:829
int mClickCount
Definition: button.h:314
void loadImage(const std::string &imageName)
Definition: button.cpp:412
std::string mCaption
Definition: button.h:291
Widget(const Widget2 *const widget)
Definition: widget.cpp:84
bool mMousePressed
Definition: button.h:340
ImageCollection * mVertexes2
Definition: button.h:297
bool mPressed
Definition: button.h:343
int mTag
Definition: button.h:321
Color mEnabledColor2
Definition: button.h:299
Color mPressedColor2
Definition: button.h:305
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
int mXOffset
Definition: button.h:323
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void init()
Definition: button.cpp:346
std::string mDescription
Definition: button.h:293

◆ ~Button()

Button::~Button ( )

Destructor.

Definition at line 381 of file button.cpp.

References button, BUTTON_COUNT, Resource::decRef(), delete2, TextChunk::deleteImage(), gui, mImages, mImageSet, mInstances, mTextChunk, mVertexes2, Widget2::mWindow, Gui::removeDragged(), Widget::removeWidgetListener(), theme, and Theme::unload().

382 {
383  if (mWindow != nullptr)
385 
386  if (gui != nullptr)
387  gui->removeDragged(this);
388 
389  mInstances--;
390 
391  if (mInstances == 0 && (theme != nullptr))
392  {
393  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
394  theme->unload(button[mode]);
395  }
397  if (mImageSet != nullptr)
398  {
399  mImageSet->decRef();
400  mImageSet = nullptr;
401  }
402  if (mImages != nullptr)
403  {
404  for (int f = 0; f < BUTTON_COUNT; f ++)
405  mImages[f] = nullptr;
406  delete [] mImages;
407  mImages = nullptr;
408  }
410 }
void unload(Skin *const skin)
Definition: theme.cpp:249
Image ** mImages
Definition: button.h:306
Gui * gui
Definition: gui.cpp:110
static int mInstances
Definition: button.h:285
virtual void decRef()
Definition: resource.cpp:49
TextChunk mTextChunk
Definition: button.h:295
ImageSet * mImageSet
Definition: button.h:307
#define delete2(var)
Definition: delete2.h:24
Widget * mWindow
Definition: widget2.h:111
ImageCollection * mVertexes2
Definition: button.h:297
Theme * theme
Definition: theme.cpp:61
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1160
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306
static Skin * button[BUTTON_COUNT]
Definition: button.h:284
void deleteImage()
Definition: textchunk.cpp:192

Member Function Documentation

◆ adjustSize()

void Button::adjustSize ( )

Definition at line 829 of file button.cpp.

References button, BUTTON_STANDARD, ServerCommandType::font, Widget::getFont(), Font::getHeight(), Skin::getPadding(), Font::getWidth(), mCaption, mImageHeight, mImages, mImageWidth, mSpacing, Widget::setHeight(), and Widget::setWidth().

Referenced by Button(), ChatWindow::ChatWindow(), NpcDialog::createSkinControls(), TabStrip::createWidget(), BuyDialog::init(), EggSelectionDialog::initButtons(), InsertCardDialog::initButtons(), ItemAmountWindow::ItemAmountWindow(), SellDialog::postInit(), setPressed(), TextCommandEditor::TextCommandEditor(), and ShopWindow::updateButtonsAndLabels().

830 {
831  const Font *const font = getFont();
832  const Skin *const skin = button[BUTTON_STANDARD];
833  if (skin == nullptr)
834  return;
835  const int padding = skin->getPadding();
836 
837  if (mImages != nullptr)
838  {
839  const int spacing = mSpacing[BUTTON_STANDARD];
840  const int width = font->getWidth(mCaption);
841  if (width != 0)
842  setWidth(width + mImageWidth + spacing + 2 * padding);
843  else
844  setWidth(mImageWidth + 2 * padding);
845  int height = font->getHeight();
846  if (height < mImageHeight)
847  height = mImageHeight;
848  setHeight(height + 2 * padding);
849  }
850  else
851  {
852  setWidth(font->getWidth(mCaption) + 2 * padding);
853  setHeight(font->getHeight() + 2 * padding);
854  }
855 }
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Font * getFont() const
Definition: widget.cpp:330
Definition: skin.h:35
void setWidth(const int width)
Definition: widget.cpp:132
Image ** mImages
Definition: button.h:306
Definition: font.h:88
int mImageHeight
Definition: button.h:326
int getPadding() const
Definition: skin.h:99
std::string mCaption
Definition: button.h:291
void setHeight(const int height)
Definition: widget.cpp:139
int getWidth(const std::string &text) const
Definition: font.cpp:333
int mImageWidth
Definition: button.h:325
int getHeight() const
Definition: font.cpp:360
static Skin * button[BUTTON_COUNT]
Definition: button.h:284

◆ draw()

void Button::draw ( Graphics *const  graphics)
virtual

Draws the button.

Implements Widget.

Definition at line 473 of file button.cpp.

References BLOCK_END, BLOCK_START, button, BUTTON_DISABLED, BUTTON_HIGHLIGHTED, BUTTON_PRESSED, BUTTON_STANDARD, Graphics::calcTileCollection(), Graphics::calcWindow(), Graphics::CENTER, ImageCollection::clear(), TextChunk::color, TextChunk::color2, TextChunk::deleteImage(), Graphics::drawImage(), Graphics::drawTileCollection(), Graphics::finalize(), ServerCommandType::font, Font::generate(), Skin::getBorder(), Widget::getFont(), Font::getHeight(), Skin::getPadding(), Graphics::getRedraw(), Graphics::getTopClip(), Font::getWidth(), Rect::height, TextChunk::img, Widget::isEnabled(), isPressed(), isPressed2(), Graphics::LEFT, mAlignment, mCaption, Widget::mDimension, mDisabledColor, mDisabledColor2, mEnabledColor, mEnabledColor2, mHasMouse, mHighlightedColor, mHighlightedColor2, mImageHeight, mImages, mImageWidth, mMode, mPressedColor, mPressedColor2, Widget::mRedraw, mSpacing, mTextChanged, mTextChunk, mVertexes2, mXOffset, mYOffset, Graphics::RIGHT, TextChunk::text, TextChunk::textFont, updateAlpha(), Rect::width, ClipRect::xOffset, and ClipRect::yOffset.

474 {
475  BLOCK_START("Button::draw")
476  int mode;
477 
478  if (!isEnabled())
479  mode = BUTTON_DISABLED;
480  else if (isPressed2())
481  mode = BUTTON_PRESSED;
482  else if (mHasMouse)
483  mode = BUTTON_HIGHLIGHTED;
484  else
485  mode = BUTTON_STANDARD;
486 
487  const Skin *const skin = button[mode];
488  if (skin == nullptr)
489  {
490  BLOCK_END("Button::draw")
491  return;
492  }
493 
494  updateAlpha();
495 
496  bool recalc = false;
497  if (mRedraw)
498  {
499  recalc = true;
500  }
501  else
502  {
503  // because we don't know where parent windows was moved,
504  // need recalc vertexes
505  ClipRect &rect = graphics->getTopClip();
506  if (rect.xOffset != mXOffset || rect.yOffset != mYOffset)
507  {
508  recalc = true;
509  mXOffset = rect.xOffset;
510  mYOffset = rect.yOffset;
511  }
512  else if (mMode != mode)
513  {
514  recalc = true;
515  mMode = mode;
516  }
517  else if (graphics->getRedraw())
518  {
519  recalc = true;
520  }
521  }
522 
523  const int padding = skin->getPadding();
524  const int spacing = mSpacing[mode];
525 
526  if (recalc)
527  mTextChanged = true;
528 
529  int imageX = 0;
530  int imageY = 0;
531  int textX = 0;
532  const Rect &rect = mDimension;
533  const int width = rect.width;
534  const int height = rect.height;
535  Font *const font = getFont();
536  int textY = height / 2 - font->getHeight() / 2;
537  if (mImages != nullptr)
538  imageY = height / 2 - mImageHeight / 2;
539 
540 // need move calculation from draw!!!
541 
542  switch (mAlignment)
543  {
544  default:
545  case Graphics::LEFT:
546  {
547  if (mImages != nullptr)
548  {
549  imageX = padding;
550  textX = padding + mImageWidth + spacing;
551  }
552  else
553  {
554  textX = padding;
555  }
556  break;
557  }
558  case Graphics::CENTER:
559  {
560  const int width1 = font->getWidth(mCaption);
561  if (mImages != nullptr)
562  {
563  const int w = width1 + mImageWidth + spacing;
564  imageX = (width - w) / 2;
565  textX = imageX + mImageWidth + spacing - width1 / 2;
566  }
567  else
568  {
569  textX = (width - width1) / 2;
570  }
571  break;
572  }
573  case Graphics::RIGHT:
574  {
575  const int width1 = font->getWidth(mCaption);
576  textX = width - width1 - padding;
577  imageX = textX - width1 - spacing;
578  break;
579  }
580  }
581 
582  if (recalc)
583  {
584  mRedraw = false;
585  mMode = mode;
586  mVertexes2->clear();
587  graphics->calcWindow(mVertexes2,
588  0, 0,
589  width, height,
590  skin->getBorder());
591 
592  if (mImages != nullptr)
593  {
594  if (isPressed())
595  {
596  graphics->calcTileCollection(mVertexes2,
597  mImages[mode],
598  imageX + 1, imageY + 1);
599  }
600  else
601  {
602  graphics->calcTileCollection(mVertexes2,
603  mImages[mode],
604  imageX, imageY);
605  }
606  }
607  graphics->finalize(mVertexes2);
608  }
609  graphics->drawTileCollection(mVertexes2);
610 
611  if (isPressed())
612  {
613  textX ++;
614  textY ++;
615  }
616 
617  if (mTextChanged)
618  {
622  switch (mode)
623  {
624  case BUTTON_DISABLED:
627  break;
628  case BUTTON_PRESSED:
631  break;
632  case BUTTON_HIGHLIGHTED:
635  break;
636  default:
639  break;
640  }
641  font->generate(mTextChunk);
642  mTextChanged = false;
643  }
644 
645  const Image *const image = mTextChunk.img;
646  if (image != nullptr)
647  graphics->drawImage(image, textX, textY);
648 
649  BLOCK_END("Button::draw")
650 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Font * getFont() const
Definition: widget.cpp:330
int width
Definition: rect.h:218
std::string text
Definition: textchunk.h:64
Definition: skin.h:35
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
ClipRect & getTopClip() const
Definition: graphics.h:280
Image ** mImages
Definition: button.h:306
Definition: font.h:88
Color mDisabledColor2
Definition: button.h:301
TextChunk mTextChunk
Definition: button.h:295
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
Color mHighlightedColor
Definition: button.h:302
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
int yOffset
Definition: cliprect.h:126
int mMode
Definition: button.h:322
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
Font * textFont
Definition: textchunk.h:63
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
int xOffset
Definition: cliprect.h:121
std::string mCaption
Definition: button.h:291
bool isEnabled() const
Definition: widget.cpp:374
int height
Definition: rect.h:223
static void updateAlpha()
Definition: button.cpp:448
ImageCollection * mVertexes2
Definition: button.h:297
bool getRedraw() const
Definition: graphics.h:286
bool isPressed2() const
Definition: button.cpp:882
Color mEnabledColor2
Definition: button.h:299
Image * img
Definition: textchunk.h:62
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
bool isPressed() const
Definition: button.cpp:887
Color mPressedColor2
Definition: button.h:305
int getWidth(const std::string &text) const
Definition: font.cpp:333
int mImageWidth
Definition: button.h:325
bool mRedraw
Definition: widget.h:1163
Color mDisabledColor
Definition: button.h:300
Definition: image.h:61
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
Color color
Definition: textchunk.h:65
int getHeight() const
Definition: font.cpp:360
static Skin * button[BUTTON_COUNT]
Definition: button.h:284
void deleteImage()
Definition: textchunk.cpp:192
Color color2
Definition: textchunk.h:66
int mXOffset
Definition: button.h:323
Rect mDimension
Definition: widget.h:1100
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
virtual void calcWindow(ImageCollection *const vertCol, const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
void generate(TextChunk &chunk)
Definition: font.cpp:428

◆ focusLost()

void Button::focusLost ( const Event event)
virtual

Called when a widget loses focus.

Parameters
eventDiscribes the event.

Reimplemented from FocusListener.

Definition at line 894 of file button.cpp.

References mKeyPressed, and mMousePressed.

Referenced by getAlignment().

895 {
896  mMousePressed = false;
897  mKeyPressed = false;
898 }
bool mKeyPressed
Definition: button.h:335
bool mMousePressed
Definition: button.h:340

◆ getAlignment()

Graphics::Alignment Button::getAlignment ( ) const
inline

Gets the alignment of the caption.

Returns
The alignment of the caption.
See also
setAlignment, Graphics

Definition at line 241 of file button.h.

References focusLost(), mAlignment, mouseDragged(), mouseEntered(), mouseExited(), mousePressed(), setParent(), and setWindow().

242  { return mAlignment; }
Graphics::Alignment mAlignment
Definition: button.h:312

◆ getCaption()

const std::string& Button::getCaption ( ) const
inline

Gets the caption of the button.

Returns
The caption of the button.

Definition at line 222 of file button.h.

References mCaption.

Referenced by PopupMenu::showPopup().

223  { return mCaption; }
std::string mCaption
Definition: button.h:291

◆ getClickCount()

int Button::getClickCount ( ) const
inline

Definition at line 173 of file button.h.

References mClickCount.

Referenced by ChangeDisplay::action().

174  { return mClickCount; }
int mClickCount
Definition: button.h:314

◆ getDescription()

std::string Button::getDescription ( ) const
inline

Definition at line 170 of file button.h.

References mDescription.

Referenced by WindowMenu::mouseMoved(), InventoryWindow::mouseMoved(), and PopupMenu::showPopup().

171  { return mDescription; }
std::string mDescription
Definition: button.h:293

◆ getTag()

int Button::getTag ( ) const
inline

Definition at line 179 of file button.h.

References mTag.

Referenced by EquipmentWindow::action(), and WindowMenu::mouseMoved().

180  { return mTag; }
int mTag
Definition: button.h:321

◆ init()

void Button::init ( )
private

Definition at line 346 of file button.cpp.

References Widget::addFocusListener(), Widget::addKeyListener(), Widget::addMouseListener(), Widget::addWidgetListener(), button, BUTTON_COUNT, data, Skin::getOption(), Theme::getThemePath(), Theme::load(), Widget::mAllowLogic, mInstances, mSpacing, Widget::setFocusable(), Widget::setFrameSize(), theme, and updateAlpha().

Referenced by Button().

347 {
348  mAllowLogic = false;
349  addMouseListener(this);
350  addKeyListener(this);
351  addFocusListener(this);
352  addWidgetListener(this);
353 
354  setFocusable(true);
355  setFrameSize(0);
356 
357  if (mInstances == 0)
358  {
359  if (theme != nullptr)
360  {
361  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
362  {
363  Skin *const skin = theme->load(data[mode],
364  "button.xml",
365  true,
367  if (skin != nullptr)
368  {
369  button[mode] = skin;
370  mSpacing[mode] = skin->getOption("spacing");
371  }
372  }
373  }
374 
375  updateAlpha();
376  }
377 
378  mInstances++;
379 }
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Definition: skin.h:35
static int mInstances
Definition: button.h:285
void addFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:281
static std::string getThemePath()
Definition: theme.h:66
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
int getOption(const std::string &name) const
Definition: skin.h:105
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:167
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
static void updateAlpha()
Definition: button.cpp:448
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
Theme * theme
Definition: theme.cpp:61
void setFocusable(const bool focusable)
Definition: widget.cpp:191
static Skin * button[BUTTON_COUNT]
Definition: button.h:284
static std::string const data[Button::BUTTON_COUNT]
Definition: button.cpp:91

◆ isPressed()

bool Button::isPressed ( ) const
private

Checks if the button is pressed. Convenient method to use when overloading the draw method of the button.

Returns
True if the button is pressed, false otherwise.

Definition at line 887 of file button.cpp.

References mHasMouse, mKeyPressed, and mMousePressed.

Referenced by draw(), isPressed2(), and safeDraw().

888 {
889  if (mMousePressed)
890  return mHasMouse;
891  return mKeyPressed;
892 }
bool mKeyPressed
Definition: button.h:335
bool mMousePressed
Definition: button.h:340
bool mHasMouse
Definition: button.h:330

◆ isPressed2()

bool Button::isPressed2 ( ) const

Definition at line 882 of file button.cpp.

References isPressed(), and mPressed.

Referenced by TabStrip::action(), draw(), TabStrip::nextTab(), TabStrip::prevTab(), safeDraw(), and setPressed().

883 {
884  return mPressed || isPressed();
885 }
bool mPressed
Definition: button.h:343
bool isPressed() const
Definition: button.cpp:887

◆ keyPressed()

void Button::keyPressed ( KeyEvent event)
virtual

Called if a key is pressed when the widget has keyboard focus. If a key is held down the widget will generate multiple key presses.

Parameters
eventDiscribes the event.

Reimplemented from KeyListener.

Definition at line 857 of file button.cpp.

References InputAction::GUI_SELECT, and mKeyPressed.

Referenced by setPressed().

858 {
859  const InputActionT action = event.getActionId();
860 
861  if (action == InputAction::GUI_SELECT)
862  {
863  mKeyPressed = true;
864  event.consume();
865  }
866 }
bool mKeyPressed
Definition: button.h:335
InputAction ::T InputActionT
Definition: inputaction.h:714

◆ keyReleased()

void Button::keyReleased ( KeyEvent event)
virtual

Called if a key is released when the widget has keyboard focus.

Parameters
eventDiscribes the event.

Reimplemented from KeyListener.

Definition at line 868 of file button.cpp.

References Widget::distributeActionEvent(), InputAction::GUI_SELECT, mKeyPressed, mPressed, and mStick.

Referenced by setPressed().

869 {
870  const InputActionT action = event.getActionId();
871 
872  if (action == InputAction::GUI_SELECT && mKeyPressed)
873  {
874  mKeyPressed = false;
875  if (mStick)
876  mPressed = !mPressed;
878  event.consume();
879  }
880 }
bool mStick
Definition: button.h:342
bool mKeyPressed
Definition: button.h:335
void distributeActionEvent()
Definition: widget.cpp:492
InputAction ::T InputActionT
Definition: inputaction.h:714
bool mPressed
Definition: button.h:343

◆ loadImage()

void Button::loadImage ( const std::string &  imageName)

Definition at line 412 of file button.cpp.

References BUTTON_COUNT, Resource::decRef(), Theme::getImageFromThemeXml(), mImages, and mImageSet.

Referenced by Button(), and setPressed().

413 {
414  if (mImageSet != nullptr)
415  {
416  mImageSet->decRef();
417  mImageSet = nullptr;
418  }
419  Image *const image = Theme::getImageFromThemeXml(imageName, "");
420  mImages = new Image*[BUTTON_COUNT];
421  for (int f = 0; f < BUTTON_COUNT; f ++)
422  mImages[f] = image;
423 }
Image ** mImages
Definition: button.h:306
virtual void decRef()
Definition: resource.cpp:49
static Image * getImageFromThemeXml(const std::string &name, const std::string &name2)
Definition: theme.cpp:1135
ImageSet * mImageSet
Definition: button.h:307
Definition: image.h:61

◆ loadImageSet()

void Button::loadImageSet ( const std::string &  imageName)

Definition at line 425 of file button.cpp.

References BUTTON_COUNT, Resource::decRef(), ImageSet::get(), Theme::getImageSetFromTheme(), mImageHeight, mImages, mImageSet, and mImageWidth.

Referenced by Button(), NpcDialog::createSkinControls(), and setPressed().

426 {
427  if (mImageSet != nullptr)
428  {
429  mImageSet->decRef();
430  mImageSet = nullptr;
431  }
434  if (mImageSet == nullptr)
435  return;
436  mImages = new Image*[BUTTON_COUNT];
437  mImages[0] = nullptr;
438  for (size_t f = 0; f < BUTTON_COUNT; f ++)
439  {
440  Image *const img = mImageSet->get(f);
441  if (img != nullptr)
442  mImages[f] = img;
443  else
444  mImages[f] = mImages[0];
445  }
446 }
Image * get(const size_type i) const
Definition: imageset.cpp:66
Image ** mImages
Definition: button.h:306
virtual void decRef()
Definition: resource.cpp:49
ImageSet * mImageSet
Definition: button.h:307
int mImageHeight
Definition: button.h:326
static ImageSet * getImageSetFromTheme(const std::string &path, const int w, const int h)
Definition: theme.cpp:659
int mImageWidth
Definition: button.h:325
Definition: image.h:61

◆ mouseDragged()

void Button::mouseDragged ( MouseEvent event)
virtual

Called when the mouse has moved and the mouse has previously been pressed on the widget.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 919 of file button.cpp.

Referenced by getAlignment().

920 {
921  event.consume();
922 }

◆ mouseEntered()

void Button::mouseEntered ( MouseEvent event)
virtual

Called when the mouse has entered into the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 909 of file button.cpp.

References mHasMouse.

Referenced by getAlignment().

910 {
911  mHasMouse = true;
912 }
bool mHasMouse
Definition: button.h:330

◆ mouseExited()

void Button::mouseExited ( MouseEvent event)
virtual

Called when the mouse has exited the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 914 of file button.cpp.

References mHasMouse.

Referenced by getAlignment().

915 {
916  mHasMouse = false;
917 }
bool mHasMouse
Definition: button.h:330

◆ mousePressed()

void Button::mousePressed ( MouseEvent event)
virtual

Called when a mouse button has been pressed on the widget area.

NOTE: A mouse press is NOT equal to a mouse click. Use mouseClickMessage to check for mouse clicks.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 900 of file button.cpp.

References MouseEvent::getButton(), MouseButton::LEFT, and mMousePressed.

Referenced by getAlignment().

901 {
902  if (event.getButton() == MouseButton::LEFT)
903  {
904  mMousePressed = true;
905  event.consume();
906  }
907 }
MouseButtonT getButton() const
Definition: mouseevent.h:115
bool mMousePressed
Definition: button.h:340

◆ mouseReleased()

void Button::mouseReleased ( MouseEvent event)
virtual

Called when a mouse button has been released on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 788 of file button.cpp.

References Widget::distributeActionEvent(), MouseEvent::getButton(), MouseButton::LEFT, mClickCount, mHasMouse, mMousePressed, mPressed, and mStick.

789 {
790  if (event.getButton() == MouseButton::LEFT)
791  {
792  if (mStick)
793  mPressed = !mPressed;
794 
795  if (mMousePressed && mHasMouse)
796  {
797  mMousePressed = false;
798  mClickCount = event.getClickCount();
800  }
801  else
802  {
803  mMousePressed = false;
804  mClickCount = 0;
805  }
806  event.consume();
807  }
808 }
bool mStick
Definition: button.h:342
MouseButtonT getButton() const
Definition: mouseevent.h:115
void distributeActionEvent()
Definition: widget.cpp:492
int mClickCount
Definition: button.h:314
bool mMousePressed
Definition: button.h:340
bool mPressed
Definition: button.h:343
bool mHasMouse
Definition: button.h:330

◆ safeDraw()

void Button::safeDraw ( Graphics *const  graphics)
virtual

Implements Widget.

Definition at line 652 of file button.cpp.

References BLOCK_END, BLOCK_START, button, BUTTON_DISABLED, BUTTON_HIGHLIGHTED, BUTTON_PRESSED, BUTTON_STANDARD, Graphics::CENTER, TextChunk::color, TextChunk::color2, TextChunk::deleteImage(), Graphics::drawImage(), Graphics::drawImageRect(), ServerCommandType::font, Font::generate(), Skin::getBorder(), Widget::getFont(), Font::getHeight(), Skin::getPadding(), Font::getWidth(), Rect::height, TextChunk::img, Widget::isEnabled(), Widget::isFocused(), isPressed(), isPressed2(), Graphics::LEFT, mAlignment, mCaption, Widget::mDimension, mDisabledColor, mDisabledColor2, mEnabledColor, mEnabledColor2, mHasMouse, mHighlightedColor, mHighlightedColor2, mImageHeight, mImages, mImageWidth, mMode, mPressedColor, mPressedColor2, mSpacing, mTextChanged, mTextChunk, Graphics::RIGHT, TextChunk::text, TextChunk::textFont, updateAlpha(), and Rect::width.

653 {
654  BLOCK_START("Button::safeDraw")
655  int mode;
656 
657  if (!isEnabled())
658  mode = BUTTON_DISABLED;
659  else if (isPressed2())
660  mode = BUTTON_PRESSED;
661  else if (mHasMouse || isFocused())
662  mode = BUTTON_HIGHLIGHTED;
663  else
664  mode = BUTTON_STANDARD;
665 
666  const Skin *const skin = button[mode];
667  if (skin == nullptr)
668  {
669  BLOCK_END("Button::safeDraw")
670  return;
671  }
672 
673  updateAlpha();
674 
675  if (mMode != mode)
676  {
677  mTextChanged = true;
678  mMode = mode;
679  }
680 
681  const int padding = skin->getPadding();
682  const int spacing = mSpacing[mode];
683 
684  int imageX = 0;
685  int imageY = 0;
686  int textX = 0;
687  const Rect &rect = mDimension;
688  const int width = rect.width;
689  const int height = rect.height;
690  Font *const font = getFont();
691  int textY = height / 2 - font->getHeight() / 2;
692  if (mImages != nullptr)
693  imageY = height / 2 - mImageHeight / 2;
694 
695 // need move calculation from draw!!!
696 
697  switch (mAlignment)
698  {
699  default:
700  case Graphics::LEFT:
701  {
702  if (mImages != nullptr)
703  {
704  imageX = padding;
705  textX = padding + mImageWidth + spacing;
706  }
707  else
708  {
709  textX = padding;
710  }
711  break;
712  }
713  case Graphics::CENTER:
714  {
715  const int width1 = font->getWidth(mCaption);
716  if (mImages != nullptr)
717  {
718  const int w = width1 + mImageWidth + spacing;
719  imageX = (width - w) / 2;
720  textX = imageX + mImageWidth + spacing - width1 / 2;
721  }
722  else
723  {
724  textX = (width - width1) / 2;
725  }
726  break;
727  }
728  case Graphics::RIGHT:
729  {
730  const int width1 = font->getWidth(mCaption);
731  textX = width - width1 - padding;
732  imageX = textX - width1 - spacing;
733  break;
734  }
735  }
736 
737  graphics->drawImageRect(0, 0, width, height, skin->getBorder());
738 
739  if (mImages != nullptr)
740  {
741  if (isPressed())
742  graphics->drawImage(mImages[mode], imageX + 1, imageY + 1);
743  else
744  graphics->drawImage(mImages[mode], imageX, imageY);
745  }
746 
747  if (isPressed())
748  {
749  textX ++;
750  textY ++;
751  }
752 
753  if (mTextChanged)
754  {
758  switch (mode)
759  {
760  case BUTTON_DISABLED:
763  break;
764  case BUTTON_PRESSED:
767  break;
768  case BUTTON_HIGHLIGHTED:
771  break;
772  default:
775  break;
776  }
777  font->generate(mTextChunk);
778  mTextChanged = false;
779  }
780 
781  const Image *const image = mTextChunk.img;
782  if (image != nullptr)
783  graphics->drawImage(image, textX, textY);
784 
785  BLOCK_END("Button::safeDraw")
786 }
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Font * getFont() const
Definition: widget.cpp:330
int width
Definition: rect.h:218
std::string text
Definition: textchunk.h:64
Definition: skin.h:35
Image ** mImages
Definition: button.h:306
Definition: font.h:88
Color mDisabledColor2
Definition: button.h:301
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
TextChunk mTextChunk
Definition: button.h:295
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
Color mHighlightedColor
Definition: button.h:302
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:79
int mMode
Definition: button.h:322
Color mEnabledColor
Definition: button.h:298
Color mPressedColor
Definition: button.h:304
int mImageHeight
Definition: button.h:326
Graphics::Alignment mAlignment
Definition: button.h:312
Font * textFont
Definition: textchunk.h:63
virtual bool isFocused() const
Definition: widget.cpp:183
std::string mCaption
Definition: button.h:291
bool isEnabled() const
Definition: widget.cpp:374
int height
Definition: rect.h:223
static void updateAlpha()
Definition: button.cpp:448
bool isPressed2() const
Definition: button.cpp:882
Color mEnabledColor2
Definition: button.h:299
Image * img
Definition: textchunk.h:62
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
bool isPressed() const
Definition: button.cpp:887
Color mPressedColor2
Definition: button.h:305
int getWidth(const std::string &text) const
Definition: font.cpp:333
int mImageWidth
Definition: button.h:325
Color mDisabledColor
Definition: button.h:300
Definition: image.h:61
Color color
Definition: textchunk.h:65
int getHeight() const
Definition: font.cpp:360
static Skin * button[BUTTON_COUNT]
Definition: button.h:284
void deleteImage()
Definition: textchunk.cpp:192
Color color2
Definition: textchunk.h:66
Rect mDimension
Definition: widget.h:1100
Color mHighlightedColor2
Definition: button.h:303
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
void generate(TextChunk &chunk)
Definition: font.cpp:428

◆ setAlignment()

void Button::setAlignment ( Graphics::Alignment  alignment)
inline

Sets the alignment of the caption. The alignment is relative to the center of the button.

Parameters
alignmentThe alignment of the caption.
See also
getAlignment, Graphics

Definition at line 232 of file button.h.

References mAlignment.

233  { mAlignment = alignment; }
Graphics::Alignment mAlignment
Definition: button.h:312

◆ setCaption()

void Button::setCaption ( const std::string &  caption)
inline

Sets the caption of the button. It's advisable to call adjustSize after setting of the caption to adjust the button's size to fit the caption.

Parameters
captionThe caption of the button.
See also
getCaption, adjustSize

Definition at line 214 of file button.h.

References mCaption, and mTextChanged.

Referenced by Setup_Joystick::action(), SkillDialog::action(), NpcDialog::buildLayout(), NpcDialog::createSkinControls(), TabStrip::createWidget(), EggSelectionDialog::initButtons(), InsertCardDialog::initButtons(), TradeWindow::setStatus(), InventoryWindow::updateButtons(), ShopWindow::updateButtonsAndLabels(), InventoryWindow::updateDropButton(), CharSelectDialog::updateState(), and SkillDialog::updateTabSelection().

215  { mCaption = caption; mTextChanged = true; }
std::string mCaption
Definition: button.h:291
bool mTextChanged
Definition: button.h:344

◆ setDescription()

void Button::setDescription ( const std::string &  text)
inline

Definition at line 167 of file button.h.

References mDescription.

Referenced by WindowMenu::addButton(), and InventoryWindow::InventoryWindow().

168  { mDescription = text; }
std::string mDescription
Definition: button.h:293

◆ setImageHeight()

void Button::setImageHeight ( const int  height)
inline

Definition at line 261 of file button.h.

References mImageHeight.

Referenced by NpcDialog::createSkinControls().

262  { mImageHeight = height; }
int mImageHeight
Definition: button.h:326

◆ setImageWidth()

void Button::setImageWidth ( const int  width)
inline

Definition at line 258 of file button.h.

References mImageWidth.

Referenced by NpcDialog::createSkinControls().

259  { mImageWidth = width; }
int mImageWidth
Definition: button.h:325

◆ setParent()

void Button::setParent ( Widget parent)
virtual

Sets the parent of the widget. A parent must be a BasicContainer.

WARNING: This function is used internally and should not be called or overloaded unless you know what you are doing.

Parameters
parentThe parent of the widget.
See also
getParent

Reimplemented from Widget.

Definition at line 924 of file button.cpp.

References Widget::addWidgetListener(), Widget2::mWindow, and Widget::setParent().

Referenced by getAlignment().

925 {
926  if (mWindow != nullptr)
927  mWindow->addWidgetListener(this);
928  Widget::setParent(widget);
929 }
Widget * mWindow
Definition: widget2.h:111
virtual void setParent(Widget *parent)
Definition: widget.h:625
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301

◆ setPressed()

void Button::setPressed ( bool  b)
inline

◆ setStick()

void Button::setStick ( bool  b)
inline

Definition at line 182 of file button.h.

References mStick.

Referenced by TabStrip::createWidget().

183  { mStick = b; }
bool mStick
Definition: button.h:342

◆ setTag()

void Button::setTag ( int  tag)
inline

Definition at line 176 of file button.h.

References mTag.

Referenced by WindowMenu::addButton(), and TabStrip::createWidget().

177  { mTag = tag; }
int mTag
Definition: button.h:321

◆ setWindow()

void Button::setWindow ( Widget *const  widget)
virtual

Reimplemented from Widget2.

Definition at line 931 of file button.cpp.

References Widget2::mWindow, Widget::removeWidgetListener(), and Widget2::setWindow().

Referenced by getAlignment().

932 {
933  if ((widget == nullptr) && (mWindow != nullptr))
934  {
936  mWindow = nullptr;
937  }
938  else
939  {
940  Widget2::setWindow(widget);
941  }
942 }
virtual void setWindow(Widget *const window)
Definition: widget2.h:96
Widget * mWindow
Definition: widget2.h:111
void removeWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:306

◆ updateAlpha()

void Button::updateAlpha ( )
static

Update the alpha value to the button components.

Definition at line 448 of file button.cpp.

References button, BUTTON_COUNT, Skin::getBorder(), Theme::getMinimumOpacity(), ImageRect::grid, Settings::guiAlpha, mAlpha, Image::setAlpha(), settings, and theme.

Referenced by draw(), init(), safeDraw(), and SliderList::updateAlpha().

449 {
450  const float alpha = std::max(settings.guiAlpha,
452 
453  if (mAlpha != alpha)
454  {
455  mAlpha = alpha;
456  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
457  {
458  for (int a = 0; a < 9; a ++)
459  {
460  Skin *const skin = button[mode];
461  if (skin != nullptr)
462  {
463  const ImageRect &rect = skin->getBorder();
464  Image *const image = rect.grid[a];
465  if (image != nullptr)
466  image->setAlpha(mAlpha);
467  }
468  }
469  }
470  }
471 }
Definition: skin.h:35
float getMinimumOpacity() const
Definition: theme.h:123
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
const ImageRect & getBorder() const
Definition: skin.h:67
Image * grid[9]
Definition: imagerect.h:41
Theme * theme
Definition: theme.cpp:61
Definition: image.h:61
static float mAlpha
Definition: button.h:286
static Skin * button[BUTTON_COUNT]
Definition: button.h:284
float guiAlpha
Definition: settings.h:129

◆ widgetHidden()

void Button::widgetHidden ( const Event event)
virtual

Invoked when a widget is hidden, i.e it's set to be not visible.

Parameters
eventDescribes the event.

Reimplemented from WidgetListener.

Definition at line 820 of file button.cpp.

References ImageCollection::clear(), TextChunk::deleteImage(), isBatchDrawRenders, Widget::mRedraw, mTextChanged, mTextChunk, mVertexes2, and openGLMode.

Referenced by setPressed().

821 {
823  mVertexes2->clear();
825  mRedraw = true;
826  mTextChanged = true;
827 }
TextChunk mTextChunk
Definition: button.h:295
RenderType openGLMode
ImageCollection * mVertexes2
Definition: button.h:297
bool mRedraw
Definition: widget.h:1163
void deleteImage()
Definition: textchunk.cpp:192
#define isBatchDrawRenders(val)
Definition: renderers.h:30
bool mTextChanged
Definition: button.h:344

◆ widgetMoved()

void Button::widgetMoved ( const Event event)
virtual

Invoked when a widget is moved.

Parameters
eventDescribes the event.

Reimplemented from WidgetListener.

Definition at line 815 of file button.cpp.

References Widget::mRedraw.

Referenced by setPressed().

816 {
817  mRedraw = true;
818 }
bool mRedraw
Definition: widget.h:1163

◆ widgetResized()

void Button::widgetResized ( const Event event)
virtual

Invoked when a widget changes its size.

Parameters
eventDescribes the event.

Reimplemented from WidgetListener.

Definition at line 810 of file button.cpp.

References Widget::mRedraw.

Referenced by setPressed().

811 {
812  mRedraw = true;
813 }
bool mRedraw
Definition: widget.h:1163

Field Documentation

◆ button

Skin * Button::button
staticprivate

Button state graphics

Definition at line 284 of file button.h.

Referenced by adjustSize(), draw(), init(), safeDraw(), updateAlpha(), and ~Button().

◆ mAlignment

Graphics::Alignment Button::mAlignment
private

Holds the alignment of the caption.

Definition at line 312 of file button.h.

Referenced by draw(), getAlignment(), safeDraw(), and setAlignment().

◆ mAlpha

float Button::mAlpha = 1.0
staticprivate

Definition at line 286 of file button.h.

Referenced by updateAlpha().

◆ mCaption

std::string Button::mCaption
private

Holds the caption of the button.

Definition at line 291 of file button.h.

Referenced by adjustSize(), draw(), getCaption(), safeDraw(), and setCaption().

◆ mClickCount

int Button::mClickCount
private

Definition at line 314 of file button.h.

Referenced by getClickCount(), and mouseReleased().

◆ mDescription

std::string Button::mDescription
private

Definition at line 293 of file button.h.

Referenced by getDescription(), and setDescription().

◆ mDisabledColor

Color Button::mDisabledColor
private

Definition at line 300 of file button.h.

Referenced by draw(), and safeDraw().

◆ mDisabledColor2

Color Button::mDisabledColor2
private

Definition at line 301 of file button.h.

Referenced by draw(), and safeDraw().

◆ mEnabledColor

Color Button::mEnabledColor
private

Definition at line 298 of file button.h.

Referenced by draw(), and safeDraw().

◆ mEnabledColor2

Color Button::mEnabledColor2
private

Definition at line 299 of file button.h.

Referenced by draw(), and safeDraw().

◆ mHasMouse

bool Button::mHasMouse
private

True if the mouse is ontop of the button, false otherwise.

Definition at line 330 of file button.h.

Referenced by draw(), isPressed(), mouseEntered(), mouseExited(), mouseReleased(), and safeDraw().

◆ mHighlightedColor

Color Button::mHighlightedColor
private

Definition at line 302 of file button.h.

Referenced by draw(), and safeDraw().

◆ mHighlightedColor2

Color Button::mHighlightedColor2
private

Definition at line 303 of file button.h.

Referenced by draw(), and safeDraw().

◆ mImageHeight

int Button::mImageHeight
private

Definition at line 326 of file button.h.

Referenced by adjustSize(), draw(), loadImageSet(), safeDraw(), and setImageHeight().

◆ mImages

Image** Button::mImages
private

Definition at line 306 of file button.h.

Referenced by adjustSize(), draw(), loadImage(), loadImageSet(), safeDraw(), and ~Button().

◆ mImageSet

ImageSet* Button::mImageSet
private

Definition at line 307 of file button.h.

Referenced by loadImage(), loadImageSet(), and ~Button().

◆ mImageWidth

int Button::mImageWidth
private

Definition at line 325 of file button.h.

Referenced by adjustSize(), draw(), loadImageSet(), safeDraw(), and setImageWidth().

◆ mInstances

int Button::mInstances = 0
staticprivate

Number of button instances

Definition at line 285 of file button.h.

Referenced by init(), and ~Button().

◆ mKeyPressed

bool Button::mKeyPressed
private

True if a key has been pressed, false otherwise.

Definition at line 335 of file button.h.

Referenced by focusLost(), isPressed(), keyPressed(), and keyReleased().

◆ mMode

int Button::mMode
private

Definition at line 322 of file button.h.

Referenced by draw(), and safeDraw().

◆ mMousePressed

bool Button::mMousePressed
private

True if a mouse has been pressed, false otherwise.

Definition at line 340 of file button.h.

Referenced by focusLost(), isPressed(), mousePressed(), and mouseReleased().

◆ mPressed

bool Button::mPressed
private

Definition at line 343 of file button.h.

Referenced by isPressed2(), keyReleased(), mouseReleased(), and setPressed().

◆ mPressedColor

Color Button::mPressedColor
private

Definition at line 304 of file button.h.

Referenced by draw(), and safeDraw().

◆ mPressedColor2

Color Button::mPressedColor2
private

Definition at line 305 of file button.h.

Referenced by draw(), and safeDraw().

◆ mSpacing

int Button::mSpacing[BUTTON_COUNT]
private

Holds the spacing between the border and the caption.

Definition at line 319 of file button.h.

Referenced by adjustSize(), draw(), init(), and safeDraw().

◆ mStick

bool Button::mStick
private

Definition at line 342 of file button.h.

Referenced by keyReleased(), mouseReleased(), and setStick().

◆ mTag

int Button::mTag
private

Definition at line 321 of file button.h.

Referenced by getTag(), and setTag().

◆ mTextChanged

bool Button::mTextChanged
private

Definition at line 344 of file button.h.

Referenced by draw(), safeDraw(), setCaption(), and widgetHidden().

◆ mTextChunk

TextChunk Button::mTextChunk
private

Definition at line 295 of file button.h.

Referenced by draw(), safeDraw(), widgetHidden(), and ~Button().

◆ mVertexes2

ImageCollection* Button::mVertexes2
private

Definition at line 297 of file button.h.

Referenced by draw(), widgetHidden(), and ~Button().

◆ mXOffset

int Button::mXOffset
private

Definition at line 323 of file button.h.

Referenced by draw().

◆ mYOffset

int Button::mYOffset
private

Definition at line 324 of file button.h.

Referenced by draw().


The documentation for this class was generated from the following files: