Permalink
Browse files

Change: Use GUITimer class instead of bare int/uints.

  • Loading branch information...
PeterN committed May 20, 2018
1 parent 59fe4f2 commit 806e7d25dddcc8b0e9c3f372ed956c63c6508381
@@ -521,6 +521,7 @@
<ClInclude Include="..\src\group_gui.h" />
<ClInclude Include="..\src\group_type.h" />
<ClInclude Include="..\src\gui.h" />
<ClInclude Include="..\src\guitimer_func.h" />
<ClInclude Include="..\src\heightmap.h" />
<ClInclude Include="..\src\highscore.h" />
<ClInclude Include="..\src\hotkeys.h" />
@@ -651,6 +651,9 @@
<ClInclude Include="..\src\gui.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\guitimer_func.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\heightmap.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -521,6 +521,7 @@
<ClInclude Include="..\src\group_gui.h" />
<ClInclude Include="..\src\group_type.h" />
<ClInclude Include="..\src\gui.h" />
<ClInclude Include="..\src\guitimer_func.h" />
<ClInclude Include="..\src\heightmap.h" />
<ClInclude Include="..\src\highscore.h" />
<ClInclude Include="..\src\hotkeys.h" />
@@ -651,6 +651,9 @@
<ClInclude Include="..\src\gui.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\guitimer_func.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\heightmap.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -210,6 +210,7 @@ group.h
group_gui.h
group_type.h
gui.h
guitimer_func.h
heightmap.h
highscore.h
hotkeys.h
@@ -0,0 +1,65 @@
/* $Id$ */

/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file guitimer_func.h GUI Timers. */

#ifndef GUITIMER_FUNC_H
#define GUITIMER_FUNC_H

class GUITimer
{
protected:
uint timer;
uint interval;

public:
GUITimer() : timer(0), interval(0) { }
explicit GUITimer(uint interval) : timer(0), interval(interval) { }

inline bool HasElapsed() const
{
return this->interval == 0;
}

inline void SetInterval(uint interval)
{
this->timer = 0;
this->interval = interval;
}

/**
* Count how many times the interval has elapsed.
* Use to ensure a specific amount of events happen within a timeframe, e.g. for animation.
* @param delta Time since last test.
* @return Number of times the interval has elapsed.
*/
inline uint CountElapsed(uint delta)
{
if (this->interval == 0) return 0;
uint count = delta / this->interval;
if (this->timer + (delta % this->interval) >= this->interval) count++;
this->timer = (this->timer + delta) % this->interval;
return count;
}

/**
* Test if a timer has elapsed.
* Use to ensure an event happens only once within a timeframe, e.g. for window updates.
* @param delta Time since last test.
* @return True iff the timer has elapsed.
*/
inline bool Elapsed(uint delta)
{
if (this->CountElapsed(delta) == 0) return false;
this->SetInterval(0);
return true;
}
};

#endif /* GUITIMER_FUNC_H */
@@ -32,6 +32,7 @@
#include "linkgraph/linkgraph_gui.h"
#include "tilehighlight_func.h"
#include "hotkeys.h"
#include "guitimer_func.h"

#include "saveload/saveload.h"

@@ -235,7 +236,7 @@ enum {

struct MainWindow : Window
{
int refresh;
GUITimer refresh;

/* Refresh times in milliseconds */
static const uint LINKGRAPH_REFRESH_PERIOD = 7650;
@@ -251,14 +252,14 @@ struct MainWindow : Window
nvp->InitializeViewport(this, TileXY(32, 32), ZOOM_LVL_VIEWPORT);

this->viewport->overlay = new LinkGraphOverlay(this, WID_M_VIEWPORT, 0, 0, 3);
this->refresh = LINKGRAPH_DELAY;
this->refresh.SetInterval(LINKGRAPH_DELAY);
}

virtual void OnRealtimeTick(uint delta_ms)
{
if (!TimerElapsed(this->refresh, delta_ms)) return;
if (!this->refresh.Elapsed(delta_ms)) return;

this->refresh = LINKGRAPH_REFRESH_PERIOD;
this->refresh.SetInterval(LINKGRAPH_REFRESH_PERIOD);

if (this->viewport->overlay->GetCargoMask() == 0 ||
this->viewport->overlay->GetCompanyMask() == 0) {
@@ -435,7 +436,7 @@ struct MainWindow : Window
this->viewport->scrollpos_y += ScaleByZoom(delta.y, this->viewport->zoom);
this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x;
this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y;
this->refresh = LINKGRAPH_DELAY;
this->refresh.SetInterval(LINKGRAPH_DELAY);
}

virtual void OnMouseWheel(int wheel)
@@ -450,7 +451,7 @@ struct MainWindow : Window
if (this->viewport != NULL) {
NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_M_VIEWPORT);
nvp->UpdateViewportCoordinates(this);
this->refresh = LINKGRAPH_DELAY;
this->refresh.SetInterval(LINKGRAPH_DELAY);
}
}

@@ -26,6 +26,7 @@
#include "core/geometry_func.hpp"
#include "newgrf_debug.h"
#include "zoom_func.h"
#include "guitimer_func.h"

#include "widgets/misc_widget.h"

@@ -458,13 +459,14 @@ struct AboutWindow : public Window {
static const int num_visible_lines = 19; ///< The number of lines visible simultaneously

static const uint TIMER_INTERVAL = 150; ///< Scrolling interval in ms
uint timer;
GUITimer timer;

AboutWindow() : Window(&_about_desc)
{
this->InitNested(WN_GAME_OPTIONS_ABOUT);

this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
this->timer.SetInterval(TIMER_INTERVAL);
}

virtual void SetStringParameters(int widget) const
@@ -505,7 +507,7 @@ struct AboutWindow : public Window {

virtual void OnRealtimeTick(uint delta_ms)
{
uint count = CountIntervalElapsed(this->timer, delta_ms, TIMER_INTERVAL);
uint count = this->timer.CountElapsed(delta_ms);
if (count > 0) {
this->text_position -= count;
/* If the last text has scrolled start a new from the start */
@@ -30,6 +30,7 @@
#include "textfile_gui.h"
#include "tilehighlight_func.h"
#include "fios.h"
#include "guitimer_func.h"

#include "widgets/newgrf_widget.h"
#include "widgets/misc_widget.h"
@@ -150,7 +151,7 @@ struct NewGRFParametersWindow : public Window {
bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
bool clicked_dropdown; ///< Whether the dropdown is open.
bool closing_dropdown; ///< True, if the dropdown list is currently closing.
int timeout; ///< How long before we unpress the last-pressed button?
GUITimer timeout; ///< How long before we unpress the last-pressed button?
uint clicked_row; ///< The selected parameter
int line_height; ///< Height of a row in the matrix widget.
Scrollbar *vscroll;
@@ -162,7 +163,6 @@ struct NewGRFParametersWindow : public Window {
clicked_button(UINT_MAX),
clicked_dropdown(false),
closing_dropdown(false),
timeout(0),
clicked_row(UINT_MAX),
editable(editable)
{
@@ -403,7 +403,7 @@ struct NewGRFParametersWindow : public Window {
par_info->SetValue(this->grf_config, val);

this->clicked_button = num;
this->timeout = 150;
this->timeout.SetInterval(150);
}
} else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
/* Display a query box so users can enter a custom value. */
@@ -485,7 +485,7 @@ struct NewGRFParametersWindow : public Window {

virtual void OnRealtimeTick(uint delta_ms)
{
if (TimerElapsed(this->timeout, delta_ms)) {
if (timeout.Elapsed(delta_ms)) {
this->clicked_button = UINT_MAX;
this->SetDirty();
}
@@ -33,6 +33,7 @@
#include "command_func.h"
#include "company_base.h"
#include "settings_internal.h"
#include "guitimer_func.h"

#include "widgets/news_widget.h"

@@ -262,7 +263,7 @@ struct NewsWindow : Window {
const NewsItem *ni; ///< News item to display.
static int duration; ///< Remaining time for showing the current news message (may only be access while a news item is displayed).

uint timer;
GUITimer timer;

NewsWindow(WindowDesc *desc, const NewsItem *ni) : Window(desc), ni(ni)
{
@@ -273,6 +274,8 @@ struct NewsWindow : Window {

this->flags |= WF_DISABLE_VP_SCROLL;

this->timer.SetInterval(15);

this->CreateNestedTree();

/* For company news with a face we have a separate headline in param[0] */
@@ -489,7 +492,7 @@ struct NewsWindow : Window {

virtual void OnRealtimeTick(uint delta_ms)
{
int count = CountIntervalElapsed(this->timer, delta_ms, 15);
int count = this->timer.CountElapsed(delta_ms);
if (count > 0) {
/* Scroll up newsmessages from the bottom */
int newtop = max(this->top - 2 * count, _screen.height - this->height - this->status_height - this->chat_height);
@@ -23,6 +23,7 @@
#include "sound_func.h"
#include "window_func.h"
#include "company_base.h"
#include "guitimer_func.h"

#include "smallmap_gui.h"

@@ -1055,7 +1056,7 @@ void SmallMapWindow::SetupWidgetData()
this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
}

SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(GUITimer(FORCE_REFRESH_PERIOD))
{
_smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
@@ -1390,7 +1391,7 @@ int SmallMapWindow::GetPositionOnLegend(Point pt)
}
if (new_highlight != _smallmap_industry_highlight) {
_smallmap_industry_highlight = new_highlight;
this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
_smallmap_industry_highlight_state = true;
this->SetDirty();
}
@@ -1573,7 +1574,7 @@ int SmallMapWindow::GetPositionOnLegend(Point pt)
/* virtual */ void SmallMapWindow::OnRealtimeTick(uint delta_ms)
{
/* Update the window every now and then */
if (!TimerElapsed(this->refresh, delta_ms)) return;
if (!this->refresh.Elapsed(delta_ms)) return;

if (this->map_type == SMT_LINKSTATS) {
uint32 company_mask = this->GetOverlayCompanyMask();
@@ -1585,7 +1586,7 @@ int SmallMapWindow::GetPositionOnLegend(Point pt)
}
_smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;

this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
this->refresh.SetInterval(_smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD);
this->SetDirty();
}

@@ -19,6 +19,7 @@
#include "blitter/factory.hpp"
#include "linkgraph/linkgraph_gui.h"
#include "widgets/smallmap_widget.h"
#include "guitimer_func.h"

/* set up the cargos to be displayed in the smallmap's route legend */
void BuildLinkStatsLegend();
@@ -79,7 +80,7 @@ class SmallMapWindow : public Window {
int32 subscroll; ///< Number of pixels (0..3) between the right end of the base tile and the pixel at the top-left corner of the smallmap display.
int zoom; ///< Zoom level. Bigger number means more zoom-out (further away).

int refresh; ///< Refresh timer, in millseconds.
GUITimer refresh; ///< Refresh timer.
LinkGraphOverlay *overlay;

static void BreakIndustryChainLink();
Oops, something went wrong.

0 comments on commit 806e7d2

Please sign in to comment.