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