]> git.localhorst.tv Git - l2e.git/blob - src/menu/SpellMenu.cpp
added spell menu dummy
[l2e.git] / src / menu / SpellMenu.cpp
1 /*
2  * SpellMenu.cpp
3  *
4  *  Created on: Nov 18, 2012
5  *      Author: holy
6  */
7
8 #include "SpellMenu.h"
9
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"
20
21 #include <SDL.h>
22 #include <vector>
23
24 using app::Input;
25 using common::Hero;
26 using common::Spell;
27 using geometry::Vector;
28 using graphics::Font;
29 using graphics::Frame;
30 using std::vector;
31
32 namespace menu {
33
34 SpellMenu::SpellMenu(PartyMenu *parent, int cursor)
35 : parent(parent)
36 , highlight(0)
37 , cursor(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);
42 }
43
44
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);
50
51         actionMenu.SetSelected();
52         LoadSpells();
53 }
54
55 void SpellMenu::LoadSpells() {
56         spellMenu.Clear();
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();
61                         i != end; ++i) {
62                 const Spell *spell = *i;
63                 spellMenu.Add(spell->Name(), spell, spell->CanUseOnStatusScreen(), 0, spell->Cost());
64         }
65 }
66
67 const Hero &SpellMenu::GetHero() const {
68         return *parent->Game().state->party[cursor];
69 }
70
71 void SpellMenu::OnExitState(SDL_Surface *) {
72         SDL_FreeSurface(highlight);
73 }
74
75 void SpellMenu::OnResumeState(SDL_Surface *) {
76
77 }
78
79 void SpellMenu::OnPauseState(SDL_Surface *) {
80
81 }
82
83
84 void SpellMenu::OnResize(int width, int height) {
85
86 }
87
88
89 void SpellMenu::HandleEvents(const Input &input) {
90         if (actionMenu.IsActive()) {
91                 if (input.JustPressed(Input::PAD_LEFT)) {
92                         actionMenu.PreviousItem();
93                 }
94                 if (input.JustPressed(Input::PAD_RIGHT)) {
95                         actionMenu.NextItem();
96                 }
97         } else {
98                 if (input.JustPressed(Input::PAD_UP)) {
99                         spellMenu.PreviousRow();
100                 }
101                 if (input.JustPressed(Input::PAD_RIGHT)) {
102                         spellMenu.NextItem();
103                 }
104                 if (input.JustPressed(Input::PAD_DOWN)) {
105                         spellMenu.NextRow();
106                 }
107                 if (input.JustPressed(Input::PAD_LEFT)) {
108                         spellMenu.PreviousItem();
109                 }
110         }
111
112         if (input.JustPressed(Input::ACTION_A)) {
113                 if (actionMenu.IsActive()) {
114                         if (actionMenu.Selected() == CHOICE_SORT) {
115                                 // TODO: sort spells
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                         // TODO: swap spells
129                         spellMenu.SwapSelected();
130                         spellMenu.SetActive();
131                 }
132         }
133
134         if (input.JustPressed(Input::ACTION_B)) {
135                 if (actionMenu.IsActive()) {
136                         Ctrl().PopState();
137                 } else if (spellMenu.IsActive()) {
138                         actionMenu.SetActive();
139                         spellMenu.SetInactive();
140                 } else {
141                         spellMenu.SetActive();
142                 }
143         }
144 }
145
146 void SpellMenu::UpdateWorld(float deltaT) {
147
148 }
149
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);
155
156         parent->RenderBackground(screen);
157         RenderHighlight(screen, offset);
158         parent->RenderHeros(screen, offset);
159         RenderMenu(screen, menuOffset + offset);
160         RenderSpells(screen, spellsOffset + offset);
161 }
162
163 int SpellMenu::Width() const {
164         return parent->Width();
165 }
166
167 int SpellMenu::Height() const {
168         return parent->Height();
169 }
170
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);
175         SDL_Rect rect;
176         rect.x = statusOffset.X();
177         rect.y = statusOffset.Y();
178         SDL_BlitSurface(highlight, 0, screen, &rect);
179 }
180
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);
184
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());
188
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);
193 }
194
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);
199
200         frame.Draw(screen, offset, 30 * font.CharWidth(), 11 * font.CharHeight());
201         spellMenu.Draw(screen, offset + menuOffset);
202 }
203
204 }