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 gcn {
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  m_hoverImage = hover_file;
46  setFrameSize(0);
47  adjustSize();
48  mCaption = caption;
49  }
50 
52  }
53 
54  void TwoButton::setDownOffset(int32_t x, int32_t y) {
55  x_downoffset = x;
56  y_downoffset = y;
57  }
58 
59  void TwoButton::draw(Graphics *graphics) {
60  Image* img = m_upImage;
61  int32_t xoffset = 0;
62  int32_t yoffset = 0;
63 
64  if (isPressed()) {
65  if( m_downImage ) {
66  img = m_downImage;
67  xoffset = x_downoffset;
68  yoffset = y_downoffset;
69  }
70  } else if(mHasMouse) {
71  if( m_hoverImage ) {
72  img = m_hoverImage;
73  }
74  }
75 
76  if (img) {
77  graphics->drawImage(img, xoffset, yoffset, 0, 0, getWidth(), getHeight());
78  }
79 
80  graphics->setColor(getForegroundColor());
81  int32_t textX;
82  int32_t textY = getHeight() / 2 - getFont()->getHeight() / 2;
83  switch (getAlignment())
84  {
85  case Graphics::LEFT:
86  textX = 4;
87  break;
88  case Graphics::CENTER:
89  textX = getWidth() / 2;
90  break;
91  case Graphics::RIGHT:
92  textX = getWidth() - 4;
93  break;
94  default:
95  textX = 4;
96  FL_WARN(_log, FIFE::LMsg("TwoButton::draw() - ") << "Unknown alignment: "
97  << getAlignment() << ". Using the default of Graphics::LEFT");
98  }
99 
100  graphics->setFont(getFont());
101  if (mCaption.size() > 0) {
102  if (isPressed())
103  graphics->drawText(getCaption(), textX + 1,
104  textY + 1, getAlignment());
105  else
106  graphics->drawText(getCaption(), textX, textY, getAlignment());
107  }
108  }
110  int32_t w = 0;
111  int32_t h = w;
112  if( m_upImage ) {
113  w = m_upImage->getWidth();
114  h = m_upImage->getHeight();
115  }
116  if( m_downImage ) {
117  w = std::max(m_downImage->getWidth(), w);
118  h = std::max(m_downImage->getHeight(), h);
119  }
120  if( m_hoverImage ) {
121  w = std::max(m_hoverImage->getWidth(), w);
122  h = std::max(m_hoverImage->getHeight(), h);
123  }
124  setWidth(w);
125  setHeight(h);
126  }
127  void TwoButton::setUpImage(Image* image) {
128  m_upImage = image;
129  adjustSize();
130  }
131  void TwoButton::setDownImage(Image* image) {
132  m_downImage = image;
133  adjustSize();
134  }
135  void TwoButton::setHoverImage(Image* image) {
136  m_hoverImage = image;
137  adjustSize();
138  }
139 
140 }
141 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
142 
Definition: modules.h:41
#define FL_WARN(logger, msg)
Definition: logger.h:72
int32_t y_downoffset
Definition: twobutton.h:60
Helper class to create log strings out from separate parts Usage: LMsg(&quot;some text&quot;) &lt;&lt; variable &lt;&lt; &quot;...
Definition: logger.h:82
void setDownOffset(int32_t x, int32_t y)
Definition: twobutton.cpp:54
int32_t x_downoffset
Definition: twobutton.h:59
Image * m_hoverImage
Definition: twobutton.h:58
void setDownImage(Image *image)
Definition: twobutton.cpp:131
void adjustSize()
Definition: twobutton.cpp:109
void draw(Graphics *graphics)
Definition: twobutton.cpp:59
TwoButton(Image *up_image=0, Image *down_image=0, Image *hover_file=0, const std::string &caption="")
Definition: twobutton.cpp:38
Image * m_upImage
Definition: twobutton.h:56
void setHoverImage(Image *image)
Definition: twobutton.cpp:135
void setUpImage(Image *image)
Definition: twobutton.cpp:127
Create a Logger instance to communicate with LogManager Logger stores information about the current m...
Definition: logger.h:211
Image * m_downImage
Definition: twobutton.h:57
static FIFE::Logger _log(LM_GUI)