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->ActiveHeroAttackChoice().Selection().HasSelected()) {
39 battle->ActiveHeroAttackChoice().SetType(AttackChoice::ITEM);
40 battle->ActiveHeroAttackChoice().SetItem(battle->GetItemMenu().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->GetItemMenu().SelectedIsEnabled()) {
58 const Item *item(battle->GetItemMenu().Selected());
59 battle->ActiveHeroAttackChoice().Selection().Reset();
60 if (item->GetTargetingMode().TargetsAlly()) {
61 battle->ActiveHeroAttackChoice().Selection().SelectHeroes();
63 battle->ActiveHeroAttackChoice().Selection().SelectEnemies();
65 if (item->GetTargetingMode().TargetsAll()) {
66 battle->ActiveHeroAttackChoice().SetType(AttackChoice::ITEM);
67 // TODO: remove item from inventory
68 battle->ActiveHeroAttackChoice().SetItem(item);
72 if (item->GetTargetingMode().TargetsSingle()) {
73 battle->ActiveHeroAttackChoice().Selection().SetSingle();
75 battle->ActiveHeroAttackChoice().Selection().SetMultiple();
77 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroAttackChoice().Selection(), battle->Res().itemTargetCursor));
81 if (input.JustPressed(Input::ACTION_B)) {
82 ctrl->PopState(); // return control to parent
84 if (input.JustPressed(Input::PAD_UP)) {
85 battle->GetItemMenu().PreviousRow();
87 if (input.JustPressed(Input::PAD_RIGHT)) {
88 battle->GetItemMenu().NextItem();
90 if (input.JustPressed(Input::PAD_DOWN)) {
91 battle->GetItemMenu().NextRow();
93 if (input.JustPressed(Input::PAD_LEFT)) {
94 battle->GetItemMenu().PreviousItem();
98 void SelectItem::UpdateWorld(float deltaT) {
102 void SelectItem::Render(SDL_Surface *screen) {
103 parent->Render(screen);
104 Vector<int> offset(battle->CalculateScreenOffset(screen));
105 RenderFrame(screen, offset);
106 RenderHeadline(screen, offset);
107 RenderMenu(screen, offset);
110 void SelectItem::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
111 const Frame *frame(battle->Res().selectFrame);
112 Point<int> position(frame->BorderWidth(), frame->BorderHeight());
113 int width(battle->Width() - 2 * frame->BorderWidth());
114 int height(battle->Res().normalFont->CharHeight() * 13);
115 frame->Draw(screen, position + offset, width, height);
118 void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
119 const Resources &res(battle->Res());
121 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
122 2 * res.selectFrame->BorderHeight());
123 res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
126 void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
127 const Resources &res(battle->Res());
129 2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
130 2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
131 battle->GetItemMenu().Draw(screen, position + offset);