FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
clicklabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2017 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
32 #include "util/base/exception.h"
33 #include "video/image.h"
34 
35 #include "clicklabel.h"
36 
37 namespace fcn {
39  mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
40  setAlignment(Graphics::Left);
41  setOpaque(true);
42  mTextWrapping = false;
43  setBorderSize(0);
44  mHasMouse = false,
45  mKeyPressed = false,
46  mMousePressed = false;
47 
48  addMouseListener(this);
49  addKeyListener(this);
50  addFocusListener(this);
51  addWidgetListener(this);
52  }
53 
54  ClickLabel::ClickLabel(const std::string& caption) {
55  mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
56  setCaption(caption);
57  setAlignment(Graphics::Left);
58  setOpaque(true);
59  mTextWrapping = false;
60  setBorderSize(0);
61  mHasMouse = false,
62  mKeyPressed = false,
63  mMousePressed = false;
64 
65  adjustSize();
66  addMouseListener(this);
67  addKeyListener(this);
68  addFocusListener(this);
69  addWidgetListener(this);
70  }
71 
73  }
74 
75  void ClickLabel::setCaption(const std::string& caption) {
76  mCaption = caption;
77  mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
78  wrapText();
79  }
80 
81  const std::string& ClickLabel::getCaption() const {
82  return mCaption;
83  }
84 
85  void ClickLabel::setAlignment(Graphics::Alignment alignment) {
86  mAlignment = alignment;
87  }
88 
89  Graphics::Alignment ClickLabel::getAlignment() const {
90  return mAlignment;
91  }
92 
93  void ClickLabel::setOpaque(bool opaque) {
94  mOpaque = opaque;
95  }
96 
97  bool ClickLabel::isOpaque() const {
98  return mOpaque;
99  }
100 
101  void ClickLabel::setTextWrapping(bool textWrapping) {
102  bool wrappingEnabled = !mTextWrapping && textWrapping;
103  mTextWrapping = textWrapping;
104  if (wrappingEnabled) {
105  wrapText();
106  }
107  }
108 
110  return mTextWrapping;
111  }
112 
114  if (isTextWrapping() && mGuiFont) {
115  int32_t w = getWidth() - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
117  }
118  }
119 
120  void ClickLabel::setDimension(const Rectangle& dimension) {
121  int32_t w = getWidth();
122  Widget::setDimension(dimension);
123  if (getWidth() != w && isTextWrapping()) {
124  wrapText();
125  }
126  }
127 
128  void ClickLabel::resizeToContent(bool recursiv) {
129  adjustSize();
130  }
131 
133  if (mGuiFont) {
134  int32_t w = getWidth();
135  int32_t h = 0;
136  if (isTextWrapping()) {
137  if (getParent()) {
138  w = getParent()->getChildrenArea().width;
139  }
140  int32_t textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
141  int32_t maxW = isFixedSize() ? getFixedSize().getWidth() : getMaxSize().getWidth();
142  if (textW < 1) {
143  w = maxW;
144  textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
145  } else if (w > maxW) {
146  w = std::min(w, maxW);
147  textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
148  }
150  } else {
152  w = image->getWidth() + 2 * getBorderSize() + getPaddingLeft() + getPaddingRight();
153  }
154  const std::string& text = isTextWrapping() ? mWrappedText : mCaption;
155  FIFE::Image* image = mGuiFont->getAsImageMultiline(text);
156  h = 2 * getBorderSize() + getPaddingTop() + getPaddingBottom() + image->getHeight();
157  setSize(w, h);
158  }
159  }
160 
161  void ClickLabel::draw(Graphics* graphics) {
162  bool active = isFocused();
163  Rectangle offsetRec(getBorderSize(), getBorderSize(), 2 * getBorderSize(), 2 * getBorderSize());
164 
165  if (isOpaque()) {
166  Color faceColor = getBackgroundColor();
167  if (active && ((getSelectionMode() & Widget::Selection_Background) == Widget::Selection_Background)) {
168  faceColor = getSelectionColor();
169  }
170  graphics->setColor(faceColor);
171  graphics->fillRectangle(offsetRec.x, offsetRec.y, getWidth() - offsetRec.width, getHeight() - offsetRec.height);
172  }
173 
174  if (getBorderSize() > 0) {
175  if (active && (getSelectionMode() & Widget::Selection_Border) == Widget::Selection_Border) {
176  drawSelectionFrame(graphics);
177  } else {
178  drawBorder(graphics);
179  }
180  }
181 
182  if (mGuiFont) {
183  graphics->setColor(getForegroundColor());
184  const std::string& text = isTextWrapping() ? mWrappedText : mCaption;
185  FIFE::Image* image = mGuiFont->getAsImageMultiline(text);
186 
187  int32_t textX = 0;
188  int32_t textY = offsetRec.y + getPaddingTop() + (getHeight() - offsetRec.height - getPaddingTop() - getPaddingBottom() - image->getHeight()) / 2;
189 
190  switch (getAlignment()) {
191  case Graphics::Left:
192  textX = offsetRec.x + getPaddingLeft();
193  break;
194  case Graphics::Center:
195  textX = offsetRec.x + getPaddingLeft() + (getWidth() - offsetRec.width - getPaddingLeft() - getPaddingRight() - image->getWidth()) / 2;
196  break;
197  case Graphics::Right:
198  textX = getWidth() - offsetRec.x - getPaddingRight() - image->getWidth();
199  break;
200  default:
201  throw FCN_EXCEPTION("Unknown alignment.");
202  }
203 
204  mGuiFont->drawMultiLineString(graphics, text, textX, textY);
205  }
206  }
207 
209  mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
210  wrapText();
211  adjustSize();
212  }
213 
214  void ClickLabel::mousePressed(MouseEvent& mouseEvent) {
215  if (mouseEvent.getButton() == MouseEvent::Left) {
216  mMousePressed = true;
217  mouseEvent.consume();
218  }
219  }
220 
221  void ClickLabel::mouseExited(MouseEvent& mouseEvent) {
222  mHasMouse = false;
223  }
224 
225  void ClickLabel::mouseEntered(MouseEvent& mouseEvent) {
226  mHasMouse = true;
227  }
228 
229  void ClickLabel::mouseReleased(MouseEvent& mouseEvent) {
230  if (mouseEvent.getButton() == MouseEvent::Left && mMousePressed && mHasMouse) {
231  mMousePressed = false;
232  distributeActionEvent();
233  mouseEvent.consume();
234  } else if (mouseEvent.getButton() == MouseEvent::Left) {
235  mMousePressed = false;
236  mouseEvent.consume();
237  }
238  }
239 
240  void ClickLabel::mouseDragged(MouseEvent& mouseEvent) {
241  mouseEvent.consume();
242  }
243 
244  void ClickLabel::keyPressed(KeyEvent& keyEvent) {
245  Key key = keyEvent.getKey();
246 
247  if (key.getValue() == Key::Enter || key.getValue() == Key::Space) {
248  mKeyPressed = true;
249  keyEvent.consume();
250  }
251  }
252 
253  void ClickLabel::keyReleased(KeyEvent& keyEvent) {
254  Key key = keyEvent.getKey();
255 
256  if ((key.getValue() == Key::Enter || key.getValue() == Key::Space) && mKeyPressed) {
257  mKeyPressed = false;
258  distributeActionEvent();
259  keyEvent.consume();
260  }
261  }
262 
263  void ClickLabel::focusLost(const Event& event) {
264  mMousePressed = false;
265  mKeyPressed = false;
266  mHasMouse = false;
267  }
268 
269  void ClickLabel::ancestorHidden(const Event& event) {
270  mMousePressed = false;
271  mKeyPressed = false;
272  mHasMouse = false;
273  }
274 }
std::string splitTextToWidth(const std::string &text, int32_t render_width)
Definition: gui_font.cpp:182
virtual void fontChanged()
Definition: clicklabel.cpp:208
Base Class for Images.
Definition: image.h:48
virtual void keyReleased(KeyEvent &keyEvent)
Definition: clicklabel.cpp:253
virtual void ancestorHidden(const Event &event)
Definition: clicklabel.cpp:269
bool mMousePressed
True if a mouse has been pressed, false otherwise.
Definition: clicklabel.h:208
virtual ~ClickLabel()
Definition: clicklabel.cpp:72
virtual void setTextWrapping(bool textWrapping)
Sets the text wrapping of the caption.
Definition: clicklabel.cpp:101
virtual void mouseExited(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:221
virtual void mouseEntered(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:225
virtual void setOpaque(bool opaque)
Sets the opacity of the background.
Definition: clicklabel.cpp:93
bool mHasMouse
True if the mouse is ontop of the button, false otherwise.
Definition: clicklabel.h:198
FIFE::GuiFont * mGuiFont
Holds the gui font.
Definition: clicklabel.h:168
std::string mCaption
Holds the caption of the label.
Definition: clicklabel.h:173
virtual void wrapText()
Definition: clicklabel.cpp:113
void drawMultiLineString(fcn::Graphics *graphics, const std::string &text, int32_t x, int32_t y)
Definition: gui_font.cpp:80
Image * getAsImageMultiline(const std::string &text)
Gets given text as Image.
Definition: gui_font.cpp:178
uint32_t getHeight() const
Definition: image.cpp:160
std::string mWrappedText
Holds the wrapped text of the label.
Definition: clicklabel.h:178
virtual void setDimension(const Rectangle &dimension)
Definition: clicklabel.cpp:120
virtual void adjustSize()
Definition: clicklabel.cpp:132
virtual const std::string & getCaption() const
Gets the caption of the label.
Definition: clicklabel.cpp:81
bool mTextWrapping
True if text wrapping is enabled, otherwise false.
Definition: clicklabel.h:193
virtual Graphics::Alignment getAlignment() const
Gets the alignment of the caption.
Definition: clicklabel.cpp:89
virtual void resizeToContent(bool recursiv=true)
Definition: clicklabel.cpp:128
virtual void mouseReleased(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:229
uint32_t getWidth() const
Definition: image.cpp:151
virtual void mousePressed(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:214
bool mOpaque
True if opaque, otherwise false.
Definition: clicklabel.h:188
virtual void mouseDragged(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:240
virtual void setAlignment(Graphics::Alignment alignment)
Sets the alignment of the caption.
Definition: clicklabel.cpp:85
ClickLabel()
Constructor.
Definition: clicklabel.cpp:38
Graphics::Alignment mAlignment
Holds the alignment of the caption.
Definition: clicklabel.h:183
virtual bool isOpaque() const
Definition: clicklabel.cpp:97
virtual bool isTextWrapping() const
Gets the text wrapping of the caption.
Definition: clicklabel.cpp:109
virtual void draw(Graphics *graphics)
Definition: clicklabel.cpp:161
bool mKeyPressed
True if a key has been pressed, false otherwise.
Definition: clicklabel.h:203
virtual void focusLost(const Event &event)
Definition: clicklabel.cpp:263
virtual void keyPressed(KeyEvent &keyEvent)
Definition: clicklabel.cpp:244
virtual void setCaption(const std::string &caption)
Sets the caption of the label.
Definition: clicklabel.cpp:75