signs_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: signs_cmd.cpp 21413 2010-12-05 22:25:08Z 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) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) 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 
00076 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00077 {
00078   Sign *si = Sign::GetIfValid(p1);
00079   if (si == NULL) return CMD_ERROR;
00080 
00081   /* Rename the signs when empty, otherwise remove it */
00082   if (!StrEmpty(text)) {
00083     if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
00084 
00085     if (flags & DC_EXEC) {
00086       /* Delete the old name */
00087       free(si->name);
00088       /* Assign the new one */
00089       si->name = strdup(text);
00090       si->owner = _current_company;
00091 
00092       si->UpdateVirtCoord();
00093       InvalidateWindowData(WC_SIGN_LIST, 0, 1);
00094     }
00095   } else { // Delete sign
00096     if (flags & DC_EXEC) {
00097       si->sign.MarkDirty();
00098       delete si;
00099 
00100       InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00101     }
00102   }
00103 
00104   return CommandCost();
00105 }
00106 
00114 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00115 {
00116   if (result.Failed()) return;
00117 
00118   ShowRenameSignWindow(Sign::Get(_new_sign_id));
00119   ResetObjectToPlace();
00120 }
00121 
00128 void PlaceProc_Sign(TileIndex tile)
00129 {
00130   DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
00131 }

Generated on Sun May 15 19:20:14 2011 for OpenTTD by  doxygen 1.6.1