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 
24 #ifndef GUI_WIDGETS_TEXTPREVIEW_H
25 #define GUI_WIDGETS_TEXTPREVIEW_H
26 
27 #include "gui/widgets/widget.h"
28 
29 #include "enums/simpletypes/opaque.h"
30 
31 #include "localconsts.h"
32 
33 /**
34  * Preview widget for particle colors, etc.
35  */
36 class TextPreview final : public Widget
37 {
38     public:
39         TextPreview(const Widget2 *const widget,
40                     const std::string &text);
41 
42         A_DELETE_COPY(TextPreview)
43 
44         ~TextPreview() override final;
45 
setTextColor(const Color * color)46         inline void setTextColor(const Color *color)
47         { mTextColor = color; adjustSize(); }
48 
setTextColor2(const Color * color)49         inline void setTextColor2(const Color *color)
50         { mTextColor2 = color; adjustSize(); }
51 
52         /**
53          * Sets the text to use the set alpha value.
54          *
55          * @param alpha whether to use alpha values for the text or not
56          */
useTextAlpha(const bool alpha)57         inline void useTextAlpha(const bool alpha)
58         { mTextAlpha = alpha; }
59 
60         /**
61          * Sets the color the text background is drawn in. This is only the
62          * rectangle directly behind the text, not to full widget.
63          *
64          * @param color the color to set
65          */
setTextBGColor(const Color * color)66         inline void setTextBGColor(const Color *color)
67         { mTextBGColor = color; }
68 
69         /**
70          * Sets the background color of the widget.
71          *
72          * @param color the color to set
73          */
setBGColor(const Color * color)74         inline void setBGColor(const Color *color)
75         { mBGColor = color; }
76 
77         /**
78          * Sets the font to render the text in.
79          *
80          * @param font the font to use.
81          */
setFont(Font * const font)82         inline void setFont(Font *const font)
83         { mFont = font; }
84 
85         /**
86          * Sets whether to use a shadow while rendering.
87          *
88          * @param shadow true, if a shadow is wanted, false else
89          */
setShadow(const bool shadow)90         inline void setShadow(const bool shadow)
91         { mShadow = shadow; }
92 
93         /**
94          * Sets whether to use an outline while rendering.
95          *
96          * @param outline true, if an outline is wanted, false else
97          */
setOutline(const bool outline)98         inline void setOutline(const bool outline)
99         { mOutline = outline; }
100 
101         /**
102          * Widget's draw method. Does the actual job.
103          *
104          * @param graphics graphics to draw into
105          */
106         void draw(Graphics *const graphics) override final A_NONNULL(2);
107 
108         void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
109 
110         /**
111          * Set opacity for this widget (whether or not to show the background
112          * color)
113          *
114          * @param opaque Whether the widget should be opaque or not
115          */
setOpaque(const Opaque opaque)116         void setOpaque(const Opaque opaque) noexcept2
117         { mOpaque = opaque; }
118 
119         /**
120          * Gets opacity for this widget (whether or not the background color
121          * is shown below the widget)
122          */
isOpaque()123         bool isOpaque() const noexcept2 A_WARN_UNUSED
124         { return mOpaque == Opaque_true; }
125 
126         void adjustSize();
127 
128     private:
129         Font *mFont;
130         std::string mText;
131         const Color *mTextColor;
132         const Color *mTextColor2;
133         const Color *mBGColor;
134         const Color *mTextBGColor;
135         int mPadding;
136         static int instances;
137         static float mAlpha;
138         static Skin *mSkin;
139         bool mTextAlpha;
140         Opaque mOpaque;
141         bool mShadow;
142         bool mOutline;
143 };
144 
145 #endif  // GUI_WIDGETS_TEXTPREVIEW_H
146