Build confirmation window, for each build action on the map

This commit is contained in:
Sergii Pylypenko
2016-04-06 23:34:28 +03:00
parent a474c233e0
commit a7a8e35968
10 changed files with 204 additions and 1 deletions

View File

@@ -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

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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 */

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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;
}

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -84,6 +84,7 @@
#include "linkgraph/linkgraph_gui.h"
#include "viewport_sprite_sorter.h"
#include "bridge_map.h"
#include "build_confirmation_func.h"
#include <map>
@@ -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 */

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
/** @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 */

View File

@@ -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:

View File

@@ -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