From cd051924d9ca6d5c39e02111bc311fda9126435e Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 10 Aug 2012 15:41:39 +0100 Subject: [PATCH] Installation with Ctrl+I, fixes #77 --- src/client/Client.cpp | 187 +++++++++++++++++++++++++++ src/client/Client.h | 2 + src/dialogues/ConfirmPrompt.cpp | 2 +- src/dialogues/InformationMessage.cpp | 61 +++++++++ src/dialogues/InformationMessage.h | 20 +++ src/game/GameController.cpp | 31 +++++ src/game/GameController.h | 2 + src/game/GameView.cpp | 4 + 8 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 src/dialogues/InformationMessage.cpp create mode 100644 src/dialogues/InformationMessage.h diff --git a/src/client/Client.cpp b/src/client/Client.cpp index eb55f8ea6..c54681349 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -8,7 +8,15 @@ #include #include +#ifdef MACOSX +#include +#include +#endif + #ifdef WIN +#include +#include +#include #include #else #include @@ -124,6 +132,185 @@ void Client::Initialise(std::string proxyString) versionCheckRequest = http_async_req_start(NULL, SERVER "/Download/Version.json", NULL, 0, 1); } +bool Client::DoInstallation() +{ +#if defined(WIN) + int returnval; + LONG rresult; + HKEY newkey; + char *currentfilename = exe_name(); + char *iconname = NULL; + char *opencommand = NULL; + //char AppDataPath[MAX_PATH]; + char *AppDataPath = NULL; + iconname = (char*)malloc(strlen(currentfilename)+6); + sprintf(iconname, "%s,-102", currentfilename); + + //Create Roaming application data folder + /*if(!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE, NULL, 0, AppDataPath))) + { + returnval = 0; + goto finalise; + }*/ + + //AppDataPath = _getcwd(NULL, 0); + + //Move Game executable into application data folder + //TODO: Implement + + opencommand = (char*)malloc(strlen(currentfilename)+53+strlen(AppDataPath)); + /*if((strlen(AppDataPath)+strlen(APPDATA_SUBDIR "\\Powder Toy"))\n" +" \n" +" \n" +" Powder Toy save\n" +" \n" +" \n" +" \n" +"\n"; + f = fopen("powdertoy-save.xml", "wb"); + if (!f) + return 0; + fwrite(mimedata, 1, strlen(mimedata), f); + fclose(f); + + char *desktopfiledata_tmp = +"[Desktop Entry]\n" +"Type=Application\n" +"Name=Powder Toy\n" +"Comment=Physics sandbox game\n" +"MimeType=application/vnd.powdertoy.save;\n" +"NoDisplay=true\n"; + char *desktopfiledata = (char *)malloc(strlen(desktopfiledata_tmp)+strlen(currentfilename)+100); + strcpy(desktopfiledata, desktopfiledata_tmp); + strappend(desktopfiledata, "Exec="); + strappend(desktopfiledata, currentfilename); + strappend(desktopfiledata, " open %f\n"); + f = fopen("powdertoy-tpt.desktop", "wb"); + if (!f) + return 0; + fwrite(desktopfiledata, 1, strlen(desktopfiledata), f); + fclose(f); + system("xdg-mime install powdertoy-save.xml"); + system("xdg-desktop-menu install powdertoy-tpt.desktop"); + f = fopen("powdertoy-save-32.png", "wb"); + if (!f) + return 0; + fwrite(icon_doc_32_png, 1, sizeof(icon_doc_32_png), f); + fclose(f); + f = fopen("powdertoy-save-16.png", "wb"); + if (!f) + return 0; + fwrite(icon_doc_16_png, 1, sizeof(icon_doc_16_png), f); + fclose(f); + system("xdg-icon-resource install --noupdate --context mimetypes --size 32 powdertoy-save-32.png application-vnd.powdertoy.save"); + system("xdg-icon-resource install --noupdate --context mimetypes --size 16 powdertoy-save-16.png application-vnd.powdertoy.save"); + system("xdg-icon-resource forceupdate"); + system("xdg-mime default powdertoy-tpt.desktop application/vnd.powdertoy.save"); + unlink("powdertoy-save-32.png"); + unlink("powdertoy-save-16.png"); + unlink("powdertoy-save.xml"); + unlink("powdertoy-tpt.desktop"); + return true; +#elif defined MACOSX + return false; +#endif +} + void Client::SetProxy(std::string proxy) { http_done(); diff --git a/src/client/Client.h b/src/client/Client.h index 375c072ca..881249613 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -86,6 +86,8 @@ public: std::vector DirectorySearch(std::string directory, std::string search, std::vector extensions); std::vector DirectorySearch(std::string directory, std::string search, std::string extension); + bool DoInstallation(); + std::vector ReadFile(std::string filename); void Initialise(std::string proxyString); diff --git a/src/dialogues/ConfirmPrompt.cpp b/src/dialogues/ConfirmPrompt.cpp index d908a1ddb..459e1c02e 100644 --- a/src/dialogues/ConfirmPrompt.cpp +++ b/src/dialogues/ConfirmPrompt.cpp @@ -46,7 +46,7 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial }; - ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X-50, 16), "Cancel"); + 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); diff --git a/src/dialogues/InformationMessage.cpp b/src/dialogues/InformationMessage.cpp new file mode 100644 index 000000000..6cdd48e62 --- /dev/null +++ b/src/dialogues/InformationMessage.cpp @@ -0,0 +1,61 @@ +/* + * InformationMessage.cpp + * + * Created on: Jan 29, 2012 + * Author: Simon + */ + +#include "Style.h" +#include "InformationMessage.h" +#include "interface/Button.h" +#include "interface/Label.h" + +InformationMessage::InformationMessage(std::string title, std::string message): + ui::Window(ui::Point(-1, -1), ui::Point(200, 75)) +{ + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 16), title); + titleLabel->SetTextColour(style::Colour::InformationTitle); + titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(titleLabel); + + ui::Label * messageLabel = new ui::Label(ui::Point(4, 24), ui::Point(Size.X-8, 60), message); + messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; + AddComponent(messageLabel); + + class DismissAction: public ui::ButtonAction + { + InformationMessage * message; + public: + DismissAction(InformationMessage * message_) { message = message_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + message->SelfDestruct(); //TODO: Fix component disposal + } + }; + + ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "Dismiss"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); + okayButton->SetActionCallback(new DismissAction(this)); + AddComponent(okayButton); + SetOkayButton(okayButton); + SetCancelButton(okayButton); + + ui::Engine::Ref().ShowWindow(this); +} + +void InformationMessage::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+4, Size.Y+4); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +InformationMessage::~InformationMessage() { +} + diff --git a/src/dialogues/InformationMessage.h b/src/dialogues/InformationMessage.h new file mode 100644 index 000000000..6f0315473 --- /dev/null +++ b/src/dialogues/InformationMessage.h @@ -0,0 +1,20 @@ +/* + * InformationMessage.h + * + * Created on: Jan 29, 2012 + * Author: Simon + */ + +#ifndef INFORMATIONMESSAGE_H_ +#define INFORMATIONMESSAGE_H_ + +#include "interface/Window.h" + +class InformationMessage: public ui::Window { +public: + InformationMessage(std::string title, std::string message); + virtual void OnDraw(); + virtual ~InformationMessage(); +}; + +#endif /* INFORMATIONMESSAGE_H_ */ diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 622752b59..9636c8f90 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -10,6 +10,7 @@ #include "login/LoginController.h" #include "interface/Point.h" #include "dialogues/ErrorMessage.h" +#include "dialogues/InformationMessage.h" #include "dialogues/ConfirmPrompt.h" #include "GameModelException.h" #include "simulation/Air.h" @@ -172,6 +173,36 @@ void GameController::PlaceSave(ui::Point position) } } +void GameController::Install() +{ +#if defined(MACOSX) + new InformationMessage("No Installation necessary", "You don't need to install The Powder Toy on Mac OS X"); +#elif defined(WIN) || defined(LIN) + class InstallConfirmation: public ConfirmDialogueCallback { + public: + GameController * c; + InstallConfirmation(GameController * c_) { c = c_; } + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + { + if(Client::Ref().DoInstallation()) + { + new InformationMessage("Install Success", "The installation completed without error"); + } + else + { + new ErrorMessage("Could not install", "The installation did not complete due to an error"); + } + } + } + virtual ~InstallConfirmation() { } + }; + new ConfirmPrompt("Install The Powder Toy", "You are about to install The Powder Toy onto this computer", new InstallConfirmation(this)); +#else + new ErrorMessage("Cannot install", "You cannot install The Powder Toy on this platform"); +#endif +} + void GameController::AdjustBrushSize(int direction, bool logarithmic, bool xAxis, bool yAxis) { if(xAxis && yAxis) diff --git a/src/game/GameController.h b/src/game/GameController.h index 187d1012d..b301032cc 100644 --- a/src/game/GameController.h +++ b/src/game/GameController.h @@ -63,6 +63,8 @@ public: void Tick(); void Exit(); + void Install(); + void LoadRenderPreset(RenderPreset preset); void SetZoomEnabled(bool zoomEnable); void SetZoomPosition(ui::Point position); diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index 885b1e510..85d0e8d25 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -1267,6 +1267,10 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool case '[': c->AdjustBrushSize(-1, !alt, shiftBehaviour, ctrlBehaviour); break; + case 'i': + if(ctrl) + c->Install(); + break; } if(key >= '0' && key <= '9')