FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
twobutton.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/log/logger.h"
32 
33 #include "twobutton.h"
34 
35 namespace fcn {
36  static FIFE::Logger _log(LM_GUI);
37 
38  TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption):
39  Button(),
40  m_upImage(up_file),
41  m_downImage(down_file),
42  m_hoverImage(hover_file),
43  x_downoffset(0),
44  y_downoffset(0) {
45  addWidgetListener(this);
46 
47  m_hoverImage = hover_file;
48  setFrameSize(0);
49  adjustSize();
50  mCaption = caption;
51  }
52 
54  }
55 
56  void TwoButton::setDownOffset(int32_t x, int32_t y) {
57  x_downoffset = x;
58  y_downoffset = y;
59  }
60 
61  void TwoButton::draw(Graphics *graphics) {
62  Image* img = m_upImage;
63  int32_t xoffset = 0;
64  int32_t yoffset = 0;
65 
66  if (isPressed()) {
67  if( m_downImage ) {
68  img = m_downImage;
69  xoffset = x_downoffset;
70  yoffset = y_downoffset;
71  }
72  } else if(mHasMouse) {
73  if( m_hoverImage ) {
74  img = m_hoverImage;
75  }
76  }
77 
78  if (img) {
79  graphics->drawImage(img, xoffset, yoffset, 0, 0, getWidth(), getHeight());
80  }
81 
82  graphics->setColor(getForegroundColor());
83  int32_t textX;
84  int32_t textY = getHeight() / 2 - getFont()->getHeight() / 2;
85  switch (getAlignment())
86  {
87  case Graphics::Left:
88  textX = 4;
89  break;
90  case Graphics::Center:
91  textX = getWidth() / 2;
92  break;
93  case Graphics::Right:
94  textX = getWidth() - 4;
95  break;
96  default:
97  textX = 4;
98  FL_WARN(_log, FIFE::LMsg("TwoButton::draw() - ") << "Unknown alignment: "
99  << getAlignment() << ". Using the default of Graphics::LEFT");
100  }
101 
102  graphics->setFont(getFont());
103  if (mCaption.size() > 0) {
104  if (isPressed())
105  graphics->drawText(getCaption(), textX + 1,
106  textY + 1, getAlignment());
107  else
108  graphics->drawText(getCaption(), textX, textY, getAlignment());
109  }
110  }
112  int32_t w = 0;
113  int32_t h = w;
114  if( m_upImage ) {
115  w = m_upImage->getWidth();
116  h = m_upImage->getHeight();
117  }
118  if( m_downImage ) {
119  w = std::max(m_downImage->getWidth(), w);
120  h = std::max(m_downImage->getHeight(), h);
121  }
122  if( m_hoverImage ) {
123  w = std::max(m_hoverImage->getWidth(), w);
124  h = std::max(m_hoverImage->getHeight(), h);
125  }
126  setWidth(w);
127  setHeight(h);
128  }
129  void TwoButton::setUpImage(Image* image) {
130  m_upImage = image;
131  adjustSize();
132  }
133  void TwoButton::setDownImage(Image* image) {
134  m_downImage = image;
135  adjustSize();
136  }
137  void TwoButton::setHoverImage(Image* image) {
138  m_hoverImage = image;
139  adjustSize();
140  }
141 
142  void TwoButton::ancestorHidden(const Event& e) {
143  mHasMouse = false;
144  }
145 
146 }
147 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
148 
Definition: modules.h:41
void setDownImage(Image *image)
Definition: twobutton.cpp:133
#define FL_WARN(logger, msg)
Definition: logger.h:72
void setDownOffset(int32_t x, int32_t y)
Definition: twobutton.cpp:56
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
int32_t y_downoffset
Definition: twobutton.h:63
void draw(Graphics *graphics)
Definition: twobutton.cpp:61
static FIFE::Logger _log(LM_GUI)
TwoButton(Image *up_image=0, Image *down_image=0, Image *hover_file=0, const std::string &caption="")
Definition: twobutton.cpp:38
int32_t x_downoffset
Definition: twobutton.h:62
Image * m_upImage
Definition: twobutton.h:59
Image * m_downImage
Definition: twobutton.h:60
void setUpImage(Image *image)
Definition: twobutton.cpp:129
Image * m_hoverImage
Definition: twobutton.h:61
void setHoverImage(Image *image)
Definition: twobutton.cpp:137
Create a Logger instance to communicate with LogManager Logger stores information about the current m...
Definition: logger.h:211
void adjustSize()
Definition: twobutton.cpp:111
virtual void ancestorHidden(const Event &e)
Definition: twobutton.cpp:142