Mercurial > fife-parpg
comparison engine/core/gui/widgets/clicklabel.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 #include "gui/base/gui_font.h" | |
32 #include "util/base/exception.h" | |
33 #include "video/image.h" | |
34 | |
35 #include "clicklabel.h" | |
36 | |
37 namespace gcn { | |
38 ClickLabel::ClickLabel() { | |
39 mGuiFont = static_cast<FIFE::GuiFont*> (getFont()); | |
40 // setAlignment(Graphics::LEFT); | |
41 setTextWrapping(false); | |
42 setFrameSize(0); | |
43 | |
44 addMouseListener(this); | |
45 addKeyListener(this); | |
46 addFocusListener(this); | |
47 | |
48 } | |
49 | |
50 ClickLabel::ClickLabel(const std::string& caption) { | |
51 mGuiFont = static_cast<FIFE::GuiFont*> (getFont()); | |
52 // setAlignment(Graphics::LEFT); | |
53 setTextWrapping(false); | |
54 setCaption(caption); | |
55 setFrameSize(0); | |
56 | |
57 addMouseListener(this); | |
58 addKeyListener(this); | |
59 addFocusListener(this); | |
60 | |
61 wrapText(); | |
62 } | |
63 | |
64 ClickLabel::~ClickLabel() { | |
65 } | |
66 | |
67 void ClickLabel::setCaption(const std::string& caption) { | |
68 mCaption = caption; | |
69 mGuiFont = static_cast<FIFE::GuiFont*> (getFont()); | |
70 wrapText(); | |
71 } | |
72 | |
73 const std::string& ClickLabel::getCaption() const { | |
74 return mCaption; | |
75 } | |
76 | |
77 void ClickLabel::setWidth(int width) { | |
78 Widget::setWidth(width); | |
79 wrapText(); | |
80 } | |
81 | |
82 void ClickLabel::draw(Graphics* graphics) { | |
83 | |
84 if (mGuiFont != static_cast<FIFE::GuiFont*> (getFont())) { | |
85 mGuiFont = static_cast<FIFE::GuiFont*> (getFont()); | |
86 wrapText(); | |
87 adjustSize(); | |
88 } | |
89 | |
90 int textX = 0; | |
91 int textY = 0; | |
92 | |
93 if (mGuiFont) { | |
94 if( isTextWrapping() ) { | |
95 mGuiFont->drawMultiLineString(graphics, mWrappedText, textX, textY); | |
96 } else { | |
97 mGuiFont->drawMultiLineString(graphics, mCaption, textX, textY); | |
98 } | |
99 } | |
100 } | |
101 | |
102 void ClickLabel::setTextWrapping(bool textWrapping) { | |
103 bool wrappingEnabled = !mTextWrapping && textWrapping; | |
104 mTextWrapping = textWrapping; | |
105 if (wrappingEnabled) { | |
106 wrapText(); | |
107 } | |
108 } | |
109 | |
110 bool ClickLabel::isTextWrapping() const { | |
111 return mTextWrapping; | |
112 } | |
113 | |
114 void ClickLabel::adjustSize() { | |
115 if (mGuiFont) { | |
116 FIFE::Image* image; | |
117 if( isTextWrapping() ) { | |
118 image = mGuiFont->getAsImageMultiline(mWrappedText); | |
119 } else { | |
120 image = mGuiFont->getAsImage(mCaption); | |
121 } | |
122 setWidth( image->getWidth() ); | |
123 setHeight( image->getHeight() ); | |
124 } | |
125 } | |
126 | |
127 void ClickLabel::wrapText() { | |
128 if( isTextWrapping() && mGuiFont ) { | |
129 mWrappedText = mGuiFont->splitTextToWidth(mCaption,getWidth()); | |
130 } | |
131 } | |
132 | |
133 | |
134 void ClickLabel::mousePressed(MouseEvent& mouseEvent) | |
135 { | |
136 if (mouseEvent.getButton() == MouseEvent::LEFT) { | |
137 mMousePressed = true; | |
138 mouseEvent.consume(); | |
139 } | |
140 } | |
141 | |
142 void ClickLabel::mouseExited(MouseEvent& mouseEvent) | |
143 { | |
144 mHasMouse = false; | |
145 } | |
146 | |
147 void ClickLabel::mouseEntered(MouseEvent& mouseEvent) | |
148 { | |
149 mHasMouse = true; | |
150 } | |
151 | |
152 void ClickLabel::mouseReleased(MouseEvent& mouseEvent) | |
153 { | |
154 if (mouseEvent.getButton() == MouseEvent::LEFT && mMousePressed && mHasMouse) { | |
155 mMousePressed = false; | |
156 distributeActionEvent(); | |
157 mouseEvent.consume(); | |
158 } else if (mouseEvent.getButton() == MouseEvent::LEFT) { | |
159 mMousePressed = false; | |
160 mouseEvent.consume(); | |
161 } | |
162 } | |
163 | |
164 void ClickLabel::mouseDragged(MouseEvent& mouseEvent) | |
165 { | |
166 mouseEvent.consume(); | |
167 } | |
168 | |
169 void ClickLabel::keyPressed(KeyEvent& keyEvent) | |
170 { | |
171 Key key = keyEvent.getKey(); | |
172 | |
173 if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) { | |
174 mKeyPressed = true; | |
175 keyEvent.consume(); | |
176 } | |
177 } | |
178 | |
179 void ClickLabel::keyReleased(KeyEvent& keyEvent) | |
180 { | |
181 Key key = keyEvent.getKey(); | |
182 | |
183 if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && mKeyPressed) { | |
184 mKeyPressed = false; | |
185 distributeActionEvent(); | |
186 keyEvent.consume(); | |
187 } | |
188 } | |
189 | |
190 void ClickLabel::focusLost(const Event& event) | |
191 { | |
192 mMousePressed = false; | |
193 mKeyPressed = false; | |
194 } | |
195 } |