]> git.localhorst.tv Git - l2e.git/blob - src/menu/InventoryMenu.cpp
eb74ab8b5ea899a5220c2e959a5c37f92c3666c1
[l2e.git] / src / menu / InventoryMenu.cpp
1 #include "InventoryMenu.h"
2
3 #include "PartyMenu.h"
4 #include "Resources.h"
5 #include "../app/Input.h"
6 #include "../common/GameConfig.h"
7 #include "../common/GameState.h"
8 #include "../common/Inventory.h"
9 #include "../common/Item.h"
10 #include "../graphics/Font.h"
11 #include "../graphics/Frame.h"
12
13 using app::Input;
14 using common::Inventory;
15 using common::Item;
16 using geometry::Vector;
17 using graphics::Font;
18 using graphics::Frame;
19 using std::swap;
20
21 namespace menu {
22
23 InventoryMenu::InventoryMenu(PartyMenu *parent)
24 : parent(parent)
25 , menu(*parent->Res().itemMenuProperties)
26 , itemMenu(*parent->Res().inventoryMenuProperties) {
27         menu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
28         menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
29         menu.Add(parent->Res().itemMenuDropText, CHOICE_DROP);
30 }
31
32
33 void InventoryMenu::OnEnterState(SDL_Surface *) {
34         menu.SetSelected();
35         LoadInventory();
36 }
37
38 void InventoryMenu::LoadInventory() {
39         const Inventory &inv(parent->Game().state->inventory);
40         itemMenu.Clear();
41         itemMenu.Reserve(inv.MaxItems());
42         for (int i(0); i < inv.MaxItems(); ++i) {
43                 const Item *item(inv.ItemAt(i));
44                 if (item) {
45                         itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
46                 } else {
47                         itemMenu.AddEmptyEntry();
48                 }
49         }
50 }
51
52 void InventoryMenu::OnExitState(SDL_Surface *) {
53
54 }
55
56 void InventoryMenu::OnResumeState(SDL_Surface *) {
57
58 }
59
60 void InventoryMenu::OnPauseState(SDL_Surface *) {
61
62 }
63
64
65 void InventoryMenu::OnResize(int width, int height) {
66
67 }
68
69
70 void InventoryMenu::HandleEvents(const Input &input) {
71         if (menu.IsActive()) {
72                 if (input.JustPressed(Input::PAD_LEFT)) {
73                         menu.PreviousItem();
74                 }
75                 if (input.JustPressed(Input::PAD_RIGHT)) {
76                         menu.NextItem();
77                 }
78         } else {
79                 if (input.JustPressed(Input::PAD_UP)) {
80                         itemMenu.PreviousItem();
81                 }
82                 if (input.JustPressed(Input::PAD_DOWN)) {
83                         itemMenu.NextItem();
84                 }
85         }
86
87         if (input.JustPressed(Input::ACTION_A)) {
88                 if (menu.IsActive()) {
89                         if (menu.Selected() == CHOICE_SORT) {
90                                 parent->Game().state->inventory.Sort();
91                                 LoadInventory();
92                         } else {
93                                 menu.SetSelected();
94                                 itemMenu.SetActive();
95                         }
96                 } else if (itemMenu.IsActive()) {
97                         itemMenu.SetDualSelection();
98                 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
99                         switch (menu.Selected()) {
100                                 case CHOICE_USE:
101                                         if (itemMenu.Selected()->CanUseOnStatusScreen()) {
102                                                 // TODO: implement item use
103                                         }
104                                         itemMenu.SetActive();
105                                         break;
106                                 case CHOICE_SORT:
107                                         // invalid state, recover
108                                         menu.SetActive();
109                                         itemMenu.SetInactive();
110                                         break;
111                                 case CHOICE_DROP:
112                                         if (itemMenu.Selected()->CanDrop()) {
113                                                 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
114                                                 itemMenu.ClearEntry(itemMenu.SelectedIndex());
115                                         }
116                                         itemMenu.SetActive();
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 }