]> git.localhorst.tv Git - l2e.git/blob - src/menu/ChangeHero.cpp
7cd9b08a3d1c3e22636fe8f0ef2833fcb2ead365
[l2e.git] / src / menu / ChangeHero.cpp
1 #include "ChangeHero.h"
2
3 #include "HeroStatus.h"
4 #include "PartyMenu.h"
5 #include "Resources.h"
6 #include "SelectHero.h"
7 #include "../app/Application.h"
8 #include "../app/Input.h"
9 #include "../common/GameConfig.h"
10 #include "../common/GameState.h"
11
12 #include <algorithm>
13
14 using app::Input;
15 using geometry::Vector;
16 using std::swap;
17
18 namespace menu {
19
20 ChangeHero::ChangeHero(PartyMenu *parent)
21 : parent(parent)
22 , highlight(0)
23 , selection(0) {
24
25 }
26
27
28 void ChangeHero::OnEnterState(SDL_Surface *) {
29         const HeroStatus &status(parent->GetHeroStatus(0));
30         highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0);
31         SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF));
32         SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20);
33 }
34
35 void ChangeHero::OnExitState(SDL_Surface *) {
36         SDL_FreeSurface(highlight);
37 }
38
39 void ChangeHero::OnResumeState(SDL_Surface *) {
40         if (selection < 0) {
41                 Ctrl().PopState();
42         } else {
43                 selection = -1;
44                 Ctrl().PushState(new SelectHero(this, parent, this, OnHeroSelected));
45         }
46 }
47
48 void ChangeHero::OnPauseState(SDL_Surface *) {
49
50 }
51
52
53 void ChangeHero::OnResize(int width, int height) {
54
55 }
56
57
58 int ChangeHero::Width() const {
59         return parent->Width();
60 }
61
62 int ChangeHero::Height() const {
63         return parent->Height();
64 }
65
66
67 void ChangeHero::HandleEvents(const Input &input) {
68
69 }
70
71 void ChangeHero::UpdateWorld(float deltaT) {
72
73 }
74
75 void ChangeHero::Render(SDL_Surface *screen) {
76         Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
77
78         parent->RenderBackground(screen);
79         RenderHighlight(screen, offset);
80         parent->RenderHeros(screen, offset);
81         parent->RenderMenu(screen, offset + Vector<int>(8 * parent->Res().normalFont->CharWidth(), 13 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8));
82         parent->RenderInfo(screen, offset + Vector<int>(14 * parent->Res().normalFont->CharWidth(), 21 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8));
83 }
84
85 void ChangeHero::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
86         if (selection < 0) return;
87         Vector<int> statusOffset(parent->StatusOffset(selection));
88         statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
89         SDL_Rect rect;
90         rect.x = statusOffset.X();
91         rect.y = statusOffset.Y();
92         SDL_BlitSurface(highlight, 0, screen, &rect);
93 }
94
95
96 void ChangeHero::OnHeroSelected(void *ref, int index) {
97         ChangeHero *self(reinterpret_cast<ChangeHero *>(ref));
98         self->SelectedHero(index);
99 }
100
101 void ChangeHero::SelectedHero(int index) {
102         if (selection < 0) {
103                 selection = index;
104         } else {
105                 if (index != selection) {
106                         swap(parent->Game().state->party[selection],
107                                         parent->Game().state->party[index]);
108                 }
109                 selection = -1;
110         }
111 }
112
113 }