+#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<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
+ const Vector<int> nameOffset(
+ font.CharWidth(),
+ 2 * font.CharHeight() - font.CharHeight() / 8);
+ const Vector<int> spriteOffset(
+ 3 * font.CharWidth() + font.CharWidth() * 3 / 4,
+ 4 * font.CharHeight() + font.CharHeight() / 4);
+ const Vector<int> hungerOffset(
+ 13 * font.CharWidth(),
+ 9 * font.CharHeight() + font.CharHeight() / 8);
+ const Vector<int> menuOffset(
+ font.CharWidth(),
+ 13 * font.CharHeight() + font.CharHeight() / 8);
+ const Vector<int> 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<int> &offset) const {
+ const Font &font(*parent->Res().statusFont);
+ const Vector<int> separatorOffset(5 * font.CharWidth(), 0);
+ const Vector<int> 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<int> &offset) const {
+ // TODO: sitting ground
+ GetCapsule().BattleSprite()->Draw(screen, offset);
+}
+
+void CapsuleFeedMenu::RenderHunger(SDL_Surface *screen, const Vector<int> &offset) const {
+ const Font &font(*parent->Res().normalFont);
+ const Frame &frame(*parent->Res().statusFrame);
+ const Vector<int> 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<int> &offset) const {
+ const Font &font(*parent->Res().normalFont);
+ const Frame &frame(*parent->Res().statusFrame);
+ const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
+ const Vector<int> menubgOffset(8 * font.CharWidth(), 0);
+ const Vector<int> 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<int> &offset) const {
+ const Font &font(*parent->Res().normalFont);
+ const Frame &frame(*parent->Res().statusFrame);
+ const Vector<int> 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();
+}
+
+}