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