]> git.localhorst.tv Git - blank.git/blob - src/ui/ui.cpp
display focused entity in debug overlay
[blank.git] / src / ui / ui.cpp
1 #include "HUD.hpp"
2 #include "Interface.hpp"
3 #include "Keymap.hpp"
4
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"
16
17 #include <algorithm>
18 #include <cmath>
19 #include <iostream>
20 #include <sstream>
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtx/io.hpp>
23
24
25 namespace blank {
26
27 HUD::HUD(const BlockTypeRegistry &types, const Font &font)
28 : types(types)
29 , font(font)
30 , block()
31 , block_buf()
32 , block_transform(1.0f)
33 , block_label()
34 , block_visible(false)
35 , crosshair() {
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));
40
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 },
45         });
46         buf.indices = std::vector<OutlineModel::Index>({
47                 0, 1, 2, 3
48         });
49         buf.colors.resize(4, { 10.0f, 10.0f, 10.0f });
50         crosshair.Update(buf);
51
52         block_label.Position(
53                 glm::vec3(50.0f, 85.0f, 0.0f),
54                 Gravity::NORTH_WEST,
55                 Gravity::NORTH
56         );
57         block_label.Foreground(glm::vec4(1.0f));
58         block_label.Background(glm::vec4(0.5f));
59 }
60
61
62 void HUD::Display(const Block &b) {
63         const BlockType &type = types.Get(b.type);
64
65         block_buf.Clear();
66         type.FillEntityModel(block_buf, b.Transform());
67         block.Update(block_buf);
68
69         block_label.Set(font, type.label);
70
71         block_visible = type.visible;
72 }
73
74
75 void HUD::Render(Viewport &viewport) noexcept {
76         viewport.ClearDepth();
77
78         PlainColor &outline_prog = viewport.HUDOutlineProgram();
79         viewport.EnableInvertBlending();
80         viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
81         outline_prog.SetM(viewport.Cursor());
82         crosshair.Draw();
83
84         if (block_visible) {
85                 DirectionalLighting &world_prog = viewport.HUDProgram();
86                 world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
87                 // disable distance fog
88                 world_prog.SetFogDensity(0.0f);
89
90                 viewport.DisableBlending();
91                 world_prog.SetM(block_transform);
92                 block.Draw();
93                 block_label.Render(viewport);
94         }
95 }
96
97
98 Interface::Interface(
99         const Config &config,
100         Environment &env,
101         World &world)
102 : env(env)
103 , world(world)
104 , ctrl(world.Player())
105 , hud(world.BlockTypes(), env.assets.small_ui_font)
106 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
107 , aim_world()
108 , aim_entity()
109 , outline()
110 , outline_transform(1.0f)
111 , counter_text()
112 , position_text()
113 , orientation_text()
114 , block_text()
115 , last_block()
116 , last_entity(nullptr)
117 , messages(env.assets.small_ui_font)
118 , msg_timer(5000)
119 , config(config)
120 , place_timer(256)
121 , remove_timer(256)
122 , remove(0)
123 , selection(1)
124 , place_sound(env.assets.LoadSound("thump"))
125 , remove_sound(env.assets.LoadSound("plop"))
126 , fwd(0)
127 , rev(0)
128 , debug(false) {
129         counter_text.Position(glm::vec3(-25.0f, 25.0f, 0.0f), Gravity::NORTH_EAST);
130         counter_text.Foreground(glm::vec4(1.0f));
131         counter_text.Background(glm::vec4(0.5f));
132         position_text.Position(glm::vec3(-25.0f, 25.0f + env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
133         position_text.Foreground(glm::vec4(1.0f));
134         position_text.Background(glm::vec4(0.5f));
135         orientation_text.Position(glm::vec3(-25.0f, 25.0f + 2 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
136         orientation_text.Foreground(glm::vec4(1.0f));
137         orientation_text.Background(glm::vec4(0.5f));
138         block_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
139         block_text.Foreground(glm::vec4(1.0f));
140         block_text.Background(glm::vec4(0.5f));
141         block_text.Set(env.assets.small_ui_font, "Block: none");
142         entity_text.Position(glm::vec3(-25.0f, 25.0f + 4 * env.assets.small_ui_font.LineSkip(), 0.0f), Gravity::NORTH_EAST);
143         entity_text.Foreground(glm::vec4(1.0f));
144         entity_text.Background(glm::vec4(0.5f));
145         entity_text.Set(env.assets.small_ui_font, "Entity: none");
146         messages.Position(glm::vec3(25.0f, -25.0f, 0.0f), Gravity::SOUTH_WEST);
147         messages.Foreground(glm::vec4(1.0f));
148         messages.Background(glm::vec4(0.5f));
149         hud.Display(selection);
150 }
151
152
153 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
154         if (config.keyboard_disabled) return;
155
156         switch (env.keymap.Lookup(event)) {
157                 case Keymap::MOVE_FORWARD:
158                         rev.z = 1;
159                         break;
160                 case Keymap::MOVE_BACKWARD:
161                         fwd.z = 1;
162                         break;
163                 case Keymap::MOVE_LEFT:
164                         rev.x = 1;
165                         break;
166                 case Keymap::MOVE_RIGHT:
167                         fwd.x = 1;
168                         break;
169                 case Keymap::MOVE_UP:
170                         fwd.y = 1;
171                         break;
172                 case Keymap::MOVE_DOWN:
173                         rev.y = 1;
174                         break;
175
176                 case Keymap::BLOCK_FACE:
177                         FaceBlock();
178                         break;
179                 case Keymap::BLOCK_TURN:
180                         TurnBlock();
181                         break;
182                 case Keymap::BLOCK_NEXT:
183                         SelectNext();
184                         break;
185                 case Keymap::BLOCK_PREV:
186                         SelectPrevious();
187                         break;
188
189                 case Keymap::BLOCK_PLACE:
190                         PlaceBlock();
191                         break;
192                 case Keymap::BLOCK_PICK:
193                         PickBlock();
194                         break;
195                 case Keymap::BLOCK_REMOVE:
196                         RemoveBlock();
197                         break;
198
199                 case Keymap::TOGGLE_COLLISION:
200                         ToggleCollision();
201                         break;
202
203                 case Keymap::PRINT_BLOCK:
204                         PrintBlockInfo();
205                         break;
206                 case Keymap::PRINT_CHUNK:
207                         PrintChunkInfo();
208                         break;
209                 case Keymap::PRINT_LIGHT:
210                         PrintLightInfo();
211                         break;
212                 case Keymap::PRINT_SELECTION:
213                         PrintSelectionInfo();
214                         break;
215
216                 case Keymap::TOGGLE_VISUAL:
217                         ToggleVisual();
218                         break;
219                 case Keymap::TOGGLE_DEBUG:
220                         ToggleDebug();
221                         break;
222                 case Keymap::TOGGLE_AUDIO:
223                         ToggleAudio();
224                         break;
225
226                 case Keymap::EXIT:
227                         env.state.Pop();
228                         break;
229
230                 default:
231                         break;
232         }
233 }
234
235 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
236         if (config.keyboard_disabled) return;
237
238         switch (env.keymap.Lookup(event)) {
239                 case Keymap::MOVE_FORWARD:
240                         rev.z = 0;
241                         break;
242                 case Keymap::MOVE_BACKWARD:
243                         fwd.z = 0;
244                         break;
245                 case Keymap::MOVE_LEFT:
246                         rev.x = 0;
247                         break;
248                 case Keymap::MOVE_RIGHT:
249                         fwd.x = 0;
250                         break;
251                 case Keymap::MOVE_UP:
252                         fwd.y = 0;
253                         break;
254                 case Keymap::MOVE_DOWN:
255                         rev.y = 0;
256                         break;
257
258                 default:
259                         break;
260         }
261 }
262
263 void Interface::FaceBlock() {
264         selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
265         hud.Display(selection);
266 }
267
268 void Interface::TurnBlock() {
269         selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
270         hud.Display(selection);
271 }
272
273 void Interface::ToggleCollision() {
274         ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
275         if (ctrl.Controlled().WorldCollidable()) {
276                 PostMessage("collision on");
277         } else {
278                 PostMessage("collision off");
279         }
280 }
281
282 void Interface::PrintBlockInfo() {
283         std::cout << std::endl;
284         if (!aim_world) {
285                 PostMessage("not looking at any block");
286                 Ray aim = ctrl.Aim();
287                 std::stringstream s;
288                 s << "aim ray: " << aim.orig << ", " << aim.dir;
289                 PostMessage(s.str());
290                 return;
291         }
292         std::stringstream s;
293         s << "looking at block " << aim_world.block
294                 << " " << aim_world.BlockCoords()
295                 << " of chunk " << aim_world.GetChunk().Position()
296         ;
297         PostMessage(s.str());
298         Print(aim_world.GetBlock());
299 }
300
301 void Interface::PrintChunkInfo() {
302         std::cout << std::endl;
303         if (!aim_world) {
304                 PostMessage("not looking at any block");
305                 return;
306         }
307         std::stringstream s;
308         s << "looking at chunk " << aim_world.GetChunk().Position();
309         PostMessage(s.str());
310
311         PostMessage("  neighbors:");
312         if (aim_world.GetChunk().HasNeighbor(Block::FACE_LEFT)) {
313                 s.str("");
314                 s << " left  " << aim_world.GetChunk().GetNeighbor(Block::FACE_LEFT).Position();
315                 PostMessage(s.str());
316         }
317         if (aim_world.GetChunk().HasNeighbor(Block::FACE_RIGHT)) {
318                 s.str("");
319                 s << " right " << aim_world.GetChunk().GetNeighbor(Block::FACE_RIGHT).Position();
320                 PostMessage(s.str());
321         }
322         if (aim_world.GetChunk().HasNeighbor(Block::FACE_UP)) {
323                 s.str("");
324                 s << " up    " << aim_world.GetChunk().GetNeighbor(Block::FACE_UP).Position();
325                 PostMessage(s.str());
326         }
327         if (aim_world.GetChunk().HasNeighbor(Block::FACE_DOWN)) {
328                 s.str("");
329                 s << " down  " << aim_world.GetChunk().GetNeighbor(Block::FACE_DOWN).Position();
330                 PostMessage(s.str());
331         }
332         if (aim_world.GetChunk().HasNeighbor(Block::FACE_FRONT)) {
333                 s.str("");
334                 s << " front " << aim_world.GetChunk().GetNeighbor(Block::FACE_FRONT).Position();
335                 PostMessage(s.str());
336         }
337         if (aim_world.GetChunk().HasNeighbor(Block::FACE_BACK)) {
338                 s.str("");
339                 s << " back  " << aim_world.GetChunk().GetNeighbor(Block::FACE_BACK).Position();
340                 PostMessage(s.str());
341         }
342         std::cout << std::endl;
343 }
344
345 void Interface::PrintLightInfo() {
346         std::stringstream s;
347         s
348                 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
349                 << " at position " << world.Player().Position()
350         ;
351         PostMessage(s.str());
352 }
353
354 void Interface::PrintSelectionInfo() {
355         std::cout << std::endl;
356         Print(selection);
357 }
358
359 void Interface::Print(const Block &block) {
360         std::stringstream s;
361         s << "type: " << block.type
362                 << ", face: " << block.GetFace()
363                 << ", turn: " << block.GetTurn()
364         ;
365         PostMessage(s.str());
366 }
367
368 void Interface::ToggleAudio() {
369         config.audio_disabled = !config.audio_disabled;
370         if (config.audio_disabled) {
371                 PostMessage("audio off");
372         } else {
373                 PostMessage("audio on");
374         }
375 }
376
377 void Interface::ToggleVisual() {
378         config.visual_disabled = !config.visual_disabled;
379         if (config.visual_disabled) {
380                 PostMessage("visual off");
381         } else {
382                 PostMessage("visual on");
383         }
384 }
385
386 void Interface::ToggleDebug() {
387         debug = !debug;
388         if (debug) {
389                 UpdateCounter();
390                 UpdatePosition();
391                 UpdateOrientation();
392                 UpdateBlockInfo();
393                 UpdateEntityInfo();
394         }
395 }
396
397 void Interface::UpdateCounter() {
398         std::stringstream s;
399         s << std::setprecision(3) <<
400                 "avg: " << env.counter.Average().running << "ms, "
401                 "peak: " << env.counter.Peak().running << "ms";
402         std::string text = s.str();
403         counter_text.Set(env.assets.small_ui_font, text);
404 }
405
406 void Interface::UpdatePosition() {
407         std::stringstream s;
408         s << std::setprecision(3) << "pos: " << ctrl.Controlled().AbsolutePosition();
409         position_text.Set(env.assets.small_ui_font, s.str());
410 }
411
412 void Interface::UpdateOrientation() {
413         std::stringstream s;
414         s << std::setprecision(3) << "pitch: " << rad2deg(ctrl.Pitch())
415                 << ", yaw: " << rad2deg(ctrl.Yaw());
416         orientation_text.Set(env.assets.small_ui_font, s.str());
417 }
418
419 void Interface::UpdateBlockInfo() {
420         if (aim_world) {
421                 const Block &block = aim_world.GetBlock();
422                 if (last_block != block) {
423                         std::stringstream s;
424                         s << "Block: "
425                                 << aim_world.GetType().label
426                                 << ", face: " << block.GetFace()
427                                 << ", turn: " << block.GetTurn();
428                         block_text.Set(env.assets.small_ui_font, s.str());
429                         last_block = block;
430                 }
431         } else {
432                 if (last_block != Block()) {
433                         std::stringstream s;
434                         s << "Block: none";
435                         block_text.Set(env.assets.small_ui_font, s.str());
436                         last_block = Block();
437                 }
438         }
439 }
440
441 void Interface::UpdateEntityInfo() {
442         if (aim_entity) {
443                 if (last_entity != aim_entity.entity) {
444                         std::stringstream s;
445                         s << "Entity: " << aim_entity.entity->Name();
446                         entity_text.Set(env.assets.small_ui_font, s.str());
447                         last_entity = aim_entity.entity;
448                 }
449         }
450 }
451
452
453 void Interface::Handle(const SDL_MouseMotionEvent &event) {
454         if (config.mouse_disabled) return;
455         ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
456         ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
457 }
458
459 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
460         if (config.mouse_disabled) return;
461
462         if (event.button == SDL_BUTTON_LEFT) {
463                 RemoveBlock();
464                 remove_timer.Start();
465         } else if (event.button == SDL_BUTTON_MIDDLE) {
466                 PickBlock();
467         } else if (event.button == SDL_BUTTON_RIGHT) {
468                 PlaceBlock();
469                 place_timer.Start();
470         }
471 }
472
473 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
474         if (config.mouse_disabled) return;
475
476         if (event.button == SDL_BUTTON_LEFT) {
477                 remove_timer.Stop();
478         } else if (event.button == SDL_BUTTON_RIGHT) {
479                 place_timer.Stop();
480         }
481 }
482
483 void Interface::PickBlock() {
484         if (!aim_world) return;
485         selection = aim_world.GetBlock();
486         hud.Display(selection);
487 }
488
489 void Interface::PlaceBlock() {
490         if (!aim_world) return;
491
492         glm::vec3 next_pos = aim_world.BlockCoords() + aim_world.normal;
493         BlockLookup next_block(&aim_world.GetChunk(), next_pos);
494         if (next_block) {
495         }
496         next_block.SetBlock(selection);
497
498         if (config.audio_disabled) return;
499         const Entity &player = ctrl.Controlled();
500         env.audio.Play(
501                 place_sound,
502                 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), next_pos)
503         );
504 }
505
506 void Interface::RemoveBlock() noexcept {
507         if (!aim_world) return;
508         aim_world.SetBlock(remove);
509
510         if (config.audio_disabled) return;
511         const Entity &player = ctrl.Controlled();
512         env.audio.Play(
513                 remove_sound,
514                 aim_world.GetChunk().ToSceneCoords(player.ChunkCoords(), aim_world.BlockCoords())
515         );
516 }
517
518
519 void Interface::Handle(const SDL_MouseWheelEvent &event) {
520         if (config.mouse_disabled) return;
521
522         if (event.y < 0) {
523                 SelectNext();
524         } else if (event.y > 0) {
525                 SelectPrevious();
526         }
527 }
528
529 void Interface::SelectNext() {
530         ++selection.type;
531         if (size_t(selection.type) >= world.BlockTypes().Size()) {
532                 selection.type = 1;
533         }
534         hud.Display(selection);
535 }
536
537 void Interface::SelectPrevious() {
538         --selection.type;
539         if (selection.type <= 0) {
540                 selection.type = world.BlockTypes().Size() - 1;
541         }
542         hud.Display(selection);
543 }
544
545
546 void Interface::PostMessage(const char *msg) {
547         messages.PushLine(msg);
548         msg_timer.Reset();
549         msg_timer.Start();
550         std::cout << msg << std::endl;
551 }
552
553
554 void Interface::Update(int dt) {
555         ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
556         ctrl.Update(dt);
557
558         msg_timer.Update(dt);
559         place_timer.Update(dt);
560         remove_timer.Update(dt);
561
562         aim = ctrl.Aim();
563         CheckAim();
564
565         if (msg_timer.HitOnce()) {
566                 msg_timer.Stop();
567         }
568
569         if (remove_timer.Hit()) {
570                 RemoveBlock();
571                 CheckAim();
572         }
573
574         if (place_timer.Hit()) {
575                 PlaceBlock();
576                 CheckAim();
577         }
578
579         if (debug) {
580                 if (env.counter.Changed()) {
581                         UpdateCounter();
582                 }
583                 UpdatePosition();
584                 UpdateOrientation();
585         }
586 }
587
588 namespace {
589
590 OutlineModel::Buffer outl_buf;
591
592 }
593
594 void Interface::CheckAim() {
595         if (!world.Intersection(aim, glm::mat4(1.0f), aim_world)) {
596                 aim_world = WorldCollision();
597         }
598         if (!world.Intersection(aim, glm::mat4(1.0f), aim_entity)) {
599                 aim_entity = EntityCollision();
600         }
601         if (aim_world && aim_entity) {
602                 // got both, pick the closest one
603                 if (aim_world.depth < aim_entity.depth) {
604                         UpdateOutline();
605                         aim_entity = EntityCollision();
606                 } else {
607                         aim_world = WorldCollision();
608                 }
609         } else if (aim_world) {
610                 UpdateOutline();
611         }
612         if (debug) {
613                 UpdateBlockInfo();
614                 UpdateEntityInfo();
615         }
616 }
617
618 void Interface::UpdateOutline() {
619         outl_buf.Clear();
620         aim_world.GetType().FillOutlineModel(outl_buf);
621         outline.Update(outl_buf);
622         outline_transform = aim_world.GetChunk().Transform(world.Player().ChunkCoords());
623         outline_transform *= aim_world.BlockTransform();
624         outline_transform *= glm::scale(glm::vec3(1.005f));
625 }
626
627
628 void Interface::Render(Viewport &viewport) noexcept {
629         if (config.visual_disabled) return;
630
631         if (aim_world) {
632                 PlainColor &outline_prog = viewport.WorldOutlineProgram();
633                 outline_prog.SetM(outline_transform);
634                 outline.Draw();
635         }
636
637         if (debug) {
638                 counter_text.Render(viewport);
639                 position_text.Render(viewport);
640                 orientation_text.Render(viewport);
641                 if (aim_world) {
642                         block_text.Render(viewport);
643                 } else if (aim_entity) {
644                         entity_text.Render(viewport);
645                 }
646         }
647
648         if (msg_timer.Running()) {
649                 messages.Render(viewport);
650         }
651
652         hud.Render(viewport);
653 }
654
655
656 Keymap::Keymap()
657 : codemap{ NONE } {
658
659 }
660
661 void Keymap::Map(SDL_Scancode scancode, Action action) {
662         if (scancode > MAX_SCANCODE) {
663                 throw std::runtime_error("refusing to map scancode: too damn high");
664         }
665         codemap[scancode] = action;
666 }
667
668 Keymap::Action Keymap::Lookup(SDL_Scancode scancode) {
669         if (scancode < NUM_SCANCODES) {
670                 return codemap[scancode];
671         } else {
672                 return NONE;
673         }
674 }
675
676
677 void Keymap::LoadDefault() {
678         Map(SDL_SCANCODE_UP, MOVE_FORWARD);
679         Map(SDL_SCANCODE_W, MOVE_FORWARD);
680         Map(SDL_SCANCODE_DOWN, MOVE_BACKWARD);
681         Map(SDL_SCANCODE_S, MOVE_BACKWARD);
682         Map(SDL_SCANCODE_LEFT, MOVE_LEFT);
683         Map(SDL_SCANCODE_A, MOVE_LEFT);
684         Map(SDL_SCANCODE_RIGHT, MOVE_RIGHT);
685         Map(SDL_SCANCODE_D, MOVE_RIGHT);
686         Map(SDL_SCANCODE_SPACE, MOVE_UP);
687         Map(SDL_SCANCODE_RSHIFT, MOVE_UP);
688         Map(SDL_SCANCODE_LSHIFT, MOVE_DOWN);
689         Map(SDL_SCANCODE_LCTRL, MOVE_DOWN);
690         Map(SDL_SCANCODE_RCTRL, MOVE_DOWN);
691
692         Map(SDL_SCANCODE_Q, BLOCK_FACE);
693         Map(SDL_SCANCODE_E, BLOCK_TURN);
694         Map(SDL_SCANCODE_TAB, BLOCK_NEXT);
695         Map(SDL_SCANCODE_RIGHTBRACKET, BLOCK_NEXT);
696         Map(SDL_SCANCODE_LEFTBRACKET, BLOCK_PREV);
697
698         Map(SDL_SCANCODE_INSERT, BLOCK_PLACE);
699         Map(SDL_SCANCODE_RETURN, BLOCK_PLACE);
700         Map(SDL_SCANCODE_MENU, BLOCK_PICK);
701         Map(SDL_SCANCODE_DELETE, BLOCK_REMOVE);
702         Map(SDL_SCANCODE_BACKSPACE, BLOCK_REMOVE);
703
704         Map(SDL_SCANCODE_N, TOGGLE_COLLISION);
705         Map(SDL_SCANCODE_F1, TOGGLE_VISUAL);
706         Map(SDL_SCANCODE_F3, TOGGLE_DEBUG);
707         Map(SDL_SCANCODE_F4, TOGGLE_AUDIO);
708
709         Map(SDL_SCANCODE_B, PRINT_BLOCK);
710         Map(SDL_SCANCODE_C, PRINT_CHUNK);
711         Map(SDL_SCANCODE_L, PRINT_LIGHT);
712         Map(SDL_SCANCODE_P, PRINT_SELECTION);
713
714         Map(SDL_SCANCODE_ESCAPE, EXIT);
715 }
716
717
718 void Keymap::Load(std::istream &is) {
719         TokenStreamReader in(is);
720         std::string key_name;
721         std::string action_name;
722         SDL_Scancode key;
723         Action action;
724         while (in.HasMore()) {
725                 if (in.Peek().type == Token::STRING) {
726                         in.ReadString(key_name);
727                         key = SDL_GetScancodeFromName(key_name.c_str());
728                 } else {
729                         key = SDL_Scancode(in.GetInt());
730                 }
731                 in.Skip(Token::EQUALS);
732                 in.ReadIdentifier(action_name);
733                 action = StringToAction(action_name);
734                 if (in.HasMore() && in.Peek().type == Token::SEMICOLON) {
735                         in.Skip(Token::SEMICOLON);
736                 }
737                 Map(key, action);
738         }
739 }
740
741 void Keymap::Save(std::ostream &out) {
742         for (unsigned int i = 0; i < NUM_SCANCODES; ++i) {
743                 if (codemap[i] == NONE) continue;
744
745                 const char *str = SDL_GetScancodeName(SDL_Scancode(i));
746                 if (str && *str) {
747                         out << '"';
748                         while (*str) {
749                                 if (*str == '"') {
750                                         out << "\\\"";
751                                 } else {
752                                         out << *str;
753                                 }
754                                 ++str;
755                         }
756                         out << '"';
757                 } else {
758                         out << i;
759                 }
760
761                 out << " = " << ActionToString(codemap[i]) << std::endl;;
762         }
763 }
764
765
766 const char *Keymap::ActionToString(Action action) {
767         switch (action) {
768                 default:
769                 case NONE:
770                         return "none";
771                 case MOVE_FORWARD:
772                         return "move_forward";
773                 case MOVE_BACKWARD:
774                         return "move_backward";
775                 case MOVE_LEFT:
776                         return "move_left";
777                 case MOVE_RIGHT:
778                         return "move_right";
779                 case MOVE_UP:
780                         return "move_up";
781                 case MOVE_DOWN:
782                         return "move_down";
783                 case BLOCK_FACE:
784                         return "block_face";
785                 case BLOCK_TURN:
786                         return "block_turn";
787                 case BLOCK_NEXT:
788                         return "block_next";
789                 case BLOCK_PREV:
790                         return "block_prev";
791                 case BLOCK_PLACE:
792                         return "block_place";
793                 case BLOCK_PICK:
794                         return "block_pick";
795                 case BLOCK_REMOVE:
796                         return "block_remove";
797                 case TOGGLE_COLLISION:
798                         return "toggle_collision";
799                 case TOGGLE_AUDIO:
800                         return "toggle_audio";
801                 case TOGGLE_VISUAL:
802                         return "toggle_visual";
803                 case TOGGLE_DEBUG:
804                         return "toggle_debug";
805                 case PRINT_BLOCK:
806                         return "print_block";
807                 case PRINT_CHUNK:
808                         return "print_chunk";
809                 case PRINT_LIGHT:
810                         return "print_light";
811                 case PRINT_SELECTION:
812                         return "print_selection";
813                 case EXIT:
814                         return "exit";
815         }
816 }
817
818 Keymap::Action Keymap::StringToAction(const std::string &str) {
819         if (str == "move_forward") {
820                 return MOVE_FORWARD;
821         } else if (str == "move_backward") {
822                 return MOVE_BACKWARD;
823         } else if (str == "move_left") {
824                 return MOVE_LEFT;
825         } else if (str == "move_right") {
826                 return MOVE_RIGHT;
827         } else if (str == "move_up") {
828                 return MOVE_UP;
829         } else if (str == "move_down") {
830                 return MOVE_DOWN;
831         } else if (str == "block_face") {
832                 return BLOCK_FACE;
833         } else if (str == "block_turn") {
834                 return BLOCK_TURN;
835         } else if (str == "block_next") {
836                 return BLOCK_NEXT;
837         } else if (str == "block_prev") {
838                 return BLOCK_PREV;
839         } else if (str == "block_place") {
840                 return BLOCK_PLACE;
841         } else if (str == "block_pick") {
842                 return BLOCK_PICK;
843         } else if (str == "block_remove") {
844                 return BLOCK_REMOVE;
845         } else if (str == "toggle_collision") {
846                 return TOGGLE_COLLISION;
847         } else if (str == "toggle_audio") {
848                 return TOGGLE_AUDIO;
849         } else if (str == "toggle_visual") {
850                 return TOGGLE_VISUAL;
851         } else if (str == "toggle_debug") {
852                 return TOGGLE_DEBUG;
853         } else if (str == "print_block") {
854                 return PRINT_BLOCK;
855         } else if (str == "print_chunk") {
856                 return PRINT_CHUNK;
857         } else if (str == "print_light") {
858                 return PRINT_LIGHT;
859         } else if (str == "print_selection") {
860                 return PRINT_SELECTION;
861         } else if (str == "exit") {
862                 return EXIT;
863         } else {
864                 return NONE;
865         }
866 }
867
868 }