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