ManaPlus
Public Types | 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, const std::string &skinName)
 
 Button (const Widget2 *const widget, const std::string &caption, const std::string &actionEventId, const std::string &skinName, 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, const std::string &skinName, ActionListener *const listener)
 
 Button (const Widget2 *const widget, const std::string &imageName, const int imageWidth, const int imageHeight, const std::string &actionEventId, const std::string &skinName, ActionListener *const listener)
 
 ~Button ()
 
void draw (Graphics *const graphics)
 
void safeDraw (Graphics *const graphics)
 
void updateAlpha ()
 
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 setImage (Image *const image)
 
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)
 

Private Member Functions

bool isPressed () const
 
void init ()
 

Private Attributes

SkinmSkin [BUTTON_COUNT]
 
std::string mCaption
 
std::string mDescription
 
std::string mSkinName
 
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 float mAlpha = 1.0
 

Additional Inherited Members

- 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)
 
- 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 96 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 263 of file button.h.

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

Constructor & Destructor Documentation

◆ Button() [1/4]

Button::Button ( const Widget2 *const  widget,
const std::string &  skinName 
)

Default constructor.

Definition at line 98 of file button.cpp.

References adjustSize(), and init().

99  :
100  Widget(widget),
101  MouseListener(),
102  KeyListener(),
103  FocusListener(),
104  WidgetListener(),
105  mSkin(),
106  mCaption(),
107  mDescription(),
108  mSkinName(skinName),
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:798
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:289
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
std::string mSkinName
Definition: button.h:293
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
void init()
Definition: button.cpp:306
std::string mDescription
Definition: button.h:291

◆ Button() [2/4]

Button::Button ( const Widget2 *const  widget,
const std::string &  caption,
const std::string &  actionEventId,
const std::string &  skinName,
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().

147  :
148  Widget(widget),
149  MouseListener(),
150  KeyListener(),
151  FocusListener(),
152  WidgetListener(),
153  mSkin(),
154  mCaption(caption),
155  mDescription(),
156  mSkinName(skinName),
157  mTextChunk(),
163  255U)),
169  mImages(nullptr),
170  mImageSet(nullptr),
172  mClickCount(0),
173  mSpacing(),
174  mTag(0),
175  mMode(0),
176  mXOffset(0),
177  mYOffset(0),
178  mImageWidth(0),
179  mImageHeight(0),
180  mHasMouse(false),
181  mKeyPressed(false),
182  mMousePressed(false),
183  mStick(false),
184  mPressed(false),
185  mTextChanged(true)
186 {
187  init();
188  adjustSize();
189  setActionEventId(actionEventId);
190 
191  if (listener != nullptr)
192  addActionListener(listener);
193 }
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:798
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:289
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
std::string mSkinName
Definition: button.h:293
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
void init()
Definition: button.cpp:306
std::string mDescription
Definition: button.h:291

◆ Button() [3/4]

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

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

Definition at line 195 of file button.cpp.

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

202  :
203  Widget(widget),
204  MouseListener(),
205  KeyListener(),
206  FocusListener(),
207  WidgetListener(),
208  mSkin(),
209  mCaption(caption),
210  mDescription(),
211  mSkinName(skinName),
212  mTextChunk(),
218  255U)),
224  mImages(nullptr),
225  mImageSet(nullptr),
227  mClickCount(0),
228  mSpacing(),
229  mTag(0),
230  mMode(0),
231  mXOffset(0),
232  mYOffset(0),
233  mImageWidth(imageWidth),
234  mImageHeight(imageHeight),
235  mHasMouse(false),
236  mKeyPressed(false),
237  mMousePressed(false),
238  mStick(false),
239  mPressed(false),
240  mTextChanged(true)
241 {
242  init();
243  loadImageSet(imageName);
244  adjustSize();
245  setActionEventId(actionEventId);
246 
247  if (listener != nullptr)
248  addActionListener(listener);
249 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
void loadImageSet(const std::string &imageName)
Definition: button.cpp:394
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:798
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:289
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
std::string mSkinName
Definition: button.h:293
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
void init()
Definition: button.cpp:306
std::string mDescription
Definition: button.h:291

◆ Button() [4/4]

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

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

Definition at line 251 of file button.cpp.

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

257  :
258  Widget(widget),
259  MouseListener(),
260  KeyListener(),
261  FocusListener(),
262  WidgetListener(),
263  mSkin(),
264  mCaption(),
265  mDescription(),
266  mSkinName(skinName),
267  mTextChunk(),
273  255U)),
279  mImages(nullptr),
280  mImageSet(nullptr),
282  mClickCount(0),
283  mSpacing(),
284  mTag(0),
285  mMode(0),
286  mXOffset(0),
287  mYOffset(0),
288  mImageWidth(imageWidth),
289  mImageHeight(imageHeight),
290  mHasMouse(false),
291  mKeyPressed(false),
292  mMousePressed(false),
293  mStick(false),
294  mPressed(false),
295  mTextChanged(true)
296 {
297  init();
298  loadImageSet(imageName);
299  adjustSize();
300  setActionEventId(actionEventId);
301 
302  if (listener != nullptr)
303  addActionListener(listener);
304 }
int mYOffset
Definition: button.h:324
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
void loadImageSet(const std::string &imageName)
Definition: button.cpp:394
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:798
int mClickCount
Definition: button.h:314
std::string mCaption
Definition: button.h:289
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
std::string mSkinName
Definition: button.h:293
bool mTextChanged
Definition: button.h:344
bool mHasMouse
Definition: button.h:330
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
void init()
Definition: button.cpp:306
std::string mDescription
Definition: button.h:291

◆ ~Button()

Button::~Button ( )

Destructor.

Definition at line 336 of file button.cpp.

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

337 {
338  if (mWindow != nullptr)
340 
341  if (gui != nullptr)
342  gui->removeDragged(this);
343 
344  if (theme != nullptr)
345  {
346  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
347  theme->unload(mSkin[mode]);
348  }
350  if (mImageSet != nullptr)
351  {
352  mImageSet->decRef();
353  mImageSet = nullptr;
354  }
355  if (mImages != nullptr)
356  {
357  for (int f = 0; f < BUTTON_COUNT; f ++)
358  mImages[f] = nullptr;
359  delete [] mImages;
360  mImages = nullptr;
361  }
363 }
void unload(Skin *const skin)
Definition: theme.cpp:249
Image ** mImages
Definition: button.h:306
Gui * gui
Definition: gui.cpp:110
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
void deleteImage()
Definition: textchunk.cpp:192
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285

Member Function Documentation

◆ adjustSize()

void Button::adjustSize ( )

Definition at line 798 of file button.cpp.

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

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

799 {
800  const Font *const font = getFont();
801  const Skin *const skin = mSkin[BUTTON_STANDARD];
802  if (skin == nullptr)
803  return;
804  const int padding = skin->getPadding();
805 
806  if (mImages != nullptr)
807  {
808  const int spacing = mSpacing[BUTTON_STANDARD];
809  const int width = font->getWidth(mCaption);
810  if (width != 0)
811  setWidth(width + mImageWidth + spacing + 2 * padding);
812  else
813  setWidth(mImageWidth + 2 * padding);
814  int height = font->getHeight();
815  if (height < mImageHeight)
816  height = mImageHeight;
817  setHeight(height + 2 * padding);
818  }
819  else
820  {
821  setWidth(font->getWidth(mCaption) + 2 * padding);
822  setHeight(font->getHeight() + 2 * padding);
823  }
824 }
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:289
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
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285

◆ draw()

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

Draws the button.

Implements Widget.

Definition at line 442 of file button.cpp.

References BLOCK_END, BLOCK_START, 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, mSkin, mSpacing, mTextChanged, mTextChunk, mVertexes2, mXOffset, mYOffset, Graphics::RIGHT, TextChunk::text, TextChunk::textFont, updateAlpha(), Rect::width, ClipRect::xOffset, and ClipRect::yOffset.

443 {
444  BLOCK_START("Button::draw")
445  int mode;
446 
447  if (!isEnabled())
448  mode = BUTTON_DISABLED;
449  else if (isPressed2())
450  mode = BUTTON_PRESSED;
451  else if (mHasMouse)
452  mode = BUTTON_HIGHLIGHTED;
453  else
454  mode = BUTTON_STANDARD;
455 
456  const Skin *const skin = mSkin[mode];
457  if (skin == nullptr)
458  {
459  BLOCK_END("Button::draw")
460  return;
461  }
462 
463  updateAlpha();
464 
465  bool recalc = false;
466  if (mRedraw)
467  {
468  recalc = true;
469  }
470  else
471  {
472  // because we don't know where parent windows was moved,
473  // need recalc vertexes
474  ClipRect &rect = graphics->getTopClip();
475  if (rect.xOffset != mXOffset || rect.yOffset != mYOffset)
476  {
477  recalc = true;
478  mXOffset = rect.xOffset;
479  mYOffset = rect.yOffset;
480  }
481  else if (mMode != mode)
482  {
483  recalc = true;
484  mMode = mode;
485  }
486  else if (graphics->getRedraw())
487  {
488  recalc = true;
489  }
490  }
491 
492  const int padding = skin->getPadding();
493  const int spacing = mSpacing[mode];
494 
495  if (recalc)
496  mTextChanged = true;
497 
498  int imageX = 0;
499  int imageY = 0;
500  int textX = 0;
501  const Rect &rect = mDimension;
502  const int width = rect.width;
503  const int height = rect.height;
504  Font *const font = getFont();
505  int textY = height / 2 - font->getHeight() / 2;
506  if (mImages != nullptr)
507  imageY = height / 2 - mImageHeight / 2;
508 
509 // need move calculation from draw!!!
510 
511  switch (mAlignment)
512  {
513  default:
514  case Graphics::LEFT:
515  {
516  if (mImages != nullptr)
517  {
518  imageX = padding;
519  textX = padding + mImageWidth + spacing;
520  }
521  else
522  {
523  textX = padding;
524  }
525  break;
526  }
527  case Graphics::CENTER:
528  {
529  const int width1 = font->getWidth(mCaption);
530  if (mImages != nullptr)
531  {
532  const int w = width1 + mImageWidth + spacing;
533  imageX = (width - w) / 2;
534  textX = imageX + mImageWidth + spacing - width1 / 2;
535  }
536  else
537  {
538  textX = (width - width1) / 2;
539  }
540  break;
541  }
542  case Graphics::RIGHT:
543  {
544  const int width1 = font->getWidth(mCaption);
545  textX = width - width1 - padding;
546  imageX = textX - width1 - spacing;
547  break;
548  }
549  }
550 
551  if (recalc)
552  {
553  mRedraw = false;
554  mMode = mode;
555  mVertexes2->clear();
556  graphics->calcWindow(mVertexes2,
557  0, 0,
558  width, height,
559  skin->getBorder());
560 
561  if (mImages != nullptr)
562  {
563  if (isPressed())
564  {
565  graphics->calcTileCollection(mVertexes2,
566  mImages[mode],
567  imageX + 1, imageY + 1);
568  }
569  else
570  {
571  graphics->calcTileCollection(mVertexes2,
572  mImages[mode],
573  imageX, imageY);
574  }
575  }
576  graphics->finalize(mVertexes2);
577  }
578  graphics->drawTileCollection(mVertexes2);
579 
580  if (isPressed())
581  {
582  textX ++;
583  textY ++;
584  }
585 
586  if (mTextChanged)
587  {
591  switch (mode)
592  {
593  case BUTTON_DISABLED:
596  break;
597  case BUTTON_PRESSED:
600  break;
601  case BUTTON_HIGHLIGHTED:
604  break;
605  default:
608  break;
609  }
610  font->generate(mTextChunk);
611  mTextChanged = false;
612  }
613 
614  const Image *const image = mTextChunk.img;
615  if (image != nullptr)
616  graphics->drawImage(image, textX, textY);
617 
618  BLOCK_END("Button::draw")
619 }
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:289
bool isEnabled() const
Definition: widget.cpp:374
int height
Definition: rect.h:223
void updateAlpha()
Definition: button.cpp:417
ImageCollection * mVertexes2
Definition: button.h:297
bool getRedraw() const
Definition: graphics.h:286
bool isPressed2() const
Definition: button.cpp:851
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:856
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
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
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
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 863 of file button.cpp.

References mKeyPressed, and mMousePressed.

Referenced by getAlignment().

864 {
865  mMousePressed = false;
866  mKeyPressed = false;
867 }
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 240 of file button.h.

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

241  { 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 221 of file button.h.

References mCaption.

Referenced by PopupMenu::showPopup().

222  { return mCaption; }
std::string mCaption
Definition: button.h:289

◆ getClickCount()

int Button::getClickCount ( ) const
inline

Definition at line 170 of file button.h.

References mClickCount.

Referenced by ChangeDisplay::action().

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

◆ getDescription()

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

Definition at line 167 of file button.h.

References mDescription.

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

168  { return mDescription; }
std::string mDescription
Definition: button.h:291

◆ getTag()

int Button::getTag ( ) const
inline

Definition at line 176 of file button.h.

References mTag.

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

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

◆ init()

void Button::init ( )
private

Definition at line 306 of file button.cpp.

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

Referenced by Button().

307 {
308  mAllowLogic = false;
309  addMouseListener(this);
310  addKeyListener(this);
311  addFocusListener(this);
312  addWidgetListener(this);
313 
314  setFocusable(true);
315  setFrameSize(0);
316 
317  if (theme != nullptr)
318  {
319  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
320  {
321  Skin *const skin = theme->load(mSkinName + data[mode],
322  "button.xml",
323  true,
325  if (skin != nullptr)
326  {
327  mSkin[mode] = skin;
328  mSpacing[mode] = skin->getOption("spacing");
329  }
330  }
331  }
332 
333  updateAlpha();
334 }
int mSpacing[BUTTON_COUNT]
Definition: button.h:319
Definition: skin.h:35
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
void updateAlpha()
Definition: button.cpp:417
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 std::string const data[Button::BUTTON_COUNT]
Definition: button.cpp:90
std::string mSkinName
Definition: button.h:293
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285

◆ 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 856 of file button.cpp.

References mHasMouse, mKeyPressed, and mMousePressed.

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

857 {
858  if (mMousePressed)
859  return mHasMouse;
860  return mKeyPressed;
861 }
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 851 of file button.cpp.

References isPressed(), and mPressed.

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

852 {
853  return mPressed || isPressed();
854 }
bool mPressed
Definition: button.h:343
bool isPressed() const
Definition: button.cpp:856

◆ 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 826 of file button.cpp.

References InputAction::GUI_SELECT, and mKeyPressed.

Referenced by setPressed().

827 {
828  const InputActionT action = event.getActionId();
829 
830  if (action == InputAction::GUI_SELECT)
831  {
832  mKeyPressed = true;
833  event.consume();
834  }
835 }
bool mKeyPressed
Definition: button.h:335
InputAction ::T InputActionT
Definition: inputaction.h:715

◆ 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 837 of file button.cpp.

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

Referenced by setPressed().

838 {
839  const InputActionT action = event.getActionId();
840 
841  if (action == InputAction::GUI_SELECT && mKeyPressed)
842  {
843  mKeyPressed = false;
844  if (mStick)
845  mPressed = !mPressed;
847  event.consume();
848  }
849 }
bool mStick
Definition: button.h:342
bool mKeyPressed
Definition: button.h:335
void distributeActionEvent()
Definition: widget.cpp:492
InputAction ::T InputActionT
Definition: inputaction.h:715
bool mPressed
Definition: button.h:343

◆ loadImage()

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

Definition at line 365 of file button.cpp.

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

Referenced by setPressed().

366 {
367  if (mImageSet != nullptr)
368  {
369  mImageSet->decRef();
370  mImageSet = nullptr;
371  }
372  Image *const image = Theme::getImageFromThemeXml(imageName, "");
373  mImages = new Image*[BUTTON_COUNT];
374  for (int f = 0; f < BUTTON_COUNT; f ++)
375  mImages[f] = image;
376 }
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:925
ImageSet * mImageSet
Definition: button.h:307
Definition: image.h:61

◆ loadImageSet()

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

Definition at line 394 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().

395 {
396  if (mImageSet != nullptr)
397  {
398  mImageSet->decRef();
399  mImageSet = nullptr;
400  }
403  if (mImageSet == nullptr)
404  return;
405  mImages = new Image*[BUTTON_COUNT];
406  mImages[0] = nullptr;
407  for (size_t f = 0; f < BUTTON_COUNT; f ++)
408  {
409  Image *const img = mImageSet->get(f);
410  if (img != nullptr)
411  mImages[f] = img;
412  else
413  mImages[f] = mImages[0];
414  }
415 }
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 888 of file button.cpp.

Referenced by getAlignment().

889 {
890  event.consume();
891 }

◆ 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 878 of file button.cpp.

References mHasMouse.

Referenced by getAlignment().

879 {
880  mHasMouse = true;
881 }
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 883 of file button.cpp.

References mHasMouse.

Referenced by getAlignment().

884 {
885  mHasMouse = false;
886 }
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 869 of file button.cpp.

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

Referenced by getAlignment().

870 {
871  if (event.getButton() == MouseButton::LEFT)
872  {
873  mMousePressed = true;
874  event.consume();
875  }
876 }
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 757 of file button.cpp.

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

758 {
759  if (event.getButton() == MouseButton::LEFT)
760  {
761  if (mStick)
762  mPressed = !mPressed;
763 
764  if (mMousePressed && mHasMouse)
765  {
766  mMousePressed = false;
767  mClickCount = event.getClickCount();
769  }
770  else
771  {
772  mMousePressed = false;
773  mClickCount = 0;
774  }
775  event.consume();
776  }
777 }
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 621 of file button.cpp.

References BLOCK_END, BLOCK_START, 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, mSkin, mSpacing, mTextChanged, mTextChunk, Graphics::RIGHT, TextChunk::text, TextChunk::textFont, updateAlpha(), and Rect::width.

622 {
623  BLOCK_START("Button::safeDraw")
624  int mode;
625 
626  if (!isEnabled())
627  mode = BUTTON_DISABLED;
628  else if (isPressed2())
629  mode = BUTTON_PRESSED;
630  else if (mHasMouse || isFocused())
631  mode = BUTTON_HIGHLIGHTED;
632  else
633  mode = BUTTON_STANDARD;
634 
635  const Skin *const skin = mSkin[mode];
636  if (skin == nullptr)
637  {
638  BLOCK_END("Button::safeDraw")
639  return;
640  }
641 
642  updateAlpha();
643 
644  if (mMode != mode)
645  {
646  mTextChanged = true;
647  mMode = mode;
648  }
649 
650  const int padding = skin->getPadding();
651  const int spacing = mSpacing[mode];
652 
653  int imageX = 0;
654  int imageY = 0;
655  int textX = 0;
656  const Rect &rect = mDimension;
657  const int width = rect.width;
658  const int height = rect.height;
659  Font *const font = getFont();
660  int textY = height / 2 - font->getHeight() / 2;
661  if (mImages != nullptr)
662  imageY = height / 2 - mImageHeight / 2;
663 
664 // need move calculation from draw!!!
665 
666  switch (mAlignment)
667  {
668  default:
669  case Graphics::LEFT:
670  {
671  if (mImages != nullptr)
672  {
673  imageX = padding;
674  textX = padding + mImageWidth + spacing;
675  }
676  else
677  {
678  textX = padding;
679  }
680  break;
681  }
682  case Graphics::CENTER:
683  {
684  const int width1 = font->getWidth(mCaption);
685  if (mImages != nullptr)
686  {
687  const int w = width1 + mImageWidth + spacing;
688  imageX = (width - w) / 2;
689  textX = imageX + mImageWidth + spacing - width1 / 2;
690  }
691  else
692  {
693  textX = (width - width1) / 2;
694  }
695  break;
696  }
697  case Graphics::RIGHT:
698  {
699  const int width1 = font->getWidth(mCaption);
700  textX = width - width1 - padding;
701  imageX = textX - width1 - spacing;
702  break;
703  }
704  }
705 
706  graphics->drawImageRect(0, 0, width, height, skin->getBorder());
707 
708  if (mImages != nullptr)
709  {
710  if (isPressed())
711  graphics->drawImage(mImages[mode], imageX + 1, imageY + 1);
712  else
713  graphics->drawImage(mImages[mode], imageX, imageY);
714  }
715 
716  if (isPressed())
717  {
718  textX ++;
719  textY ++;
720  }
721 
722  if (mTextChanged)
723  {
727  switch (mode)
728  {
729  case BUTTON_DISABLED:
732  break;
733  case BUTTON_PRESSED:
736  break;
737  case BUTTON_HIGHLIGHTED:
740  break;
741  default:
744  break;
745  }
746  font->generate(mTextChunk);
747  mTextChanged = false;
748  }
749 
750  const Image *const image = mTextChunk.img;
751  if (image != nullptr)
752  graphics->drawImage(image, textX, textY);
753 
754  BLOCK_END("Button::safeDraw")
755 }
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:289
bool isEnabled() const
Definition: widget.cpp:374
int height
Definition: rect.h:223
void updateAlpha()
Definition: button.cpp:417
bool isPressed2() const
Definition: button.cpp:851
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:856
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
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
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285
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 231 of file button.h.

References mAlignment.

232  { 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 213 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(), Pincode::shuffle(), InventoryWindow::updateButtons(), ShopWindow::updateButtonsAndLabels(), InventoryWindow::updateDropButton(), CharSelectDialog::updateState(), and SkillDialog::updateTabSelection().

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

◆ setDescription()

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

Definition at line 164 of file button.h.

References mDescription.

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

165  { mDescription = text; }
std::string mDescription
Definition: button.h:291

◆ setImage()

void Button::setImage ( Image *const  image)

Definition at line 378 of file button.cpp.

References BUTTON_COUNT, Resource::decRef(), Image::getHeight(), Image::getWidth(), mImageHeight, mImages, mImageSet, and mImageWidth.

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

379 {
380  if (image == nullptr)
381  return;
382  if (mImageSet != nullptr)
383  {
384  mImageSet->decRef();
385  mImageSet = nullptr;
386  }
387  mImageWidth = image->getWidth();
388  mImageHeight = image->getHeight();
389  mImages = new Image*[BUTTON_COUNT];
390  for (int f = 0; f < BUTTON_COUNT; f ++)
391  mImages[f] = image;
392 }
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
int mImageWidth
Definition: button.h:325
Definition: image.h:61
int getWidth() const A_INLINE
Definition: image.h:116
int getHeight() const A_INLINE
Definition: image.h:122

◆ setImageHeight()

void Button::setImageHeight ( const int  height)
inline

Definition at line 260 of file button.h.

References mImageHeight.

Referenced by NpcDialog::createSkinControls().

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

◆ setImageWidth()

void Button::setImageWidth ( const int  width)
inline

Definition at line 257 of file button.h.

References mImageWidth.

Referenced by NpcDialog::createSkinControls().

258  { 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 893 of file button.cpp.

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

Referenced by getAlignment().

894 {
895  if (mWindow != nullptr)
896  mWindow->addWidgetListener(this);
897  Widget::setParent(widget);
898 }
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 179 of file button.h.

References mStick.

Referenced by TabStrip::createWidget().

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

◆ setTag()

void Button::setTag ( int  tag)
inline

Definition at line 173 of file button.h.

References mTag.

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

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

◆ setWindow()

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

Reimplemented from Widget2.

Definition at line 900 of file button.cpp.

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

Referenced by getAlignment().

901 {
902  if ((widget == nullptr) && (mWindow != nullptr))
903  {
905  mWindow = nullptr;
906  }
907  else
908  {
909  Widget2::setWindow(widget);
910  }
911 }
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 ( )

Update the alpha value to the button components.

Definition at line 417 of file button.cpp.

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

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

418 {
419  const float alpha = std::max(settings.guiAlpha,
421 
422  if (mAlpha != alpha)
423  {
424  mAlpha = alpha;
425  for (int mode = 0; mode < BUTTON_COUNT; mode ++)
426  {
427  for (int a = 0; a < 9; a ++)
428  {
429  Skin *const skin = mSkin[mode];
430  if (skin != nullptr)
431  {
432  const ImageRect &rect = skin->getBorder();
433  Image *const image = rect.grid[a];
434  if (image != nullptr)
435  image->setAlpha(mAlpha);
436  }
437  }
438  }
439  }
440 }
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:286
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:283
float guiAlpha
Definition: settings.h:129
Skin * mSkin[BUTTON_COUNT]
Definition: button.h:285

◆ 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 789 of file button.cpp.

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

Referenced by setPressed().

790 {
792  mVertexes2->clear();
794  mRedraw = true;
795  mTextChanged = true;
796 }
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 784 of file button.cpp.

References Widget::mRedraw.

Referenced by setPressed().

785 {
786  mRedraw = true;
787 }
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 779 of file button.cpp.

References Widget::mRedraw.

Referenced by setPressed().

780 {
781  mRedraw = true;
782 }
bool mRedraw
Definition: widget.h:1163

Field Documentation

◆ 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 283 of file button.h.

Referenced by updateAlpha().

◆ mCaption

std::string Button::mCaption
private

Holds the caption of the button.

Definition at line 289 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 291 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(), setImage(), and setImageHeight().

◆ mImages

Image** Button::mImages
private

Definition at line 306 of file button.h.

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

◆ mImageSet

ImageSet* Button::mImageSet
private

Definition at line 307 of file button.h.

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

◆ mImageWidth

int Button::mImageWidth
private

Definition at line 325 of file button.h.

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

◆ 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().

◆ mSkin

Skin* Button::mSkin[BUTTON_COUNT]
private

Button state graphics

Definition at line 285 of file button.h.

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

◆ mSkinName

std::string Button::mSkinName
private

Definition at line 293 of file button.h.

Referenced by init().

◆ 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: