]> git.localhorst.tv Git - blank.git/commitdiff
better handling of focus and input
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 22 Oct 2015 10:34:49 +0000 (12:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 22 Oct 2015 10:34:49 +0000 (12:34 +0200)
it's not perfect, but much better already

src/client/InteractiveState.hpp
src/client/client.cpp
src/standalone/MasterState.cpp
src/standalone/MasterState.hpp
src/ui/Interface.hpp
src/ui/ui.cpp

index dc84199c6be49025b35fe42a1feb1e6248d5bd8d..55e03d029f630ddf8481870476546b2677aecd89 100644 (file)
@@ -43,7 +43,11 @@ public:
        PlayerController &GetPlayerController() noexcept { return input; }
        ChunkReceiver &GetChunkReceiver() noexcept { return chunk_receiver; }
 
-       void OnEnter() override;
+       void OnResume() override;
+       void OnPause() override;
+
+       void OnFocus() override;
+       void OnBlur() override;
 
        void Handle(const SDL_Event &) override;
        void Update(int dt) override;
index 8b532633ab0cd85dc2926f2dbda72eb3ed7762e3..f0a3f35c7b984825d2dcffbc4c529a8c0d9815cb 100644 (file)
@@ -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) {
index 49ee0fbf9214967d550b526a7fd59469a9646c05..9cc154387e3f6becb55bf756b5df29606514f2fd 100644 (file)
@@ -67,19 +67,30 @@ 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();
 }
 
 
index 173c07cdd1e8208dd5939efb9b5842311a449650..942cc132e94a585bd6bcc066245f738daefe5cf3 100644 (file)
@@ -48,6 +48,9 @@ public:
        void OnResume() override;
        void OnPause() override;
 
+       void OnFocus() override;
+       void OnBlur() override;
+
        void Handle(const SDL_Event &) override;
        void Update(int dt) override;
        void Render(Viewport &) override;
index aa040a1ede174e63a20708bdb1208fe0118e4896..92d1c943705c8f9ecf83b74856aa0e193e75d756 100644 (file)
@@ -20,6 +20,9 @@ public:
 
        void SetInventorySlots(int num) { num_slots = num; }
 
+       void Lock();
+       void Unlock();
+
        void HandlePress(const SDL_KeyboardEvent &);
        void HandleRelease(const SDL_KeyboardEvent &);
        void Handle(const SDL_MouseMotionEvent &);
@@ -42,6 +45,8 @@ private:
        int slot;
        int num_slots;
 
+       bool locked;
+
 };
 
 }
index 048aed1ac81559e21e2eee740d90a195bf5df22b..5a39fa8109ebfc810ed1a333c0d089d407db7706 100644 (file)
@@ -457,10 +457,20 @@ Interface::Interface(
 , fwd(0)
 , rev(0)
 , slot(0)
-, num_slots(10) {
+, num_slots(10)
+, locked(false) {
 
 }
 
+void Interface::Lock() {
+       fwd = glm::ivec3(0);
+       rev = glm::ivec3(0);
+       locked = true;
+}
+
+void Interface::Unlock() {
+       locked = false;
+}
 
 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
        if (!config.input.keyboard) return;
@@ -592,7 +602,7 @@ void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
 }
 
 void Interface::Handle(const SDL_MouseMotionEvent &event) {
-       if (!config.input.mouse) return;
+       if (locked || !config.input.mouse) return;
        player_ctrl.TurnHead(
                event.yrel * config.input.pitch_sensitivity,
                event.xrel * config.input.yaw_sensitivity);