1 #include "InventoryMenu.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"
16 using common::Inventory;
20 using graphics::Frame;
25 InventoryMenu::InventoryMenu(PartyMenu *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);
35 void InventoryMenu::OnEnterState(SDL_Surface *) {
40 void InventoryMenu::LoadInventory() {
41 const Inventory &inv(parent->Game().state->inventory);
43 itemMenu.Reserve(inv.MaxItems());
44 for (int i(0); i < inv.MaxItems(); ++i) {
45 const Item *item(inv.ItemAt(i));
47 itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
49 itemMenu.AddEmptyEntry();
54 void InventoryMenu::OnExitState(SDL_Surface *) {
58 void InventoryMenu::OnResumeState(SDL_Surface *) {
62 void InventoryMenu::OnPauseState(SDL_Surface *) {
67 void InventoryMenu::OnResize(int width, int height) {
72 void InventoryMenu::HandleEvents(const Input &input) {
73 if (menu.IsActive()) {
74 if (input.JustPressed(Input::PAD_LEFT)) {
77 if (input.JustPressed(Input::PAD_RIGHT)) {
81 if (input.JustPressed(Input::PAD_UP)) {
82 itemMenu.PreviousItem();
84 if (input.JustPressed(Input::PAD_DOWN)) {
89 if (input.JustPressed(Input::ACTION_A)) {
90 if (menu.IsActive()) {
91 if (menu.Selected() == CHOICE_SORT) {
92 parent->Game().state->inventory.Sort();
98 } else if (itemMenu.IsActive()) {
99 itemMenu.SetDualSelection();
100 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
101 switch (menu.Selected()) {
103 if (itemMenu.Selected()->CanUseOnStatusScreen()) {
104 // TODO: implement item use
106 itemMenu.SetActive();
109 // invalid state, recover
111 itemMenu.SetInactive();
114 if (itemMenu.Selected()->CanDrop()) {
115 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
116 itemMenu.ClearEntry(itemMenu.SelectedIndex());
118 itemMenu.SetActive();
122 parent->Game().state->inventory.SwapEntriesAt(
123 itemMenu.SelectedIndex(),
124 itemMenu.SecondaryIndex());
125 itemMenu.SwapSelected();
126 itemMenu.SetActive();
129 if (input.JustPressed(Input::ACTION_B)) {
130 if (menu.IsActive()) {
132 } else if (itemMenu.IsActive()) {
134 itemMenu.SetInactive();
136 itemMenu.SetActive();
141 void InventoryMenu::UpdateWorld(Uint32 deltaT) {
146 void InventoryMenu::Render(SDL_Surface *screen) {
147 const Font &font(*parent->Res().normalFont);
148 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
149 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
150 Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
152 parent->RenderBackground(screen);
153 parent->RenderHeros(screen, offset);
154 RenderMenu(screen, menuOffset + offset);
155 RenderInventory(screen, inventoryOffset + offset);
158 int InventoryMenu::Width() const {
159 return parent->Width();
162 int InventoryMenu::Height() const {
163 return parent->Height();
166 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
167 const Font &font(*parent->Res().normalFont);
168 const Frame &frame(*parent->Res().statusFrame);
170 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
171 const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
172 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
174 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
175 font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
176 frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
177 menu.Draw(screen, menuOffset);
180 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
181 const Font &font(*parent->Res().normalFont);
182 const Frame &frame(*parent->Res().statusFrame);
183 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
185 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
186 itemMenu.Draw(screen, offset + menuOffset);