]> git.localhorst.tv Git - l2e.git/blob - src/menu/InventoryMenu.cpp
437cd9cb03d096c72d722ded4c13f2d910d5afb4
[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                                         if (itemMenu.Selected()->CanUseOnStatusScreen()) {
109                                                 // TODO: implement item use
110                                         }
111                                         itemMenu.SetActive();
112                                         break;
113                                 case CHOICE_SORT:
114                                         // invalid state, recover
115                                         menu.SetActive();
116                                         itemMenu.SetInactive();
117                                         break;
118                                 case CHOICE_DROP:
119                                         if (itemMenu.Selected()->CanDrop()) {
120                                                 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
121                                                 itemMenu.ClearEntry(itemMenu.SelectedIndex());
122                                         }
123                                         itemMenu.SetActive();
124                                         break;
125                         }
126                 } else {
127                         parent->Game().state->inventory.SwapEntriesAt(
128                                         itemMenu.SelectedIndex(),
129                                         itemMenu.SecondaryIndex());
130                         itemMenu.SwapSelected();
131                         itemMenu.SetActive();
132                 }
133         }
134         if (input.JustPressed(Input::ACTION_B)) {
135                 if (menu.IsActive()) {
136                         Ctrl().PopState();
137                 } else if (itemMenu.IsActive()) {
138                         menu.SetActive();
139                         itemMenu.SetInactive();
140                 } else {
141                         itemMenu.SetActive();
142                 }
143         }
144 }
145
146 void InventoryMenu::UpdateWorld(float deltaT) {
147
148 }
149
150
151 void InventoryMenu::Render(SDL_Surface *screen) {
152         const Font &font(*parent->Res().normalFont);
153         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
154         Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
155         Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
156
157         parent->RenderBackground(screen);
158         parent->RenderHeros(screen, offset);
159         RenderMenu(screen, menuOffset + offset);
160         RenderInventory(screen, inventoryOffset + offset);
161 }
162
163 int InventoryMenu::Width() const {
164         return parent->Width();
165 }
166
167 int InventoryMenu::Height() const {
168         return parent->Height();
169 }
170
171 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
172         const Font &font(*parent->Res().normalFont);
173         const Frame &frame(*parent->Res().statusFrame);
174
175         const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
176         const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
177         const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
178
179         frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
180         font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
181         frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
182         menu.Draw(screen, menuOffset);
183 }
184
185 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
186         const Font &font(*parent->Res().normalFont);
187         const Frame &frame(*parent->Res().statusFrame);
188         const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
189
190         frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
191         itemMenu.Draw(screen, offset + menuOffset);
192 }
193
194 }