]> git.localhorst.tv Git - l2e.git/blob - src/menu/CapsuleNameMenu.cpp
removed lazy fwd headers
[l2e.git] / src / menu / CapsuleNameMenu.cpp
1 #include "CapsuleNameMenu.h"
2
3 #include "CapsuleMenu.h"
4 #include "Resources.h"
5 #include "../app/Application.h"
6 #include "../app/Input.h"
7 #include "../common/Inventory.h"
8 #include "../common/Item.h"
9 #include "../common/GameConfig.h"
10 #include "../common/GameState.h"
11 #include "../graphics/Font.h"
12 #include "../graphics/Frame.h"
13 #include "../math/Vector.h"
14
15 #include <cstring>
16
17 using app::Input;
18 using common::Capsule;
19 using common::Inventory;
20 using common::Item;
21 using math::Vector;
22 using graphics::Font;
23 using graphics::Frame;
24
25 namespace menu {
26
27 CapsuleNameMenu::CapsuleNameMenu(CapsuleMenu *parent)
28 : parent(parent)
29 , select(*parent->Res().capsuleNameCharSelectTemplate)
30 , cursor(5)
31 , first(true) {
32         bzero(buffer, 6);
33         std::strncpy(buffer, GetCapsule().Name(), 6);
34         for (int i = 4; i > 0; --i) {
35                 if (buffer[i] == '\0') {
36                         buffer[i] = '_';
37                 }
38         }
39 }
40
41
42 void CapsuleNameMenu::OnEnterState(SDL_Surface *) {
43
44 }
45
46 void CapsuleNameMenu::OnExitState(SDL_Surface *) {
47
48 }
49
50 void CapsuleNameMenu::OnResumeState(SDL_Surface *) {
51
52 }
53
54 void CapsuleNameMenu::OnPauseState(SDL_Surface *) {
55
56 }
57
58
59 void CapsuleNameMenu::OnResize(int width, int height) {
60
61 }
62
63
64 void CapsuleNameMenu::HandleEvents(const Input &input) {
65         if (input.JustPressed(Input::PAD_UP)) {
66                 select.PreviousRow();
67         }
68         if (input.JustPressed(Input::PAD_RIGHT)) {
69                 select.NextCol();
70         }
71         if (input.JustPressed(Input::PAD_DOWN)) {
72                 select.NextRow();
73         }
74         if (input.JustPressed(Input::PAD_LEFT)) {
75                 select.PreviousCol();
76         }
77
78         if (input.JustPressed(Input::ACTION_A)) {
79                 AddChar();
80         }
81         if (input.JustPressed(Input::ACTION_B)) {
82                 RemoveChar();
83         }
84
85         if (input.JustPressed(Input::START) && cursor > 0) {
86                 StoreName();
87                 Ctrl().PopState();
88         }
89 }
90
91 void CapsuleNameMenu::AddChar() {
92         if (first) {
93                 cursor = 1;
94                 buffer[0] = select.Selected();
95                 for (int i = 1; i < 5; ++i) {
96                         buffer[i] = '_';
97                 }
98                 first = false;
99         } else {
100                 if (cursor < 5) {
101                         buffer[cursor] = select.Selected();
102                         ++cursor;
103                 } else {
104                         // noise
105                 }
106         }
107 }
108
109 void CapsuleNameMenu::RemoveChar() {
110         first = false;
111         if (cursor > 0) {
112                 --cursor;
113                 buffer[cursor] = '_';
114         }
115 }
116
117 void CapsuleNameMenu::StoreName() {
118         // NOTE: this will leak the memory allocated for the new name
119         char *name = 0;
120         for (int i = 0; i < 6; ++i) {
121                 if (buffer[i] == '_' || buffer[i] == '\0') {
122                         buffer[i] = '\0';
123                         name = new char[i + 1];
124                         std::strncpy(name, buffer, i + 1);
125                         break;
126                 }
127         }
128         if (name != 0) {
129                 GetCapsule().SetName(name);
130         }
131 }
132
133 void CapsuleNameMenu::UpdateWorld(Uint32 deltaT) {
134
135 }
136
137 void CapsuleNameMenu::Render(SDL_Surface *screen) {
138         const Font &font(*parent->Res().statusFont);
139         const Vector<int> offset((screen->w - Width()) / 2, (screen->h - Height()) / 2);
140         const Vector<int> nameOffset(
141                         4 * font.CharWidth(),
142                         4 * font.CharHeight() - font.CharHeight() / 8);
143         const Vector<int> alphaOffset(
144                         4 * font.CharWidth(),
145                         7 * font.CharHeight() - font.CharHeight() / 8);
146
147         parent->RenderBackground(screen);
148         RenderName(screen, offset + nameOffset);
149         RenderAlphabet(screen, offset + alphaOffset);
150 }
151
152 void CapsuleNameMenu::RenderName(SDL_Surface *screen, const Vector<int> &offset) const {
153         const Font &font(*parent->Res().normalFont);
154         const Frame &frame(*parent->Res().statusFrame);
155         const Vector<int> labelOffset(2 * font.CharWidth(), font.CharHeight());
156         const Vector<int> namebgOffset(8 * font.CharWidth(), 0);
157         const Vector<int> nameOffset(13 * font.CharWidth(), font.CharHeight());
158
159         frame.Draw(screen, offset, 8 * font.CharWidth(), 3 * font.CharHeight());
160         font.DrawString(parent->Res().capsuleNameLabel, screen, offset + labelOffset, 5);
161         frame.Draw(screen, offset + namebgOffset, 16 * font.CharWidth(), 3 * font.CharHeight());
162         font.DrawString(buffer, screen, offset + nameOffset, 5);
163 }
164
165 void CapsuleNameMenu::RenderAlphabet(SDL_Surface *screen, const Vector<int> &offset) const {
166         const Font &font(*parent->Res().normalFont);
167         const Frame &frame(*parent->Res().statusFrame);
168         const Vector<int> selectOffset(2 * font.CharWidth(), 2 * font.CharHeight());
169
170         frame.Draw(screen, offset, 24 * font.CharWidth(), 17 * font.CharHeight());
171         select.Draw(screen, offset + selectOffset);
172 }
173
174
175 int CapsuleNameMenu::Width() const {
176         return parent->Width();
177 }
178
179 int CapsuleNameMenu::Height() const {
180         return parent->Height();
181 }
182
183 Capsule &CapsuleNameMenu::GetCapsule() {
184         return parent->GetCapsule();
185 }
186
187 const Capsule &CapsuleNameMenu::GetCapsule() const {
188         return parent->GetCapsule();
189 }
190
191 }