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"
27 using geometry::Vector;
29 using graphics::Frame;
34 SpellMenu::SpellMenu(PartyMenu *parent, int cursor)
38 , actionMenu(*parent->Res().itemMenuProperties)
39 , spellMenu(*parent->Res().spellMenuProperties) {
40 actionMenu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
41 actionMenu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
45 void SpellMenu::OnEnterState(SDL_Surface *) {
46 const HeroStatus &status(parent->GetHeroStatus(0));
47 highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0);
48 SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF));
49 SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20);
51 actionMenu.SetSelected();
55 void SpellMenu::LoadSpells() {
57 // TODO: set to max spells once implementation is changed
58 spellMenu.Reserve(GetHero().Spells().size());
59 for (vector<const Spell *>::const_iterator
60 i = GetHero().Spells().begin(), end = GetHero().Spells().end();
62 const Spell *spell = *i;
63 spellMenu.Add(spell->Name(), spell, spell->CanUseOnStatusScreen(), 0, spell->Cost());
67 const Hero &SpellMenu::GetHero() const {
68 return *parent->Game().state->party[cursor];
71 void SpellMenu::OnExitState(SDL_Surface *) {
72 SDL_FreeSurface(highlight);
75 void SpellMenu::OnResumeState(SDL_Surface *) {
79 void SpellMenu::OnPauseState(SDL_Surface *) {
84 void SpellMenu::OnResize(int width, int height) {
89 void SpellMenu::HandleEvents(const Input &input) {
90 if (actionMenu.IsActive()) {
91 if (input.JustPressed(Input::PAD_LEFT)) {
92 actionMenu.PreviousItem();
94 if (input.JustPressed(Input::PAD_RIGHT)) {
95 actionMenu.NextItem();
98 if (input.JustPressed(Input::PAD_UP)) {
99 spellMenu.PreviousRow();
101 if (input.JustPressed(Input::PAD_RIGHT)) {
102 spellMenu.NextItem();
104 if (input.JustPressed(Input::PAD_DOWN)) {
107 if (input.JustPressed(Input::PAD_LEFT)) {
108 spellMenu.PreviousItem();
112 if (input.JustPressed(Input::ACTION_A)) {
113 if (actionMenu.IsActive()) {
114 if (actionMenu.Selected() == CHOICE_SORT) {
118 actionMenu.SetSelected();
119 spellMenu.SetActive();
121 } else if (spellMenu.IsActive()) {
122 spellMenu.SetDualSelection();
123 } else if (spellMenu.SelectedIndex() == spellMenu.SecondaryIndex()) {
124 if (spellMenu.Selected()->CanUseOnStatusScreen()) {
129 spellMenu.SwapSelected();
130 spellMenu.SetActive();
134 if (input.JustPressed(Input::ACTION_B)) {
135 if (actionMenu.IsActive()) {
137 } else if (spellMenu.IsActive()) {
138 actionMenu.SetActive();
139 spellMenu.SetInactive();
141 spellMenu.SetActive();
146 void SpellMenu::UpdateWorld(float deltaT) {
150 void SpellMenu::Render(SDL_Surface *screen) {
151 const Font &font(*parent->Res().normalFont);
152 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
153 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
154 Vector<int> spellsOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
156 parent->RenderBackground(screen);
157 RenderHighlight(screen, offset);
158 parent->RenderHeros(screen, offset);
159 RenderMenu(screen, menuOffset + offset);
160 RenderSpells(screen, spellsOffset + offset);
163 int SpellMenu::Width() const {
164 return parent->Width();
167 int SpellMenu::Height() const {
168 return parent->Height();
171 void SpellMenu::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
172 if (cursor < 0) return;
173 Vector<int> statusOffset(parent->StatusOffset(cursor));
174 statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
176 rect.x = statusOffset.X();
177 rect.y = statusOffset.Y();
178 SDL_BlitSurface(highlight, 0, screen, &rect);
181 void SpellMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
182 const Font &font(*parent->Res().normalFont);
183 const Frame &frame(*parent->Res().statusFrame);
185 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
186 const Vector<int> menuFrameOffset(offset.X() + 9 * font.CharWidth(), offset.Y());
187 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
189 frame.Draw(screen, offset, 9 * font.CharWidth(), 3 * font.CharHeight());
190 font.DrawString(parent->Res().mainMenuSpellText, screen, labelOffset + offset);
191 frame.Draw(screen, menuFrameOffset, 21 * font.CharWidth(), 3 * font.CharHeight());
192 actionMenu.Draw(screen, menuOffset);
195 void SpellMenu::RenderSpells(SDL_Surface *screen, const Vector<int> &offset) const {
196 const Font &font(*parent->Res().normalFont);
197 const Frame &frame(*parent->Res().statusFrame);
198 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
200 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
201 spellMenu.Draw(screen, offset + menuOffset);