Mana
Loading...
Searching...
No Matches
popupmenu.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana 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
22#include "gui/popupmenu.h"
23
24#include "actorspritemanager.h"
25#include "being.h"
26#include "flooritem.h"
27#include "graphics.h"
28#include "item.h"
29#include "localplayer.h"
30#include "log.h"
31#include "playerinfo.h"
32#include "playerrelations.h"
33
34#include "gui/chatwindow.h"
35#include "gui/equipmentwindow.h"
36#include "gui/inventorywindow.h"
38
40
41#include "net/adminhandler.h"
43#include "net/net.h"
44#include "net/partyhandler.h"
45#include "net/tradehandler.h"
46
47#include "resources/itemdb.h"
48#include "resources/iteminfo.h"
49
50#include "utils/gettext.h"
51#include "utils/stringutils.h"
52
53#include <cassert>
54
55std::string tradePartnerName;
56
65
66void PopupMenu::showPopup(int x, int y, Being *being)
67{
68 mBeingId = being->getId();
70
71 const std::string &name = being->getName();
72
73 switch (being->getType())
74 {
76 {
77 // Players can be traded with.
78 mBrowserBox->addRow(strprintf("@@trade|%s@@",
79 strprintf(_("Trade with %s..."),
80 name.c_str()).c_str()));
81 // TRANSLATORS: Attacking a player.
82 mBrowserBox->addRow(strprintf("@@attack|%s@@",
83 strprintf(_("Attack %s"),
84 name.c_str()).c_str()));
85 // TRANSLATORS: Whispering a player.
86 mBrowserBox->addRow(strprintf("@@whisper|%s@@",
87 strprintf(_("Whisper %s"),
88 name.c_str()).c_str()));
89
90 mBrowserBox->addRow("##3---");
91
92 switch (player_relations.getRelation(name))
93 {
95 mBrowserBox->addRow(strprintf("@@friend|%s@@",
96 strprintf(_("Befriend %s"),
97 name.c_str()).c_str()));
98
100 mBrowserBox->addRow(strprintf("@@disregard|%s@@",
101 strprintf(_("Disregard %s"),
102 name.c_str()).c_str()));
103 mBrowserBox->addRow(strprintf("@@ignore|%s@@",
104 strprintf(_("Ignore %s"),
105 name.c_str()).c_str()));
106 break;
107
109 mBrowserBox->addRow(strprintf("@@unignore|%s@@",
110 strprintf(_("Unignore %s"),
111 name.c_str()).c_str()));
112 mBrowserBox->addRow(strprintf("@@ignore|%s@@",
113 strprintf(_("Completely ignore %s"),
114 name.c_str()).c_str()));
115 break;
116
118 mBrowserBox->addRow(strprintf("@@unignore|%s@@",
119 strprintf(_("Unignore %s"),
120 name.c_str()).c_str()));
121 break;
122 }
123
125 mBrowserBox->addRow(strprintf("@@guild|%s@@",
126 strprintf(_("Invite %s to join your guild"),
127 name.c_str()).c_str()));
128 if (local_player->isInParty() ||
130 {
131 mBrowserBox->addRow(strprintf("@@party|%s@@",
132 strprintf(_("Invite %s to join your party"),
133 name.c_str()).c_str()));
134 }
135
136 if (local_player->isGM())
137 {
138 mBrowserBox->addRow("##3---");
139 mBrowserBox->addRow(strprintf("@@admin-kick|%s@@",
140 _("Kick player")));
141 }
142 }
143 break;
144
145 case ActorSprite::NPC:
146 // NPCs can be talked to (single option, candidate for removal
147 // unless more options would be added)
148 mBrowserBox->addRow(strprintf("@@talk|%s@@",
149 strprintf(_("Talk to %s"),
150 name.c_str()).c_str()));
151 break;
152
154 {
155 // Monsters can be attacked
156 mBrowserBox->addRow(strprintf("@@attack|%s@@",
157 strprintf(_("Attack %s"),
158 name.c_str()).c_str()));
159
160 if (local_player->isGM())
161 mBrowserBox->addRow(strprintf("@@admin-kick|%s@@",
162 _("Kick monster")));
163 }
164 break;
165
166 default:
167 /* Other beings aren't interesting... */
168 return;
169 }
170 mBrowserBox->addRow(strprintf("@@name|%s@@", _("Add name to chat")));
171
172 //mBrowserBox->addRow(strprintf("@@look|%s@@", _("Look To")));
173 mBrowserBox->addRow("##3---");
174 mBrowserBox->addRow(strprintf("@@cancel|%s@@", _("Cancel")));
175
176 showPopup(x, y);
177}
178
179void PopupMenu::showPopup(int x, int y, FloorItem *floorItem)
180{
181 mFloorItem = floorItem;
183
184 // Floor item can be picked up (single option, candidate for removal)
185 const std::string &name = floorItem->getInfo().name;
186 mBrowserBox->addRow(strprintf("@@pickup|%s@@", strprintf(_("Pick up %s"),
187 name.c_str()).c_str()));
188 mBrowserBox->addRow(strprintf("@@chat|%s@@", _("Add to chat")));
189
190 //mBrowserBox->addRow(strprintf("@@look|%s@@", _("Look To")));
191 mBrowserBox->addRow("##3---");
192 mBrowserBox->addRow(strprintf("@@cancel|%s@@", _("Cancel")));
193
194 showPopup(x, y);
195}
196
197void PopupMenu::handleLink(const std::string &link)
198{
200
201 // Talk To action
202 if (link == "talk" && being && being->canTalk())
203 {
204 being->talkTo();
205 }
206
207 // Trade action
208 else if (link == "trade" && being &&
209 being->getType() == ActorSprite::PLAYER)
210 {
212 tradePartnerName = being->getName();
213 }
214 // Attack action
215 else if (link == "attack" && being)
216 {
217 local_player->attack(being, true);
218 }
219 else if (link == "whisper" && being)
220 {
221 chatWindow->addInputText("/w \"" + being->getName() + "\" ");
222 }
223 else if (link == "unignore" && being &&
224 being->getType() == ActorSprite::PLAYER)
225 {
227 }
228
229 else if (link == "ignore" && being &&
230 being->getType() == ActorSprite::PLAYER)
231 {
233 }
234
235 else if (link == "disregard" && being &&
236 being->getType() == ActorSprite::PLAYER)
237 {
239 }
240
241 else if (link == "friend" && being &&
242 being->getType() == ActorSprite::PLAYER)
243 {
245 }
246 // Guild action
247 else if (link == "guild" && being &&
248 being->getType() == ActorSprite::PLAYER)
249 {
251 }
252 // Pick Up Floor Item action
253 else if ((link == "pickup") && mFloorItem)
254 {
256 }
257 // Look To action
258 else if (link == "look")
259 {
260 }
261 else if (link == "activate" || link == "equip" || link == "unequip")
262 {
263 assert(mItem);
264 if (mItem->isEquippable())
265 {
266 if (mItem->isEquipped())
267 {
270 }
271 else
272 {
274 }
275 }
276 else
277 {
279 }
280 }
281 else if (link == "chat")
282 {
283 if (mItem)
285 else if (mFloorItem)
287 }
288 else if (link == "drop")
289 {
292 }
293 else if (link == "store")
294 {
297 }
298 else if (link == "retrieve")
299 {
301 mItem);
302 }
303 else if (link == "party" && being &&
304 being->getType() == ActorSprite::PLAYER)
305 {
307 }
308 else if (link == "name" && being)
309 {
310 const std::string &name = being->getName();
312 }
313 else if (link == "admin-kick" && being &&
314 being->getType() == ActorSprite::PLAYER)
315 {
316 Net::getAdminHandler()->kick(being->getName());
317 }
318 // Unknown actions
319 else if (link != "cancel")
320 {
321 Log::info("PopupMenu: Warning, unknown action '%s'", link.c_str());
322 }
323
324 setVisible(false);
325
326 mBeingId = 0;
327 mFloorItem = nullptr;
328 mItem = nullptr;
329}
330
331void PopupMenu::showPopup(Window *parent, int x, int y, Item *item,
332 bool isInventory, bool canDrop)
333{
334 assert(item);
335 mItem = item;
336 mWindow = parent;
338
339 if (isInventory)
340 {
342 {
343 mBrowserBox->addRow(strprintf("@@store|%s@@", _("Store")));
344 }
345
346 if (item->isEquippable())
347 {
348 if (item->isEquipped())
349 mBrowserBox->addRow(strprintf("@@unequip|%s@@", _("Unequip")));
350 else
351 mBrowserBox->addRow(strprintf("@@equip|%s@@", _("Equip")));
352 }
353 if (item->getInfo().activatable)
354 mBrowserBox->addRow(strprintf("@@activate|%s@@", _("Activate")));
355
356 if (canDrop)
357 {
358 if (item->getQuantity() > 1)
359 mBrowserBox->addRow(strprintf("@@drop|%s@@", _("Drop...")));
360 else
361 mBrowserBox->addRow(strprintf("@@drop|%s@@", _("Drop")));
362 }
363 }
364 // Assume in storage for now
365 // TODO: make this whole system more flexible, if needed
366 else
367 {
368 mBrowserBox->addRow(strprintf("@@retrieve|%s@@", _("Retrieve")));
369 }
370 mBrowserBox->addRow(strprintf("@@chat|%s@@", _("Add to chat")));
371 mBrowserBox->addRow("##3---");
372 mBrowserBox->addRow(strprintf("@@cancel|%s@@", _("Cancel")));
373
374 showPopup(x, y);
375}
376
377void PopupMenu::showPopup(int x, int y)
378{
379 setContentSize(mBrowserBox->getWidth(), mBrowserBox->getHeight());
380 if (graphics->getWidth() < (x + getWidth() + 5))
381 x = graphics->getWidth() - getWidth();
382 if (graphics->getHeight() < (y + getHeight() + 5))
383 y = graphics->getHeight() - getHeight();
384 setPosition(x, y);
385 setVisible(true);
386 requestMoveToTop();
387}
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
Being * findBeing(int id) const
Returns a specific Being, by id;.
int getId() const
Definition actorsprite.h:63
Definition being.h:65
Type getType() const final
Returns the type of the ActorSprite.
Definition being.h:122
bool isGM() const
Whether or not this player is a GM.
Definition being.h:417
void talkTo()
Definition being.cpp:1360
bool isInParty() const
Definition being.h:257
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
short getNumberOfGuilds() const
Get number of guilds the being belongs to.
Definition being.h:254
bool canTalk()
Definition being.cpp:1355
A simple browser box able to handle links and forward events to the parent conteiner.
Definition browserbox.h:74
void setHighlightMode(unsigned int mode)
Sets the Highlight mode for links.
Definition browserbox.h:95
void addRow(std::string_view row)
Adds a text row to the browser.
void setLinkHandler(LinkHandler *handler)
Sets the handler for links.
Definition browserbox.h:88
void clearRows()
Remove all rows.
void addItemText(const std::string &item)
Called to add item to chat.
void addInputText(const std::string &text)
Add the given text to the chat input.
int getSelected() const
Returns the current selected slot or -1 if none.
void triggerUnequip(int slotIndex) const
Definition equipment.h:62
@ DoUse
Definition event.h:79
@ DoEquip
Definition event.h:76
An item lying on the floor.
Definition flooritem.h:32
const ItemInfo & getInfo() const
Returns the item info for this floor item.
Definition flooritem.cpp:76
int getHeight() const
Returns the logical height of the screen.
Definition graphics.h:226
int getWidth() const
Returns the logical width of the screen.
Definition graphics.h:221
static void showWindow(Usage usage, Window *parent, Item *item, int maxRange=0)
Creates the dialog, or bypass it if there aren't enough items.
std::string name
Definition iteminfo.h:113
bool activatable
Whether this item can be activated.
Definition iteminfo.h:134
Represents one or more instances of a certain item type.
Definition item.h:35
void doEvent(Event::Type eventType)
Definition item.cpp:54
bool isEquipped() const
Returns whether this item is equipped.
Definition item.h:79
int getQuantity() const
Returns the number of items.
Definition item.h:69
bool isEquippable() const
Returns whether this item is equippable.
Definition item.cpp:69
const ItemInfo & getInfo() const
Returns information about this item type.
Definition item.h:103
void inviteToGuild(Being *being)
Invite a player to join guild.
void pickUp(FloorItem *item)
void attack(Being *target=nullptr, bool keep=false)
virtual void kick(const std::string &name)=0
virtual void invite(Being *player)=0
virtual void request(Being *being)
PlayerRelation getRelation(const std::string &name) const
Updates the relationship with this player.
void setRelation(const std::string &name, PlayerRelation relation)
Updates the relationship with this player.
Item * mItem
Definition popupmenu.h:69
int mBeingId
Definition popupmenu.h:67
void handleLink(const std::string &link) override
Handles link action.
void showPopup(int x, int y, Being *being)
Shows the being related popup menu at the specified mouse coords.
Definition popupmenu.cpp:66
BrowserBox * mBrowserBox
Definition popupmenu.h:65
Window * mWindow
Definition popupmenu.h:71
FloorItem * mFloorItem
Definition popupmenu.h:68
A light version of the Window class.
Definition popup.h:48
void add(gcn::Widget *widget) override
Definition popup.cpp:69
void setContentSize(int width, int height)
Sets the size of this popup.
Definition popup.cpp:127
A window.
Definition window.h:59
Graphics * graphics
Definition client.cpp:104
InventoryWindow * inventoryWindow
Definition game.cpp:96
ChatWindow * chatWindow
Definition game.cpp:93
EquipmentWindow * equipmentWindow
Definition game.cpp:100
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
void info(const char *log_text,...) LOG_PRINTF_ATTR
ServerType getNetworkType()
Definition net.cpp:200
TradeHandler * getTradeHandler()
Definition net.cpp:120
AdminHandler * getAdminHandler()
Definition net.cpp:60
PartyHandler * getPartyHandler()
Definition net.cpp:105
int getStorageCount()
Returns the number of currently open storage windows.
Equipment * getEquipment()
Returns the player's equipment.
PlayerRelationsManager player_relations
std::string tradePartnerName
Definition popupmenu.cpp:55
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.