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