X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmenu%2FCapsuleFeedMenu.cpp;fp=src%2Fmenu%2FCapsuleFeedMenu.cpp;h=184212d820537b47bbaac4cb2813357125608670;hb=7a14d357d9d05c2bac1efcdcf57365a4ce13729a;hp=0000000000000000000000000000000000000000;hpb=7f0a586b8238c7093a8942ff5b5c4122edd386fc;p=l2e.git diff --git a/src/menu/CapsuleFeedMenu.cpp b/src/menu/CapsuleFeedMenu.cpp new file mode 100644 index 0000000..184212d --- /dev/null +++ b/src/menu/CapsuleFeedMenu.cpp @@ -0,0 +1,224 @@ +#include "CapsuleFeedMenu.h" + +#include "CapsuleMenu.h" +#include "Resources.h" +#include "../app/Application.h" +#include "../app/Input.h" +#include "../common/Inventory.h" +#include "../common/Item.h" +#include "../common/GameConfig.h" +#include "../common/GameState.h" +#include "../graphics/Font.h" +#include "../graphics/Frame.h" + +using app::Input; +using common::Capsule; +using common::Inventory; +using common::Item; +using geometry::Vector; +using graphics::Font; +using graphics::Frame; + +namespace menu { + +CapsuleFeedMenu::CapsuleFeedMenu(CapsuleMenu *parent) +: parent(parent) +, menu(*parent->Res().capsuleFeedMenuProperties) +, itemMenu(*parent->Res().inventoryMenuProperties) { + menu.Add(parent->Res().itemMenuSelectText, CHOICE_SELECT); + menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT); + LoadInventory(); +} + + +void CapsuleFeedMenu::OnEnterState(SDL_Surface *) { + menu.SetSelected(); + itemMenu.SetActive(); +} + +void CapsuleFeedMenu::LoadInventory() { + 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) { + // TODO: find out which items are impossible to feed to a capsule + itemMenu.Add(item->Name(), item, true, item->MenuIcon(), inv.ItemCountAt(i)); + } else { + itemMenu.AddEmptyEntry(); + } + } +} + +void CapsuleFeedMenu::OnExitState(SDL_Surface *) { + +} + +void CapsuleFeedMenu::OnResumeState(SDL_Surface *) { + +} + +void CapsuleFeedMenu::OnPauseState(SDL_Surface *) { + +} + + +void CapsuleFeedMenu::OnResize(int width, int height) { + +} + + +void CapsuleFeedMenu::HandleEvents(const Input &input) { + if (menu.IsActive()) { + if (input.JustPressed(Input::PAD_LEFT)) { + menu.PreviousItem(); + } + if (input.JustPressed(Input::PAD_RIGHT)) { + menu.NextItem(); + } + } else { + if (input.JustPressed(Input::PAD_UP)) { + itemMenu.PreviousItem(); + } + if (input.JustPressed(Input::PAD_DOWN)) { + itemMenu.NextItem(); + } + } + + if (input.JustPressed(Input::ACTION_A)) { + if (menu.IsActive()) { + if (menu.Selected() == CHOICE_SORT) { + parent->Game().state->inventory.Sort(); + LoadInventory(); + } else { + menu.SetSelected(); + itemMenu.SetActive(); + } + } else if (itemMenu.IsActive()) { + itemMenu.SetDualSelection(); + } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) { + switch (menu.Selected()) { + case CHOICE_SELECT: + if (true /* can feed */) { + // TODO: implement capsule feeding + } + itemMenu.SetActive(); + break; + case CHOICE_SORT: + // invalid state, recover + menu.SetActive(); + itemMenu.SetInactive(); + break; + } + } else { + parent->Game().state->inventory.SwapEntriesAt( + itemMenu.SelectedIndex(), + itemMenu.SecondaryIndex()); + itemMenu.SwapSelected(); + itemMenu.SetActive(); + } + } + if (input.JustPressed(Input::ACTION_B)) { + if (menu.IsActive()) { + Ctrl().PopState(); + } else if (itemMenu.IsActive()) { + menu.SetActive(); + itemMenu.SetInactive(); + } else { + itemMenu.SetActive(); + } + } +} + +void CapsuleFeedMenu::UpdateWorld(float deltaT) { + +} + +void CapsuleFeedMenu::Render(SDL_Surface *screen) { + const Font &font(*parent->Res().statusFont); + const Vector offset((screen->w - Width()) / 2, (screen->h - Height()) / 2); + const Vector nameOffset( + font.CharWidth(), + 2 * font.CharHeight() - font.CharHeight() / 8); + const Vector spriteOffset( + 3 * font.CharWidth() + font.CharWidth() * 3 / 4, + 4 * font.CharHeight() + font.CharHeight() / 4); + const Vector hungerOffset( + 13 * font.CharWidth(), + 9 * font.CharHeight() + font.CharHeight() / 8); + const Vector menuOffset( + font.CharWidth(), + 13 * font.CharHeight() + font.CharHeight() / 8); + const Vector itemsOffset( + font.CharWidth(), + 16 * font.CharHeight() + font.CharHeight() / 8); + + parent->RenderBackground(screen); + RenderName(screen, offset + nameOffset); + RenderSprite(screen, offset + spriteOffset); + RenderHunger(screen, offset + hungerOffset); + RenderMenu(screen, offset + menuOffset); + RenderItems(screen, offset + itemsOffset); +} + +void CapsuleFeedMenu::RenderName(SDL_Surface *screen, const Vector &offset) const { + const Font &font(*parent->Res().statusFont); + const Vector separatorOffset(5 * font.CharWidth(), 0); + const Vector nameOffset(6 * font.CharWidth(), 0); + + font.DrawString(parent->Res().capsuleNameLabel, screen, offset, 5); + font.DrawChar(':', screen, offset + separatorOffset); + font.DrawString(GetCapsule().Name(), screen, offset + nameOffset); +} + +void CapsuleFeedMenu::RenderSprite(SDL_Surface *screen, const Vector &offset) const { + // TODO: sitting ground + GetCapsule().BattleSprite()->Draw(screen, offset); +} + +void CapsuleFeedMenu::RenderHunger(SDL_Surface *screen, const Vector &offset) const { + const Font &font(*parent->Res().normalFont); + const Frame &frame(*parent->Res().statusFrame); + const Vector textOffset(2 * font.CharWidth(), font.CharHeight()); + + frame.Draw(screen, offset, 18 * font.CharWidth(), 3 * font.CharHeight()); + font.DrawString(parent->Res().capsuleNotHungryText, screen, offset + textOffset, 15); +} + +void CapsuleFeedMenu::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 menubgOffset(8 * font.CharWidth(), 0); + const Vector menuOffset(11 * font.CharWidth(), font.CharHeight()); + + frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight()); + font.DrawString(parent->Res().capsuleFeedLabel, screen, offset + labelOffset); + frame.Draw(screen, offset + menubgOffset, 22 * font.CharWidth(), 3 * font.CharHeight()); + menu.Draw(screen, offset + menuOffset); +} + +void CapsuleFeedMenu::RenderItems(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() * 5 / 4); + + frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight()); + itemMenu.Draw(screen, offset + menuOffset); +} + + +int CapsuleFeedMenu::Width() const { + return parent->Width(); +} + +int CapsuleFeedMenu::Height() const { + return parent->Height(); +} + +const Capsule &CapsuleFeedMenu::GetCapsule() const { + return parent->GetCapsule(); +} + +}