From a7a8e359680d053fe4982e58e284cb2b321193a7 Mon Sep 17 00:00:00 2001 From: Sergii Pylypenko Date: Wed, 6 Apr 2016 23:34:28 +0300 Subject: [PATCH] Build confirmation window, for each build action on the map --- source.list | 2 + src/build_confirmation_func.h | 25 +++++ src/build_confirmation_gui.cpp | 119 ++++++++++++++++++++++++ src/industry_gui.cpp | 3 + src/tilehighlight_func.h | 2 + src/town_gui.cpp | 3 + src/viewport.cpp | 15 ++- src/widgets/build_confirmation_widget.h | 22 +++++ src/window.cpp | 8 ++ src/window_type.h | 6 ++ 10 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 src/build_confirmation_func.h create mode 100644 src/build_confirmation_gui.cpp create mode 100644 src/widgets/build_confirmation_widget.h diff --git a/source.list b/source.list index 2e1b24c98e..b28fe8fb25 100644 --- a/source.list +++ b/source.list @@ -144,6 +144,7 @@ base_media_func.h base_station_base.h bmp.h bridge.h +build_confirmation_func.h cargo_type.h cargoaction.h cargomonitor.h @@ -453,6 +454,7 @@ airport_gui.cpp autoreplace_gui.cpp bootstrap_gui.cpp bridge_gui.cpp +build_confirmation_gui.cpp build_vehicle_gui.cpp cheat_gui.cpp company_gui.cpp diff --git a/src/build_confirmation_func.h b/src/build_confirmation_func.h new file mode 100644 index 0000000000..a1cbf4dbf7 --- /dev/null +++ b/src/build_confirmation_func.h @@ -0,0 +1,25 @@ +/* $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 . + */ + +/** @file build_confirmation_func.h Transparent confirmation dialog for building anything on the map. */ + +#ifndef BUILD_CONFIRMATION_FUNC_H +#define BUILD_CONFIRMATION_FUNC_H + +#include "stdafx.h" +#include "window_func.h" +#include "widget_type.h" + + +void ShowBuildConfirmationWindow(); +void HideBuildConfirmationWindow(); +bool ConfirmationWindowShown(); + + +#endif /* BUILD_CONFIRMATION_FUNC_H */ diff --git a/src/build_confirmation_gui.cpp b/src/build_confirmation_gui.cpp new file mode 100644 index 0000000000..fad04cb92d --- /dev/null +++ b/src/build_confirmation_gui.cpp @@ -0,0 +1,119 @@ +/* $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 . + */ + +/** @file build_confirmation_gui.cpp Transparent confirmation dialog for building anything on the map. */ + +#include "stdafx.h" +#include "string_func.h" +#include "strings_func.h" +#include "window_func.h" +#include "widget_type.h" +#include "window_gui.h" +#include "gfx_func.h" +#include "tilehighlight_func.h" + +#include "widgets/build_confirmation_widget.h" +#include "build_confirmation_func.h" + +#include "table/strings.h" + +#include "safeguards.h" + + +static const NWidgetPart _nested_build_confirmation_widgets[] = { + NWidget(NWID_VERTICAL), + NWidget(NWID_SPACER), SetFill(1, 1), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BC_OK), SetDataTip(STR_BUTTON_OK, STR_NULL), SetSizingType(NWST_BUTTON), + NWidget(NWID_SPACER), SetFill(1, 1), + NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BC_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetSizingType(NWST_BUTTON), + NWidget(NWID_SPACER), SetFill(1, 1), + EndContainer(), +}; + +/** GUI for confirming building actions. */ +struct BuildConfirmationWindow : Window { + + BuildConfirmationWindow(WindowDesc *desc) : Window(desc) + { + this->InitNested(0); + } + + void OnClick(Point pt, int widget, int click_count) + { + switch (widget) { + case WID_BC_OK: + ConfirmPlacingObject(); + break; + case WID_BC_CANCEL: + ResetObjectToPlace(); + break; + } + HideBuildConfirmationWindow(); // this == NULL after this call + } + + virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) + { + } + + /* + virtual void DrawWidget(const Rect &r, int widget) const + { + switch (widget) { + case WID_BV_LIST: + break; + } + } + */ + + /* + virtual void OnPaint() + { + this->DrawWidgets(); + } + */ +}; + +static WindowDesc _build_confirmation_desc( + WDP_AUTO, "build_confirmation", 100, 80, + WC_BUILD_CONFIRMATION, WC_NONE, + WDF_CONSTRUCTION, + _nested_build_confirmation_widgets, lengthof(_nested_build_confirmation_widgets) +); + +static bool _confirmationWindowShown = false; // Just to speed up window hiding, HideBuildConfirmationWindow() is called very often + +/** + * Show build confirmation window under the mouse cursor +*/ +void ShowBuildConfirmationWindow() +{ + HideBuildConfirmationWindow(); + BuildConfirmationWindow *wnd = new BuildConfirmationWindow(&_build_confirmation_desc); + wnd->left = _cursor.pos.x - wnd->width / 2; + wnd->top = _cursor.pos.y - wnd->height / 4; + wnd->SetDirty(); + _confirmationWindowShown = true; +} + +/** + * Destory build confirmation window, this does not cancel current action +*/ +void HideBuildConfirmationWindow() +{ + if (!_confirmationWindowShown) { + //return; + } + DeleteWindowById(WC_BUILD_CONFIRMATION, 0); + _confirmationWindowShown = false; +} + +bool ConfirmationWindowShown() +{ + return _confirmationWindowShown; +} diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 3804cb126f..552aeeba97 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -550,6 +550,7 @@ public: virtual void OnPlaceObject(Point pt, TileIndex tile) { VpStartPlaceSizing(tile, VPM_SINGLE_TILE, DDSP_SINGLE_TILE); + MoveAllWindowsOffScreen(); } virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) @@ -562,6 +563,7 @@ public: if (pt.x == -1) return; assert(end_tile == start_tile); + MoveAllHiddenWindowsBackToScreen(); bool success = true; /* We do not need to protect ourselves against "Random Many Industries" in this mode */ const IndustrySpec *indsp = GetIndustrySpec(this->selected_type); @@ -624,6 +626,7 @@ public: virtual void OnPlaceObjectAbort() { + MoveAllHiddenWindowsBackToScreen(); this->RaiseButtons(); } diff --git a/src/tilehighlight_func.h b/src/tilehighlight_func.h index ac1567277b..c9c7ea809f 100644 --- a/src/tilehighlight_func.h +++ b/src/tilehighlight_func.h @@ -22,6 +22,8 @@ bool HandlePlacePushButton(Window *w, int widget, CursorID cursor, HighLightStyl void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w); void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowClass window_class, WindowNumber window_num); void ResetObjectToPlace(); +void ConfirmPlacingObject(); + void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method); void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index f86e2a12bd..9acf17dd7c 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1176,6 +1176,7 @@ public: virtual void OnPlaceObject(Point pt, TileIndex tile) { VpStartPlaceSizing(tile, VPM_SINGLE_TILE, DDSP_SINGLE_TILE); + MoveAllWindowsOffScreen(); } virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) @@ -1187,10 +1188,12 @@ public: { assert(start_tile == end_tile); this->ExecuteFoundTownCommand(end_tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown); + MoveAllHiddenWindowsBackToScreen(); } virtual void OnPlaceObjectAbort() { + MoveAllHiddenWindowsBackToScreen(); this->RaiseButtons(); this->UpdateButtons(false); } diff --git a/src/viewport.cpp b/src/viewport.cpp index 957628de12..251bdfc0a4 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -84,6 +84,7 @@ #include "linkgraph/linkgraph_gui.h" #include "viewport_sprite_sorter.h" #include "bridge_map.h" +#include "build_confirmation_func.h" #include @@ -2163,6 +2164,7 @@ static void PlaceObject() w = _thd.GetCallbackWnd(); if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y)); + HideBuildConfirmationWindow(); } @@ -3139,6 +3141,7 @@ EventState VpHandlePlaceSizingDrag() /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */ if (_left_button_down) { + HideBuildConfirmationWindow(); w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor()); return ES_HANDLED; } @@ -3161,10 +3164,19 @@ EventState VpHandlePlaceSizingDrag() SetTileSelectSize(1, 1); place_mouseup: - w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y)); + ShowBuildConfirmationWindow(); return ES_HANDLED; } +void ConfirmPlacingObject() +{ + Window *w = _thd.GetCallbackWnd(); + if (w == NULL) ResetObjectToPlace(); + + + w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y)); +} + /** * Change the cursor and mouse click/drag handling to a mode for performing special operations like tile area selection, object placement, etc. * @param icon New shape of the mouse cursor. @@ -3202,6 +3214,7 @@ void SetObjectToPlace(CursorID icon, PaletteID pal, HighLightStyle mode, WindowC * place or not properly reset the original selection. */ _thd.window_class = WC_INVALID; if (w != NULL) w->OnPlaceObjectAbort(); + HideBuildConfirmationWindow(); } /* Mark the old selection dirty, in case the selection shape or colour changes */ diff --git a/src/widgets/build_confirmation_widget.h b/src/widgets/build_confirmation_widget.h new file mode 100644 index 0000000000..66ca8cb5f4 --- /dev/null +++ b/src/widgets/build_confirmation_widget.h @@ -0,0 +1,22 @@ +/* $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 . + */ + +/** @file build_confirmation_widget.h Types related to the build_confirmation widgets. */ + +#ifndef WIDGETS_BUILD_CONFIRMATION_WIDGET_H +#define WIDGETS_BUILD_CONFIRMATION_WIDGET_H + +/** Widgets of the #BuildVehicleWindow class. */ +enum BuildConfirmationWidgets { + WID_BC_PRICE, ///< Estimated price. + WID_BC_OK, ///< Confirm action. + WID_BC_CANCEL, ///< Cancel action. +}; + +#endif /* WIDGETS_BUILD_CONFIRMATION_WIDGET_H */ diff --git a/src/window.cpp b/src/window.cpp index 34291ec94b..a0382f78df 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -42,6 +42,7 @@ #include "station_base.h" #include "waypoint_base.h" #include "command_func.h" +#include "build_confirmation_func.h" #include "table/strings.h" @@ -1439,6 +1440,12 @@ void Window::InitializeData(WindowNumber window_number) this->nested_focus = NULL; this->window_number = window_number; + if (this->window_class != WC_BUILD_CONFIRMATION && + this->window_class != WC_TOOLTIPS && + this->window_class != WC_NEWS_WINDOW) { + HideBuildConfirmationWindow(); + } + this->OnInit(); /* Initialize nested widget tree. */ if (this->nested_array == NULL) { @@ -3650,6 +3657,7 @@ static void MoveAllWindowsOffScreen(bool moveOffScreen) FOR_ALL_WINDOWS_FROM_BACK(w) { switch (w->window_class) { case WC_MAIN_WINDOW: + case WC_BUILD_CONFIRMATION: case WC_BOOTSTRAP: case WC_MAIN_TOOLBAR: case WC_MAIN_TOOLBAR_RIGHT: diff --git a/src/window_type.h b/src/window_type.h index 2cf70331ee..91c0f25c82 100644 --- a/src/window_type.h +++ b/src/window_type.h @@ -59,6 +59,12 @@ enum WindowClass { */ WC_MAIN_TOOLBAR_RIGHT, + /** + * Confirmation window for building anything; %Window numbers: + * - 0 = #BuildConfirmationWidgets + */ + WC_BUILD_CONFIRMATION, + /** * Statusbar (at the bottom of your screen); %Window numbers: * - 0 = #StatusbarWidgets