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