summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-26 14:00:53 +0100
committerPatric Stout <github@truebrain.nl>2021-02-27 00:36:14 +0100
commit853bfc35626ddb1c1c5794b6afdaa059e2d79f05 (patch)
treed8db603fb4906a9ae4ab19b83f9966d310c6e6c3
parent53c28a8ec9845a90f0c9e1ed83a87dbb7959d14d (diff)
downloadopenttd-853bfc35626ddb1c1c5794b6afdaa059e2d79f05.tar.xz
Codechange: replace _realtime_tick with std::chrono for mouse events
-rw-r--r--src/newgrf_debug.h1
-rw-r--r--src/newgrf_debug_gui.cpp2
-rw-r--r--src/window.cpp38
3 files changed, 21 insertions, 20 deletions
diff --git a/src/newgrf_debug.h b/src/newgrf_debug.h
index 1679e17ee..34530dc75 100644
--- a/src/newgrf_debug.h
+++ b/src/newgrf_debug.h
@@ -26,7 +26,6 @@ enum NewGrfDebugSpritePickerMode {
struct NewGrfDebugSpritePicker {
NewGrfDebugSpritePickerMode mode; ///< Current state
void *clicked_pixel; ///< Clicked pixel (pointer to blitter buffer)
- uint32 click_time; ///< Realtime tick when clicked to detect next frame
std::vector<SpriteID> sprites; ///< Sprites found
};
diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp
index f82f4d93b..bd78deee9 100644
--- a/src/newgrf_debug_gui.cpp
+++ b/src/newgrf_debug_gui.cpp
@@ -45,7 +45,7 @@
#include "safeguards.h"
/** The sprite picker. */
-NewGrfDebugSpritePicker _newgrf_debug_sprite_picker = { SPM_NONE, nullptr, 0, std::vector<SpriteID>() };
+NewGrfDebugSpritePicker _newgrf_debug_sprite_picker = { SPM_NONE, nullptr, std::vector<SpriteID>() };
/**
* Get the feature index related to the window number.
diff --git a/src/window.cpp b/src/window.cpp
index 5e49aacb0..e164d0d4a 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -2825,11 +2825,12 @@ enum MouseClick {
MC_HOVER,
MAX_OFFSET_DOUBLE_CLICK = 5, ///< How much the mouse is allowed to move to call it a double click
- TIME_BETWEEN_DOUBLE_CLICK = 500, ///< Time between 2 left clicks before it becoming a double click, in ms
MAX_OFFSET_HOVER = 5, ///< Maximum mouse movement before stopping a hover event.
};
extern EventState VpHandlePlaceSizingDrag();
+const std::chrono::milliseconds TIME_BETWEEN_DOUBLE_CLICK(500); ///< Time between 2 left clicks before it becoming a double click.
+
static void ScrollMainViewport(int x, int y)
{
if (_game_mode != GM_MENU) {
@@ -2991,19 +2992,27 @@ void HandleMouseEvents()
* But there is no company related window open anyway, so _current_company is not used. */
assert(HasModalProgress() || IsLocalCompany());
- static int double_click_time = 0;
+ /* Handle sprite picker before any GUI interaction */
+ if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && _input_events_this_tick == 0) {
+ /* We are done with the last draw-frame, so we know what sprites we
+ * clicked on. Reset the picker mode and invalidate the window. */
+ _newgrf_debug_sprite_picker.mode = SPM_NONE;
+ InvalidateWindowData(WC_SPRITE_ALIGNER, 0, 1);
+ }
+
+ static std::chrono::steady_clock::time_point double_click_time = {};
static Point double_click_pos = {0, 0};
/* Mouse event? */
MouseClick click = MC_NONE;
if (_left_button_down && !_left_button_clicked) {
click = MC_LEFT;
- if (double_click_time != 0 && _realtime_tick - double_click_time < TIME_BETWEEN_DOUBLE_CLICK &&
+ if (std::chrono::steady_clock::now() <= double_click_time + TIME_BETWEEN_DOUBLE_CLICK &&
double_click_pos.x != 0 && abs(_cursor.pos.x - double_click_pos.x) < MAX_OFFSET_DOUBLE_CLICK &&
double_click_pos.y != 0 && abs(_cursor.pos.y - double_click_pos.y) < MAX_OFFSET_DOUBLE_CLICK) {
click = MC_DOUBLE_LEFT;
}
- double_click_time = _realtime_tick;
+ double_click_time = std::chrono::steady_clock::now();
double_click_pos = _cursor.pos;
_left_button_clicked = true;
_input_events_this_tick++;
@@ -3020,7 +3029,7 @@ void HandleMouseEvents()
_input_events_this_tick++;
}
- static uint32 hover_time = 0;
+ static std::chrono::steady_clock::time_point hover_time = {};
static Point hover_pos = {0, 0};
if (_settings_client.gui.hover_delay_ms > 0) {
@@ -3028,10 +3037,10 @@ void HandleMouseEvents()
hover_pos.x == 0 || abs(_cursor.pos.x - hover_pos.x) >= MAX_OFFSET_HOVER ||
hover_pos.y == 0 || abs(_cursor.pos.y - hover_pos.y) >= MAX_OFFSET_HOVER) {
hover_pos = _cursor.pos;
- hover_time = _realtime_tick;
+ hover_time = std::chrono::steady_clock::now();
_mouse_hovering = false;
} else {
- if (hover_time != 0 && _realtime_tick > hover_time + _settings_client.gui.hover_delay_ms) {
+ if (std::chrono::steady_clock::now() > hover_time + std::chrono::milliseconds(_settings_client.gui.hover_delay_ms)) {
click = MC_HOVER;
_input_events_this_tick++;
_mouse_hovering = true;
@@ -3039,18 +3048,10 @@ void HandleMouseEvents()
}
}
- /* Handle sprite picker before any GUI interaction */
- if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW && _newgrf_debug_sprite_picker.click_time != _realtime_tick) {
- /* Next realtime tick? Then redraw has finished */
- _newgrf_debug_sprite_picker.mode = SPM_NONE;
- InvalidateWindowData(WC_SPRITE_ALIGNER, 0, 1);
- }
-
if (click == MC_LEFT && _newgrf_debug_sprite_picker.mode == SPM_WAIT_CLICK) {
/* Mark whole screen dirty, and wait for the next realtime tick, when drawing is finished. */
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
_newgrf_debug_sprite_picker.clicked_pixel = blitter->MoveTo(_screen.dst_ptr, _cursor.pos.x, _cursor.pos.y);
- _newgrf_debug_sprite_picker.click_time = _realtime_tick;
_newgrf_debug_sprite_picker.sprites.clear();
_newgrf_debug_sprite_picker.mode = SPM_REDRAW;
MarkWholeScreenDirty();
@@ -3138,12 +3139,13 @@ void CallWindowRealtimeTickEvent(uint delta_ms)
*/
void UpdateWindows()
{
- static uint32 last_realtime_tick = _realtime_tick;
- uint delta_ms = _realtime_tick - last_realtime_tick;
- last_realtime_tick = _realtime_tick;
+ static std::chrono::steady_clock::time_point last_time = std::chrono::steady_clock::now();
+ uint delta_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - last_time).count();
if (delta_ms == 0) return;
+ last_time = std::chrono::steady_clock::now();
+
PerformanceMeasurer framerate(PFE_DRAWING);
PerformanceAccumulator::Reset(PFE_DRAWWORLD);