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 *) {
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 (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 void InventoryMenu::UpdateWorld(float deltaT) {
92 void InventoryMenu::Render(SDL_Surface *screen) {
93 const Font &font(*parent->Res().normalFont);
94 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
95 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
96 Vector<int> inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
98 parent->RenderBackground(screen);
99 parent->RenderHeros(screen, offset);
100 RenderMenu(screen, menuOffset + offset);
101 RenderInventory(screen, inventoryOffset + offset);
104 int InventoryMenu::Width() const {
105 return parent->Width();
108 int InventoryMenu::Height() const {
109 return parent->Height();
112 void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
113 const Font &font(*parent->Res().normalFont);
114 const Frame &frame(*parent->Res().statusFrame);
116 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
117 const Vector<int> menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y());
118 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
120 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
121 font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset);
122 frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
123 menu.Draw(screen, menuOffset);
126 void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector<int> &offset) const {
127 const Font &font(*parent->Res().normalFont);
128 const Frame &frame(*parent->Res().statusFrame);
129 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
131 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
132 itemMenu.Draw(screen, offset + menuOffset);