From: Daniel Karbach Date: Thu, 22 Oct 2015 10:34:49 +0000 (+0200) Subject: better handling of focus and input X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=80a9a59d71a7b144c12f64cbef4644751bd54745;p=blank.git better handling of focus and input it's not perfect, but much better already --- diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index dc84199..55e03d0 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -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; diff --git a/src/client/client.cpp b/src/client/client.cpp index 8b53263..f0a3f35 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -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) { diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 49ee0fb..9cc1543 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -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(); } diff --git a/src/standalone/MasterState.hpp b/src/standalone/MasterState.hpp index 173c07c..942cc13 100644 --- a/src/standalone/MasterState.hpp +++ b/src/standalone/MasterState.hpp @@ -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; diff --git a/src/ui/Interface.hpp b/src/ui/Interface.hpp index aa040a1..92d1c94 100644 --- a/src/ui/Interface.hpp +++ b/src/ui/Interface.hpp @@ -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; + }; } diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 048aed1..5a39fa8 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -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);