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