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->ActiveHeroTargets().HasSelected()) {
43 void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
48 void SelectItem::Resize(int width, int height) {
53 void SelectItem::HandleEvents(const Input &input) {
54 if (input.JustPressed(Input::ACTION_A)) {
55 if (battle->GetItemMenu().SelectedIsEnabled()) {
56 const Item *item(battle->GetItemMenu().Selected());
57 battle->ActiveHeroTargets().Reset();
58 if (item->GetTargetingMode().TargetsAlly()) {
59 battle->ActiveHeroTargets().SelectHeroes();
61 battle->ActiveHeroTargets().SelectEnemies();
63 if (item->GetTargetingMode().TargetsAll()) {
64 battle->SetAttackType(AttackChoice::ITEM);
65 // TODO: remove item from inventory
66 battle->ActiveHeroAttackChoice().SetItem(item);
70 if (item->GetTargetingMode().TargetsSingle()) {
71 battle->ActiveHeroTargets().SetSingle();
73 battle->ActiveHeroTargets().SetMultiple();
75 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().itemTargetCursor));
79 if (input.JustPressed(Input::ACTION_B)) {
80 ctrl->PopState(); // return control to parent
82 if (input.JustPressed(Input::PAD_UP)) {
83 battle->GetItemMenu().PreviousRow();
85 if (input.JustPressed(Input::PAD_RIGHT)) {
86 battle->GetItemMenu().NextItem();
88 if (input.JustPressed(Input::PAD_DOWN)) {
89 battle->GetItemMenu().NextRow();
91 if (input.JustPressed(Input::PAD_LEFT)) {
92 battle->GetItemMenu().PreviousItem();
96 void SelectItem::UpdateWorld(float deltaT) {
100 void SelectItem::Render(SDL_Surface *screen) {
101 parent->Render(screen);
102 Vector<int> offset(battle->CalculateScreenOffset(screen));
103 RenderFrame(screen, offset);
104 RenderHeadline(screen, offset);
105 RenderMenu(screen, offset);
108 void SelectItem::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
109 const Frame *frame(battle->Res().selectFrame);
110 Point<int> position(frame->BorderWidth(), frame->BorderHeight());
111 int width(battle->BackgroundWidth() - 2 * frame->BorderWidth());
112 int height(battle->Res().normalFont->CharHeight() * 13);
113 frame->Draw(screen, position + offset, width, height);
116 void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
117 const Resources &res(battle->Res());
119 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
120 2 * res.selectFrame->BorderHeight());
121 res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
124 void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
125 const Resources &res(battle->Res());
127 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
128 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
129 battle->GetItemMenu().Draw(screen, position + offset);