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"
15 using common::Inventory;
17 using geometry::Vector;
19 using graphics::Frame;
24 InventoryMenu::InventoryMenu(PartyMenu *parent)
26 , menu(*parent->Res().itemMenuProperties)
27 , itemMenu(*parent->Res().inventoryMenuProperties) {
28 menu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
29 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
30 menu.Add(parent->Res().itemMenuDropText, CHOICE_DROP);
34 void InventoryMenu::OnEnterState(SDL_Surface *) {
39 void InventoryMenu::LoadInventory() {
40 const Inventory &inv(parent->Game().state->inventory);
42 itemMenu.Reserve(inv.MaxItems());
43 for (int i(0); i < inv.MaxItems(); ++i) {
44 const Item *item(inv.ItemAt(i));
46 itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
48 itemMenu.AddEmptyEntry();
53 void InventoryMenu::OnExitState(SDL_Surface *) {
57 void InventoryMenu::OnResumeState(SDL_Surface *) {
61 void InventoryMenu::OnPauseState(SDL_Surface *) {
66 void InventoryMenu::OnResize(int width, int height) {
71 void InventoryMenu::HandleEvents(const Input &input) {
72 if (menu.IsActive()) {
73 if (input.JustPressed(Input::PAD_LEFT)) {
76 if (input.JustPressed(Input::PAD_RIGHT)) {
80 if (input.JustPressed(Input::PAD_UP)) {
81 itemMenu.PreviousItem();
83 if (input.JustPressed(Input::PAD_DOWN)) {
88 if (input.JustPressed(Input::ACTION_A)) {
89 if (menu.IsActive()) {
90 if (menu.Selected() == CHOICE_SORT) {
91 parent->Game().state->inventory.Sort();
97 } else if (itemMenu.IsActive()) {
98 itemMenu.SetDualSelection();
99 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
100 switch (menu.Selected()) {
102 if (itemMenu.Selected()->CanUseOnStatusScreen()) {
103 // TODO: implement item use
105 itemMenu.SetActive();
108 // invalid state, recover
110 itemMenu.SetInactive();
113 if (itemMenu.Selected()->CanDrop()) {
114 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
115 itemMenu.ClearEntry(itemMenu.SelectedIndex());
117 itemMenu.SetActive();
121 parent->Game().state->inventory.SwapEntriesAt(
122 itemMenu.SelectedIndex(),
123 itemMenu.SecondaryIndex());
124 itemMenu.SwapSelected();
125 itemMenu.SetActive();
128 if (input.JustPressed(Input::ACTION_B)) {
129 if (menu.IsActive()) {
131 } else if (itemMenu.IsActive()) {
133 itemMenu.SetInactive();
135 itemMenu.SetActive();
140 void InventoryMenu::UpdateWorld(float deltaT) {
145 void InventoryMenu::Render(SDL_Surface *screen) {
146 const Font &font(*parent->Res().normalFont);
147 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
148 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
149 Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
151 parent->RenderBackground(screen);
152 parent->RenderHeros(screen, offset);
153 RenderMenu(screen, menuOffset + offset);
154 RenderInventory(screen, inventoryOffset + offset);
157 int InventoryMenu::Width() const {
158 return parent->Width();
161 int InventoryMenu::Height() const {
162 return parent->Height();
165 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
166 const Font &font(*parent->Res().normalFont);
167 const Frame &frame(*parent->Res().statusFrame);
169 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
170 const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
171 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
173 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
174 font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
175 frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
176 menu.Draw(screen, menuOffset);
179 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
180 const Font &font(*parent->Res().normalFont);
181 const Frame &frame(*parent->Res().statusFrame);
182 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
184 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
185 itemMenu.Draw(screen, offset + menuOffset);