ManaPlus
buyselldialog.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "actormanager.h"
27 
28 #include "net/buysellhandler.h"
29 #include "net/npchandler.h"
30 
32 
33 #include "gui/widgets/button.h"
34 
35 #include "utils/foreach.h"
36 #include "utils/gettext.h"
37 
38 #include "debug.h"
39 
41 
43  // TRANSLATORS: shop window name
44  Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
46  mNpcId(npcId),
47  mNick(""),
48  mBuyButton(nullptr)
49 {
50  init();
51 }
52 
53 BuySellDialog::BuySellDialog(const std::string &nick) :
54  // TRANSLATORS: shop window name
55  Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
58  mNick(nick),
59  mBuyButton(nullptr)
60 {
61  init();
62 }
63 
65 {
66  setWindowName("BuySell");
67  setCloseButton(true);
68 
69  if (setupWindow != nullptr)
71 
72  static const char *const buttonNames[] =
73  {
74  // TRANSLATORS: shop window button
75  N_("Buy"),
76  // TRANSLATORS: shop window button
77  N_("Sell"),
78  // TRANSLATORS: shop window button
79  N_("Cancel"),
80  nullptr
81  };
82  const int buttonPadding = getOption("buttonpadding", 10);
83  int x = buttonPadding;
84  const int y = buttonPadding;
85 
86  for (const char *const *curBtn = buttonNames;
87  *curBtn != nullptr;
88  curBtn++)
89  {
90  Button *const btn = new Button(this,
91  gettext(*curBtn),
92  *curBtn,
94  this);
95  if (mBuyButton == nullptr)
96  mBuyButton = btn; // For focus request
97  btn->setPosition(x, y);
98  add(btn);
99  x += btn->getWidth() + buttonPadding;
100  }
101  if (mBuyButton != nullptr)
102  {
105  }
106 
107  center();
108  setDefaultSize();
109  loadWindowState();
110  enableVisibleSound(true);
111 
112  dialogInstances.push_back(this);
114 }
115 
117 {
118  dialogInstances.remove(this);
119 }
120 
122 {
123  Window::setVisible(visible);
124 
125  if (visible == Visible_true)
126  {
127  if (mBuyButton != nullptr)
129  }
130  else
131  {
132  scheduleDelete();
133  }
134 }
135 
137 {
138  const std::string &eventId = event.getId();
139  if (eventId == "Buy")
140  {
141  if (mNpcId != BeingId_negOne)
142  {
143  const Being *const being = actorManager->findBeing(mNpcId);
144  if (being != nullptr)
145  npcHandler->buy(being);
146  else
148  }
149  else
150  {
152  }
153  }
154  else if (eventId == "Sell")
155  {
156  if (mNpcId != BeingId_negOne)
158  else
160  }
161 
162  close();
163 }
164 
166 {
167  FOR_EACH (DialogList::const_iterator, it, dialogInstances)
168  {
169  if (*it != nullptr)
170  (*it)->close();
171  }
172 }
ActorManager * actorManager
int BeingId
Definition: beingid.h:30
const BeingId BeingId_negOne
Definition: beingid.h:31
const std::string BUTTON_SKIN
Definition: button.h:89
Net::BuySellHandler * buySellHandler
Definition: net.cpp:100
Being * findBeing(const BeingId id) const
virtual void add(Widget *const widget)
Definition: being.h:96
Definition: button.h:102
static void closeAll()
Button * mBuyButton
Definition: buyselldialog.h:84
std::list< BuySellDialog * > DialogList
Definition: buyselldialog.h:79
BuySellDialog(const BeingId npcId)
static DialogList dialogInstances
Definition: buyselldialog.h:80
void setVisible(Visible visible)
void action(const ActionEvent &event)
BeingId mNpcId
Definition: buyselldialog.h:82
std::string mNick
Definition: buyselldialog.h:83
virtual void requestSellList(const std::string &nick) const =0
virtual void requestBuyList(const std::string &nick) const =0
virtual void buy(const Being *const being) const =0
virtual void sell(const BeingId beingId) const =0
void registerWindowForReset(Window *const window)
virtual void requestFocus()
Definition: widget.cpp:204
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
Definition: window.h:102
void center()
Definition: window.cpp:1417
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setContentSize(int width, int height)
Definition: window.cpp:492
void setWindowName(const std::string &name)
Definition: window.h:355
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1454
void enableVisibleSound(bool b)
Definition: window.h:481
void setCloseButton(const bool flag)
Definition: window.cpp:749
virtual void scheduleDelete()
Definition: window.cpp:831
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define N_(s)
Definition: gettext.h:36
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
const bool Modal_false
Definition: modal.h:30
Net::NpcHandler * npcHandler
Definition: net.cpp:93
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
bool Visible
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30