comparison engine/core/gui/widgets/twobutton.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, 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_listener(NULL),
43 m_helptext("") {
44 m_hoverImage = hover_file;
45 setFrameSize(0);
46 adjustSize();
47 mCaption = caption;
48 }
49
50 TwoButton::~TwoButton() {
51 }
52
53 void TwoButton::setDownOffset(int x, int y) {
54 x_downoffset = x;
55 y_downoffset = y;
56 }
57
58 void TwoButton::draw(Graphics *graphics) {
59 Image* img = NULL;
60 int xoffset = 0;
61 int yoffset = 0;
62
63 if (isPressed()) {
64 if( m_downImage ) {
65 img = m_downImage;
66 xoffset = x_downoffset;
67 yoffset = y_downoffset;
68 }
69 } else if(mHasMouse) {
70 if( m_hoverImage ) {
71 graphics->drawImage(m_hoverImage, 0, 0);
72 }
73 } else {
74 if( mHasMouse && m_hoverImage) {
75 img = m_hoverImage;
76 } else if (m_upImage) {
77 img = m_upImage;
78 }
79 }
80 if (img) {
81 graphics->drawImage(img, xoffset, yoffset);
82 }
83
84 graphics->setColor(getForegroundColor());
85 int textX;
86 int textY = getHeight() / 2 - getFont()->getHeight() / 2;
87 switch (getAlignment())
88 {
89 case Graphics::LEFT:
90 textX = 4;
91 break;
92 case Graphics::CENTER:
93 textX = getWidth() / 2;
94 break;
95 case Graphics::RIGHT:
96 textX = getWidth() - 4;
97 break;
98 default:
99 throw GCN_EXCEPTION("Unknown alignment.");
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 }
111 void TwoButton::adjustSize() {
112 int w = 0;
113 int 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::mouseEntered(MouseEvent& mouseEvent) {
143 if (m_listener) {
144 m_listener->mouseEntered(*this);
145 }
146 Button::mouseEntered(mouseEvent);
147 }
148
149 void TwoButton::mouseExited(MouseEvent& mouseEvent) {
150 if (m_listener) {
151 m_listener->mouseExited(*this);
152 }
153 Button::mouseExited(mouseEvent);
154 }
155 }
156 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
157