Hotkey repeat handling update

This commit is contained in:
Dan Paulat 2024-04-11 23:57:12 -05:00
parent 399894f2db
commit dcaba52db7
3 changed files with 18 additions and 15 deletions

View file

@ -71,7 +71,7 @@ void HotkeyManager::HandleKeyPress(QKeyEvent* ev)
if (hotkey.second.count() == 1 && if (hotkey.second.count() == 1 &&
hotkey.second[0] == ev->keyCombination()) hotkey.second[0] == ev->keyCombination())
{ {
Q_EMIT HotkeyPressed(hotkey.first); Q_EMIT HotkeyPressed(hotkey.first, ev->isAutoRepeat());
} }
} }
} }

View file

@ -30,7 +30,7 @@ public:
static std::shared_ptr<HotkeyManager> Instance(); static std::shared_ptr<HotkeyManager> Instance();
signals: signals:
void HotkeyPressed(scwx::qt::types::Hotkey hotkey); void HotkeyPressed(scwx::qt::types::Hotkey hotkey, bool isAutoRepeat);
void HotkeyReleased(scwx::qt::types::Hotkey hotkey); void HotkeyReleased(scwx::qt::types::Hotkey hotkey);
private: private:

View file

@ -149,7 +149,7 @@ public:
const std::string& before); const std::string& before);
void ConnectMapSignals(); void ConnectMapSignals();
void ConnectSignals(); void ConnectSignals();
void HandleHotkey(types::Hotkey hotkey); void HandleHotkey(types::Hotkey hotkey, bool isAutoRepeat);
void ImGuiCheckFonts(); void ImGuiCheckFonts();
void InitializeNewRadarProductView(const std::string& colorPalette); void InitializeNewRadarProductView(const std::string& colorPalette);
void RadarProductManagerConnect(); void RadarProductManagerConnect();
@ -345,21 +345,20 @@ void MapWidgetImpl::ConnectSignals()
&MapWidgetImpl::HandleHotkey); &MapWidgetImpl::HandleHotkey);
} }
void MapWidgetImpl::HandleHotkey(types::Hotkey hotkey) void MapWidgetImpl::HandleHotkey(types::Hotkey hotkey, bool isAutoRepeat)
{ {
static constexpr float kMapPanFactor = 0.2f; static constexpr float kMapPanFactor = 0.2f;
static constexpr float kMapRotateFactor = 0.2f; static constexpr float kMapRotateFactor = 0.2f;
static constexpr long long kMapScaleFactor = 1000; static constexpr double kMapScaleFactor = 1000.0;
using namespace std::chrono_literals; using namespace std::chrono_literals;
std::chrono::system_clock::time_point hotkeyTime = std::chrono::system_clock::time_point hotkeyTime =
std::chrono::system_clock::now(); std::chrono::system_clock::now();
std::chrono::milliseconds hotkeyElapsed = std::chrono::milliseconds hotkeyElapsed =
prevHotkey_ != types::Hotkey::Unknown ? isAutoRepeat ? std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::duration_cast<std::chrono::milliseconds>( hotkeyTime - prevHotkeyTime_) :
hotkeyTime - prevHotkeyTime_) : 100ms;
100ms;
switch (hotkey) switch (hotkey)
{ {
@ -431,16 +430,20 @@ void MapWidgetImpl::HandleHotkey(types::Hotkey hotkey)
case types::Hotkey::MapZoomIn: case types::Hotkey::MapZoomIn:
{ {
double scale = std::pow(2.0, hotkeyElapsed.count() / kMapScaleFactor); auto widgetSize = widget_->size();
map_->scaleBy(scale); QPointF center = {widgetSize.width() * 0.5f, widgetSize.height() * 0.5f};
double scale = std::pow(2.0, hotkeyElapsed.count() / kMapScaleFactor);
map_->scaleBy(scale, center);
break; break;
} }
case types::Hotkey::MapZoomOut: case types::Hotkey::MapZoomOut:
{ {
double scale = auto widgetSize = widget_->size();
QPointF center = {widgetSize.width() * 0.5f, widgetSize.height() * 0.5f};
double scale =
1.0 / std::pow(2.0, hotkeyElapsed.count() / kMapScaleFactor); 1.0 / std::pow(2.0, hotkeyElapsed.count() / kMapScaleFactor);
map_->scaleBy(scale); map_->scaleBy(scale, center);
break; break;
} }