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