]> git.localhorst.tv Git - l2e.git/blob - src/menu/SpellMenu.cpp
removed stupid file headers that eclipse put in
[l2e.git] / src / menu / SpellMenu.cpp
1 #include "SpellMenu.h"
2
3 #include "HeroStatus.h"
4 #include "PartyMenu.h"
5 #include "Resources.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"
13
14 #include <algorithm>
15 #include <SDL.h>
16 #include <vector>
17
18 using app::Input;
19 using common::Hero;
20 using common::Spell;
21 using geometry::Vector;
22 using graphics::Font;
23 using graphics::Frame;
24 using std::vector;
25
26 namespace menu {
27
28 SpellMenu::SpellMenu(PartyMenu *parent, int cursor)
29 : parent(parent)
30 , highlight(0)
31 , cursor(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);
36 }
37
38
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);
44
45         actionMenu.SetSelected();
46         LoadSpells();
47 }
48
49 void SpellMenu::LoadSpells() {
50         spellMenu.Clear();
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();
55                         i != end; ++i) {
56                 const Spell *spell = *i;
57                 spellMenu.Add(spell->Name(), spell, spell->CanUseOnStatusScreen(), 0, spell->Cost());
58         }
59 }
60
61 const Hero &SpellMenu::GetHero() const {
62         return *parent->Game().state->party[cursor];
63 }
64
65 Hero &SpellMenu::GetHero() {
66         return *parent->Game().state->party[cursor];
67 }
68
69 void SpellMenu::OnExitState(SDL_Surface *) {
70         SDL_FreeSurface(highlight);
71 }
72
73 void SpellMenu::OnResumeState(SDL_Surface *) {
74
75 }
76
77 void SpellMenu::OnPauseState(SDL_Surface *) {
78
79 }
80
81
82 void SpellMenu::OnResize(int width, int height) {
83
84 }
85
86
87 void SpellMenu::HandleEvents(const Input &input) {
88         if (actionMenu.IsActive()) {
89                 if (input.JustPressed(Input::PAD_LEFT)) {
90                         actionMenu.PreviousItem();
91                 }
92                 if (input.JustPressed(Input::PAD_RIGHT)) {
93                         actionMenu.NextItem();
94                 }
95         } else {
96                 if (input.JustPressed(Input::PAD_UP)) {
97                         spellMenu.PreviousRow();
98                 }
99                 if (input.JustPressed(Input::PAD_RIGHT)) {
100                         spellMenu.NextItem();
101                 }
102                 if (input.JustPressed(Input::PAD_DOWN)) {
103                         spellMenu.NextRow();
104                 }
105                 if (input.JustPressed(Input::PAD_LEFT)) {
106                         spellMenu.PreviousItem();
107                 }
108         }
109
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(),
115                                                 Spell::Less);
116                                 LoadSpells();
117                         } else {
118                                 actionMenu.SetSelected();
119                                 spellMenu.SetActive();
120                         }
121                 } else if (spellMenu.IsActive()) {
122                         spellMenu.SetDualSelection();
123                 } else if (spellMenu.SelectedIndex() == spellMenu.SecondaryIndex()) {
124                         if (spellMenu.Selected()->CanUseOnStatusScreen()) {
125                                 // TODO: use spell
126                         }
127                 } else {
128                         std::swap(GetHero().Spells().at(spellMenu.SelectedIndex()),
129                                         GetHero().Spells().at(spellMenu.SecondaryIndex()));
130                         spellMenu.SwapSelected();
131                         spellMenu.SetActive();
132                 }
133         }
134
135         if (input.JustPressed(Input::ACTION_B)) {
136                 if (actionMenu.IsActive()) {
137                         Ctrl().PopState();
138                 } else if (spellMenu.IsActive()) {
139                         actionMenu.SetActive();
140                         spellMenu.SetInactive();
141                 } else {
142                         spellMenu.SetActive();
143                 }
144         }
145 }
146
147 void SpellMenu::UpdateWorld(float deltaT) {
148
149 }
150
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);
156
157         parent->RenderBackground(screen);
158         RenderHighlight(screen, offset);
159         parent->RenderHeros(screen, offset);
160         RenderMenu(screen, menuOffset + offset);
161         RenderSpells(screen, spellsOffset + offset);
162 }
163
164 int SpellMenu::Width() const {
165         return parent->Width();
166 }
167
168 int SpellMenu::Height() const {
169         return parent->Height();
170 }
171
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);
176         SDL_Rect rect;
177         rect.x = statusOffset.X();
178         rect.y = statusOffset.Y();
179         SDL_BlitSurface(highlight, 0, screen, &rect);
180 }
181
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);
185
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());
189
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);
194 }
195
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);
200
201         frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
202         spellMenu.Draw(screen, offset + menuOffset);
203 }
204
205 }