GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/socialnavigationtab.h Lines: 13 138 9.4 %
Date: 2017-11-29 Branches: 7 144 4.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  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
24
#include "gui/widgets/tabs/socialtab.h"
25
26
#include "actormanager.h"
27
#include "configuration.h"
28
29
#include "gui/models/beingslistmodel.h"
30
31
#include "gui/windows/outfitwindow.h"
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"
42
#include "resources/map/speciallayer.h"
43
44
#include "localconsts.h"
45
46
class Avatar;
47
48
class SocialNavigationTab final : public SocialTab
49
{
50
    public:
51
2
        SocialNavigationTab(const Widget2 *const widget,
52
2
                            const Opaque showBackground) :
53
            SocialTab(widget),
54
4
            mBeings(new BeingsListModel)
55
        {
56
2
            createControls(mBeings, showBackground);
57
58
            // TRANSLATORS: Navigation tab name in social window.
59
            // TRANSLATORS: Should be small
60

8
            setCaption(_("Nav"));
61
4
            mMenuAction = "navigation";
62
2
        }
63
64
        A_DELETE_COPY(SocialNavigationTab)
65
66
4
        ~SocialNavigationTab()
67
4
        {
68
2
            delete2(mList)
69
2
            delete2(mScroll)
70
4
            delete2(mBeings)
71
4
        }
72
73
        void updateList() override final
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
82
            if (socialWindow->getProcessedPortals())
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
                    {
139
                        being->setName(KeyboardConfig::getKeyShortString(
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
                        {
149
                            portal->setName(KeyboardConfig::getKeyShortString(
150
                                OutfitWindow::keyName(idx)));
151
                        }
152
                    }
153
                }
154
155
                ++i;
156
                idx ++;
157
            }
158
            if (socialWindow != nullptr)
159
                socialWindow->setProcessedPortals(true);
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) && (localPlayer != 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:
305
        BeingsListModel *mBeings;
306
};
307
308
#endif  // GUI_WIDGETS_TABS_SOCIALNAVIGATIONTAB_H