1 #include "SelectItem.h"
3 #include "SelectAttackType.h"
4 #include "SelectTarget.h"
5 #include "../BattleState.h"
6 #include "../../app/Application.h"
7 #include "../../app/Input.h"
8 #include "../../common/Item.h"
9 #include "../../graphics/Frame.h"
10 #include "../../math/Vector.h"
12 using app::Application;
16 using graphics::Frame;
20 SelectItem::SelectItem(Battle *battle, SelectAttackType *parent)
27 void SelectItem::OnEnterState(SDL_Surface *screen) {
28 OnResize(screen->w, screen->h);
31 void SelectItem::OnExitState(SDL_Surface *screen) {
35 void SelectItem::OnResumeState(SDL_Surface *screen) {
36 if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
37 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::ITEM);
38 battle->ActiveHero().GetAttackChoice().SetItem(parent->ItemMenu().Selected());
43 void SelectItem::OnPauseState(SDL_Surface *screen) {
48 void SelectItem::OnResize(int width, int height) {
49 const Vector<int> offset = parent->ScreenOffset();
51 const Resources &res = parent->Res();
52 const Frame &frame = *res.selectFrame;
54 framePosition = offset + frame.BorderSize();
55 frameSize = Vector<int>(
56 parent->Width() - 2 * frame.BorderWidth(),
57 res.normalFont->CharHeight() * 13);
59 headlinePosition = offset + Vector<int>(
60 2 * frame.BorderWidth() + res.normalFont->CharWidth(),
61 2 * frame.BorderHeight());
63 menuPosition = offset + Vector<int>(
64 2 * frame.BorderWidth() + res.normalFont->CharWidth(),
65 2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight());
69 void SelectItem::HandleEvents(const Input &input) {
70 if (input.JustPressed(Input::ACTION_A)) {
71 if (parent->ItemMenu().SelectedIsEnabled()) {
72 AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
73 const Item *item(parent->ItemMenu().Selected());
74 ac.Selection().Reset();
75 if (item->GetTargetingMode().TargetsAlly()) {
76 ac.Selection().SelectHeroes();
78 ac.Selection().SelectMonsters();
80 if (item->GetTargetingMode().TargetsAll()) {
81 ac.SetType(AttackChoice::ITEM);
86 if (item->GetTargetingMode().TargetsSingle()) {
87 ac.Selection().SetSingle();
89 ac.Selection().SetMultiple();
91 Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), parent->Res().itemTargetCursor));
95 if (input.JustPressed(Input::ACTION_B)) {
96 Ctrl().PopState(); // return control to parent
98 if (input.JustPressed(Input::PAD_UP)) {
99 parent->ItemMenu().PreviousRow();
101 if (input.JustPressed(Input::PAD_RIGHT)) {
102 parent->ItemMenu().NextItem();
104 if (input.JustPressed(Input::PAD_DOWN)) {
105 parent->ItemMenu().NextRow();
107 if (input.JustPressed(Input::PAD_LEFT)) {
108 parent->ItemMenu().PreviousItem();
112 void SelectItem::UpdateWorld(Uint32 deltaT) {
116 void SelectItem::Render(SDL_Surface *screen) {
117 parent->Render(screen);
119 RenderHeadline(screen);
123 void SelectItem::RenderFrame(SDL_Surface *screen) {
124 const Frame &frame = *parent->Res().selectFrame;
125 frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
128 void SelectItem::RenderHeadline(SDL_Surface *screen) {
129 const Resources &res = parent->Res();
130 res.normalFont->DrawString(res.itemMenuHeadline, screen, headlinePosition);
133 void SelectItem::RenderMenu(SDL_Surface *screen) {
134 parent->ItemMenu().Draw(screen, menuPosition);