]> git.localhorst.tv Git - l2e.git/blob - CapsuleFeedMenu.cpp
184212d820537b47bbaac4cb2813357125608670
[l2e.git] / CapsuleFeedMenu.cpp
1 #include "CapsuleFeedMenu.h"
2
3 #include "CapsuleMenu.h"
4 #include "Resources.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"
13
14 using app::Input;
15 using common::Capsule;
16 using common::Inventory;
17 using common::Item;
18 using geometry::Vector;
19 using graphics::Font;
20 using graphics::Frame;
21
22 namespace menu {
23
24 CapsuleFeedMenu::CapsuleFeedMenu(CapsuleMenu *parent)
25 : parent(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);
30         LoadInventory();
31 }
32
33
34 void CapsuleFeedMenu::OnEnterState(SDL_Surface *) {
35         menu.SetSelected();
36         itemMenu.SetActive();
37 }
38
39 void CapsuleFeedMenu::LoadInventory() {
40         const Inventory &inv(parent->Game().state->inventory);
41         itemMenu.Clear();
42         itemMenu.Reserve(inv.MaxItems());
43         for (int i(0); i < inv.MaxItems(); ++i) {
44                 const Item *item(inv.ItemAt(i));
45                 if (item) {
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));
48                 } else {
49                         itemMenu.AddEmptyEntry();
50                 }
51         }
52 }
53
54 void CapsuleFeedMenu::OnExitState(SDL_Surface *) {
55
56 }
57
58 void CapsuleFeedMenu::OnResumeState(SDL_Surface *) {
59
60 }
61
62 void CapsuleFeedMenu::OnPauseState(SDL_Surface *) {
63
64 }
65
66
67 void CapsuleFeedMenu::OnResize(int width, int height) {
68
69 }
70
71
72 void CapsuleFeedMenu::HandleEvents(const Input &input) {
73         if (menu.IsActive()) {
74                 if (input.JustPressed(Input::PAD_LEFT)) {
75                         menu.PreviousItem();
76                 }
77                 if (input.JustPressed(Input::PAD_RIGHT)) {
78                         menu.NextItem();
79                 }
80         } else {
81                 if (input.JustPressed(Input::PAD_UP)) {
82                         itemMenu.PreviousItem();
83                 }
84                 if (input.JustPressed(Input::PAD_DOWN)) {
85                         itemMenu.NextItem();
86                 }
87         }
88
89         if (input.JustPressed(Input::ACTION_A)) {
90                 if (menu.IsActive()) {
91                         if (menu.Selected() == CHOICE_SORT) {
92                                 parent->Game().state->inventory.Sort();
93                                 LoadInventory();
94                         } else {
95                                 menu.SetSelected();
96                                 itemMenu.SetActive();
97                         }
98                 } else if (itemMenu.IsActive()) {
99                         itemMenu.SetDualSelection();
100                 } else if (itemMenu.SelectedIndex() == itemMenu.SecondaryIndex()) {
101                         switch (menu.Selected()) {
102                                 case CHOICE_SELECT:
103                                         if (true /* can feed */) {
104                                                 // TODO: implement capsule feeding
105                                         }
106                                         itemMenu.SetActive();
107                                         break;
108                                 case CHOICE_SORT:
109                                         // invalid state, recover
110                                         menu.SetActive();
111                                         itemMenu.SetInactive();
112                                         break;
113                         }
114                 } else {
115                         parent->Game().state->inventory.SwapEntriesAt(
116                                         itemMenu.SelectedIndex(),
117                                         itemMenu.SecondaryIndex());
118                         itemMenu.SwapSelected();
119                         itemMenu.SetActive();
120                 }
121         }
122         if (input.JustPressed(Input::ACTION_B)) {
123                 if (menu.IsActive()) {
124                         Ctrl().PopState();
125                 } else if (itemMenu.IsActive()) {
126                         menu.SetActive();
127                         itemMenu.SetInactive();
128                 } else {
129                         itemMenu.SetActive();
130                 }
131         }
132 }
133
134 void CapsuleFeedMenu::UpdateWorld(float deltaT) {
135
136 }
137
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(
142                         font.CharWidth(),
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(
151                         font.CharWidth(),
152                         13 * font.CharHeight() + font.CharHeight() / 8);
153         const Vector<int> itemsOffset(
154                         font.CharWidth(),
155                         16 * font.CharHeight() + font.CharHeight() / 8);
156
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);
163 }
164
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);
169
170         font.DrawString(parent->Res().capsuleNameLabel, screen, offset, 5);
171         font.DrawChar(':', screen, offset + separatorOffset);
172         font.DrawString(GetCapsule().Name(), screen, offset + nameOffset);
173 }
174
175 void CapsuleFeedMenu::RenderSprite(SDL_Surface *screen, const Vector<int> &offset) const {
176         // TODO: sitting ground
177         GetCapsule().BattleSprite()->Draw(screen, offset);
178 }
179
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());
184
185         frame.Draw(screen, offset, 18 * font.CharWidth(), 3 * font.CharHeight());
186         font.DrawString(parent->Res().capsuleNotHungryText, screen, offset + textOffset, 15);
187 }
188
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());
195
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);
200 }
201
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);
206
207         frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
208         itemMenu.Draw(screen, offset + menuOffset);
209 }
210
211
212 int CapsuleFeedMenu::Width() const {
213         return parent->Width();
214 }
215
216 int CapsuleFeedMenu::Height() const {
217         return parent->Height();
218 }
219
220 const Capsule &CapsuleFeedMenu::GetCapsule() const {
221         return parent->GetCapsule();
222 }
223
224 }