4 * Created on: Aug 8, 2012
8 #include "SelectSpell.h"
10 #include "SelectAttackType.h"
11 #include "SelectMoveAction.h"
12 #include "SelectTarget.h"
13 #include "../BattleState.h"
14 #include "../../app/Application.h"
15 #include "../../app/Input.h"
16 #include "../../common/Spell.h"
17 #include "../../geometry/operators.h"
18 #include "../../geometry/Point.h"
19 #include "../../graphics/Frame.h"
21 using app::Application;
24 using geometry::Point;
25 using geometry::Vector;
26 using graphics::Frame;
30 void SelectSpell::EnterState(Application &c, SDL_Surface *screen) {
34 void SelectSpell::ExitState(Application &c, SDL_Surface *screen) {
38 void SelectSpell::ResumeState(Application &ctrl, SDL_Surface *screen) {
39 if (battle->ActiveHeroTargets().HasSelected()) {
40 battle->SetAttackType(AttackChoice::MAGIC);
41 battle->ActiveHeroAttackChoice().SetSpell(battle->GetSpellMenu().Selected());
46 void SelectSpell::PauseState(Application &ctrl, SDL_Surface *screen) {
51 void SelectSpell::Resize(int width, int height) {
56 void SelectSpell::HandleEvents(const Input &input) {
57 if (input.JustPressed(Input::ACTION_A)) {
58 if (battle->GetSpellMenu().SelectedIsEnabled()) {
59 const Spell *spell(battle->GetSpellMenu().Selected());
60 battle->ActiveHeroTargets().Reset();
61 if (spell->GetTargetingMode().TargetsAlly()) {
62 battle->ActiveHeroTargets().SelectHeroes();
64 battle->ActiveHeroTargets().SelectEnemies();
66 if (spell->GetTargetingMode().TargetsAll()) {
67 battle->SetAttackType(AttackChoice::MAGIC);
68 // TODO: remove item from inventory
69 battle->ActiveHeroAttackChoice().SetSpell(spell);
73 if (spell->GetTargetingMode().TargetsSingle()) {
74 battle->ActiveHeroTargets().SetSingle();
76 battle->ActiveHeroTargets().SetMultiple();
78 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().magicTargetCursor));
82 if (input.JustPressed(Input::ACTION_B)) {
83 ctrl->PopState(); // return control to parent
85 if (input.JustPressed(Input::PAD_UP)) {
86 battle->GetSpellMenu().PreviousRow();
88 if (input.JustPressed(Input::PAD_RIGHT)) {
89 battle->GetSpellMenu().NextItem();
91 if (input.JustPressed(Input::PAD_DOWN)) {
92 battle->GetSpellMenu().NextRow();
94 if (input.JustPressed(Input::PAD_LEFT)) {
95 battle->GetSpellMenu().PreviousItem();
99 void SelectSpell::UpdateWorld(float deltaT) {
103 void SelectSpell::Render(SDL_Surface *screen) {
104 parent->Render(screen);
105 Vector<int> offset(battle->CalculateScreenOffset(screen));
106 RenderFrame(screen, offset);
107 RenderHeadline(screen, offset);
108 RenderMenu(screen, offset);
111 void SelectSpell::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
112 const Frame *frame(battle->Res().selectFrame);
113 Point<int> position(frame->BorderWidth(), frame->BorderHeight());
114 int width(battle->BackgroundWidth() - 2 * frame->BorderWidth());
115 int height(battle->Res().normalFont->CharHeight() * 13);
116 frame->Draw(screen, position + offset, width, height);
119 void SelectSpell::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
120 const Resources &res(battle->Res());
122 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
123 2 * res.selectFrame->BorderHeight());
124 res.normalFont->DrawString(res.spellMenuHeadline, screen, position + offset);
127 void SelectSpell::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
128 const Resources &res(battle->Res());
130 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
131 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
132 battle->GetSpellMenu().Draw(screen, position + offset);