Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / signs_cmd.cpp
bloba0843917d840cb377f9e0b9fa3d21b908992c14b
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file signs_cmd.cpp Handling of sign related commands. */
10 #include "stdafx.h"
11 #include "landscape.h"
12 #include "company_func.h"
13 #include "signs_base.h"
14 #include "signs_func.h"
15 #include "command_func.h"
16 #include "tilehighlight_func.h"
17 #include "viewport_kdtree.h"
18 #include "window_func.h"
19 #include "string_func.h"
21 #include "table/strings.h"
23 #include "safeguards.h"
25 /** The last built sign. */
26 SignID _new_sign_id;
28 /**
29 * Place a sign at the given coordinates. Ownership of sign has
30 * no effect whatsoever except for the colour the sign gets for easy recognition,
31 * but everybody is able to rename/remove it.
32 * @param tile tile to place sign at
33 * @param flags type of operation
34 * @param p1 unused
35 * @param p2 unused
36 * @param text unused
37 * @return the cost of this operation or an error
39 CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
41 /* Try to locate a new sign */
42 if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
44 /* Check sign text length if any */
45 if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
47 /* When we execute, really make the sign */
48 if (flags & DC_EXEC) {
49 Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
50 int x = TileX(tile) * TILE_SIZE;
51 int y = TileY(tile) * TILE_SIZE;
53 si->x = x;
54 si->y = y;
55 si->z = GetSlopePixelZ(x, y);
56 if (!StrEmpty(text)) {
57 si->name = stredup(text);
59 si->UpdateVirtCoord();
60 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
61 _new_sign_id = si->index;
64 return CommandCost();
67 /**
68 * Rename a sign. If the new name of the sign is empty, we assume
69 * the user wanted to delete it. So delete it. Ownership of signs
70 * has no meaning/effect whatsoever except for eyecandy
71 * @param tile unused
72 * @param flags type of operation
73 * @param p1 index of the sign to be renamed/removed
74 * @param p2 unused
75 * @param text the new name or an empty string when resetting to the default
76 * @return the cost of this operation or an error
78 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
80 Sign *si = Sign::GetIfValid(p1);
81 if (si == nullptr) return CMD_ERROR;
82 if (si->owner == OWNER_DEITY && _current_company != OWNER_DEITY && _game_mode != GM_EDITOR) return CMD_ERROR;
84 /* Rename the signs when empty, otherwise remove it */
85 if (!StrEmpty(text)) {
86 if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
88 if (flags & DC_EXEC) {
89 /* Assign the new one */
90 si->name = text;
91 if (_game_mode != GM_EDITOR) si->owner = _current_company;
93 si->UpdateVirtCoord();
94 InvalidateWindowData(WC_SIGN_LIST, 0, 1);
96 } else { // Delete sign
97 if (flags & DC_EXEC) {
98 si->sign.MarkDirty();
99 if (si->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeSign(si->index));
100 delete si;
102 InvalidateWindowData(WC_SIGN_LIST, 0, 0);
106 return CommandCost();
110 * Callback function that is called after a sign is placed
111 * @param result of the operation
112 * @param tile unused
113 * @param p1 unused
114 * @param p2 unused
115 * @param cmd unused
117 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
119 if (result.Failed()) return;
121 ShowRenameSignWindow(Sign::Get(_new_sign_id));
122 ResetObjectToPlace();
127 * PlaceProc function, called when someone pressed the button if the
128 * sign-tool is selected
129 * @param tile on which to place the sign
131 void PlaceProc_Sign(TileIndex tile)
133 DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);