1
0
Fork 0

Codechange: Change SetDateCallback into a std::function, so there is no need for void* user data.

pull/14037/head
frosch 2025-04-18 19:57:07 +02:00 committed by frosch
parent 0d4588688f
commit c09e825e0b
3 changed files with 13 additions and 24 deletions

View File

@ -26,8 +26,7 @@
/** Window to select a date graphically by using dropdowns */
struct SetDateWindow : Window {
SetDateCallback *callback = nullptr; ///< Callback to call when a date has been selected
void *callback_data = nullptr; ///< Callback data pointer.
SetDateCallback callback; ///< Callback to call when a date has been selected
TimerGameEconomy::YearMonthDay date{}; ///< The currently selected date
TimerGameEconomy::Year min_year{}; ///< The minimum year in the year dropdown
TimerGameEconomy::Year max_year{}; ///< The maximum year (inclusive) in the year dropdown
@ -42,10 +41,9 @@ struct SetDateWindow : Window {
* @param max_year the maximum year (inclusive) to show in the year dropdown
* @param callback the callback to call once a date has been selected
*/
SetDateWindow(WindowDesc &desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data) :
SetDateWindow(WindowDesc &desc, WindowNumber window_number, Window *parent, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback) :
Window(desc),
callback(callback),
callback_data(callback_data),
callback(std::move(callback)),
min_year(std::max(EconomyTime::MIN_YEAR, min_year)),
max_year(std::min(EconomyTime::MAX_YEAR, max_year))
{
@ -149,7 +147,7 @@ struct SetDateWindow : Window {
break;
case WID_SD_SET_DATE:
if (this->callback != nullptr) this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day), this->callback_data);
this->callback(this, TimerGameEconomy::ConvertYMDToDate(this->date.year, this->date.month, this->date.day));
this->Close();
break;
}
@ -212,10 +210,9 @@ static WindowDesc _set_date_desc(
* @param min_year the minimum year to show in the year dropdown
* @param max_year the maximum year (inclusive) to show in the year dropdown
* @param callback the callback to call once a date has been selected
* @param callback_data extra callback data
*/
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback)
{
CloseWindowByClass(WC_SET_DATE);
new SetDateWindow(_set_date_desc, window_number, parent, initial_date, min_year, max_year, callback, callback_data);
new SetDateWindow(_set_date_desc, window_number, parent, initial_date, min_year, max_year, std::move(callback));
}

View File

@ -18,8 +18,8 @@
* @param w the window that sends the callback
* @param date the date that has been chosen
*/
typedef void SetDateCallback(const Window *w, TimerGameEconomy::Date date, void *data);
using SetDateCallback = std::function<void (const Window *w, TimerGameEconomy::Date date)>;
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data);
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback &&callback);
#endif /* DATE_GUI_H */

View File

@ -176,18 +176,6 @@ static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID
}
}
/**
* Callback for when a time has been chosen to start the time table
* @param w the window related to the setting of the date
* @param date the actually chosen date
*/
static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
{
Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date));
}
struct TimetableWindow : Window {
int sel_index = -1;
VehicleTimetableWidgets query_widget{}; ///< Which button was clicked to open the query text input?
@ -661,7 +649,11 @@ struct TimetableWindow : Window {
this->change_timetable_all = _ctrl_pressed;
ShowQueryString({}, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QueryStringFlag::AcceptUnchanged);
} else {
ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed)));
ShowSetDateWindow(this, v->index.base(), TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS,
[ctrl=_ctrl_pressed](const Window *w, TimerGameEconomy::Date date) {
Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, ctrl, GetStartTickFromDate(date));
}
);
}
break;