]> git.localhorst.tv Git - blank.git/blobdiff - src/standalone/MasterState.cpp
better handling of focus and input
[blank.git] / src / standalone / MasterState.cpp
index 1035bb23688da035893c3a10fdd8e687a9190b79..9cc154387e3f6becb55bf756b5df29606514f2fd 100644 (file)
@@ -37,7 +37,8 @@ MasterState::MasterState(
 , spawner(world, res.models, env.rng)
 , sky(env.loader.LoadCubeMap("skybox"))
 , preload(env, chunk_loader, chunk_renderer)
-, unload(env, world.Chunks(), save) {
+, unload(env, world.Chunks(), save)
+, chat(env, *this, *this) {
        res.Load(env.loader, "default");
        if (res.models.size() < 2) {
                throw std::runtime_error("need at least two models to run");
@@ -51,6 +52,8 @@ MasterState::MasterState(
        chunk_renderer.FogDensity(wc.fog_density);
        if (save.Exists(player)) {
                save.Read(player);
+               glm::vec3 orient(glm::eulerAngles(player.GetEntity().Orientation()));
+               input.TurnHead(orient.x, orient.y);
        } else {
                spawn_player = true;
        }
@@ -64,25 +67,43 @@ MasterState::~MasterState() {
 void MasterState::OnResume() {
        if (spawn_index.MissingChunks() > 0) {
                env.state.Push(&preload);
-       }
-       if (config.input.mouse) {
-               env.window.GrabMouse();
+               return;
        }
        if (spawn_player) {
                // TODO: spawn
                spawn_player = false;
        }
+       hud.KeepMessages(false);
+       OnFocus();
 }
 
 void MasterState::OnPause() {
+       OnBlur();
+}
+
+void MasterState::OnFocus() {
+       if (config.input.mouse) {
+               env.window.GrabMouse();
+       }
+       interface.Unlock();
+}
+
+void MasterState::OnBlur() {
        env.window.ReleaseMouse();
+       interface.Lock();
 }
 
 
 void MasterState::Handle(const SDL_Event &event) {
        switch (event.type) {
                case SDL_KEYDOWN:
-                       interface.HandlePress(event.key);
+                       // TODO: move to interface
+                       if (event.key.keysym.sym == SDLK_RETURN) {
+                               env.state.Push(&chat);
+                               hud.KeepMessages(true);
+                       } else {
+                               interface.HandlePress(event.key);
+                       }
                        break;
                case SDL_KEYUP:
                        interface.HandleRelease(event.key);
@@ -185,5 +206,11 @@ void MasterState::Exit() {
        env.state.Switch(&unload);
 }
 
+void MasterState::OnLineSubmit(const std::string &line) {
+       if (!line.empty()) {
+               hud.PostMessage(line);
+       }
+}
+
 }
 }