ManaPlus
characterviewsmall.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 "gui/widgets/button.h"
25 #include "gui/widgets/label.h"
26 
27 #include "utils/foreach.h"
28 #include "utils/stringutils.h"
29 
30 #include "debug.h"
31 
33  STD_VECTOR<CharacterDisplay*>
34  *const entries,
35  const int padding) :
36  CharacterViewBase(widget, padding),
37  mSelectedEntry(nullptr),
38  mPrevious(new Button(this, "<", "prev", this)),
39  mNext(new Button(this, ">", "next", this)),
40  mNumber(new Label(this, "??")),
41  mCharacterEntries(entries)
42 {
43  addKeyListener(widget);
44  if (entries != nullptr)
45  {
46  FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
47  it, entries)
48  {
49  add(*it);
50  }
51  const int sz = CAST_S32(mCharacterEntries->size());
52  if (sz > 0)
53  {
54  mSelected = 0;
55  mSelectedEntry = (*mCharacterEntries)[mSelected];
57  mNumber->setCaption(strprintf("%d / %d", mSelected + 1, sz));
59  }
60  else
61  {
62  mSelected = -1;
63  mSelectedEntry = nullptr;
64  mNumber->setCaption("0 / 0");
66  }
67  }
68  add(mPrevious);
69  add(mNext);
70  add(mNumber);
71 
72  setHeight(200);
73 }
74 
76 {
78 }
79 
80 void CharacterViewSmall::show(const int i)
81 {
82  const int sz = CAST_S32(mCharacterEntries->size());
83  if (sz <= 0)
84  return;
85  if (mSelectedEntry != nullptr)
87  if (i >= sz)
88  mSelected = 0;
89  else if (i < 0)
90  mSelected = sz - 1;
91  else
92  mSelected = i;
93  mSelectedEntry = (*mCharacterEntries)[mSelected];
95  mNumber->setCaption(strprintf("%d / %d", mSelected + 1, sz));
97 }
98 
100 {
101  const int sz = CAST_S32(mCharacterEntries->size());
102  if (sz <= 0)
103  return;
104  const CharacterDisplay *const firtChar = (*mCharacterEntries)[0];
105  const int w = mDimension.width;
106  const int h = mDimension.height;
107  const int x = (w - firtChar->getWidth()) / 2;
108  const int y = (h - firtChar->getHeight()) / 2;
109  FOR_EACHP (STD_VECTOR<CharacterDisplay*>::iterator,
110  it, mCharacterEntries)
111  {
112  (*it)->setPosition(x, y);
113  }
114  const int y2 = (h - mPrevious->getHeight()) / 2;
115  const int y3 = y2 - 55;
116  mPrevious->setPosition(x - mPrevious->getWidth() - 10, y3);
117  mNext->setPosition(w - x + 10, y3);
118  mNumber->setPosition(10, y2);
119 }
120 
122 {
123  const std::string &eventId = event.getId();
124  if (eventId == "next")
125  {
126  mSelected ++;
127  show(mSelected);
128  mParent->updateState();
129  }
130  else if (eventId == "prev")
131  {
132  mSelected --;
133  show(mSelected);
134  mParent->updateState();
135  }
136 }
int width
Definition: rect.h:218
#define FOR_EACHP(type, iter, array)
Definition: foreach.h:30
int getWidth() const
Definition: widget.h:220
const bool Visible_true
Definition: visible.h:29
void setVisible(Visible visible)
Definition: widget.cpp:224
void action(const ActionEvent &event)
Definition: button.h:94
CharacterViewSmall(CharSelectDialog *const widget, std::vector< CharacterDisplay *> *const entries, const int padding)
void removeKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:276
CharSelectDialog * mParent
#define new
Definition: debug_new.h:147
#define CAST_S32
Definition: cast.h:29
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
void setCaption(const std::string &caption)
Definition: label.cpp:261
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
void show(const int i)
std::vector< CharacterDisplay * > * mCharacterEntries
virtual void add(Widget *const widget)
#define nullptr
Definition: localconsts.h:44
CharacterDisplay * mSelectedEntry
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:271
void setHeight(const int height)
Definition: widget.cpp:139
int getHeight() const
Definition: widget.h:239
Rect mDimension
Definition: widget.h:1100
void adjustSize()
Definition: label.cpp:197