1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/***************************************************************************
* Copyright (C) 2005-2019 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
#include <cassert>
// 3rd party library includes
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "gui/fifechan/base/gui_font.h"
#include "util/base/exception.h"
#include "video/image.h"
#include "clicklabel.h"
namespace fcn {
ClickLabel::ClickLabel() {
mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
setAlignment(Graphics::Left);
setOpaque(true);
mTextWrapping = false;
setBorderSize(0);
mHasMouse = false,
mKeyPressed = false,
mMousePressed = false;
addMouseListener(this);
addKeyListener(this);
addFocusListener(this);
addWidgetListener(this);
}
ClickLabel::ClickLabel(const std::string& caption) {
mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
setCaption(caption);
setAlignment(Graphics::Left);
setOpaque(true);
mTextWrapping = false;
setBorderSize(0);
mHasMouse = false,
mKeyPressed = false,
mMousePressed = false;
adjustSize();
addMouseListener(this);
addKeyListener(this);
addFocusListener(this);
addWidgetListener(this);
}
ClickLabel::~ClickLabel() {
}
void ClickLabel::setCaption(const std::string& caption) {
mCaption = caption;
mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
wrapText();
}
const std::string& ClickLabel::getCaption() const {
return mCaption;
}
void ClickLabel::setAlignment(Graphics::Alignment alignment) {
mAlignment = alignment;
}
Graphics::Alignment ClickLabel::getAlignment() const {
return mAlignment;
}
void ClickLabel::setOpaque(bool opaque) {
mOpaque = opaque;
}
bool ClickLabel::isOpaque() const {
return mOpaque;
}
void ClickLabel::setTextWrapping(bool textWrapping) {
bool wrappingEnabled = !mTextWrapping && textWrapping;
mTextWrapping = textWrapping;
if (wrappingEnabled) {
wrapText();
}
}
bool ClickLabel::isTextWrapping() const {
return mTextWrapping;
}
void ClickLabel::wrapText() {
if (isTextWrapping() && mGuiFont) {
int32_t w = getWidth() - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
mWrappedText = mGuiFont->splitTextToWidth(mCaption, w);
}
}
void ClickLabel::setDimension(const Rectangle& dimension) {
int32_t w = getWidth();
Widget::setDimension(dimension);
if (getWidth() != w && isTextWrapping()) {
wrapText();
}
}
void ClickLabel::resizeToContent(bool recursiv) {
adjustSize();
}
void ClickLabel::adjustSize() {
if (mGuiFont) {
int32_t w = getWidth();
int32_t h = 0;
if (isTextWrapping()) {
if (getParent()) {
w = getParent()->getChildrenArea().width;
}
int32_t textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
int32_t maxW = isFixedSize() ? getFixedSize().getWidth() : getMaxSize().getWidth();
if (textW < 1) {
w = maxW;
textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
} else if (w > maxW) {
w = std::min(w, maxW);
textW = w - 2 * getBorderSize() - getPaddingLeft() - getPaddingRight();
}
mWrappedText = mGuiFont->splitTextToWidth(mCaption, textW);
} else {
FIFE::Image* image = mGuiFont->getAsImageMultiline(mCaption);
w = image->getWidth() + 2 * getBorderSize() + getPaddingLeft() + getPaddingRight();
}
const std::string& text = isTextWrapping() ? mWrappedText : mCaption;
FIFE::Image* image = mGuiFont->getAsImageMultiline(text);
h = 2 * getBorderSize() + getPaddingTop() + getPaddingBottom() + image->getHeight();
setSize(w, h);
}
}
void ClickLabel::draw(Graphics* graphics) {
bool active = isFocused();
Rectangle offsetRec(getBorderSize(), getBorderSize(), 2 * getBorderSize(), 2 * getBorderSize());
if (isOpaque()) {
Color faceColor = getBackgroundColor();
if (active && ((getSelectionMode() & Widget::Selection_Background) == Widget::Selection_Background)) {
faceColor = getSelectionColor();
}
graphics->setColor(faceColor);
graphics->fillRectangle(offsetRec.x, offsetRec.y, getWidth() - offsetRec.width, getHeight() - offsetRec.height);
}
if (getBorderSize() > 0) {
if (active && (getSelectionMode() & Widget::Selection_Border) == Widget::Selection_Border) {
drawSelectionFrame(graphics);
} else {
drawBorder(graphics);
}
}
if (mGuiFont) {
graphics->setColor(getForegroundColor());
const std::string& text = isTextWrapping() ? mWrappedText : mCaption;
FIFE::Image* image = mGuiFont->getAsImageMultiline(text);
int32_t textX = 0;
int32_t textY = offsetRec.y + getPaddingTop() + (getHeight() - offsetRec.height - getPaddingTop() - getPaddingBottom() - image->getHeight()) / 2;
switch (getAlignment()) {
case Graphics::Left:
textX = offsetRec.x + getPaddingLeft();
break;
case Graphics::Center:
textX = offsetRec.x + getPaddingLeft() + (getWidth() - offsetRec.width - getPaddingLeft() - getPaddingRight() - image->getWidth()) / 2;
break;
case Graphics::Right:
textX = getWidth() - offsetRec.x - getPaddingRight() - image->getWidth();
break;
default:
throw FCN_EXCEPTION("Unknown alignment.");
}
mGuiFont->drawMultiLineString(graphics, text, textX, textY);
}
}
void ClickLabel::fontChanged() {
mGuiFont = static_cast<FIFE::GuiFont*>(getFont());
wrapText();
adjustSize();
}
void ClickLabel::mousePressed(MouseEvent& mouseEvent) {
if (mouseEvent.getButton() == MouseEvent::Left) {
mMousePressed = true;
mouseEvent.consume();
}
}
void ClickLabel::mouseExited(MouseEvent& mouseEvent) {
mHasMouse = false;
}
void ClickLabel::mouseEntered(MouseEvent& mouseEvent) {
mHasMouse = true;
}
void ClickLabel::mouseReleased(MouseEvent& mouseEvent) {
if (mouseEvent.getButton() == MouseEvent::Left && mMousePressed && mHasMouse) {
mMousePressed = false;
distributeActionEvent();
mouseEvent.consume();
} else if (mouseEvent.getButton() == MouseEvent::Left) {
mMousePressed = false;
mouseEvent.consume();
}
}
void ClickLabel::mouseDragged(MouseEvent& mouseEvent) {
mouseEvent.consume();
}
void ClickLabel::keyPressed(KeyEvent& keyEvent) {
Key key = keyEvent.getKey();
if (key.getValue() == Key::Enter || key.getValue() == Key::Space) {
mKeyPressed = true;
keyEvent.consume();
}
}
void ClickLabel::keyReleased(KeyEvent& keyEvent) {
Key key = keyEvent.getKey();
if ((key.getValue() == Key::Enter || key.getValue() == Key::Space) && mKeyPressed) {
mKeyPressed = false;
distributeActionEvent();
keyEvent.consume();
}
}
void ClickLabel::focusLost(const Event& event) {
mMousePressed = false;
mKeyPressed = false;
mHasMouse = false;
}
void ClickLabel::ancestorHidden(const Event& event) {
mMousePressed = false;
mKeyPressed = false;
mHasMouse = false;
}
}
|