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