3 #include "HeroStatus.h"
6 #include "../app/Input.h"
7 #include "../common/GameConfig.h"
8 #include "../common/GameState.h"
9 #include "../common/Hero.h"
10 #include "../common/Spell.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
21 using geometry::Vector;
23 using graphics::Frame;
28 SpellMenu::SpellMenu(PartyMenu *parent, int cursor)
32 , actionMenu(*parent->Res().itemMenuProperties)
33 , spellMenu(*parent->Res().spellMenuProperties) {
34 actionMenu.Add(parent->Res().itemMenuUseText, CHOICE_USE);
35 actionMenu.Add(parent->Res().itemMenuSortText, CHOICE_SORT);
39 void SpellMenu::OnEnterState(SDL_Surface *) {
40 const HeroStatus &status(parent->GetHeroStatus(0));
41 highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0);
42 SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF));
43 SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20);
45 actionMenu.SetSelected();
49 void SpellMenu::LoadSpells() {
51 // TODO: set to max spells once implementation is changed
52 spellMenu.Reserve(GetHero().Spells().size());
53 for (vector<const Spell *>::const_iterator
54 i = GetHero().Spells().begin(), end = GetHero().Spells().end();
56 const Spell *spell = *i;
57 spellMenu.Add(spell->Name(), spell, spell->CanUseOnStatusScreen(), 0, spell->Cost());
61 const Hero &SpellMenu::GetHero() const {
62 return *parent->Game().state->party[cursor];
65 Hero &SpellMenu::GetHero() {
66 return *parent->Game().state->party[cursor];
69 void SpellMenu::OnExitState(SDL_Surface *) {
70 SDL_FreeSurface(highlight);
73 void SpellMenu::OnResumeState(SDL_Surface *) {
77 void SpellMenu::OnPauseState(SDL_Surface *) {
82 void SpellMenu::OnResize(int width, int height) {
87 void SpellMenu::HandleEvents(const Input &input) {
88 if (actionMenu.IsActive()) {
89 if (input.JustPressed(Input::PAD_LEFT)) {
90 actionMenu.PreviousItem();
92 if (input.JustPressed(Input::PAD_RIGHT)) {
93 actionMenu.NextItem();
96 if (input.JustPressed(Input::PAD_UP)) {
97 spellMenu.PreviousRow();
99 if (input.JustPressed(Input::PAD_RIGHT)) {
100 spellMenu.NextItem();
102 if (input.JustPressed(Input::PAD_DOWN)) {
105 if (input.JustPressed(Input::PAD_LEFT)) {
106 spellMenu.PreviousItem();
110 if (input.JustPressed(Input::ACTION_A)) {
111 if (actionMenu.IsActive()) {
112 if (actionMenu.Selected() == CHOICE_SORT) {
113 std::sort(GetHero().Spells().begin(),
114 GetHero().Spells().end(),
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()) {
128 std::swap(GetHero().Spells().at(spellMenu.SelectedIndex()),
129 GetHero().Spells().at(spellMenu.SecondaryIndex()));
130 spellMenu.SwapSelected();
131 spellMenu.SetActive();
135 if (input.JustPressed(Input::ACTION_B)) {
136 if (actionMenu.IsActive()) {
138 } else if (spellMenu.IsActive()) {
139 actionMenu.SetActive();
140 spellMenu.SetInactive();
142 spellMenu.SetActive();
147 void SpellMenu::UpdateWorld(float deltaT) {
151 void SpellMenu::Render(SDL_Surface *screen) {
152 const Font &font(*parent->Res().normalFont);
153 Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
154 Vector<int> menuOffset(font.CharWidth(), 13 * font.CharHeight() + font.CharHeight() / 8);
155 Vector<int> spellsOffset(font.CharWidth(), 16 * font.CharHeight() + font.CharHeight() / 8);
157 parent->RenderBackground(screen);
158 RenderHighlight(screen, offset);
159 parent->RenderHeros(screen, offset);
160 RenderMenu(screen, menuOffset + offset);
161 RenderSpells(screen, spellsOffset + offset);
164 int SpellMenu::Width() const {
165 return parent->Width();
168 int SpellMenu::Height() const {
169 return parent->Height();
172 void SpellMenu::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
173 if (cursor < 0) return;
174 Vector<int> statusOffset(parent->StatusOffset(cursor));
175 statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
177 rect.x = statusOffset.X();
178 rect.y = statusOffset.Y();
179 SDL_BlitSurface(highlight, 0, screen, &rect);
182 void SpellMenu::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) const {
183 const Font &font(*parent->Res().normalFont);
184 const Frame &frame(*parent->Res().statusFrame);
186 const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
187 const Vector<int> menuFrameOffset(offset.X() + 9 * font.CharWidth(), offset.Y());
188 const Vector<int> menuOffset(menuFrameOffset.X() + 3 * font.CharWidth(), menuFrameOffset.Y() + font.CharHeight());
190 frame.Draw(screen, offset, 9 * font.CharWidth(), 3 * font.CharHeight());
191 font.DrawString(parent->Res().mainMenuSpellText, screen, labelOffset + offset);
192 frame.Draw(screen, menuFrameOffset, 21 * font.CharWidth(), 3 * font.CharHeight());
193 actionMenu.Draw(screen, menuOffset);
196 void SpellMenu::RenderSpells(SDL_Surface *screen, const Vector<int> &offset) const {
197 const Font &font(*parent->Res().normalFont);
198 const Frame &frame(*parent->Res().statusFrame);
199 const Vector<int> menuOffset(3 * font.CharWidth(), font.CharHeight() + font.CharHeight() / 4);
201 frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
202 spellMenu.Draw(screen, offset + menuOffset);