ManaPlus
characterviewnormal.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 
23 
24 #include "configuration.h"
25 
27 
28 #include "utils/foreach.h"
29 
30 #include "debug.h"
31 
32 namespace
33 {
34  int perRowCount = 5;
35 } // namespace
36 
38  STD_VECTOR<CharacterDisplay*>
39  *const entries,
40  const int padding) :
41  CharacterViewBase(widget, padding),
42  mCharacterEntries(entries),
43  mRows(2)
44 {
45  addKeyListener(widget);
46  if (entries != nullptr)
47  {
48  FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
49  it, entries)
50  {
51  CharacterDisplay *const character = *it;
52  add(character);
53  character->setVisible(Visible_true);
54  }
55  const size_t sz = mCharacterEntries->size();
56  if (mSelected >= 0 && mSelected < CAST_S32(sz))
57  {
58  CharacterDisplay *const display = (*mCharacterEntries)[mSelected];
59  if (display != nullptr)
60  display->setSelect(false);
61  }
62  if (sz > 0)
63  {
64  mSelected = 0;
65  CharacterDisplay *const display = (*mCharacterEntries)[0];
66  display->setSelect(true);
67  setWidth(display->getWidth() * perRowCount + mPadding * 2);
68  }
69  else
70  {
71  mSelected = -1;
72  }
73  mRows = CAST_S32(sz / perRowCount);
74 
75  if (mRows * perRowCount != CAST_S32(sz))
76  mRows ++;
77  }
78 
79  setHeight((105 + config.getIntValue("fontSize")) * mRows);
80 }
81 
83 {
85 }
86 
87 void CharacterViewNormal::show(const int i)
88 {
89  const int sz = CAST_S32(mCharacterEntries->size());
90  if (i >= 0 && i < sz)
91  {
92  if (mSelected >= 0)
93  (*mCharacterEntries)[mSelected]->setSelect(false);
94  mSelected = i;
95  (*mCharacterEntries)[i]->setSelect(true);
96  }
97 }
98 
100 {
101  const size_t sz = mCharacterEntries->size();
102  if (sz == 0)
103  return;
104  const CharacterDisplay *const firtChar = (*mCharacterEntries)[0];
105  unsigned int y = 0;
106  const int width = firtChar->getWidth();
107  const int height = firtChar->getHeight();
108  int x = 0;
109  for (size_t f = 0; f < sz; f ++, x ++)
110  {
111  if (x >= perRowCount)
112  {
113  x = 0;
114  y += height;
115  }
116  (*mCharacterEntries)[f]->setPosition(x * width, y);
117  }
118 }
119 
121 {
122 }
#define CAST_S32
Definition: cast.h:30
virtual void add(Widget *const widget)
void setSelect(bool b)
CharSelectDialog * mParent
void action(const ActionEvent &event)
CharacterViewNormal(CharSelectDialog *const widget, std::vector< CharacterDisplay * > *const entries, const int padding)
std::vector< CharacterDisplay * > * mCharacterEntries
void show(const int i)
int getIntValue(const std::string &key) const
void setVisible(Visible visible)
Definition: widget.cpp:225
void setWidth(const int width)
Definition: widget.cpp:133
void removeKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:277
void setHeight(const int height)
Definition: widget.cpp:140
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Configuration config
#define FOR_EACHP(type, iter, array)
Definition: foreach.h:31
#define A_UNUSED
Definition: localconsts.h:160
const bool Visible_true
Definition: visible.h:30