]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectItem.cpp
offset target selection cursor from indicator
[l2e.git] / src / battle / states / SelectItem.cpp
1 /*
2  * SelectItem.cpp
3  *
4  *  Created on: Aug 9, 2012
5  *      Author: holy
6  */
7
8 #include "SelectItem.h"
9
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"
19
20 using app::Application;
21 using app::Input;
22 using common::Item;
23 using geometry::Point;
24 using geometry::Vector;
25 using graphics::Frame;
26
27 namespace battle {
28
29 void SelectItem::EnterState(Application &c, SDL_Surface *screen) {
30         ctrl = &c;
31 }
32
33 void SelectItem::ExitState(Application &c, SDL_Surface *screen) {
34         ctrl = 0;
35 }
36
37 void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) {
38         if (battle->ActiveHeroTargets().HasSelected()) {
39                 ctrl.PopState();
40         }
41 }
42
43 void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
44
45 }
46
47
48 void SelectItem::Resize(int width, int height) {
49
50 }
51
52
53 void SelectItem::HandleInput(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->TargetAlly()) {
59                                 battle->ActiveHeroTargets().SelectHeroes();
60                         } else {
61                                 battle->ActiveHeroTargets().SelectEnemies();
62                         }
63                         if (item->TargetAll()) {
64                                 battle->SetAttackType(AttackChoice::ITEM);
65                                 // TODO: remove item from inventory
66                                 battle->ActiveHeroAttackChoice().SetItem(item);
67                                 battle->NextHero();
68                                 ctrl->PopState();
69                         } else {
70                                 if (item->TargetOne()) {
71                                         battle->ActiveHeroTargets().SetSingle();
72                                 } else {
73                                         battle->ActiveHeroTargets().SetMultiple();
74                                 }
75                                 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().itemTargetCursor));
76                         }
77 //                      battle->SetAttackType(AttackChoice::ITEM);
78 //                      battle->NextHero();
79 //                      ctrl->PopState();
80                 }
81         }
82         if (input.JustPressed(Input::ACTION_B)) {
83                 ctrl->PopState(); // return control to parent
84         }
85         if (input.JustPressed(Input::PAD_UP)) {
86                 battle->GetItemMenu().PreviousRow();
87         }
88         if (input.JustPressed(Input::PAD_RIGHT)) {
89                 battle->GetItemMenu().NextItem();
90         }
91         if (input.JustPressed(Input::PAD_DOWN)) {
92                 battle->GetItemMenu().NextRow();
93         }
94         if (input.JustPressed(Input::PAD_LEFT)) {
95                 battle->GetItemMenu().PreviousItem();
96         }
97 }
98
99 void SelectItem::UpdateWorld(float deltaT) {
100
101 }
102
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);
109 }
110
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->BackgroundWidth() - 2 * frame->BorderWidth());
115         int height(battle->Res().normalFont->CharHeight() * 13);
116         frame->Draw(screen, position + offset, width, height);
117 }
118
119 void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
120         const Resources &res(battle->Res());
121         Point<int> position(
122                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
123                         2 * res.selectFrame->BorderHeight());
124         res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
125 }
126
127 void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
128         const Resources &res(battle->Res());
129         Point<int> position(
130                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
131                         2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
132         battle->GetItemMenu().Draw(screen, position + offset);
133 }
134
135 }