FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
twobutton.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
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 
32 #include "twobutton.h"
33 
34 namespace gcn {
35  TwoButton::TwoButton(Image *up_file , Image *down_file, Image *hover_file, const std::string& caption):
36  Button(),
37  m_upImage(up_file),
38  m_downImage(down_file),
39  m_hoverImage(hover_file),
40  x_downoffset(0),
41  y_downoffset(0) {
42  m_hoverImage = hover_file;
43  setFrameSize(0);
44  adjustSize();
45  mCaption = caption;
46  }
47 
48  TwoButton::~TwoButton() {
49  }
50 
51  void TwoButton::setDownOffset(int x, int y) {
52  x_downoffset = x;
53  y_downoffset = y;
54  }
55 
56  void TwoButton::draw(Graphics *graphics) {
57  Image* img = m_upImage;
58  int xoffset = 0;
59  int yoffset = 0;
60 
61  if (isPressed()) {
62  if( m_downImage ) {
63  img = m_downImage;
64  xoffset = x_downoffset;
65  yoffset = y_downoffset;
66  }
67  } else if(mHasMouse) {
68  if( m_hoverImage ) {
69  img = m_hoverImage;
70  }
71  }
72 
73  if (img) {
74  graphics->drawImage(img, xoffset, yoffset);
75  }
76 
77  graphics->setColor(getForegroundColor());
78  int textX;
79  int textY = getHeight() / 2 - getFont()->getHeight() / 2;
80  switch (getAlignment())
81  {
82  case Graphics::LEFT:
83  textX = 4;
84  break;
85  case Graphics::CENTER:
86  textX = getWidth() / 2;
87  break;
88  case Graphics::RIGHT:
89  textX = getWidth() - 4;
90  break;
91  default:
92  throw GCN_EXCEPTION("Unknown alignment.");
93  }
94 
95  graphics->setFont(getFont());
96  if (mCaption.size() > 0) {
97  if (isPressed())
98  graphics->drawText(getCaption(), textX + 1,
99  textY + 1, getAlignment());
100  else
101  graphics->drawText(getCaption(), textX, textY, getAlignment());
102  }
103  }
104  void TwoButton::adjustSize() {
105  int w = 0;
106  int h = w;
107  if( m_upImage ) {
108  w = m_upImage->getWidth();
109  h = m_upImage->getHeight();
110  }
111  if( m_downImage ) {
112  w = std::max(m_downImage->getWidth(), w);
113  h = std::max(m_downImage->getHeight(), h);
114  }
115  if( m_hoverImage ) {
116  w = std::max(m_hoverImage->getWidth(), w);
117  h = std::max(m_hoverImage->getHeight(), h);
118  }
119  setWidth(w);
120  setHeight(h);
121  }
122  void TwoButton::setUpImage(Image* image) {
123  m_upImage = image;
124  adjustSize();
125  }
126  void TwoButton::setDownImage(Image* image) {
127  m_downImage = image;
128  adjustSize();
129  }
130  void TwoButton::setHoverImage(Image* image) {
131  m_hoverImage = image;
132  adjustSize();
133  }
134 
135 }
136 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
137