4 * Created on: Nov 18, 2012
10 #include "HeroStatus.h"
11 #include "PartyMenu.h"
12 #include "Resources.h"
13 #include "../app/Input.h"
14 #include "../common/GameConfig.h"
15 #include "../common/GameState.h"
16 #include "../common/Hero.h"
17 #include "../common/Spell.h"
18 #include "../graphics/Font.h"
19 #include "../graphics/Frame.h"
28 using geometry::Vector;
30 using graphics::Frame;
35 SpellMenu::SpellMenu(PartyMenu *parent, int cursor)
39 , actionMenu(*parent->Res().itemMenuProperties)
40 , spellMenu(*parent->Res().spellMenuProperties) {
41 actionMenu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
42 actionMenu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
46 void SpellMenu::OnEnterState(SDL_Surface *) {
47 const HeroStatus &status(parent->GetHeroStatus(0));
48 highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0);
49 SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF));
50 SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20);
52 actionMenu.SetSelected();
56 void SpellMenu::LoadSpells() {
58 // TODO: set to max spells once implementation is changed
59 spellMenu.Reserve(GetHero().Spells().size());
60 for (vector<const Spell *>::const_iterator
61 i = GetHero().Spells().begin(), end = GetHero().Spells().end();
63 const Spell *spell = *i;
64 spellMenu.Add(spell->Name(), spell, spell->CanUseOnStatusScreen(), 0, spell->Cost());
68 const Hero &SpellMenu::GetHero() const {
69 return *parent->Game().state->party[cursor];
72 Hero &SpellMenu::GetHero() {
73 return *parent->Game().state->party[cursor];
76 void SpellMenu::OnExitState(SDL_Surface *) {
77 SDL_FreeSurface(highlight);
80 void SpellMenu::OnResumeState(SDL_Surface *) {
84 void SpellMenu::OnPauseState(SDL_Surface *) {
89 void SpellMenu::OnResize(int width, int height) {
94 void SpellMenu::HandleEvents(const Input &input) {
95 if (actionMenu.IsActive()) {
96 if (input.JustPressed(Input::PAD_LEFT)) {
97 actionMenu.PreviousItem();
99 if (input.JustPressed(Input::PAD_RIGHT)) {
100 actionMenu.NextItem();
103 if (input.JustPressed(Input::PAD_UP)) {
104 spellMenu.PreviousRow();
106 if (input.JustPressed(Input::PAD_RIGHT)) {
107 spellMenu.NextItem();
109 if (input.JustPressed(Input::PAD_DOWN)) {
112 if (input.JustPressed(Input::PAD_LEFT)) {
113 spellMenu.PreviousItem();
117 if (input.JustPressed(Input::ACTION_A)) {
118 if (actionMenu.IsActive()) {
119 if (actionMenu.Selected() == CHOICE_SORT) {
120 std::sort(GetHero().Spells().begin(),
121 GetHero().Spells().end(),
125 actionMenu.SetSelected();
126 spellMenu.SetActive();
128 } else if (spellMenu.IsActive()) {
129 spellMenu.SetDualSelection();
130 } else if (spellMenu.SelectedIndex() == spellMenu.SecondaryIndex()) {
131 if (spellMenu.Selected()->CanUseOnStatusScreen()) {
135 std::swap(GetHero().Spells().at(spellMenu.SelectedIndex()),
136 GetHero().Spells().at(spellMenu.SecondaryIndex()));
137 spellMenu.SwapSelected();
138 spellMenu.SetActive();
142 if (input.JustPressed(Input::ACTION_B)) {
143 if (actionMenu.IsActive()) {
145 } else if (spellMenu.IsActive()) {
146 actionMenu.SetActive();
147 spellMenu.SetInactive();
149 spellMenu.SetActive();
154 void SpellMenu::UpdateWorld(float deltaT) {
158 void SpellMenu::Render(SDL_Surface *screen) {
159 const Font &font(*parent->Res().normalFont);
160 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
161 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
162 Vector<int> spellsOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
164 parent->RenderBackground(screen);
165 RenderHighlight(screen, offset);
166 parent->RenderHeros(screen, offset);
167 RenderMenu(screen, menuOffset + offset);
168 RenderSpells(screen, spellsOffset + offset);
171 int SpellMenu::Width() const {
172 return parent->Width();
175 int SpellMenu::Height() const {
176 return parent->Height();
179 void SpellMenu::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
180 if (cursor < 0) return;
181 Vector<int> statusOffset(parent->StatusOffset(cursor));
182 statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
184 rect.x = statusOffset.X();
185 rect.y = statusOffset.Y();
186 SDL_BlitSurface(highlight, 0, screen, &rect);
189 void SpellMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
190 const Font &font(*parent->Res().normalFont);
191 const Frame &frame(*parent->Res().statusFrame);
193 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
194 const Vector<int> menuFrameOffset(offset.X() + 9 * font.CharWidth(), offset.Y());
195 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
197 frame.Draw(screen, offset, 9 * font.CharWidth(), 3 * font.CharHeight());
198 font.DrawString(parent->Res().mainMenuSpellText, screen, labelOffset + offset);
199 frame.Draw(screen, menuFrameOffset, 21 * font.CharWidth(), 3 * font.CharHeight());
200 actionMenu.Draw(screen, menuOffset);
203 void SpellMenu::RenderSpells(SDL_Surface *screen, const Vector<int> &offset) const {
204 const Font &font(*parent->Res().normalFont);
205 const Frame &frame(*parent->Res().statusFrame);
206 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
208 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
209 spellMenu.Draw(screen, offset + menuOffset);