FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
clicklabel.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
32 #include "util/base/exception.h"
33 #include "video/image.h"
34 
35 #include "clicklabel.h"
36 
37 namespace gcn {
39  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
40 // setAlignment(Graphics::LEFT);
41  setTextWrapping(false);
42  setFrameSize(0);
43  addMouseListener(this);
44  addKeyListener(this);
45  addFocusListener(this);
46 
47  }
48 
49  ClickLabel::ClickLabel(const std::string& caption) {
50  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
51 // setAlignment(Graphics::LEFT);
52  setTextWrapping(false);
53  setCaption(caption);
54  setFrameSize(0);
55  addMouseListener(this);
56  addKeyListener(this);
57  addFocusListener(this);
58 
59  wrapText();
60  }
61 
63  }
64 
65  void ClickLabel::setCaption(const std::string& caption) {
66  mCaption = caption;
67  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
68  wrapText();
69  }
70 
71  const std::string& ClickLabel::getCaption() const {
72  return mCaption;
73  }
74 
75  void ClickLabel::setWidth(int32_t width) {
76  Widget::setWidth(width);
77  wrapText();
78  }
79 
80  void ClickLabel::draw(Graphics* graphics) {
81 
82  if (mGuiFont != static_cast<FIFE::GuiFont*> (getFont())) {
83  mGuiFont = static_cast<FIFE::GuiFont*> (getFont());
84  wrapText();
85  adjustSize();
86  }
87 
88  int32_t textX = 0;
89  int32_t textY = 0;
90 
91  graphics->setColor(getBackgroundColor());
92  graphics->fillRectangle(Rectangle(1, 1, getDimension().width-1, getHeight() - 1));
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 
111  return mTextWrapping;
112  }
113 
115  if (mGuiFont) {
116  FIFE::Image* image;
117  if( isTextWrapping() ) {
119  } else {
121  }
122  setWidth( image->getWidth() );
123  setHeight( image->getHeight() );
124  }
125  }
126 
128  if( isTextWrapping() && mGuiFont ) {
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 }
std::string splitTextToWidth(const std::string &text, int32_t render_width)
Definition: gui_font.cpp:126
void drawMultiLineString(gcn::Graphics *graphics, const std::string &text, int32_t x, int32_t y)
Definition: gui_font.cpp:72
Base Class for Images.
Definition: image.h:47
virtual void setCaption(const std::string &caption)
Definition: clicklabel.cpp:65
virtual void focusLost(const Event &event)
Definition: clicklabel.cpp:190
virtual void mouseReleased(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:152
bool mTextWrapping
Definition: clicklabel.h:87
virtual const std::string & getCaption() const
Definition: clicklabel.cpp:71
bool mMousePressed
Definition: clicklabel.h:93
Image * getAsImageMultiline(const std::string &text)
Gets given text as Image.
Definition: gui_font.cpp:122
uint32_t getHeight() const
Definition: image.cpp:155
FIFE::GuiFont * mGuiFont
Definition: clicklabel.h:86
virtual void setWidth(int32_t width)
Definition: clicklabel.cpp:75
void setTextWrapping(bool)
Definition: clicklabel.cpp:102
bool isTextWrapping() const
Definition: clicklabel.cpp:110
virtual void keyPressed(KeyEvent &keyEvent)
Definition: clicklabel.cpp:169
std::string mCaption
Definition: clicklabel.h:88
uint32_t getWidth() const
Definition: image.cpp:146
virtual void adjustSize()
Definition: clicklabel.cpp:114
virtual void keyReleased(KeyEvent &keyEvent)
Definition: clicklabel.cpp:179
virtual ~ClickLabel()
Definition: clicklabel.cpp:62
std::string mWrappedText
Definition: clicklabel.h:89
virtual void draw(Graphics *graphics)
Definition: clicklabel.cpp:80
virtual void mousePressed(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:134
virtual void mouseEntered(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:147
virtual void mouseExited(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:142
virtual void mouseDragged(MouseEvent &mouseEvent)
Definition: clicklabel.cpp:164