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