ManaPlus
extendedlistbox.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-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 
23 
25 
26 #include "gui/skin.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "render/graphics.h"
31 
32 #include "debug.h"
33 
35  ListModel *const listModel,
36  const std::string &skin,
37  const unsigned int rowHeight) :
38  ListBox(widget, listModel, skin),
39  mImagePadding(mSkin != nullptr ? mSkin->getOption("imagePadding") : 0),
40  mSpacing(mSkin != nullptr ? mSkin->getOption("spacing") : 0),
41  mHeight(0),
42  mListItems(),
43  mSelectedItems()
44 {
45  if (rowHeight != 0U)
46  mRowHeight = rowHeight;
47 }
48 
50 {
51 }
52 
53 void ExtendedListBox::draw(Graphics *const graphics)
54 {
55  if (mListModel == nullptr)
56  return;
57 
58  BLOCK_START("ExtendedListBox::draw")
59  ExtendedListModel *const model = static_cast<ExtendedListModel *>(
60  mListModel);
61 
62  updateAlpha();
63  Font *const font = getFont();
64 
65  const int height = CAST_S32(mRowHeight);
66  const int pad2 = 2 + mPadding;
67  const int width = mDimension.width;
68  int textPos = (height - font->getHeight()) / 2 + mPadding;
69  if (textPos < 0)
70  textPos = 0;
71 
72  const int sz = mListModel->getNumberOfElements();
73  mListItems.clear();
74  mSelectedItems.clear();
75  int y = 0;
76  const int insideWidth = width - pad2;
77  for (int f = 0; f < sz; f ++)
78  {
79  int row = f;
80  bool useImage = true;
81  std::string str = mListModel->getElementAt(f);
82  int strWidth = font->getWidth(str) + 8;
83 
84  const Image *const image = model->getImageAt(row);
85  if (image != nullptr)
86  strWidth += image->getWidth() + mImagePadding;
87 
88  STD_VECTOR<ExtendedListBoxItem> &list =
90 
91  if (insideWidth < strWidth)
92  {
93  const size_t strSize = str.size();
94  size_t divPos = strSize / 2;
95  if (divPos > 0 && CAST_U8(
96  str[divPos - 1]) >= 0xc0)
97  {
98  divPos --;
99  }
100  for (size_t d = divPos; d > 10; d --)
101  {
102  if (str[d] == 32)
103  {
104  divPos = d + 1;
105  break;
106  }
107  }
108  list.push_back(ExtendedListBoxItem(row,
109  str.substr(0, divPos), useImage, y));
110  str = str.substr(divPos);
111  y += height;
112  useImage = false;
113  }
114  list.push_back(ExtendedListBoxItem(row, str, useImage, y));
115 
116  y += height;
117  }
118  mHeight = y + height;
119 
120  const size_t itemsSz = mListItems.size();
121  const size_t selSz = mSelectedItems.size();
122  int minY = -1;
123  int maxY = -1;
124  for (size_t f = 0; f < selSz; f ++)
125  {
126  const ExtendedListBoxItem &item = mSelectedItems[f];
127  const int y1 = item.y;
128  if (minY == -1)
129  minY = y1;
130  if (maxY < y1)
131  maxY = y1;
132  }
133 
134  if (minY != -1)
135  {
136  mHighlightColor.a = CAST_U32(mAlpha * 255.0F);
137  graphics->setColor(mHighlightColor);
138  graphics->fillRectangle(Rect(mPadding, minY + mPadding,
139  width - pad2, maxY - minY + height));
140  }
141 
142  for (size_t f = 0; f < itemsSz; ++f)
143  {
144  const ExtendedListBoxItem &item = mListItems[f];
145  if (item.image)
146  {
147  const int row1 = item.row;
148  const Image *const image = model->getImageAt(row1);
149  if (image != nullptr)
150  {
151  graphics->drawImage(image,
153  item.y + (height - image->getHeight()) / 2 + mPadding);
154  }
155  }
156  }
157 
158  for (size_t f = 0; f < itemsSz; ++f)
159  {
160  const ExtendedListBoxItem &item = mListItems[f];
161  const int row1 = item.row;
162  const int y1 = item.y;
163  const Image *const image = model->getImageAt(row1);
164  if ((image == nullptr) || !item.image)
165  {
166  font->drawString(graphics,
168  item.text,
169  mPadding, y1 + textPos);
170  }
171  else
172  {
173  font->drawString(graphics,
175  item.text,
176  image->getWidth() + mImagePadding + mSpacing, y1 + textPos);
177  }
178  }
179 
180  for (size_t f = 0; f < selSz; ++f)
181  {
182  const ExtendedListBoxItem &item = mSelectedItems[f];
183  const int row1 = item.row;
184  const int y1 = item.y;
185  const Image *const image = model->getImageAt(row1);
186  if ((image == nullptr) || !item.image)
187  {
188  font->drawString(graphics,
190  item.text,
191  mPadding, y1 + textPos);
192  }
193  else
194  {
195  font->drawString(graphics,
197  item.text,
198  image->getWidth() + mImagePadding + mSpacing, y1 + textPos);
199  }
200  }
201 
202  for (size_t f = 0; f < selSz; ++f)
203  {
204  const ExtendedListBoxItem &item = mSelectedItems[f];
205  if (item.image)
206  {
207  const int row1 = item.row;
208  const Image *const image = model->getImageAt(row1);
209  if (image != nullptr)
210  {
211  graphics->drawImage(image,
213  item.y + (height - image->getHeight()) / 2 + mPadding);
214  }
215  }
216  }
217 
218  BLOCK_END("ExtendedListBox::draw")
219 }
220 
221 void ExtendedListBox::safeDraw(Graphics *const graphics)
222 {
223  ExtendedListBox::draw(graphics);
224 }
225 
227 {
228  if (mHeight != 0)
229  setHeight(mHeight + 2 * mPadding);
230  else
232 }
233 
235 {
236  if (mListItems.empty() && mSelectedItems.empty())
238 
239  const int height = CAST_S32(mRowHeight);
240  const size_t itemsSz = mListItems.size();
241  for (size_t f = 0; f < itemsSz; f ++)
242  {
243  const ExtendedListBoxItem &item = mListItems[f];
244  const int y2 = item.y + mPadding;
245  if (y2 <= y && y2 + height > y)
246  return item.row;
247  }
248 
249  const size_t selSz = mSelectedItems.size();
250  for (size_t f = 0; f < selSz; f ++)
251  {
252  const ExtendedListBoxItem &item = mSelectedItems[f];
253  const int y2 = item.y + mPadding;
254  if (y2 <= y && y2 + height > y)
255  return item.row;
256  }
257  return 0;
258 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
#define CAST_U8
Definition: cast.h:27
unsigned int a
Definition: color.h:251
std::vector< ExtendedListBoxItem > mListItems
void safeDraw(Graphics *const graphics)
void draw(Graphics *const graphics)
std::vector< ExtendedListBoxItem > mSelectedItems
int getSelectionByMouse(const int y) const
ExtendedListBox(const Widget2 *const widget, ListModel *const listModel, const std::string &skin, const unsigned int rowHeight)
virtual const Image * getImageAt(int i)=0
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 drawImage(const Image *const image, int dstX, int dstY)=0
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
Color mForegroundSelectedColor2
Definition: listbox.h:285
virtual void adjustSize()
Definition: listbox.cpp:374
ListModel * mListModel
Definition: listbox.h:261
int mPadding
Definition: listbox.h:287
static float mAlpha
Definition: listbox.h:292
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:392
Color mForegroundSelectedColor
Definition: listbox.h:284
virtual std::string getElementAt(int i)=0
virtual int getNumberOfElements()=0
Definition: rect.h:74
int width
Definition: rect.h:219
Color mForegroundColor2
Definition: widget2.h:113
Color mForegroundColor
Definition: widget.h:1086
Rect mDimension
Definition: widget.h:1101
void setHeight(const int height)
Definition: widget.cpp:140
Font * getFont() const
Definition: widget.cpp:331
#define nullptr
Definition: localconsts.h:45
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79