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;
29 InventoryMenu::InventoryMenu(PartyMenu *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);
39 void InventoryMenu::OnEnterState(SDL_Surface *) {
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()) {
95 if (input.JustPressed(Input::ACTION_B)) {
96 if (menu.IsActive()) {
100 itemMenu.SetInactive();
105 void InventoryMenu::UpdateWorld(float deltaT) {
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);
116 parent->RenderBackground(screen);
117 parent->RenderHeros(screen, offset);
118 RenderMenu(screen, menuOffset + offset);
119 RenderInventory(screen, inventoryOffset + offset);
122 int InventoryMenu::Width() const {
123 return parent->Width();
126 int InventoryMenu::Height() const {
127 return parent->Height();
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);
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());
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);
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);
149 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
150 itemMenu.Draw(screen, offset + menuOffset);