]> git.localhorst.tv Git - blank.git/blob - src/ui/ui.cpp
set and display block type labels
[blank.git] / src / ui / ui.cpp
1 #include "HUD.hpp"
2 #include "Interface.hpp"
3
4 #include "../app/Assets.hpp"
5 #include "../app/init.hpp"
6 #include "../graphics/BlendedSprite.hpp"
7 #include "../graphics/DirectionalLighting.hpp"
8 #include "../graphics/Font.hpp"
9 #include "../model/shapes.hpp"
10 #include "../world/World.hpp"
11
12 #include <algorithm>
13 #include <cmath>
14 #include <iostream>
15 #include <glm/gtc/matrix_transform.hpp>
16 #include <glm/gtx/io.hpp>
17
18
19 namespace blank {
20
21 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
22 : types(types)
23 , font(font)
24 , block()
25 , block_buf()
26 , block_transform(1.0f)
27 , block_label()
28 , label_sprite()
29 , label_transform(1.0f)
30 , label_color{0xFF, 0xFF, 0xFF, 0xFF}
31 , block_visible(false)
32 , crosshair()
33 , crosshair_transform(1.0f)
34 , near(100.0f)
35 , far(-100.0f)
36 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
37 , view(1.0f) {
38         block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
39         block_transform = glm::scale(block_transform, glm::vec3(50.0f));
40         block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
41         block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
42
43         crosshair.vertices = std::vector<glm::vec3>({
44                 { -10.0f,   0.0f, 0.0f }, { 10.0f,  0.0f, 0.0f },
45                 {   0.0f, -10.0f, 0.0f }, {  0.0f, 10.0f, 0.0f },
46         });
47         crosshair.indices = std::vector<OutlineModel::Index>({
48                 0, 1, 2, 3
49         });
50         crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
51         crosshair.Invalidate();
52 }
53
54
55 void HUD::Viewport(float width, float height) noexcept {
56         Viewport(0, 0, width, height);
57 }
58
59 void HUD::Viewport(float x, float y, float width, float height) noexcept {
60         projection = glm::ortho(x, width, height, y, near, far);
61         crosshair_transform = glm::translate(glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
62 }
63
64
65 void HUD::Display(const Block &b) {
66         const BlockType &type = types.Get(b.type);
67
68         block_buf.Clear();
69         type.FillModel(block_buf, b.Transform());
70         block.Update(block_buf);
71
72         font.Render(type.label.c_str(), label_color, block_label);
73         glm::vec2 size(font.TextSize(type.label.c_str()));
74         label_sprite.LoadRect(size.x, size.y);
75         label_transform = glm::translate(glm::vec3(
76                 std::max(5.0f, 50.0f - std::round(size.x * 0.5f)),
77                 70.0f + size.y,
78                 0.75f
79         ));
80
81         block_visible = type.visible;
82 }
83
84
85 void HUD::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
86         world_prog.Activate();
87         world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
88         // disable distance fog
89         world_prog.SetFogDensity(0.0f);
90         GLContext::ClearDepthBuffer();
91
92         world_prog.SetMVP(crosshair_transform, view, projection);
93         crosshair.Draw();
94
95         if (block_visible) {
96                 world_prog.SetM(block_transform);
97                 block.Draw();
98
99                 sprite_prog.Activate();
100                 sprite_prog.SetMVP(label_transform, view, projection);
101                 sprite_prog.SetTexture(block_label);
102                 label_sprite.Draw();
103         }
104 }
105
106
107 Interface::Interface(const Config &config, const Assets &assets, World &world)
108 : world(world)
109 , ctrl(world.Player())
110 , font(assets.LoadFont("DejaVuSans", 16))
111 , hud(world.BlockTypes(), font)
112 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
113 , aim_chunk(nullptr)
114 , aim_block(0)
115 , aim_normal()
116 , outline()
117 , outline_transform(1.0f)
118 , config(config)
119 , place_timer(256)
120 , remove_timer(256)
121 , remove(0)
122 , selection(1)
123 , fwd(0)
124 , rev(0) {
125         hud.Viewport(960, 600);
126         hud.Display(selection);
127 }
128
129
130 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
131         if (config.keyboard_disabled) return;
132
133         switch (event.keysym.sym) {
134                 case SDLK_w:
135                         rev.z = 1;
136                         break;
137                 case SDLK_s:
138                         fwd.z = 1;
139                         break;
140                 case SDLK_a:
141                         rev.x = 1;
142                         break;
143                 case SDLK_d:
144                         fwd.x = 1;
145                         break;
146                 case SDLK_SPACE:
147                         fwd.y = 1;
148                         break;
149                 case SDLK_LSHIFT:
150                         rev.y = 1;
151                         break;
152
153                 case SDLK_q:
154                         FaceBlock();
155                         break;
156                 case SDLK_e:
157                         TurnBlock();
158                         break;
159
160                 case SDLK_n:
161                         ToggleCollision();
162                         break;
163
164                 case SDLK_b:
165                         PrintBlockInfo();
166                         break;
167                 case SDLK_c:
168                         PrintChunkInfo();
169                         break;
170                 case SDLK_l:
171                         PrintLightInfo();
172                         break;
173                 case SDLK_p:
174                         PrintSelectionInfo();
175                         break;
176         }
177 }
178
179 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
180         if (config.keyboard_disabled) return;
181
182         switch (event.keysym.sym) {
183                 case SDLK_w:
184                         rev.z = 0;
185                         break;
186                 case SDLK_s:
187                         fwd.z = 0;
188                         break;
189                 case SDLK_a:
190                         rev.x = 0;
191                         break;
192                 case SDLK_d:
193                         fwd.x = 0;
194                         break;
195                 case SDLK_SPACE:
196                         fwd.y = 0;
197                         break;
198                 case SDLK_LSHIFT:
199                         rev.y = 0;
200                         break;
201         }
202 }
203
204 void Interface::FaceBlock() {
205         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
206         hud.Display(selection);
207 }
208
209 void Interface::TurnBlock() {
210         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
211         hud.Display(selection);
212 }
213
214 void Interface::ToggleCollision() {
215         ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
216         std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
217 }
218
219 void Interface::PrintBlockInfo() {
220         std::cout << std::endl;
221         if (!aim_chunk) {
222                 std::cout << "not looking at any block" << std::endl;
223                 Ray aim = ctrl.Aim();
224                 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
225                 return;
226         }
227         std::cout << "looking at block " << aim_block
228                 << " " << Chunk::ToCoords(aim_block)
229                 << " of chunk " << aim_chunk->Position()
230                 << std::endl;
231         Print(aim_chunk->BlockAt(aim_block));
232 }
233
234 void Interface::PrintChunkInfo() {
235         std::cout << std::endl;
236         if (!aim_chunk) {
237                 std::cout << "not looking at any block" << std::endl;
238                 return;
239         }
240         std::cout << "looking at chunk "
241                 << aim_chunk->Position()
242                 << std::endl;
243
244         std::cout << "  neighbors:" << std::endl;
245         if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
246                 std::cout << " left  " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
247         }
248         if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
249                 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
250         }
251         if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
252                 std::cout << " up    " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
253         }
254         if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
255                 std::cout << " down  " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
256         }
257         if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
258                 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
259         }
260         if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
261                 std::cout << " back  " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
262         }
263         std::cout << std::endl;
264 }
265
266 void Interface::PrintLightInfo() {
267         std::cout
268                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
269                 << " at position " << world.Player().Position()
270                 << std::endl;
271 }
272
273 void Interface::PrintSelectionInfo() {
274         std::cout << std::endl;
275         Print(selection);
276 }
277
278 void Interface::Print(const Block &block) {
279         std::cout << "type: " << block.type
280                 << ", face: " << block.GetFace()
281                 << ", turn: " << block.GetTurn()
282                 << std::endl;
283 }
284
285
286 void Interface::Handle(const SDL_MouseMotionEvent &event) {
287         if (config.mouse_disabled) return;
288         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
289         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
290 }
291
292 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
293         if (config.mouse_disabled) return;
294
295         if (event.button == SDL_BUTTON_LEFT) {
296                 RemoveBlock();
297                 remove_timer.Start();
298         } else if (event.button == SDL_BUTTON_MIDDLE) {
299                 PickBlock();
300         } else if (event.button == SDL_BUTTON_RIGHT) {
301                 PlaceBlock();
302                 place_timer.Start();
303         }
304 }
305
306 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
307         if (config.mouse_disabled) return;
308
309         if (event.button == SDL_BUTTON_LEFT) {
310                 remove_timer.Stop();
311         } else if (event.button == SDL_BUTTON_RIGHT) {
312                 place_timer.Stop();
313         }
314 }
315
316 void Interface::PickBlock() {
317         if (!aim_chunk) return;
318         selection = aim_chunk->BlockAt(aim_block);
319         hud.Display(selection);
320 }
321
322 void Interface::PlaceBlock() {
323         if (!aim_chunk) return;
324         Chunk *mod_chunk = aim_chunk;
325         glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
326         if (!Chunk::InBounds(next_pos)) {
327                 mod_chunk = &world.Next(*aim_chunk, aim_normal);
328                 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
329         }
330         mod_chunk->SetBlock(next_pos, selection);
331         mod_chunk->Invalidate();
332 }
333
334 void Interface::RemoveBlock() noexcept {
335         if (!aim_chunk) return;
336         aim_chunk->SetBlock(aim_block, remove);
337         aim_chunk->Invalidate();
338 }
339
340
341 void Interface::Handle(const SDL_MouseWheelEvent &event) {
342         if (config.mouse_disabled) return;
343
344         if (event.y < 0) {
345                 SelectNext();
346         } else if (event.y > 0) {
347                 SelectPrevious();
348         }
349 }
350
351 void Interface::SelectNext() {
352         ++selection.type;
353         if (size_t(selection.type) >= world.BlockTypes().Size()) {
354                 selection.type = 1;
355         }
356         hud.Display(selection);
357 }
358
359 void Interface::SelectPrevious() {
360         --selection.type;
361         if (selection.type <= 0) {
362                 selection.type = world.BlockTypes().Size() - 1;
363         }
364         hud.Display(selection);
365 }
366
367 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
368         if (event.event == SDL_WINDOWEVENT_RESIZED) {
369                 hud.Viewport(event.data1, event.data2);
370         }
371 }
372
373
374 void Interface::Update(int dt) {
375         ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
376         ctrl.Update(dt);
377
378         place_timer.Update(dt);
379         remove_timer.Update(dt);
380
381         aim = ctrl.Aim();
382         CheckAim();
383
384         if (remove_timer.Hit()) {
385                 RemoveBlock();
386                 CheckAim();
387         }
388
389         if (place_timer.Hit()) {
390                 PlaceBlock();
391                 CheckAim();
392         }
393 }
394
395 void Interface::CheckAim() {
396         float dist;
397         if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
398                 outline.Clear();
399                 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
400                 outline_transform = glm::scale(glm::vec3(1.0002f));
401                 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
402                 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
403         } else {
404                 aim_chunk = nullptr;
405         }
406 }
407
408
409 void Interface::Render(DirectionalLighting &world_prog, BlendedSprite &sprite_prog) noexcept {
410         if (config.visual_disabled) return;
411
412         if (aim_chunk) {
413                 world_prog.Activate();
414                 world_prog.SetM(outline_transform);
415                 outline.Draw();
416         }
417
418         hud.Render(world_prog, sprite_prog);
419 }
420
421 }