ManaPlus
textpreview.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2006-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "settings.h"
26 
27 #include "gui/gui.h"
28 #include "gui/skin.h"
29 
30 #include "gui/fonts/font.h"
31 
32 #include "render/graphics.h"
33 
34 #include "debug.h"
35 
37 float TextPreview::mAlpha = 1.0;
38 Skin *TextPreview::mSkin = nullptr;
39 
40 TextPreview::TextPreview(const Widget2 *const widget,
41  const std::string &text) :
42  Widget(widget),
43  mFont(gui->getFont()),
44  mText(text),
45  mTextColor(&getThemeColor(ThemeColorId::TEXT, 255U)),
46  mTextColor2(&getThemeColor(ThemeColorId::TEXT_OUTLINE, 255U)),
47  mBGColor(&getThemeColor(ThemeColorId::BACKGROUND, 255U)),
48  mTextBGColor(nullptr),
49  mPadding(0),
50  mTextAlpha(false),
51  mOpaque(Opaque_false),
52  mShadow(false),
53  mOutline(false)
54 {
55  mAllowLogic = false;
56  if (instances == 0)
57  {
58  if (theme != nullptr)
59  {
60  mSkin = theme->load("textpreview.xml",
61  "",
62  true,
64  }
65  }
66 
67  instances++;
68 
69  if (mSkin != nullptr)
70  mPadding = mSkin->getOption("padding", 0);
71 
72  adjustSize();
73 }
74 
76 {
77  if (gui != nullptr)
78  gui->removeDragged(this);
79 
80  instances--;
81 
82  if (instances == 0)
83  {
84  if (theme != nullptr)
85  theme->unload(mSkin);
86  }
87 }
88 
89 void TextPreview::draw(Graphics *const graphics)
90 {
91  if (mFont == nullptr)
92  return;
93 
94  BLOCK_START("TextPreview::draw")
95  if (settings.guiAlpha != mAlpha)
97 
98  const int intAlpha = CAST_S32(mAlpha * 255.0F);
99  const int alpha = mTextAlpha ? intAlpha : 255;
100 
101  if (mOpaque == Opaque_true)
102  {
103  graphics->setColor(Color(CAST_S32(mBGColor->r),
104  CAST_S32(mBGColor->g),
105  CAST_S32(mBGColor->b),
106  CAST_S32(mAlpha * 255.0F)));
107  graphics->fillRectangle(Rect(0, 0,
109  }
110 
111  if (mTextBGColor != nullptr)
112  {
113  const int x = mFont->getWidth(mText) + 1
114  + 2 * ((mOutline || mShadow) ? 1 :0);
115  const int y = mFont->getHeight() + 1
116  + 2 * ((mOutline || mShadow) ? 1 : 0);
117  graphics->setColor(Color(CAST_S32(mTextBGColor->r),
120  intAlpha));
121  graphics->fillRectangle(Rect(mPadding, mPadding, x, y));
122  }
123 
124  Color color1(mTextColor->r, mTextColor->g, mTextColor->b, alpha);
125 
126  if (mOutline && mTextColor != mTextColor2)
127  {
128  const Color &color2 = getThemeColor(ThemeColorId::OUTLINE, 255U);
129  mFont->drawString(graphics,
130  color1,
131  color2,
132  mText,
133  mPadding + 1, mPadding + 1);
134  }
135  else
136  {
137  Color color2(mTextColor2->r, mTextColor2->g, mTextColor2->b, alpha);
138  mFont->drawString(graphics,
139  color1,
140  color2,
141  mText,
142  mPadding + 1, mPadding + 1);
143  }
144 
145  BLOCK_END("TextPreview::draw")
146 }
147 
148 void TextPreview::safeDraw(Graphics *const graphics)
149 {
150  TextPreview::draw(graphics);
151 }
152 
154 {
155  setHeight(getFont()->getHeight() + 2 * mPadding);
156 }
Font * getFont() const
Definition: widget.cpp:330
int width
Definition: rect.h:218
Definition: skin.h:35
void unload(Skin *const skin)
Definition: theme.cpp:249
Gui * gui
Definition: gui.cpp:110
unsigned int g
Definition: color.h:239
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
static std::string getThemePath()
Definition: theme.h:66
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
#define BLOCK_END(name)
Definition: perfomance.h:79
static float mAlpha
Definition: textpreview.h:136
bool mTextAlpha
Definition: textpreview.h:138
const Color * mTextColor2
Definition: textpreview.h:131
int getOption(const std::string &name) const
Definition: skin.h:105
std::string mText
Definition: textpreview.h:129
Settings settings
Definition: settings.cpp:31
virtual void setColor(const Color &color)
Definition: graphics.h:319
#define CAST_S32
Definition: cast.h:29
unsigned int b
Definition: color.h:244
const Color * mTextColor
Definition: textpreview.h:130
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
void safeDraw(Graphics *const graphics)
void draw(Graphics *const graphics)
Definition: textpreview.cpp:89
#define nullptr
Definition: localconsts.h:44
bool mAllowLogic
Definition: widget.h:1159
TextPreview(const Widget2 *const widget, const std::string &text)
Definition: textpreview.cpp:40
const bool Opaque_false
Definition: opaque.h:29
int height
Definition: rect.h:223
const Color * mTextBGColor
Definition: textpreview.h:133
void setHeight(const int height)
Definition: widget.cpp:139
Opaque mOpaque
Definition: textpreview.h:139
virtual void fillRectangle(const Rect &rectangle)=0
Theme * theme
Definition: theme.cpp:61
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1161
Definition: widget.h:97
const bool Opaque_true
Definition: opaque.h:29
int getWidth(const std::string &text) const
Definition: font.cpp:333
int getHeight() const
Definition: widget.h:239
Definition: color.h:74
static int instances
Definition: textpreview.h:135
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
Font * mFont
Definition: textpreview.h:128
const Color * mBGColor
Definition: textpreview.h:132
int getHeight() const
Definition: font.cpp:361
float guiAlpha
Definition: settings.h:129
Rect mDimension
Definition: widget.h:1100
static Skin * mSkin
Definition: textpreview.h:137
void adjustSize()
unsigned int r
Definition: color.h:234