4 * Created on: Nov 4, 2012
8 #include "InventoryMenu.h"
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"
21 using common::Inventory;
23 using geometry::Vector;
25 using graphics::Frame;
30 InventoryMenu::InventoryMenu(PartyMenu *parent)
32 , menu(*parent->Res().itemMenuProperties)
33 , itemMenu(*parent->Res().inventoryMenuProperties) {
34 menu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
35 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
36 menu.Add(parent->Res().itemMenuDropText, CHOICE_DROP);
40 void InventoryMenu::OnEnterState(SDL_Surface *) {
45 void InventoryMenu::LoadInventory() {
46 const Inventory &inv(parent->Game().state->inventory);
48 itemMenu.Reserve(inv.MaxItems());
49 for (int i(0); i < inv.MaxItems(); ++i) {
50 const Item *item(inv.ItemAt(i));
52 itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
54 itemMenu.AddEmptyEntry();
59 void InventoryMenu::OnExitState(SDL_Surface *) {
63 void InventoryMenu::OnResumeState(SDL_Surface *) {
67 void InventoryMenu::OnPauseState(SDL_Surface *) {
72 void InventoryMenu::OnResize(int width, int height) {
77 void InventoryMenu::HandleEvents(const Input &input) {
78 if (menu.IsActive()) {
79 if (input.JustPressed(Input::PAD_LEFT)) {
82 if (input.JustPressed(Input::PAD_RIGHT)) {
86 if (input.JustPressed(Input::PAD_UP)) {
87 itemMenu.PreviousItem();
89 if (input.JustPressed(Input::PAD_DOWN)) {
94 if (input.JustPressed(Input::ACTION_A)) {
95 if (menu.IsActive()) {
96 if (menu.Selected() == CHOICE_SORT) {
97 parent->Game().state->inventory.Sort();
101 itemMenu.SetActive();
103 } else if (itemMenu.IsActive()) {
104 itemMenu.SetDualSelection();
105 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
106 switch (menu.Selected()) {
108 if (itemMenu.Selected()->CanUseOnStatusScreen()) {
109 // TODO: implement item use
111 itemMenu.SetActive();
114 // invalid state, recover
116 itemMenu.SetInactive();
119 if (itemMenu.Selected()->CanDrop()) {
120 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
121 itemMenu.ClearEntry(itemMenu.SelectedIndex());
123 itemMenu.SetActive();
127 parent->Game().state->inventory.SwapEntriesAt(
128 itemMenu.SelectedIndex(),
129 itemMenu.SecondaryIndex());
130 itemMenu.SwapSelected();
131 itemMenu.SetActive();
134 if (input.JustPressed(Input::ACTION_B)) {
135 if (menu.IsActive()) {
137 } else if (itemMenu.IsActive()) {
139 itemMenu.SetInactive();
141 itemMenu.SetActive();
146 void InventoryMenu::UpdateWorld(float deltaT) {
151 void InventoryMenu::Render(SDL_Surface *screen) {
152 const Font &font(*parent->Res().normalFont);
153 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
154 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
155 Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
157 parent->RenderBackground(screen);
158 parent->RenderHeros(screen, offset);
159 RenderMenu(screen, menuOffset + offset);
160 RenderInventory(screen, inventoryOffset + offset);
163 int InventoryMenu::Width() const {
164 return parent->Width();
167 int InventoryMenu::Height() const {
168 return parent->Height();
171 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
172 const Font &font(*parent->Res().normalFont);
173 const Frame &frame(*parent->Res().statusFrame);
175 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
176 const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
177 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
179 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
180 font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
181 frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
182 menu.Draw(screen, menuOffset);
185 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
186 const Font &font(*parent->Res().normalFont);
187 const Frame &frame(*parent->Res().statusFrame);
188 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
190 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
191 itemMenu.Draw(screen, offset + menuOffset);