ManaPlus
colorpage.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "gui/widgets/colorpage.h"
23 
24 #include "gui/skin.h"
25 
26 #include "gui/fonts/font.h"
27 
28 #include "gui/models/colormodel.h"
29 
30 #include "render/graphics.h"
31 
32 #include "debug.h"
33 
34 ColorPage::ColorPage(const Widget2 *const widget,
35  ListModel *const listModel,
36  const std::string &skin) :
37  ListBox(widget, listModel, skin)
38 {
39  mItemPadding = mSkin != nullptr ? mSkin->getOption("itemPadding") : 1;
40  const Font *const font = getFont();
41  mRowHeight = CAST_U32(font->getHeight() +
42  2 * mItemPadding);
43  if (mListModel != nullptr)
44  {
47  + 2 * mPadding + 20);
48  }
49 }
50 
52 {
53 }
54 
55 void ColorPage::draw(Graphics *const graphics)
56 {
57  BLOCK_START("ColorPage::draw")
58 
59  const ColorModel *const model = static_cast<const ColorModel*>(
60  mListModel);
61 
62  mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
63  updateAlpha();
64  Font *const font = getFont();
65 
66  const int rowHeight = CAST_S32(getRowHeight());
67  const int width = mDimension.width;
68 
69  if (mSelected >= 0)
70  {
71  graphics->setColor(mHighlightColor);
72  graphics->fillRectangle(Rect(mPadding,
73  rowHeight * mSelected + mPadding,
74  mDimension.width - 2 * mPadding, rowHeight));
75 
76  const ColorPair *const colors = model->getColorAt(mSelected);
77  const std::string str = mListModel->getElementAt(mSelected);
78  font->drawString(graphics,
79  *colors->color1,
80  *colors->color2,
81  str,
82  (width - font->getWidth(str)) / 2,
83  mSelected * rowHeight + mPadding);
84  }
85 
86  const int sz = mListModel->getNumberOfElements();
87  for (int i = 0, y = mPadding; i < sz; ++i, y += rowHeight)
88  {
89  if (i != mSelected)
90  {
91  const ColorPair *const colors = model->getColorAt(i);
92  const std::string str = mListModel->getElementAt(i);
93  font->drawString(graphics,
94  *colors->color1,
95  *colors->color2,
96  str,
97  (width - font->getWidth(str)) / 2,
98  y);
99  }
100  }
101 
102  BLOCK_END("ColorPage::draw")
103 }
104 
105 void ColorPage::safeDraw(Graphics *const graphics)
106 {
107  ColorPage::draw(graphics);
108 }
109 
111 {
112  setSelected(-1);
113 }
114 
116 {
117  BLOCK_START("ColorPage::adjustSize")
118  if (mListModel != nullptr)
119  {
122  2 * mPadding + 20);
123  }
124  BLOCK_END("ColorPage::adjustSize")
125 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
const ColorPair * getColorAt(const int i) const
Definition: colormodel.cpp:53
ColorPage(const Widget2 *const widget, ListModel *const listModel, const std::string &skin)
Definition: colorpage.cpp:34
void draw(Graphics *const graphics)
Definition: colorpage.cpp:55
void adjustSize()
Definition: colorpage.cpp:115
void resetAction()
Definition: colorpage.cpp:110
void safeDraw(Graphics *const graphics)
Definition: colorpage.cpp:105
unsigned int a
Definition: color.h:251
Definition: font.h:90
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
int mSelected
Definition: listbox.h:256
Color mHighlightColor
Definition: listbox.h:283
void updateAlpha()
Definition: listbox.cpp:154
unsigned int mRowHeight
Definition: listbox.h:289
virtual unsigned int getRowHeight() const
Definition: listbox.h:156
ListModel * mListModel
Definition: listbox.h:261
int mItemPadding
Definition: listbox.h:290
int mPadding
Definition: listbox.h:287
static float mAlpha
Definition: listbox.h:292
Skin * mSkin
Definition: listbox.h:291
void setSelected(const int selected)
Definition: listbox.cpp:399
virtual std::string getElementAt(int i)=0
virtual int getNumberOfElements()=0
Definition: rect.h:74
int width
Definition: rect.h:219
int getOption(const std::string &name) const
Definition: skin.h:106
Rect mDimension
Definition: widget.h:1101
void setHeight(const int height)
Definition: widget.cpp:140
Font * getFont() const
Definition: widget.cpp:331
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
const Color * color2
Definition: colorpair.h:41
const Color * color1
Definition: colorpair.h:40