1 #include "InventoryMenu.h"
5 #include "../app/Input.h"
6 #include "../common/GameConfig.h"
7 #include "../common/GameState.h"
8 #include "../common/Inventory.h"
9 #include "../common/Item.h"
10 #include "../graphics/Font.h"
11 #include "../graphics/Frame.h"
14 using common::Inventory;
16 using geometry::Vector;
18 using graphics::Frame;
23 InventoryMenu::InventoryMenu(PartyMenu *parent)
25 , menu(*parent->Res().itemMenuProperties)
26 , itemMenu(*parent->Res().inventoryMenuProperties) {
27 menu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
28 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
29 menu.Add(parent->Res().itemMenuDropText, CHOICE_DROP);
33 void InventoryMenu::OnEnterState(SDL_Surface *) {
38 void InventoryMenu::LoadInventory() {
39 const Inventory &inv(parent->Game().state->inventory);
41 itemMenu.Reserve(inv.MaxItems());
42 for (int i(0); i < inv.MaxItems(); ++i) {
43 const Item *item(inv.ItemAt(i));
45 itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i));
47 itemMenu.AddEmptyEntry();
52 void InventoryMenu::OnExitState(SDL_Surface *) {
56 void InventoryMenu::OnResumeState(SDL_Surface *) {
60 void InventoryMenu::OnPauseState(SDL_Surface *) {
65 void InventoryMenu::OnResize(int width, int height) {
70 void InventoryMenu::HandleEvents(const Input &input) {
71 if (menu.IsActive()) {
72 if (input.JustPressed(Input::PAD_LEFT)) {
75 if (input.JustPressed(Input::PAD_RIGHT)) {
79 if (input.JustPressed(Input::PAD_UP)) {
80 itemMenu.PreviousItem();
82 if (input.JustPressed(Input::PAD_DOWN)) {
87 if (input.JustPressed(Input::ACTION_A)) {
88 if (menu.IsActive()) {
89 if (menu.Selected() == CHOICE_SORT) {
90 parent->Game().state->inventory.Sort();
96 } else if (itemMenu.IsActive()) {
97 itemMenu.SetDualSelection();
98 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
99 switch (menu.Selected()) {
101 if (itemMenu.Selected()->CanUseOnStatusScreen()) {
102 // TODO: implement item use
104 itemMenu.SetActive();
107 // invalid state, recover
109 itemMenu.SetInactive();
112 if (itemMenu.Selected()->CanDrop()) {
113 parent->Game().state->inventory.RemoveAll(itemMenu.Selected());
114 itemMenu.ClearEntry(itemMenu.SelectedIndex());
116 itemMenu.SetActive();
120 parent->Game().state->inventory.SwapEntriesAt(
121 itemMenu.SelectedIndex(),
122 itemMenu.SecondaryIndex());
123 itemMenu.SwapSelected();
124 itemMenu.SetActive();
127 if (input.JustPressed(Input::ACTION_B)) {
128 if (menu.IsActive()) {
130 } else if (itemMenu.IsActive()) {
132 itemMenu.SetInactive();
134 itemMenu.SetActive();
139 void InventoryMenu::UpdateWorld(float deltaT) {
144 void InventoryMenu::Render(SDL_Surface *screen) {
145 const Font &font(*parent->Res().normalFont);
146 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
147 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
148 Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
150 parent->RenderBackground(screen);
151 parent->RenderHeros(screen, offset);
152 RenderMenu(screen, menuOffset + offset);
153 RenderInventory(screen, inventoryOffset + offset);
156 int InventoryMenu::Width() const {
157 return parent->Width();
160 int InventoryMenu::Height() const {
161 return parent->Height();
164 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
165 const Font &font(*parent->Res().normalFont);
166 const Frame &frame(*parent->Res().statusFrame);
168 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
169 const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
170 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
172 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
173 font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
174 frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
175 menu.Draw(screen, menuOffset);
178 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
179 const Font &font(*parent->Res().normalFont);
180 const Frame &frame(*parent->Res().statusFrame);
181 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
183 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
184 itemMenu.Draw(screen, offset + menuOffset);