From c1de2db3755c2cffd790c58bbd92e7fc993afcb5 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 4 Nov 2012 18:13:38 +0100 Subject: [PATCH] added basic (non-functional) inventory menu --- Debug/src/menu/subdir.mk | 3 + Release/src/menu/subdir.mk | 3 + src/graphics/Menu.h | 13 +++- src/main.cpp | 29 ++++++++ src/menu/InventoryMenu.cpp | 135 +++++++++++++++++++++++++++++++++++++ src/menu/InventoryMenu.h | 53 +++++++++++++++ src/menu/PartyMenu.cpp | 2 + src/menu/Resources.cpp | 9 ++- src/menu/Resources.h | 7 ++ src/menu/fwd.h | 1 + 10 files changed, 251 insertions(+), 4 deletions(-) create mode 100644 src/menu/InventoryMenu.cpp create mode 100644 src/menu/InventoryMenu.h diff --git a/Debug/src/menu/subdir.mk b/Debug/src/menu/subdir.mk index 22cbe4b..49f3428 100644 --- a/Debug/src/menu/subdir.mk +++ b/Debug/src/menu/subdir.mk @@ -6,6 +6,7 @@ CPP_SRCS += \ ../src/menu/ChangeHero.cpp \ ../src/menu/HeroStatus.cpp \ +../src/menu/InventoryMenu.cpp \ ../src/menu/PartyMenu.cpp \ ../src/menu/Resources.cpp \ ../src/menu/SelectHero.cpp \ @@ -14,6 +15,7 @@ CPP_SRCS += \ OBJS += \ ./src/menu/ChangeHero.o \ ./src/menu/HeroStatus.o \ +./src/menu/InventoryMenu.o \ ./src/menu/PartyMenu.o \ ./src/menu/Resources.o \ ./src/menu/SelectHero.o \ @@ -22,6 +24,7 @@ OBJS += \ CPP_DEPS += \ ./src/menu/ChangeHero.d \ ./src/menu/HeroStatus.d \ +./src/menu/InventoryMenu.d \ ./src/menu/PartyMenu.d \ ./src/menu/Resources.d \ ./src/menu/SelectHero.d \ diff --git a/Release/src/menu/subdir.mk b/Release/src/menu/subdir.mk index b4b3d7a..bfcd29c 100644 --- a/Release/src/menu/subdir.mk +++ b/Release/src/menu/subdir.mk @@ -6,6 +6,7 @@ CPP_SRCS += \ ../src/menu/ChangeHero.cpp \ ../src/menu/HeroStatus.cpp \ +../src/menu/InventoryMenu.cpp \ ../src/menu/PartyMenu.cpp \ ../src/menu/Resources.cpp \ ../src/menu/SelectHero.cpp \ @@ -14,6 +15,7 @@ CPP_SRCS += \ OBJS += \ ./src/menu/ChangeHero.o \ ./src/menu/HeroStatus.o \ +./src/menu/InventoryMenu.o \ ./src/menu/PartyMenu.o \ ./src/menu/Resources.o \ ./src/menu/SelectHero.o \ @@ -22,6 +24,7 @@ OBJS += \ CPP_DEPS += \ ./src/menu/ChangeHero.d \ ./src/menu/HeroStatus.d \ +./src/menu/InventoryMenu.d \ ./src/menu/PartyMenu.d \ ./src/menu/Resources.d \ ./src/menu/SelectHero.d \ diff --git a/src/graphics/Menu.h b/src/graphics/Menu.h index 188c574..e42f24e 100644 --- a/src/graphics/Menu.h +++ b/src/graphics/Menu.h @@ -39,8 +39,8 @@ struct MenuProperties { MenuProperties() : font(0), disabledFont(0), cursor(0) - , charsPerEntry(0), rows(0), rowGap(0) - , iconSpace(0), cols(0), colGap(0) + , charsPerEntry(0), rows(1), rowGap(0) + , iconSpace(0), cols(1), colGap(0) , charsPerNumber(0), charsPerAdditionalText(0) , additionalTextGap(0), delimiter(':') , wrapX(false), wrapY(false) { } @@ -214,6 +214,13 @@ void Menu::Draw(SDL_Surface *dest, const geometry::Vector &position) con geometry::Vector iconOffset( (i % cols) * (ColWidth() + colGap), (i / cols) * RowHeight()); + + // Third column hack! + // This fixes the position of the "DROP" item in the inventory menu. + if (i % cols == 2) { + iconOffset += geometry::Vector(font->CharWidth(), 0); + } + if (entries[start + i].icon) { entries[start + i].icon->Draw(dest, position + iconOffset); } @@ -234,7 +241,7 @@ void Menu::Draw(SDL_Surface *dest, const geometry::Vector &position) con if (charsPerNumber) { usedFont->DrawChar(delimiter, dest, position + textOffset); textOffset += geometry::Vector(usedFont->CharWidth(), 0); - usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset); + usedFont->DrawNumber(entries[start + i].number, dest, position + textOffset, charsPerNumber); } } geometry::Vector cursorOffset( diff --git a/src/main.cpp b/src/main.cpp index 4c350de..5ea2a03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -354,6 +354,35 @@ int main(int argc, char **argv) { menuResources.nextLabel = "NEXT"; menuResources.returnLabel = "RETURN"; + graphics::MenuProperties itemMenuProperties; + itemMenuProperties.cols = 3; + itemMenuProperties.rows = 1; + itemMenuProperties.charsPerEntry = 5; + itemMenuProperties.rowGap = 8; + itemMenuProperties.colGap = 16; + itemMenuProperties.cursor = &menuCursor; + itemMenuProperties.font = &menuFont; + itemMenuProperties.wrapX = true; + itemMenuProperties.wrapY = true; + menuResources.itemMenuProperties = &itemMenuProperties; + menuResources.itemMenuUseText = "USE"; + menuResources.itemMenuSortText = "SORT"; + menuResources.itemMenuDropText = "DROP"; + + graphics::MenuProperties inventoryMenuProperties; + inventoryMenuProperties.cols = 1; + inventoryMenuProperties.rows = 6; + inventoryMenuProperties.charsPerEntry = 13; + inventoryMenuProperties.rowGap = 8; + inventoryMenuProperties.cursor = &menuCursor; + inventoryMenuProperties.font = &menuFont; + // TODO: disabled font + inventoryMenuProperties.disabledFont = &menuFont; + inventoryMenuProperties.iconSpace = 16; + inventoryMenuProperties.charsPerNumber = 2; + inventoryMenuProperties.delimiter = ':'; + menuResources.inventoryMenuProperties = &inventoryMenuProperties; + InitScreen screen(width, height); app::State *state(0); diff --git a/src/menu/InventoryMenu.cpp b/src/menu/InventoryMenu.cpp new file mode 100644 index 0000000..2ea7fb3 --- /dev/null +++ b/src/menu/InventoryMenu.cpp @@ -0,0 +1,135 @@ +/* + * InventoryMenu.cpp + * + * Created on: Nov 4, 2012 + * Author: holy + */ + +#include "InventoryMenu.h" + +#include "PartyMenu.h" +#include "Resources.h" +#include "../app/Input.h" +#include "../common/GameConfig.h" +#include "../common/GameState.h" +#include "../common/Inventory.h" +#include "../common/Item.h" +#include "../graphics/Font.h" +#include "../graphics/Frame.h" + +using app::Input; +using common::Inventory; +using common::Item; +using geometry::Vector; +using graphics::Font; +using graphics::Frame; + +namespace menu { + +InventoryMenu::InventoryMenu(PartyMenu *parent) +: parent(parent) +, menu(*parent->Res().itemMenuProperties) +, itemMenu(*parent->Res().inventoryMenuProperties) { + menu.Add(parent->Res().itemMenuUseText, 0); + menu.Add(parent->Res().itemMenuSortText, 1); + menu.Add(parent->Res().itemMenuDropText, 2); +} + + +void InventoryMenu::OnEnterState(SDL_Surface *) { + const Inventory &inv(parent->Game().state->inventory); + itemMenu.Clear(); + itemMenu.Reserve(inv.MaxItems()); + for (int i(0); i < inv.MaxItems(); ++i) { + const Item *item(inv.ItemAt(i)); + if (item) { + itemMenu.Add(item->Name(), item, item->CanUseOnStatusScreen(), item->MenuIcon(), inv.ItemCountAt(i)); + } else { + itemMenu.AddEmptyEntry(); + } + } +} + +void InventoryMenu::OnExitState(SDL_Surface *) { + +} + +void InventoryMenu::OnResumeState(SDL_Surface *) { + +} + +void InventoryMenu::OnPauseState(SDL_Surface *) { + +} + + +void InventoryMenu::OnResize(int width, int height) { + +} + + +void InventoryMenu::HandleEvents(const Input &input) { + if (input.JustPressed(Input::PAD_LEFT)) { + menu.PreviousItem(); + } + if (input.JustPressed(Input::PAD_RIGHT)) { + menu.NextItem(); + } + + if (input.JustPressed(Input::PAD_UP)) { + itemMenu.PreviousItem(); + } + if (input.JustPressed(Input::PAD_DOWN)) { + itemMenu.NextItem(); + } +} + +void InventoryMenu::UpdateWorld(float deltaT) { + +} + + +void InventoryMenu::Render(SDL_Surface *screen) { + const Font &font(*parent->Res().normalFont); + Vector offset((screen->w - Width()) / 2, (screen->h - Height()) / 2); + Vector menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8); + Vector inventoryOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8); + + parent->RenderBackground(screen); + parent->RenderHeros(screen, offset); + RenderMenu(screen, menuOffset + offset); + RenderInventory(screen, inventoryOffset + offset); +} + +int InventoryMenu::Width() const { + return parent->Width(); +} + +int InventoryMenu::Height() const { + return parent->Height(); +} + +void InventoryMenu::RenderMenu(SDL_Surface *screen, const Vector &offset) const { + const Font &font(*parent->Res().normalFont); + const Frame &frame(*parent->Res().statusFrame); + + const Vector labelOffset(2 * font.CharWidth(), font.CharHeight()); + const Vector menuFrameOffset(offset.X() + 8 * font.CharWidth(), offset.Y()); + const Vector menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight()); + + frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight()); + font.DrawString(parent->Res().mainMenuItemText, screen, labelOffset + offset); + frame.Draw(screen, menuFrameOffset, 22 * font.CharWidth(), 3 * font.CharHeight()); + menu.Draw(screen, menuOffset); +} + +void InventoryMenu::RenderInventory(SDL_Surface *screen, const Vector &offset) const { + const Font &font(*parent->Res().normalFont); + const Frame &frame(*parent->Res().statusFrame); + const Vector menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4); + + frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight()); + itemMenu.Draw(screen, offset + menuOffset); +} + +} diff --git a/src/menu/InventoryMenu.h b/src/menu/InventoryMenu.h new file mode 100644 index 0000000..20cbe17 --- /dev/null +++ b/src/menu/InventoryMenu.h @@ -0,0 +1,53 @@ +/* + * InventoryMenu.h + * + * Created on: Nov 4, 2012 + * Author: holy + */ + +#ifndef MENU_INVENTORYMENU_H_ +#define MENU_INVENTORYMENU_H_ + +#include "fwd.h" +#include "../app/State.h" +#include "../common/fwd.h" +#include "../geometry/Vector.h" +#include "../graphics/Menu.h" + +namespace menu { + +class InventoryMenu +: public app::State { + +public: + explicit InventoryMenu(PartyMenu *parent); + +public: + virtual void HandleEvents(const app::Input &); + virtual void UpdateWorld(float deltaT); + virtual void Render(SDL_Surface *); + + int Width() const; + int Height() const; + +private: + virtual void OnEnterState(SDL_Surface *screen); + virtual void OnExitState(SDL_Surface *screen); + virtual void OnResumeState(SDL_Surface *screen); + virtual void OnPauseState(SDL_Surface *screen); + + virtual void OnResize(int width, int height); + + void RenderMenu(SDL_Surface *screen, const geometry::Vector &offset) const; + void RenderInventory(SDL_Surface *screen, const geometry::Vector &offset) const; + +private: + PartyMenu *parent; + graphics::Menu menu; + graphics::Menu itemMenu; + +}; + +} + +#endif /* MENU_INVENTORYMENU_H_ */ diff --git a/src/menu/PartyMenu.cpp b/src/menu/PartyMenu.cpp index e605c19..07b7770 100644 --- a/src/menu/PartyMenu.cpp +++ b/src/menu/PartyMenu.cpp @@ -8,6 +8,7 @@ #include "PartyMenu.h" #include "ChangeHero.h" +#include "InventoryMenu.h" #include "Resources.h" #include "SelectHero.h" #include "StatusMenu.h" @@ -94,6 +95,7 @@ void PartyMenu::HandleEvents(const Input &input) { if (input.JustPressed(Input::ACTION_A)) { switch (mainMenu.Selected()) { case MENU_ITEM_ITEM: + Ctrl().PushState(new InventoryMenu(this)); break; case MENU_ITEM_SPELL: break; diff --git a/src/menu/Resources.cpp b/src/menu/Resources.cpp index 256fb1f..87abfa5 100644 --- a/src/menu/Resources.cpp +++ b/src/menu/Resources.cpp @@ -52,7 +52,14 @@ Resources::Resources() , statusMenuProperties(0) , nextLabel(0) -, returnLabel(0) { +, returnLabel(0) + +, itemMenuProperties(0) +, itemMenuUseText(0) +, itemMenuSortText(0) +, itemMenuDropText(0) + +, inventoryMenuProperties(0) { } diff --git a/src/menu/Resources.h b/src/menu/Resources.h index a0c60e1..32d393e 100644 --- a/src/menu/Resources.h +++ b/src/menu/Resources.h @@ -59,6 +59,13 @@ struct Resources { const char *nextLabel; const char *returnLabel; + graphics::MenuProperties *itemMenuProperties; + const char *itemMenuUseText; + const char *itemMenuSortText; + const char *itemMenuDropText; + + graphics::MenuProperties *inventoryMenuProperties; + Resources(); }; diff --git a/src/menu/fwd.h b/src/menu/fwd.h index 3017e93..5e08e00 100644 --- a/src/menu/fwd.h +++ b/src/menu/fwd.h @@ -12,6 +12,7 @@ namespace menu { class ChangeHero; class HeroStatus; +class InventoryMenu; class PartyMenu; struct Resources; class SelectHero; -- 2.39.2