]> git.localhorst.tv Git - blank.git/blob - shared/states.cpp
new gcc version
[blank.git] / shared / states.cpp
1 #include "ChatState.hpp"
2 #include "MessageState.hpp"
3 #include "ProgressState.hpp"
4
5 #include "../app/Environment.hpp"
6 #include "../io/event.hpp"
7
8 #include <iostream>
9
10
11 namespace blank {
12
13 ChatState::ChatState(Environment &env, State &parent, Responder &responder)
14 : env(env)
15 , parent(parent)
16 , responder(responder)
17 , preset()
18 , input(env.assets.small_ui_font) {
19         input.Position(glm::vec3(25.0f, -25.0f, -1.0f), Gravity::SOUTH_WEST, Gravity::SOUTH_WEST);
20         input.Width(env.viewport.Width() - 50.0f);
21         input.Foreground(PrimitiveMesh::Color(255));
22         input.Background(PrimitiveMesh::Color(127));
23 }
24
25 void ChatState::Preset(const std::string &text) {
26         preset = text;
27 }
28
29 void ChatState::Clear() {
30         preset.clear();
31 }
32
33 void ChatState::OnResume() {
34         OnResize(env.viewport);
35         input.Clear();
36         if (!preset.empty()) {
37                 input.Insert(preset.c_str());
38         }
39         input.Focus(env.viewport);
40 }
41
42 void ChatState::OnPause() {
43         input.Blur();
44 }
45
46 void ChatState::OnResize(Viewport &viewport) {
47         input.Width(viewport.Width() - 50.0f);
48 }
49
50
51 void ChatState::Handle(const SDL_Event &e) {
52         switch (e.type) {
53                 case SDL_KEYDOWN:
54                         switch (e.key.keysym.sym) {
55                                 case SDLK_ESCAPE:
56                                         Quit();
57                                         break;
58                                 case SDLK_KP_ENTER:
59                                 case SDLK_RETURN:
60                                         responder.OnLineSubmit(input.GetInput());
61                                         Quit();
62                                         break;
63
64                                 case SDLK_BACKSPACE:
65                                         input.Backspace();
66                                         break;
67                                 case SDLK_DELETE:
68                                         input.Delete();
69                                         break;
70
71                                 case SDLK_LEFT:
72                                         input.MoveBackward();
73                                         break;
74                                 case SDLK_RIGHT:
75                                         input.MoveForward();
76                                         break;
77
78                                 case SDLK_HOME:
79                                         input.MoveBegin();
80                                         break;
81                                 case SDLK_END:
82                                         input.MoveEnd();
83                                         break;
84
85                                 default:
86                                         break;
87                         }
88                         break;
89
90                 case SDL_QUIT:
91                         env.state.PopAll();
92                         break;
93
94                 case SDL_TEXTINPUT:
95                         input.Handle(e.text);
96                         break;
97
98                 case SDL_TEXTEDITING:
99                         std::cout << e << std::endl;
100                         input.Handle(e.edit);
101                         break;
102
103                 default:
104                         break;
105         }
106 }
107
108 void ChatState::Quit() {
109         env.state.PopUntil(this);
110 }
111
112 void ChatState::Update(int dt) {
113         parent.Update(dt);
114 }
115
116 void ChatState::Render(Viewport &viewport) {
117         parent.Render(viewport);
118         input.Render(viewport);
119 }
120
121
122 MessageState::MessageState(Environment &env)
123 : env(env) {
124         message.Position(glm::vec3(0.0f), Gravity::CENTER);
125         message.Hide();
126         press_key.Position(glm::vec3(0.0f, env.assets.large_ui_font.LineSkip(), 0.0f), Gravity::CENTER);
127         press_key.Set(env.assets.small_ui_font, "press any key to continue");
128         press_key.Show();
129 }
130
131 void MessageState::SetMessage(const char *msg) {
132         message.Set(env.assets.large_ui_font, msg);
133         message.Show();
134 }
135
136 void MessageState::ClearMessage() {
137         message.Hide();
138 }
139
140 void MessageState::Handle(const SDL_Event &e) {
141         if (e.type == SDL_QUIT || e.type == SDL_KEYDOWN) {
142                 env.state.Pop();
143         }
144 }
145
146 void MessageState::Update(int) {
147
148 }
149
150 void MessageState::Render(Viewport &viewport) {
151         if (message.Visible()) {
152                 message.Render(viewport);
153         }
154         if (press_key.Visible()) {
155                 press_key.Render(viewport);
156         }
157 }
158
159
160 ProgressState::ProgressState(Environment &env, const char *tpl)
161 : env(env)
162 , progress(env.assets.large_ui_font) {
163         progress.Position(glm::vec3(0.0f), Gravity::CENTER);
164         progress.Template(tpl);
165 }
166
167 void ProgressState::SetProgress(int value, int total) {
168         progress.Update(value, total);
169 }
170
171 void ProgressState::Handle(const SDL_Event &e) {
172         if (e.type == SDL_QUIT) {
173                 env.state.PopAll();
174         }
175 }
176
177 void ProgressState::Render(Viewport &viewport) {
178         progress.Render(viewport);
179 }
180
181 }