diff --git a/src/PowderToy.h b/src/PowderToy.h new file mode 100644 index 000000000..2e8cb3687 --- /dev/null +++ b/src/PowderToy.h @@ -0,0 +1,3 @@ +#pragma once + +void EngineProcess(); \ No newline at end of file diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 0a331f51d..9d1aa1517 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -284,188 +284,21 @@ std::map readArguments(int argc, char * argv[]) return arguments; } -int main(int argc, char * argv[]) +int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; +unsigned int lastTick = 0; +float fps = 0, delta = 1.0f, inputScale = 1.0f; +ui::Engine * engine = NULL; + +void EngineProcess() { - int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; - unsigned int lastTick = 0; - float fps = 0, delta = 1.0f, inputScale = 1.0f; - float currentWidth = XRES+BARSIZE, currentHeight = YRES+MENUSIZE; - - std::map arguments = readArguments(argc, argv); - - if(arguments["ddir"].length()) -#ifdef WIN - _chdir(arguments["ddir"].c_str()); -#else - chdir(arguments["ddir"].c_str()); -#endif - - int tempScale = 1; - bool tempFullscreen = false; - - tempScale = Client::Ref().GetPrefInteger("Scale", 1); - tempFullscreen = Client::Ref().GetPrefBool("Fullscreen", false); - - - if(arguments["kiosk"] == "true") - { - tempFullscreen = true; - Client::Ref().SetPref("Fullscreen", tempFullscreen); - } - - if(arguments["scale"].length()) - { - tempScale = format::StringToNumber(arguments["scale"]); - Client::Ref().SetPref("Scale", tempScale); - } - - std::string proxyString = ""; - if(arguments["proxy"].length()) - { - if(arguments["proxy"] == "false") - { - proxyString = ""; - Client::Ref().SetPref("Proxy", ""); - } - else - { - proxyString = (arguments["proxy"]); - Client::Ref().SetPref("Proxy", arguments["proxy"]); - } - } - else if(Client::Ref().GetPrefString("Proxy", "").length()) - { - proxyString = (Client::Ref().GetPrefString("Proxy", "")); - } - - Client::Ref().Initialise(proxyString); - - if(tempScale != 1 && tempScale != 2) - tempScale = 1; - - int sdlStatus = SDLOpen(); - sdl_scrn = SDLSetScreen(tempScale, tempFullscreen); -#ifdef OGLI - SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); - //glScaled(2.0f, 2.0f, 1.0f); -#endif -#if defined(OGLI) - int status = glewInit(); - if(status != GLEW_OK) - { - fprintf(stderr, "Initializing Glew: %d\n", status); - exit(-1); - } -#endif - ui::Engine::Ref().g = new Graphics(); - ui::Engine::Ref().Scale = scale; - inputScale = 1.0f/float(scale); - ui::Engine::Ref().Fullscreen = fullscreen; - - ui::Engine * engine = &ui::Engine::Ref(); - engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); - - GameController * gameController = new GameController(); - engine->ShowWindow(gameController->GetView()); - - if(arguments["open"].length()) - { -#ifdef DEBUG - std::cout << "Loading " << arguments["open"] << std::endl; -#endif - if(Client::Ref().FileExists(arguments["open"])) - { - try - { - std::vector gameSaveData = Client::Ref().ReadFile(arguments["open"]); - if(!gameSaveData.size()) - { - new ErrorMessage("Error", "Could not read file"); - } - else - { - SaveFile * newFile = new SaveFile(arguments["open"]); - GameSave * newSave = new GameSave(gameSaveData); - newFile->SetGameSave(newSave); - gameController->LoadSaveFile(newFile); - delete newFile; - } - - } - catch(std::exception & e) - { - new ErrorMessage("Error", "Could not open save file:\n"+std::string(e.what())) ; - } - } - else - { - new ErrorMessage("Error", "Could not open file"); - } - } - - if(arguments["ptsave"].length()) - { - engine->g->fillrect((engine->GetWidth()/2)-101, (engine->GetHeight()/2)-26, 202, 52, 0, 0, 0, 210); - engine->g->drawrect((engine->GetWidth()/2)-100, (engine->GetHeight()/2)-25, 200, 50, 255, 255, 255, 180); - engine->g->drawtext((engine->GetWidth()/2)-(Graphics::textwidth("Loading save...")/2), (engine->GetHeight()/2)-5, "Loading save...", style::Colour::InformationTitle.Red, style::Colour::InformationTitle.Green, style::Colour::InformationTitle.Blue, 255); - -#ifdef OGLI - blit(); -#else - if(engine->Scale==2) - blit2(engine->g->vid, engine->Scale); - else - blit(engine->g->vid); -#endif - std::string ptsaveArg = arguments["ptsave"]; - try - { - if(!ptsaveArg.find("ptsave:")) - { - std::string saveIdPart = ""; - int saveId; - int hashPos = ptsaveArg.find('#'); - if(hashPos != std::string::npos) - { - saveIdPart = ptsaveArg.substr(7, hashPos-7); - } - else - { - saveIdPart = ptsaveArg.substr(7); - } - if(saveIdPart.length()) - { -#ifdef DEBUG - std::cout << "Got Ptsave: id: " << saveIdPart << std::endl; -#endif - saveId = format::StringToNumber(saveIdPart); - if(!saveId) - throw std::runtime_error("Invalid Save ID"); - - SaveInfo * newSave = Client::Ref().GetSave(saveId, 0); - GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0)); - newSave->SetGameSave(newGameSave); - if(!newSave) - throw std::runtime_error("Could not load save"); - - gameController->LoadSave(newSave); - delete newSave; - } - else - { - throw std::runtime_error("No Save ID"); - } - } - } - catch (std::exception & e) - { - new ErrorMessage("Error", "Invalid save link"); - } - } - SDL_Event event; while(engine->Running()) { + if(engine->Broken()) + { + engine->Break(); + break; + } event.type = 0; while (SDL_PollEvent(&event)) { @@ -581,6 +414,186 @@ int main(int argc, char * argv[]) } engine->SetFps(fps); } +} + +int main(int argc, char * argv[]) +{ + float currentWidth = XRES+BARSIZE, currentHeight = YRES+MENUSIZE; + + std::map arguments = readArguments(argc, argv); + + if(arguments["ddir"].length()) +#ifdef WIN + _chdir(arguments["ddir"].c_str()); +#else + chdir(arguments["ddir"].c_str()); +#endif + + int tempScale = 1; + bool tempFullscreen = false; + + tempScale = Client::Ref().GetPrefInteger("Scale", 1); + tempFullscreen = Client::Ref().GetPrefBool("Fullscreen", false); + + + if(arguments["kiosk"] == "true") + { + tempFullscreen = true; + Client::Ref().SetPref("Fullscreen", tempFullscreen); + } + + if(arguments["scale"].length()) + { + tempScale = format::StringToNumber(arguments["scale"]); + Client::Ref().SetPref("Scale", tempScale); + } + + std::string proxyString = ""; + if(arguments["proxy"].length()) + { + if(arguments["proxy"] == "false") + { + proxyString = ""; + Client::Ref().SetPref("Proxy", ""); + } + else + { + proxyString = (arguments["proxy"]); + Client::Ref().SetPref("Proxy", arguments["proxy"]); + } + } + else if(Client::Ref().GetPrefString("Proxy", "").length()) + { + proxyString = (Client::Ref().GetPrefString("Proxy", "")); + } + + Client::Ref().Initialise(proxyString); + + if(tempScale != 1 && tempScale != 2) + tempScale = 1; + + int sdlStatus = SDLOpen(); + sdl_scrn = SDLSetScreen(tempScale, tempFullscreen); +#ifdef OGLI + SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); + //glScaled(2.0f, 2.0f, 1.0f); +#endif +#if defined(OGLI) + int status = glewInit(); + if(status != GLEW_OK) + { + fprintf(stderr, "Initializing Glew: %d\n", status); + exit(-1); + } +#endif + ui::Engine::Ref().g = new Graphics(); + ui::Engine::Ref().Scale = scale; + inputScale = 1.0f/float(scale); + ui::Engine::Ref().Fullscreen = fullscreen; + + engine = &ui::Engine::Ref(); + engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); + + GameController * gameController = new GameController(); + engine->ShowWindow(gameController->GetView()); + + if(arguments["open"].length()) + { +#ifdef DEBUG + std::cout << "Loading " << arguments["open"] << std::endl; +#endif + if(Client::Ref().FileExists(arguments["open"])) + { + try + { + std::vector gameSaveData = Client::Ref().ReadFile(arguments["open"]); + if(!gameSaveData.size()) + { + new ErrorMessage("Error", "Could not read file"); + } + else + { + SaveFile * newFile = new SaveFile(arguments["open"]); + GameSave * newSave = new GameSave(gameSaveData); + newFile->SetGameSave(newSave); + gameController->LoadSaveFile(newFile); + delete newFile; + } + + } + catch(std::exception & e) + { + new ErrorMessage("Error", "Could not open save file:\n"+std::string(e.what())) ; + } + } + else + { + new ErrorMessage("Error", "Could not open file"); + } + } + + if(arguments["ptsave"].length()) + { + engine->g->fillrect((engine->GetWidth()/2)-101, (engine->GetHeight()/2)-26, 202, 52, 0, 0, 0, 210); + engine->g->drawrect((engine->GetWidth()/2)-100, (engine->GetHeight()/2)-25, 200, 50, 255, 255, 255, 180); + engine->g->drawtext((engine->GetWidth()/2)-(Graphics::textwidth("Loading save...")/2), (engine->GetHeight()/2)-5, "Loading save...", style::Colour::InformationTitle.Red, style::Colour::InformationTitle.Green, style::Colour::InformationTitle.Blue, 255); + +#ifdef OGLI + blit(); +#else + if(engine->Scale==2) + blit2(engine->g->vid, engine->Scale); + else + blit(engine->g->vid); +#endif + std::string ptsaveArg = arguments["ptsave"]; + try + { + if(!ptsaveArg.find("ptsave:")) + { + std::string saveIdPart = ""; + int saveId; + int hashPos = ptsaveArg.find('#'); + if(hashPos != std::string::npos) + { + saveIdPart = ptsaveArg.substr(7, hashPos-7); + } + else + { + saveIdPart = ptsaveArg.substr(7); + } + if(saveIdPart.length()) + { +#ifdef DEBUG + std::cout << "Got Ptsave: id: " << saveIdPart << std::endl; +#endif + saveId = format::StringToNumber(saveIdPart); + if(!saveId) + throw std::runtime_error("Invalid Save ID"); + + SaveInfo * newSave = Client::Ref().GetSave(saveId, 0); + GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0)); + newSave->SetGameSave(newGameSave); + if(!newSave) + throw std::runtime_error("Could not load save"); + + gameController->LoadSave(newSave); + delete newSave; + } + else + { + throw std::runtime_error("No Save ID"); + } + } + } + catch (std::exception & e) + { + new ErrorMessage("Error", "Invalid save link"); + } + } + + EngineProcess(); + ui::Engine::Ref().CloseWindow(); delete gameController; delete ui::Engine::Ref().g; diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 14b2bf1ec..c68c72c77 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -12,9 +12,18 @@ #include "TPTScriptInterface.h" #include "dialogues/ErrorMessage.h" #include "dialogues/InformationMessage.h" +#include "dialogues/TextPrompt.h" +#include "dialogues/ConfirmPrompt.h" #include "simulation/Simulation.h" #include "game/GameModel.h" #include "LuaScriptHelper.h" +#include "client/HTTP.h" + + #ifdef WIN +#include +#else +#include +#endif LuaScriptInterface::LuaScriptInterface(GameModel * m): CommandInterface(m), @@ -1000,7 +1009,7 @@ int luatpt_graphics_func(lua_State *l) int luatpt_error(lua_State* l) { std::string errorMessage = std::string(luaL_optstring(l, 1, "Error text")); - new ErrorMessage("Error", errorMessage); + ErrorMessage::Blocking("Error", errorMessage); return 0; } int luatpt_drawtext(lua_State* l) @@ -1841,30 +1850,16 @@ int luatpt_unregister_mouseclick(lua_State* l) } int luatpt_input(lua_State* l) { - /*char *prompt, *title, *result, *shadow, *text; - title = mystrdup((char*)luaL_optstring(l, 1, "Title")); - prompt = mystrdup((char*)luaL_optstring(l, 2, "Enter some text:")); - text = mystrdup((char*)luaL_optstring(l, 3, "")); - shadow = mystrdup((char*)luaL_optstring(l, 4, "")); + std::string prompt, title, result, shadow, text; + title = std::string(luaL_optstring(l, 1, "Title")); + prompt = std::string(luaL_optstring(l, 2, "Enter some text:")); + text = std::string(luaL_optstring(l, 3, "")); + shadow = std::string(luaL_optstring(l, 4, "")); - if (vid_buf!=NULL) - { - result = input_ui(vid_buf, title, prompt, text, shadow); - lua_pushstring(l, result); - free(result); - free(title); - free(prompt); - free(text); - free(shadow); - return 1; - } - free(title); - free(prompt); - free(text); - free(shadow); - return luaL_error(l, "Screen buffer does not exist");*/ - //TODO IMPLEMENT - return 0; + result = TextPrompt::Blocking(title, prompt, text, shadow, false); + + lua_pushstring(l, result.c_str()); + return 1; } int luatpt_message_box(lua_State* l) { @@ -1989,22 +1984,24 @@ int luatpt_setfpscap(lua_State* l) } int luatpt_getscript(lua_State* l) { - /*char *fileid = NULL, *filedata = NULL, *fileuri = NULL, *fileauthor = NULL, *filename = NULL, *lastError = NULL, *luacommand = NULL; + char *filedata = NULL, *fileuri = NULL, *filename = NULL, *lastError = NULL, *luacommand = NULL; + std::string fileauthor = "", fileid = ""; int len, ret,run_script; FILE * outputfile; - fileauthor = mystrdup(luaL_optstring(l, 1, "")); - fileid = mystrdup(luaL_optstring(l, 2, "")); + fileauthor = std::string(luaL_optstring(l, 1, "")); + fileid = std::string(luaL_optstring(l, 2, "")); run_script = luaL_optint(l, 3, 0); - if(!fileauthor || !fileid || strlen(fileauthor)<1 || strlen(fileid)<1) + if(!fileauthor.length() || !fileid.length()) goto fin; - if(!confirm_ui(vid_buf, "Do you want to install script?", fileid, "Install")) + if(!ConfirmPrompt::Blocking("Do you want to install script?", fileid, "Install")) goto fin; - fileuri = malloc(strlen(SCRIPTSERVER)+strlen(fileauthor)+strlen(fileid)+44); - sprintf(fileuri, "http://" SCRIPTSERVER "/GetScript.api?Author=%s&Filename=%s", fileauthor, fileid); + fileuri = new char[strlen(SCRIPTSERVER)+fileauthor.length()+fileid.length()+44]; + sprintf(fileuri, "http://" SCRIPTSERVER "/GetScript.api?Author=%s&Filename=%s", fileauthor.c_str(), fileid.c_str()); - filedata = http_auth_get(fileuri, svf_user_id, NULL, svf_session_id, &ret, &len); + //filedata = http_auth_get(fileuri, svf_user_id, NULL, svf_session_id, &ret, &len); + filedata = http_auth_get(fileuri, NULL, NULL, NULL, &ret, &len); if(len <= 0 || !filedata) { @@ -2017,8 +2014,8 @@ int luatpt_getscript(lua_State* l) goto fin; } - filename = (char*)malloc(strlen(fileauthor)+strlen(fileid)+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6); - sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor, fileid); + filename = new char[fileauthor.length()+fileid.length()+strlen(PATH_SEP)+strlen(LOCAL_LUA_DIR)+6]; + sprintf(filename, LOCAL_LUA_DIR PATH_SEP "%s_%s.lua", fileauthor.c_str(), fileid.c_str()); #ifdef WIN _mkdir(LOCAL_LUA_DIR); @@ -2031,7 +2028,7 @@ int luatpt_getscript(lua_State* l) { fclose(outputfile); outputfile = NULL; - if(confirm_ui(vid_buf, "File already exists, overwrite?", filename, "Overwrite")) + if(ConfirmPrompt::Blocking("File already exists, overwrite?", filename, "Overwrite")) { outputfile = fopen(filename, "w"); } @@ -2057,24 +2054,20 @@ int luatpt_getscript(lua_State* l) outputfile = NULL; if(run_script) { - luacommand = malloc(strlen(filename)+20); + luacommand = new char[strlen(filename)+20]; sprintf(luacommand,"dofile(\"%s\")",filename); luaL_dostring (l, luacommand); } fin: - if(fileid) free(fileid); if(filedata) free(filedata); - if(fileuri) free(fileuri); - if(fileauthor) free(fileauthor); - if(filename) free(filename); - if(luacommand) free(luacommand); + if(fileuri) delete[] fileuri; + if(filename) delete[] filename; + if(luacommand) delete[] luacommand; luacommand = NULL; if(lastError) return luaL_error(l, lastError); - return 0;*/ - //TODO IMPLEMENT - return luaL_error(l, "Not implemented"); + return 0; } int luatpt_setwindowsize(lua_State* l) diff --git a/src/dialogues/ConfirmPrompt.cpp b/src/dialogues/ConfirmPrompt.cpp index 459e1c02e..312d98c98 100644 --- a/src/dialogues/ConfirmPrompt.cpp +++ b/src/dialogues/ConfirmPrompt.cpp @@ -9,6 +9,7 @@ #include "Style.h" #include "interface/Label.h" #include "interface/Button.h" +#include "PowderToy.h" ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDialogueCallback * callback_): ui::Window(ui::Point(-1, -1), ui::Point(250, 50)), @@ -40,7 +41,8 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial void ActionCallback(ui::Button * sender) { ui::Engine::Ref().CloseWindow(); - prompt->callback->ConfirmCallback(result); + if(prompt->callback) + prompt->callback->ConfirmCallback(result); prompt->SelfDestruct(); } }; @@ -65,6 +67,83 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial ui::Engine::Ref().ShowWindow(this); } +ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, std::string buttonText, ConfirmDialogueCallback * callback_): + ui::Window(ui::Point(-1, -1), ui::Point(250, 50)), + callback(callback_) +{ + int width, height; + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), title); + titleLabel->SetTextColour(style::Colour::WarningTitle); + titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(titleLabel); + + + ui::Label * messageLabel = new ui::Label(ui::Point(4, 25), ui::Point(Size.X-8, -1), message); + messageLabel->SetMultiline(true); + messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; + AddComponent(messageLabel); + + Size.Y += messageLabel->Size.Y+12; + Position.Y = (ui::Engine::Ref().GetHeight()-Size.Y)/2; + + class CloseAction: public ui::ButtonAction + { + public: + ConfirmPrompt * prompt; + DialogueResult result; + CloseAction(ConfirmPrompt * prompt_, DialogueResult result_) { prompt = prompt_; result = result_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->callback) + prompt->callback->ConfirmCallback(result); + prompt->SelfDestruct(); + } + }; + + + ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X-75, 16), "Cancel"); + cancelButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + cancelButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + cancelButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); + cancelButton->SetActionCallback(new CloseAction(this, ResultCancel)); + AddComponent(cancelButton); + SetCancelButton(cancelButton); + + ui::Button * okayButton = new ui::Button(ui::Point(Size.X-76, Size.Y-16), ui::Point(76, 16), buttonText); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.TextInactive = style::Colour::WarningTitle; + okayButton->SetActionCallback(new CloseAction(this, ResultOkay)); + AddComponent(okayButton); + SetOkayButton(okayButton); + + ui::Engine::Ref().ShowWindow(this); +} + +bool ConfirmPrompt::Blocking(std::string title, std::string message, std::string buttonText) +{ + class BlockingPromptCallback: public ConfirmDialogueCallback { + public: + bool & outputResult; + BlockingPromptCallback(bool & output): outputResult(output) {} + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + outputResult = true; + else + outputResult = false; + ui::Engine::Ref().Break(); + } + virtual ~BlockingPromptCallback() { } + }; + bool result; + new ConfirmPrompt(title, message, buttonText, new BlockingPromptCallback(result)); + EngineProcess(); + return result; +} + void ConfirmPrompt::OnDraw() { Graphics * g = ui::Engine::Ref().g; diff --git a/src/dialogues/ConfirmPrompt.h b/src/dialogues/ConfirmPrompt.h index f86a38636..b866338e9 100644 --- a/src/dialogues/ConfirmPrompt.h +++ b/src/dialogues/ConfirmPrompt.h @@ -14,7 +14,9 @@ class ConfirmDialogueCallback; class ConfirmPrompt: public ui::Window { public: enum DialogueResult { ResultCancel, ResultOkay }; - ConfirmPrompt(std::string title, std::string message, ConfirmDialogueCallback * callback_); + ConfirmPrompt(std::string title, std::string message, ConfirmDialogueCallback * callback_ = NULL); + ConfirmPrompt(std::string title, std::string message, std::string buttonText, ConfirmDialogueCallback * callback_ = NULL); + static bool Blocking(std::string title, std::string message, std::string buttonText = "Confirm"); virtual void OnDraw(); virtual ~ConfirmPrompt(); ConfirmDialogueCallback * callback; diff --git a/src/dialogues/ErrorMessage.cpp b/src/dialogues/ErrorMessage.cpp index 9bff8fbb6..083e5417e 100644 --- a/src/dialogues/ErrorMessage.cpp +++ b/src/dialogues/ErrorMessage.cpp @@ -9,9 +9,11 @@ #include "ErrorMessage.h" #include "interface/Button.h" #include "interface/Label.h" +#include "PowderToy.h" -ErrorMessage::ErrorMessage(std::string title, std::string message): - ui::Window(ui::Point(-1, -1), ui::Point(200, 75)) +ErrorMessage::ErrorMessage(std::string title, std::string message, ErrorMessageCallback * callback_): + ui::Window(ui::Point(-1, -1), ui::Point(200, 75)), + callback(callback_) { ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 16), title); titleLabel->SetTextColour(style::Colour::ErrorTitle); @@ -32,7 +34,9 @@ ErrorMessage::ErrorMessage(std::string title, std::string message): void ActionCallback(ui::Button * sender) { ui::Engine::Ref().CloseWindow(); - message->SelfDestruct(); //TODO: Fix component disposal + if(message->callback) + message->callback->DismissCallback(); + message->SelfDestruct(); } }; @@ -48,6 +52,20 @@ ErrorMessage::ErrorMessage(std::string title, std::string message): ui::Engine::Ref().ShowWindow(this); } +void ErrorMessage::Blocking(std::string title, std::string message) +{ + class BlockingDismissCallback: public ErrorMessageCallback { + public: + BlockingDismissCallback() {} + virtual void DismissCallback() { + ui::Engine::Ref().Break(); + } + virtual ~BlockingDismissCallback() { } + }; + new ErrorMessage(title, message, new BlockingDismissCallback()); + EngineProcess(); +} + void ErrorMessage::OnDraw() { Graphics * g = ui::Engine::Ref().g; @@ -57,5 +75,7 @@ void ErrorMessage::OnDraw() } ErrorMessage::~ErrorMessage() { + if(callback) + delete callback; } diff --git a/src/dialogues/ErrorMessage.h b/src/dialogues/ErrorMessage.h index f8575bde7..769127e27 100644 --- a/src/dialogues/ErrorMessage.h +++ b/src/dialogues/ErrorMessage.h @@ -10,11 +10,21 @@ #include "interface/Window.h" +class ErrorMessageCallback; class ErrorMessage: public ui::Window { + ErrorMessageCallback * callback; public: - ErrorMessage(std::string title, std::string message); + ErrorMessage(std::string title, std::string message, ErrorMessageCallback * callback_ = NULL); + static void Blocking(std::string title, std::string message); virtual void OnDraw(); virtual ~ErrorMessage(); }; +class ErrorMessageCallback +{ + public: + virtual void DismissCallback() {} + virtual ~ErrorMessageCallback() {} +}; + #endif /* ERRORMESSAGE_H_ */ diff --git a/src/dialogues/TextPrompt.cpp b/src/dialogues/TextPrompt.cpp index d5b6c9567..3a2a39f46 100644 --- a/src/dialogues/TextPrompt.cpp +++ b/src/dialogues/TextPrompt.cpp @@ -9,6 +9,7 @@ #include "interface/Label.h" #include "interface/Button.h" #include "Style.h" +#include "PowderToy.h" class CloseAction: public ui::ButtonAction { @@ -25,7 +26,7 @@ public: } }; -TextPrompt::TextPrompt(std::string title, std::string message, bool multiline, TextDialogueCallback * callback_): +TextPrompt::TextPrompt(std::string title, std::string message, std::string text, std::string placeholder, bool multiline, TextDialogueCallback * callback_): ui::Window(ui::Point(-1, -1), ui::Point(200, 80)), callback(callback_) { @@ -39,7 +40,7 @@ TextPrompt::TextPrompt(std::string title, std::string message, bool multiline, T messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; AddComponent(messageLabel); - textField = new ui::Textbox(ui::Point(4, 45 ), ui::Point(Size.X-8, 16), "", "Reason"); + textField = new ui::Textbox(ui::Point(4, 45 ), ui::Point(Size.X-8, 16), text, placeholder); if(multiline) { textField->SetMultiline(true); @@ -73,6 +74,29 @@ TextPrompt::TextPrompt(std::string title, std::string message, bool multiline, T ui::Engine::Ref().ShowWindow(this); } +std::string TextPrompt::Blocking(std::string title, std::string message, std::string text, std::string placeholder, bool multiline) +{ + std::string returnString = ""; + + class BlockingTextCallback: public TextDialogueCallback { + std::string & outputString; + public: + BlockingTextCallback(std::string & output) : outputString(output) {} + virtual void TextCallback(TextPrompt::DialogueResult result, std::string resultText) { + if(result == ResultOkay) + outputString = resultText; + else + outputString = ""; + ui::Engine::Ref().Break(); + } + virtual ~BlockingTextCallback() { } + }; + new TextPrompt(title, message, text, placeholder, multiline, new BlockingTextCallback(returnString)); + EngineProcess(); + + return returnString; +} + void TextPrompt::OnDraw() { Graphics * g = ui::Engine::Ref().g; diff --git a/src/dialogues/TextPrompt.h b/src/dialogues/TextPrompt.h index 8016d565d..1c4a672cf 100644 --- a/src/dialogues/TextPrompt.h +++ b/src/dialogues/TextPrompt.h @@ -5,8 +5,8 @@ * Author: Simon */ -#ifndef CONFIRMPROMPT_H_ -#define CONFIRMPROMPT_H_ +#ifndef TEXTPROMPT_H_ +#define TEXTPROMPT_H_ #include "interface/Window.h" #include "interface/Textbox.h" @@ -18,7 +18,8 @@ protected: public: friend class CloseAction; enum DialogueResult { ResultCancel, ResultOkay }; - TextPrompt(std::string title, std::string message, bool multiline, TextDialogueCallback * callback_); + TextPrompt(std::string title, std::string message, std::string text, std::string placeholder, bool multiline, TextDialogueCallback * callback_); + static std::string Blocking(std::string title, std::string message, std::string text, std::string placeholder, bool multiline); virtual void OnDraw(); virtual ~TextPrompt(); TextDialogueCallback * callback; @@ -31,4 +32,4 @@ class TextDialogueCallback virtual ~TextDialogueCallback() {} }; -#endif /* CONFIRMPROMPT_H_ */ +#endif /* TEXTPROMPT_H_ */ diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp index b0052012a..31311fe63 100644 --- a/src/interface/Engine.cpp +++ b/src/interface/Engine.cpp @@ -24,7 +24,8 @@ Engine::Engine(): windowTargetPosition(0, 0), FrameIndex(0), Fullscreen(false), - Scale(1) + Scale(1), + break_(false) { } @@ -49,6 +50,11 @@ void Engine::Begin(int width, int height) height_ = height; } +void Engine::Break() +{ + break_ = !break_; +} + void Engine::Exit() { running_ = false; diff --git a/src/interface/Engine.h b/src/interface/Engine.h index cd1c341e5..b28ba8448 100644 --- a/src/interface/Engine.h +++ b/src/interface/Engine.h @@ -35,7 +35,9 @@ namespace ui void Begin(int width, int height); inline bool Running() { return running_; } + inline bool Broken() { return break_; } void Exit(); + void Break(); void SetFullscreen(bool fullscreen) { Fullscreen = fullscreen; } inline bool GetFullscreen() { return Fullscreen; } @@ -76,6 +78,7 @@ namespace ui float windowOpenState; bool running_; + bool break_; int mousex_; int mousey_; diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index 920284869..762cf8034 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -113,7 +113,7 @@ PreviewView::PreviewView(): ReportAction(PreviewView * v_){ v = v_; } virtual void ActionCallback(ui::Button * sender) { - new TextPrompt("Report Save", "Reason for reporting", true, new ReportPromptCallback(v)); + new TextPrompt("Report Save", "Reason for reporting", "", "[reason]", true, new ReportPromptCallback(v)); } }; reportButton = new ui::Button(ui::Point(100, Size.Y-19), ui::Point(51, 19), "Report");