when called with no arguments, some tpt. functions will act as get functions (unfinished)

Also, redo some menu stuff to use int's instead of Menu *s, and fix bug with toggleable buttons being toggled without triggering their actions
This commit is contained in:
jacob1 2013-05-14 23:50:26 -04:00
parent ab3675d6ee
commit 94d21a1679
9 changed files with 165 additions and 134 deletions

View File

@ -692,14 +692,11 @@ char *luacon_geterror(){
lua_pop(luacon_ci->l, 1); lua_pop(luacon_ci->l, 1);
return err; return err;
} }
/*void luacon_close(){
lua_close(l);
}*/
//TPT Interface methods //TPT Interface methods
int luatpt_test(lua_State* l) int luatpt_test(lua_State* l)
{ {
int testint = 0; int testint = 0;
testint = luaL_optint(l, 1, 0); testint = luaL_optint(l, 1, 0);
printf("Test successful, got %d\n", testint); printf("Test successful, got %d\n", testint);
return 0; return 0;
@ -863,7 +860,7 @@ int luatpt_error(lua_State* l)
} }
int luatpt_drawtext(lua_State* l) int luatpt_drawtext(lua_State* l)
{ {
char *string; char *string;
int textx, texty, textred, textgreen, textblue, textalpha; int textx, texty, textred, textgreen, textblue, textalpha;
textx = luaL_optint(l, 1, 0); textx = luaL_optint(l, 1, 0);
texty = luaL_optint(l, 2, 0); texty = luaL_optint(l, 2, 0);
@ -916,7 +913,12 @@ int luatpt_create(lua_State* l)
int luatpt_setpause(lua_State* l) int luatpt_setpause(lua_State* l)
{ {
int pausestate; int pausestate;
pausestate = luaL_optint(l, 1, 0); pausestate = luaL_optint(l, 1, -1);
if (pausestate == -1)
{
lua_pushnumber(l, luacon_model->GetPaused());
return 1;
}
luacon_model->SetPaused(pausestate==0?0:1); luacon_model->SetPaused(pausestate==0?0:1);
return 0; return 0;
} }
@ -924,21 +926,27 @@ int luatpt_setpause(lua_State* l)
int luatpt_togglepause(lua_State* l) int luatpt_togglepause(lua_State* l)
{ {
luacon_model->SetPaused(!luacon_model->GetPaused()); luacon_model->SetPaused(!luacon_model->GetPaused());
//sys_pause=!sys_pause; lua_pushnumber(l, luacon_model->GetPaused());
return 0; return 1;
} }
int luatpt_togglewater(lua_State* l) int luatpt_togglewater(lua_State* l)
{ {
luacon_sim->water_equal_test=!luacon_sim->water_equal_test; luacon_sim->water_equal_test=!luacon_sim->water_equal_test;
return 0; lua_pushnumber(l, luacon_sim->water_equal_test);
return 1;
} }
int luatpt_setconsole(lua_State* l) int luatpt_setconsole(lua_State* l)
{ {
int consolestate; int consolestate;
consolestate = luaL_optint(l, 1, 0); consolestate = luaL_optint(l, 1, -1);
if (consolestate) if (consolestate == -1)
{
lua_pushnumber(l, luacon_ci->Window != ui::Engine::Ref().GetWindow());
return 1;
}
else if (consolestate)
luacon_controller->ShowConsole(); luacon_controller->ShowConsole();
else else
luacon_controller->HideConsole(); luacon_controller->HideConsole();
@ -1542,11 +1550,13 @@ int luatpt_get_name(lua_State* l)
int luatpt_set_shortcuts(lua_State* l) int luatpt_set_shortcuts(lua_State* l)
{ {
int shortcut = luaL_optint(l, 1, 0); int shortcut = luaL_optint(l, 1, -1);
if (shortcut) if (shortcut == -1)
shortcuts = true; {
else lua_pushnumber(l, shortcuts);
shortcuts = false; return 1;
}
shortcuts = shortcut?true:false;
return 0; return 0;
} }
@ -1746,84 +1756,107 @@ int luatpt_message_box(lua_State* l)
} }
int luatpt_get_numOfParts(lua_State* l) int luatpt_get_numOfParts(lua_State* l)
{ {
lua_pushinteger(l, luacon_sim->parts_lastActiveIndex); lua_pushinteger(l, luacon_sim->parts_lastActiveIndex);
return 1; return 1;
} }
int luatpt_start_getPartIndex(lua_State* l) int luatpt_start_getPartIndex(lua_State* l)
{ {
getPartIndex_curIdx = -1; getPartIndex_curIdx = -1;
return 1; return 1;
} }
int luatpt_next_getPartIndex(lua_State* l) int luatpt_next_getPartIndex(lua_State* l)
{ {
while(1) while(1)
{ {
getPartIndex_curIdx++; getPartIndex_curIdx++;
if(getPartIndex_curIdx >= NPART) if(getPartIndex_curIdx >= NPART)
{ {
getPartIndex_curIdx = 0; getPartIndex_curIdx = 0;
lua_pushboolean(l, 0); lua_pushboolean(l, 0);
return 1; return 1;
} }
if(luacon_sim->parts[getPartIndex_curIdx].type) if(luacon_sim->parts[getPartIndex_curIdx].type)
break; break;
} }
lua_pushboolean(l, 1); lua_pushboolean(l, 1);
return 1; return 1;
} }
int luatpt_getPartIndex(lua_State* l) int luatpt_getPartIndex(lua_State* l)
{ {
if(getPartIndex_curIdx < 0) if(getPartIndex_curIdx < 0)
{ {
lua_pushinteger(l, 0); lua_pushinteger(l, 0);
return 1; return 1;
} }
lua_pushinteger(l, getPartIndex_curIdx); lua_pushinteger(l, getPartIndex_curIdx);
return 1; return 1;
} }
int luatpt_hud(lua_State* l) int luatpt_hud(lua_State* l)
{ {
int hudstate = luaL_optint(l, 1, 0); int hudstate = luaL_optint(l, 1, -1);
if (hudstate) if (hudstate == -1)
{
lua_pushinteger(l, luacon_controller->GetHudEnable());
return 1;
}
else if (hudstate)
luacon_controller->SetHudEnable(1); luacon_controller->SetHudEnable(1);
else else
luacon_controller->SetHudEnable(0); luacon_controller->SetHudEnable(0);
return 0; return 0;
} }
int luatpt_gravity(lua_State* l) int luatpt_gravity(lua_State* l)
{ {
int gravstate; int gravstate;
gravstate = luaL_optint(l, 1, 0); gravstate = luaL_optint(l, 1, -1);
if(gravstate) if (gravstate == -1)
luacon_sim->grav->start_grav_async(); {
else lua_pushinteger(l, luacon_sim->grav->ngrav_enable);
luacon_sim->grav->stop_grav_async(); return 1;
return 0; }
else if(gravstate)
luacon_sim->grav->start_grav_async();
else
luacon_sim->grav->stop_grav_async();
luacon_model->UpdateQuickOptions();
return 0;
} }
int luatpt_airheat(lua_State* l) int luatpt_airheat(lua_State* l)
{ {
int aheatstate; int aheatstate;
aheatstate = luaL_optint(l, 1, 0); aheatstate = luaL_optint(l, 1, -1);
luacon_sim->aheat_enable = (aheatstate==0?0:1); if (aheatstate == -1)
return 0; {
lua_pushinteger(l, luacon_sim->aheat_enable);
return 1;
}
luacon_sim->aheat_enable = (aheatstate==0?0:1);
luacon_model->UpdateQuickOptions();
return 0;
} }
int luatpt_active_menu(lua_State* l) int luatpt_active_menu(lua_State* l)
{ {
int menuid; int menuid;
menuid = luaL_optint(l, 1, -1); menuid = luaL_optint(l, 1, -1);
if (menuid < SC_TOTAL && menuid >= 0) if (menuid == -1)
luacon_model->SetActiveMenu(luacon_model->GetMenuList()[menuid]); {
else lua_pushinteger(l, luacon_model->GetActiveMenu());
return luaL_error(l, "Invalid menu"); return 1;
return 0; }
if (menuid >= 0 && menuid < SC_TOTAL)
luacon_controller->SetActiveMenu(menuid);
else
return luaL_error(l, "Invalid menu");
return 0;
} }
int luatpt_decorations_enable(lua_State* l) int luatpt_decorations_enable(lua_State* l)
{ {
int decostate; int decostate;
decostate = luaL_optint(l, 1, 0); decostate = luaL_optint(l, 1, 0);
luacon_model->SetDecoration(decostate==0?false:true); luacon_model->SetDecoration(decostate==0?false:true);
luacon_model->UpdateQuickOptions();
return 0; return 0;
} }
@ -1940,7 +1973,7 @@ int luatpt_getscript(lua_State* l)
luacommand = new char[strlen(filename)+20]; luacommand = new char[strlen(filename)+20];
sprintf(luacommand,"dofile(\"%s\")",filename); sprintf(luacommand,"dofile(\"%s\")",filename);
luaL_dostring (l, luacommand); luaL_dostring (l, luacommand);
} }
fin: fin:
if(filedata) free(filedata); if(filedata) free(filedata);

View File

@ -2431,6 +2431,7 @@ std::string LuaScriptInterface::FormatCommand(std::string command)
} }
LuaScriptInterface::~LuaScriptInterface() { LuaScriptInterface::~LuaScriptInterface() {
lua_close(l);
delete legacy; delete legacy;
} }
#endif #endif

View File

@ -924,6 +924,11 @@ void GameController::SetHudEnable(bool hudState)
gameView->SetHudEnable(hudState); gameView->SetHudEnable(hudState);
} }
bool GameController::GetHudEnable()
{
return gameView->GetHudEnable();
}
void GameController::SetActiveColourPreset(int preset) void GameController::SetActiveColourPreset(int preset)
{ {
gameModel->SetActiveColourPreset(preset); gameModel->SetActiveColourPreset(preset);
@ -935,18 +940,15 @@ void GameController::SetColour(ui::Colour colour)
gameModel->SetPresetColour(colour); gameModel->SetPresetColour(colour);
} }
void GameController::SetActiveMenu(Menu * menu) void GameController::SetActiveMenu(int menuID)
{ {
gameModel->SetActiveMenu(menu); gameModel->SetActiveMenu(menuID);
vector<Menu*> menuList = gameModel->GetMenuList(); vector<Menu*> menuList = gameModel->GetMenuList();
bool set = false; bool set = false;
for(int i = 0; i < menuList.size(); i++) if(menuID == SC_DECO)
{ {
if(menuList[i]==menu && i == SC_DECO) gameModel->SetColourSelectorVisibility(true);
{ set = true;
gameModel->SetColourSelectorVisibility(true);
set = true;
}
} }
if(!set) if(!set)
gameModel->SetColourSelectorVisibility(false); gameModel->SetColourSelectorVisibility(false);

View File

@ -100,7 +100,8 @@ public:
void SetDecoration(); void SetDecoration();
void ShowGravityGrid(); void ShowGravityGrid();
void SetHudEnable(bool hudState); void SetHudEnable(bool hudState);
void SetActiveMenu(Menu * menu); bool GetHudEnable();
void SetActiveMenu(int menuID);
std::vector<Menu*> GetMenuList(); std::vector<Menu*> GetMenuList();
void SetActiveTool(int toolSelection, Tool * tool); void SetActiveTool(int toolSelection, Tool * tool);
void ActiveToolChanged(int toolSelection, Tool *tool); void ActiveToolChanged(int toolSelection, Tool *tool);

View File

@ -31,7 +31,7 @@ GameModel::GameModel():
colour(255, 0, 0, 255), colour(255, 0, 0, 255),
toolStrength(1.0f), toolStrength(1.0f),
activeColourPreset(-1), activeColourPreset(-1),
activeMenu(NULL), activeMenu(-1),
edgeMode(0) edgeMode(0)
{ {
sim = new Simulation(); sim = new Simulation();
@ -221,9 +221,9 @@ void GameModel::BuildQuickOptionMenu(GameController * controller)
void GameModel::BuildMenus() void GameModel::BuildMenus()
{ {
char lastMenu = 0; int lastMenu = -1;
if(activeMenu) if(activeMenu != -1)
lastMenu = activeMenu->GetIcon(); lastMenu = activeMenu;
std::string activeToolIdentifiers[3]; std::string activeToolIdentifiers[3];
if(regularToolset[0]) if(regularToolset[0])
@ -346,19 +346,15 @@ void GameModel::BuildMenus()
lastTool = activeTools[0]; lastTool = activeTools[0];
//Set default menu //Set default menu
activeMenu = menuList[SC_POWDERS]; activeMenu = SC_POWDERS;
if(lastMenu) if(lastMenu != -1) //What is this? ...
{ {
for(std::vector<Menu*>::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) activeMenu = lastMenu;
{
if((*iter)->GetIcon() == lastMenu)
activeMenu = *iter;
}
} }
if(activeMenu) if(activeMenu != -1)
toolList = activeMenu->GetToolList(); toolList = menuList[activeMenu]->GetToolList();
else else
toolList = std::vector<Tool*>(); toolList = std::vector<Tool*>();
@ -471,32 +467,26 @@ float GameModel::GetToolStrength()
return toolStrength; return toolStrength;
} }
void GameModel::SetActiveMenu(Menu * menu) void GameModel::SetActiveMenu(int menuID)
{ {
for(int i = 0; i < menuList.size(); i++) activeMenu = menuID;
{ toolList = menuList[menuID]->GetToolList();
if(menuList[i]==menu) notifyToolListChanged();
{
activeMenu = menu;
toolList = menu->GetToolList();
notifyToolListChanged();
if(menu == menuList[SC_DECO]) if(menuID == SC_DECO)
{ {
if(activeTools != decoToolset) if(activeTools != decoToolset)
{ {
activeTools = decoToolset; activeTools = decoToolset;
notifyActiveToolsChanged(); notifyActiveToolsChanged();
} }
} }
else else
{ {
if(activeTools != regularToolset) if(activeTools != regularToolset)
{ {
activeTools = regularToolset; activeTools = regularToolset;
notifyActiveToolsChanged(); notifyActiveToolsChanged();
}
}
} }
} }
} }
@ -511,7 +501,7 @@ vector<Tool*> GameModel::GetToolList()
return toolList; return toolList;
} }
Menu * GameModel::GetActiveMenu() int GameModel::GetActiveMenu()
{ {
return activeMenu; return activeMenu;
} }

View File

@ -53,7 +53,7 @@ private:
vector<Menu*> menuList; vector<Menu*> menuList;
vector<QuickOption*> quickOptions; vector<QuickOption*> quickOptions;
Menu * activeMenu; int activeMenu;
int currentBrush; int currentBrush;
vector<Brush *> brushList; vector<Brush *> brushList;
SaveInfo * currentSave; SaveInfo * currentSave;
@ -168,8 +168,8 @@ public:
void ClearSimulation(); void ClearSimulation();
vector<Menu*> GetMenuList(); vector<Menu*> GetMenuList();
vector<QuickOption*> GetQuickOptions(); vector<QuickOption*> GetQuickOptions();
void SetActiveMenu(Menu * menu); void SetActiveMenu(int menuID);
Menu * GetActiveMenu(); int GetActiveMenu();
void FrameStep(int frames); void FrameStep(int frames);
User GetUser(); User GetUser();
void SetUser(User user); void SetUser(User user);

View File

@ -189,7 +189,7 @@ GameView::GameView():
toolTipPresence(0), toolTipPresence(0),
currentSaveType(0), currentSaveType(0),
lastLogEntry(0.0f), lastLogEntry(0.0f),
lastMenu(NULL) lastMenu(-1)
{ {
int currentX = 1; int currentX = 1;
@ -403,7 +403,6 @@ GameView::GameView():
}; };
pauseButton = new ui::Button(ui::Point(Size.X-16, Size.Y-16), ui::Point(15, 15), "", "Pause/Resume the simulation"); //Pause pauseButton = new ui::Button(ui::Point(Size.X-16, Size.Y-16), ui::Point(15, 15), "", "Pause/Resume the simulation"); //Pause
pauseButton->SetIcon(IconPause); pauseButton->SetIcon(IconPause);
pauseButton->SetTogglable(true);
pauseButton->SetActionCallback(new PauseAction(this)); pauseButton->SetActionCallback(new PauseAction(this));
AddComponent(pauseButton); AddComponent(pauseButton);
@ -459,13 +458,13 @@ class GameView::MenuAction: public ui::ButtonAction
{ {
GameView * v; GameView * v;
public: public:
Menu * menu; int menuID;
bool needsClick; bool needsClick;
MenuAction(GameView * _v, Menu * menu_) MenuAction(GameView * _v, int menuID_)
{ {
v = _v; v = _v;
menu = menu_; menuID = menuID_;
if (v->c->GetMenuList()[SC_DECO] == menu) if (menuID == SC_DECO)
needsClick = true; needsClick = true;
else else
needsClick = false; needsClick = false;
@ -473,12 +472,12 @@ public:
void MouseEnterCallback(ui::Button * sender) void MouseEnterCallback(ui::Button * sender)
{ {
if(!needsClick && !ui::Engine::Ref().GetMouseButton()) if(!needsClick && !ui::Engine::Ref().GetMouseButton())
v->c->SetActiveMenu(menu); v->c->SetActiveMenu(menuID);
} }
void ActionCallback(ui::Button * sender) void ActionCallback(ui::Button * sender)
{ {
if (needsClick) if (needsClick)
v->c->SetActiveMenu(menu); v->c->SetActiveMenu(menuID);
else else
MouseEnterCallback(sender); MouseEnterCallback(sender);
} }
@ -566,15 +565,14 @@ void GameView::NotifyMenuListChanged(GameModel * sender)
} }
toolButtons.clear(); toolButtons.clear();
vector<Menu*> menuList = sender->GetMenuList(); vector<Menu*> menuList = sender->GetMenuList();
for(vector<Menu*>::reverse_iterator iter = menuList.rbegin(), end = menuList.rend(); iter != end; ++iter) for (int i = menuList.size()-1; i >= 0; i--)
{ {
std::string tempString = ""; std::string tempString = "";
Menu * item = *iter; tempString += menuList[i]->GetIcon();
tempString += item->GetIcon(); ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, currentY), ui::Point(15, 15), tempString, menuList[i]->GetDescription());
ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, currentY), ui::Point(15, 15), tempString, item->GetDescription());
tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2); tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2);
tempButton->SetTogglable(true); tempButton->SetTogglable(true);
tempButton->SetActionCallback(new MenuAction(this, item)); tempButton->SetActionCallback(new MenuAction(this, i));
currentY-=16; currentY-=16;
AddComponent(tempButton); AddComponent(tempButton);
menuButtons.push_back(tempButton); menuButtons.push_back(tempButton);
@ -591,6 +589,11 @@ void GameView::SetHudEnable(bool hudState)
showHud = hudState; showHud = hudState;
} }
bool GameView::GetHudEnable()
{
return showHud;
}
ui::Point GameView::GetMousePosition() ui::Point GameView::GetMousePosition()
{ {
return mousePosition; return mousePosition;
@ -643,7 +646,7 @@ void GameView::NotifyToolListChanged(GameModel * sender)
int totalColour; int totalColour;
for(int i = 0; i < menuButtons.size(); i++) for(int i = 0; i < menuButtons.size(); i++)
{ {
if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu()) if(((MenuAction*)menuButtons[i]->GetActionCallback())->menuID==sender->GetActiveMenu())
{ {
menuButtons[i]->SetToggleState(true); menuButtons[i]->SetToggleState(true);
} }
@ -698,7 +701,7 @@ void GameView::NotifyToolListChanged(GameModel * sender)
AddComponent(tempButton); AddComponent(tempButton);
toolButtons.push_back(tempButton); toolButtons.push_back(tempButton);
} }
if (sender->GetActiveMenu() != sender->GetMenuList()[SC_DECO]) if (sender->GetActiveMenu() != SC_DECO)
lastMenu = sender->GetActiveMenu(); lastMenu = sender->GetActiveMenu();
} }
@ -1364,7 +1367,7 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
{ {
c->SetDecoration(true); c->SetDecoration(true);
c->SetPaused(true); c->SetPaused(true);
c->SetActiveMenu(c->GetMenuList()[SC_DECO]); c->SetActiveMenu(SC_DECO);
} }
break; break;
case 'y': case 'y':

View File

@ -54,7 +54,7 @@ private:
std::string introTextMessage; std::string introTextMessage;
int toolIndex; int toolIndex;
int currentSaveType; int currentSaveType;
Menu * lastMenu; int lastMenu;
int toolTipPresence; int toolTipPresence;
std::string toolTip; std::string toolTip;
@ -130,6 +130,7 @@ public:
ui::Point GetMousePosition(); ui::Point GetMousePosition();
void SetSample(SimulationSample sample); void SetSample(SimulationSample sample);
void SetHudEnable(bool hudState); void SetHudEnable(bool hudState);
bool GetHudEnable();
bool CtrlBehaviour(){ return ctrlBehaviour; } bool CtrlBehaviour(){ return ctrlBehaviour; }
bool ShiftBehaviour(){ return shiftBehaviour; } bool ShiftBehaviour(){ return shiftBehaviour; }
void ExitPrompt(); void ExitPrompt();

View File

@ -153,6 +153,10 @@ void Button::OnMouseUnclick(int x, int y, unsigned int button)
{ {
if(isButtonDown) if(isButtonDown)
{ {
if(isTogglable)
{
toggle = !toggle;
}
isButtonDown = false; isButtonDown = false;
DoAction(); DoAction();
} }
@ -173,10 +177,6 @@ void Button::OnMouseClick(int x, int y, unsigned int button)
return; return;
if(button == 1) if(button == 1)
{ {
if(isTogglable)
{
toggle = !toggle;
}
isButtonDown = true; isButtonDown = true;
} }
else if(button == 3) else if(button == 3)