signs_cmd.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "company_func.h"
00015 #include "signs_base.h"
00016 #include "signs_func.h"
00017 #include "command_func.h"
00018 #include "tilehighlight_func.h"
00019 #include "window_func.h"
00020 #include "string_func.h"
00021 
00022 #include "table/strings.h"
00023 
00025 SignID _new_sign_id;
00026 
00038 CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00039 {
00040   /* Try to locate a new sign */
00041   if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
00042 
00043   /* Check sign text length if any */
00044   if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
00045 
00046   /* When we execute, really make the sign */
00047   if (flags & DC_EXEC) {
00048     Sign *si = new Sign(_current_company);
00049     int x = TileX(tile) * TILE_SIZE;
00050     int y = TileY(tile) * TILE_SIZE;
00051 
00052     si->x = x;
00053     si->y = y;
00054     si->z = GetSlopePixelZ(x, y);
00055     if (!StrEmpty(text)) {
00056       si->name = strdup(text);
00057     }
00058     si->UpdateVirtCoord();
00059     InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00060     _new_sign_id = si->index;
00061   }
00062 
00063   return CommandCost();
00064 }
00065 
00077 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00078 {
00079   Sign *si = Sign::GetIfValid(p1);
00080   if (si == NULL) return CMD_ERROR;
00081 
00082   /* Rename the signs when empty, otherwise remove it */
00083   if (!StrEmpty(text)) {
00084     if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
00085 
00086     if (flags & DC_EXEC) {
00087       /* Delete the old name */
00088       free(si->name);
00089       /* Assign the new one */
00090       si->name = strdup(text);
00091       si->owner = _current_company;
00092 
00093       si->UpdateVirtCoord();
00094       InvalidateWindowData(WC_SIGN_LIST, 0, 1);
00095     }
00096   } else { // Delete sign
00097     if (flags & DC_EXEC) {
00098       si->sign.MarkDirty();
00099       delete si;
00100 
00101       InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00102     }
00103   }
00104 
00105   return CommandCost();
00106 }
00107 
00115 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00116 {
00117   if (result.Failed()) return;
00118 
00119   ShowRenameSignWindow(Sign::Get(_new_sign_id));
00120   ResetObjectToPlace();
00121 }
00122 
00129 void PlaceProc_Sign(TileIndex tile)
00130 {
00131   DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
00132 }