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