+/*
+ * ChangeHero.cpp
+ *
+ * Created on: Nov 4, 2012
+ * Author: holy
+ */
+
+#include "ChangeHero.h"
+
+#include "HeroStatus.h"
+#include "PartyMenu.h"
+#include "Resources.h"
+#include "SelectHero.h"
+#include "../app/Application.h"
+#include "../app/Input.h"
+#include "../common/GameConfig.h"
+#include "../common/GameState.h"
+
+#include <algorithm>
+
+using app::Input;
+using geometry::Vector;
+using std::swap;
+
+namespace menu {
+
+ChangeHero::ChangeHero(PartyMenu *parent)
+: parent(parent)
+, highlight(0)
+, selection(0) {
+
+}
+
+
+void ChangeHero::OnEnterState(SDL_Surface *) {
+ const HeroStatus &status(parent->GetHeroStatus(0));
+ highlight = SDL_CreateRGBSurface(0, status.Width(), status.Height(), 32, 0xFF000000, 0xFF0000, 0xFF00, 0);
+ SDL_FillRect(highlight, 0, SDL_MapRGB(highlight->format, 0xFF, 0xFF, 0xFF));
+ SDL_SetAlpha(highlight, SDL_SRCALPHA|SDL_RLEACCEL, 0x20);
+}
+
+void ChangeHero::OnExitState(SDL_Surface *) {
+ SDL_FreeSurface(highlight);
+}
+
+void ChangeHero::OnResumeState(SDL_Surface *) {
+ if (selection < 0) {
+ Ctrl().PopState();
+ } else {
+ selection = -1;
+ Ctrl().PushState(new SelectHero(this, parent, this, OnHeroSelected));
+ }
+}
+
+void ChangeHero::OnPauseState(SDL_Surface *) {
+
+}
+
+
+void ChangeHero::OnResize(int width, int height) {
+
+}
+
+
+int ChangeHero::Width() const {
+ return parent->Width();
+}
+
+int ChangeHero::Height() const {
+ return parent->Height();
+}
+
+
+void ChangeHero::HandleEvents(const Input &input) {
+
+}
+
+void ChangeHero::UpdateWorld(float deltaT) {
+
+}
+
+void ChangeHero::Render(SDL_Surface *screen) {
+ Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
+
+ parent->RenderBackground(screen);
+ RenderHighlight(screen, offset);
+ parent->RenderHeros(screen, offset);
+ parent->RenderMenu(screen, offset + Vector<int>(8 * parent->Res().normalFont->CharWidth(), 13 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8));
+ parent->RenderInfo(screen, offset + Vector<int>(14 * parent->Res().normalFont->CharWidth(), 21 * parent->Res().normalFont->CharHeight() + parent->Res().normalFont->CharHeight() / 8));
+}
+
+void ChangeHero::RenderHighlight(SDL_Surface *screen, const Vector<int> &offset) const {
+ if (selection < 0) return;
+ Vector<int> statusOffset(parent->StatusOffset(selection));
+ statusOffset -= Vector<int>(0, parent->Res().normalFont->CharHeight() / 8);
+ SDL_Rect rect;
+ rect.x = statusOffset.X();
+ rect.y = statusOffset.Y();
+ SDL_BlitSurface(highlight, 0, screen, &rect);
+}
+
+
+void ChangeHero::OnHeroSelected(void *ref, int index) {
+ ChangeHero *self(reinterpret_cast<ChangeHero *>(ref));
+ self->SelectedHero(index);
+}
+
+void ChangeHero::SelectedHero(int index) {
+ if (selection < 0) {
+ selection = index;
+ } else {
+ if (index != selection) {
+ swap(parent->Game().state->party[selection],
+ parent->Game().state->party[index]);
+ }
+ selection = -1;
+ }
+}
+
+}