fix crashes when the selected element is NULL (via lua), add gfx.getHexColor (inverse of gfx.getColors)

This commit is contained in:
jacob1 2014-06-09 17:42:44 -04:00
parent 89ffa60529
commit c44e734abe
5 changed files with 38 additions and 11 deletions

View File

@ -338,7 +338,7 @@ if not GetOption('clean'):
findLibs(env, conf)
env = conf.Finish()
else:
import os, shutil
import shutil
try:
shutil.rmtree("generated/")
except:

View File

@ -558,7 +558,7 @@ bool GameController::MouseDown(int x, int y, unsigned button)
x = point.X;
y = point.Y;
if (ret && y<YRES && x<XRES && !gameView->GetPlacingSave())
if (gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
if (gameModel->GetActiveTool(0) && gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
{
sign * foundSign = GetSignAt(x, y);
if(foundSign && splitsign(foundSign->text.c_str()))
@ -575,7 +575,7 @@ bool GameController::MouseUp(int x, int y, unsigned button)
y = point.Y;
if (ret && y<YRES && x<XRES && !gameView->GetPlacingSave())
{
if (gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
if (gameModel->GetActiveTool(0) && gameModel->GetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking
{
sign * foundSign = GetSignAt(x, y);
if(foundSign) {

View File

@ -379,15 +379,21 @@ void GameModel::BuildMenus()
Tool * GameModel::GetToolFromIdentifier(std::string identifier)
{
for(std::vector<Menu*>::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter)
for (std::vector<Menu*>::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter)
{
std::vector<Tool*> menuTools = (*iter)->GetToolList();
for(std::vector<Tool*>::iterator titer = menuTools.begin(), tend = menuTools.end(); titer != tend; ++titer)
for (std::vector<Tool*>::iterator titer = menuTools.begin(), tend = menuTools.end(); titer != tend; ++titer)
{
if(identifier == (*titer)->GetIdentifier())
if (identifier == (*titer)->GetIdentifier())
return *titer;
}
}
for (std::vector<Tool*>::iterator iter = extraElementTools.begin(), end = extraElementTools.end(); iter != end; ++iter)
{
if (identifier == (*iter)->GetIdentifier())
return *iter;
}
return NULL;
}

View File

@ -2395,6 +2395,7 @@ void LuaScriptInterface::initGraphicsAPI()
{"drawCircle", graphics_drawCircle},
{"fillCircle", graphics_fillCircle},
{"getColors", graphics_getColors},
{"getHexColor", graphics_getHexColor},
{NULL, NULL}
};
luaL_register(l, "graphics", graphicsAPIMethods);
@ -2563,7 +2564,7 @@ int LuaScriptInterface::graphics_fillCircle(lua_State * l)
int LuaScriptInterface::graphics_getColors(lua_State * l)
{
int color = lua_tointeger(l, 1);
unsigned int color = lua_tointeger(l, 1);
int a = color >> 24;
int r = (color >> 16)&0xFF;
@ -2577,6 +2578,20 @@ int LuaScriptInterface::graphics_getColors(lua_State * l)
return 4;
}
int LuaScriptInterface::graphics_getHexColor(lua_State * l)
{
int r = lua_tointeger(l, 1);
int g = lua_tointeger(l, 2);
int b = lua_tointeger(l, 3);
int a = 0;
if (lua_gettop(l) >= 4)
a = lua_tointeger(l, 4);
unsigned int color = (a<<24) + (r<<16) + (g<<8) + b;
lua_pushinteger(l, color);
return 1;
}
void LuaScriptInterface::initFileSystemAPI()
{
//Methods
@ -2819,14 +2834,19 @@ bool LuaScriptInterface::OnBrushChanged(int brushType, int rx, int ry)
bool LuaScriptInterface::OnActiveToolChanged(int toolSelection, Tool * tool)
{
std::string identifier;
if (tool)
identifier = tool->GetIdentifier();
else
identifier = "";
if (toolSelection == 0)
luacon_selectedl = tool->GetIdentifier();
luacon_selectedl = identifier;
else if (toolSelection == 1)
luacon_selectedr = tool->GetIdentifier();
luacon_selectedr = identifier;
else if (toolSelection == 2)
luacon_selectedalt = tool->GetIdentifier();
luacon_selectedalt = identifier;
else if (toolSelection == 3)
luacon_selectedreplace = tool->GetIdentifier();
luacon_selectedreplace = identifier;
return true;
}

View File

@ -132,6 +132,7 @@ class LuaScriptInterface: public CommandInterface
static int graphics_drawCircle(lua_State * l);
static int graphics_fillCircle(lua_State * l);
static int graphics_getColors(lua_State * l);
static int graphics_getHexColor(lua_State * l);
void initFileSystemAPI();
static int fileSystem_list(lua_State * l);