Mana
Loading...
Searching...
No Matches
abilitieswindow.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2009-2012 The Mana Developers
4 *
5 * This file is part of The Mana 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#include "gui/abilitieswindow.h"
22
23#include "log.h"
24
25#include "gui/setup.h"
26
27#include "gui/widgets/button.h"
29#include "gui/widgets/icon.h"
30#include "gui/widgets/label.h"
33
34#include "net/net.h"
35#include "net/abilityhandler.h"
36
37#include "resources/abilitydb.h"
38#include "resources/theme.h"
39
40#include "utils/dtor.h"
41#include "utils/gettext.h"
42
43#include "localplayer.h"
44
45#include <string>
46
47#define ABILITIES_WIDTH 200
48#define ABILITIES_HEIGHT 32
49
50class AbilityEntry : public Container
51{
52 public:
54
55 void update(int current, int needed);
56
57 protected:
58 friend class AbilitiesWindow;
60
61 private:
62 Icon *mIcon = nullptr; // icon to display
63 Label *mNameLabel = nullptr; // name to display
64 Button *mUse = nullptr; // use button (only shown when applicable)
65 ProgressBar *mRechargeBar = nullptr; // recharge bar (only shown when applicable)
66};
67
69 Window(_("Abilities"))
70{
71 setWindowName("Abilities");
72 setCloseButton(true);
73 setResizable(true);
74 setSaveVisible(true);
75 setDefaultSize(windowContainer->getWidth() - 280, 40, ABILITIES_WIDTH + 20, 225);
77
78 center();
80}
81
83{
84 // Clear gui
85}
86
87void AbilitiesWindow::action(const gcn::ActionEvent &event)
88{
89 if (event.getId() == "use")
90 {
91 auto *disp = dynamic_cast<AbilityEntry*>(event.getSource()->getParent());
92
93 if (disp)
94 {
95 if (disp->mInfo->targetMode == AbilityInfo::TARGET_BEING)
96 {
97 Being *target = local_player->getTarget();
98
99 if (target)
100 Net::getAbilityHandler()->useOn(disp->mInfo->id, target->getId());
101 else
102 Net::getAbilityHandler()->use(disp->mInfo->id);
103 }
104 else
105 {
106 // TODO: Allow the player to aim at a position on the map and
107 // Use abilities on the map position.
108 }
109 }
110 }
111 else if (event.getId() == "close")
112 {
113 setVisible(false);
114 }
115}
116
117void AbilitiesWindow::draw(gcn::Graphics *graphics)
118{
119 // update the progress bars
120 const std::map<int, Ability> &abilityData = PlayerInfo::getAbilityStatus();
121 bool foundNew = false;
122 unsigned int found = 0; // number of entries in abilityData which match mEntries
123
124 for (auto &[id, ability] : abilityData)
125 {
126 auto e = mEntries.find(id);
127 if (e == mEntries.end())
128 {
129 // found a new ability - abort update and rebuild from scratch
130 foundNew = true;
131 break;
132 }
133
134 // update progress bar of ability
135 e->second->update(ability.currentMana, ability.neededMana);
136 found++;
137 }
138 // a rebuild is needed when a) the number of abilities changed or b) an existing entry isn't found anymore
139 if (foundNew || found != mEntries.size())
140 rebuild(abilityData);
141
143}
144
145void AbilitiesWindow::rebuild(const std::map<int, Ability> &abilityData)
146{
148
149 mEntries.clear();
150 int vPos = 0; //vertical position of next placed element
151
152 for (auto &[id, ability] : abilityData)
153 {
154 Log::info("Updating ability GUI for %d", id);
155
156 AbilityInfo *info = AbilityDB::get(id);
157 if (info)
158 {
159 info->rechargeCurrent = ability.currentMana;
160 info->rechargeNeeded = ability.neededMana;
161 auto *entry = new AbilityEntry(info);
162 entry->setPosition(0, vPos);
163 vPos += entry->getHeight() + 3;
164 add(entry);
165 mEntries[id] = entry;
166 }
167 else
168 {
169 Log::warn("No info available of ability %d", id);
170 }
171 }
172}
173
174
176 mInfo(info)
177{
179
180 if (!info->icon.empty())
181 mIcon = new Icon(info->icon);
182 else
183 mIcon = new Icon(Theme::getImageFromTheme("unknown-item.png"));
184
185 mIcon->setPosition(1, 0);
186 add(mIcon);
187
188 mNameLabel = new Label(info->name);
189 mNameLabel->setPosition(35, 0);
190 add(mNameLabel);
191
192 mUse = new Button("Use", "use", abilitiesWindow);
193 mUse->setPosition(getWidth() - mUse->getWidth(), 5);
194 add(mUse);
195
196 if (info->rechargeable)
197 {
198 float progress = (float)info->rechargeCurrent / (float)info->rechargeNeeded;
199 mRechargeBar = new ProgressBar(progress, 100, 10, Theme::PROG_MP);
201 mRechargeBar->setPosition(mNameLabel->getX(), 18);
202 add(mRechargeBar);
203 }
204}
205
206void AbilityEntry::update(int current, int needed)
207{
208 if (mRechargeBar)
209 {
210 float progress = (float)current / (float)needed;
211 mRechargeBar->setProgress(progress);
212 }
213}
#define ABILITIES_WIDTH
#define ABILITIES_HEIGHT
void rebuild(const std::map< int, Ability > &abilityData)
void action(const gcn::ActionEvent &actionEvent) override
Called when receiving actions from widget.
std::map< int, AbilityEntry * > mEntries
~AbilitiesWindow() override
void draw(gcn::Graphics *graphics) override
AbilityEntry(AbilityInfo *info)
void update(int current, int needed)
ProgressBar * mRechargeBar
AbilityInfo * mInfo
int getId() const
Definition actorsprite.h:63
Definition being.h:65
Button widget.
Definition button.h:38
A widget container.
Definition container.h:41
An icon.
Definition icon.h:36
Label widget.
Definition label.h:34
Being * getTarget() const
Returns the current target of the player.
virtual void use(int id)=0
virtual void useOn(int id, int beingId)=0
A progress bar.
Definition progressbar.h:34
void setProgress(float progress)
Sets the current progress.
void setSmoothProgress(bool smoothProgress)
Set whether the progress is moved smoothly.
Definition progressbar.h:94
void registerWindowForReset(Window *window)
Enables the Reset Windows button.
Definition setup.cpp:108
static ResourceRef< Image > getImageFromTheme(const std::string &path)
Definition theme.cpp:308
@ PROG_MP
Definition theme.h:271
A window.
Definition window.h:59
void draw(gcn::Graphics *graphics) override
Draws the window contents.
Definition window.cpp:103
void center()
Positions the window in the center of it's parent.
Definition window.cpp:768
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
void setResizable(bool resize)
Sets whether or not the window can be resized.
Definition window.cpp:212
void setSaveVisible(bool save)
Sets whether the window will save it's visibility.
Definition window.h:225
void setCloseButton(bool flag)
Sets whether or not the window has a close button.
Definition window.cpp:262
void setDefaultSize()
Set the default win pos and size to the current ones.
Definition window.cpp:545
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
Definition window.cpp:467
Graphics * graphics
Definition client.cpp:104
void delete_all(Container &c)
Definition dtor.h:46
AbilitiesWindow * abilitiesWindow
Definition game.cpp:107
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
AbilityInfo * get(int id)
Gets the ability info for ID.
Definition abilitydb.cpp:89
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
AbilityHandler * getAbilityHandler()
Definition net.cpp:115
const std::map< int, Ability > & getAbilityStatus()
Returns the status all abilities.
Setup * setupWindow
Definition setup.cpp:120
WindowContainer * windowContainer