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