ManaPlus
socialnavigationtab.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-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 
21 #ifndef GUI_WIDGETS_TABS_SOCIALNAVIGATIONTAB_H
22 #define GUI_WIDGETS_TABS_SOCIALNAVIGATIONTAB_H
23 
25 
26 #include "actormanager.h"
27 #include "configuration.h"
28 
30 
32 
33 #include "being/localplayer.h"
34 
35 #include "input/keyboardconfig.h"
36 
37 #include "utils/delete2.h"
38 #include "utils/gettext.h"
39 
40 #include "resources/map/map.h"
41 #include "resources/map/mapitem.h"
43 
44 #include "localconsts.h"
45 
46 class Avatar;
47 
49 {
50  public:
51  SocialNavigationTab(const Widget2 *const widget,
52  const Opaque showBackground) :
53  SocialTab(widget),
55  {
56  createControls(mBeings, showBackground);
57 
58  // TRANSLATORS: Navigation tab name in social window.
59  // TRANSLATORS: Should be small
60  setCaption(_("Nav"));
61  mMenuAction = "navigation";
62  }
63 
65 
67  {
68  delete2(mList)
71  }
72 
74  {
75  if ((socialWindow == nullptr) || (localPlayer == nullptr))
76  return;
77 
78  const Map *const map = socialWindow->getMap();
79  if ((map == nullptr) || map->empty())
80  return;
81 
83  return;
84 
85  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
86  STD_VECTOR<MapItem*> portals = map->getPortals();
87 
88  STD_VECTOR<MapItem*>::const_iterator i = portals.begin();
89  const SpecialLayer *const specialLayer = map->getSpecialLayer();
90 
91  STD_VECTOR<Avatar*>::iterator ia = avatars->begin();
92 
93  while (ia != avatars->end())
94  {
95  delete *ia;
96  ++ ia;
97  }
98 
99  avatars->clear();
100 
101  int online = 0;
102  int total = 0;
103 
104  int idx = 0;
105  while (i != portals.end())
106  {
107  MapItem *portal = *i;
108  if (portal == nullptr)
109  continue;
110 
111  const int x = portal->getX();
112  const int y = portal->getY();
113 
114  const std::string name = strprintf("%s [%d %d]",
115  portal->getComment().c_str(), x, y);
116 
117  Avatar *const ava = new Avatar(name);
118  if (localPlayer != nullptr)
119  ava->setOnline(localPlayer->isReachable(x, y, true));
120  else
121  ava->setOnline(false);
122  ava->setLevel(-1);
123  ava->setType(portal->getType());
124  ava->setX(x);
125  ava->setY(y);
126  avatars->push_back(ava);
127 
128  if (ava->getOnline())
129  online ++;
130  total ++;
131 
132  if (config.getBoolValue("drawHotKeys")
133  && idx < 80 && (outfitWindow != nullptr))
134  {
135  Being *const being = actorManager
136  ->findPortalByTile(x, y);
137  if (being != nullptr)
138  {
140  OutfitWindow::keyName(idx)));
141  }
142 
143  if (specialLayer != nullptr)
144  {
145  portal = specialLayer->getTile(
146  ava->getX(), ava->getY());
147  if (portal != nullptr)
148  {
150  OutfitWindow::keyName(idx)));
151  }
152  }
153  }
154 
155  ++i;
156  idx ++;
157  }
158  if (socialWindow != nullptr)
160 
161  // TRANSLATORS: social window label
162  mCounterString = strprintf(_("Portals: %u/%u"),
163  CAST_U32(online),
164  CAST_U32(total));
165  updateCounter();
166  }
167 
168  void selectIndex(const unsigned num) override final
169  {
170  if (localPlayer == nullptr)
171  return;
172 
173  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
174  if (avatars->size() <= CAST_SIZE(num))
175  return;
176 
177  const Avatar *const ava = avatars->at(num);
178  if (ava != nullptr)
179  localPlayer->navigateTo(ava->getX(), ava->getY());
180  }
181 
182  void updateNames()
183  {
184  if (socialWindow == nullptr)
185  return;
186 
187  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
188 
189  const Map *const map = socialWindow->getMap();
190  if (map == nullptr)
191  return;
192 
193  STD_VECTOR<Avatar*>::const_iterator i = avatars->begin();
194  const STD_VECTOR<Avatar*>::const_iterator i_end = avatars->end();
195  while (i != i_end)
196  {
197  Avatar *const ava = *i;
198  if (ava == nullptr)
199  break;
200 
201  const MapItem *const item = map->findPortalXY(
202  ava->getX(), ava->getY());
203  if (item != nullptr)
204  {
205  const std::string name = strprintf("%s [%d %d]",
206  item->getComment().c_str(),
207  item->getX(), item->getY());
208  ava->setName(name);
209  ava->setOriginalName(name);
210  }
211 
212  ++i;
213  }
214  }
215 
216  int getPortalIndex(const int x, const int y)
217  {
218  if (socialWindow == nullptr)
219  return -1;
220 
221  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
222  const Map *const map = socialWindow->getMap();
223  if (map == nullptr)
224  return -1;
225 
226  STD_VECTOR<Avatar*>::const_iterator i = avatars->begin();
227  const STD_VECTOR<Avatar*>::const_iterator i_end = avatars->end();
228  unsigned num = 0;
229  while (i != i_end)
230  {
231  const Avatar *const ava = *i;
232  if (ava == nullptr)
233  break;
234 
235  if (ava->getX() == x && ava->getY() == y)
236  return num;
237 
238  ++i;
239  num ++;
240  }
241  return -1;
242  }
243 
244  void addPortal(const int x, const int y)
245  {
246  if ((socialWindow == nullptr) || (localPlayer == nullptr))
247  return;
248 
249  const Map *const map = socialWindow->getMap();
250  if (map == nullptr)
251  return;
252 
253  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
254  const MapItem *const portal = map->findPortalXY(x, y);
255  if (portal == nullptr)
256  return;
257 
258  const std::string name = strprintf("%s [%d %d]",
259  portal->getComment().c_str(), x, y);
260 
261  Avatar *const ava = new Avatar(name);
262  if (localPlayer != nullptr)
263  ava->setOnline(localPlayer->isReachable(x, y, true));
264  else
265  ava->setOnline(false);
266  ava->setLevel(-1);
267  ava->setType(portal->getType());
268  ava->setX(x);
269  ava->setY(y);
270  avatars->push_back(ava);
271  }
272 
273  void removePortal(const int x, const int y)
274  {
275  if ((socialWindow == nullptr) || (localPlayer == nullptr))
276  return;
277 
278  const Map *const map = socialWindow->getMap();
279  if (map == nullptr)
280  return;
281 
282  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
283  STD_VECTOR<Avatar*>::iterator i = avatars->begin();
284  const STD_VECTOR<Avatar*>::iterator i_end = avatars->end();
285 
286  while (i != i_end)
287  {
288  Avatar *ava = (*i);
289 
290  if (ava == nullptr)
291  break;
292 
293  if (ava->getX() == x && ava->getY() == y)
294  {
295  delete ava;
296  avatars->erase(i);
297  return;
298  }
299 
300  ++ i;
301  }
302  }
303 
304  private:
306 };
307 
308 #endif // GUI_WIDGETS_TABS_SOCIALNAVIGATIONTAB_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
#define CAST_U32
Definition: cast.h:30
void setProcessedPortals(const bool n)
Definition: socialwindow.h:109
#define _(s)
Definition: gettext.h:34
bool empty() const
Definition: map.h:320
void setCaption(const std::string &caption)
Definition: tab.cpp:457
void setOriginalName(const std::string &name)
Definition: avatar.h:74
int getY() const
Definition: mapitem.h:64
OutfitWindow * outfitWindow
const std::string & getComment() const
Definition: mapitem.h:67
ScrollArea * mScroll
Definition: socialtab.h:137
Configuration config
#define final
Definition: localconsts.h:45
std::string mCounterString
Definition: socialtab.h:139
void selectIndex(const unsigned num)
void setName(const std::string &name)
Definition: mapitem.h:76
const std::vector< MapItem * > & getPortals() const
Definition: map.h:270
#define delete2(var)
Definition: delete2.h:24
void createControls(AvatarListModel *const listModel, const Opaque showBackground)
Definition: socialtab.h:106
bool getProcessedPortals() const
Definition: socialwindow.h:106
#define new
Definition: debug_new.h:147
static std::string getKeyShortString(const std::string &key)
bool getBoolValue(const std::string &key) const
void addPortal(const int x, const int y)
bool isReachable(Being *const being, const int maxCost)
int getType() const
Definition: mapitem.h:54
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
int getX() const
Definition: mapitem.h:61
SocialNavigationTab(const Widget2 *const widget, const Opaque showBackground)
BeingsListModel * mBeings
std::string mMenuAction
Definition: socialtab.h:140
LocalPlayer * localPlayer
SpecialLayer * getSpecialLayer() const
Definition: map.h:240
MapItem * getTile(const int x, const int y) const
std::vector< Avatar * > * getMembers()
const Map * getMap() const
Definition: socialwindow.h:100
AvatarListBox * mList
Definition: socialtab.h:138
int getX() const
Definition: avatar.h:125
void setName(const std::string &name)
Definition: avatar.h:58
bool navigateTo(const int x, const int y)
SocialWindow * socialWindow
MapItem * findPortalXY(const int x, const int y) const
Definition: map.cpp:1333
Definition: avatar.h:39
Definition: map.h:71
int getPortalIndex(const int x, const int y)
static std::string keyName(const int number)
void removePortal(const int x, const int y)
void setName(const std::string &name)
Definition: being.cpp:1135
#define CAST_SIZE
Definition: cast.h:33
#define override
Definition: localconsts.h:46
Definition: being.h:93
void updateCounter() const
Definition: socialtab.h:123
Being * findPortalByTile(const int x, const int y) const
ActorManager * actorManager
int getY() const
Definition: avatar.h:131