signs_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: signs_cmd.cpp 18781 2010-01-11 18:46:09Z rubidium $ */
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 
00024 SignID _new_sign_id;
00025 
00037 CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00038 {
00039   /* Try to locate a new sign */
00040   if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
00041 
00042   /* Check sign text length if any */
00043   if (!StrEmpty(text) && strlen(text) >= MAX_LENGTH_SIGN_NAME_BYTES) return CMD_ERROR;
00044 
00045   /* When we execute, really make the sign */
00046   if (flags & DC_EXEC) {
00047     Sign *si = new Sign(_current_company);
00048     int x = TileX(tile) * TILE_SIZE;
00049     int y = TileY(tile) * TILE_SIZE;
00050 
00051     si->x = x;
00052     si->y = y;
00053     si->z = GetSlopeZ(x, y);
00054     if (!StrEmpty(text)) {
00055       si->name = strdup(text);
00056     }
00057     si->UpdateVirtCoord();
00058     InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00059     _new_sign_id = si->index;
00060   }
00061 
00062   return CommandCost();
00063 }
00064 
00075 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00076 {
00077   Sign *si = Sign::GetIfValid(p1);
00078   if (si == NULL) return CMD_ERROR;
00079 
00080   /* Rename the signs when empty, otherwise remove it */
00081   if (!StrEmpty(text)) {
00082     if (strlen(text) >= MAX_LENGTH_SIGN_NAME_BYTES) return CMD_ERROR;
00083 
00084     if (flags & DC_EXEC) {
00085       /* Delete the old name */
00086       free(si->name);
00087       /* Assign the new one */
00088       si->name = strdup(text);
00089       si->owner = _current_company;
00090 
00091       si->UpdateVirtCoord();
00092       InvalidateWindowData(WC_SIGN_LIST, 0, 1);
00093     }
00094   } else { // Delete sign
00095     if (flags & DC_EXEC) {
00096       si->sign.MarkDirty();
00097       delete si;
00098 
00099       InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00100     }
00101   }
00102 
00103   return CommandCost();
00104 }
00105 
00113 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00114 {
00115   if (result.Failed()) return;
00116 
00117   ShowRenameSignWindow(Sign::Get(_new_sign_id));
00118   ResetObjectToPlace();
00119 }
00120 
00127 void PlaceProc_Sign(TileIndex tile)
00128 {
00129   DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
00130 }

Generated on Sat Jul 31 21:37:52 2010 for OpenTTD by  doxygen 1.6.1