1 #include "CapsuleFeedMenu.h"
3 #include "CapsuleMenu.h"
5 #include "../app/Application.h"
6 #include "../app/Input.h"
7 #include "../common/Inventory.h"
8 #include "../common/Item.h"
9 #include "../common/GameConfig.h"
10 #include "../common/GameState.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
15 using common::Capsule;
16 using common::Inventory;
18 using geometry::Vector;
20 using graphics::Frame;
24 CapsuleFeedMenu::CapsuleFeedMenu(CapsuleMenu *parent)
26 , menu(*parent->Res().capsuleFeedMenuProperties)
27 , itemMenu(*parent->Res().inventoryMenuProperties) {
28 menu.Add(parent->Res().itemMenuSelectText, CHOICE_SELECT);
29 menu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
34 void CapsuleFeedMenu::OnEnterState(SDL_Surface *) {
39 void CapsuleFeedMenu::LoadInventory() {
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 // TODO: find out which items are impossible to feed to a capsule
47 itemMenu.Add(item->Name(), item, true, item->MenuIcon(), inv.ItemCountAt(i));
49 itemMenu.AddEmptyEntry();
54 void CapsuleFeedMenu::OnExitState(SDL_Surface *) {
58 void CapsuleFeedMenu::OnResumeState(SDL_Surface *) {
62 void CapsuleFeedMenu::OnPauseState(SDL_Surface *) {
67 void CapsuleFeedMenu::OnResize(int width, int height) {
72 void CapsuleFeedMenu::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()) {
91 if (menu.Selected() == CHOICE_SORT) {
92 parent->Game().state->inventory.Sort();
98 } else if (itemMenu.IsActive()) {
99 itemMenu.SetDualSelection();
100 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
101 switch (menu.Selected()) {
103 if (true /* can feed */) {
104 // TODO: implement capsule feeding
106 itemMenu.SetActive();
109 // invalid state, recover
111 itemMenu.SetInactive();
115 parent->Game().state->inventory.SwapEntriesAt(
116 itemMenu.SelectedIndex(),
117 itemMenu.SecondaryIndex());
118 itemMenu.SwapSelected();
119 itemMenu.SetActive();
122 if (input.JustPressed(Input::ACTION_B)) {
123 if (menu.IsActive()) {
125 } else if (itemMenu.IsActive()) {
127 itemMenu.SetInactive();
129 itemMenu.SetActive();
134 void CapsuleFeedMenu::UpdateWorld(float deltaT) {
138 void CapsuleFeedMenu::Render(SDL_Surface *screen) {
139 const Font &font(*parent->Res().statusFont);
140 const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
141 const Vector<int> nameOffset(
143 2 * font.CharHeight() - font.CharHeight() / 8);
144 const Vector<int> spriteOffset(
145 3 * font.CharWidth() + font.CharWidth() * 3 / 4,
146 4 * font.CharHeight() + font.CharHeight() / 4);
147 const Vector<int> hungerOffset(
148 13 * font.CharWidth(),
149 9 * font.CharHeight() + font.CharHeight() / 8);
150 const Vector<int> menuOffset(
152 13 * font.CharHeight() + font.CharHeight() / 8);
153 const Vector<int> itemsOffset(
155 16 * font.CharHeight() + font.CharHeight() / 8);
157 parent->RenderBackground(screen);
158 RenderName(screen, offset + nameOffset);
159 RenderSprite(screen, offset + spriteOffset);
160 RenderHunger(screen, offset + hungerOffset);
161 RenderMenu(screen, offset + menuOffset);
162 RenderItems(screen, offset + itemsOffset);
165 void CapsuleFeedMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
166 const Font &font(*parent->Res().statusFont);
167 const Vector<int> separatorOffset(5 * font.CharWidth(), 0);
168 const Vector<int> nameOffset(6 * font.CharWidth(), 0);
170 font.DrawString(parent->Res().capsuleNameLabel, screen, offset, 5);
171 font.DrawChar(':', screen, offset + separatorOffset);
172 font.DrawString(GetCapsule().Name(), screen, offset + nameOffset);
175 void CapsuleFeedMenu::RenderSprite(SDL_Surface *screen, const Vector<int> &offset) const {
176 // TODO: sitting ground
177 GetCapsule().BattleSprite()->Draw(screen, offset);
180 void CapsuleFeedMenu::RenderHunger(SDL_Surface *screen, const Vector<int> &offset) const {
181 const Font &font(*parent->Res().normalFont);
182 const Frame &frame(*parent->Res().statusFrame);
183 const Vector<int> textOffset(2 * font.CharWidth(), font.CharHeight());
185 frame.Draw(screen, offset, 18 * font.CharWidth(), 3 * font.CharHeight());
186 font.DrawString(parent->Res().capsuleNotHungryText, screen, offset + textOffset, 15);
189 void CapsuleFeedMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
190 const Font &font(*parent->Res().normalFont);
191 const Frame &frame(*parent->Res().statusFrame);
192 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
193 const Vector<int> menubgOffset(8 * font.CharWidth(), 0);
194 const Vector<int> menuOffset(11 * font.CharWidth(), font.CharHeight());
196 frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
197 font.DrawString(parent->Res().capsuleFeedLabel, screen, offset + labelOffset);
198 frame.Draw(screen, offset + menubgOffset, 22 * font.CharWidth(), 3 * font.CharHeight());
199 menu.Draw(screen, offset + menuOffset);
202 void CapsuleFeedMenu::RenderItems(SDL_Surface *screen, const Vector<int> &offset) const {
203 const Font &font(*parent->Res().normalFont);
204 const Frame &frame(*parent->Res().statusFrame);
205 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() * 5 / 4);
207 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
208 itemMenu.Draw(screen, offset + menuOffset);
212 int CapsuleFeedMenu::Width() const {
213 return parent->Width();
216 int CapsuleFeedMenu::Height() const {
217 return parent->Height();
220 const Capsule &CapsuleFeedMenu::GetCapsule() const {
221 return parent->GetCapsule();