4 * Created on: Aug 9, 2012
8 #include "SelectItem.h"
10 #include "SelectAttackType.h"
11 #include "SelectTarget.h"
12 #include "../BattleState.h"
13 #include "../../app/Application.h"
14 #include "../../app/Input.h"
15 #include "../../common/Item.h"
16 #include "../../geometry/Point.h"
17 #include "../../geometry/operators.h"
18 #include "../../graphics/Frame.h"
20 using app::Application;
23 using geometry::Point;
24 using geometry::Vector;
25 using graphics::Frame;
29 void SelectItem::EnterState(Application &c, SDL_Surface *screen) {
33 void SelectItem::ExitState(Application &c, SDL_Surface *screen) {
37 void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) {
38 if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
39 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::ITEM);
40 battle->ActiveHero().GetAttackChoice().SetItem(battle->ItemMenu().Selected());
45 void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
50 void SelectItem::Resize(int width, int height) {
55 void SelectItem::HandleEvents(const Input &input) {
56 if (input.JustPressed(Input::ACTION_A)) {
57 if (battle->ItemMenu().SelectedIsEnabled()) {
58 AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
59 const Item *item(battle->ItemMenu().Selected());
60 ac.Selection().Reset();
61 if (item->GetTargetingMode().TargetsAlly()) {
62 ac.Selection().SelectHeroes();
64 ac.Selection().SelectMonsters();
66 if (item->GetTargetingMode().TargetsAll()) {
67 ac.SetType(AttackChoice::ITEM);
68 // TODO: remove item from inventory
73 if (item->GetTargetingMode().TargetsSingle()) {
74 ac.Selection().SetSingle();
76 ac.Selection().SetMultiple();
78 ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().itemTargetCursor));
82 if (input.JustPressed(Input::ACTION_B)) {
83 ctrl->PopState(); // return control to parent
85 if (input.JustPressed(Input::PAD_UP)) {
86 battle->ItemMenu().PreviousRow();
88 if (input.JustPressed(Input::PAD_RIGHT)) {
89 battle->ItemMenu().NextItem();
91 if (input.JustPressed(Input::PAD_DOWN)) {
92 battle->ItemMenu().NextRow();
94 if (input.JustPressed(Input::PAD_LEFT)) {
95 battle->ItemMenu().PreviousItem();
99 void SelectItem::UpdateWorld(float deltaT) {
103 void SelectItem::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 SelectItem::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->Width() - 2 * frame->BorderWidth());
115 int height(battle->Res().normalFont->CharHeight() * 13);
116 frame->Draw(screen, position + offset, width, height);
119 void SelectItem::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.itemMenuHeadline, screen, position + offset);
127 void SelectItem::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->ItemMenu().Draw(screen, position + offset);