| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 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 |
| 31 |
#include "gui/fifechan/base/gui_font.h" |
| 32 |
#include "util/base/exception.h" |
| 33 |
#include "video/image.h" |
| 34 |
|
| 35 |
#include "clicklabel.h" |
| 36 |
|
| 37 |
namespace fcn { |
| 38 |
ClickLabel::ClickLabel() { |
| 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 |
|
| 72 |
ClickLabel::~ClickLabel() { |
| 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 |
|
| 109 |
bool ClickLabel::isTextWrapping() const { |
| 110 |
return mTextWrapping; |
| 111 |
} |
| 112 |
|
| 113 |
void ClickLabel::wrapText() { |
| 114 |
if (isTextWrapping() && mGuiFont) { |
| 115 |
int32_t w = getWidth() - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight(); |
| 116 |
mWrappedText = mGuiFont->splitTextToWidth(mCaption, w); |
| 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 |
|
| 132 |
void ClickLabel::adjustSize() { |
| 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 |
} |
| 149 |
mWrappedText = mGuiFont->splitTextToWidth(mCaption, textW); |
| 150 |
} else { |
| 151 |
FIFE::Image* image = mGuiFont->getAsImageMultiline(mCaption); |
| 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 |
|
| 208 |
void ClickLabel::fontChanged() { |
| 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 |
} |