]> git.localhorst.tv Git - l2e.git/blob - src/menu/InventoryMenu.cpp
a2881719f717caff55f0f6d4ca315846ec228543
[l2e.git] / src / menu / InventoryMenu.cpp
1 #include "InventoryMenu.h"
2
3 #include "PartyMenu.h"
4 #include "Resources.h"
5 #include "../app/Application.h"
6 #include "../app/Input.h"
7 #include "../common/GameConfig.h"
8 #include "../common/GameState.h"
9 #include "../common/Inventory.h"
10 #include "../common/Item.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
13 #include "../math/Vector.h"
14
15 using app::Input;
16 using common::Inventory;
17 using common::Item;
18 using math::Vector;
19 using graphics::Font;
20 using graphics::Frame;
21 using std::swap;
22
23 namespace menu {
24
25 InventoryMenu::InventoryMenu(PartyMenu *parent)
26 : parent(parent)
27 , menu(*parent->Res().itemMenuProperties)
28 , itemMenu(*parent->Res().inventoryMenuProperties) {
29         menu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
30         menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
31         menu.Add(parent->Res().itemMenuDropText, CHOICE_DROP);
32 }
33
34
35 void InventoryMenu::OnEnterState(SDL_Surface *) {
36         menu.SetSelected();
37         LoadInventory();
38 }
39
40 void InventoryMenu::LoadInventory() {
41         const Inventory &inv(parent->Game().state->inventory);
42         itemMenu.Clear();
43         itemMenu.Reserve(inv.MaxItems());
44         for (int i(0); i < inv.MaxItems(); ++i) {
45                 const Item *item(inv.ItemAt(i));
46                 if (item) {
47                         itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
48                 } else {
49                         itemMenu.AddEmptyEntry();
50                 }
51         }
52 }
53
54 void InventoryMenu::OnExitState(SDL_Surface *) {
55
56 }
57
58 void InventoryMenu::OnResumeState(SDL_Surface *) {
59
60 }
61
62 void InventoryMenu::OnPauseState(SDL_Surface *) {
63
64 }
65
66
67 void InventoryMenu::OnResize(int width, int height) {
68
69 }
70
71
72 void InventoryMenu::HandleEvents(const Input &input) {
73         if (menu.IsActive()) {
74                 if (input.JustPressed(Input::PAD_LEFT)) {
75                         menu.PreviousItem();
76                 }
77                 if (input.JustPressed(Input::PAD_RIGHT)) {
78                         menu.NextItem();
79                 }
80         } else {
81                 if (input.JustPressed(Input::PAD_UP)) {
82                         itemMenu.PreviousItem();
83                 }
84                 if (input.JustPressed(Input::PAD_DOWN)) {
85                         itemMenu.NextItem();
86                 }
87         }
88
89         if (input.JustPressed(Input::ACTION_A)) {
90                 if (menu.IsActive()) {
91                         if (menu.Selected() == CHOICE_SORT) {
92                                 parent->Game().state->inventory.Sort();
93                                 LoadInventory();
94                         } else {
95                                 menu.SetSelected();
96                                 itemMenu.SetActive();
97                         }
98                 } else if (itemMenu.IsActive()) {
99                         itemMenu.SetDualSelection();
100                 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
101                         switch (menu.Selected()) {
102                                 case CHOICE_USE:
103                                         if (itemMenu.Selected()->CanUseOnStatusScreen()) {
104                                                 // TODO: implement item use
105                                         }
106                                         itemMenu.SetActive();
107                                         break;
108                                 case CHOICE_SORT:
109                                         // invalid state, recover
110                                         menu.SetActive();
111                                         itemMenu.SetInactive();
112                                         break;
113                                 case CHOICE_DROP:
114                                         if (itemMenu.Selected()->CanDrop()) {
115                                                 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
116                                                 itemMenu.ClearEntry(itemMenu.SelectedIndex());
117                                         }
118                                         itemMenu.SetActive();
119                                         break;
120                         }
121                 } else {
122                         parent->Game().state->inventory.SwapEntriesAt(
123                                         itemMenu.SelectedIndex(),
124                                         itemMenu.SecondaryIndex());
125                         itemMenu.SwapSelected();
126                         itemMenu.SetActive();
127                 }
128         }
129         if (input.JustPressed(Input::ACTION_B)) {
130                 if (menu.IsActive()) {
131                         Ctrl().PopState();
132                 } else if (itemMenu.IsActive()) {
133                         menu.SetActive();
134                         itemMenu.SetInactive();
135                 } else {
136                         itemMenu.SetActive();
137                 }
138         }
139 }
140
141 void InventoryMenu::UpdateWorld(Uint32 deltaT) {
142
143 }
144
145
146 void InventoryMenu::Render(SDL_Surface *screen) {
147         const Font &font(*parent->Res().normalFont);
148         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
149         Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
150         Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
151
152         parent->RenderBackground(screen);
153         parent->RenderHeros(screen, offset);
154         RenderMenu(screen, menuOffset + offset);
155         RenderInventory(screen, inventoryOffset + offset);
156 }
157
158 int InventoryMenu::Width() const {
159         return parent->Width();
160 }
161
162 int InventoryMenu::Height() const {
163         return parent->Height();
164 }
165
166 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
167         const Font &font(*parent->Res().normalFont);
168         const Frame &frame(*parent->Res().statusFrame);
169
170         const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
171         const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
172         const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
173
174         frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
175         font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
176         frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
177         menu.Draw(screen, menuOffset);
178 }
179
180 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
181         const Font &font(*parent->Res().normalFont);
182         const Frame &frame(*parent->Res().statusFrame);
183         const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
184
185         frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
186         itemMenu.Draw(screen, offset + menuOffset);
187 }
188
189 }