diff options
| -rw-r--r-- | src/PowderToy.h | 3 | ||||
| -rw-r--r-- | src/PowderToySDL.cpp | 257 | ||||
| -rw-r--r-- | src/cat/LuaScriptInterface.cpp | 81 | ||||
| -rw-r--r-- | src/dialogues/ConfirmPrompt.cpp | 81 | ||||
| -rw-r--r-- | src/dialogues/ConfirmPrompt.h | 4 | ||||
| -rw-r--r-- | src/dialogues/ErrorMessage.cpp | 26 | ||||
| -rw-r--r-- | src/dialogues/ErrorMessage.h | 12 | ||||
| -rw-r--r-- | src/dialogues/TextPrompt.cpp | 28 | ||||
| -rw-r--r-- | src/dialogues/TextPrompt.h | 9 | ||||
| -rw-r--r-- | src/interface/Engine.cpp | 8 | ||||
| -rw-r--r-- | src/interface/Engine.h | 3 | ||||
| -rw-r--r-- | src/preview/PreviewView.cpp | 2 |
12 files changed, 334 insertions, 180 deletions
diff --git a/src/PowderToy.h b/src/PowderToy.h new file mode 100644 index 0000000..2e8cb36 --- /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 0a331f5..9d1aa15 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -284,11 +284,140 @@ std::map<std::string, std::string> readArguments(int argc, char * argv[]) return arguments; } +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() +{ + SDL_Event event; + while(engine->Running()) + { + if(engine->Broken()) + { + engine->Break(); + break; + } + event.type = 0; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + engine->Exit(); + break; + case SDL_KEYDOWN: + engine->onKeyPress(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_LSHIFT, event.key.keysym.mod&KEY_MOD_LCONTROL, event.key.keysym.mod&KEY_MOD_LALT); + break; + case SDL_KEYUP: + engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_LSHIFT, event.key.keysym.mod&KEY_MOD_LCONTROL, event.key.keysym.mod&KEY_MOD_LALT); + break; + case SDL_MOUSEMOTION: + engine->onMouseMove(event.motion.x*inputScale, event.motion.y*inputScale); + break; + case SDL_MOUSEBUTTONDOWN: + if(event.button.button == SDL_BUTTON_WHEELUP) + { + engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, 1); + } + else if (event.button.button == SDL_BUTTON_WHEELDOWN) + { + engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, -1); + } + else + { + engine->onMouseClick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); + } + break; + case SDL_MOUSEBUTTONUP: + if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) + engine->onMouseUnclick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); + break; +#ifdef OGLI + case SDL_VIDEORESIZE: + float ratio = float(XRES+BARSIZE) / float(YRES+MENUSIZE); + float width = event.resize.w; + float height = width/ratio; + + sdl_scrn = SDL_SetVideoMode(event.resize.w, height, 32, SDL_OPENGL | SDL_RESIZABLE); + + glViewport(0, 0, width, height); + engine->g->Reset(); + //glScaled(width/currentWidth, height/currentHeight, 1.0f); + + currentWidth = width; + currentHeight = height; + inputScale = float(XRES+BARSIZE)/currentWidth; + + glLineWidth(currentWidth/float(XRES+BARSIZE)); + if(sdl_scrn == NULL) + { + std::cerr << "Oh bugger" << std::endl; + } + break; +#endif + } + event.type = 0; //Clear last event + } + + engine->Tick(); + engine->Draw(); + + if(SDL_GetTicks()-lastTick>250) + { + //Run client tick every second + lastTick = SDL_GetTicks(); + Client::Ref().Tick(); + } + + if(scale != engine->Scale || fullscreen != engine->Fullscreen) + { + sdl_scrn = SDLSetScreen(engine->Scale, engine->Fullscreen); + inputScale = 1.0f/float(scale); + } + +#ifdef OGLI + blit(); +#else + if(engine->Scale==2) + blit2(engine->g->vid, engine->Scale); + else + blit(engine->g->vid); +#endif + + currentFrame++; + currentTime = SDL_GetTicks(); + elapsedTime = currentTime - lastTime; + if(ui::Engine::Ref().FpsLimit > 2.0f && (currentFrame>2 || elapsedTime > 1000*2/ui::Engine::Ref().FpsLimit) && elapsedTime && currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit) + { + while (currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit) + { + SDL_Delay(1); + currentTime = SDL_GetTicks(); + elapsedTime = currentTime-lastTime; + } + } + if(elapsedTime>=1000) + { + fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f; + currentFrame = 0; + lastTime = currentTime; + if(ui::Engine::Ref().FpsLimit > 2.0f) + { + delta = ui::Engine::Ref().FpsLimit/fps; + } + else + { + delta = 1.0f; + } + } + engine->SetFps(fps); + } +} + 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; float currentWidth = XRES+BARSIZE, currentHeight = YRES+MENUSIZE; std::map<std::string, std::string> arguments = readArguments(argc, argv); @@ -362,7 +491,7 @@ int main(int argc, char * argv[]) inputScale = 1.0f/float(scale); ui::Engine::Ref().Fullscreen = fullscreen; - ui::Engine * engine = &ui::Engine::Ref(); + engine = &ui::Engine::Ref(); engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); GameController * gameController = new GameController(); @@ -463,124 +592,8 @@ int main(int argc, char * argv[]) } } - SDL_Event event; - while(engine->Running()) - { - event.type = 0; - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - engine->Exit(); - break; - case SDL_KEYDOWN: - engine->onKeyPress(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_LSHIFT, event.key.keysym.mod&KEY_MOD_LCONTROL, event.key.keysym.mod&KEY_MOD_LALT); - break; - case SDL_KEYUP: - engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_LSHIFT, event.key.keysym.mod&KEY_MOD_LCONTROL, event.key.keysym.mod&KEY_MOD_LALT); - break; - case SDL_MOUSEMOTION: - engine->onMouseMove(event.motion.x*inputScale, event.motion.y*inputScale); - break; - case SDL_MOUSEBUTTONDOWN: - if(event.button.button == SDL_BUTTON_WHEELUP) - { - engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, 1); - } - else if (event.button.button == SDL_BUTTON_WHEELDOWN) - { - engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, -1); - } - else - { - engine->onMouseClick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); - } - break; - case SDL_MOUSEBUTTONUP: - if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) - engine->onMouseUnclick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); - break; -#ifdef OGLI - case SDL_VIDEORESIZE: - float ratio = float(XRES+BARSIZE) / float(YRES+MENUSIZE); - float width = event.resize.w; - float height = width/ratio; - - sdl_scrn = SDL_SetVideoMode(event.resize.w, height, 32, SDL_OPENGL | SDL_RESIZABLE); - - glViewport(0, 0, width, height); - engine->g->Reset(); - //glScaled(width/currentWidth, height/currentHeight, 1.0f); - - currentWidth = width; - currentHeight = height; - inputScale = float(XRES+BARSIZE)/currentWidth; - - glLineWidth(currentWidth/float(XRES+BARSIZE)); - if(sdl_scrn == NULL) - { - std::cerr << "Oh bugger" << std::endl; - } - break; -#endif - } - event.type = 0; //Clear last event - } - - engine->Tick(); - engine->Draw(); - - if(SDL_GetTicks()-lastTick>250) - { - //Run client tick every second - lastTick = SDL_GetTicks(); - Client::Ref().Tick(); - } - - if(scale != engine->Scale || fullscreen != engine->Fullscreen) - { - sdl_scrn = SDLSetScreen(engine->Scale, engine->Fullscreen); - inputScale = 1.0f/float(scale); - } - -#ifdef OGLI - blit(); -#else - if(engine->Scale==2) - blit2(engine->g->vid, engine->Scale); - else - blit(engine->g->vid); -#endif - - currentFrame++; - currentTime = SDL_GetTicks(); - elapsedTime = currentTime - lastTime; - if(ui::Engine::Ref().FpsLimit > 2.0f && (currentFrame>2 || elapsedTime > 1000*2/ui::Engine::Ref().FpsLimit) && elapsedTime && currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit) - { - while (currentFrame*1000/elapsedTime > ui::Engine::Ref().FpsLimit) - { - SDL_Delay(1); - currentTime = SDL_GetTicks(); - elapsedTime = currentTime-lastTime; - } - } - if(elapsedTime>=1000) - { - fps = (((float)currentFrame)/((float)elapsedTime))*1000.0f; - currentFrame = 0; - lastTime = currentTime; - if(ui::Engine::Ref().FpsLimit > 2.0f) - { - delta = ui::Engine::Ref().FpsLimit/fps; - } - else - { - delta = 1.0f; - } - } - engine->SetFps(fps); - } + 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 14b2bf1..c68c72c 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 <direct.h> +#else +#include <sys/stat.h> +#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 459e1c0..312d98c 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 f86a386..b866338 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 9bff8fb..083e541 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 f8575bd..769127e 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 d5b6c95..3a2a39f 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 8016d56..1c4a672 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 b005201..31311fe 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 cd1c341..b28ba84 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 9202848..762cf80 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"); |
