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()) {
136 spellMenu.SwapSelected();
137 spellMenu.SetActive();
141 if (input.JustPressed(Input::ACTION_B)) {
142 if (actionMenu.IsActive()) {
144 } else if (spellMenu.IsActive()) {
145 actionMenu.SetActive();
146 spellMenu.SetInactive();
148 spellMenu.SetActive();
153 void SpellMenu::UpdateWorld(float deltaT) {
157 void SpellMenu::Render(SDL_Surface *screen) {
158 const Font &font(*parent->Res().normalFont);
159 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
160 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
161 Vector<int> spellsOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
163 parent->RenderBackground(screen);
164 RenderHighlight(screen, offset);
165 parent->RenderHeros(screen, offset);
166 RenderMenu(screen, menuOffset + offset);
167 RenderSpells(screen, spellsOffset + offset);
170 int SpellMenu::Width() const {
171 return parent->Width();
174 int SpellMenu::Height() const {
175 return parent->Height();
178 void SpellMenu::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
179 if (cursor < 0) return;
180 Vector<int> statusOffset(parent->StatusOffset(cursor));
181 statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
183 rect.x = statusOffset.X();
184 rect.y = statusOffset.Y();
185 SDL_BlitSurface(highlight, 0, screen, &rect);
188 void SpellMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
189 const Font &font(*parent->Res().normalFont);
190 const Frame &frame(*parent->Res().statusFrame);
192 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
193 const Vector<int> menuFrameOffset(offset.X() + 9 * font.CharWidth(), offset.Y());
194 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
196 frame.Draw(screen, offset, 9 * font.CharWidth(), 3 * font.CharHeight());
197 font.DrawString(parent->Res().mainMenuSpellText, screen, labelOffset + offset);
198 frame.Draw(screen, menuFrameOffset, 21 * font.CharWidth(), 3 * font.CharHeight());
199 actionMenu.Draw(screen, menuOffset);
202 void SpellMenu::RenderSpells(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() + font.CharHeight() / 4);
207 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
208 spellMenu.Draw(screen, offset + menuOffset);