From 451fc714ed87826c2e1072ff50c726b53e1faa02 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 22 May 2021 20:23:12 +0200 Subject: [PATCH] [BlockCursor] Fixed right-click holding. --- source/client/hud/BlockCursor.cpp | 13 +++++++------ source/client/hud/BlockCursor.hpp | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/source/client/hud/BlockCursor.cpp b/source/client/hud/BlockCursor.cpp index d473fab0..7ec1d3a4 100644 --- a/source/client/hud/BlockCursor.cpp +++ b/source/client/hud/BlockCursor.cpp @@ -55,15 +55,14 @@ void BlockCursor::onEvent(const SDL_Event &event, const Hotbar &hotbar) { m_animationStart = gk::GameClock::getInstance().getTicks(); m_currentTool = &m_player.inventory().getStack(hotbar.cursorPos(), 0); } - else if (event.button.button == SDL_BUTTON_RIGHT) { + else if (event.button.button == SDL_BUTTON_RIGHT) activateBlock(hotbar); - } } else if (event.type == SDL_MOUSEBUTTONUP) { if (event.button.button == SDL_BUTTON_LEFT) reset(); else if (event.button.button == SDL_BUTTON_RIGHT) - m_activationRepeat = 0; + m_lastActivationTime.reset(); } } @@ -131,8 +130,10 @@ void BlockCursor::update(const Hotbar &hotbar) { else m_currentBlock = nullptr; - if (m_activationRepeat - && gk::GameClock::getInstance().getTicks() > m_activationRepeat + m_activationRepeatDelay) + // Repeat block activation when right click is held + u64 now = gk::GameClock::getInstance().getTicks(); + u64 last = m_lastActivationTime.value_or(now); + if (now - last > m_activationRepeatDelay) activateBlock(hotbar); } @@ -140,7 +141,7 @@ void BlockCursor::activateBlock(const Hotbar &hotbar) { if (m_animationStart != 0) m_animationStart = 0; - m_activationRepeat = gk::GameClock::getInstance().getTicks(); + m_lastActivationTime = gk::GameClock::getInstance().getTicks(); u32 blockId = m_world.getBlock(m_selectedBlock.x, m_selectedBlock.y, m_selectedBlock.z); const Block &block = Registry::getInstance().getBlock(blockId); diff --git a/source/client/hud/BlockCursor.hpp b/source/client/hud/BlockCursor.hpp index f9efe293..9766ecca 100644 --- a/source/client/hud/BlockCursor.hpp +++ b/source/client/hud/BlockCursor.hpp @@ -45,7 +45,7 @@ class BlockCursor : public gk::Drawable { void update(const Hotbar &hotbar); - void reset() { m_animationStart = 0; m_activationRepeat = 0; } + void reset() { m_animationStart = 0; m_lastActivationTime.reset(); } const BlockState *currentBlock() const { return m_currentBlock; } @@ -76,7 +76,7 @@ class BlockCursor : public gk::Drawable { gk::Texture *m_blockDestroyTexture = nullptr; const u16 m_activationRepeatDelay = 250; - u16 m_activationRepeat = 0; + std::optional m_lastActivationTime = 0; }; #endif // BLOCKCURSOR_HPP_