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