2 #include "Interface.hpp"
5 #include "../app/Assets.hpp"
6 #include "../app/Environment.hpp"
7 #include "../app/FrameCounter.hpp"
8 #include "../app/init.hpp"
9 #include "../audio/Audio.hpp"
10 #include "../graphics/Font.hpp"
11 #include "../graphics/Viewport.hpp"
12 #include "../io/TokenStreamReader.hpp"
13 #include "../model/shapes.hpp"
14 #include "../world/BlockLookup.hpp"
15 #include "../world/World.hpp"
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtx/io.hpp>
27 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
32 , block_transform(1.0f)
34 , block_visible(false)
36 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
37 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
38 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
39 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
41 OutlineModel::Buffer buf;
42 buf.vertices = std::vector<glm::vec3>({
43 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
44 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
46 buf.indices = std::vector<OutlineModel::Index>({
49 buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
50 crosshair.Update(buf);
53 glm::vec3(50.0f, 85.0f, 0.0f),
57 block_label.Foreground(glm::vec4(1.0f));
58 block_label.Background(glm::vec4(0.5f));
62 void HUD::DisplayNone() {
63 block_visible = false;
66 void HUD::Display(const Block &b) {
67 const BlockType &type = types.Get(b.type);
70 type.FillEntityModel(block_buf, b.Transform());
71 block.Update(block_buf);
73 block_label.Set(font, type.label);
75 block_visible = type.visible;
79 void HUD::Render(Viewport &viewport) noexcept {
80 viewport.ClearDepth();
82 PlainColor &outline_prog = viewport.HUDOutlineProgram();
83 viewport.EnableInvertBlending();
84 viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
85 outline_prog.SetM(viewport.Cursor());
89 DirectionalLighting &world_prog = viewport.HUDProgram();
90 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
91 // disable distance fog
92 world_prog.SetFogDensity(0.0f);
94 viewport.DisableBlending();
95 world_prog.SetM(block_transform);
97 block_label.Render(viewport);
102 Interface::Interface(
103 const Config &config,
106 const Player &player)
110 , ctrl(*player.entity)
111 , hud(world.BlockTypes(), env.assets.small_ui_font)
112 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
116 , outline_transform(1.0f)
122 , last_entity(nullptr)
123 , messages(env.assets.small_ui_font)
130 , place_sound(env.loader.LoadSound("thump"))
131 , remove_sound(env.loader.LoadSound("plop"))
135 counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
136 counter_text.Foreground(glm::vec4(1.0f));
137 counter_text.Background(glm::vec4(0.5f));
138 position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
139 position_text.Foreground(glm::vec4(1.0f));
140 position_text.Background(glm::vec4(0.5f));
141 orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
142 orientation_text.Foreground(glm::vec4(1.0f));
143 orientation_text.Background(glm::vec4(0.5f));
144 block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
145 block_text.Foreground(glm::vec4(1.0f));
146 block_text.Background(glm::vec4(0.5f));
147 block_text.Set(env.assets.small_ui_font, "Block: none");
148 entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
149 entity_text.Foreground(glm::vec4(1.0f));
150 entity_text.Background(glm::vec4(0.5f));
151 entity_text.Set(env.assets.small_ui_font, "Entity: none");
152 messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
153 messages.Foreground(glm::vec4(1.0f));
154 messages.Background(glm::vec4(0.5f));
159 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
160 if (config.keyboard_disabled) return;
162 switch (env.keymap.Lookup(event)) {
163 case Keymap::MOVE_FORWARD:
166 case Keymap::MOVE_BACKWARD:
169 case Keymap::MOVE_LEFT:
172 case Keymap::MOVE_RIGHT:
175 case Keymap::MOVE_UP:
178 case Keymap::MOVE_DOWN:
182 case Keymap::BLOCK_FACE:
185 case Keymap::BLOCK_TURN:
188 case Keymap::BLOCK_NEXT:
191 case Keymap::BLOCK_PREV:
195 case Keymap::BLOCK_PLACE:
198 case Keymap::BLOCK_PICK:
201 case Keymap::BLOCK_REMOVE:
205 case Keymap::TOGGLE_COLLISION:
209 case Keymap::TOGGLE_VISUAL:
212 case Keymap::TOGGLE_DEBUG:
215 case Keymap::TOGGLE_AUDIO:
228 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
229 if (config.keyboard_disabled) return;
231 switch (env.keymap.Lookup(event)) {
232 case Keymap::MOVE_FORWARD:
235 case Keymap::MOVE_BACKWARD:
238 case Keymap::MOVE_LEFT:
241 case Keymap::MOVE_RIGHT:
244 case Keymap::MOVE_UP:
247 case Keymap::MOVE_DOWN:
256 void Interface::FaceBlock() {
257 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
258 hud.Display(selection);
261 void Interface::TurnBlock() {
262 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
263 hud.Display(selection);
266 void Interface::ToggleCollision() {
267 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
268 if (ctrl.Controlled().WorldCollidable()) {
269 PostMessage("collision on");
271 PostMessage("collision off");
275 void Interface::ToggleAudio() {
276 config.audio_disabled = !config.audio_disabled;
277 if (config.audio_disabled) {
278 PostMessage("audio off");
280 PostMessage("audio on");
284 void Interface::ToggleVisual() {
285 config.visual_disabled = !config.visual_disabled;
286 if (config.visual_disabled) {
287 PostMessage("visual off");
289 PostMessage("visual on");
293 void Interface::ToggleDebug() {
304 void Interface::UpdateCounter() {
306 s << std::setprecision(3) <<
307 "avg: " << env.counter.Average().running << "ms, "
308 "peak: " << env.counter.Peak().running << "ms";
309 std::string text = s.str();
310 counter_text.Set(env.assets.small_ui_font, text);
313 void Interface::UpdatePosition() {
315 s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
316 position_text.Set(env.assets.small_ui_font, s.str());
319 void Interface::UpdateOrientation() {
321 s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
322 << ", yaw: " << rad2deg(ctrl.Yaw());
323 orientation_text.Set(env.assets.small_ui_font, s.str());
326 void Interface::UpdateBlockInfo() {
328 const Block &block = aim_world.GetBlock();
329 if (last_block != block) {
332 << aim_world.GetType().label
333 << ", face: " << block.GetFace()
334 << ", turn: " << block.GetTurn();
335 block_text.Set(env.assets.small_ui_font, s.str());
339 if (last_block != Block()) {
342 block_text.Set(env.assets.small_ui_font, s.str());
343 last_block = Block();
348 void Interface::UpdateEntityInfo() {
350 if (last_entity != aim_entity.entity) {
352 s << "Entity: " << aim_entity.entity->Name();
353 entity_text.Set(env.assets.small_ui_font, s.str());
354 last_entity = aim_entity.entity;
360 void Interface::Handle(const SDL_MouseMotionEvent &event) {
361 if (config.mouse_disabled) return;
362 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
363 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
366 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
367 if (config.mouse_disabled) return;
369 if (event.button == SDL_BUTTON_LEFT) {
371 remove_timer.Start();
372 } else if (event.button == SDL_BUTTON_MIDDLE) {
374 } else if (event.button == SDL_BUTTON_RIGHT) {
380 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
381 if (config.mouse_disabled) return;
383 if (event.button == SDL_BUTTON_LEFT) {
385 } else if (event.button == SDL_BUTTON_RIGHT) {
390 void Interface::PickBlock() {
391 if (!aim_world) return;
392 selection = aim_world.GetBlock();
393 hud.Display(selection);
396 void Interface::PlaceBlock() {
397 if (!aim_world) return;
399 glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
400 BlockLookup next_block(&aim_world.GetChunk(), next_pos);
403 next_block.SetBlock(selection);
405 if (config.audio_disabled) return;
406 const Entity &player = ctrl.Controlled();
409 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
413 void Interface::RemoveBlock() noexcept {
414 if (!aim_world) return;
415 aim_world.SetBlock(remove);
417 if (config.audio_disabled) return;
418 const Entity &player = ctrl.Controlled();
421 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
426 void Interface::Handle(const SDL_MouseWheelEvent &event) {
427 if (config.mouse_disabled) return;
431 } else if (event.y > 0) {
436 void Interface::SelectNext() {
438 if (size_t(selection.type) >= world.BlockTypes().Size()) {
441 hud.Display(selection);
444 void Interface::SelectPrevious() {
446 if (selection.type <= 0) {
447 selection.type = world.BlockTypes().Size() - 1;
449 hud.Display(selection);
453 void Interface::PostMessage(const char *msg) {
454 messages.PushLine(msg);
457 std::cout << msg << std::endl;
461 void Interface::Update(int dt) {
462 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
465 msg_timer.Update(dt);
466 place_timer.Update(dt);
467 remove_timer.Update(dt);
472 if (msg_timer.HitOnce()) {
476 if (remove_timer.Hit()) {
481 if (place_timer.Hit()) {
487 if (env.counter.Changed()) {
497 OutlineModel::Buffer outl_buf;
501 void Interface::CheckAim() {
502 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled().ChunkCoords(), aim_world)) {
503 aim_world = WorldCollision();
505 if (!world.Intersection(aim, glm::mat4(1.0f), ctrl.Controlled(), aim_entity)) {
506 aim_entity = EntityCollision();
508 if (aim_world && aim_entity) {
509 // got both, pick the closest one
510 if (aim_world.depth < aim_entity.depth) {
512 aim_entity = EntityCollision();
514 aim_world = WorldCollision();
516 } else if (aim_world) {
525 void Interface::UpdateOutline() {
527 aim_world.GetType().FillOutlineModel(outl_buf);
528 outline.Update(outl_buf);
529 outline_transform = aim_world.GetChunk().Transform(player.entity->ChunkCoords());
530 outline_transform *= aim_world.BlockTransform();
531 outline_transform *= glm::scale(glm::vec3(1.005f));
535 void Interface::Render(Viewport &viewport) noexcept {
536 if (config.visual_disabled) return;
539 PlainColor &outline_prog = viewport.WorldOutlineProgram();
540 outline_prog.SetM(outline_transform);
545 counter_text.Render(viewport);
546 position_text.Render(viewport);
547 orientation_text.Render(viewport);
549 block_text.Render(viewport);
550 } else if (aim_entity) {
551 entity_text.Render(viewport);
555 if (msg_timer.Running()) {
556 messages.Render(viewport);
559 hud.Render(viewport);
568 void Keymap::Map(SDL_Scancode scancode, Action action) {
569 if (scancode > MAX_SCANCODE) {
570 throw std::runtime_error("refusing to map scancode: too damn high");
572 codemap[scancode] = action;
575 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
576 if (scancode < NUM_SCANCODES) {
577 return codemap[scancode];
584 void Keymap::LoadDefault() {
585 Map(SDL_SCANCODE_UP, MOVE_FORWARD);
586 Map(SDL_SCANCODE_W, MOVE_FORWARD);
587 Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
588 Map(SDL_SCANCODE_S, MOVE_BACKWARD);
589 Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
590 Map(SDL_SCANCODE_A, MOVE_LEFT);
591 Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
592 Map(SDL_SCANCODE_D, MOVE_RIGHT);
593 Map(SDL_SCANCODE_SPACE, MOVE_UP);
594 Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
595 Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
596 Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
597 Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
599 Map(SDL_SCANCODE_Q, BLOCK_FACE);
600 Map(SDL_SCANCODE_E, BLOCK_TURN);
601 Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
602 Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
603 Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
605 Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
606 Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
607 Map(SDL_SCANCODE_MENU, BLOCK_PICK);
608 Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
609 Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
611 Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
612 Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
613 Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
614 Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
616 Map(SDL_SCANCODE_ESCAPE, EXIT);
620 void Keymap::Load(std::istream &is) {
621 TokenStreamReader in(is);
622 std::string key_name;
623 std::string action_name;
626 while (in.HasMore()) {
627 if (in.Peek().type == Token::STRING) {
628 in.ReadString(key_name);
629 key = SDL_GetScancodeFromName(key_name.c_str());
631 key = SDL_Scancode(in.GetInt());
633 in.Skip(Token::EQUALS);
634 in.ReadIdentifier(action_name);
635 action = StringToAction(action_name);
636 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
637 in.Skip(Token::SEMICOLON);
643 void Keymap::Save(std::ostream &out) {
644 for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
645 if (codemap[i] == NONE) continue;
647 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
663 out << " = " << ActionToString(codemap[i]) << std::endl;;
668 const char *Keymap::ActionToString(Action action) {
674 return "move_forward";
676 return "move_backward";
694 return "block_place";
698 return "block_remove";
699 case TOGGLE_COLLISION:
700 return "toggle_collision";
702 return "toggle_audio";
704 return "toggle_visual";
706 return "toggle_debug";
712 Keymap::Action Keymap::StringToAction(const std::string &str) {
713 if (str == "move_forward") {
715 } else if (str == "move_backward") {
716 return MOVE_BACKWARD;
717 } else if (str == "move_left") {
719 } else if (str == "move_right") {
721 } else if (str == "move_up") {
723 } else if (str == "move_down") {
725 } else if (str == "block_face") {
727 } else if (str == "block_turn") {
729 } else if (str == "block_next") {
731 } else if (str == "block_prev") {
733 } else if (str == "block_place") {
735 } else if (str == "block_pick") {
737 } else if (str == "block_remove") {
739 } else if (str == "toggle_collision") {
740 return TOGGLE_COLLISION;
741 } else if (str == "toggle_audio") {
743 } else if (str == "toggle_visual") {
744 return TOGGLE_VISUAL;
745 } else if (str == "toggle_debug") {
747 } else if (str == "exit") {