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