]> git.localhorst.tv Git - blank.git/blobdiff - src/client/client.cpp
better handling of focus and input
[blank.git] / src / client / client.cpp
index 773c0be595e06fd95f06aa410bf062c7d0d225f6..f0a3f35c7b984825d2dcffbc4c529a8c0d9815cb 100644 (file)
@@ -51,7 +51,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 , sounds()
 , save(master.GetEnv().config.GetWorldPath(master.GetWorldConf().name, master.GetConfig().net.host))
 , world(res.block_types, master.GetWorldConf())
-, player(*world.AddPlayer(master.GetConfig().player.name))
+, player(*world.AddPlayer(master.GetConfig().player.name, player_id))
 , hud(master.GetEnv(), master.GetConfig(), player)
 , manip(master.GetEnv().audio, sounds, player.GetEntity())
 , input(world, player, master.GetClient())
@@ -80,8 +80,24 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
        }
 }
 
-void InteractiveState::OnEnter() {
-       master.GetEnv().window.GrabMouse();
+void InteractiveState::OnResume() {
+       OnFocus();
+}
+
+void InteractiveState::OnPause() {
+       OnBlur();
+}
+
+void InteractiveState::OnFocus() {
+       if (master.GetConfig().input.mouse) {
+               master.GetEnv().window.GrabMouse();
+       }
+       interface.Unlock();
+}
+
+void InteractiveState::OnBlur() {
+       master.GetEnv().window.ReleaseMouse();
+       interface.Lock();
 }
 
 void InteractiveState::Handle(const SDL_Event &event) {
@@ -201,6 +217,7 @@ void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
        glm::ivec3 base;
        pack.ReadEntityCount(count);
        pack.ReadChunkBase(base);
+       EntityState state;
 
        for (uint32_t i = 0; i < count; ++i) {
                uint32_t entity_id = 0;
@@ -215,7 +232,8 @@ void InteractiveState::Handle(const Packet::EntityUpdate &pack) {
                }
                if (world_iter->ID() == entity_id) {
                        if (UpdateEntity(entity_id, pack.Seq())) {
-                               pack.ReadEntityState(world_iter->GetState(), base, i);
+                               pack.ReadEntityState(state, base, i);
+                               world_iter->SetState(state);
                        }
                }
        }
@@ -321,7 +339,9 @@ void InteractiveState::Exit() {
 }
 
 void InteractiveState::OnLineSubmit(const string &line) {
-       master.GetClient().SendMessage(1, 0, line);
+       if (!line.empty()) {
+               master.GetClient().SendMessage(1, 0, line);
+       }
 }
 
 
@@ -399,9 +419,9 @@ void MasterState::On(const Packet::Join &pack) {
        pack.ReadPlayerID(player_id);
        state.reset(new InteractiveState(*this, player_id));
 
-       pack.ReadPlayerState(state->GetPlayer().GetEntity().GetState());
-       glm::vec3 orient(glm::eulerAngles(state->GetPlayer().GetEntity().Orientation()));
-       state->GetPlayerController().TurnHead(orient.x, orient.y);
+       EntityState player_state;
+       pack.ReadPlayerState(player_state);
+       state->GetPlayer().GetEntity().SetState(player_state);
 
        env.state.PopAfter(this);
        env.state.Push(state.get());