]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectItem.cpp
added loading callback for type descriptions
[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 "../../graphics/Frame.h"
17
18 using app::Application;
19 using app::Input;
20 using common::Item;
21 using geometry::Vector;
22 using graphics::Frame;
23
24 namespace battle {
25
26 void SelectItem::EnterState(Application &c, SDL_Surface *screen) {
27         ctrl = &c;
28 }
29
30 void SelectItem::ExitState(Application &c, SDL_Surface *screen) {
31         ctrl = 0;
32 }
33
34 void SelectItem::ResumeState(Application &ctrl, SDL_Surface *screen) {
35         if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
36                 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::ITEM);
37                 battle->ActiveHero().GetAttackChoice().SetItem(battle->ItemMenu().Selected());
38                 ctrl.PopState();
39         }
40 }
41
42 void SelectItem::PauseState(Application &ctrl, SDL_Surface *screen) {
43
44 }
45
46
47 void SelectItem::Resize(int width, int height) {
48
49 }
50
51
52 void SelectItem::HandleEvents(const Input &input) {
53         if (input.JustPressed(Input::ACTION_A)) {
54                 if (battle->ItemMenu().SelectedIsEnabled()) {
55                         AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
56                         const Item *item(battle->ItemMenu().Selected());
57                         ac.Selection().Reset();
58                         if (item->GetTargetingMode().TargetsAlly()) {
59                                 ac.Selection().SelectHeroes();
60                         } else {
61                                 ac.Selection().SelectMonsters();
62                         }
63                         if (item->GetTargetingMode().TargetsAll()) {
64                                 ac.SetType(AttackChoice::ITEM);
65                                 ac.SetItem(item);
66                                 battle->NextHero();
67                                 ctrl->PopState();
68                         } else {
69                                 if (item->GetTargetingMode().TargetsSingle()) {
70                                         ac.Selection().SetSingle();
71                                 } else {
72                                         ac.Selection().SetMultiple();
73                                 }
74                                 ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().itemTargetCursor));
75                         }
76                 }
77         }
78         if (input.JustPressed(Input::ACTION_B)) {
79                 ctrl->PopState(); // return control to parent
80         }
81         if (input.JustPressed(Input::PAD_UP)) {
82                 battle->ItemMenu().PreviousRow();
83         }
84         if (input.JustPressed(Input::PAD_RIGHT)) {
85                 battle->ItemMenu().NextItem();
86         }
87         if (input.JustPressed(Input::PAD_DOWN)) {
88                 battle->ItemMenu().NextRow();
89         }
90         if (input.JustPressed(Input::PAD_LEFT)) {
91                 battle->ItemMenu().PreviousItem();
92         }
93 }
94
95 void SelectItem::UpdateWorld(float deltaT) {
96
97 }
98
99 void SelectItem::Render(SDL_Surface *screen) {
100         parent->Render(screen);
101         Vector<int> offset(battle->CalculateScreenOffset(screen));
102         RenderFrame(screen, offset);
103         RenderHeadline(screen, offset);
104         RenderMenu(screen, offset);
105 }
106
107 void SelectItem::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
108         const Frame *frame(battle->Res().selectFrame);
109         Vector<int> position(frame->BorderSize());
110         int width(battle->Width() - 2 * frame->BorderWidth());
111         int height(battle->Res().normalFont->CharHeight() * 13);
112         frame->Draw(screen, position + offset, width, height);
113 }
114
115 void SelectItem::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
116         const Resources &res(battle->Res());
117         Vector<int> position(
118                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
119                         2 * res.selectFrame->BorderHeight());
120         res.normalFont->DrawString(res.itemMenuHeadline, screen, position + offset);
121 }
122
123 void SelectItem::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
124         const Resources &res(battle->Res());
125         Vector<int> position(
126                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
127                         2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
128         battle->ItemMenu().Draw(screen, position + offset);
129 }
130
131 }