From 9abe51526cac2634af0541c3de69834dd30e9f78 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 22 Mar 2013 14:14:17 +0000 Subject: Move all GUI source files into gui/ diff --git a/src/Activity.h b/src/Activity.h index 0263467..b91444d 100644 --- a/src/Activity.h +++ b/src/Activity.h @@ -1,6 +1,6 @@ #pragma once -#include "interface/Window.h" +#include "gui/interface/Window.h" class Activity { diff --git a/src/PowderToyRenderer.cpp b/src/PowderToyRenderer.cpp index b5b7fa1..929a205 100644 --- a/src/PowderToyRenderer.cpp +++ b/src/PowderToyRenderer.cpp @@ -9,7 +9,7 @@ #include "Config.h" #include "Format.h" -#include "interface/Engine.h" +#include "gui/interface/Engine.h" #include "graphics/Graphics.h" #include "graphics/Renderer.h" diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 48a77b2..c57b3b9 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -25,13 +25,6 @@ #endif #include "Format.h" -#include "Style.h" -#include "interface/Engine.h" -#include "interface/Button.h" -#include "interface/Panel.h" -#include "interface/Point.h" -#include "interface/Label.h" -#include "interface/Keys.h" #include "client/GameSave.h" #include "client/SaveFile.h" @@ -39,10 +32,12 @@ #include "client/Client.h" #include "Misc.h" -#include "game/GameController.h" -#include "game/GameView.h" +#include "gui/game/GameController.h" +#include "gui/game/GameView.h" -#include "dialogues/ErrorMessage.h" +#include "gui/dialogues/ErrorMessage.h" +#include "gui/interface/Keys.h" +#include "gui/Style.h" #include "client/HTTP.h" diff --git a/src/Style.cpp b/src/Style.cpp deleted file mode 100644 index 1972ce8..0000000 --- a/src/Style.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include - -#include "Style.h" -#include "interface/Colour.h" - -namespace style { - ui::Colour Colour::InformationTitle = ui::Colour(140, 140, 255); - ui::Colour Colour::WarningTitle = ui::Colour(255, 216, 32); - ui::Colour Colour::ErrorTitle = ui::Colour(255, 64, 32); - - ui::Colour Colour::ConfirmButton = ui::Colour(255, 255, 50); - - ui::Colour Colour::ActiveBorder = ui::Colour(255, 255, 255); - ui::Colour Colour::InactiveBorder = ui::Colour(100, 100, 100); - - ui::Colour Colour::ActiveBackground = ui::Colour(50, 50, 50); - ui::Colour Colour::InactiveBackground = ui::Colour(0, 0, 0); -} diff --git a/src/Style.h b/src/Style.h deleted file mode 100644 index 7ac0a01..0000000 --- a/src/Style.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef STYLE_H_ -#define STYLE_H_ - -namespace ui { class Colour; } - -namespace style -{ - class Colour - { - public: - static ui::Colour InformationTitle; - static ui::Colour WarningTitle; - static ui::Colour ErrorTitle; - - static ui::Colour ConfirmButton; - - static ui::Colour ActiveBorder; - static ui::Colour InactiveBorder; - - static ui::Colour ActiveBackground; - static ui::Colour InactiveBackground; - }; - class Metrics - { - }; -} - -#endif diff --git a/src/cat/CommandInterface.cpp b/src/cat/CommandInterface.cpp index 992ce74..6b22488 100644 --- a/src/cat/CommandInterface.cpp +++ b/src/cat/CommandInterface.cpp @@ -5,8 +5,8 @@ #include #endif #include "CommandInterface.h" -#include "game/GameModel.h" -#include "game/GameController.h" +#include "gui/game/GameModel.h" +#include "gui/game/GameController.h" CommandInterface::CommandInterface(GameController * c, GameModel * m) { this->m = m; diff --git a/src/cat/CommandInterface.h b/src/cat/CommandInterface.h index 192e1f9..2e1a1df 100644 --- a/src/cat/CommandInterface.h +++ b/src/cat/CommandInterface.h @@ -2,7 +2,7 @@ #define COMMANDINTERFACE_H_ #include -#include "interface/Engine.h" +#include "gui/interface/Engine.h" //#include "game/GameModel.h" class GameModel; diff --git a/src/cat/LegacyLuaAPI.cpp b/src/cat/LegacyLuaAPI.cpp index 5df660c..df019fa 100644 --- a/src/cat/LegacyLuaAPI.cpp +++ b/src/cat/LegacyLuaAPI.cpp @@ -9,12 +9,12 @@ #include "LuaScriptInterface.h" #include "LuaScriptHelper.h" -#include "dialogues/ErrorMessage.h" -#include "dialogues/InformationMessage.h" -#include "dialogues/TextPrompt.h" -#include "dialogues/ConfirmPrompt.h" +#include "gui/dialogues/ErrorMessage.h" +#include "gui/dialogues/InformationMessage.h" +#include "gui/dialogues/TextPrompt.h" +#include "gui/dialogues/ConfirmPrompt.h" +#include "gui/game/GameModel.h" #include "simulation/Simulation.h" -#include "game/GameModel.h" #include diff --git a/src/cat/LuaButton.cpp b/src/cat/LuaButton.cpp index 01ca875..013adac 100644 --- a/src/cat/LuaButton.cpp +++ b/src/cat/LuaButton.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaButton.h" #include "LuaScriptInterface.h" -#include "interface/Button.h" +#include "gui/interface/Button.h" const char LuaButton::className[] = "Button"; diff --git a/src/cat/LuaCheckbox.cpp b/src/cat/LuaCheckbox.cpp index 8256730..bac5c4e 100644 --- a/src/cat/LuaCheckbox.cpp +++ b/src/cat/LuaCheckbox.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaCheckbox.h" #include "LuaScriptInterface.h" -#include "interface/Checkbox.h" +#include "gui/interface/Checkbox.h" const char LuaCheckbox::className[] = "Checkbox"; diff --git a/src/cat/LuaComponent.cpp b/src/cat/LuaComponent.cpp index 8c2d3a4..75e7a21 100644 --- a/src/cat/LuaComponent.cpp +++ b/src/cat/LuaComponent.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaComponent.h" #include "LuaScriptInterface.h" -#include "interface/Component.h" +#include "gui/interface/Component.h" LuaComponent::LuaComponent(lua_State * l) diff --git a/src/cat/LuaLabel.cpp b/src/cat/LuaLabel.cpp index 007e009..b08e687 100644 --- a/src/cat/LuaLabel.cpp +++ b/src/cat/LuaLabel.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaScriptInterface.h" #include "LuaLabel.h" -#include "interface/Label.h" +#include "gui/interface/Label.h" const char LuaLabel::className[] = "Label"; diff --git a/src/cat/LuaProgressBar.cpp b/src/cat/LuaProgressBar.cpp index 71b8f7b..08c88ca 100644 --- a/src/cat/LuaProgressBar.cpp +++ b/src/cat/LuaProgressBar.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaProgressBar.h" #include "LuaScriptInterface.h" -#include "interface/ProgressBar.h" +#include "gui/interface/ProgressBar.h" const char LuaProgressBar::className[] = "ProgressBar"; diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 79c79a1..f843f24 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -9,13 +9,13 @@ #include "LuaLuna.h" #include "LuaScriptInterface.h" #include "TPTScriptInterface.h" -#include "dialogues/ErrorMessage.h" -#include "dialogues/InformationMessage.h" -#include "dialogues/TextPrompt.h" -#include "dialogues/ConfirmPrompt.h" +#include "gui/dialogues/ErrorMessage.h" +#include "gui/dialogues/InformationMessage.h" +#include "gui/dialogues/TextPrompt.h" +#include "gui/dialogues/ConfirmPrompt.h" #include "simulation/Simulation.h" -#include "game/GameModel.h" -#include "game/Tool.h" +#include "gui/game/GameModel.h" +#include "gui/game/Tool.h" #include "LuaScriptHelper.h" #include "client/HTTP.h" #include "PowderToy.h" diff --git a/src/cat/LuaSlider.cpp b/src/cat/LuaSlider.cpp index 0fedbfb..ce07ebb 100644 --- a/src/cat/LuaSlider.cpp +++ b/src/cat/LuaSlider.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaSlider.h" #include "LuaScriptInterface.h" -#include "interface/Slider.h" +#include "gui/interface/Slider.h" const char LuaSlider::className[] = "Slider"; diff --git a/src/cat/LuaTextbox.cpp b/src/cat/LuaTextbox.cpp index 46285c8..5721525 100644 --- a/src/cat/LuaTextbox.cpp +++ b/src/cat/LuaTextbox.cpp @@ -8,7 +8,7 @@ extern "C" #include #include "LuaScriptInterface.h" #include "LuaTextbox.h" -#include "interface/Textbox.h" +#include "gui/interface/Textbox.h" const char LuaTextbox::className[] = "Textbox"; diff --git a/src/cat/LuaWindow.cpp b/src/cat/LuaWindow.cpp index 502c7f1..d765d68 100644 --- a/src/cat/LuaWindow.cpp +++ b/src/cat/LuaWindow.cpp @@ -14,9 +14,9 @@ extern "C" #include "LuaCheckbox.h" #include "LuaSlider.h" #include "LuaProgressBar.h" -#include "interface/Button.h" -#include "interface/Label.h" -#include "interface/Window.h" +#include "gui/interface/Button.h" +#include "gui/interface/Label.h" +#include "gui/interface/Window.h" const char LuaWindow::className[] = "Window"; diff --git a/src/cat/LuaWindow.h b/src/cat/LuaWindow.h index d9d25e1..f48c5f9 100644 --- a/src/cat/LuaWindow.h +++ b/src/cat/LuaWindow.h @@ -8,7 +8,7 @@ extern "C" { #include "LuaLuna.h" -#include "interface/Platform.h" +#include "gui/interface/Platform.h" namespace ui { class Window; diff --git a/src/cat/TPTSTypes.h b/src/cat/TPTSTypes.h index 6a2cec4..b9fa92b 100644 --- a/src/cat/TPTSTypes.h +++ b/src/cat/TPTSTypes.h @@ -3,7 +3,7 @@ #include #include -#include "interface/Point.h" +#include "gui/interface/Point.h" enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction }; diff --git a/src/cat/TPTScriptInterface.cpp b/src/cat/TPTScriptInterface.cpp index 84f55ef..601c228 100644 --- a/src/cat/TPTScriptInterface.cpp +++ b/src/cat/TPTScriptInterface.cpp @@ -5,7 +5,7 @@ #include #include #include "TPTScriptInterface.h" -#include "game/GameModel.h" +#include "gui/game/GameModel.h" #include "simulation/Air.h" TPTScriptInterface::TPTScriptInterface(GameController * c, GameModel * m): CommandInterface(c, m) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 22490a1..1a2f296 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -36,13 +36,13 @@ #include "HTTP.h" #include "simulation/SaveRenderer.h" -#include "interface/Point.h" +#include "gui/interface/Point.h" #include "client/SaveInfo.h" #include "client/SaveFile.h" #include "client/GameSave.h" #include "client/UserInfo.h" -#include "search/Thumbnail.h" -#include "preview/Comment.h" +#include "gui/search/Thumbnail.h" +#include "gui/preview/Comment.h" #include "ClientListener.h" #include "requestbroker/RequestBroker.h" #include "requestbroker/APIRequest.h" diff --git a/src/client/SaveFile.cpp b/src/client/SaveFile.cpp index 5ca16aa..9d08c30 100644 --- a/src/client/SaveFile.cpp +++ b/src/client/SaveFile.cpp @@ -1,7 +1,7 @@ #include "SaveFile.h" #include "GameSave.h" #include "Client.h" -#include "search/Thumbnail.h" +#include "gui/search/Thumbnail.h" SaveFile::SaveFile(SaveFile & save): gameSave(NULL), diff --git a/src/client/requestbroker/ThumbRenderRequest.cpp b/src/client/requestbroker/ThumbRenderRequest.cpp index cf56814..3601401 100644 --- a/src/client/requestbroker/ThumbRenderRequest.cpp +++ b/src/client/requestbroker/ThumbRenderRequest.cpp @@ -3,7 +3,6 @@ #include "ThumbRenderRequest.h" #include "client/GameSave.h" #include "graphics/Graphics.h" -#include "search/Thumbnail.h" #include "simulation/SaveRenderer.h" ThumbRenderRequest::ThumbRenderRequest(GameSave * save, bool decorations, bool fire, int width, int height, ListenerHandle listener): diff --git a/src/colourpicker/ColourPickerActivity.cpp b/src/colourpicker/ColourPickerActivity.cpp deleted file mode 100644 index 04aa644..0000000 --- a/src/colourpicker/ColourPickerActivity.cpp +++ /dev/null @@ -1,309 +0,0 @@ -#include -#include -#include "ColourPickerActivity.h" -#include "interface/Textbox.h" -#include "interface/Label.h" -#include "interface/Keys.h" -#include "game/Tool.h" -#include "Style.h" -#include "Format.h" -#include "game/GameModel.h" - -ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, ColourPickedCallback * callback) : - WindowActivity(ui::Point(-1, -1), ui::Point(266, 175)), - currentHue(0), - currentSaturation(0), - currentValue(0), - mouseDown(false), - valueMouseDown(false), - callback(callback) -{ - - class ColourChange : public ui::TextboxAction - { - ColourPickerActivity * a; - public: - ColourChange(ColourPickerActivity * a) : a(a) {} - - void TextChangedCallback(ui::Textbox * sender) - { - int r, g, b, alpha; - r = format::StringToNumber(a->rValue->GetText()); - g = format::StringToNumber(a->gValue->GetText()); - b = format::StringToNumber(a->bValue->GetText()); - alpha = format::StringToNumber(a->aValue->GetText()); - if (r > 255) - r = 255; - if (g > 255) - g = 255; - if (b > 255) - b = 255; - if (alpha > 255) - alpha = 255; - - RGB_to_HSV(r, g, b, &a->currentHue, &a->currentSaturation, &a->currentValue); - a->currentAlpha = alpha; - a->UpdateTextboxes(r, g, b, alpha); - } - }; - - rValue = new ui::Textbox(ui::Point(5, Size.Y-23), ui::Point(30, 17), "255"); - rValue->SetActionCallback(new ColourChange(this)); - rValue->SetLimit(3); - rValue->SetInputType(ui::Textbox::Number); - AddComponent(rValue); - - gValue = new ui::Textbox(ui::Point(40, Size.Y-23), ui::Point(30, 17), "255"); - gValue->SetActionCallback(new ColourChange(this)); - gValue->SetLimit(3); - gValue->SetInputType(ui::Textbox::Number); - AddComponent(gValue); - - bValue = new ui::Textbox(ui::Point(75, Size.Y-23), ui::Point(30, 17), "255"); - bValue->SetActionCallback(new ColourChange(this)); - bValue->SetLimit(3); - bValue->SetInputType(ui::Textbox::Number); - AddComponent(bValue); - - aValue = new ui::Textbox(ui::Point(110, Size.Y-23), ui::Point(30, 17), "255"); - aValue->SetActionCallback(new ColourChange(this)); - aValue->SetLimit(3); - aValue->SetInputType(ui::Textbox::Number); - AddComponent(aValue); - - hexValue = new::ui::Label(ui::Point(150, Size.Y-23), ui::Point(53, 17), "0xFFFFFFFF"); - AddComponent(hexValue); - - class OkayAction: public ui::ButtonAction - { - ColourPickerActivity * a; - public: - OkayAction(ColourPickerActivity * a) : a(a) { } - void ActionCallback(ui::Button * sender) - { - int Red, Green, Blue; - Red = format::StringToNumber(a->rValue->GetText()); - Green = format::StringToNumber(a->gValue->GetText()); - Blue = format::StringToNumber(a->bValue->GetText()); - ui::Colour col(Red, Green, Blue, a->currentAlpha); - if(a->callback) - a->callback->ColourPicked(col); - a->Exit(); - } - }; - - ui::Button * doneButton = new ui::Button(ui::Point(Size.X-45, Size.Y-23), ui::Point(40, 17), "Done"); - doneButton->SetActionCallback(new OkayAction(this)); - AddComponent(doneButton); - SetOkayButton(doneButton); - - RGB_to_HSV(initialColour.Red, initialColour.Green, initialColour.Blue, ¤tHue, ¤tSaturation, ¤tValue); - currentAlpha = initialColour.Alpha; - UpdateTextboxes(initialColour.Red, initialColour.Green, initialColour.Blue, initialColour.Alpha); -} - -void ColourPickerActivity::UpdateTextboxes(int r, int g, int b, int a) -{ - rValue->SetText(format::NumberToString(r)); - gValue->SetText(format::NumberToString(g)); - bValue->SetText(format::NumberToString(b)); - aValue->SetText(format::NumberToString(a)); - std::stringstream hex; - hex << std::hex << "0x" << std::setfill('0') << std::setw(2) << std::uppercase << a << std::setw(2) << r << std::setw(2) << g << std::setw(2) << b; - hexValue->SetText(hex.str()); -} -void ColourPickerActivity::OnTryExit(ExitMethod method) -{ - Exit(); -} - -void ColourPickerActivity::OnMouseMove(int x, int y, int dx, int dy) -{ - if(mouseDown) - { - x -= Position.X+5; - y -= Position.Y+5; - - currentHue = (float(x)/float(255))*359.0f; - currentSaturation = 255-(y*2); - - if(currentSaturation > 255) - currentSaturation = 255; - if(currentSaturation < 0) - currentSaturation = 0; - if(currentHue > 359) - currentHue = 359; - if(currentHue < 0) - currentHue = 0; - } - - if(valueMouseDown) - { - x -= Position.X+5; - y -= Position.Y+5; - - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } - - if(mouseDown || valueMouseDown) - { - int cr, cg, cb; - HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); - UpdateTextboxes(cr, cg, cb, currentAlpha); - } -} - -void ColourPickerActivity::OnMouseDown(int x, int y, unsigned button) -{ - x -= Position.X+5; - y -= Position.Y+5; - if(x >= 0 && x < 256 && y >= 0 && y <= 128) - { - mouseDown = true; - currentHue = (float(x)/float(255))*359.0f; - currentSaturation = 255-(y*2); - - if(currentSaturation > 255) - currentSaturation = 255; - if(currentSaturation < 0) - currentSaturation = 0; - if(currentHue > 359) - currentHue = 359; - if(currentHue < 0) - currentHue = 0; - } - - if(x >= 0 && x < 256 && y >= 132 && y <= 142) - { - valueMouseDown = true; - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } - - if(mouseDown || valueMouseDown) - { - int cr, cg, cb; - HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); - UpdateTextboxes(cr, cg, cb, currentAlpha); - } -} - -void ColourPickerActivity::OnMouseUp(int x, int y, unsigned button) -{ - if(mouseDown || valueMouseDown) - { - int cr, cg, cb; - HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); - UpdateTextboxes(cr, cg, cb, currentAlpha); - } - - if(mouseDown) - { - mouseDown = false; - x -= Position.X+5; - y -= Position.Y+5; - - currentHue = (float(x)/float(255))*359.0f; - currentSaturation = 255-(y*2); - - if(currentSaturation > 255) - currentSaturation = 255; - if(currentSaturation < 0) - currentSaturation = 0; - if(currentHue > 359) - currentHue = 359; - if(currentHue < 0) - currentHue = 0; - } - - if(valueMouseDown) - { - valueMouseDown = false; - - x -= Position.X+5; - y -= Position.Y+5; - - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } -} - - -void ColourPickerActivity::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - //g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->fillrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3, 0, 0, 0, currentAlpha); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); - - g->drawrect(Position.X+4, Position.Y+4, 258, 130, 180, 180, 180, 255); - - g->drawrect(Position.X+4, Position.Y+4+4+128, 258, 12, 180, 180, 180, 255); - - - int offsetX = Position.X+5; - int offsetY = Position.Y+5; - - - //draw color square - int lastx = -1, currx = 0; - for(int saturation = 0; saturation <= 255; saturation+=2) - { - for(int hue = 0; hue <= 359; hue++) - { - currx = clamp_flt(hue, 0, 359)+offsetX; - if (currx == lastx) - continue; - lastx = currx; - int cr = 0; - int cg = 0; - int cb = 0; - HSV_to_RGB(hue, 255-saturation, currentValue, &cr, &cg, &cb); - g->blendpixel(currx, (saturation/2)+offsetY, cr, cg, cb, currentAlpha); - } - } - - //draw brightness bar - for(int value = 0; value <= 255; value++) - for(int i = 0; i < 10; i++) - { - int cr = 0; - int cg = 0; - int cb = 0; - HSV_to_RGB(currentHue, currentSaturation, value, &cr, &cg, &cb); - - g->blendpixel(value+offsetX, i+offsetY+127+5, cr, cg, cb, currentAlpha); - } - - //draw color square pointer - int currentHueX = clamp_flt(currentHue, 0, 359); - int currentSaturationY = ((255-currentSaturation)/2); - g->xor_line(offsetX+currentHueX, offsetY+currentSaturationY-5, offsetX+currentHueX, offsetY+currentSaturationY-1); - g->xor_line(offsetX+currentHueX, offsetY+currentSaturationY+1, offsetX+currentHueX, offsetY+currentSaturationY+5); - g->xor_line(offsetX+currentHueX-5, offsetY+currentSaturationY, offsetX+currentHueX-1, offsetY+currentSaturationY); - g->xor_line(offsetX+currentHueX+1, offsetY+currentSaturationY, offsetX+currentHueX+5, offsetY+currentSaturationY); - - //draw brightness bar pointer - int currentValueX = restrict_flt(currentValue, 0, 254); - g->xor_line(offsetX+currentValueX, offsetY+4+128, offsetX+currentValueX, offsetY+13+128); - g->xor_line(offsetX+currentValueX+1, offsetY+4+128, offsetX+currentValueX+1, offsetY+13+128); -} - -ColourPickerActivity::~ColourPickerActivity() { - if(callback) - delete callback; -} - diff --git a/src/colourpicker/ColourPickerActivity.h b/src/colourpicker/ColourPickerActivity.h deleted file mode 100644 index 5cc3f04..0000000 --- a/src/colourpicker/ColourPickerActivity.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include -#include "Activity.h" -#include "interface/Window.h" -#include "interface/Textbox.h" - -class ColourPickedCallback -{ -public: - ColourPickedCallback() {} - virtual ~ColourPickedCallback() {} - virtual void ColourPicked(ui::Colour colour) {} -}; - -class ColourPickerActivity: public WindowActivity { - int currentHue; - int currentSaturation; - int currentValue; - int currentAlpha; - - bool mouseDown; - bool valueMouseDown; - - ui::Textbox * rValue; - ui::Textbox * gValue; - ui::Textbox * bValue; - ui::Textbox * aValue; - ui::Label * hexValue; - - ColourPickedCallback * callback; - - void UpdateTextboxes(int r, int g, int b, int a); -public: - virtual void OnMouseMove(int x, int y, int dx, int dy); - virtual void OnMouseDown(int x, int y, unsigned button); - virtual void OnMouseUp(int x, int y, unsigned button); - virtual void OnTryExit(ExitMethod method); - ColourPickerActivity(ui::Colour initialColour, ColourPickedCallback * callback = NULL); - virtual ~ColourPickerActivity(); - virtual void OnDraw(); -}; diff --git a/src/console/ConsoleCommand.h b/src/console/ConsoleCommand.h deleted file mode 100644 index 31e41b0..0000000 --- a/src/console/ConsoleCommand.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef CONSOLECOMMAND_H_ -#define CONSOLECOMMAND_H_ - -class ConsoleCommand -{ -public: - ConsoleCommand(std::string command, int returnStatus, std::string returnValue): - Command(command), ReturnStatus(returnStatus), ReturnValue(returnValue) - { - - } - std::string Command; - int ReturnStatus; - std::string ReturnValue; - - operator std::string() const - { - return Command; - } -}; - - -#endif /* CONSOLECOMMAND_H_ */ diff --git a/src/console/ConsoleController.cpp b/src/console/ConsoleController.cpp deleted file mode 100644 index 602636f..0000000 --- a/src/console/ConsoleController.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include "ConsoleController.h" - -ConsoleController::ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface): - HasDone(false) -{ - consoleModel = new ConsoleModel(); - consoleView = new ConsoleView(); - consoleView->AttachController(this); - consoleModel->AddObserver(consoleView); - - this->callback = callback; - this->commandInterface = commandInterface; -} - -void ConsoleController::EvaluateCommand(std::string command) -{ - if (command.substr(0, 6) == "!load ") - CloseConsole(); - int returnCode = commandInterface->Command(command); - if(command.length()) - consoleModel->AddLastCommand(ConsoleCommand(command, returnCode, commandInterface->GetLastError())); - else - CloseConsole(); -} - -void ConsoleController::CloseConsole() -{ - if(ui::Engine::Ref().GetWindow() == consoleView) - ui::Engine::Ref().CloseWindow(); -} - -std::string ConsoleController::FormatCommand(std::string command) -{ - return commandInterface->FormatCommand(command); -} - -void ConsoleController::NextCommand() -{ - int cIndex = consoleModel->GetCurrentCommandIndex(); - if(cIndex < consoleModel->GetPreviousCommands().size()) - consoleModel->SetCurrentCommandIndex(cIndex+1); -} - -void ConsoleController::PreviousCommand() -{ - int cIndex = consoleModel->GetCurrentCommandIndex(); - if(cIndex > 0) - consoleModel->SetCurrentCommandIndex(cIndex-1); -} - -void ConsoleController::Exit() -{ - if(ui::Engine::Ref().GetWindow() == consoleView) - ui::Engine::Ref().CloseWindow(); - if(callback) - callback->ControllerExit(); - HasDone = true; -} - -ConsoleView * ConsoleController::GetView() -{ - return consoleView; -} - -ConsoleController::~ConsoleController() { - if(ui::Engine::Ref().GetWindow() == consoleView) - ui::Engine::Ref().CloseWindow(); - if(callback) - delete callback; - delete consoleModel; - delete consoleView; -} - diff --git a/src/console/ConsoleController.h b/src/console/ConsoleController.h deleted file mode 100644 index cf8b36e..0000000 --- a/src/console/ConsoleController.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef CONSOLECONTROLLER_H_ -#define CONSOLECONTROLLER_H_ - -#include -#include "Controller.h" -#include "ConsoleView.h" -#include "ConsoleModel.h" -#include "ConsoleCommand.h" -#include "cat/CommandInterface.h" - -class ConsoleModel; -class ConsoleView; -class ConsoleController { - ControllerCallback * callback; - ConsoleView * consoleView; - ConsoleModel * consoleModel; - CommandInterface * commandInterface; -public: - bool HasDone; - ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface); - std::string FormatCommand(std::string command); - void EvaluateCommand(std::string command); - void NextCommand(); - void PreviousCommand(); - void Exit(); - void CloseConsole(); - ConsoleView * GetView(); - virtual ~ConsoleController(); -}; - -#endif /* CONSOLECONTROLLER_H_ */ diff --git a/src/console/ConsoleModel.cpp b/src/console/ConsoleModel.cpp deleted file mode 100644 index ceb6b32..0000000 --- a/src/console/ConsoleModel.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "client/Client.h" -#include "ConsoleModel.h" - -ConsoleModel::ConsoleModel() { - std::vector previousHistory = Client::Ref().GetPrefStringArray("Console.History"); - for(std::vector::reverse_iterator iter = previousHistory.rbegin(), end = previousHistory.rend(); iter != end; ++iter) - { - if(previousCommands.size()<25) - { - previousCommands.push_front(ConsoleCommand(*iter, 0, "")); - currentCommandIndex = previousCommands.size(); - } - } -} - -void ConsoleModel::AddObserver(ConsoleView * observer) -{ - observers.push_back(observer); - observer->NotifyPreviousCommandsChanged(this); -} - -int ConsoleModel::GetCurrentCommandIndex() -{ - return currentCommandIndex; -} - -void ConsoleModel::SetCurrentCommandIndex(int index) -{ - currentCommandIndex = index; - notifyCurrentCommandChanged(); -} - -ConsoleCommand ConsoleModel::GetCurrentCommand() -{ - if(currentCommandIndex < 0 || currentCommandIndex >= previousCommands.size()) - { - return ConsoleCommand("", 0, ""); - } - return previousCommands[currentCommandIndex]; -} - -void ConsoleModel::AddLastCommand(ConsoleCommand command) -{ - previousCommands.push_back(command); - if(previousCommands.size()>25) - previousCommands.pop_front(); - currentCommandIndex = previousCommands.size(); - notifyPreviousCommandsChanged(); -} - -std::deque ConsoleModel::GetPreviousCommands() -{ - return previousCommands; -} - -void ConsoleModel::notifyPreviousCommandsChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyPreviousCommandsChanged(this); - } -} - -void ConsoleModel::notifyCurrentCommandChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyCurrentCommandChanged(this); - } -} - -ConsoleModel::~ConsoleModel() { - Client::Ref().SetPref("Console.History", std::vector(previousCommands.begin(), previousCommands.end())); -} - diff --git a/src/console/ConsoleModel.h b/src/console/ConsoleModel.h deleted file mode 100644 index 312739a..0000000 --- a/src/console/ConsoleModel.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef CONSOLEMODEL_H_ -#define CONSOLEMODEL_H_ - -#include -#include -#include "ConsoleView.h" -#include "ConsoleCommand.h" - -class ConsoleView; -class ConsoleModel { - int currentCommandIndex; - std::vector observers; - std::deque previousCommands; - void notifyPreviousCommandsChanged(); - void notifyCurrentCommandChanged(); -public: - int GetCurrentCommandIndex(); - void SetCurrentCommandIndex(int index); - ConsoleCommand GetCurrentCommand(); - - std::deque GetPreviousCommands(); - ConsoleModel(); - void AddObserver(ConsoleView * observer); - void AddLastCommand(ConsoleCommand command); - virtual ~ConsoleModel(); -}; - -#endif /* CONSOLEMODEL_H_ */ diff --git a/src/console/ConsoleView.cpp b/src/console/ConsoleView.cpp deleted file mode 100644 index 9a51366..0000000 --- a/src/console/ConsoleView.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "ConsoleView.h" -#include "interface/Keys.h" - -ConsoleView::ConsoleView(): - ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, 150)), - commandField(NULL) -{ - class CommandHighlighter: public ui::TextboxAction - { - ConsoleView * v; - public: - CommandHighlighter(ConsoleView * v_) { v = v_; } - virtual void TextChangedCallback(ui::Textbox * sender) - { - sender->SetDisplayText(v->c->FormatCommand(sender->GetText())); - } - }; - commandField = new ui::Textbox(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), ""); - commandField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - commandField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - commandField->SetActionCallback(new CommandHighlighter(this)); - AddComponent(commandField); - FocusComponent(commandField); - commandField->SetBorder(false); -} - -void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - switch(key) - { - case KEY_ESCAPE: - case '`': - if (character != '~') - c->CloseConsole(); - else - Window::DoKeyPress(key, character, shift, ctrl, alt); - break; - case KEY_RETURN: - case KEY_ENTER: - c->EvaluateCommand(commandField->GetText()); - commandField->SetText(""); - commandField->SetDisplayText(""); - break; - case KEY_DOWN: - c->NextCommand(); - break; - case KEY_UP: - c->PreviousCommand(); - break; - default: - Window::DoKeyPress(key, character, shift, ctrl, alt); - break; - } -} - -void ConsoleView::NotifyPreviousCommandsChanged(ConsoleModel * sender) -{ - for(int i = 0; i < commandList.size(); i++) - { - RemoveComponent(commandList[i]); - delete commandList[i]; - } - commandList.clear(); - std::deque commands = sender->GetPreviousCommands(); - int currentY = Size.Y - 32; - if(commands.size()) - for(int i = commands.size()-1; i >= 0; i--) - { - if(currentY <= 0) - break; - ui::Label * tempLabel = new ui::Label(ui::Point(Size.X/2, currentY), ui::Point(Size.X/2, 16), commands[i].ReturnValue); - tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - commandList.push_back(tempLabel); - AddComponent(tempLabel); - tempLabel = new ui::Label(ui::Point(0, currentY), ui::Point(Size.X/2, 16), commands[i].Command); - tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - commandList.push_back(tempLabel); - AddComponent(tempLabel); - currentY-=16; - } -} - -void ConsoleView::NotifyCurrentCommandChanged(ConsoleModel * sender) -{ - commandField->SetText(sender->GetCurrentCommand().Command); - commandField->SetDisplayText(c->FormatCommand(commandField->GetText())); -} - - -void ConsoleView::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 0, 0, 0, 110); - g->draw_line(Position.X, Position.Y+Size.Y-16, Position.X+Size.X, Position.Y+Size.Y-16, 255, 255, 255, 160); - g->draw_line(Position.X, Position.Y+Size.Y, Position.X+Size.X, Position.Y+Size.Y, 255, 255, 255, 200); -} - -ConsoleView::~ConsoleView() { -} - diff --git a/src/console/ConsoleView.h b/src/console/ConsoleView.h deleted file mode 100644 index de1c48e..0000000 --- a/src/console/ConsoleView.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef CONSOLEVIEW_H_ -#define CONSOLEVIEW_H_ - -#include -#include -#include "interface/Label.h" -#include "interface/Window.h" -#include "ConsoleController.h" -#include "ConsoleModel.h" -#include "interface/Textbox.h" -#include "ConsoleCommand.h" - - -class ConsoleController; -class ConsoleModel; -class ConsoleView: public ui::Window { - ConsoleController * c; - ui::Textbox * commandField; - std::vector commandList; -public: - ConsoleView(); - virtual void OnDraw(); - virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); - void AttachController(ConsoleController * c_) { c = c_; } - void NotifyPreviousCommandsChanged(ConsoleModel * sender); - void NotifyCurrentCommandChanged(ConsoleModel * sender); - virtual ~ConsoleView(); -}; - -#endif /* CONSOLEVIEW_H_ */ diff --git a/src/debug/DebugInfo.h b/src/debug/DebugInfo.h index 3ddd4b3..49e46a4 100644 --- a/src/debug/DebugInfo.h +++ b/src/debug/DebugInfo.h @@ -1,6 +1,6 @@ #pragma once -#include "interface/Point.h" +#include "gui/interface/Point.h" class DebugInfo { diff --git a/src/debug/ElementPopulation.cpp b/src/debug/ElementPopulation.cpp index 225cf02..4caaf11 100644 --- a/src/debug/ElementPopulation.cpp +++ b/src/debug/ElementPopulation.cpp @@ -1,7 +1,7 @@ #pragma once #include "ElementPopulation.h" -#include "interface/Engine.h" +#include "gui/interface/Engine.h" #include "simulation/Simulation.h" #include "Format.h" diff --git a/src/dialogues/ConfirmPrompt.cpp b/src/dialogues/ConfirmPrompt.cpp deleted file mode 100644 index 0a89efa..0000000 --- a/src/dialogues/ConfirmPrompt.cpp +++ /dev/null @@ -1,152 +0,0 @@ -#include "ConfirmPrompt.h" -#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, 35)), - 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), "Continue"); - 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); -} - -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; - - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -ConfirmPrompt::~ConfirmPrompt() { - if(callback) - delete callback; -} - diff --git a/src/dialogues/ConfirmPrompt.h b/src/dialogues/ConfirmPrompt.h deleted file mode 100644 index 9e38a48..0000000 --- a/src/dialogues/ConfirmPrompt.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef CONFIRMPROMPT_H_ -#define CONFIRMPROMPT_H_ - -#include "interface/Window.h" - -class ConfirmDialogueCallback; -class ConfirmPrompt: public ui::Window { -public: - enum DialogueResult { ResultCancel, ResultOkay }; - 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; -}; - -class ConfirmDialogueCallback -{ - public: - virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) {} - virtual ~ConfirmDialogueCallback() {} -}; - -#endif /* CONFIRMPROMPT_H_ */ diff --git a/src/dialogues/ErrorMessage.cpp b/src/dialogues/ErrorMessage.cpp deleted file mode 100644 index 876bc10..0000000 --- a/src/dialogues/ErrorMessage.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "Style.h" -#include "ErrorMessage.h" -#include "interface/Button.h" -#include "interface/Label.h" -#include "PowderToy.h" - -ErrorMessage::ErrorMessage(std::string title, std::string message, ErrorMessageCallback * callback_): - ui::Window(ui::Point(-1, -1), ui::Point(200, 35)), - callback(callback_) -{ - ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 16), title); - titleLabel->SetTextColour(style::Colour::ErrorTitle); - 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, -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 DismissAction: public ui::ButtonAction - { - ErrorMessage * message; - public: - DismissAction(ErrorMessage * message_) { message = message_; } - void ActionCallback(ui::Button * sender) - { - ui::Engine::Ref().CloseWindow(); - if(message->callback) - message->callback->DismissCallback(); - message->SelfDestruct(); - } - }; - - 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 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; - - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -ErrorMessage::~ErrorMessage() { - if(callback) - delete callback; -} - diff --git a/src/dialogues/ErrorMessage.h b/src/dialogues/ErrorMessage.h deleted file mode 100644 index 34cdb0c..0000000 --- a/src/dialogues/ErrorMessage.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef ERRORMESSAGE_H_ -#define ERRORMESSAGE_H_ - -#include "interface/Window.h" - -class ErrorMessageCallback; -class ErrorMessage: public ui::Window { - ErrorMessageCallback * callback; -public: - 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/InformationMessage.cpp b/src/dialogues/InformationMessage.cpp deleted file mode 100644 index 7d11a26..0000000 --- a/src/dialogues/InformationMessage.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "Style.h" -#include "InformationMessage.h" -#include "interface/Button.h" -#include "interface/Label.h" -#include "interface/ScrollPanel.h" - -InformationMessage::InformationMessage(std::string title, std::string message, bool large): - ui::Window(ui::Point(-1, -1), ui::Point(200, 75)) -{ - if (large) //Maybe also use this large mode for changelogs eventually, or have it as a customizable size? - { - Size.X += 200; - Size.Y += 175; - } - - if (large) - { - ui::ScrollPanel *messagePanel = new ui::ScrollPanel(ui::Point(4, 24), ui::Point(Size.X-8, 206)); - AddComponent(messagePanel); - - ui::Label * messageLabel = new ui::Label(ui::Point(4, 0), ui::Point(Size.X-28, -1), message); - messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; - messageLabel->SetMultiline(true); - messagePanel->AddChild(messageLabel); - - messagePanel->InnerSize = ui::Point(messagePanel->Size.X, messageLabel->Size.Y+4); - } - else - { - 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); - } - - 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); - - 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+3, Size.Y+3); - 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 deleted file mode 100644 index 1df4794..0000000 --- a/src/dialogues/InformationMessage.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef INFORMATIONMESSAGE_H_ -#define INFORMATIONMESSAGE_H_ - -#include "interface/Window.h" - -class InformationMessage: public ui::Window { -public: - InformationMessage(std::string title, std::string message, bool large); - virtual void OnDraw(); - virtual ~InformationMessage(); -}; - -#endif /* INFORMATIONMESSAGE_H_ */ diff --git a/src/dialogues/LegacyDialogues.h b/src/dialogues/LegacyDialogues.h deleted file mode 100644 index 7f6097d..0000000 --- a/src/dialogues/LegacyDialogues.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -//Legacy blocking prompts -//This are not implemented here, but rather in the engine bootstrapper -bool ConfirmUI(std::string title, std::string message, std::string confirmText) {} - -void ErrorUI(std::string title, std::string message) {} - -void InformationUI(std::string title, std::string message) {} - -std::string MessagePromptUI(std::string title, std::string message, std::string text, std::string placeholder) {} \ No newline at end of file diff --git a/src/dialogues/TextPrompt.cpp b/src/dialogues/TextPrompt.cpp deleted file mode 100644 index cd06a8c..0000000 --- a/src/dialogues/TextPrompt.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include "TextPrompt.h" -#include "interface/Label.h" -#include "interface/Button.h" -#include "Style.h" -#include "PowderToy.h" - -class CloseAction: public ui::ButtonAction -{ -public: - TextPrompt * prompt; - TextPrompt::DialogueResult result; - CloseAction(TextPrompt * prompt_, TextPrompt::DialogueResult result_) { prompt = prompt_; result = result_; } - void ActionCallback(ui::Button * sender) - { - ui::Engine::Ref().CloseWindow(); - if(prompt->callback) - prompt->callback->TextCallback(result, prompt->textField->GetText()); - prompt->SelfDestruct(); //TODO: Fix component disposal - } -}; - -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, 65)), - callback(callback_) -{ - if(multiline) - Size.X += 100; - - ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 18), 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+4; - - textField = new ui::Textbox(ui::Point(4, messageLabel->Position.Y + messageLabel->Size.Y + 7), ui::Point(Size.X-8, 16), text, placeholder); - if(multiline) - { - textField->SetMultiline(true); - textField->Size.Y = 60; - Size.Y += 45; - textField->Appearance.VerticalAlign = ui::Appearance::AlignTop; - } - else - { - textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - } - textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - AddComponent(textField); - FocusComponent(textField); - - ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point((Size.X/2)+1, 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/2, Size.Y-16), ui::Point(Size.X/2, 16), "Okay"); - okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignRight; - 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); -} - -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; - - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -TextPrompt::~TextPrompt() { - if(callback) - delete callback; -} - diff --git a/src/dialogues/TextPrompt.h b/src/dialogues/TextPrompt.h deleted file mode 100644 index 5478cb5..0000000 --- a/src/dialogues/TextPrompt.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef TEXTPROMPT_H_ -#define TEXTPROMPT_H_ - -#include "interface/Window.h" -#include "interface/Textbox.h" - -class TextDialogueCallback; -class TextPrompt: public ui::Window { -protected: - ui::Textbox * textField; -public: - friend class CloseAction; - enum DialogueResult { ResultCancel, ResultOkay }; - 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; -}; - -class TextDialogueCallback -{ - public: - virtual void TextCallback(TextPrompt::DialogueResult result, std::string resultText) {} - virtual ~TextDialogueCallback() {} -}; - -#endif /* TEXTPROMPT_H_ */ diff --git a/src/elementsearch/ElementSearchActivity.cpp b/src/elementsearch/ElementSearchActivity.cpp deleted file mode 100644 index 7156cb1..0000000 --- a/src/elementsearch/ElementSearchActivity.cpp +++ /dev/null @@ -1,184 +0,0 @@ -#include -#include "ElementSearchActivity.h" -#include "interface/Textbox.h" -#include "interface/Label.h" -#include "interface/Keys.h" -#include "game/Tool.h" -#include "Style.h" -#include "game/GameModel.h" - -class ElementSearchActivity::ToolAction: public ui::ButtonAction -{ - ElementSearchActivity * a; -public: - Tool * tool; - ToolAction(ElementSearchActivity * a, Tool * tool) : a(a), tool(tool) { } - void ActionCallback(ui::Button * sender_) - { - ToolButton *sender = (ToolButton*)sender_; - if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 2) - a->SetActiveTool(sender->GetSelectionState(), tool); - } -}; - -ElementSearchActivity::ElementSearchActivity(GameModel * gameModel, std::vector tools) : - WindowActivity(ui::Point(-1, -1), ui::Point(236, 302)), - gameModel(gameModel), - tools(tools), - firstResult(NULL) -{ - ui::Label * title = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), "Element Search"); - title->SetTextColour(style::Colour::InformationTitle); - title->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - AddComponent(title); - - class SearchAction : public ui::TextboxAction - { - private: - ElementSearchActivity * a; - public: - SearchAction(ElementSearchActivity * a) : a(a) {} - virtual void TextChangedCallback(ui::Textbox * sender) { - a->searchTools(sender->GetText()); - } - }; - - searchField = new ui::Textbox(ui::Point(8, 23), ui::Point(Size.X-16, 17), ""); - searchField->SetActionCallback(new SearchAction(this)); - searchField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - AddComponent(searchField); - FocusComponent(searchField); - - class CloseAction: public ui::ButtonAction - { - ElementSearchActivity * a; - public: - CloseAction(ElementSearchActivity * a) : a(a) { } - void ActionCallback(ui::Button * sender_) - { - a->Exit(); - } - }; - - class OKAction: public ui::ButtonAction - { - ElementSearchActivity * a; - public: - OKAction(ElementSearchActivity * a) : a(a) { } - void ActionCallback(ui::Button * sender_) - { - if(a->GetFirstResult()) - a->SetActiveTool(0, a->GetFirstResult()); - } - }; - - ui::Button * closeButton = new ui::Button(ui::Point(0, Size.Y-15), ui::Point((Size.X/2)+1, 15), "Close"); - closeButton->SetActionCallback(new CloseAction(this)); - ui::Button * okButton = new ui::Button(ui::Point(Size.X/2, Size.Y-15), ui::Point(Size.X/2, 15), "OK"); - okButton->SetActionCallback(new OKAction(this)); - - AddComponent(okButton); - AddComponent(closeButton); - - searchTools(""); -} - -void ElementSearchActivity::searchTools(std::string query) -{ - firstResult = NULL; - for(std::vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter != end; ++iter) { - delete *iter; - RemoveComponent(*iter); - } - toolButtons.clear(); - - ui::Point viewPosition = searchField->Position + ui::Point(2+0, searchField->Size.Y+2+8); - ui::Point current = ui::Point(0, 0); - - std::string queryLower = std::string(query); - std::transform(queryLower.begin(), queryLower.end(), queryLower.begin(), ::tolower); - - for(std::vector::iterator iter = tools.begin(), end = tools.end(); iter != end; ++iter) { - std::string nameLower = std::string((*iter)->GetName()); - std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower); - - if(strstr(nameLower.c_str(), queryLower.c_str())!=0) - { - Tool * tool = *iter; - - if(!firstResult) - firstResult = tool; - - VideoBuffer * tempTexture = tool->GetTexture(26, 14); - ToolButton * tempButton; - - if(tempTexture) - tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), "", tool->GetDescription()); - else - tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->GetName(), tool->GetDescription()); - - tempButton->Appearance.SetTexture(tempTexture); - tempButton->Appearance.BackgroundInactive = ui::Colour(tool->colRed, tool->colGreen, tool->colBlue); - tempButton->SetActionCallback(new ToolAction(this, tool)); - - if(gameModel->GetActiveTool(0) == tool) - { - tempButton->SetSelectionState(0); //Primary - } - else if(gameModel->GetActiveTool(1) == tool) - { - tempButton->SetSelectionState(1); //Secondary - } - else if(gameModel->GetActiveTool(2) == tool) - { - tempButton->SetSelectionState(2); //Tertiary - } - - toolButtons.push_back(tempButton); - AddComponent(tempButton); - - current.X += 31; - - if(current.X + 30 > searchField->Size.X) { - current.X = 0; - current.Y += 19; - } - - if(current.Y + viewPosition.Y + 18 > Size.Y-23) - break; - } - } -} - -void ElementSearchActivity::SetActiveTool(int selectionState, Tool * tool) -{ - gameModel->SetActiveTool(selectionState, tool); - Exit(); -} - -void ElementSearchActivity::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); - - g->drawrect(Position.X+searchField->Position.X, Position.Y+searchField->Position.Y+searchField->Size.Y+8, searchField->Size.X, Size.Y-(searchField->Position.Y+searchField->Size.Y+8)-23, 255, 255, 255, 180); -} - -void ElementSearchActivity::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if(key == KEY_ENTER || key == KEY_RETURN) - { - if(firstResult) - gameModel->SetActiveTool(0, firstResult); - Exit(); - } - if(key == KEY_ESCAPE) - { - Exit(); - } -} - -ElementSearchActivity::~ElementSearchActivity() { -} - diff --git a/src/elementsearch/ElementSearchActivity.h b/src/elementsearch/ElementSearchActivity.h deleted file mode 100644 index 987d8fb..0000000 --- a/src/elementsearch/ElementSearchActivity.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef ELEMENTSEARCHACTIVITY_H_ -#define ELEMENTSEARCHACTIVITY_H_ - -#include -#include -#include "Activity.h" -#include "interface/Window.h" -#include "interface/Textbox.h" -#include "game/ToolButton.h" - -class Tool; - -class GameModel; - -class ElementSearchActivity: public WindowActivity { - Tool * firstResult; - GameModel * gameModel; - std::vector tools; - ui::Textbox * searchField; - std::vector toolButtons; - void searchTools(std::string query); -public: - class ToolAction; - Tool * GetFirstResult() { return firstResult; } - ElementSearchActivity(GameModel * gameModel, std::vector tools); - void SetActiveTool(int selectionState, Tool * tool); - virtual ~ElementSearchActivity(); - virtual void OnDraw(); - virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); -}; - -#endif /* ELEMENTSEARCHACTIVITY_H_ */ diff --git a/src/filebrowser/FileBrowserActivity.cpp b/src/filebrowser/FileBrowserActivity.cpp deleted file mode 100644 index 0ea7104..0000000 --- a/src/filebrowser/FileBrowserActivity.cpp +++ /dev/null @@ -1,336 +0,0 @@ -#include -#include -#include "FileBrowserActivity.h" -#include "interface/Label.h" -#include "interface/Textbox.h" -#include "interface/ScrollPanel.h" -#include "interface/SaveButton.h" -#include "interface/ProgressBar.h" -#include "client/Client.h" -#include "client/SaveFile.h" -#include "client/GameSave.h" -#include "Style.h" -#include "tasks/Task.h" -#include "simulation/SaveRenderer.h" -#include "dialogues/TextPrompt.h" -#include "dialogues/ConfirmPrompt.h" -#include "dialogues/ErrorMessage.h" - -class Thumbnail; - - -class SaveSelectedAction: public ui::SaveButtonAction -{ - FileBrowserActivity * a; -public: - SaveSelectedAction(FileBrowserActivity * _a) { a = _a; } - virtual void ActionCallback(ui::SaveButton * sender) - { - a->SelectSave(sender->GetSaveFile()); - } - virtual void AltActionCallback(ui::SaveButton * sender) - { - a->RenameSave(sender->GetSaveFile()); - } - virtual void AltActionCallback2(ui::SaveButton * sender) - { - a->DeleteSave(sender->GetSaveFile()); - } -}; - -//Currently, reading is done on another thread, we can't render outside the main thread due to some bullshit with OpenGL -class LoadFilesTask: public Task -{ - std::string directory; - std::string search; - std::vector saveFiles; - - virtual void before() - { - - } - - virtual void after() - { - - } - - virtual bool doWork() - { - std::vector files = Client::Ref().DirectorySearch(directory, search, ".cps"); - - - notifyProgress(-1); - for(std::vector::iterator iter = files.begin(), end = files.end(); iter != end; ++iter) - { - SaveFile * saveFile = new SaveFile(*iter); - try - { - std::vector data = Client::Ref().ReadFile(*iter); - GameSave * tempSave = new GameSave(data); - saveFile->SetGameSave(tempSave); - saveFiles.push_back(saveFile); - - std::string filename = *iter; - size_t folderPos = filename.rfind(PATH_SEP); - if(folderPos!=std::string::npos && folderPos+1 < filename.size()) - { - filename = filename.substr(folderPos+1); - } - size_t extPos = filename.rfind("."); - if(extPos!=std::string::npos) - { - filename = filename.substr(0, extPos); - } - saveFile->SetDisplayName(filename); - } - catch(std::exception & e) - { - //:( - } - } - return true; - } - -public: - std::vector GetSaveFiles() - { - return saveFiles; - } - - LoadFilesTask(std::string directory, std::string search): - directory(directory), - search(search) - { - - } -}; - -class FileBrowserActivity::SearchAction: public ui::TextboxAction -{ -public: - FileBrowserActivity * a; - SearchAction(FileBrowserActivity * a) : a(a) {} - virtual void TextChangedCallback(ui::Textbox * sender) { - a->DoSearch(sender->GetText()); - } -}; - -FileBrowserActivity::FileBrowserActivity(std::string directory, FileSelectedCallback * callback): - WindowActivity(ui::Point(-1, -1), ui::Point(450, 300)), - callback(callback), - directory(directory), - totalFiles(0) -{ - - ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 18), "Save Browser"); - titleLabel->SetTextColour(style::Colour::WarningTitle); - titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(titleLabel); - - ui::Textbox * textField = new ui::Textbox(ui::Point(8, 25), ui::Point(Size.X-16, 16), "", "[search]"); - textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - textField->SetActionCallback(new SearchAction(this)); - AddComponent(textField); - - itemList = new ui::ScrollPanel(ui::Point(4, 45), ui::Point(Size.X-8, Size.Y-53)); - AddComponent(itemList); - - progressBar = new ui::ProgressBar(ui::Point((Size.X-200)/2, 45+(Size.Y-66)/2), ui::Point(200, 17)); - AddComponent(progressBar); - - infoText = new ui::Label(ui::Point((Size.X-200)/2, 45+(Size.Y-66)/2), ui::Point(200, 17), "No saves found"); - AddComponent(infoText); - - filesX = 4; - filesY = 3; - buttonPadding = 2; - fileX = 0; - fileY = 0; - - buttonXOffset = 0; - buttonYOffset = 0; - buttonAreaWidth = itemList->Size.X; - buttonAreaHeight = itemList->Size.Y;// - buttonYOffset - 18; - buttonWidth = (buttonAreaWidth/filesX) - buttonPadding*2; - buttonHeight = (buttonAreaHeight/filesY) - buttonPadding*2; - - loadDirectory(directory, ""); -} - -void FileBrowserActivity::DoSearch(std::string search) -{ - if(!loadFiles) - { - loadDirectory(directory, search); - } -} - -void FileBrowserActivity::SelectSave(SaveFile * file) -{ - if(callback) - callback->FileSelected(new SaveFile(*file)); - Exit(); -} - -void FileBrowserActivity::DeleteSave(SaveFile * file) -{ - std::string deleteMessage = "Are you sure you want to delete " + file->GetDisplayName() + ".cps?"; - if (ConfirmPrompt::Blocking("Delete Save", deleteMessage)) - { - remove(file->GetName().c_str()); - loadDirectory(directory, ""); - } -} - -void FileBrowserActivity::RenameSave(SaveFile * file) -{ - std::string newName = TextPrompt::Blocking("Rename", "Change save name", file->GetDisplayName(), "", 0); - if (newName.length()) - { - newName = directory + PATH_SEP + newName + ".cps"; - int ret = rename(file->GetName().c_str(), newName.c_str()); - if (ret) - ErrorMessage::Blocking("Error", "Could not rename file"); - else - loadDirectory(directory, ""); - } - else - ErrorMessage::Blocking("Error", "No save name given"); -} - -void FileBrowserActivity::loadDirectory(std::string directory, std::string search) -{ - for(int i = 0; i < components.size(); i++) - { - RemoveComponent(components[i]); - itemList->RemoveChild(components[i]); - } - - for(std::vector::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter) - { - delete *iter; - } - componentsQueue.clear(); - - for(std::vector::iterator iter = files.begin(), end = files.end(); iter != end; ++iter) - { - delete *iter; - } - files.clear(); - - infoText->Visible = false; - progressBar->Visible = true; - progressBar->SetProgress(-1); - progressBar->SetStatus("Loading files"); - loadFiles = new LoadFilesTask(directory, search); - loadFiles->AddTaskListener(this); - loadFiles->Start(); -} - -void FileBrowserActivity::NotifyDone(Task * task) -{ - fileX = 0; - fileY = 0; - files = ((LoadFilesTask*)task)->GetSaveFiles(); - totalFiles = files.size(); - delete loadFiles; - loadFiles = NULL; - if(!files.size()) - { - progressBar->Visible = false; - infoText->Visible = true; - } - for(int i = 0; i < components.size(); i++) - { - delete components[i]; - } - components.clear(); -} - -void FileBrowserActivity::OnMouseDown(int x, int y, unsigned button) -{ - if(!(x > Position.X && y > Position.Y && y < Position.Y+Size.Y && x < Position.X+Size.X)) //Clicked outside window - Exit(); -} - -void FileBrowserActivity::OnTryExit(ExitMethod method) -{ - Exit(); -} - -void FileBrowserActivity::NotifyError(Task * task) -{ - -} - -void FileBrowserActivity::NotifyProgress(Task * task) -{ - progressBar->SetProgress(task->GetProgress()); -} - -void FileBrowserActivity::NotifyStatus(Task * task) -{ - -} - -void FileBrowserActivity::OnTick(float dt) -{ - if(loadFiles) - loadFiles->Poll(); - - if(files.size()) - { - SaveFile * saveFile = files.back(); - files.pop_back(); - - if(fileX == filesX) - { - fileX = 0; - fileY++; - } - ui::SaveButton * saveButton = new ui::SaveButton( - ui::Point( - buttonXOffset + buttonPadding + fileX*(buttonWidth+buttonPadding*2), - buttonYOffset + buttonPadding + fileY*(buttonHeight+buttonPadding*2) - ), - ui::Point(buttonWidth, buttonHeight), - saveFile); - saveButton->AddContextMenu(1); - saveButton->Tick(dt); - saveButton->SetActionCallback(new SaveSelectedAction(this)); - progressBar->SetStatus("Rendering thumbnails"); - progressBar->SetProgress((float(totalFiles-files.size())/float(totalFiles))*100.0f); - componentsQueue.push_back(saveButton); - fileX++; - } - else if(componentsQueue.size()) - { - for(std::vector::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter) - { - components.push_back(*iter); - itemList->AddChild(*iter); - } - componentsQueue.clear(); - itemList->InnerSize.Y = (buttonHeight+(buttonPadding*2))*fileY; - if(!componentsQueue.size()) - progressBar->Visible = false; - } -} - -void FileBrowserActivity::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - - //Window Background+Outline - g->clearrect(Position.X-2, Position.Y-2, Size.X+4, Size.Y+4); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); -} - -FileBrowserActivity::~FileBrowserActivity() -{ - if(callback) - delete callback; -} \ No newline at end of file diff --git a/src/filebrowser/FileBrowserActivity.h b/src/filebrowser/FileBrowserActivity.h deleted file mode 100644 index fa8f32a..0000000 --- a/src/filebrowser/FileBrowserActivity.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include -#include -#include "Activity.h" -#include "interface/Window.h" -#include "tasks/TaskListener.h" - - -class SaveFile; -class FileSelectedCallback -{ -public: - FileSelectedCallback() {} - virtual ~FileSelectedCallback() {} - virtual void FileSelected(SaveFile* file) {} -}; - -namespace ui -{ - class Label; - class ScrollPanel; - class ProgressBar; -} - -class LoadFilesTask; -class FileBrowserActivity: public TaskListener, public WindowActivity -{ - LoadFilesTask * loadFiles; - FileSelectedCallback * callback; - ui::ScrollPanel * itemList; - ui::Label * infoText; - std::vector files; - std::vector components; - std::vector componentsQueue; - std::string directory; - - ui::ProgressBar * progressBar; - - int totalFiles; - int filesX, filesY, buttonPadding; - int fileX, fileY; - int buttonWidth, buttonHeight, buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset; - - - class SearchAction; - void populateList(); -public: - FileBrowserActivity(std::string directory, FileSelectedCallback * callback); - virtual void OnDraw(); - virtual void OnTick(float dt); - virtual void OnTryExit(ExitMethod method); - virtual void OnMouseDown(int x, int y, unsigned button); - void loadDirectory(std::string directory, std::string search); - void SelectSave(SaveFile * file); - void DeleteSave(SaveFile * file); - void RenameSave(SaveFile * file); - void DoSearch(std::string search); - virtual ~FileBrowserActivity(); - - virtual void NotifyDone(Task * task); - virtual void NotifyError(Task * task); - virtual void NotifyProgress(Task * task); - virtual void NotifyStatus(Task * task); -}; \ No newline at end of file diff --git a/src/game/BitmapBrush.h b/src/game/BitmapBrush.h deleted file mode 100644 index e1c0445..0000000 --- a/src/game/BitmapBrush.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * BitmapBrush.h - * - * Created on: Nov 18, 2012 - * Author: Simon Robertshaw - */ - -#ifndef BTIMAPBRUSH_H_ -#define BTIMAPBRUSH_H_ - -#include -#include -#include "Brush.h" - -class BitmapBrush: public Brush -{ -protected: - ui::Point origSize; - unsigned char * origBitmap; -public: - BitmapBrush(std::vector newBitmap, ui::Point rectSize_): - Brush(ui::Point(0, 0)), - origSize(0, 0) - { - ui::Point newSize = rectSize_; - - //Ensure the rect has odd dimentions so we can pull an integer radius with a 1x1 centre - if(!(newSize.X % 2)) - newSize.X += 1; - if(!(newSize.Y % 2)) - newSize.Y += 1; - - radius = (newSize-ui::Point(1, 1))/2; - size = newSize; - origSize = size; - - origBitmap = new unsigned char[size.X*size.Y]; - std::fill(origBitmap, origBitmap+(size.X*size.Y), 0); - for(int y = 0; y < rectSize_.Y; y++) - { - for(int x = 0; x < rectSize_.X; x++) - { - if(newBitmap[(y*rectSize_.X)+x] >= 128) - origBitmap[(y*size.X)+x] = newBitmap[(y*rectSize_.X)+x]; - } - } - - SetRadius(radius); - }; - virtual void GenerateBitmap() - { - if(origBitmap) - { - if(bitmap) - delete[] bitmap; - bitmap = new unsigned char[size.X*size.Y]; - if(size == origSize) - std::copy(origBitmap, origBitmap+(origSize.X*origSize.Y), bitmap); - else - { - //Bilinear interpolation - float factorX = ((float)origSize.X)/((float)size.X); - float factorY = ((float)origSize.Y)/((float)size.Y); - for(int y = 0; y < size.Y; y++) - { - for(int x = 0; x < size.X; x++) - { - float originalY = ((float)y)*factorY; - float originalX = ((float)x)*factorX; - - int lowerX = std::floor(originalX); - int upperX = std::min((float)(origSize.X-1), std::floor(originalX+1.0f)); - int lowerY = std::floor(originalY); - int upperY = std::min((float)(origSize.Y-1), std::floor(originalY+1.0f)); - - unsigned char topRight = origBitmap[(lowerY*origSize.X)+upperX]; - unsigned char topLeft = origBitmap[(lowerY*origSize.X)+lowerX]; - unsigned char bottomRight = origBitmap[(upperY*origSize.X)+upperX]; - unsigned char bottomLeft = origBitmap[(upperY*origSize.X)+lowerX]; - float top = LinearInterpolate(topLeft, topRight, lowerX, upperX, originalX); - float bottom = LinearInterpolate(bottomLeft, bottomRight, lowerX, upperX, originalX); - float mid = LinearInterpolate(top, bottom, lowerY, upperY, originalY); - bitmap[(y*size.X)+x] = mid > 128 ? 255 : 0; - } - } - } - } - } - virtual ~BitmapBrush() - { - if(origBitmap) - delete[] origBitmap; - } -}; - -#endif /* BTIMAPBRUSH_H_ */ diff --git a/src/game/Brush.cpp b/src/game/Brush.cpp deleted file mode 100644 index 9f2f2a8..0000000 --- a/src/game/Brush.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "Brush.h" -#include "graphics/Renderer.h" - -void Brush::RenderRect(Renderer * ren, ui::Point position1, ui::Point position2) -{ - int width, height, t; - width = position2.X-position1.X; - height = position2.Y-position1.Y; - if(height<0) - { - position1.Y += height; - height *= -1; - } - if(width<0) - { - position1.X += width; - width *= -1; - } - - ren->xor_line(position1.X, position1.Y, position1.X+width, position1.Y); - if(height>0){ - ren->xor_line(position1.X, position1.Y+height, position1.X+width, position1.Y+height); - if(height>1){ - ren->xor_line(position1.X+width, position1.Y+1, position1.X+width, position1.Y+height-1); - if(width>0) - ren->xor_line(position1.X, position1.Y+1, position1.X, position1.Y+height-1); - } - } -} - -void Brush::RenderLine(Renderer * ren, ui::Point position1, ui::Point position2) -{ - ren->xor_line(position1.X, position1.Y, position2.X, position2.Y); -} - -void Brush::RenderPoint(Renderer * ren, ui::Point position) -{ - if(!outline) - updateOutline(); - if(!outline) - return; - ren->xor_bitmap(outline, position.X-radius.X, position.Y-radius.Y, size.X, size.Y); -} - -void Brush::RenderFill(Renderer * ren, ui::Point position) -{ - ren->xor_line(position.X-5, position.Y, position.X-1, position.Y); - ren->xor_line(position.X+5, position.Y, position.X+1, position.Y); - ren->xor_line(position.X, position.Y-5, position.X, position.Y-1); - ren->xor_line(position.X, position.Y+5, position.X, position.Y+1); -} diff --git a/src/game/Brush.h b/src/game/Brush.h deleted file mode 100644 index 7671caf..0000000 --- a/src/game/Brush.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef BRUSH_H_ -#define BRUSH_H_ - -#include -#include "interface/Point.h" - -class Renderer; -class Brush -{ -protected: - unsigned char * outline; - unsigned char * bitmap; - ui::Point size; - ui::Point radius; - void updateOutline() - { - if(!bitmap) - GenerateBitmap(); - if(!bitmap) - return; - if(outline) - delete[] outline; - outline = new unsigned char[size.X*size.Y]; - for(int x = 0; x < size.X; x++) - { - for(int y = 0; y < size.Y; y++) - { - if(bitmap[y*size.X+x] && (!y || !x || x == size.X-1 || y == size.Y-1 || !bitmap[y*size.X+(x+1)] || !bitmap[y*size.X+(x-1)] || !bitmap[(y-1)*size.X+x] || !bitmap[(y+1)*size.X+x])) - { - outline[y*size.X+x] = 255; - } - else - outline[y*size.X+x] = 0; - } - } - } -public: - Brush(ui::Point size_): - bitmap(NULL), - outline(NULL), - radius(0, 0), - size(0, 0) - { - SetRadius(size_); - }; - - //Radius of the brush 0x0 - infxinf (Radius of 0x0 would be 1x1, radius of 1x1 would be 3x3) - ui::Point GetRadius() - { - return radius; - } - - //Size of the brush bitmap mask, 1x1 - infxinf - ui::Point GetSize() - { - return size; - } - virtual void SetRadius(ui::Point radius) - { - this->radius = radius; - this->size = radius+radius+ui::Point(1, 1); - - GenerateBitmap(); - updateOutline(); - } - virtual ~Brush() { - if(bitmap) - delete[] bitmap; - if(outline) - delete[] outline; - } - virtual void RenderRect(Renderer * ren, ui::Point position1, ui::Point position2); - virtual void RenderLine(Renderer * ren, ui::Point position1, ui::Point position2); - virtual void RenderPoint(Renderer * ren, ui::Point position); - virtual void RenderFill(Renderer * ren, ui::Point position); - virtual void GenerateBitmap() - { - if(bitmap) - delete[] bitmap; - bitmap = new unsigned char[size.X*size.Y]; - for(int x = 0; x < size.X; x++) - { - for(int y = 0; y < size.Y; y++) - { - bitmap[(y*size.X)+x] = 255; - } - } - } - //Get a bitmap for drawing particles - unsigned char * GetBitmap() - { - if(!bitmap) - GenerateBitmap(); - return bitmap; - } - - unsigned char * GetOutline() - { - if(!outline) - updateOutline(); - if(!outline) - return NULL; - return outline; - } -}; - - -#endif /* BRUSH_H_ */ diff --git a/src/game/DecorationTool.h b/src/game/DecorationTool.h deleted file mode 100644 index 679c854..0000000 --- a/src/game/DecorationTool.h +++ /dev/null @@ -1,43 +0,0 @@ - -#ifndef DECORATIONTOOL_H_ -#define DECORATIONTOOL_H_ - -#include "Tool.h" - -class DecorationTool: public Tool -{ -public: - enum ToolType { BlendAdd = DECO_ADD, BlendRemove = DECO_SUBTRACT, BlendMultiply = DECO_MULTIPLY, BlendDivide = DECO_DIVIDE, BlendSet = DECO_DRAW, BlendSmudge = DECO_SMUDGE, Remove = DECO_CLEAR }; - - ToolType decoMode; - - unsigned char Red; - unsigned char Green; - unsigned char Blue; - unsigned char Alpha; - - DecorationTool(ToolType decoMode_, string name, string description, int r, int g, int b, std::string identifier): - Tool(0, name, description, r, g, b, identifier), - decoMode(decoMode_), - Red(0), - Green(0), - Blue(0), - Alpha(0) - { - } - virtual ~DecorationTool() {} - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position){ - sim->ApplyDecorationPoint(position.X, position.Y, Red, Green, Blue, Alpha, decoMode, brush); - } - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - sim->ApplyDecorationLine(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode, brush); - } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->ApplyDecorationBox(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode); - } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { - - } -}; - -#endif diff --git a/src/game/EllipseBrush.h b/src/game/EllipseBrush.h deleted file mode 100644 index ad28766..0000000 --- a/src/game/EllipseBrush.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef ELIPSEBRUSH_H_ -#define ELIPSEBRUSH_H_ - -#include -#include "Brush.h" - -class EllipseBrush: public Brush -{ -public: - EllipseBrush(ui::Point size_): - Brush(size_) - { - SetRadius(size_); - }; - virtual void GenerateBitmap() - { - if(bitmap) - delete[] bitmap; - bitmap = new unsigned char[size.X*size.Y]; - int rx = radius.X; - int ry = radius.Y; - - if (!rx) - { - for (int j = 0; j <= 2*ry; j++) - { - bitmap[j*(size.X)+rx] = 255; - } - } - else - { - int yTop = ry, yBottom, i, j; - for (i = 0; i <= rx; i++) - { - while (pow(i-rx,2.0f)*pow(ry,2.0f) + pow(yTop-ry,2.0f)*pow(rx,2.0f) <= pow(rx,2.0f)*pow(ry,2.0f)) - yTop++; - yBottom = 2*ry - yTop; - for (int j = 0; j <= ry*2; j++) - { - if (j > yBottom && j < yTop) - { - bitmap[j*(size.X)+i] = 255; - bitmap[j*(size.X)+2*rx-i] = 255; - } - else - { - bitmap[j*(size.X)+i] = 0; - bitmap[j*(size.X)+2*rx-i] = 0; - } - } - } - } - } -}; - -#endif /* ELIPSEBRUSH_H_ */ diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp deleted file mode 100644 index 1493b39..0000000 --- a/src/game/GameController.cpp +++ /dev/null @@ -1,1426 +0,0 @@ - -#include -#include -#include "Config.h" -#include "Format.h" -#include "GameController.h" -#include "GameModel.h" -#include "client/SaveInfo.h" -#include "client/GameSave.h" -#include "search/SearchController.h" -#include "render/RenderController.h" -#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" -#include "elementsearch/ElementSearchActivity.h" -#include "profile/ProfileActivity.h" -#include "colourpicker/ColourPickerActivity.h" -#include "update/UpdateActivity.h" -#include "Notification.h" -#include "filebrowser/FileBrowserActivity.h" -#include "save/LocalSaveActivity.h" -#include "save/ServerSaveActivity.h" -#include "interface/Keys.h" -#include "simulation/Snapshot.h" -#include "debug/DebugInfo.h" -//#include "debug/ElementPopulation.h" - -using namespace std; - -class GameController::SearchCallback: public ControllerCallback -{ - GameController * cc; -public: - SearchCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - if(cc->search->GetLoadedSave()) - { - try - { - cc->gameModel->SetSave(cc->search->GetLoadedSave()); - cc->search->ReleaseLoadedSave(); - } - catch(GameModelException & ex) - { - new ErrorMessage("Cannot open save", ex.what()); - } - } - } -}; - -class GameController::SaveOpenCallback: public ControllerCallback -{ - GameController * cc; -public: - SaveOpenCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - if(cc->activePreview->GetDoOpen() && cc->activePreview->GetSave()) - { - try - { - cc->LoadSave(cc->activePreview->GetSave()); - } - catch(GameModelException & ex) - { - new ErrorMessage("Cannot open save", ex.what()); - } - } - } -}; - - -class GameController::RenderCallback: public ControllerCallback -{ - GameController * cc; -public: - RenderCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - //cc->gameModel->SetUser(cc->loginWindow->GetUser()); - } -}; - -class GameController::OptionsCallback: public ControllerCallback -{ - GameController * cc; -public: - OptionsCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - cc->gameModel->UpdateQuickOptions(); - //cc->gameModel->SetUser(cc->loginWindow->GetUser()); - } -}; - -class GameController::TagsCallback: public ControllerCallback -{ - GameController * cc; -public: - TagsCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - cc->gameView->NotifySaveChanged(cc->gameModel); - } -}; - -class GameController::StampsCallback: public ControllerCallback -{ - GameController * cc; -public: - StampsCallback(GameController * cc_) { cc = cc_; } - virtual void ControllerExit() - { - if(cc->localBrowser->GetSave()) - { - cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave()); - if (cc->localBrowser->GetMoveToFront()) - Client::Ref().MoveStampToFront(cc->localBrowser->GetSave()->GetName()); - cc->LoadStamp(); - } - } -}; - -GameController::GameController(): - search(NULL), - renderOptions(NULL), - loginWindow(NULL), - console(NULL), - tagsWindow(NULL), - options(NULL), - activePreview(NULL), - localBrowser(NULL), - HasDone(false), - firstTick(true) -{ - gameView = new GameView(); - gameModel = new GameModel(); - gameModel->BuildQuickOptionMenu(this); - - gameView->AttachController(this); - gameModel->AddObserver(gameView); - - commandInterface = new LuaScriptInterface(this, gameModel);//new TPTScriptInterface(); - ((LuaScriptInterface*)commandInterface)->SetWindow(gameView); - - commandInterface->OnBrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().X); - ActiveToolChanged(0, gameModel->GetActiveTool(0)); - ActiveToolChanged(1, gameModel->GetActiveTool(1)); - ActiveToolChanged(2, gameModel->GetActiveTool(2)); - - //sim = new Simulation(); - Client::Ref().AddListener(this); - - //debugInfo.push_back(new ElementPopulationDebug(gameModel->GetSimulation())); -} - -GameController::~GameController() -{ - if(search) - { - delete search; - } - if(renderOptions) - { - delete renderOptions; - } - if(loginWindow) - { - delete loginWindow; - } - if(tagsWindow) - { - delete tagsWindow; - } - if(console) - { - delete console; - } - if(activePreview) - { - delete activePreview; - } - if(localBrowser) - { - delete localBrowser; - } - if (options) - { - delete options; - } - if(ui::Engine::Ref().GetWindow() == gameView) - { - ui::Engine::Ref().CloseWindow(); - } - //deleted here because it refuses to be deleted when deleted from gameModel even with the same code - std::deque history = gameModel->GetHistory(); - for(std::deque::iterator iter = history.begin(), end = history.end(); iter != end; ++iter) - { - delete *iter; - } - std::vector quickOptions = gameModel->GetQuickOptions(); - for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) - { - delete *iter; - } - std::vector notifications = gameModel->GetNotifications(); - for(std::vector::iterator iter = notifications.begin(); iter != notifications.end(); ++iter) - { - delete *iter; - } - delete gameModel; - delete gameView; -} - -void GameController::HistoryRestore() -{ - std::deque history = gameModel->GetHistory(); - if(history.size()) - { - Snapshot * snap = history.back(); - gameModel->GetSimulation()->Restore(*snap); - if(history.size()>1) - { - history.pop_back(); - delete snap; - gameModel->SetHistory(history); - } - } -} - -void GameController::HistorySnapshot() -{ - std::deque history = gameModel->GetHistory(); - Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); - if(newSnap) - { - if(history.size() >= 1) //History limit is current 1 - { - Snapshot * snap = history.front(); - history.pop_front(); - //snap->Particles.clear(); - delete snap; - } - history.push_back(newSnap); - gameModel->SetHistory(history); - } -} - -GameView * GameController::GetView() -{ - return gameView; -} - -void GameController::PlaceSave(ui::Point position) -{ - if(gameModel->GetPlaceSave()) - { - gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetPlaceSave()); - gameModel->SetPaused(gameModel->GetPlaceSave()->paused | gameModel->GetPaused()); - } -} - -void GameController::Install() -{ -#if defined(MACOSX) - new InformationMessage("No Installation necessary", "You don't need to install The Powder Toy on Mac OS X", false); -#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!", false); - } - else - { - new ErrorMessage("Could not install", "The installation did not complete due to an error"); - } - } - } - virtual ~InstallConfirmation() { } - }; - new ConfirmPrompt("Install The Powder Toy", "Do you wish to install The Powder Toy on this computer?\nThis allows you to open save files and saves directly from the website.", new InstallConfirmation(this)); -#else - new ErrorMessage("Cannot install", "You cannot install The Powder Toy on this platform"); -#endif -} - -void GameController::AdjustGridSize(int direction) -{ - if(direction > 0) - gameModel->GetRenderer()->SetGridSize((gameModel->GetRenderer()->GetGridSize()+1)%10); - else - gameModel->GetRenderer()->SetGridSize((gameModel->GetRenderer()->GetGridSize()+9)%10); -} - -void GameController::InvertAirSim() -{ - gameModel->GetSimulation()->air->Invert(); -} - - -void GameController::AdjustBrushSize(int direction, bool logarithmic, bool xAxis, bool yAxis) -{ - if(xAxis && yAxis) - return; - - ui::Point newSize(0, 0); - ui::Point oldSize = gameModel->GetBrush()->GetRadius(); - if(logarithmic) - newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction * ((gameModel->GetBrush()->GetRadius().X/5)>0?gameModel->GetBrush()->GetRadius().X/5:1), direction * ((gameModel->GetBrush()->GetRadius().Y/5)>0?gameModel->GetBrush()->GetRadius().Y/5:1)); - else - newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction, direction); - if(newSize.X < 0) - newSize.X = 0; - if(newSize.Y < 0) - newSize.Y = 0; - if(newSize.X > 200) - newSize.X = 200; - if(newSize.Y > 200) - newSize.Y = 200; - - if(xAxis) - gameModel->GetBrush()->SetRadius(ui::Point(newSize.X, oldSize.Y)); - else if(yAxis) - gameModel->GetBrush()->SetRadius(ui::Point(oldSize.X, newSize.Y)); - else - gameModel->GetBrush()->SetRadius(newSize); - - BrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().Y); -} - -void GameController::AdjustZoomSize(int direction, bool logarithmic) -{ - int newSize; - if(logarithmic) - newSize = gameModel->GetZoomSize()+(((gameModel->GetZoomSize()/10)>0?(gameModel->GetZoomSize()/10):1)*direction); - else - newSize = gameModel->GetZoomSize()+direction; - if(newSize<5) - newSize = 5; - if(newSize>64) - newSize = 64; - gameModel->SetZoomSize(newSize); - - int newZoomFactor = 256/newSize; - if(newZoomFactor<3) - newZoomFactor = 3; - gameModel->SetZoomFactor(newZoomFactor); -} - -ui::Point GameController::PointTranslate(ui::Point point) -{ - if(point.X >= XRES) - point.X = XRES-1; - if(point.Y >= YRES) - point.Y = YRES-1; - if(point.Y < 0) - point.Y = 0; - if(point.X < 0) - point.X = 0; - - bool zoomEnabled = gameModel->GetZoomEnabled(); - if(!zoomEnabled) - return point; - //If we try to draw inside the zoom window, normalise the coordinates - int zoomFactor = gameModel->GetZoomFactor(); - ui::Point zoomWindowPosition = gameModel->GetZoomWindowPosition(); - ui::Point zoomWindowSize = ui::Point(gameModel->GetZoomSize()*zoomFactor, gameModel->GetZoomSize()*zoomFactor); - - if(point.X >= zoomWindowPosition.X && point.X >= zoomWindowPosition.Y && point.X <= zoomWindowPosition.X+zoomWindowSize.X && point.Y <= zoomWindowPosition.Y+zoomWindowSize.Y) - return ((point-zoomWindowPosition)/gameModel->GetZoomFactor())+gameModel->GetZoomPosition(); - return point; -} - -ui::Point GameController::NormaliseBlockCoord(ui::Point point) -{ - return (point/CELL)*CELL; -} - -void GameController::DrawRect(int toolSelection, ui::Point point1, ui::Point point2) -{ - Simulation * sim = gameModel->GetSimulation(); - Tool * activeTool = gameModel->GetActiveTool(toolSelection); - gameModel->SetLastTool(activeTool); - Brush * cBrush = gameModel->GetBrush(); - if(!activeTool || !cBrush) - return; - activeTool->SetStrength(gameModel->GetToolStrength()); - activeTool->DrawRect(sim, cBrush, point1, point2); -} - -void GameController::DrawLine(int toolSelection, ui::Point point1, ui::Point point2) -{ - Simulation * sim = gameModel->GetSimulation(); - Tool * activeTool = gameModel->GetActiveTool(toolSelection); - gameModel->SetLastTool(activeTool); - Brush * cBrush = gameModel->GetBrush(); - if(!activeTool || !cBrush) - return; - activeTool->SetStrength(gameModel->GetToolStrength()); - activeTool->DrawLine(sim, cBrush, point1, point2); -} - -void GameController::DrawFill(int toolSelection, ui::Point point) -{ - Simulation * sim = gameModel->GetSimulation(); - Tool * activeTool = gameModel->GetActiveTool(toolSelection); - gameModel->SetLastTool(activeTool); - Brush * cBrush = gameModel->GetBrush(); - if(!activeTool || !cBrush) - return; - activeTool->SetStrength(gameModel->GetToolStrength()); - activeTool->DrawFill(sim, cBrush, point); -} - -void GameController::DrawPoints(int toolSelection, queue & pointQueue) -{ - Simulation * sim = gameModel->GetSimulation(); - Tool * activeTool = gameModel->GetActiveTool(toolSelection); - gameModel->SetLastTool(activeTool); - Brush * cBrush = gameModel->GetBrush(); - if(!activeTool || !cBrush) - { - if(!pointQueue.empty()) - { - while(!pointQueue.empty()) - { - //delete pointQueue.front(); - pointQueue.pop(); - } - } - return; - } - - activeTool->SetStrength(gameModel->GetToolStrength()); - if(!pointQueue.empty()) - { - ui::Point sPoint(0, 0); - bool first = true; - while(!pointQueue.empty()) - { - ui::Point fPoint = pointQueue.front(); - //delete pointQueue.front(); - pointQueue.pop(); - if(!first) - { - activeTool->DrawLine(sim, cBrush, sPoint, fPoint, true); - } - else - { - first = false; - activeTool->Draw(sim, cBrush, fPoint); - } - sPoint = fPoint; - } - } -} - -void GameController::LoadClipboard() -{ - gameModel->SetPlaceSave(gameModel->GetClipboard()); - if(gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) - gameModel->GetPlaceSave()->Expand(); -} - -void GameController::LoadStamp() -{ - gameModel->SetPlaceSave(gameModel->GetStamp()); - if(gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) - gameModel->GetPlaceSave()->Expand(); -} - -void GameController::TranslateSave(ui::Point point) -{ - matrix2d transform = m2d_identity; - vector2d translate = v2d_new(point.X, point.Y); - gameModel->GetPlaceSave()->Transform(transform, translate); - gameModel->SetPlaceSave(gameModel->GetPlaceSave()); -} - -void GameController::TransformSave(matrix2d transform) -{ - vector2d translate = v2d_zero; - gameModel->GetPlaceSave()->Transform(transform, translate); - gameModel->SetPlaceSave(gameModel->GetPlaceSave()); -} - -void GameController::ToolClick(int toolSelection, ui::Point point) -{ - Simulation * sim = gameModel->GetSimulation(); - Tool * activeTool = gameModel->GetActiveTool(toolSelection); - Brush * cBrush = gameModel->GetBrush(); - if(!activeTool || !cBrush) - return; - activeTool->Click(sim, cBrush, point); -} - -void GameController::StampRegion(ui::Point point1, ui::Point point2) -{ - GameSave * newSave; - newSave = gameModel->GetSimulation()->Save(point1.X, point1.Y, point2.X, point2.Y); - if(newSave) - { - newSave->paused = gameModel->GetPaused(); - gameModel->AddStamp(newSave); - } - else - new ErrorMessage("Could not create stamp", "Error generating save file"); -} - -void GameController::CopyRegion(ui::Point point1, ui::Point point2) -{ - GameSave * newSave; - newSave = gameModel->GetSimulation()->Save(point1.X, point1.Y, point2.X, point2.Y); - if(newSave) - { - newSave->paused = gameModel->GetPaused(); - gameModel->SetClipboard(newSave); - } -} - -void GameController::CutRegion(ui::Point point1, ui::Point point2) -{ - CopyRegion(point1, point2); - gameModel->GetSimulation()->clear_area(point1.X, point1.Y, point2.X-point1.X, point2.Y-point1.Y); -} - -bool GameController::MouseMove(int x, int y, int dx, int dy) -{ - return commandInterface->OnMouseMove(x, y, dx, dy); -} - -bool GameController::BrushChanged(int brushType, int rx, int ry) -{ - return commandInterface->OnBrushChanged(brushType, rx, ry); -} - -bool GameController::MouseDown(int x, int y, unsigned button) -{ - return commandInterface->OnMouseDown(x, y, button); -} - -bool GameController::MouseUp(int x, int y, unsigned button) -{ - bool ret = commandInterface->OnMouseUp(x, y, button); - ui::Point point = PointTranslate(ui::Point(x, y)); - x = point.X; - y = point.Y; - if(ret && yGetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking - { - Simulation * sim = gameModel->GetSimulation(); - for (std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) - { - int signx, signy, signw, signh; - (*iter).pos((*iter).getText(sim), signx, signy, signw, signh); - if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh) - { - if (sregexp((*iter).text.c_str(), "^{[c|t]:[0-9]*|.*}$")==0) - { - const char * signText = (*iter).text.c_str(); - char buff[256]; - int sldr; - - memset(buff, 0, sizeof(buff)); - - for (sldr=3; signText[sldr] != '|'; sldr++) - buff[sldr-3] = signText[sldr]; - - buff[sldr-3] = '\0'; - - int tempSaveID = format::StringToNumber(std::string(buff)); - if (tempSaveID) - { - if ((*iter).text.c_str()[1] == 'c') - OpenSavePreview(tempSaveID, 0); - else if ((*iter).text.c_str()[1] == 't') - { - char url[256]; - sprintf(url, "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=%i", tempSaveID); - OpenURI(url); - } - } - break; - } - } - } - } - } - return ret; -} - -bool GameController::MouseWheel(int x, int y, int d) -{ - return commandInterface->OnMouseWheel(x, y, d); -} - -bool GameController::KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - bool ret = commandInterface->OnKeyPress(key, character, shift, ctrl, alt); - if(ret) - { - Simulation * sim = gameModel->GetSimulation(); - if (key == KEY_RIGHT) - { - sim->player.comm = (int)(sim->player.comm)|0x02; //Go right command - } - if (key == KEY_LEFT) - { - sim->player.comm = (int)(sim->player.comm)|0x01; //Go left command - } - if (key == KEY_DOWN && ((int)(sim->player.comm)&0x08)!=0x08) - { - sim->player.comm = (int)(sim->player.comm)|0x08; //Use element command - } - if (key == KEY_UP && ((int)(sim->player.comm)&0x04)!=0x04) - { - sim->player.comm = (int)(sim->player.comm)|0x04; //Jump command - } - - if (key == KEY_d) - { - sim->player2.comm = (int)(sim->player2.comm)|0x02; //Go right command - } - if (key == KEY_a) - { - sim->player2.comm = (int)(sim->player2.comm)|0x01; //Go left command - } - if (key == KEY_s && ((int)(sim->player2.comm)&0x08)!=0x08) - { - sim->player2.comm = (int)(sim->player2.comm)|0x08; //Use element command - } - if (key == KEY_w && ((int)(sim->player2.comm)&0x04)!=0x04) - { - sim->player2.comm = (int)(sim->player2.comm)|0x04; //Jump command - } - - if((!sim->elementCount[PT_STKM2] || ctrl) && gameView->GetSelectMode() == SelectNone) - { - switch(key) - { - case 'w': - SwitchGravity(); - break; - case 'd': - gameView->ToggleDebug(); - break; - case 's': - gameView->BeginStampSelection(); - break; - } - } - } - return ret; -} - -bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - bool ret = commandInterface->OnKeyRelease(key, character, shift, ctrl, alt); - if(ret) - { - Simulation * sim = gameModel->GetSimulation(); - if (key == KEY_RIGHT || key == KEY_LEFT) - { - sim->player.pcomm = sim->player.comm; //Saving last movement - sim->player.comm = (int)(sim->player.comm)&12; //Stop command - } - if (key == KEY_UP) - { - sim->player.comm = (int)(sim->player.comm)&11; - } - if (key == KEY_DOWN) - { - sim->player.comm = (int)(sim->player.comm)&7; - } - - if (key == KEY_d || key == KEY_a) - { - sim->player2.pcomm = sim->player2.comm; //Saving last movement - sim->player2.comm = (int)(sim->player2.comm)&12; //Stop command - } - if (key == KEY_w) - { - sim->player2.comm = (int)(sim->player2.comm)&11; - } - if (key == KEY_s) - { - sim->player2.comm = (int)(sim->player2.comm)&7; - } - } - return ret; -} - -void GameController::Tick() -{ - if(firstTick) - { - ((LuaScriptInterface*)commandInterface)->Init(); - if(!Client::Ref().GetPrefBool("InstallCheck", false)) - { - Client::Ref().SetPref("InstallCheck", true); - Install(); - } - firstTick = false; - } - for(std::vector::iterator iter = debugInfo.begin(), end = debugInfo.end(); iter != end; iter++) - { - (*iter)->Draw(ui::Point(10, 10)); - } - commandInterface->OnTick(); -} - -void GameController::Exit() -{ - if(ui::Engine::Ref().GetWindow() == gameView) - ui::Engine::Ref().CloseWindow(); - HasDone = true; -} - -void GameController::ResetAir() -{ - Simulation * sim = gameModel->GetSimulation(); - sim->air->Clear(); - for (int i = 0; i < NPART; i++) - { - if (sim->parts[i].type == PT_QRTZ || sim->parts[i].type == PT_GLAS) - { - sim->parts[i].pavg[0] = sim->parts[i].pavg[1] = 0; - } - } -} - -void GameController::ResetSpark() -{ - Simulation * sim = gameModel->GetSimulation(); - for (int i = 0; i < NPART; i++) - if (sim->parts[i].type == PT_SPRK) - { - if (sim->parts[i].ctype >= 0 && sim->parts[i].ctype < PT_NUM && sim->elements[sim->parts[i].ctype].Enabled) - { - sim->parts[i].type = sim->parts[i].ctype; - sim->parts[i].life = 0; - } - else - sim->kill_part(i); - } -} - -void GameController::SwitchGravity() -{ - gameModel->GetSimulation()->gravityMode = (gameModel->GetSimulation()->gravityMode+1)%3; - - switch (gameModel->GetSimulation()->gravityMode) - { - case 0: - gameModel->SetInfoTip("Gravity: Vertical"); - break; - case 1: - gameModel->SetInfoTip("Gravity: Off"); - break; - case 2: - gameModel->SetInfoTip("Gravity: Radial"); - break; - } -} - -void GameController::SwitchAir() -{ - gameModel->GetSimulation()->air->airMode = (gameModel->GetSimulation()->air->airMode+1)%5; - - switch (gameModel->GetSimulation()->air->airMode) - { - case 0: - gameModel->SetInfoTip("Air: On"); - break; - case 1: - gameModel->SetInfoTip("Air: Pressure Off"); - break; - case 2: - gameModel->SetInfoTip("Air: Velocity Off"); - break; - case 3: - gameModel->SetInfoTip("Air: Off"); - break; - case 4: - gameModel->SetInfoTip("Air: No Update"); - break; - } -} - -void GameController::ToggleAHeat() -{ - gameModel->SetAHeatEnable(!gameModel->GetAHeatEnable()); -} - - -void GameController::LoadRenderPreset(int presetNum) -{ - Renderer * renderer = gameModel->GetRenderer(); - RenderPreset preset = renderer->renderModePresets[presetNum]; - gameModel->SetInfoTip(preset.Name); - renderer->SetRenderMode(preset.RenderModes); - renderer->SetDisplayMode(preset.DisplayModes); - renderer->SetColourMode(preset.ColourMode); -} - -void GameController::Update() -{ - ui::Point pos = gameView->GetMousePosition(); - if(pos.X >= 0 && pos.Y >= 0 && pos.X < XRES && pos.Y < YRES) - { - gameModel->GetRenderer()->mousePosX = pos.X; - gameModel->GetRenderer()->mousePosY = pos.Y; - gameView->SetSample(gameModel->GetSimulation()->Get(pos.X, pos.Y)); - } - - gameModel->GetSimulation()->update_particles(); - if(renderOptions && renderOptions->HasExited) - { - delete renderOptions; - renderOptions = NULL; - } - - if(search && search->HasExited) - { - delete search; - search = NULL; - } - - if(activePreview && activePreview->HasExited) - { - delete activePreview; - activePreview = NULL; - } - - if(loginWindow && loginWindow->HasExited) - { - delete loginWindow; - loginWindow = NULL; - } - - if(localBrowser && localBrowser->HasDone) - { - delete localBrowser; - localBrowser = NULL; - } -} - -void GameController::SetZoomEnabled(bool zoomEnabled) -{ - gameModel->SetZoomEnabled(zoomEnabled); -} - -void GameController::SetToolStrength(float value) -{ - gameModel->SetToolStrength(value); -} - -void GameController::SetZoomPosition(ui::Point position) -{ - ui::Point zoomPosition = position-(gameModel->GetZoomSize()/2); - if(zoomPosition.X < 0) - zoomPosition.X = 0; - if(zoomPosition.Y < 0) - zoomPosition.Y = 0; - if(zoomPosition.X >= XRES-gameModel->GetZoomSize()) - zoomPosition.X = XRES-gameModel->GetZoomSize(); - if(zoomPosition.Y >= YRES-gameModel->GetZoomSize()) - zoomPosition.Y = YRES-gameModel->GetZoomSize(); - - ui::Point zoomWindowPosition = ui::Point(0, 0); - if(position.X < XRES/2) - zoomWindowPosition.X = XRES-(gameModel->GetZoomSize()*gameModel->GetZoomFactor()); - - gameModel->SetZoomPosition(zoomPosition); - gameModel->SetZoomWindowPosition(zoomWindowPosition); -} - -void GameController::SetPaused(bool pauseState) -{ - gameModel->SetPaused(pauseState); -} - -void GameController::SetPaused() -{ - gameModel->SetPaused(!gameModel->GetPaused()); -} - -void GameController::SetDecoration(bool decorationState) -{ - gameModel->SetDecoration(decorationState); -} - -void GameController::SetDecoration() -{ - gameModel->SetDecoration(!gameModel->GetDecoration()); -} - -void GameController::ShowGravityGrid() -{ - gameModel->ShowGravityGrid(!gameModel->GetGravityGrid()); - gameModel->UpdateQuickOptions(); -} - -void GameController::SetHudEnable(bool hudState) -{ - gameView->SetHudEnable(hudState); -} - -void GameController::SetActiveColourPreset(int preset) -{ - gameModel->SetActiveColourPreset(preset); -} - -void GameController::SetColour(ui::Colour colour) -{ - gameModel->SetColourSelectorColour(colour); - gameModel->SetPresetColour(colour); -} - -void GameController::SetActiveMenu(Menu * menu) -{ - gameModel->SetActiveMenu(menu); - vector menuList = gameModel->GetMenuList(); - bool set = false; - for(int i = 0; i < menuList.size(); i++) - { - if(menuList[i]==menu && i == SC_DECO) - { - gameModel->SetColourSelectorVisibility(true); - set = true; - } - } - if(!set) - gameModel->SetColourSelectorVisibility(false); -} - -std::vector GameController::GetMenuList() -{ - return gameModel->GetMenuList(); -} - -void GameController::ActiveToolChanged(int toolSelection, Tool *tool) -{ - commandInterface->OnActiveToolChanged(toolSelection, tool); -} - -void GameController::SetActiveTool(int toolSelection, Tool * tool) -{ - gameModel->SetActiveTool(toolSelection, tool); - gameModel->GetRenderer()->gravityZonesEnabled = false; - gameModel->SetLastTool(tool); - for(int i = 0; i < 3; i++) - { - if(gameModel->GetActiveTool(i) == gameModel->GetMenuList().at(SC_WALL)->GetToolList().at(WL_GRAV)) - { - gameModel->GetRenderer()->gravityZonesEnabled = true; - } - } -} - -void GameController::OpenSearch() -{ - if(!search) - search = new SearchController(new SearchCallback(this)); - ui::Engine::Ref().ShowWindow(search->GetView()); -} - -void GameController::OpenLocalSaveWindow(bool asCurrent) -{ - Simulation * sim = gameModel->GetSimulation(); - GameSave * gameSave = sim->Save(); - gameSave->paused = gameModel->GetPaused(); - gameSave->gravityMode = sim->gravityMode; - gameSave->airMode = sim->air->airMode; - gameSave->legacyEnable = sim->legacy_enable; - gameSave->waterEEnabled = sim->water_equal_test; - gameSave->gravityEnable = sim->grav->ngrav_enable; - if(!gameSave) - { - new ErrorMessage("Error", "Unable to build save."); - } - else - { - std::string filename = ""; - if (gameModel->GetSaveFile()) - filename = gameModel->GetSaveFile()->GetDisplayName(); - SaveFile tempSave(filename); - tempSave.SetGameSave(gameSave); - - if (!asCurrent || !gameModel->GetSaveFile()) - { - class LocalSaveCallback: public FileSavedCallback - { - GameController * c; - public: - LocalSaveCallback(GameController * _c): c(_c) {} - virtual ~LocalSaveCallback() {}; - virtual void FileSaved(SaveFile* file) - { - c->gameModel->SetSaveFile(file); - } - }; - - new LocalSaveActivity(tempSave, new LocalSaveCallback(this)); - } - else if (gameModel->GetSaveFile()) - { - Client::Ref().MakeDirectory(LOCAL_SAVE_DIR); - Client::Ref().WriteFile(gameSave->Serialise(), gameModel->GetSaveFile()->GetName()); - } - } -} - -void GameController::LoadSaveFile(SaveFile * file) -{ - gameModel->SetSaveFile(file); -} - - -void GameController::LoadSave(SaveInfo * save) -{ - gameModel->SetSave(save); -} - -void GameController::OpenSavePreview(int saveID, int saveDate) -{ - activePreview = new PreviewController(saveID, new SaveOpenCallback(this)); - ui::Engine::Ref().ShowWindow(activePreview->GetView()); -} - -void GameController::OpenSavePreview() -{ - if(gameModel->GetSave()) - { - activePreview = new PreviewController(gameModel->GetSave()->GetID(), new SaveOpenCallback(this)); - ui::Engine::Ref().ShowWindow(activePreview->GetView()); - } -} - -void GameController::OpenLocalBrowse() -{ - class LocalSaveOpenCallback: public FileSelectedCallback - { - GameController * c; - public: - LocalSaveOpenCallback(GameController * _c): c(_c) {} - virtual ~LocalSaveOpenCallback() {}; - virtual void FileSelected(SaveFile* file) - { - c->LoadSaveFile(file); - delete file; - } - }; - new FileBrowserActivity(LOCAL_SAVE_DIR PATH_SEP, new LocalSaveOpenCallback(this)); -} - -void GameController::OpenLogin() -{ - if(Client::Ref().GetAuthUser().ID) - { - new ProfileActivity(Client::Ref().GetAuthUser().Username); - } - else - { - loginWindow = new LoginController(); - ui::Engine::Ref().ShowWindow(loginWindow->GetView()); - } -} - -void GameController::OpenElementSearch() -{ - vector toolList; - vector menuList = gameModel->GetMenuList(); - for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter!=end; ++iter) { - if(!(*iter)) - continue; - vector menuToolList = (*iter)->GetToolList(); - if(!menuToolList.size()) - continue; - toolList.insert(toolList.end(), menuToolList.begin(), menuToolList.end()); - } - vector hiddenTools = gameModel->GetUnlistedTools(); - toolList.insert(toolList.end(), hiddenTools.begin(), hiddenTools.end()); - new ElementSearchActivity(gameModel, toolList); -} - -void GameController::OpenColourPicker() -{ - class ColourPickerCallback: public ColourPickedCallback - { - GameController * c; - public: - ColourPickerCallback(GameController * _c): c(_c) {} - virtual ~ColourPickerCallback() {}; - virtual void ColourPicked(ui::Colour colour) - { - c->SetColour(colour); - } - }; - new ColourPickerActivity(gameModel->GetColourSelectorColour(), new ColourPickerCallback(this)); -} - -void GameController::OpenTags() -{ - if(gameModel->GetUser().ID) - { - if(gameModel->GetSave() && gameModel->GetSave()->GetID()) - { - tagsWindow = new TagsController(new TagsCallback(this), gameModel->GetSave()); - ui::Engine::Ref().ShowWindow(tagsWindow->GetView()); - } - else - { - new ErrorMessage("Error", "No save open"); - } - } - else - { - new ErrorMessage("Error", "You need to login to edit tags."); - } -} - -void GameController::OpenStamps() -{ - localBrowser = new LocalBrowserController(new StampsCallback(this)); - ui::Engine::Ref().ShowWindow(localBrowser->GetView()); -} - -void GameController::OpenOptions() -{ - options = new OptionsController(gameModel, new OptionsCallback(this)); - ui::Engine::Ref().ShowWindow(options->GetView()); - -} - -void GameController::ShowConsole() -{ - if(!console) - console = new ConsoleController(NULL, commandInterface); - if (console->GetView() != ui::Engine::Ref().GetWindow()) - ui::Engine::Ref().ShowWindow(console->GetView()); -} - -void GameController::HideConsole() -{ - if(!console) - return; - if (console->GetView() == ui::Engine::Ref().GetWindow()) - ui::Engine::Ref().CloseWindow(); -} - -void GameController::OpenRenderOptions() -{ - renderOptions = new RenderController(gameModel->GetRenderer(), new RenderCallback(this)); - ui::Engine::Ref().ShowWindow(renderOptions->GetView()); -} - -void GameController::OpenSaveWindow() -{ - class SaveUploadedCallback: public ServerSaveActivity::SaveUploadedCallback - { - GameController * c; - public: - SaveUploadedCallback(GameController * _c): c(_c) {} - virtual ~SaveUploadedCallback() {}; - virtual void SaveUploaded(SaveInfo save) - { - c->LoadSave(&save); - } - }; - if(gameModel->GetUser().ID) - { - Simulation * sim = gameModel->GetSimulation(); - GameSave * gameSave = sim->Save(); - gameSave->paused = gameModel->GetPaused(); - gameSave->gravityMode = sim->gravityMode; - gameSave->airMode = sim->air->airMode; - gameSave->legacyEnable = sim->legacy_enable; - gameSave->waterEEnabled = sim->water_equal_test; - gameSave->gravityEnable = sim->grav->ngrav_enable; - if(!gameSave) - { - new ErrorMessage("Error", "Unable to build save."); - } - else - { - if(gameModel->GetSave()) - { - SaveInfo tempSave(*gameModel->GetSave()); - tempSave.SetGameSave(gameSave); - new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); - } - else - { - SaveInfo tempSave(0, 0, 0, 0, gameModel->GetUser().Username, ""); - tempSave.SetGameSave(gameSave); - new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); - } - } - } - else - { - new ErrorMessage("Error", "You need to login to upload saves."); - } -} - -void GameController::SaveAsCurrent() -{ - - class SaveUploadedCallback: public ServerSaveActivity::SaveUploadedCallback - { - GameController * c; - public: - SaveUploadedCallback(GameController * _c): c(_c) {} - virtual ~SaveUploadedCallback() {}; - virtual void SaveUploaded(SaveInfo save) - { - c->LoadSave(&save); - } - }; - - if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetUser().Username == gameModel->GetSave()->GetUserName()) - { - Simulation * sim = gameModel->GetSimulation(); - GameSave * gameSave = sim->Save(); - gameSave->paused = gameModel->GetPaused(); - gameSave->gravityMode = sim->gravityMode; - gameSave->airMode = sim->air->airMode; - gameSave->legacyEnable = sim->legacy_enable; - gameSave->waterEEnabled = sim->water_equal_test; - gameSave->gravityEnable = sim->grav->ngrav_enable; - if(!gameSave) - { - new ErrorMessage("Error", "Unable to build save."); - } - else - { - if(gameModel->GetSave()) - { - SaveInfo tempSave(*gameModel->GetSave()); - tempSave.SetGameSave(gameSave); - new ServerSaveActivity(tempSave, true, new SaveUploadedCallback(this)); - } - else - { - SaveInfo tempSave(0, 0, 0, 0, gameModel->GetUser().Username, ""); - tempSave.SetGameSave(gameSave); - new ServerSaveActivity(tempSave, true, new SaveUploadedCallback(this)); - } - } - } - else if(gameModel->GetUser().ID) - { - OpenSaveWindow(); - } - else - { - new ErrorMessage("Error", "You need to login to upload saves."); - } -} - -void GameController::FrameStep() -{ - gameModel->FrameStep(1); - gameModel->SetPaused(true); -} - -void GameController::Vote(int direction) -{ - if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetSave()->GetID() && gameModel->GetSave()->GetVote()==0) - { - try - { - gameModel->SetVote(direction); - } - catch(GameModelException & ex) - { - new ErrorMessage("Error while voting", ex.what()); - } - } -} - -void GameController::ChangeBrush() -{ - gameModel->SetBrush(gameModel->GetBrushID()+1); - BrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().Y); -} - -void GameController::ClearSim() -{ - gameModel->SetSave(NULL); - gameModel->ClearSimulation(); -} - -void GameController::ReloadSim() -{ - if(gameModel->GetSave() && gameModel->GetSave()->GetGameSave()) - { - gameModel->SetSave(gameModel->GetSave()); - } - else if(gameModel->GetSaveFile() && gameModel->GetSaveFile()->GetGameSave()) - { - gameModel->SetSaveFile(gameModel->GetSaveFile()); - } -} - -std::string GameController::ElementResolve(int type) -{ - if(gameModel && gameModel->GetSimulation() && gameModel->GetSimulation()->elements && type >= 0 && type < PT_NUM) - return std::string(gameModel->GetSimulation()->elements[type].Name); - else - return ""; -} - -std::string GameController::WallName(int type) -{ - if(gameModel && gameModel->GetSimulation() && gameModel->GetSimulation()->wtypes && type >= 0 && type < UI_WALLCOUNT) - return std::string(gameModel->GetSimulation()->wtypes[type].name); - else - return ""; -} - -void GameController::NotifyAuthUserChanged(Client * sender) -{ - User newUser = sender->GetAuthUser(); - gameModel->SetUser(newUser); -} - -void GameController::NotifyNewNotification(Client * sender, std::pair notification) -{ - class LinkNotification : public Notification - { - std::string link; - public: - LinkNotification(std::string link_, std::string message) : link(link_), Notification(message) {} - virtual ~LinkNotification() {} - - virtual void Action() - { - OpenURI(link); - } - }; - gameModel->AddNotification(new LinkNotification(notification.second, notification.first)); -} - -void GameController::NotifyUpdateAvailable(Client * sender) -{ - class UpdateConfirmation: public ConfirmDialogueCallback { - public: - GameController * c; - UpdateConfirmation(GameController * c_) { c = c_; } - virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { - if (result == ConfirmPrompt::ResultOkay) - { - c->RunUpdater(); - } - } - virtual ~UpdateConfirmation() { } - }; - - class UpdateNotification : public Notification - { - GameController * c; - public: - UpdateNotification(GameController * c, std::string message) : c(c), Notification(message) {} - virtual ~UpdateNotification() {} - - virtual void Action() - { - std::string currentVersion, newVersion; -#ifdef BETA - currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Beta, Build " MTOS(BUILD_NUM); -#elif defined(SNAPSHOT) - currentVersion = "Snapshot " MTOS(SNAPSHOT_ID); -#else - currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Stable, Build " MTOS(BUILD_NUM); -#endif - - UpdateInfo info = Client::Ref().GetUpdateInfo(); - if(info.Type == UpdateInfo::Beta) - newVersion = format::NumberToString(info.Major) + " " + format::NumberToString(info.Minor) + " Beta, Build " + format::NumberToString(info.Build); - else if(info.Type == UpdateInfo::Snapshot) - newVersion = "Snapshot " + format::NumberToString(info.Time); - else if(info.Type == UpdateInfo::Stable) - newVersion = format::NumberToString(info.Major) + " " + format::NumberToString(info.Minor) + " Stable, Build " + format::NumberToString(info.Build); - - new ConfirmPrompt("Run Updater", "Are you sure you want to run the updater, please save any changes before updating.\n\nCurrent version:\n " + currentVersion + "\nNew version:\n " + newVersion, new UpdateConfirmation(c)); - } - }; - - switch(sender->GetUpdateInfo().Type) - { - case UpdateInfo::Snapshot: - gameModel->AddNotification(new UpdateNotification(this, std::string("A new snapshot is available - click here to update"))); - break; - case UpdateInfo::Stable: - gameModel->AddNotification(new UpdateNotification(this, std::string("A new version is available - click here to update"))); - break; - case UpdateInfo::Beta: - gameModel->AddNotification(new UpdateNotification(this, std::string("A new beta is available - click here to update"))); - break; - } -} - -void GameController::RemoveNotification(Notification * notification) -{ - gameModel->RemoveNotification(notification); -} - -void GameController::RunUpdater() -{ - Exit(); - new UpdateActivity(); -} diff --git a/src/game/GameController.h b/src/game/GameController.h deleted file mode 100644 index 070d39d..0000000 --- a/src/game/GameController.h +++ /dev/null @@ -1,155 +0,0 @@ - #ifndef GAMECONTROLLER_H -#define GAMECONTROLLER_H - -#include -#include "GameView.h" -#include "GameModel.h" -#include "interface/Point.h" -#include "simulation/Simulation.h" -#include "search/SearchController.h" -#include "render/RenderController.h" -#include "preview/PreviewController.h" -#include "login/LoginController.h" -#include "tags/TagsController.h" -#include "console/ConsoleController.h" -#include "localbrowser/LocalBrowserController.h" -//#include "cat/TPTScriptInterface.h" -#include "cat/LuaScriptInterface.h" -#include "options/OptionsController.h" -#include "client/ClientListener.h" -#include "RenderPreset.h" -#include "Menu.h" - -using namespace std; - -class DebugInfo; -class Notification; -class GameModel; -class GameView; -class CommandInterface; -class ConsoleController; -class GameController: public ClientListener -{ -private: - //Simulation * sim; - bool firstTick; - int screenshotIndex; - PreviewController * activePreview; - GameView * gameView; - GameModel * gameModel; - SearchController * search; - RenderController * renderOptions; - LoginController * loginWindow; - ConsoleController * console; - TagsController * tagsWindow; - LocalBrowserController * localBrowser; - OptionsController * options; - CommandInterface * commandInterface; - vector debugInfo; -public: - bool HasDone; - class SearchCallback; - class RenderCallback; - class SSaveCallback; - class TagsCallback; - class StampsCallback; - class OptionsCallback; - class SaveOpenCallback; - friend class SaveOpenCallback; - GameController(); - ~GameController(); - GameView * GetView(); - - bool BrushChanged(int brushType, int rx, int ry); - bool MouseMove(int x, int y, int dx, int dy); - bool MouseDown(int x, int y, unsigned button); - bool MouseUp(int x, int y, unsigned button); - bool MouseWheel(int x, int y, int d); - bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); - bool KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); - void Tick(); - void Exit(); - - void Install(); - - void HistoryRestore(); - void HistorySnapshot(); - - void AdjustGridSize(int direction); - void InvertAirSim(); - void LoadRenderPreset(int presetNum); - void SetZoomEnabled(bool zoomEnable); - void SetZoomPosition(ui::Point position); - void AdjustBrushSize(int direction, bool logarithmic = false, bool xAxis = false, bool yAxis = false); - void AdjustZoomSize(int direction, bool logarithmic = false); - void ToolClick(int toolSelection, ui::Point point); - void DrawPoints(int toolSelection, queue & pointQueue); - void DrawRect(int toolSelection, ui::Point point1, ui::Point point2); - void DrawLine(int toolSelection, ui::Point point1, ui::Point point2); - void DrawFill(int toolSelection, ui::Point point); - void StampRegion(ui::Point point1, ui::Point point2); - void CopyRegion(ui::Point point1, ui::Point point2); - void CutRegion(ui::Point point1, ui::Point point2); - void Update(); - void SetPaused(bool pauseState); - void SetPaused(); - void SetDecoration(bool decorationState); - void SetDecoration(); - void ShowGravityGrid(); - void SetHudEnable(bool hudState); - void SetActiveMenu(Menu * menu); - std::vector GetMenuList(); - void SetActiveTool(int toolSelection, Tool * tool); - void ActiveToolChanged(int toolSelection, Tool *tool); - void SetActiveColourPreset(int preset); - void SetColour(ui::Colour colour); - void SetToolStrength(float value); - void LoadSaveFile(SaveFile * file); - void LoadSave(SaveInfo * save); - void OpenSearch(); - void OpenLogin(); - void OpenTags(); - void OpenSavePreview(int saveID, int saveDate); - void OpenSavePreview(); - void OpenLocalSaveWindow(bool asCurrent); - void OpenLocalBrowse(); - void OpenOptions(); - void OpenRenderOptions(); - void OpenSaveWindow(); - void SaveAsCurrent(); - void OpenStamps(); - void OpenElementSearch(); - void OpenColourPicker(); - void PlaceSave(ui::Point position); - void ClearSim(); - void ReloadSim(); - void Vote(int direction); - void ChangeBrush(); - void ShowConsole(); - void HideConsole(); - void FrameStep(); - void TranslateSave(ui::Point point); - void TransformSave(matrix2d transform); - ui::Point PointTranslate(ui::Point point); - ui::Point NormaliseBlockCoord(ui::Point point); - std::string ElementResolve(int type); - std::string WallName(int type); - - void ResetAir(); - void ResetSpark(); - void SwitchGravity(); - void SwitchAir(); - void ToggleAHeat(); - - void LoadClipboard(); - void LoadStamp(); - - void RemoveNotification(Notification * notification); - - virtual void NotifyUpdateAvailable(Client * sender); - virtual void NotifyAuthUserChanged(Client * sender); - virtual void NotifyNewNotification(Client * sender, std::pair notification); - void RunUpdater(); -}; - -#endif // GAMECONTROLLER_H diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp deleted file mode 100644 index e687674..0000000 --- a/src/game/GameModel.cpp +++ /dev/null @@ -1,1151 +0,0 @@ -#include "interface/Engine.h" -#include "GameModel.h" -#include "GameView.h" -#include "simulation/Simulation.h" -#include "simulation/Air.h" -#include "simulation/Tools.h" -#include "graphics/Renderer.h" -#include "interface/Point.h" -#include "Brush.h" -#include "EllipseBrush.h" -#include "TriangleBrush.h" -#include "BitmapBrush.h" -#include "client/Client.h" -#include "client/GameSave.h" -#include "game/DecorationTool.h" -#include "GameModelException.h" -#include "QuickOptions.h" -#include "Format.h" - -GameModel::GameModel(): - sim(NULL), - ren(NULL), - currentBrush(0), - currentUser(0, ""), - currentSave(NULL), - currentFile(NULL), - colourSelector(false), - clipboard(NULL), - stamp(NULL), - placeSave(NULL), - colour(255, 0, 0, 255), - toolStrength(1.0f), - activeColourPreset(-1), - activeMenu(NULL), - edgeMode(0) -{ - sim = new Simulation(); - ren = new Renderer(ui::Engine::Ref().g, sim); - - activeTools = regularToolset; - - std::fill(decoToolset, decoToolset+3, (Tool*)NULL); - std::fill(regularToolset, regularToolset+3, (Tool*)NULL); - - //Default render prefs - std::vector tempArray; - tempArray.push_back(RENDER_FIRE); - tempArray.push_back(RENDER_EFFE); - tempArray.push_back(RENDER_BASC); - ren->SetRenderMode(tempArray); - tempArray.clear(); - - ren->SetDisplayMode(tempArray); - - ren->SetColourMode(0); - - //Load config into renderer - try - { - ren->SetColourMode(Client::Ref().GetPrefUInteger("Renderer.ColourMode", 0)); - - vector tempArray = Client::Ref().GetPrefUIntegerArray("Renderer.DisplayModes"); - if(tempArray.size()) - { - std::vector displayModes(tempArray.begin(), tempArray.end()); - ren->SetDisplayMode(displayModes); - } - - tempArray = Client::Ref().GetPrefUIntegerArray("Renderer.RenderModes"); - if(tempArray.size()) - { - std::vector renderModes(tempArray.begin(), tempArray.end()); - ren->SetRenderMode(renderModes); - } - - ren->gravityFieldEnabled = Client::Ref().GetPrefBool("Renderer.GravityField", false); - ren->decorations_enable = Client::Ref().GetPrefBool("Renderer.Decorations", true); - } - catch(json::Exception & e) - { - } - - //Load config into simulation - edgeMode = Client::Ref().GetPrefInteger("Simulation.EdgeMode", 0); - sim->SetEdgeMode(edgeMode); - - //Load last user - if(Client::Ref().GetAuthUser().ID) - { - currentUser = Client::Ref().GetAuthUser(); - } - - //Set stamp to first stamp in list - vector stamps = Client::Ref().GetStamps(0, 1); - if(stamps.size()>0) - { - SaveFile * stampFile = Client::Ref().GetStamp(stamps[0]); - if(stampFile && stampFile->GetGameSave()) - stamp = stampFile->GetGameSave(); - } - - BuildMenus(); - - //Set default brush palette - brushList.push_back(new EllipseBrush(ui::Point(4, 4))); - brushList.push_back(new Brush(ui::Point(4, 4))); - brushList.push_back(new TriangleBrush(ui::Point(4, 4))); - - //Load more from brushes folder - std::vector brushFiles = Client::Ref().DirectorySearch(BRUSH_DIR, "", ".ptb"); - for(int i = 0; i < brushFiles.size(); i++) - { - std::vector brushData = Client::Ref().ReadFile(brushFiles[i]); - if(!brushData.size()) - { - std::cout << "Brushes: Skipping " << brushFiles[i] << ". Could not open" << std::endl; - continue; - } - int dimension = std::sqrt((float)brushData.size()); - if(dimension * dimension != brushData.size()) - { - std::cout << "Brushes: Skipping " << brushFiles[i] << ". Invalid bitmap size" << std::endl; - continue; - } - brushList.push_back(new BitmapBrush(brushData, ui::Point(dimension, dimension))); - } - - //Set default decoration colour - unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255); - unsigned char colourG = min(Client::Ref().GetPrefInteger("Decoration.Green", 100), 255); - unsigned char colourB = min(Client::Ref().GetPrefInteger("Decoration.Blue", 50), 255); - unsigned char colourA = min(Client::Ref().GetPrefInteger("Decoration.Alpha", 255), 255); - - SetColourSelectorColour(ui::Colour(colourR, colourG, colourB, colourA)); - - colourPresets.push_back(ui::Colour(255, 255, 255)); - colourPresets.push_back(ui::Colour(0, 255, 255)); - colourPresets.push_back(ui::Colour(255, 0, 255)); - colourPresets.push_back(ui::Colour(255, 255, 0)); - colourPresets.push_back(ui::Colour(255, 0, 0)); - colourPresets.push_back(ui::Colour(0, 255, 0)); - colourPresets.push_back(ui::Colour(0, 0, 255)); -} - -GameModel::~GameModel() -{ - //Save to config: - Client::Ref().SetPref("Renderer.ColourMode", ren->GetColourMode()); - - std::vector displayModes = ren->GetDisplayMode(); - Client::Ref().SetPref("Renderer.DisplayModes", std::vector(displayModes.begin(), displayModes.end())); - - std::vector renderModes = ren->GetRenderMode(); - Client::Ref().SetPref("Renderer.RenderModes", std::vector(renderModes.begin(), renderModes.end())); - - Client::Ref().SetPref("Renderer.GravityField", (bool)ren->gravityFieldEnabled); - Client::Ref().SetPref("Renderer.Decorations", (bool)ren->decorations_enable); - - Client::Ref().SetPref("Simulation.EdgeMode", sim->edgeMode); - - Client::Ref().SetPref("Decoration.Red", (int)colour.Red); - Client::Ref().SetPref("Decoration.Green", (int)colour.Green); - Client::Ref().SetPref("Decoration.Blue", (int)colour.Blue); - Client::Ref().SetPref("Decoration.Alpha", (int)colour.Alpha); - - for(int i = 0; i < menuList.size(); i++) - { - delete menuList[i]; - } - for(std::vector::iterator iter = extraElementTools.begin(), end = extraElementTools.end(); iter != end; ++iter) - { - delete *iter; - } - for(int i = 0; i < brushList.size(); i++) - { - delete brushList[i]; - } - delete sim; - delete ren; - if(placeSave) - delete placeSave; - if(clipboard) - delete clipboard; - if(stamp) - delete stamp; - if(currentSave) - delete currentSave; - if(currentFile) - delete currentFile; - //if(activeTools) - // delete[] activeTools; -} - -void GameModel::UpdateQuickOptions() -{ - for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) - { - QuickOption * option = *iter; - option->Update(); - } -} - -void GameModel::BuildQuickOptionMenu(GameController * controller) -{ - for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) - { - delete *iter; - } - quickOptions.clear(); - - quickOptions.push_back(new SandEffectOption(this)); - quickOptions.push_back(new DrawGravOption(this)); - quickOptions.push_back(new DecorationsOption(this)); - quickOptions.push_back(new NGravityOption(this)); - quickOptions.push_back(new AHeatOption(this)); - quickOptions.push_back(new ConsoleShowOption(this, controller)); - - notifyQuickOptionsChanged(); - UpdateQuickOptions(); -} - -void GameModel::BuildMenus() -{ - char lastMenu = 0; - if(activeMenu) - lastMenu = activeMenu->GetIcon(); - - std::string activeToolIdentifiers[3]; - if(regularToolset[0]) - activeToolIdentifiers[0] = regularToolset[0]->GetIdentifier(); - if(regularToolset[1]) - activeToolIdentifiers[1] = regularToolset[1]->GetIdentifier(); - if(regularToolset[2]) - activeToolIdentifiers[2] = regularToolset[2]->GetIdentifier(); - - //Empty current menus - for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) - { - delete *iter; - } - menuList.clear(); - toolList.clear(); - - for(std::vector::iterator iter = extraElementTools.begin(), end = extraElementTools.end(); iter != end; ++iter) - { - delete *iter; - } - extraElementTools.clear(); - elementTools.clear(); - - //Create menus - for(int i = 0; i < SC_TOTAL; i++) - { - menuList.push_back(new Menu((const char)sim->msections[i].icon[0], sim->msections[i].name)); - } - - //Build menus from Simulation elements - for(int i = 0; i < PT_NUM; i++) - { - if(sim->elements[i].Enabled) - { - Tool * tempTool; - if(i == PT_LIGH) - { - tempTool = new Element_LIGH_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); - } - else if(i == PT_TESC) - { - tempTool = new Element_TESC_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); - } - else if(i == PT_STKM || i == PT_FIGH || i == PT_STKM2) - { - tempTool = new PlopTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); - } - else - { - tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); - } - - if(sim->elements[i].MenuSection < SC_TOTAL && sim->elements[i].MenuVisible) - { - menuList[sim->elements[i].MenuSection]->AddTool(tempTool); - } - else - { - extraElementTools.push_back(tempTool); - } - elementTools.push_back(tempTool); - } - } - - //Build menu for GOL types - for(int i = 0; i < NGOL; i++) - { - Tool * tempTool = new GolTool(i, sim->gmenu[i].name, std::string(sim->gmenu[i].description), PIXR(sim->gmenu[i].colour), PIXG(sim->gmenu[i].colour), PIXB(sim->gmenu[i].colour), "DEFAULT_PT_LIFE_"+std::string(sim->gmenu[i].name)); - menuList[SC_LIFE]->AddTool(tempTool); - } - - //Build other menus from wall data - for(int i = 0; i < UI_WALLCOUNT; i++) - { - Tool * tempTool = new WallTool(i, "", std::string(sim->wtypes[i].descs), PIXR(sim->wtypes[i].colour), PIXG(sim->wtypes[i].colour), PIXB(sim->wtypes[i].colour), "DEFAULT_WL_"+format::NumberToString(i), sim->wtypes[i].textureGen); - menuList[SC_WALL]->AddTool(tempTool); - //sim->wtypes[i] - } - - //Add special sign and prop tools - menuList[SC_TOOL]->AddTool(new SampleTool(this)); - menuList[SC_TOOL]->AddTool(new SignTool()); - menuList[SC_TOOL]->AddTool(new PropertyTool()); - menuList[SC_TOOL]->AddTool(new WindTool(0, "WIND", "Create air movement", 64, 64, 64, "DEFAULT_UI_WIND")); - - //Build menu for simtools - for(int i = 0; i < sim->tools.size(); i++) - { - Tool * tempTool; - tempTool = new Tool(i, sim->tools[i]->Name, sim->tools[i]->Description, PIXR(sim->tools[i]->Colour), PIXG(sim->tools[i]->Colour), PIXB(sim->tools[i]->Colour), sim->tools[i]->Identifier); - menuList[SC_TOOL]->AddTool(tempTool); - } - - //Add decoration tools to menu - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendAdd, "ADD", "Colour blending: Add", 0, 0, 0, "DEFAULT_DECOR_ADD")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendRemove, "SUB", "Colour blending: Subtract", 0, 0, 0, "DEFAULT_DECOR_SUB")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendMultiply, "MUL", "Colour blending: Multiply", 0, 0, 0, "DEFAULT_DECOR_MUL")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendDivide, "DIV", "Colour blending: Divide" , 0, 0, 0, "DEFAULT_DECOR_DIV")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSmudge, "SMDG", "Smudge colour", 0, 0, 0, "DEFAULT_DECOR_SMDG")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSet, "SET", "Set colour (No blending)", 0, 0, 0, "DEFAULT_DECOR_SET")); - menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::Remove, "CLR", "Clear any set decoration", 0, 0, 0, "DEFAULT_DECOR_CLR")); - decoToolset[0] = GetToolFromIdentifier("DEFAULT_DECOR_SET"); - decoToolset[1] = GetToolFromIdentifier("DEFAULT_DECOR_CLR"); - decoToolset[2] = GetToolFromIdentifier("DEFAULT_UI_SAMPLE"); - - //Set default tools - regularToolset[0] = GetToolFromIdentifier("DEFAULT_PT_DUST"); - regularToolset[1] = GetToolFromIdentifier("DEFAULT_PT_NONE"); - regularToolset[2] = GetToolFromIdentifier("DEFAULT_UI_SAMPLE"); - - - if(activeToolIdentifiers[0].length()) - regularToolset[0] = GetToolFromIdentifier(activeToolIdentifiers[0]); - if(activeToolIdentifiers[1].length()) - regularToolset[1] = GetToolFromIdentifier(activeToolIdentifiers[1]); - if(activeToolIdentifiers[2].length()) - regularToolset[2] = GetToolFromIdentifier(activeToolIdentifiers[2]); - - lastTool = activeTools[0]; - - //Set default menu - activeMenu = menuList[SC_POWDERS]; - - if(lastMenu) - { - for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) - { - if((*iter)->GetIcon() == lastMenu) - activeMenu = *iter; - } - } - - if(activeMenu) - toolList = activeMenu->GetToolList(); - else - toolList = std::vector(); - - notifyMenuListChanged(); - notifyToolListChanged(); - notifyActiveToolsChanged(); - notifyLastToolChanged(); -} - -Tool * GameModel::GetToolFromIdentifier(std::string identifier) -{ - for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) - { - std::vector menuTools = (*iter)->GetToolList(); - for(std::vector::iterator titer = menuTools.begin(), tend = menuTools.end(); titer != tend; ++titer) - { - if(identifier == (*titer)->GetIdentifier()) - return *titer; - } - } - return NULL; -} - -void GameModel::SetEdgeMode(int edgeMode) -{ - this->edgeMode = edgeMode; - sim->SetEdgeMode(edgeMode); -} - -int GameModel::GetEdgeMode() -{ - return this->edgeMode; -} - -std::deque GameModel::GetHistory() -{ - return history; -} -void GameModel::SetHistory(std::deque newHistory) -{ - history = newHistory; -} - -void GameModel::SetVote(int direction) -{ - if(currentSave) - { - RequestStatus status = Client::Ref().ExecVote(currentSave->GetID(), direction); - if(status == RequestOkay) - { - currentSave->vote = direction; - notifySaveChanged(); - } - else - { - throw GameModelException("Could not vote: "+Client::Ref().GetLastError()); - } - } -} - -Brush * GameModel::GetBrush() -{ - return brushList[currentBrush]; -} - -int GameModel::GetBrushID() -{ - return currentBrush; -} - -void GameModel::SetBrush(int i) -{ - currentBrush = i%brushList.size(); - notifyBrushChanged(); -} - -void GameModel::AddObserver(GameView * observer){ - observers.push_back(observer); - - observer->NotifySimulationChanged(this); - observer->NotifyRendererChanged(this); - observer->NotifyPausedChanged(this); - observer->NotifySaveChanged(this); - observer->NotifyBrushChanged(this); - observer->NotifyMenuListChanged(this); - observer->NotifyToolListChanged(this); - observer->NotifyUserChanged(this); - observer->NotifyZoomChanged(this); - observer->NotifyColourSelectorVisibilityChanged(this); - observer->NotifyColourSelectorColourChanged(this); - observer->NotifyColourPresetsChanged(this); - observer->NotifyColourActivePresetChanged(this); - observer->NotifyQuickOptionsChanged(this); - observer->NotifyLastToolChanged(this); - UpdateQuickOptions(); -} - -void GameModel::SetToolStrength(float value) -{ - toolStrength = value; -} - -float GameModel::GetToolStrength() -{ - return toolStrength; -} - -void GameModel::SetActiveMenu(Menu * menu) -{ - for(int i = 0; i < menuList.size(); i++) - { - if(menuList[i]==menu) - { - activeMenu = menu; - toolList = menu->GetToolList(); - notifyToolListChanged(); - - if(menu == menuList[SC_DECO]) - { - if(activeTools != decoToolset) - { - activeTools = decoToolset; - notifyActiveToolsChanged(); - } - } - else - { - if(activeTools != regularToolset) - { - activeTools = regularToolset; - notifyActiveToolsChanged(); - } - } - } - } -} - -vector GameModel::GetUnlistedTools() -{ - return extraElementTools; -} - -vector GameModel::GetToolList() -{ - return toolList; -} - -Menu * GameModel::GetActiveMenu() -{ - return activeMenu; -} - -Tool * GameModel::GetElementTool(int elementID) -{ - std::cout << elementID << std::endl; - for(std::vector::iterator iter = elementTools.begin(), end = elementTools.end(); iter != end; ++iter) - { - if((*iter)->GetToolID() == elementID) - return *iter; - } - return NULL; -} - -Tool * GameModel::GetActiveTool(int selection) -{ - return activeTools[selection]; -} - -void GameModel::SetActiveTool(int selection, Tool * tool) -{ - activeTools[selection] = tool; - notifyActiveToolsChanged(); -} - -vector GameModel::GetQuickOptions() -{ - return quickOptions; -} - -vector GameModel::GetMenuList() -{ - return menuList; -} - -SaveInfo * GameModel::GetSave() -{ - return currentSave; -} - -void GameModel::SetSave(SaveInfo * newSave) -{ - if(currentSave != newSave) - { - if(currentSave) - delete currentSave; - if(newSave == NULL) - currentSave = NULL; - else - currentSave = new SaveInfo(*newSave); - } - if(currentFile) - delete currentFile; - currentFile = NULL; - - if(currentSave && currentSave->GetGameSave()) - { - GameSave * saveData = currentSave->GetGameSave(); - SetPaused(saveData->paused | GetPaused()); - sim->gravityMode = saveData->gravityMode; - sim->air->airMode = saveData->airMode; - sim->legacy_enable = saveData->legacyEnable; - sim->water_equal_test = saveData->waterEEnabled; - if(saveData->gravityEnable) - sim->grav->start_grav_async(); - else - sim->grav->stop_grav_async(); - sim->SetEdgeMode(0); - sim->clear_sim(); - ren->ClearAccumulation(); - sim->Load(saveData); - } - notifySaveChanged(); - UpdateQuickOptions(); -} - -SaveFile * GameModel::GetSaveFile() -{ - return currentFile; -} - -void GameModel::SetSaveFile(SaveFile * newSave) -{ - if(currentFile != newSave) - { - if(currentFile) - delete currentFile; - if(newSave == NULL) - currentFile = NULL; - else - currentFile = new SaveFile(*newSave); - } - if (currentSave) - delete currentSave; - currentSave = NULL; - - if(newSave && newSave->GetGameSave()) - { - GameSave * saveData = newSave->GetGameSave(); - SetPaused(saveData->paused | GetPaused()); - sim->gravityMode = saveData->gravityMode; - sim->air->airMode = saveData->airMode; - sim->legacy_enable = saveData->legacyEnable; - sim->water_equal_test = saveData->waterEEnabled; - if(saveData->gravityEnable && !sim->grav->ngrav_enable) - { - sim->grav->start_grav_async(); - } - else if(!saveData->gravityEnable && sim->grav->ngrav_enable) - { - sim->grav->stop_grav_async(); - } - sim->SetEdgeMode(0); - sim->clear_sim(); - ren->ClearAccumulation(); - sim->Load(saveData); - } - - notifySaveChanged(); - UpdateQuickOptions(); -} - -Simulation * GameModel::GetSimulation() -{ - return sim; -} - -Renderer * GameModel::GetRenderer() -{ - return ren; -} - -User GameModel::GetUser() -{ - return currentUser; -} - -Tool * GameModel::GetLastTool() -{ - return lastTool; -} - -void GameModel::SetLastTool(Tool * newTool) -{ - if(lastTool != newTool) - { - lastTool = newTool; - notifyLastToolChanged(); - } -} - -void GameModel::SetZoomEnabled(bool enabled) -{ - ren->zoomEnabled = enabled; - notifyZoomChanged(); -} - -bool GameModel::GetZoomEnabled() -{ - return ren->zoomEnabled; -} - -void GameModel::SetZoomPosition(ui::Point position) -{ - ren->zoomScopePosition = position; - notifyZoomChanged(); -} - -ui::Point GameModel::GetZoomPosition() -{ - return ren->zoomScopePosition; -} - -void GameModel::SetZoomWindowPosition(ui::Point position) -{ - ren->zoomWindowPosition = position; - notifyZoomChanged(); -} - -ui::Point GameModel::GetZoomWindowPosition() -{ - return ren->zoomWindowPosition; -} - -void GameModel::SetZoomSize(int size) -{ - ren->zoomScopeSize = size; - notifyZoomChanged(); -} - -int GameModel::GetZoomSize() -{ - return ren->zoomScopeSize; -} - -void GameModel::SetZoomFactor(int factor) -{ - ren->ZFACTOR = factor; - notifyZoomChanged(); -} - -int GameModel::GetZoomFactor() -{ - return ren->ZFACTOR; -} - -void GameModel::SetActiveColourPreset(int preset) -{ - activeColourPreset = preset; - notifyColourActivePresetChanged(); -} - -int GameModel::GetActiveColourPreset() -{ - return activeColourPreset; -} - -void GameModel::SetPresetColour(ui::Colour colour) -{ - if(activeColourPreset >= 0 && activeColourPreset < colourPresets.size()) - { - colourPresets[activeColourPreset] = colour; - notifyColourPresetsChanged(); - } -} - -std::vector GameModel::GetColourPresets() -{ - return colourPresets; -} - -void GameModel::SetColourSelectorVisibility(bool visibility) -{ - if(colourSelector != visibility) - { - colourSelector = visibility; - notifyColourSelectorVisibilityChanged(); - } -} - -bool GameModel::GetColourSelectorVisibility() -{ - return colourSelector; -} - -void GameModel::SetColourSelectorColour(ui::Colour colour_) -{ - colour = colour_; - notifyColourSelectorColourChanged(); - - vector tools = GetMenuList()[SC_DECO]->GetToolList(); - for(int i = 0; i < tools.size(); i++) - { - ((DecorationTool*)tools[i])->Red = colour.Red; - ((DecorationTool*)tools[i])->Green = colour.Green; - ((DecorationTool*)tools[i])->Blue = colour.Blue; - ((DecorationTool*)tools[i])->Alpha = colour.Alpha; - } -} - -ui::Colour GameModel::GetColourSelectorColour() -{ - return colour; -} - -void GameModel::SetUser(User user) -{ - currentUser = user; - //Client::Ref().SetAuthUser(user); - notifyUserChanged(); -} - -void GameModel::SetPaused(bool pauseState) -{ - sim->sys_pause = pauseState?1:0; - notifyPausedChanged(); -} - -bool GameModel::GetPaused() -{ - return sim->sys_pause?true:false; -} - -void GameModel::SetDecoration(bool decorationState) -{ - ren->decorations_enable = decorationState?1:0; - notifyDecorationChanged(); - UpdateQuickOptions(); - if (decorationState) - SetInfoTip("Decorations Layer: On"); - else - SetInfoTip("Decorations Layer: Off"); -} - -bool GameModel::GetDecoration() -{ - return ren->decorations_enable?true:false; -} - -void GameModel::SetAHeatEnable(bool aHeat) -{ - sim->aheat_enable = aHeat; - UpdateQuickOptions(); - if (aHeat) - SetInfoTip("Ambient Heat: On"); - else - SetInfoTip("Ambient Heat: Off"); -} - -bool GameModel::GetAHeatEnable() -{ - return sim->aheat_enable; -} - -void GameModel::ShowGravityGrid(bool showGrid) -{ - ren->gravityFieldEnabled = showGrid; - if (showGrid) - SetInfoTip("Gravity Grid: On"); - else - SetInfoTip("Gravity Grid: Off"); -} - -bool GameModel::GetGravityGrid() -{ - return ren->gravityFieldEnabled; -} - -void GameModel::FrameStep(int frames) -{ - sim->framerender += frames; -} - -void GameModel::ClearSimulation() -{ - //Load defaults - sim->gravityMode = 0; - sim->air->airMode = 0; - sim->legacy_enable = false; - sim->water_equal_test = false; - sim->SetEdgeMode(edgeMode); - - sim->clear_sim(); - ren->ClearAccumulation(); - - notifySaveChanged(); - UpdateQuickOptions(); -} - -void GameModel::SetStamp(GameSave * save) -{ - if(stamp != save) - { - if(stamp) - delete stamp; - if(save) - stamp = new GameSave(*save); - else - stamp = NULL; - } -} - -void GameModel::SetPlaceSave(GameSave * save) -{ - if(save != placeSave) - { - if(placeSave) - delete placeSave; - if(save) - placeSave = new GameSave(*save); - else - placeSave = NULL; - } - notifyPlaceSaveChanged(); -} - -void GameModel::AddStamp(GameSave * save) -{ - if(stamp) - delete stamp; - stamp = save; - Client::Ref().AddStamp(save); -} - -void GameModel::SetClipboard(GameSave * save) -{ - if(clipboard) - delete clipboard; - clipboard = save; -} - -GameSave * GameModel::GetClipboard() -{ - return clipboard; -} - -GameSave * GameModel::GetPlaceSave() -{ - return placeSave; -} - -GameSave * GameModel::GetStamp() -{ - return stamp; -} - -void GameModel::Log(string message) -{ - consoleLog.push_front(message); - if(consoleLog.size()>100) - consoleLog.pop_back(); - notifyLogChanged(message); -} - -deque GameModel::GetLog() -{ - return consoleLog; -} - -std::vector GameModel::GetNotifications() -{ - return notifications; -} - -void GameModel::AddNotification(Notification * notification) -{ - notifications.push_back(notification); - notifyNotificationsChanged(); -} - -void GameModel::RemoveNotification(Notification * notification) -{ - for(std::vector::iterator iter = notifications.begin(); iter != notifications.end(); ++iter) - { - if(*iter == notification) - { - delete *iter; - notifications.erase(iter); - break; - } - } - notifyNotificationsChanged(); -} - -void GameModel::SetToolTip(std::string text) -{ - toolTip = text; - notifyToolTipChanged(); -} - -void GameModel::SetInfoTip(std::string text) -{ - infoTip = text; - notifyInfoTipChanged(); -} - -std::string GameModel::GetToolTip() -{ - return toolTip; -} - -std::string GameModel::GetInfoTip() -{ - return infoTip; -} - -void GameModel::notifyNotificationsChanged() -{ - for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) - { - (*iter)->NotifyNotificationsChanged(this); - } -} - -void GameModel::notifyColourPresetsChanged() -{ - for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) - { - (*iter)->NotifyColourPresetsChanged(this); - } -} - -void GameModel::notifyColourActivePresetChanged() -{ - for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) - { - (*iter)->NotifyColourActivePresetChanged(this); - } -} - -void GameModel::notifyColourSelectorColourChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyColourSelectorColourChanged(this); - } -} - -void GameModel::notifyColourSelectorVisibilityChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyColourSelectorVisibilityChanged(this); - } -} - -void GameModel::notifyRendererChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyRendererChanged(this); - } -} - -void GameModel::notifySaveChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifySaveChanged(this); - } -} - -void GameModel::notifySimulationChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifySimulationChanged(this); - } -} - -void GameModel::notifyPausedChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyPausedChanged(this); - } -} - -void GameModel::notifyDecorationChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - //observers[i]->NotifyPausedChanged(this); - } -} - -void GameModel::notifyBrushChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyBrushChanged(this); - } -} - -void GameModel::notifyMenuListChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyMenuListChanged(this); - } -} - -void GameModel::notifyToolListChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyToolListChanged(this); - } -} - -void GameModel::notifyActiveToolsChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyActiveToolsChanged(this); - } -} - -void GameModel::notifyUserChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyUserChanged(this); - } -} - -void GameModel::notifyZoomChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyZoomChanged(this); - } -} - -void GameModel::notifyPlaceSaveChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyPlaceSaveChanged(this); - } -} - -void GameModel::notifyLogChanged(string entry) -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyLogChanged(this, entry); - } -} - -void GameModel::notifyInfoTipChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyInfoTipChanged(this); - } -} - -void GameModel::notifyToolTipChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyToolTipChanged(this); - } -} - -void GameModel::notifyQuickOptionsChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyQuickOptionsChanged(this); - } -} - -void GameModel::notifyLastToolChanged() -{ - for(int i = 0; i < observers.size(); i++) - { - observers[i]->NotifyLastToolChanged(this); - } -} diff --git a/src/game/GameModel.h b/src/game/GameModel.h deleted file mode 100644 index c1d97d6..0000000 --- a/src/game/GameModel.h +++ /dev/null @@ -1,206 +0,0 @@ -#ifndef GAMEMODEL_H -#define GAMEMODEL_H - -#include -#include -#include "client/SaveInfo.h" -#include "simulation/Simulation.h" -#include "interface/Colour.h" -#include "graphics/Renderer.h" -#include "GameView.h" -#include "GameController.h" -#include "Brush.h" -#include "client/User.h" -#include "Notification.h" - -#include "Tool.h" -#include "Menu.h" - -using namespace std; - -class GameView; -class GameController; -class Simulation; -class Renderer; - -class QuickOption; -class ToolSelection -{ -public: - enum - { - ToolPrimary, ToolSecondary, ToolTertiary - }; -}; - -class GameModel -{ -private: - vector notifications; - //int clipboardSize; - //unsigned char * clipboardData; - GameSave * stamp; - GameSave * clipboard; - GameSave * placeSave; - deque consoleLog; - vector observers; - vector toolList; - - //All tools that are associated with elements - vector elementTools; - //Tools that are present in elementTools, but don't have an associated menu and need to be freed manually - vector extraElementTools; - - vector menuList; - vector quickOptions; - Menu * activeMenu; - int currentBrush; - vector brushList; - SaveInfo * currentSave; - SaveFile * currentFile; - Simulation * sim; - Renderer * ren; - Tool * lastTool; - Tool ** activeTools; - Tool * decoToolset[3]; - Tool * regularToolset[3]; - User currentUser; - float toolStrength; - std::deque history; - - int activeColourPreset; - std::vector colourPresets; - bool colourSelector; - ui::Colour colour; - - int edgeMode; - - std::string infoTip; - std::string toolTip; - //bool zoomEnabled; - void notifyRendererChanged(); - void notifySimulationChanged(); - void notifyPausedChanged(); - void notifyDecorationChanged(); - void notifySaveChanged(); - void notifyBrushChanged(); - void notifyMenuListChanged(); - void notifyToolListChanged(); - void notifyActiveToolsChanged(); - void notifyUserChanged(); - void notifyZoomChanged(); - void notifyClipboardChanged(); - void notifyPlaceSaveChanged(); - void notifyColourSelectorColourChanged(); - void notifyColourSelectorVisibilityChanged(); - void notifyColourPresetsChanged(); - void notifyColourActivePresetChanged(); - void notifyNotificationsChanged(); - void notifyLogChanged(string entry); - void notifyInfoTipChanged(); - void notifyToolTipChanged(); - void notifyQuickOptionsChanged(); - void notifyLastToolChanged(); -public: - GameModel(); - ~GameModel(); - - Tool * GetToolFromIdentifier(std::string identifier); - - void SetEdgeMode(int edgeMode); - int GetEdgeMode(); - - void SetActiveColourPreset(int preset); - int GetActiveColourPreset(); - - void SetPresetColour(ui::Colour colour); - - std::vector GetColourPresets(); - - void SetColourSelectorVisibility(bool visibility); - bool GetColourSelectorVisibility(); - - void SetColourSelectorColour(ui::Colour colour); - ui::Colour GetColourSelectorColour(); - - void SetToolTip(std::string text); - void SetInfoTip(std::string text); - std::string GetToolTip(); - std::string GetInfoTip(); - - void BuildMenus(); - void BuildQuickOptionMenu(GameController * controller); - - std::deque GetHistory(); - void SetHistory(std::deque newHistory); - - void UpdateQuickOptions(); - - void SetToolStrength(float value); - float GetToolStrength(); - - Tool * GetLastTool(); - void SetLastTool(Tool * newTool); - - void SetVote(int direction); - SaveInfo * GetSave(); - SaveFile * GetSaveFile(); - Brush * GetBrush(); - void SetSave(SaveInfo * newSave); - void SetSaveFile(SaveFile * newSave); - void AddObserver(GameView * observer); - - //Get an element tool from an element ID - Tool * GetElementTool(int elementID); - - Tool * GetActiveTool(int selection); - void SetActiveTool(int selection, Tool * tool); - - bool GetPaused(); - void SetPaused(bool pauseState); - bool GetDecoration(); - void SetDecoration(bool decorationState); - bool GetAHeatEnable(); - void SetAHeatEnable(bool aHeat); - bool GetGravityGrid(); - void ShowGravityGrid(bool showGrid); - void ClearSimulation(); - vector GetMenuList(); - vector GetUnlistedTools(); - vector GetToolList(); - vector GetQuickOptions(); - void SetActiveMenu(Menu * menu); - Menu * GetActiveMenu(); - void FrameStep(int frames); - User GetUser(); - void SetUser(User user); - void SetBrush(int i); - int GetBrushID(); - Simulation * GetSimulation(); - Renderer * GetRenderer(); - void SetZoomEnabled(bool enabled); - bool GetZoomEnabled(); - void SetZoomSize(int size); - int GetZoomSize(); - void SetZoomFactor(int factor); - int GetZoomFactor(); - void SetZoomPosition(ui::Point position); - ui::Point GetZoomPosition(); - void SetZoomWindowPosition(ui::Point position); - ui::Point GetZoomWindowPosition(); - void SetStamp(GameSave * newStamp); - void AddStamp(GameSave * save); - void SetClipboard(GameSave * save); - void SetPlaceSave(GameSave * save); - void Log(string message); - deque GetLog(); - GameSave * GetClipboard(); - GameSave * GetStamp(); - GameSave * GetPlaceSave(); - - std::vector GetNotifications(); - void AddNotification(Notification * notification); - void RemoveNotification(Notification * notification); -}; - -#endif // GAMEMODEL_H diff --git a/src/game/GameModelException.h b/src/game/GameModelException.h deleted file mode 100644 index c95f7e6..0000000 --- a/src/game/GameModelException.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef GAMEMODELEXCEPTION_H_ -#define GAMEMODELEXCEPTION_H_ - -#include -#include -using namespace std; - -struct GameModelException: public exception { - string message; -public: - GameModelException(string message_): message(message_) {} - const char * what() const throw() - { - return message.c_str(); - } - ~GameModelException() throw() {}; -}; - -#endif /* GAMEMODELEXCEPTION_H_ */ diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp deleted file mode 100644 index 5a7ad33..0000000 --- a/src/game/GameView.cpp +++ /dev/null @@ -1,2140 +0,0 @@ -#include -#include - -#include "Config.h" -#include "Style.h" -#include "GameView.h" -#include "graphics/Graphics.h" -#include "interface/Window.h" -#include "interface/Button.h" -#include "interface/Colour.h" -#include "interface/Keys.h" -#include "interface/Slider.h" -#include "search/Thumbnail.h" -#include "simulation/SaveRenderer.h" -#include "simulation/SimulationData.h" -#include "dialogues/ConfirmPrompt.h" -#include "Format.h" -#include "QuickOption.h" -#include "IntroText.h" - - -class SplitButton; -class SplitButtonAction -{ -public: - virtual void ActionCallbackLeft(ui::Button * sender) {} - virtual void ActionCallbackRight(ui::Button * sender) {} - virtual ~SplitButtonAction() {} -}; -class SplitButton : public ui::Button -{ -private: - bool rightDown; - bool leftDown; - bool showSplit; - int splitPosition; - std::string toolTip2; - SplitButtonAction * splitActionCallback; -public: - SplitButton(ui::Point position, ui::Point size, std::string buttonText, std::string toolTip, std::string toolTip2, int split) : - Button(position, size, buttonText, toolTip), - toolTip2(toolTip2), - splitPosition(split), - splitActionCallback(NULL), - showSplit(true) - { - - } - bool GetShowSplit() { return showSplit; } - void SetShowSplit(bool split) { showSplit = split; } - SplitButtonAction * GetSplitActionCallback() { return splitActionCallback; } - void SetSplitActionCallback(SplitButtonAction * newAction) { splitActionCallback = newAction; } - virtual void OnMouseUnclick(int x, int y, unsigned int button) - { - if(isButtonDown) - { - if(leftDown) - DoLeftAction(); - else if(rightDown) - DoRightAction(); - } - ui::Button::OnMouseUnclick(x, y, button); - - } - virtual void OnMouseMovedInside(int x, int y, int dx, int dy) - { - if(x >= splitPosition || !showSplit) - { - if(toolTip.length()>0 && GetParentWindow()) - { - GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip2); - } - } - else if(x < splitPosition) - { - if(toolTip2.length()>0 && GetParentWindow()) - { - GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); - } - } - } - virtual void OnMouseEnter(int x, int y) - { - isMouseInside = true; - if(!Enabled) - return; - if(x >= splitPosition || !showSplit) - { - if(toolTip.length()>0 && GetParentWindow()) - { - GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip2); - } - } - else if(x < splitPosition) - { - if(toolTip2.length()>0 && GetParentWindow()) - { - GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); - } - } - } - virtual void TextPosition() - { - ui::Button::TextPosition(); - textPosition.X += 3; - } - virtual void OnMouseClick(int x, int y, unsigned int button) - { - ui::Button::OnMouseClick(x, y, button); - rightDown = false; - leftDown = false; - if(x >= splitPosition) - rightDown = true; - else if(x < splitPosition) - leftDown = true; - } - void DoRightAction() - { - if(!Enabled) - return; - if(splitActionCallback) - splitActionCallback->ActionCallbackRight(this); - } - void DoLeftAction() - { - if(!Enabled) - return; - if(splitActionCallback) - splitActionCallback->ActionCallbackLeft(this); - } - void Draw(const ui::Point& screenPos) - { - ui::Button::Draw(screenPos); - Graphics * g = ui::Engine::Ref().g; - drawn = true; - - if(showSplit) - g->draw_line(splitPosition+screenPos.X, screenPos.Y+1, splitPosition+screenPos.X, screenPos.Y+Size.Y-2, 180, 180, 180, 255); - } - virtual ~SplitButton() - { - if(splitActionCallback) - delete splitActionCallback; - } -}; - - -GameView::GameView(): - ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)), - pointQueue(queue()), - isMouseDown(false), - ren(NULL), - activeBrush(NULL), - currentMouse(0, 0), - toolIndex(0), - zoomEnabled(false), - zoomCursorFixed(false), - drawPoint1(0, 0), - drawPoint2(0, 0), - drawMode(DrawPoints), - drawModeReset(false), - selectMode(SelectNone), - selectPoint1(0, 0), - selectPoint2(0, 0), - placeSaveThumb(NULL), - mousePosition(0, 0), - lastOffset(0), - drawSnap(false), - toolTip(""), - infoTip(""), - infoTipPresence(0), - buttonTipShow(0), - toolTipPosition(-1, -1), - shiftBehaviour(false), - ctrlBehaviour(false), - altBehaviour(false), - showHud(true), - showDebug(false), - introText(2048), - introTextMessage(introTextData), - wallBrush(false), - doScreenshot(false), - recording(false), - screenshotIndex(0), - recordingIndex(0), - toolTipPresence(0), - currentSaveType(0), - lastLogEntry(0.0f), - lastMenu(NULL) -{ - - int currentX = 1; - //Set up UI - class SearchAction : public ui::ButtonAction - { - GameView * v; - public: - SearchAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - if(v->CtrlBehaviour()) - v->c->OpenLocalBrowse(); - else - v->c->OpenSearch(); - } - }; - - scrollBar = new ui::Button(ui::Point(0,YRES+21), ui::Point(XRES, 2), ""); - scrollBar->Appearance.BackgroundInactive = ui::Colour(255, 255, 255); - scrollBar->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; - scrollBar->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(scrollBar); - - searchButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(17, 15), "", "Find & open a simulation"); //Open - searchButton->SetIcon(IconOpen); - currentX+=18; - searchButton->SetTogglable(false); - searchButton->SetActionCallback(new SearchAction(this)); - AddComponent(searchButton); - - class ReloadAction : public ui::ButtonAction - { - GameView * v; - public: - ReloadAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->ReloadSim(); - } - void AltActionCallback(ui::Button * sender) - { - v->c->OpenSavePreview(); - } - }; - reloadButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(17, 15), "", "Reload the simulation"); - reloadButton->SetIcon(IconReload); - reloadButton->Appearance.Margin.Left+=2; - currentX+=18; - reloadButton->SetActionCallback(new ReloadAction(this)); - AddComponent(reloadButton); - - class SaveSimulationAction : public SplitButtonAction - { - GameView * v; - public: - SaveSimulationAction(GameView * _v) { v = _v; } - void ActionCallbackRight(ui::Button * sender) - { - if(v->CtrlBehaviour()) - v->c->OpenLocalSaveWindow(false); - else - v->c->OpenSaveWindow(); - } - void ActionCallbackLeft(ui::Button * sender) - { - if(v->CtrlBehaviour()) - v->c->OpenLocalSaveWindow(true); - else - v->c->SaveAsCurrent(); - } - }; - saveSimulationButton = new SplitButton(ui::Point(currentX, Size.Y-16), ui::Point(150, 15), "[untitled simulation]", "Save game as current name", "Save game as new name", 19); - saveSimulationButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - saveSimulationButton->SetIcon(IconSave); - currentX+=151; - ((SplitButton*)saveSimulationButton)->SetSplitActionCallback(new SaveSimulationAction(this)); - AddComponent(saveSimulationButton); - - class UpVoteAction : public ui::ButtonAction - { - GameView * v; - public: - UpVoteAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->Vote(1); - } - }; - upVoteButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(15, 15), "", "Like this save"); - upVoteButton->SetIcon(IconVoteUp); - upVoteButton->Appearance.Margin.Top+=2; - upVoteButton->Appearance.Margin.Left+=2; - currentX+=14; - upVoteButton->SetActionCallback(new UpVoteAction(this)); - AddComponent(upVoteButton); - - class DownVoteAction : public ui::ButtonAction - { - GameView * v; - public: - DownVoteAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->Vote(-1); - } - }; - downVoteButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(15, 15), "", "Dislike this save"); - downVoteButton->SetIcon(IconVoteDown); - downVoteButton->Appearance.Margin.Bottom+=2; - downVoteButton->Appearance.Margin.Left+=2; - currentX+=16; - downVoteButton->SetActionCallback(new DownVoteAction(this)); - AddComponent(downVoteButton); - - class TagSimulationAction : public ui::ButtonAction - { - GameView * v; - public: - TagSimulationAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenTags(); - } - }; - tagSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(251, 15), "[no tags set]", "Add simulation tags"); - tagSimulationButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - tagSimulationButton->SetIcon(IconTag); - currentX+=252; - tagSimulationButton->SetActionCallback(new TagSimulationAction(this)); - AddComponent(tagSimulationButton); - - class ClearSimAction : public ui::ButtonAction - { - GameView * v; - public: - ClearSimAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->ClearSim(); - } - }; - clearSimButton = new ui::Button(ui::Point(Size.X-159, Size.Y-16), ui::Point(17, 15), "", "Erase everything"); - clearSimButton->SetIcon(IconNew); - clearSimButton->Appearance.Margin.Left+=2; - clearSimButton->SetActionCallback(new ClearSimAction(this)); - AddComponent(clearSimButton); - - class LoginAction : public ui::ButtonAction - { - GameView * v; - public: - LoginAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenLogin(); - } - }; - loginButton = new ui::Button(ui::Point(Size.X-141, Size.Y-16), ui::Point(92, 15), "[sign in]", "Sign into simulation server"); - loginButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - loginButton->SetIcon(IconLogin); - loginButton->SetActionCallback(new LoginAction(this)); - AddComponent(loginButton); - - class SimulationOptionAction : public ui::ButtonAction - { - GameView * v; - public: - SimulationOptionAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenOptions(); - } - }; - simulationOptionButton = new ui::Button(ui::Point(Size.X-48, Size.Y-16), ui::Point(15, 15), "", "Simulation options"); - simulationOptionButton->SetIcon(IconSimulationSettings); - simulationOptionButton->Appearance.Margin.Left+=2; - simulationOptionButton->SetActionCallback(new SimulationOptionAction(this)); - AddComponent(simulationOptionButton); - - class DisplayModeAction : public ui::ButtonAction - { - GameView * v; - public: - DisplayModeAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenRenderOptions(); - } - }; - displayModeButton = new ui::Button(ui::Point(Size.X-32, Size.Y-16), ui::Point(15, 15), "", "Renderer options"); - displayModeButton->SetIcon(IconRenderSettings); - displayModeButton->Appearance.Margin.Left+=2; - displayModeButton->SetActionCallback(new DisplayModeAction(this)); - AddComponent(displayModeButton); - - class PauseAction : public ui::ButtonAction - { - GameView * v; - public: - PauseAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->SetPaused(sender->GetToggleState()); - } - }; - 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->SetTogglable(true); - pauseButton->SetActionCallback(new PauseAction(this)); - AddComponent(pauseButton); - - class ElementSearchAction : public ui::ButtonAction - { - GameView * v; - public: - ElementSearchAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenElementSearch(); - } - }; - ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, YRES+MENUSIZE-32), ui::Point(15, 15), "\xE5", "Search for elements"); - tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2); - tempButton->SetActionCallback(new ElementSearchAction(this)); - AddComponent(tempButton); - - class ColourPickerAction : public ui::ButtonAction - { - GameView * v; - public: - ColourPickerAction(GameView * _v) { v = _v; } - void ActionCallback(ui::Button * sender) - { - v->c->OpenColourPicker(); - } - }; - colourPicker = new ui::Button(ui::Point((XRES/2)-8, YRES+1), ui::Point(16, 16), "", "Pick Colour"); - colourPicker->SetActionCallback(new ColourPickerAction(this)); -} - -GameView::~GameView() -{ - if(!colourPicker->GetParentWindow()) - delete colourPicker; - - for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) - { - ToolButton * button = *iter; - if(!button->GetParentWindow()) - { - delete button; - } - - } - - if(placeSaveThumb) - delete placeSaveThumb; -} - -class GameView::MenuAction: public ui::ButtonAction -{ - GameView * v; -public: - Menu * menu; - bool needsClick; - MenuAction(GameView * _v, Menu * menu_) - { - v = _v; - menu = menu_; - if (v->c->GetMenuList()[SC_DECO] == menu) - needsClick = true; - else - needsClick = false; - } - void MouseEnterCallback(ui::Button * sender) - { - if(!needsClick && !ui::Engine::Ref().GetMouseButton()) - v->c->SetActiveMenu(menu); - } - void ActionCallback(ui::Button * sender) - { - if (needsClick) - v->c->SetActiveMenu(menu); - else - MouseEnterCallback(sender); - } -}; - -class GameView::OptionAction: public ui::ButtonAction -{ - QuickOption * option; -public: - OptionAction(QuickOption * _option) { option = _option; } - void ActionCallback(ui::Button * sender) - { - option->Perform(); - } -}; - -class GameView::OptionListener: public QuickOptionListener -{ - ui::Button * button; -public: - OptionListener(ui::Button * _button) { button = _button; } - virtual void OnValueChanged(QuickOption * option) - { - switch(option->GetType()) - { - case QuickOption::Toggle: - button->SetTogglable(true); - button->SetToggleState(option->GetToggle()); - } - } -}; - -class GameView::ToolAction: public ui::ButtonAction -{ - GameView * v; -public: - Tool * tool; - ToolAction(GameView * _v, Tool * tool_) { v = _v; tool = tool_; } - void ActionCallback(ui::Button * sender_) - { - ToolButton *sender = (ToolButton*)sender_; - if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 2) - v->c->SetActiveTool(sender->GetSelectionState(), tool); - } -}; - -void GameView::NotifyQuickOptionsChanged(GameModel * sender) -{ - for(int i = 0; i < quickOptionButtons.size(); i++) - { - RemoveComponent(quickOptionButtons[i]); - delete quickOptionButtons[i]; - } - - int currentY = 1; - vector optionList = sender->GetQuickOptions(); - for(vector::iterator iter = optionList.begin(), end = optionList.end(); iter != end; ++iter) - { - QuickOption * option = *iter; - ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, currentY), ui::Point(15, 15), option->GetIcon(), option->GetDescription()); - //tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2); - tempButton->SetTogglable(true); - tempButton->SetActionCallback(new OptionAction(option)); - option->AddListener(new OptionListener(tempButton)); - AddComponent(tempButton); - - quickOptionButtons.push_back(tempButton); - currentY += 16; - } -} - -void GameView::NotifyMenuListChanged(GameModel * sender) -{ - int currentY = YRES+MENUSIZE-48;//-(sender->GetMenuList().size()*16); - for(int i = 0; i < menuButtons.size(); i++) - { - RemoveComponent(menuButtons[i]); - delete menuButtons[i]; - } - menuButtons.clear(); - for(int i = 0; i < toolButtons.size(); i++) - { - RemoveComponent(toolButtons[i]); - delete toolButtons[i]; - } - toolButtons.clear(); - vector menuList = sender->GetMenuList(); - for(vector::reverse_iterator iter = menuList.rbegin(), end = menuList.rend(); iter != end; ++iter) - { - std::string tempString = ""; - Menu * item = *iter; - tempString += item->GetIcon(); - 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->SetTogglable(true); - tempButton->SetActionCallback(new MenuAction(this, item)); - currentY-=16; - AddComponent(tempButton); - menuButtons.push_back(tempButton); - } -} - -void GameView::SetSample(SimulationSample sample) -{ - this->sample = sample; -} - -void GameView::SetHudEnable(bool hudState) -{ - showHud = hudState; -} - -ui::Point GameView::GetMousePosition() -{ - return mousePosition; -} - -void GameView::NotifyActiveToolsChanged(GameModel * sender) -{ - for(int i = 0; i < toolButtons.size(); i++) - { - Tool * tool = ((ToolAction*)toolButtons[i]->GetActionCallback())->tool; - if(sender->GetActiveTool(0) == tool) - { - toolButtons[i]->SetSelectionState(0); //Primary - c->ActiveToolChanged(0, tool); - } - else if(sender->GetActiveTool(1) == tool) - { - toolButtons[i]->SetSelectionState(1); //Secondary - c->ActiveToolChanged(1, tool); - } - else if(sender->GetActiveTool(2) == tool) - { - toolButtons[i]->SetSelectionState(2); //Tertiary - c->ActiveToolChanged(2, tool); - } - else - { - toolButtons[i]->SetSelectionState(-1); - } - } -} - -void GameView::NotifyLastToolChanged(GameModel * sender) -{ - if(sender->GetLastTool() && sender->GetLastTool()->GetResolution() == CELL) - { - wallBrush = true; - } - else - { - wallBrush = false; - } -} - -void GameView::NotifyToolListChanged(GameModel * sender) -{ - //int currentY = YRES+MENUSIZE-36; - lastOffset = 0; - int currentX = XRES+BARSIZE-56; - int totalColour; - for(int i = 0; i < menuButtons.size(); i++) - { - if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu()) - { - menuButtons[i]->SetToggleState(true); - } - else - { - menuButtons[i]->SetToggleState(false); - } - } - for(int i = 0; i < toolButtons.size(); i++) - { - RemoveComponent(toolButtons[i]); - delete toolButtons[i]; - } - toolButtons.clear(); - vector toolList = sender->GetToolList(); - for(int i = 0; i < toolList.size(); i++) - { - //ToolButton * tempButton = new ToolButton(ui::Point(XRES+1, currentY), ui::Point(28, 15), toolList[i]->GetName()); - VideoBuffer * tempTexture = toolList[i]->GetTexture(26, 14); - ToolButton * tempButton; - - if(tempTexture) - tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", toolList[i]->GetDescription()); - else - tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), toolList[i]->GetName(), toolList[i]->GetDescription()); - - //currentY -= 17; - currentX -= 31; - tempButton->SetActionCallback(new ToolAction(this, toolList[i])); - - tempButton->Appearance.SetTexture(tempTexture); - if(tempTexture) - delete tempTexture; - - tempButton->Appearance.BackgroundInactive = ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue); - - if(sender->GetActiveTool(0) == toolList[i]) - { - tempButton->SetSelectionState(0); //Primary - } - else if(sender->GetActiveTool(1) == toolList[i]) - { - tempButton->SetSelectionState(1); //Secondary - } - else if(sender->GetActiveTool(2) == toolList[i]) - { - tempButton->SetSelectionState(2); //Tertiary - } - - tempButton->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; - tempButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(tempButton); - toolButtons.push_back(tempButton); - } - if (sender->GetActiveMenu() != sender->GetMenuList()[SC_DECO]) - lastMenu = sender->GetActiveMenu(); -} - -void GameView::NotifyColourSelectorVisibilityChanged(GameModel * sender) -{ - for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) - { - ToolButton * button = *iter; - RemoveComponent(button); - button->SetParentWindow(NULL); - } - - RemoveComponent(colourPicker); - colourPicker->SetParentWindow(NULL); - - if(sender->GetColourSelectorVisibility()) - { - for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) - { - ToolButton * button = *iter; - AddComponent(button); - } - AddComponent(colourPicker); - } -} - -void GameView::NotifyColourPresetsChanged(GameModel * sender) -{ - class ColourPresetAction: public ui::ButtonAction - { - GameView * v; - public: - int preset; - ColourPresetAction(GameView * _v, int preset) : preset(preset) { v = _v; } - void ActionCallback(ui::Button * sender_) - { - ToolButton *sender = (ToolButton*)sender_; - if(sender->GetSelectionState() == 0) - { - v->c->SetActiveColourPreset(preset); - v->c->SetColour(sender->Appearance.BackgroundInactive); - } - else - sender->SetSelectionState(0); - } - }; - - - for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) - { - ToolButton * button = *iter; - RemoveComponent(button); - delete button; - } - colourPresets.clear(); - - int currentX = 5; - std::vector colours = sender->GetColourPresets(); - int i = 0; - for(std::vector::iterator iter = colours.begin(), end = colours.end(); iter != end; ++iter) - { - ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), ""); - tempButton->Appearance.BackgroundInactive = *iter; - tempButton->SetActionCallback(new ColourPresetAction(this, i)); - - currentX += 31; - - if(sender->GetColourSelectorVisibility()) - AddComponent(tempButton); - colourPresets.push_back(tempButton); - - i++; - } - NotifyColourActivePresetChanged(sender); -} - -void GameView::NotifyColourActivePresetChanged(GameModel * sender) -{ - for(int i = 0; i < colourPresets.size(); i++) - { - if(sender->GetActiveColourPreset() == i) - { - colourPresets[i]->SetSelectionState(0); //Primary - } - else - { - colourPresets[i]->SetSelectionState(-1); - } - } -} - -void GameView::NotifyColourSelectorColourChanged(GameModel * sender) -{ - colourPicker->Appearance.BackgroundInactive = sender->GetColourSelectorColour(); - colourPicker->Appearance.BackgroundHover = sender->GetColourSelectorColour(); -} - -void GameView::NotifyRendererChanged(GameModel * sender) -{ - ren = sender->GetRenderer(); -} - -void GameView::NotifySimulationChanged(GameModel * sender) -{ - -} -void GameView::NotifyUserChanged(GameModel * sender) -{ - if(!sender->GetUser().ID) - { - loginButton->SetText("[sign in]"); - } - else - { - loginButton->SetText(sender->GetUser().Username); - } - NotifySaveChanged(sender); -} - - -void GameView::NotifyPausedChanged(GameModel * sender) -{ - pauseButton->SetToggleState(sender->GetPaused()); -} - -void GameView::NotifyToolTipChanged(GameModel * sender) -{ - toolTip = sender->GetToolTip(); -} - -void GameView::NotifyInfoTipChanged(GameModel * sender) -{ - infoTip = sender->GetInfoTip(); - infoTipPresence = 120; -} - -void GameView::NotifySaveChanged(GameModel * sender) -{ - if(sender->GetSave()) - { - if(introText > 50) - introText = 50; - - saveSimulationButton->SetText(sender->GetSave()->GetName()); - if(sender->GetSave()->GetUserName() == sender->GetUser().Username) - ((SplitButton*)saveSimulationButton)->SetShowSplit(true); - else - ((SplitButton*)saveSimulationButton)->SetShowSplit(false); - reloadButton->Enabled = true; - upVoteButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==0); - if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==1) - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 108, 10, 255)); - else - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - - downVoteButton->Enabled = upVoteButton->Enabled; - if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==-1) - downVoteButton->Appearance.BackgroundDisabled = (ui::Colour(108, 0, 10, 255)); - else - downVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - - if (sender->GetUser().ID) - { - upVoteButton->Appearance.BorderDisabled = upVoteButton->Appearance.BorderInactive; - downVoteButton->Appearance.BorderDisabled = downVoteButton->Appearance.BorderInactive; - } - else - { - upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); - downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); - } - - tagSimulationButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID); - if(sender->GetSave()->GetID()) - { - std::stringstream tagsStream; - std::vector tags = sender->GetSave()->GetTags(); - if(tags.size()) - { - for(int i = 0; i < tags.size(); i++) - { - tagsStream << sender->GetSave()->GetTags()[i]; - if(i < tags.size()-1) - tagsStream << " "; - } - tagSimulationButton->SetText(tagsStream.str()); - } - else - { - tagSimulationButton->SetText("[no tags set]"); - } - } - else - { - tagSimulationButton->SetText("[no tags set]"); - } - currentSaveType = 1; - } - else if (sender->GetSaveFile()) - { - if (ctrlBehaviour) - ((SplitButton*)saveSimulationButton)->SetShowSplit(true); - else - ((SplitButton*)saveSimulationButton)->SetShowSplit(false); - saveSimulationButton->SetText(sender->GetSaveFile()->GetDisplayName()); - reloadButton->Enabled = true; - upVoteButton->Enabled = false; - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); - downVoteButton->Enabled = false; - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); - tagSimulationButton->Enabled = false; - tagSimulationButton->SetText("[no tags set]"); - currentSaveType = 2; - } - else - { - ((SplitButton*)saveSimulationButton)->SetShowSplit(false); - saveSimulationButton->SetText("[untitled simulation]"); - reloadButton->Enabled = false; - upVoteButton->Enabled = false; - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100), - downVoteButton->Enabled = false; - upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); - downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100), - tagSimulationButton->Enabled = false; - tagSimulationButton->SetText("[no tags set]"); - currentSaveType = 0; - } -} - -void GameView::NotifyBrushChanged(GameModel * sender) -{ - activeBrush = sender->GetBrush(); -} - -void GameView::screenshot() -{ - doScreenshot = true; -} - -void GameView::record() -{ - if(recording) - { - recording = false; - } - else - { - class RecordingConfirmation: public ConfirmDialogueCallback { - public: - GameView * v; - RecordingConfirmation(GameView * v): v(v) {} - virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { - if (result == ConfirmPrompt::ResultOkay) - { - v->recording = true; - } - } - virtual ~RecordingConfirmation() { } - }; - new ConfirmPrompt("Recording", "You're about to start recording all drawn frames. This may use a load of hard disk space.", new RecordingConfirmation(this)); - } -} - -void GameView::setToolButtonOffset(int offset) -{ - int offset_ = offset; - offset = offset-lastOffset; - lastOffset = offset_; - - for(vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter!=end; ++iter) - { - ToolButton * button = *iter; - button->Position.X -= offset; - if(button->Position.X <= 0 || (button->Position.X+button->Size.X) > XRES-2) { - button->Visible = false; - } else { - button->Visible = true; - } - } -} - -void GameView::OnMouseMove(int x, int y, int dx, int dy) -{ - mousePosition = c->PointTranslate(ui::Point(x, y)); - if(selectMode!=SelectNone) - { - if(selectMode==PlaceSave) - selectPoint1 = c->PointTranslate(ui::Point(x, y)); - if(selectPoint1.X!=-1) - selectPoint2 = c->PointTranslate(ui::Point(x, y)); - return; - } - currentMouse = ui::Point(x, y); - if(isMouseDown && drawMode == DrawPoints) - { - pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x-dx, y-dy)))); - pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); - } -} - -void GameView::OnMouseDown(int x, int y, unsigned button) -{ - if(altBehaviour && !shiftBehaviour && !ctrlBehaviour) - button = BUTTON_MIDDLE; - if(selectMode!=SelectNone) - { - if(button==BUTTON_LEFT) - { - selectPoint1 = c->PointTranslate(ui::Point(x, y)); - selectPoint2 = selectPoint1; - } - return; - } - if(currentMouse.X >= 0 && currentMouse.X < XRES && currentMouse.Y >= 0 && currentMouse.Y < YRES && !(zoomEnabled && !zoomCursorFixed)) - { - if(button == BUTTON_LEFT) - toolIndex = 0; - if(button == BUTTON_RIGHT) - toolIndex = 1; - if(button == BUTTON_MIDDLE) - toolIndex = 2; - isMouseDown = true; - if(!pointQueue.size()) - c->HistorySnapshot(); - if(drawMode == DrawRect || drawMode == DrawLine) - { - drawPoint1 = c->PointTranslate(ui::Point(x, y)); - } - if(drawMode == DrawPoints) - { - pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); - } - } -} - -void GameView::OnMouseUp(int x, int y, unsigned button) -{ - if(selectMode!=SelectNone) - { - if(button==BUTTON_LEFT) - { - if(selectMode==PlaceSave) - { - if(placeSaveThumb) - { - int thumbX = selectPoint2.X - (placeSaveThumb->Width/2); - int thumbY = selectPoint2.Y - (placeSaveThumb->Height/2); - - if(thumbX<0) - thumbX = 0; - if(thumbX+(placeSaveThumb->Width)>=XRES) - thumbX = XRES-placeSaveThumb->Width; - - if(thumbY<0) - thumbY = 0; - if(thumbY+(placeSaveThumb->Height)>=YRES) - thumbY = YRES-placeSaveThumb->Height; - - c->PlaceSave(ui::Point(thumbX, thumbY)); - } - } - else - { - int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; - int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; - int x1 = (selectPoint2.XCopyRegion(ui::Point(x1, y1), ui::Point(x2, y2)); - else if(selectMode==SelectCut) - c->CutRegion(ui::Point(x1, y1), ui::Point(x2, y2)); - else if(selectMode==SelectStamp) - c->StampRegion(ui::Point(x1, y1), ui::Point(x2, y2)); - } - } - currentMouse = ui::Point(x, y); - selectMode = SelectNone; - return; - } - - if(zoomEnabled && !zoomCursorFixed) - zoomCursorFixed = true; - else - { - if(isMouseDown) - { - isMouseDown = false; - if(drawMode == DrawRect || drawMode == DrawLine) - { - ui::Point finalDrawPoint2(0, 0); - drawPoint2 = c->PointTranslate(ui::Point(x, y)); - finalDrawPoint2 = drawPoint2; - - if(drawSnap && drawMode == DrawLine) - { - finalDrawPoint2 = lineSnapCoords(c->PointTranslate(drawPoint1), drawPoint2); - } - - if(drawSnap && drawMode == DrawRect) - { - finalDrawPoint2 = rectSnapCoords(c->PointTranslate(drawPoint1), drawPoint2); - } - - if(drawMode == DrawRect) - { - c->DrawRect(toolIndex, c->PointTranslate(drawPoint1), finalDrawPoint2); - } - if(drawMode == DrawLine) - { - c->DrawLine(toolIndex, c->PointTranslate(drawPoint1), finalDrawPoint2); - } - } - if(drawMode == DrawPoints) - { - c->ToolClick(toolIndex, c->PointTranslate(ui::Point(x, y))); - //pointQueue.push(ui::Point(x, y)); - } - if(drawModeReset) - { - drawModeReset = false; - drawMode = DrawPoints; - } - } - } -} - -void GameView::ExitPrompt() -{ - class ExitConfirmation: public ConfirmDialogueCallback { - public: - ExitConfirmation() {} - virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { - if (result == ConfirmPrompt::ResultOkay) - { - ui::Engine::Ref().Exit(); - } - } - virtual ~ExitConfirmation() { } - }; - new ConfirmPrompt("You are about to quit", "Are you sure you want to exit the game?", new ExitConfirmation()); -} - -void GameView::ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip) -{ - if(sender->Position.Y > Size.Y-17) - { - buttonTip = toolTip; - buttonTipShow = 120; - } - else if(sender->Position.X > Size.X-BARSIZE)// < Size.Y-(quickOptionButtons.size()+1)*16) - { - this->toolTip = toolTip; - toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), sender->Position.Y+3); - if(toolTipPosition.Y+10 > Size.Y-MENUSIZE) - toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), Size.Y-MENUSIZE-10); - toolTipPresence = 120; - } - else - { - this->toolTip = toolTip; - toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), Size.Y-MENUSIZE-10); - toolTipPresence = 160; - } -} - -void GameView::OnMouseWheel(int x, int y, int d) -{ - if(!d) - return; - if(selectMode!=SelectNone) - { - return; - } - if(zoomEnabled && !zoomCursorFixed) - { - c->AdjustZoomSize(d); - } - else - { - c->AdjustBrushSize(d, false, shiftBehaviour, ctrlBehaviour); - if(isMouseDown) - { - pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); - } - } -} - -void GameView::ToggleDebug() -{ - showDebug = !showDebug; - if (ren) - ren->debugLines = showDebug; -} - -void GameView::BeginStampSelection() -{ - selectMode = SelectStamp; - selectPoint1 = ui::Point(-1, -1); - infoTip = "\x0F\xEF\xEF\x10Select an area to create a stamp"; - infoTipPresence = 120; -} - -void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if(introText > 50) - { - introText = 50; - } - - if(selectMode!=SelectNone) - { - if(selectMode==PlaceSave) - { - switch(key) - { - case KEY_RIGHT: - case 'd': - c->TranslateSave(ui::Point(1, 0)); - break; - case KEY_LEFT: - case 'a': - c->TranslateSave(ui::Point(-1, 0)); - break; - case KEY_UP: - case 'w': - c->TranslateSave(ui::Point(0, -1)); - break; - case KEY_DOWN: - case 's': - c->TranslateSave(ui::Point(0, 1)); - break; - case 'r': - if(shift) - { - //Flip - c->TransformSave(m2d_new(-1,0,0,1)); - } - else - { - //Rotate 90deg - c->TransformSave(m2d_new(0,1,-1,0)); - } - break; - } - } - if(key != ' ') - return; - } - switch(key) - { - case KEY_ALT: - drawSnap = true; - enableAltBehaviour(); - break; - case KEY_CTRL: - if(!isMouseDown) - { - if(drawModeReset) - drawModeReset = false; - else - drawPoint1 = currentMouse; - if(shift) - drawMode = DrawFill; - else - drawMode = DrawRect; - } - enableCtrlBehaviour(); - break; - case KEY_SHIFT: - if(!isMouseDown) - { - if(drawModeReset) - drawModeReset = false; - else - drawPoint1 = currentMouse; - if(ctrl) - drawMode = DrawFill; - else - drawMode = DrawLine; - } - enableShiftBehaviour(); - break; - case ' ': //Space - c->SetPaused(); - break; - case KEY_TAB: //Tab - c->ChangeBrush(); - break; - case 'z': - if(ctrl) - { - c->HistoryRestore(); - } - else - { - if (drawMode != DrawLine) - isMouseDown = false; - zoomCursorFixed = false; - c->SetZoomEnabled(true); - } - break; - case '`': - c->ShowConsole(); - break; - case 'p': - screenshot(); - break; - case 'r': - record(); - break; - case 'e': - c->OpenElementSearch(); - break; - case 'f': - c->FrameStep(); - break; - case 'g': - if (ctrl) - c->ShowGravityGrid(); - else if(shift) - c->AdjustGridSize(-1); - else - c->AdjustGridSize(1); - break; - case KEY_F1: - if(!introText) - introText = 8047; - else - introText = 0; - break; - case 'h': - if(ctrl) - { - if(!introText) - introText = 8047; - else - introText = 0; - } - else - showHud = !showHud; - break; - case 'b': - if(ctrl) - c->SetDecoration(); - else - if (colourPicker->GetParentWindow()) - c->SetActiveMenu(lastMenu); - else - { - c->SetDecoration(true); - c->SetPaused(true); - c->SetActiveMenu(c->GetMenuList()[SC_DECO]); - } - break; - case 'y': - c->SwitchAir(); - break; - case KEY_ESCAPE: - case 'q': - ExitPrompt(); - break; - case 'u': - c->ToggleAHeat(); - break; - case '=': - if(ctrl) - c->ResetSpark(); - else - c->ResetAir(); - break; - case 'c': - if(ctrl) - { - selectMode = SelectCopy; - selectPoint1 = ui::Point(-1, -1); - infoTip = "\x0F\xEF\xEF\x10Select an area to copy"; - infoTipPresence = 120; - } - break; - case 'x': - if(ctrl) - { - selectMode = SelectCut; - selectPoint1 = ui::Point(-1, -1); - infoTip = "\x0F\xEF\xEF\x10Select an area to cut"; - infoTipPresence = 120; - } - break; - case 'v': - if(ctrl) - { - c->LoadClipboard(); - selectPoint2 = mousePosition; - selectPoint1 = selectPoint2; - } - break; - case 'l': - c->LoadStamp(); - selectPoint2 = mousePosition; - selectPoint1 = selectPoint2; - isMouseDown = false; - drawMode = DrawPoints; - break; - case 'k': - selectPoint2 = ui::Point(-1, -1); - selectPoint1 = selectPoint2; - c->OpenStamps(); - break; - case ']': - if(zoomEnabled && !zoomCursorFixed) - c->AdjustZoomSize(1, !alt); - else - c->AdjustBrushSize(1, !alt, shiftBehaviour, ctrlBehaviour); - break; - case '[': - if(zoomEnabled && !zoomCursorFixed) - c->AdjustZoomSize(-1, !alt); - else - c->AdjustBrushSize(-1, !alt, shiftBehaviour, ctrlBehaviour); - break; - case 'i': - if(ctrl) - c->Install(); - else - c->InvertAirSim(); - break; - } - - if (shift && showDebug && key == '1') - c->LoadRenderPreset(10); - else if(key >= '0' && key <= '9') - { - c->LoadRenderPreset(key-'0'); - } -} - -void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if(ctrl && shift) - drawMode = DrawFill; - else if (ctrl) - drawMode = DrawRect; - else if (shift) - drawMode = DrawLine; - else if(!isMouseDown) - drawMode = DrawPoints; - else - drawModeReset = true; - switch(key) - { - case KEY_ALT: - drawSnap = false; - disableAltBehaviour(); - break; - case KEY_CTRL: - disableCtrlBehaviour(); - break; - case KEY_SHIFT: - disableShiftBehaviour(); - break; - case 'z': - if(!ctrl) - { - if(!zoomCursorFixed && !alt) - c->SetZoomEnabled(false); - } - break; - } -} - -void GameView::OnBlur() -{ - disableAltBehaviour(); - disableCtrlBehaviour(); - disableShiftBehaviour(); - isMouseDown = false; - drawMode = DrawPoints; -} - -void GameView::OnTick(float dt) -{ - if(selectMode==PlaceSave && !placeSaveThumb) - selectMode = SelectNone; - if(zoomEnabled && !zoomCursorFixed) - c->SetZoomPosition(currentMouse); - if(drawMode == DrawPoints) - { - if(isMouseDown) - { - pointQueue.push(ui::Point(c->PointTranslate(currentMouse))); - } - if(!pointQueue.empty()) - { - c->DrawPoints(toolIndex, pointQueue); - } - } - if(drawMode == DrawFill && isMouseDown) - { - c->DrawFill(toolIndex, c->PointTranslate(currentMouse)); - } - if(introText) - { - introText -= int(dt)>0?((int)dt < 5? dt:5):1; - if(introText < 0) - introText = 0; - } - if(infoTipPresence>0) - { - infoTipPresence -= int(dt)>0?int(dt):1; - if(infoTipPresence<0) - infoTipPresence = 0; - } - if(buttonTipShow>0) - { - buttonTipShow -= int(dt)>0?int(dt):1; - if(buttonTipShow<0) - buttonTipShow = 0; - } - if(toolTipPresence>0) - { - toolTipPresence -= int(dt)>0?int(dt):1; - if(toolTipPresence<0) - toolTipPresence = 0; - } - c->Update(); - if(lastLogEntry > -0.1f) - lastLogEntry -= 0.16*dt; -} - - -void GameView::DoMouseMove(int x, int y, int dx, int dy) -{ - if(c->MouseMove(x, y, dx, dy)) - Window::DoMouseMove(x, y, dx, dy); - - if(toolButtons.size()) - { - int totalWidth = (toolButtons[0]->Size.X+1)*toolButtons.size(); - int scrollSize = (int)(((float)(XRES-15))/((float)totalWidth) * ((float)XRES-15)); - if (scrollSize>XRES) - scrollSize = XRES; - if(totalWidth > XRES-15) - { - int mouseX = x; - if(mouseX > XRES) - mouseX = XRES; - - scrollBar->Position.X = (int)(((float)mouseX/((float)XRES-15))*(float)(XRES-scrollSize)); - - float overflow = totalWidth-(XRES-15), mouseLocation = float(XRES)/float(mouseX-(XRES)); - setToolButtonOffset(overflow/mouseLocation); - - //Ensure that mouseLeave events are make their way to the buttons should they move from underneith the mouse pointer - if(toolButtons[0]->Position.Y < y && toolButtons[0]->Position.Y+toolButtons[0]->Size.Y > y) - { - for(vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter!=end; ++iter) - { - ToolButton * button = *iter; - if(button->Position.X < x && button->Position.X+button->Size.X > x) - button->OnMouseEnter(x, y); - else - button->OnMouseLeave(x, y); - } - } - } - else - { - scrollBar->Position.X = 0; - } - scrollBar->Size.X=scrollSize; - } -} - -void GameView::DoMouseDown(int x, int y, unsigned button) -{ - if(introText > 50) - introText = 50; - if(c->MouseDown(x, y, button)) - Window::DoMouseDown(x, y, button); -} - -void GameView::DoMouseUp(int x, int y, unsigned button) -{ - if(c->MouseUp(x, y, button)) - Window::DoMouseUp(x, y, button); -} - -void GameView::DoMouseWheel(int x, int y, int d) -{ - if(c->MouseWheel(x, y, d)) - Window::DoMouseWheel(x, y, d); -} - -void GameView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if(c->KeyPress(key, character, shift, ctrl, alt)) - Window::DoKeyPress(key, character, shift, ctrl, alt); -} - -void GameView::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if(c->KeyRelease(key, character, shift, ctrl, alt)) - Window::DoKeyRelease(key, character, shift, ctrl, alt); -} - -void GameView::DoDraw() -{ - Window::DoDraw(); - c->Tick(); -} - -void GameView::NotifyNotificationsChanged(GameModel * sender) -{ - class NotificationButtonAction : public ui::ButtonAction - { - GameView * v; - Notification * notification; - public: - NotificationButtonAction(GameView * v, Notification * notification) : v(v), notification(notification) { } - void ActionCallback(ui::Button * sender) - { - notification->Action(); - //v->c->RemoveNotification(notification); - } - }; - class CloseNotificationButtonAction : public ui::ButtonAction - { - GameView * v; - Notification * notification; - public: - CloseNotificationButtonAction(GameView * v, Notification * notification) : v(v), notification(notification) { } - void ActionCallback(ui::Button * sender) - { - v->c->RemoveNotification(notification); - } - void AltActionCallback(ui::Button * sender) - { - v->c->RemoveNotification(notification); - } - }; - - for(std::vector::const_iterator iter = notificationComponents.begin(), end = notificationComponents.end(); iter != end; ++iter) { - ui::Component * cNotification = *iter; - RemoveComponent(cNotification); - delete cNotification; - } - notificationComponents.clear(); - - std::vector notifications = sender->GetNotifications(); - - int currentY = YRES-23; - for(std::vector::iterator iter = notifications.begin(), end = notifications.end(); iter != end; ++iter) - { - int width = (Graphics::textwidth((*iter)->Message.c_str()))+8; - ui::Button * tempButton = new ui::Button(ui::Point(XRES-width-22, currentY), ui::Point(width, 15), (*iter)->Message); - tempButton->SetActionCallback(new NotificationButtonAction(this, *iter)); - tempButton->Appearance.BorderInactive = style::Colour::WarningTitle; - tempButton->Appearance.TextInactive = style::Colour::WarningTitle; - tempButton->Appearance.BorderHover = ui::Colour(255, 175, 0); - tempButton->Appearance.TextHover = ui::Colour(255, 175, 0); - AddComponent(tempButton); - notificationComponents.push_back(tempButton); - - tempButton = new ui::Button(ui::Point(XRES-20, currentY), ui::Point(15, 15)); - tempButton->SetIcon(IconClose); - tempButton->SetActionCallback(new CloseNotificationButtonAction(this, *iter)); - tempButton->Appearance.Margin.Left+=2; - tempButton->Appearance.Margin.Top+=2; - tempButton->Appearance.BorderInactive = style::Colour::WarningTitle; - tempButton->Appearance.TextInactive = style::Colour::WarningTitle; - tempButton->Appearance.BorderHover = ui::Colour(255, 175, 0); - tempButton->Appearance.TextHover = ui::Colour(255, 175, 0); - AddComponent(tempButton); - notificationComponents.push_back(tempButton); - - currentY -= 17; - } -} - -void GameView::NotifyZoomChanged(GameModel * sender) -{ - zoomEnabled = sender->GetZoomEnabled(); -} - -void GameView::NotifyLogChanged(GameModel * sender, string entry) -{ - logEntries.push_front(entry); - lastLogEntry = 100.0f; - if(logEntries.size()>20) - logEntries.pop_back(); -} - -void GameView::NotifyPlaceSaveChanged(GameModel * sender) -{ - if(placeSaveThumb) - delete placeSaveThumb; - if(sender->GetPlaceSave()) - { - placeSaveThumb = SaveRenderer::Ref().Render(sender->GetPlaceSave()); - selectMode = PlaceSave; - selectPoint2 = mousePosition; - } - else - { - placeSaveThumb = NULL; - selectMode = SelectNone; - } -} - -void GameView::enableShiftBehaviour() -{ - if(!shiftBehaviour) - { - shiftBehaviour = true; - if(isMouseDown) - { - c->SetToolStrength(10.0f); - } - } -} - -void GameView::disableShiftBehaviour() -{ - if(shiftBehaviour) - { - shiftBehaviour = false; - if(!ctrlBehaviour) - c->SetToolStrength(1.0f); - else - c->SetToolStrength(.1f); - } -} - -void GameView::enableAltBehaviour() -{ - if(!altBehaviour) - { - altBehaviour = true; - } -} - -void GameView::disableAltBehaviour() -{ - if(altBehaviour) - { - altBehaviour = false; - } -} - -void GameView::enableCtrlBehaviour() -{ - if(!ctrlBehaviour) - { - ctrlBehaviour = true; - - //Show HDD save & load buttons - saveSimulationButton->Appearance.BackgroundInactive = saveSimulationButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); - saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(0, 0, 0); - searchButton->Appearance.BackgroundInactive = searchButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); - searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(0, 0, 0); - if (currentSaveType == 2) - ((SplitButton*)saveSimulationButton)->SetShowSplit(true); - if(isMouseDown) - { - if(!shiftBehaviour) - c->SetToolStrength(.1f); - else - c->SetToolStrength(10.0f); - } - } -} - -void GameView::disableCtrlBehaviour() -{ - if(ctrlBehaviour) - { - ctrlBehaviour = false; - - //Hide HDD save & load buttons - saveSimulationButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); - saveSimulationButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); - saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(255, 255, 255); - searchButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); - searchButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); - searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(255, 255, 255); - if (currentSaveType == 2) - ((SplitButton*)saveSimulationButton)->SetShowSplit(false); - if(!shiftBehaviour) - c->SetToolStrength(1.0f); - else - c->SetToolStrength(10.0f); - } -} - -void GameView::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - if(ren) - { - ren->clearScreen(1.0f); - ren->RenderBegin(); - ren->SetSample(c->PointTranslate(currentMouse).X, c->PointTranslate(currentMouse).Y); - if(selectMode == SelectNone && (!zoomEnabled || zoomCursorFixed) && activeBrush && currentMouse.X >= 0 && currentMouse.X < XRES && currentMouse.Y >= 0 && currentMouse.Y < YRES) - { - ui::Point finalCurrentMouse = c->PointTranslate(currentMouse); - ui::Point initialDrawPoint = drawPoint1; - - if(wallBrush) - { - finalCurrentMouse = c->NormaliseBlockCoord(finalCurrentMouse); - initialDrawPoint = c->NormaliseBlockCoord(initialDrawPoint); - } - - if(drawMode==DrawRect && isMouseDown) - { - if(drawSnap) - { - finalCurrentMouse = rectSnapCoords(c->PointTranslate(initialDrawPoint), finalCurrentMouse); - } - activeBrush->RenderRect(ren, c->PointTranslate(initialDrawPoint), finalCurrentMouse); - } - else if(drawMode==DrawLine && isMouseDown) - { - if(drawSnap) - { - finalCurrentMouse = lineSnapCoords(c->PointTranslate(initialDrawPoint), finalCurrentMouse); - } - activeBrush->RenderLine(ren, c->PointTranslate(initialDrawPoint), finalCurrentMouse); - } - else if(drawMode==DrawFill)// || altBehaviour) - { - activeBrush->RenderFill(ren, finalCurrentMouse); - } - if(drawMode == DrawPoints || drawMode==DrawLine || (drawMode == DrawRect && !isMouseDown)) - { - if(wallBrush) - { - ui::Point finalBrushRadius = c->NormaliseBlockCoord(activeBrush->GetRadius()); - ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y-finalBrushRadius.Y, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y-finalBrushRadius.Y); - ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-1, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-1); - - ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y-finalBrushRadius.Y+1, finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-2); - ren->xor_line(finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y-finalBrushRadius.Y+1, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-2); - } - else - { - activeBrush->RenderPoint(ren, finalCurrentMouse); - } - } - } - - if(selectMode!=SelectNone) - { - if(selectMode==PlaceSave) - { - if(placeSaveThumb && selectPoint2.X!=-1) - { - int thumbX = selectPoint2.X - (placeSaveThumb->Width/2); - int thumbY = selectPoint2.Y - (placeSaveThumb->Height/2); - - ui::Point thumbPos = c->NormaliseBlockCoord(ui::Point(thumbX, thumbY)); - - if(thumbPos.X<0) - thumbPos.X = 0; - if(thumbPos.X+(placeSaveThumb->Width)>=XRES) - thumbPos.X = XRES-placeSaveThumb->Width; - - if(thumbPos.Y<0) - thumbPos.Y = 0; - if(thumbPos.Y+(placeSaveThumb->Height)>=YRES) - thumbPos.Y = YRES-placeSaveThumb->Height; - - ren->draw_image(placeSaveThumb, thumbPos.X, thumbPos.Y, 128); - - ren->xor_rect(thumbPos.X, thumbPos.Y, placeSaveThumb->Width, placeSaveThumb->Width); - } - } - else - { - if(selectPoint1.X==-1) - { - ren->fillrect(0, 0, XRES, YRES, 0, 0, 0, 100); - } - else - { - int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; - int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; - int x1 = (selectPoint2.XXRES-1) - x2 = XRES-1; - if(y2>YRES-1) - y2 = YRES-1; - - ren->fillrect(0, 0, XRES, y1, 0, 0, 0, 100); - ren->fillrect(0, y2+1, XRES, YRES-y2-1, 0, 0, 0, 100); - - ren->fillrect(0, y1, x1, (y2-y1)+1, 0, 0, 0, 100); - ren->fillrect(x2+1, y1, XRES-x2-1, (y2-y1)+1, 0, 0, 0, 100); - - ren->xor_rect(x1, y1, (x2-x1)+1, (y2-y1)+1); - } - } - } - - ren->RenderEnd(); - - if(doScreenshot) - { - VideoBuffer screenshot(ren->DumpFrame()); - std::vector data = format::VideoBufferToPNG(screenshot); - - std::stringstream filename; - filename << "screenshot_"; - filename << std::setfill('0') << std::setw(6) << (screenshotIndex++); - filename << ".png"; - - Client::Ref().WriteFile(data, filename.str()); - doScreenshot = false; - } - - if(recording) - { - VideoBuffer screenshot(ren->DumpFrame()); - std::vector data = format::VideoBufferToPPM(screenshot); - - std::stringstream filename; - filename << "frame_"; - filename << std::setfill('0') << std::setw(6) << (recordingIndex++); - filename << ".ppm"; - - Client::Ref().WriteFile(data, filename.str()); - } - - int startX = 20; - int startY = YRES-20; - int startAlpha; - if(lastLogEntry>0.1f && logEntries.size()) - { - startAlpha = 2.55f*lastLogEntry; - deque::iterator iter; - for(iter = logEntries.begin(); iter != logEntries.end() && startAlpha>0; iter++) - { - string message = (*iter); - startY -= 13; - g->fillrect(startX-3, startY-3, Graphics::textwidth((char*)message.c_str())+6, 14, 0, 0, 0, 100); - g->drawtext(startX, startY, message.c_str(), 255, 255, 255, startAlpha); - startAlpha-=14; - } - } - } - - if(recording) - { - std::stringstream sampleInfo; - sampleInfo << recordingIndex; - sampleInfo << ". \x8E REC"; - - int textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); - g->fillrect(XRES-20-textWidth, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); - g->drawtext(XRES-16-textWidth, 16, (const char*)sampleInfo.str().c_str(), 255, 50, 20, 255); - } - else if(showHud && !introText) - { - //Draw info about simulation under cursor - int wavelengthGfx = 0; - std::stringstream sampleInfo; - sampleInfo.precision(2); - if(sample.particle.type) - { - if(showDebug) - { - int ctype = sample.particle.ctype; - if (sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) - ctype = sample.particle.tmp; - - if (sample.particle.type == PT_LAVA && ctype > 0 && ctype < PT_NUM) - sampleInfo << "Molten " << c->ElementResolve(ctype); - else if((sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) && ctype > 0 && ctype < PT_NUM) - sampleInfo << c->ElementResolve(sample.particle.type) << " with " << c->ElementResolve(ctype); - else - { - sampleInfo << c->ElementResolve(sample.particle.type); - if(ctype > 0 && ctype < PT_NUM) - sampleInfo << " (" << c->ElementResolve(ctype) << ")"; - else - sampleInfo << " ()"; - } - sampleInfo << ", Temp: " << std::fixed << sample.particle.temp -273.15f; - sampleInfo << ", Life: " << sample.particle.life; - sampleInfo << ", Tmp: " << sample.particle.tmp; - sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; - } - else - { - if (sample.particle.type == PT_LAVA && sample.particle.ctype > 0 && sample.particle.ctype < PT_NUM) - sampleInfo << "Molten " << c->ElementResolve(sample.particle.ctype); - else if((sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) && sample.particle.tmp > 0 && sample.particle.tmp < PT_NUM) - sampleInfo << c->ElementResolve(sample.particle.type) << " with " << c->ElementResolve(sample.particle.tmp); - else - sampleInfo << c->ElementResolve(sample.particle.type); - sampleInfo << ", Temp: " << std::fixed << sample.particle.temp -273.15f; - sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; - } - if(sample.particle.type == PT_PHOT) - wavelengthGfx = sample.particle.ctype; - } - else if (sample.WallType) - { - sampleInfo << c->WallName(sample.WallType); - sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; - } - else - { - sampleInfo << "Empty, Pressure: " << std::fixed << sample.AirPressure; - } - - int textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); - g->fillrect(XRES-20-textWidth, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); - g->drawtext(XRES-16-textWidth, 16, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255*0.75); - -#ifndef OGLI - if(wavelengthGfx) - { - int i, cr, cg, cb, j, h = 3, x = XRES-19-textWidth, y = 10; - int tmp; - g->fillrect(x, y, 30, h, 64, 64, 64, 255); // coords -1 size +1 to work around bug in fillrect - TODO: fix fillrect - for (i = 0; i < 30; i++) - { - if ((wavelengthGfx >> i)&1) - { - // Need a spread of wavelengths to get a smooth spectrum, 5 bits seems to work reasonably well - if (i>2) tmp = 0x1F << (i-2); - else tmp = 0x1F >> (2-i); - - cg = 0; - cb = 0; - cr = 0; - - for (j=0; j<12; j++) - { - cr += (tmp >> (j+18)) & 1; - cb += (tmp >> j) & 1; - } - for (j=0; j<13; j++) - cg += (tmp >> (j+9)) & 1; - - tmp = 624/(cr+cg+cb+1); - cr *= tmp; - cg *= tmp; - cb *= tmp; - for (j=0; jblendpixel(x+29-i,y+j,cr>255?255:cr,cg>255?255:cg,cb>255?255:cb,255); - } - } - } -#endif - - if(showDebug) - { - sampleInfo.str(std::string()); - - if(sample.particle.type) - { - sampleInfo << "#" << sample.ParticleID << ", "; - } - sampleInfo << "X:" << sample.PositionX << " Y:" << sample.PositionY; - if (sample.Gravity) - sampleInfo << " GX: " << sample.GravityVelocityX << " GY: " << sample.GravityVelocityY; - - textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); - g->fillrect(XRES-20-textWidth, 26, textWidth+8, 15, 0, 0, 0, 255*0.5); - g->drawtext(XRES-16-textWidth, 30, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255*0.75); - } - - - //FPS and some version info -#ifndef DEBUG //In debug mode, the Engine will draw FPS and other info instead - std::stringstream fpsInfo; - fpsInfo.precision(2); -#ifdef SNAPSHOT - fpsInfo << "Snapshot " << SNAPSHOT_ID << ", "; -#elif defined(BETA) - fpsInfo << "Beta " << SAVE_VERSION << "." << MINOR_VERSION << "." << BUILD_NUM << ", "; -#endif - fpsInfo << "FPS: " << std::fixed << ui::Engine::Ref().GetFps(); - - if (showDebug) - fpsInfo << " Parts: " << sample.NumParts; - if (ren->GetGridSize()) - fpsInfo << " [GRID: " << ren->GetGridSize() << "]"; - - textWidth = Graphics::textwidth((char*)fpsInfo.str().c_str()); - g->fillrect(12, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); - g->drawtext(16, 16, (const char*)fpsInfo.str().c_str(), 32, 216, 255, 255*0.75); -#endif - } - - //Tooltips - if(infoTipPresence) - { - int infoTipAlpha = (infoTipPresence>50?50:infoTipPresence)*5; - g->drawtext((XRES-Graphics::textwidth((char*)infoTip.c_str()))/2, (YRES/2)-2, (char*)infoTip.c_str(), 255, 255, 255, infoTipAlpha); - } - - if(toolTipPresence && toolTipPosition.X!=-1 && toolTipPosition.Y!=-1 && toolTip.length()) - { - g->drawtext(toolTipPosition.X, toolTipPosition.Y, (char*)toolTip.c_str(), 255, 255, 255, toolTipPresence>51?255:toolTipPresence*5); - } - - if(buttonTipShow > 0) - { - g->drawtext(6, Size.Y-MENUSIZE-10, (char*)buttonTip.c_str(), 255, 255, 255, buttonTipShow>51?255:buttonTipShow*5); - } - - //Introduction text - if(introText) - { - g->fillrect(0, 0, XRES+BARSIZE, YRES+MENUSIZE, 0, 0, 0, introText>51?102:introText*2); - g->drawtext(16, 20, (char*)introTextMessage.c_str(), 255, 255, 255, introText>51?255:introText*5); - } -} - -ui::Point GameView::lineSnapCoords(ui::Point point1, ui::Point point2) -{ - ui::Point newPoint(0, 0); - float snapAngle = floor(atan2((float)point2.Y-point1.Y, point2.X-point1.X)/(M_PI*0.25)+0.5)*M_PI*0.25; - float lineMag = sqrtf(pow((float)(point2.X-point1.X),2)+pow((float)(point2.Y-point1.Y),2)); - newPoint.X = (int)(lineMag*cos(snapAngle)+point1.X+0.5f); - newPoint.Y = (int)(lineMag*sin(snapAngle)+point1.Y+0.5f); - return newPoint; -} - -ui::Point GameView::rectSnapCoords(ui::Point point1, ui::Point point2) -{ - ui::Point newPoint(0, 0); - float snapAngle = floor((atan2((float)point2.Y-point1.Y, point2.X-point1.X)+M_PI*0.25)/(M_PI*0.5)+0.5)*M_PI*0.5 - M_PI*0.25; - float lineMag = sqrtf(pow((float)(point2.X-point1.X),2)+pow((float)(point2.Y-point1.Y),2)); - newPoint.X = (int)(lineMag*cos(snapAngle)+point1.X+0.5f); - newPoint.Y = (int)(lineMag*sin(snapAngle)+point1.Y+0.5f); - return newPoint; -} diff --git a/src/game/GameView.h b/src/game/GameView.h deleted file mode 100644 index 00dec5d..0000000 --- a/src/game/GameView.h +++ /dev/null @@ -1,189 +0,0 @@ -#ifndef GAMEVIEW_H -#define GAMEVIEW_H - -#include -#include -#include -#include -#include "GameController.h" -#include "GameModel.h" -#include "interface/Window.h" -#include "interface/Point.h" -#include "interface/Button.h" -#include "interface/Slider.h" -#include "interface/Textbox.h" -#include "ToolButton.h" -#include "Brush.h" -#include "simulation/Sample.h" - -using namespace std; - -enum DrawMode -{ - DrawPoints, DrawLine, DrawRect, DrawFill -}; - -enum SelectMode -{ - SelectNone, SelectStamp, SelectCopy, SelectCut, PlaceSave -}; - -class GameController; -class GameModel; -class GameView: public ui::Window -{ -private: - DrawMode drawMode; - - bool doScreenshot; - bool recording; - int screenshotIndex; - int recordingIndex; - - bool isMouseDown; - bool zoomEnabled; - bool zoomCursorFixed; - bool drawSnap; - bool shiftBehaviour; - bool ctrlBehaviour; - bool altBehaviour; - bool showHud; - bool showDebug; - bool wallBrush; - int introText; - int buttonTipShow; - std::string buttonTip; - std::string introTextMessage; - int toolIndex; - int currentSaveType; - Menu * lastMenu; - - int infoTipPresence; - std::string toolTip; - ui::Point toolTipPosition; - std::string infoTip; - int toolTipPresence; - - queue pointQueue; - GameController * c; - Renderer * ren; - Brush * activeBrush; - //UI Elements - vector quickOptionButtons; - vector menuButtons; - vector toolButtons; - vector notificationComponents; - deque logEntries; - float lastLogEntry; - ui::Button * scrollBar; - ui::Button * searchButton; - ui::Button * reloadButton; - ui::Button * saveSimulationButton; - ui::Button * downVoteButton; - ui::Button * upVoteButton; - ui::Button * tagSimulationButton; - ui::Button * clearSimButton; - ui::Button * loginButton; - ui::Button * simulationOptionButton; - ui::Button * displayModeButton; - ui::Button * pauseButton; - ui::Point currentMouse; - - ui::Button * colourPicker; - vector colourPresets; - - bool drawModeReset; - ui::Point drawPoint1; - ui::Point drawPoint2; - - SelectMode selectMode; - ui::Point selectPoint1; - ui::Point selectPoint2; - - ui::Point mousePosition; - - VideoBuffer * placeSaveThumb; - - SimulationSample sample; - - int lastOffset; - void setToolButtonOffset(int offset); - virtual ui::Point lineSnapCoords(ui::Point point1, ui::Point point2); - virtual ui::Point rectSnapCoords(ui::Point point1, ui::Point point2); - - void screenshot(); - void record(); - - void enableShiftBehaviour(); - void disableShiftBehaviour(); - void enableCtrlBehaviour(); - void disableCtrlBehaviour(); - void enableAltBehaviour(); - void disableAltBehaviour(); -public: - GameView(); - virtual ~GameView(); - - //Breaks MVC, but any other way is going to be more of a mess. - ui::Point GetMousePosition(); - void SetSample(SimulationSample sample); - void SetHudEnable(bool hudState); - bool CtrlBehaviour(){ return ctrlBehaviour; } - bool ShiftBehaviour(){ return shiftBehaviour; } - void ExitPrompt(); - void ToggleDebug(); - SelectMode GetSelectMode() { return selectMode; } - void BeginStampSelection(); - - void AttachController(GameController * _c){ c = _c; } - void NotifyRendererChanged(GameModel * sender); - void NotifySimulationChanged(GameModel * sender); - void NotifyPausedChanged(GameModel * sender); - void NotifySaveChanged(GameModel * sender); - void NotifyBrushChanged(GameModel * sender); - void NotifyMenuListChanged(GameModel * sender); - void NotifyToolListChanged(GameModel * sender); - void NotifyActiveToolsChanged(GameModel * sender); - void NotifyUserChanged(GameModel * sender); - void NotifyZoomChanged(GameModel * sender); - void NotifyColourSelectorVisibilityChanged(GameModel * sender); - void NotifyColourSelectorColourChanged(GameModel * sender); - void NotifyColourPresetsChanged(GameModel * sender); - void NotifyColourActivePresetChanged(GameModel * sender); - void NotifyPlaceSaveChanged(GameModel * sender); - void NotifyNotificationsChanged(GameModel * sender); - void NotifyLogChanged(GameModel * sender, string entry); - void NotifyToolTipChanged(GameModel * sender); - void NotifyInfoTipChanged(GameModel * sender); - void NotifyQuickOptionsChanged(GameModel * sender); - void NotifyLastToolChanged(GameModel * sender); - - - virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip); - - virtual void OnMouseMove(int x, int y, int dx, int dy); - virtual void OnMouseDown(int x, int y, unsigned button); - virtual void OnMouseUp(int x, int y, unsigned button); - virtual void OnMouseWheel(int x, int y, int d); - virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); - virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); - virtual void OnTick(float dt); - virtual void OnDraw(); - virtual void OnBlur(); - - //Top-level handlers, for Lua interface - virtual void DoDraw(); - virtual void DoMouseMove(int x, int y, int dx, int dy); - virtual void DoMouseDown(int x, int y, unsigned button); - virtual void DoMouseUp(int x, int y, unsigned button); - virtual void DoMouseWheel(int x, int y, int d); - virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); - virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); - - class MenuAction; - class ToolAction; - class OptionAction; - class OptionListener; -}; - -#endif // GAMEVIEW_H diff --git a/src/game/Menu.h b/src/game/Menu.h deleted file mode 100644 index 3f955e1..0000000 --- a/src/game/Menu.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef MENU_H_ -#define MENU_H_ - -#include "Tool.h" - -class Menu -{ - char icon; - string description; - vector tools; -public: - Menu(char icon_, string description_): - icon(icon_), - description(description_), - tools(vector()) - { - - } - - virtual ~Menu() - { - for(int i = 0; i < tools.size(); i++) - { - delete tools[i]; - } - tools.clear(); - } - - vector GetToolList() - { - return tools; - } - - char GetIcon() - { - return icon; - } - - string GetDescription() - { - return description; - } - - void AddTool(Tool * tool_) - { - tools.push_back(tool_); - } -}; - - -#endif /* MENU_H_ */ diff --git a/src/game/Notification.h b/src/game/Notification.h deleted file mode 100644 index b2bf6f8..0000000 --- a/src/game/Notification.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef NOTIFICATION_H_ -#define NOTIFICATION_H_ - -#include - -class Notification -{ -public: - Notification(std::string message) : Message(message) {} - virtual ~Notification() {}; - std::string Message; - - virtual void Action() { } -}; - -#endif /* NOTIFICATION_H_ */ diff --git a/src/game/PropertyTool.cpp b/src/game/PropertyTool.cpp deleted file mode 100644 index ee9f9eb..0000000 --- a/src/game/PropertyTool.cpp +++ /dev/null @@ -1,235 +0,0 @@ -#include -#include -#include "Style.h" -#include "simulation/Simulation.h" -#include "Tool.h" -#include "interface/Window.h" -#include "interface/Button.h" -#include "interface/Label.h" -#include "interface/Textbox.h" -#include "interface/DropDown.h" -#include "interface/Keys.h" -#include "dialogues/ErrorMessage.h" - -class PropertyWindow: public ui::Window -{ -public: - ui::DropDown * property; - ui::Textbox * textField; - SignTool * tool; - Simulation * sim; - int signID; - ui::Point position; - std::vector properties; - PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_); - void SetProperty(); - virtual void OnDraw(); - virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); - virtual void OnTryExit(ExitMethod method); - virtual ~PropertyWindow() {} - class OkayAction: public ui::ButtonAction - { - public: - PropertyWindow * prompt; - OkayAction(PropertyWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) - { - ui::Engine::Ref().CloseWindow(); - if(prompt->textField->GetText().length()) - prompt->SetProperty(); - prompt->SelfDestruct(); - return; - } - }; -}; - -PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_): -ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), -sim(sim_), -position(position_) -{ - properties = Particle::GetProperties(); - - ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Edit property"); - messageLabel->SetTextColour(style::Colour::InformationTitle); - messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; - AddComponent(messageLabel); - - ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-17), ui::Point(Size.X, 17), "OK"); - okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - okayButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); - okayButton->SetActionCallback(new OkayAction(this)); - AddComponent(okayButton); - SetOkayButton(okayButton); - - class PropertyChanged: public ui::DropDownAction - { - PropertyWindow * w; - public: - PropertyChanged(PropertyWindow * w): w(w) { } - virtual void OptionChanged(ui::DropDown * sender, std::pair option) - { - w->FocusComponent(w->textField); - } - }; - property = new ui::DropDown(ui::Point(8, 25), ui::Point(Size.X-16, 17)); - property->SetActionCallback(new PropertyChanged(this)); - AddComponent(property); - for(int i = 0; i < properties.size(); i++) - { - property->AddOption(std::pair(properties[i].Name, i)); - } - property->SetOption(0); - - textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "", "[value]"); - textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(textField); - FocusComponent(textField); - - ui::Engine::Ref().ShowWindow(this); -} -void PropertyWindow::SetProperty() -{ - if(property->GetOption().second!=-1 && textField->GetText().length() > 0) - { - void * propValue; - int tempInt; - unsigned int tempUInt; - float tempFloat; - std::string value = textField->GetText(); - try { - switch(properties[property->GetOption().second].Type) - { - case StructProperty::Integer: - case StructProperty::ParticleType: - if(value.length() > 2 && value.substr(0, 2) == "0x") - { - //0xC0FFEE - std::stringstream buffer; - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer << std::hex << value.substr(2); - buffer >> tempInt; - } - else if(value.length() > 1 && value[0] == '#') - { - //#C0FFEE - std::stringstream buffer; - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer << std::hex << value.substr(1); - buffer >> tempInt; - } - else - { - if(properties[property->GetOption().second].Type == StructProperty::ParticleType) - { - int type = sim->GetParticleType(value); - if(type != -1) - { -#ifdef DEBUG - std::cout << "Got type from particle name" << std::endl; -#endif - tempInt = type; - } - else - { - std::stringstream buffer(value); - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer >> tempInt; - } - } - else - { - std::stringstream buffer(value); - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer >> tempInt; - } - } -#ifdef DEBUG - std::cout << "Got int value " << tempInt << std::endl; -#endif - propValue = &tempInt; - break; - case StructProperty::UInteger: - if(value.length() > 2 && value.substr(0, 2) == "0x") - { - //0xC0FFEE - std::stringstream buffer; - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer << std::hex << value.substr(2); - buffer >> tempUInt; - } - else if(value.length() > 1 && value[0] == '#') - { - //#C0FFEE - std::stringstream buffer; - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer << std::hex << value.substr(1); - buffer >> tempUInt; - } - else - { - std::stringstream buffer(value); - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer >> tempUInt; - } -#ifdef DEBUG - std::cout << "Got uint value " << tempUInt << std::endl; -#endif - propValue = &tempUInt; - break; - case StructProperty::Float: - { - std::stringstream buffer(value); - buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); - buffer >> tempFloat; -#ifdef DEBUG - std::cout << "Got float value " << tempFloat << std::endl; -#endif - propValue = &tempFloat; - } - break; - default: - new ErrorMessage("Could not set property", "Invalid property"); - } - sim->flood_prop( - position.X, - position.Y, - properties[property->GetOption().second].Offset, - propValue, - properties[property->GetOption().second].Type - ); - } catch (const std::exception& ex) { - new ErrorMessage("Could not set property", "Invalid value provided"); - } - } -} - -void PropertyWindow::OnTryExit(ExitMethod method) -{ - ui::Engine::Ref().CloseWindow(); - SelfDestruct(); -} - -void PropertyWindow::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -void PropertyWindow::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) -{ - if (key == KEY_UP) - property->SetOption(property->GetOption().second-1); - else if (key == KEY_DOWN) - property->SetOption(property->GetOption().second+1); -} - -void PropertyTool::Click(Simulation * sim, Brush * brush, ui::Point position) -{ - new PropertyWindow(this, sim, position); -} \ No newline at end of file diff --git a/src/game/QuickOption.h b/src/game/QuickOption.h deleted file mode 100644 index 4bf27ff..0000000 --- a/src/game/QuickOption.h +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#include -#include - -class GameModel; -class QuickOption; -class QuickOptionListener -{ -protected: - QuickOptionListener() {} -public: - virtual ~QuickOptionListener() {} - virtual void OnValueChanged(QuickOption * sender) {} -}; -class QuickOption -{ -public: - enum Type { - Toggle, Multi - }; -protected: - std::vector listeners; - GameModel * m; - Type type; - std::string icon; - std::string description; - QuickOption(std::string icon, std::string description, GameModel * m, Type type) : - icon(icon), - description(description), - m(m), - type(type) - { - - } - virtual void perform() {} -public: - virtual ~QuickOption() - { - //for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) - // delete *iter; - } - - std::vector GetListeners() - { - return listeners; - } - - void AddListener(QuickOptionListener * listener) - { - listeners.push_back(listener); - } - - Type GetType() { return type; } - - virtual bool GetToggle() { return true;} - virtual int GetMutli() { return 0;} - virtual int GetMultiCount() { return 0;} - - std::string GetIcon() { return icon; } - void SetIcon(std::string icon) { this->icon = icon; } - std::string GetDescription() { return description; } - void SetDescription(std::string description) { this->description = description; } - void Perform() - { - perform(); - for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) - (*iter)->OnValueChanged(this); - } - void Update() - { - for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) - (*iter)->OnValueChanged(this); - } -}; - diff --git a/src/game/QuickOptions.h b/src/game/QuickOptions.h deleted file mode 100644 index 7c31f9c..0000000 --- a/src/game/QuickOptions.h +++ /dev/null @@ -1,120 +0,0 @@ -#include "QuickOption.h" -#include "GameModel.h" - -class SandEffectOption: public QuickOption -{ -public: - SandEffectOption(GameModel * m): - QuickOption("P", "Sand effect", m, Toggle) - { - - } - virtual bool GetToggle() - { - return m->GetSimulation()->pretty_powder; - } - virtual void perform() - { - m->GetSimulation()->pretty_powder = !m->GetSimulation()->pretty_powder; - } -}; - -class DrawGravOption: public QuickOption -{ -public: - DrawGravOption(GameModel * m): - QuickOption("G", "Draw gravity field", m, Toggle) - { - - } - virtual bool GetToggle() - { - return m->GetGravityGrid(); - } - virtual void perform() - { - m->ShowGravityGrid(!m->GetGravityGrid()); - } -}; - -class DecorationsOption: public QuickOption -{ -public: - DecorationsOption(GameModel * m): - QuickOption("D", "Draw decorations", m, Toggle) - { - - } - virtual bool GetToggle() - { - return m->GetDecoration(); - } - virtual void perform() - { - m->SetDecoration(!m->GetDecoration()); - } -}; - -class NGravityOption: public QuickOption -{ -public: - NGravityOption(GameModel * m): - QuickOption("N", "Newtonian Gravity", m, Toggle) - { - - } - virtual bool GetToggle() - { - return m->GetSimulation()->grav->ngrav_enable; - } - virtual void perform() - { - if(m->GetSimulation()->grav->ngrav_enable) - { - m->GetSimulation()->grav->stop_grav_async(); - m->SetInfoTip("Newtonian Gravity: Off"); - } - else - { - m->GetSimulation()->grav->start_grav_async(); - m->SetInfoTip("Newtonian Gravity: On"); - } - } -}; - -class AHeatOption: public QuickOption -{ -public: - AHeatOption(GameModel * m): - QuickOption("A", "Ambient heat", m, Toggle) - { - - } - virtual bool GetToggle() - { - return m->GetAHeatEnable(); - } - virtual void perform() - { - m->SetAHeatEnable(!m->GetAHeatEnable()); - } -}; - -class ConsoleShowOption: public QuickOption -{ - GameController * c; -public: - ConsoleShowOption(GameModel * m, GameController * c_): - QuickOption("C", "Show Console", m, Toggle) - { - c = c_; - } - virtual bool GetToggle() - { - return 0; - } - virtual void perform() - { - c->ShowConsole(); - } -}; diff --git a/src/game/RenderPreset.h b/src/game/RenderPreset.h deleted file mode 100644 index 9cc9f4c..0000000 --- a/src/game/RenderPreset.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef RENDER_PRESET_H -#define RENDER_PRESET_H -class RenderPreset -{ -public: - std::string Name; - std::vector RenderModes; - std::vector DisplayModes; - unsigned int ColourMode; - - RenderPreset(): Name(""), ColourMode(0) {} - RenderPreset(std::string name, std::vector renderModes, std::vector displayModes, unsigned int colourMode): - Name(name), - RenderModes(renderModes), - DisplayModes(displayModes), - ColourMode(colourMode) - {} -}; -#endif \ No newline at end of file diff --git a/src/game/SampleTool.cpp b/src/game/SampleTool.cpp deleted file mode 100644 index a42a214..0000000 --- a/src/game/SampleTool.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include "graphics/Graphics.h" -#include "Tool.h" -#include "GameModel.h" -#include "interface/Colour.h" - -VideoBuffer * SampleTool::GetIcon(int toolID, int width, int height) -{ - VideoBuffer * newTexture = new VideoBuffer(width, height); - for (int y=0; ySetPixel(x, y, PIXR(pc), PIXG(pc), PIXB(pc), 255); - } - } - newTexture->SetCharacter((width/2)-5, (height/2)-5, 0xE6, 255, 255, 255, 255); - newTexture->BlendCharacter((width/2)-5, (height/2)-5, 0xE7, 100, 180, 255, 255); - return newTexture; -} - -void SampleTool::Draw(Simulation * sim, Brush * brush, ui::Point position) -{ - if(gameModel->GetColourSelectorVisibility()) - { - pixel colour = gameModel->GetRenderer()->sampleColor; - gameModel->SetColourSelectorColour(ui::Colour(PIXR(colour), PIXG(colour), PIXB(colour), 255)); - } - else - { - int particleType = 0; - int particleCtype = 0; - if (sim->photons[position.Y][position.X]) - { - particleType = sim->parts[sim->photons[position.Y][position.X]>>8].type; - particleCtype = sim->parts[sim->pmap[position.Y][position.X]>>8].ctype; - } - else if (sim->pmap[position.Y][position.X]) - { - particleType = sim->parts[sim->pmap[position.Y][position.X]>>8].type; - particleCtype = sim->parts[sim->pmap[position.Y][position.X]>>8].ctype; - } - - if(particleType) - { - if(particleType == PT_LIFE) - { - Menu * lifeMenu = gameModel->GetMenuList()[SC_LIFE]; - std::vector elementTools = lifeMenu->GetToolList(); - - for(std::vector::iterator iter = elementTools.begin(), end = elementTools.end(); iter != end; ++iter) - { - Tool * elementTool = *iter; - if(elementTool && elementTool->GetToolID() == particleCtype) - gameModel->SetActiveTool(0, elementTool); - } - } - else - { - Tool * elementTool = gameModel->GetElementTool(particleType); - if(elementTool) - gameModel->SetActiveTool(0, elementTool); - } - } - } -} diff --git a/src/game/SignTool.cpp b/src/game/SignTool.cpp deleted file mode 100644 index d2bba88..0000000 --- a/src/game/SignTool.cpp +++ /dev/null @@ -1,278 +0,0 @@ -#include -#include "Style.h" -#include "simulation/Simulation.h" -#include "Tool.h" -#include "interface/Window.h" -#include "interface/Button.h" -#include "interface/Label.h" -#include "interface/Textbox.h" -#include "interface/DropDown.h" - -class SignWindow: public ui::Window -{ -public: - ui::DropDown * justification; - ui::Textbox * textField; - SignTool * tool; - sign * movingSign; - bool signMoving; - Simulation * sim; - int signID; - ui::Point signPosition; - SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_); - virtual void OnDraw(); - virtual void DoDraw(); - virtual void DoMouseMove(int x, int y, int dx, int dy); - virtual void DoMouseDown(int x, int y, unsigned button); - virtual void DoMouseUp(int x, int y, unsigned button) { if(!signMoving) ui::Window::DoMouseUp(x, y, button); } - virtual void DoMouseWheel(int x, int y, int d) { if(!signMoving) ui::Window::DoMouseWheel(x, y, d); } - virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyPress(key, character, shift, ctrl, alt); }; - virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyRelease(key, character, shift, ctrl, alt); }; - virtual ~SignWindow() {} - virtual void OnTryExit(ui::Window::ExitMethod method); - class OkayAction: public ui::ButtonAction - { - public: - SignWindow * prompt; - OkayAction(SignWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) - { - ui::Engine::Ref().CloseWindow(); - if(prompt->signID==-1 && prompt->textField->GetText().length()) - { - prompt->sim->signs.push_back(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); - } - else if(prompt->signID!=-1 && prompt->textField->GetText().length()) - { - prompt->sim->signs[prompt->signID] = sign(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); - } - prompt->SelfDestruct(); - } - }; - class DeleteAction: public ui::ButtonAction - { - public: - SignWindow * prompt; - DeleteAction(SignWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) - { - ui::Engine::Ref().CloseWindow(); - if(prompt->signID!=-1) - { - prompt->sim->signs.erase(prompt->sim->signs.begin()+prompt->signID); - } - prompt->SelfDestruct(); - } - }; - - class SignTextAction: public ui::TextboxAction - { - public: - SignWindow * prompt; - SignTextAction(SignWindow * prompt_) { prompt = prompt_; } - virtual void TextChangedCallback(ui::Textbox * sender) - { - if(prompt->signID!=-1) - { - prompt->sim->signs[prompt->signID].text = sender->GetText(); - prompt->sim->signs[prompt->signID].ju = (sign::Justification)prompt->justification->GetOption().second; - } - } - }; - - class MoveAction: public ui::ButtonAction - { - public: - SignWindow * prompt; - MoveAction(SignWindow * prompt_) { prompt = prompt_; } - void ActionCallback(ui::Button * sender) - { - if(prompt->signID!=-1) - { - prompt->movingSign = &prompt->sim->signs[prompt->signID]; - prompt->sim->signs[prompt->signID].ju = (sign::Justification)prompt->justification->GetOption().second; - prompt->signMoving = true; - } - } - }; -}; - -SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_): - ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), - tool(tool_), - signID(signID_), - sim(sim_), - signPosition(position_), - movingSign(NULL), - signMoving(false) -{ - ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), "New sign"); - messageLabel->SetTextColour(style::Colour::InformationTitle); - messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(messageLabel); - - ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "OK"); - okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - okayButton->Appearance.BorderInactive = (ui::Colour(200, 200, 200)); - okayButton->SetActionCallback(new OkayAction(this)); - AddComponent(okayButton); - SetOkayButton(okayButton); - - ui::Label * tempLabel = new ui::Label(ui::Point(8, 48), ui::Point(40, 15), "Justify:"); - okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - AddComponent(tempLabel); - - justification = new ui::DropDown(ui::Point(52, 48), ui::Point(50, 16)); - AddComponent(justification); - justification->AddOption(std::pair("\x9D Left", (int)sign::Left)); - justification->AddOption(std::pair("\x9E Centre", (int)sign::Centre)); - justification->AddOption(std::pair("\x9F Right", (int)sign::Right)); - justification->SetOption(1); - justification->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - - textField = new ui::Textbox(ui::Point(8, 25), ui::Point(Size.X-16, 17), "", "[message]"); - textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; - textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; - textField->SetActionCallback(new SignTextAction(this)); - AddComponent(textField); - FocusComponent(textField); - - if(signID!=-1) - { - messageLabel->SetText("Edit sign"); - - textField->SetText(sim->signs[signID].text); - justification->SetOption(sim->signs[signID].ju); - - ui::Point position = ui::Point(justification->Position.X+justification->Size.X+3, 48); - ui::Button * moveButton = new ui::Button(position, ui::Point(((Size.X-position.X-8)/2)-2, 16), "Move"); - moveButton->SetActionCallback(new MoveAction(this)); - AddComponent(moveButton); - - position = ui::Point(justification->Position.X+justification->Size.X+3, 48)+ui::Point(moveButton->Size.X+3, 0); - ui::Button * deleteButton = new ui::Button(position, ui::Point((Size.X-position.X-8)-1, 16), "Delete"); - //deleteButton->SetIcon(IconDelete); - deleteButton->SetActionCallback(new DeleteAction(this)); - - signPosition.X = sim->signs[signID].x; - signPosition.Y = sim->signs[signID].y; - - AddComponent(deleteButton); - } - - ui::Engine::Ref().ShowWindow(this); -} - -void SignWindow::OnTryExit(ui::Window::ExitMethod method) -{ - ui::Engine::Ref().CloseWindow(); - SelfDestruct(); -} - -void SignWindow::DoDraw() -{ - for(std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) - { - sign & currentSign = *iter; - int x, y, w, h, dx, dy; - Graphics * g = ui::Engine::Ref().g; - std::string text = currentSign.getText(sim); - currentSign.pos(text, x, y, w, h); - g->clearrect(x, y, w+1, h); - g->drawrect(x, y, w+1, h, 192, 192, 192, 255); - if (sregexp(currentSign.text.c_str(), "^{[c|t]:[0-9]*|.*}$")) - g->drawtext(x+3, y+3, text, 255, 255, 255, 255); - else - g->drawtext(x+3, y+3, text, 0, 191, 255, 255); - - x = currentSign.x; - y = currentSign.y; - dx = 1 - currentSign.ju; - dy = (currentSign.y > 18) ? -1 : 1; -#ifdef OGLR - glBegin(GL_LINES); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - glVertex2i(x, y); - glVertex2i(x+(dx*4), y+(dy*4)); - glEnd(); -#else - for (int j=0; j<4; j++) - { - g->blendpixel(x, y, 192, 192, 192, 255); - x+=dx; - y+=dy; - } -#endif - } - if(!signMoving) - { - ui::Window::DoDraw(); - } -} - -void SignWindow::DoMouseMove(int x, int y, int dx, int dy) { - if(!signMoving) - ui::Window::DoMouseMove(x, y, dx, dy); - else - { - if(x < XRES && y < YRES) - { - movingSign->x = x; - movingSign->y = y; - signPosition.X = x; - signPosition.Y = y; - } - } -} - -void SignWindow::DoMouseDown(int x, int y, unsigned button) -{ - if(!signMoving) - ui::Window::DoMouseDown(x, y, button); - else - { - signMoving = false; - } -} - -void SignWindow::OnDraw() -{ - Graphics * g = ui::Engine::Ref().g; - - g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); -} - -VideoBuffer * SignTool::GetIcon(int toolID, int width, int height) -{ - VideoBuffer * newTexture = new VideoBuffer(width, height); - for (int y=0; ySetPixel(x, y, PIXR(pc), PIXG(pc), PIXB(pc), 255); - } - } - newTexture->SetCharacter((width/2)-5, (height/2)-5, 0xA1, 32, 64, 128, 255); - newTexture->BlendCharacter((width/2)-5, (height/2)-5, 0xA0, 255, 255, 255, 255); - return newTexture; -} - -void SignTool::Click(Simulation * sim, Brush * brush, ui::Point position) -{ - int signX, signY, signW, signH, signIndex = -1; - for(int i = 0; i < sim->signs.size(); i++){ - sim->signs[i].pos(sim->signs[i].getText(sim), signX, signY, signW, signH); - if(position.X > signX && position.X < signX+signW && position.Y > signY && position.Y < signY+signH) - { - signIndex = i; - break; - } - } - if (signIndex != -1 || sim->signs.size() < MAXSIGNS) - new SignWindow(this, sim, signIndex, position); -} diff --git a/src/game/Tool.cpp b/src/game/Tool.cpp deleted file mode 100644 index 42f9b59..0000000 --- a/src/game/Tool.cpp +++ /dev/null @@ -1,215 +0,0 @@ -#include -#include "Tool.h" -#include "game/Brush.h" - -#include "simulation/Simulation.h" - -using namespace std; - -Tool::Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): - toolID(id), - toolName(name), - toolDescription(description), - colRed(r), - colGreen(g), - colBlue(b), - textureGen(textureGen), - strength(1.0f), - resolution(1), - identifier(identifier) -{ -} - -VideoBuffer * Tool::GetTexture(int width, int height) -{ - if(textureGen) - { - return textureGen(toolID, width, height); - } - return NULL; -} -void Tool::SetTextureGen(VideoBuffer * (*textureGen)(int, int, int)) -{ - this->textureGen = textureGen; -} -std::string Tool::GetIdentifier() { return identifier; } -string Tool::GetName() { return toolName; } -string Tool::GetDescription() { return toolDescription; } -Tool::~Tool() {} - -void Tool::Click(Simulation * sim, Brush * brush, ui::Point position) { } -void Tool::Draw(Simulation * sim, Brush * brush, ui::Point position) { - sim->ToolBrush(position.X, position.Y, toolID, brush, strength); -} -void Tool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - sim->ToolLine(position1.X, position1.Y, position2.X, position2.Y, toolID, brush, strength); -} -void Tool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->ToolBox(position1.X, position1.Y, position2.X, position2.Y, toolID, brush, strength); -} -void Tool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) {}; - - -ElementTool::ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, identifier, textureGen) -{ -} -ElementTool::~ElementTool() {} -void ElementTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ - sim->CreateParts(position.X, position.Y, toolID, brush); -} -void ElementTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, toolID, brush); -} -void ElementTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, toolID, 0); -} -void ElementTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { - sim->FloodParts(position.X, position.Y, toolID, -1, -1, 0); -} - - -WallTool::WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): -Tool(id, name, description, r, g, b, identifier, textureGen) -{ - resolution = CELL; -} -WallTool::~WallTool() {} -void WallTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ - sim->CreateWalls(position.X, position.Y, 1, 1, toolID, 0, brush); -} -void WallTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - int wallX = position1.X/CELL; - int wallY = position1.Y/CELL; - if(dragging == false && toolID == WL_FAN && sim->bmap[wallY][wallX]==WL_FAN) - { - float newFanVelX = (position2.X-position1.X)*0.005f; - newFanVelX *= strength; - float newFanVelY = (position2.Y-position1.Y)*0.005f; - newFanVelY *= strength; - sim->FloodWalls(position1.X, position1.Y, WL_FLOODHELPER, -1, WL_FAN, 0); - for (int j = 0; j < YRES/CELL; j++) - for (int i = 0; i < XRES/CELL; i++) - if (sim->bmap[j][i] == WL_FLOODHELPER) - { - sim->fvx[j][i] = newFanVelX; - sim->fvy[j][i] = newFanVelY; - sim->bmap[j][i] = WL_FAN; - } - } - else - { - sim->CreateWallLine(position1.X, position1.Y, position2.X, position2.Y, 1, 1, toolID, 0, brush); - } -} -void WallTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->CreateWallBox(position1.X, position1.Y, position2.X, position2.Y, toolID, 0); -} -void WallTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { - if (toolID != WL_STREAM) - sim->FloodWalls(position.X, position.Y, toolID, -1, -1, 0); -} - - -GolTool::GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, identifier, textureGen) -{ -} -GolTool::~GolTool() {} -void GolTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ - sim->CreateParts(position.X, position.Y, PT_LIFE|(toolID<<8), brush); -} -void GolTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, PT_LIFE|(toolID<<8), brush); -} -void GolTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, PT_LIFE|(toolID<<8), 0); -} -void GolTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { - sim->FloodParts(position.X, position.Y, PT_LIFE|(toolID<<8), -1, -1, 0); -} - - -WindTool::WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): - Tool(id, name, description, r, g, b, identifier, textureGen) -{ -} -WindTool::~WindTool() {} -void WindTool::Draw(Simulation * sim, Brush * brush, ui::Point position) -{ - -} -void WindTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) -{ - int radiusX, radiusY, sizeX, sizeY; - - float strength = dragging?0.01f:0.002f; - strength *= this->strength; - - radiusX = brush->GetRadius().X; - radiusY = brush->GetRadius().Y; - - sizeX = brush->GetSize().X; - sizeY = brush->GetSize().Y; - - unsigned char *bitmap = brush->GetBitmap(); - - for(int y = 0; y < sizeY; y++) - { - for(int x = 0; x < sizeX; x++) - { - if(bitmap[(y*sizeX)+x] && (position1.X+(x-radiusX) >= 0 && position1.Y+(y-radiusY) >= 0 && position1.X+(x-radiusX) < XRES && position1.Y+(y-radiusY) < YRES)) - { - sim->vx[(position1.Y+(y-radiusY))/CELL][(position1.X+(x-radiusX))/CELL] += (position2.X-position1.X)*strength; - sim->vy[(position1.Y+(y-radiusY))/CELL][(position1.X+(x-radiusX))/CELL] += (position2.Y-position1.Y)*strength; - } - } - } -} -void WindTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {} -void WindTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) {} - - -void Element_LIGH_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position) -{ - if(sim->currentTick >= nextUse) - { - int p = sim->create_part(-2, position.X, position.Y, toolID); - if (p != -1) - { - sim->parts[p].life = brush->GetRadius().X+brush->GetRadius().Y; - if (sim->parts[p].life > 55) - sim->parts[p].life = 55; - sim->parts[p].temp = sim->parts[p].life*150; // temperature of the lighting shows the power of the lighting - nextUse = sim->currentTick+sim->parts[p].life/4; - } - } -} - - -Element_TESC_Tool::Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): - ElementTool(id, name, description, r, g, b, identifier, textureGen) - { - } -void Element_TESC_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position){ - int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; - sim->CreateParts(position.X, position.Y, toolID | (radiusInfo << 8), brush); -} -void Element_TESC_Tool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { - int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; - sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, toolID | (radiusInfo << 8), brush); -} -void Element_TESC_Tool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { - int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; - sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, toolID | (radiusInfo << 8), 0); -} -void Element_TESC_Tool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { - int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; - sim->FloodParts(position.X, position.Y, toolID | (radiusInfo << 8), -1, -1, 0); -} - - -void PlopTool::Click(Simulation * sim, Brush * brush, ui::Point position) -{ - sim->create_part(-1, position.X, position.Y, toolID); -} \ No newline at end of file diff --git a/src/game/Tool.h b/src/game/Tool.h deleted file mode 100644 index 0c8ab65..0000000 --- a/src/game/Tool.h +++ /dev/null @@ -1,183 +0,0 @@ -#ifndef TOOL_H_ -#define TOOL_H_ - -#include - -using namespace std; - -#include "interface/Point.h" - -class Simulation; -class Brush; -class VideoBuffer; - -class Tool -{ -protected: - VideoBuffer * (*textureGen)(int, int, int); - int toolID; - string toolName; - string toolDescription; - float strength; - int resolution; - std::string identifier; -public: - Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - int GetToolID() { return toolID; } - string GetName(); - string GetDescription(); - std::string GetIdentifier(); - int GetResolution() { return resolution; } - void SetStrength(float value) { strength = value; } - float GetStrength() { return strength; } - VideoBuffer * GetTexture(int width, int height); - void SetTextureGen(VideoBuffer * (*textureGen)(int, int, int)); - virtual ~Tool(); - virtual void Click(Simulation * sim, Brush * brush, ui::Point position); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); - int colRed, colBlue, colGreen; -}; - -class SignTool: public Tool -{ -public: - SignTool(): - Tool(0, "SIGN", "Sign. Click a sign to edit or anywhere else to create a new one", 0, 0, 0, "DEFAULT_UI_SIGN", SignTool::GetIcon) - { - } - static VideoBuffer * GetIcon(int toolID, int width, int height); - virtual ~SignTool() {} - virtual void Click(Simulation * sim, Brush * brush, ui::Point position); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) { } - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -}; - -class GameModel; - -class SampleTool: public Tool -{ - GameModel * gameModel; -public: - SampleTool(GameModel * model): - Tool(0, "SMPL", "Sample an element on the screen", 0, 0, 0, "DEFAULT_UI_SAMPLE", SampleTool::GetIcon), - gameModel(model) - { - } - static VideoBuffer * GetIcon(int toolID, int width, int height); - virtual ~SampleTool() {} - virtual void Click(Simulation * sim, Brush * brush, ui::Point position) { } - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -}; - -class PropertyTool: public Tool -{ -public: - PropertyTool(): - Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL) - { - } - virtual ~PropertyTool() {} - virtual void Click(Simulation * sim, Brush * brush, ui::Point position); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {}; - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -}; - -class Element_LIGH_Tool: public Tool -{ - int nextUse; -public: - Element_LIGH_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): - Tool(id, name, description, r, g, b, identifier, textureGen), - nextUse(0) - { - } - virtual ~Element_LIGH_Tool() {} - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void Click(Simulation * sim, Brush * brush, ui::Point position) { } - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -}; - - -class ElementTool: public Tool -{ -public: - ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - virtual ~ElementTool(); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); -}; - -class Element_TESC_Tool: public ElementTool -{ -public: - Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - virtual ~Element_TESC_Tool() {} - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); -}; - -class PlopTool: public ElementTool -{ -public: - PlopTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): - ElementTool(id, name, description, r, g, b, identifier, textureGen) - { - } - virtual ~PlopTool() {} - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {} - virtual void Click(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } -}; - -class WallTool: public Tool -{ -public: - WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - virtual ~WallTool(); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); -}; - -class GolTool: public Tool -{ -public: - GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - virtual ~GolTool(); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); -}; - -class WindTool: public Tool -{ -public: - WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); - virtual ~WindTool(); - virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); - virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); - virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); - virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); -}; - -#endif /* TOOL_H_ */ diff --git a/src/game/ToolButton.cpp b/src/game/ToolButton.cpp deleted file mode 100644 index 575167f..0000000 --- a/src/game/ToolButton.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "ToolButton.h" -#include "interface/Keys.h" - -ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip): - ui::Button(position, size, text_, toolTip) -{ - SetSelectionState(-1); - Appearance.BorderActive = ui::Colour(255, 0, 0); -} - -void ToolButton::OnMouseClick(int x, int y, unsigned int button) -{ - isButtonDown = true; -} - -void ToolButton::OnMouseUnclick(int x, int y, unsigned int button) -{ - if(isButtonDown) - { - isButtonDown = false; - if(button == BUTTON_LEFT) - SetSelectionState(0); - if(button == BUTTON_RIGHT) - SetSelectionState(1); - if(button == BUTTON_MIDDLE) - SetSelectionState(2); - DoAction(); - } -} - -void ToolButton::Draw(const ui::Point& screenPos) -{ - Graphics * g = ui::Engine::Ref().g; - int totalColour = Appearance.BackgroundInactive.Blue + (3*Appearance.BackgroundInactive.Green) + (2*Appearance.BackgroundInactive.Red); - - if(Appearance.GetTexture()) - { - g->draw_image(Appearance.GetTexture(), screenPos.X+2, screenPos.Y+2, 255); - } - else - { - g->fillrect(screenPos.X+2, screenPos.Y+2, Size.X-4, Size.Y-4, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, Appearance.BackgroundInactive.Alpha); - } - - if(isMouseInside && currentSelection == -1) - { - g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, Appearance.BorderActive.Alpha); - } - else - { - g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, Appearance.BorderInactive.Alpha); - } - - if (totalColour<544) - { - g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 255, 255, 255, 255); - } - else - { - g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 0, 0, 0, 255); - } -} - -void ToolButton::SetSelectionState(int state) -{ - currentSelection = state; - switch(state) - { - case 0: - Appearance.BorderInactive = ui::Colour(255, 0, 0); - break; - case 1: - Appearance.BorderInactive = ui::Colour(0, 0, 255); - break; - case 2: - Appearance.BorderInactive = ui::Colour(0, 255, 0); - break; - default: - Appearance.BorderInactive = ui::Colour(0, 0, 0); - break; - } -} - -int ToolButton::GetSelectionState() -{ - return currentSelection; -} - -ToolButton::~ToolButton() { -} - diff --git a/src/game/ToolButton.h b/src/game/ToolButton.h deleted file mode 100644 index 6fdd733..0000000 --- a/src/game/ToolButton.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef TOOLBUTTON_H_ -#define TOOLBUTTON_H_ - -#include "interface/Button.h" - -class ToolButton: public ui::Button { - int currentSelection; -public: - ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip = ""); - virtual void OnMouseUnclick(int x, int y, unsigned int button); - virtual void OnMouseClick(int x, int y, unsigned int button); - virtual void Draw(const ui::Point& screenPos); - void SetSelectionState(int state); - int GetSelectionState(); - virtual ~ToolButton(); -}; - -#endif /* TOOLBUTTON_H_ */ diff --git a/src/game/TriangleBrush.h b/src/game/TriangleBrush.h deleted file mode 100644 index 7d537bd..0000000 --- a/src/game/TriangleBrush.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * TriangleBrush.h - * - * Created on: Jan 26, 2012 - * Author: Savely Skresanov - */ - -#ifndef TRIANGLEBRUSH_H_ -#define TRIANGLEBRUSH_H_ - -#include -#include "Brush.h" - -class TriangleBrush: public Brush -{ -public: - TriangleBrush(ui::Point size_): - Brush(size_) - { - SetRadius(size_); - }; - virtual void GenerateBitmap() - { - if(bitmap) - delete[] bitmap; - bitmap = new unsigned char[size.X*size.Y]; - int rx = radius.X; - int ry = radius.Y; - for(int x = -rx; x <= rx; x++) - { - for(int y = -ry; y <= ry; y++) - { - if ((abs((rx+2*x)*ry+rx*y) + abs(2*rx*(y-ry)) + abs((rx-2*x)*ry+rx*y))<=(4*rx*ry)) - { - bitmap[(y+ry)*(size.X)+x+rx] = 255; - } - else - { - bitmap[(y+ry)*(size.X)+x+rx] = 0; - } - } - } - } -}; - -#endif /* TRIANGLEBRUSH_H_ */ diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h index c5fe015..6328c19 100644 --- a/src/graphics/Renderer.h +++ b/src/graphics/Renderer.h @@ -9,8 +9,8 @@ #include "Config.h" #include "client/Client.h" #include "Graphics.h" -#include "interface/Point.h" -#include "game/RenderPreset.h" +#include "gui/interface/Point.h" +#include "gui/game/RenderPreset.h" class Simulation; diff --git a/src/gui/Style.cpp b/src/gui/Style.cpp new file mode 100644 index 0000000..920e1b5 --- /dev/null +++ b/src/gui/Style.cpp @@ -0,0 +1,18 @@ +#include + +#include "Style.h" +#include "gui/interface/Colour.h" + +namespace style { + ui::Colour Colour::InformationTitle = ui::Colour(140, 140, 255); + ui::Colour Colour::WarningTitle = ui::Colour(255, 216, 32); + ui::Colour Colour::ErrorTitle = ui::Colour(255, 64, 32); + + ui::Colour Colour::ConfirmButton = ui::Colour(255, 255, 50); + + ui::Colour Colour::ActiveBorder = ui::Colour(255, 255, 255); + ui::Colour Colour::InactiveBorder = ui::Colour(100, 100, 100); + + ui::Colour Colour::ActiveBackground = ui::Colour(50, 50, 50); + ui::Colour Colour::InactiveBackground = ui::Colour(0, 0, 0); +} diff --git a/src/gui/Style.h b/src/gui/Style.h new file mode 100644 index 0000000..7ac0a01 --- /dev/null +++ b/src/gui/Style.h @@ -0,0 +1,28 @@ +#ifndef STYLE_H_ +#define STYLE_H_ + +namespace ui { class Colour; } + +namespace style +{ + class Colour + { + public: + static ui::Colour InformationTitle; + static ui::Colour WarningTitle; + static ui::Colour ErrorTitle; + + static ui::Colour ConfirmButton; + + static ui::Colour ActiveBorder; + static ui::Colour InactiveBorder; + + static ui::Colour ActiveBackground; + static ui::Colour InactiveBackground; + }; + class Metrics + { + }; +} + +#endif diff --git a/src/gui/colourpicker/ColourPickerActivity.cpp b/src/gui/colourpicker/ColourPickerActivity.cpp new file mode 100644 index 0000000..1e5c0c1 --- /dev/null +++ b/src/gui/colourpicker/ColourPickerActivity.cpp @@ -0,0 +1,309 @@ +#include +#include +#include "ColourPickerActivity.h" +#include "gui/interface/Textbox.h" +#include "gui/interface/Label.h" +#include "gui/interface/Keys.h" +#include "game/Tool.h" +#include "Style.h" +#include "Format.h" +#include "game/GameModel.h" + +ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, ColourPickedCallback * callback) : + WindowActivity(ui::Point(-1, -1), ui::Point(266, 175)), + currentHue(0), + currentSaturation(0), + currentValue(0), + mouseDown(false), + valueMouseDown(false), + callback(callback) +{ + + class ColourChange : public ui::TextboxAction + { + ColourPickerActivity * a; + public: + ColourChange(ColourPickerActivity * a) : a(a) {} + + void TextChangedCallback(ui::Textbox * sender) + { + int r, g, b, alpha; + r = format::StringToNumber(a->rValue->GetText()); + g = format::StringToNumber(a->gValue->GetText()); + b = format::StringToNumber(a->bValue->GetText()); + alpha = format::StringToNumber(a->aValue->GetText()); + if (r > 255) + r = 255; + if (g > 255) + g = 255; + if (b > 255) + b = 255; + if (alpha > 255) + alpha = 255; + + RGB_to_HSV(r, g, b, &a->currentHue, &a->currentSaturation, &a->currentValue); + a->currentAlpha = alpha; + a->UpdateTextboxes(r, g, b, alpha); + } + }; + + rValue = new ui::Textbox(ui::Point(5, Size.Y-23), ui::Point(30, 17), "255"); + rValue->SetActionCallback(new ColourChange(this)); + rValue->SetLimit(3); + rValue->SetInputType(ui::Textbox::Number); + AddComponent(rValue); + + gValue = new ui::Textbox(ui::Point(40, Size.Y-23), ui::Point(30, 17), "255"); + gValue->SetActionCallback(new ColourChange(this)); + gValue->SetLimit(3); + gValue->SetInputType(ui::Textbox::Number); + AddComponent(gValue); + + bValue = new ui::Textbox(ui::Point(75, Size.Y-23), ui::Point(30, 17), "255"); + bValue->SetActionCallback(new ColourChange(this)); + bValue->SetLimit(3); + bValue->SetInputType(ui::Textbox::Number); + AddComponent(bValue); + + aValue = new ui::Textbox(ui::Point(110, Size.Y-23), ui::Point(30, 17), "255"); + aValue->SetActionCallback(new ColourChange(this)); + aValue->SetLimit(3); + aValue->SetInputType(ui::Textbox::Number); + AddComponent(aValue); + + hexValue = new::ui::Label(ui::Point(150, Size.Y-23), ui::Point(53, 17), "0xFFFFFFFF"); + AddComponent(hexValue); + + class OkayAction: public ui::ButtonAction + { + ColourPickerActivity * a; + public: + OkayAction(ColourPickerActivity * a) : a(a) { } + void ActionCallback(ui::Button * sender) + { + int Red, Green, Blue; + Red = format::StringToNumber(a->rValue->GetText()); + Green = format::StringToNumber(a->gValue->GetText()); + Blue = format::StringToNumber(a->bValue->GetText()); + ui::Colour col(Red, Green, Blue, a->currentAlpha); + if(a->callback) + a->callback->ColourPicked(col); + a->Exit(); + } + }; + + ui::Button * doneButton = new ui::Button(ui::Point(Size.X-45, Size.Y-23), ui::Point(40, 17), "Done"); + doneButton->SetActionCallback(new OkayAction(this)); + AddComponent(doneButton); + SetOkayButton(doneButton); + + RGB_to_HSV(initialColour.Red, initialColour.Green, initialColour.Blue, ¤tHue, ¤tSaturation, ¤tValue); + currentAlpha = initialColour.Alpha; + UpdateTextboxes(initialColour.Red, initialColour.Green, initialColour.Blue, initialColour.Alpha); +} + +void ColourPickerActivity::UpdateTextboxes(int r, int g, int b, int a) +{ + rValue->SetText(format::NumberToString(r)); + gValue->SetText(format::NumberToString(g)); + bValue->SetText(format::NumberToString(b)); + aValue->SetText(format::NumberToString(a)); + std::stringstream hex; + hex << std::hex << "0x" << std::setfill('0') << std::setw(2) << std::uppercase << a << std::setw(2) << r << std::setw(2) << g << std::setw(2) << b; + hexValue->SetText(hex.str()); +} +void ColourPickerActivity::OnTryExit(ExitMethod method) +{ + Exit(); +} + +void ColourPickerActivity::OnMouseMove(int x, int y, int dx, int dy) +{ + if(mouseDown) + { + x -= Position.X+5; + y -= Position.Y+5; + + currentHue = (float(x)/float(255))*359.0f; + currentSaturation = 255-(y*2); + + if(currentSaturation > 255) + currentSaturation = 255; + if(currentSaturation < 0) + currentSaturation = 0; + if(currentHue > 359) + currentHue = 359; + if(currentHue < 0) + currentHue = 0; + } + + if(valueMouseDown) + { + x -= Position.X+5; + y -= Position.Y+5; + + currentValue = x; + + if(currentValue > 255) + currentValue = 255; + if(currentValue < 0) + currentValue = 0; + } + + if(mouseDown || valueMouseDown) + { + int cr, cg, cb; + HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); + UpdateTextboxes(cr, cg, cb, currentAlpha); + } +} + +void ColourPickerActivity::OnMouseDown(int x, int y, unsigned button) +{ + x -= Position.X+5; + y -= Position.Y+5; + if(x >= 0 && x < 256 && y >= 0 && y <= 128) + { + mouseDown = true; + currentHue = (float(x)/float(255))*359.0f; + currentSaturation = 255-(y*2); + + if(currentSaturation > 255) + currentSaturation = 255; + if(currentSaturation < 0) + currentSaturation = 0; + if(currentHue > 359) + currentHue = 359; + if(currentHue < 0) + currentHue = 0; + } + + if(x >= 0 && x < 256 && y >= 132 && y <= 142) + { + valueMouseDown = true; + currentValue = x; + + if(currentValue > 255) + currentValue = 255; + if(currentValue < 0) + currentValue = 0; + } + + if(mouseDown || valueMouseDown) + { + int cr, cg, cb; + HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); + UpdateTextboxes(cr, cg, cb, currentAlpha); + } +} + +void ColourPickerActivity::OnMouseUp(int x, int y, unsigned button) +{ + if(mouseDown || valueMouseDown) + { + int cr, cg, cb; + HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); + UpdateTextboxes(cr, cg, cb, currentAlpha); + } + + if(mouseDown) + { + mouseDown = false; + x -= Position.X+5; + y -= Position.Y+5; + + currentHue = (float(x)/float(255))*359.0f; + currentSaturation = 255-(y*2); + + if(currentSaturation > 255) + currentSaturation = 255; + if(currentSaturation < 0) + currentSaturation = 0; + if(currentHue > 359) + currentHue = 359; + if(currentHue < 0) + currentHue = 0; + } + + if(valueMouseDown) + { + valueMouseDown = false; + + x -= Position.X+5; + y -= Position.Y+5; + + currentValue = x; + + if(currentValue > 255) + currentValue = 255; + if(currentValue < 0) + currentValue = 0; + } +} + + +void ColourPickerActivity::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + //g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->fillrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3, 0, 0, 0, currentAlpha); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + + g->drawrect(Position.X+4, Position.Y+4, 258, 130, 180, 180, 180, 255); + + g->drawrect(Position.X+4, Position.Y+4+4+128, 258, 12, 180, 180, 180, 255); + + + int offsetX = Position.X+5; + int offsetY = Position.Y+5; + + + //draw color square + int lastx = -1, currx = 0; + for(int saturation = 0; saturation <= 255; saturation+=2) + { + for(int hue = 0; hue <= 359; hue++) + { + currx = clamp_flt(hue, 0, 359)+offsetX; + if (currx == lastx) + continue; + lastx = currx; + int cr = 0; + int cg = 0; + int cb = 0; + HSV_to_RGB(hue, 255-saturation, currentValue, &cr, &cg, &cb); + g->blendpixel(currx, (saturation/2)+offsetY, cr, cg, cb, currentAlpha); + } + } + + //draw brightness bar + for(int value = 0; value <= 255; value++) + for(int i = 0; i < 10; i++) + { + int cr = 0; + int cg = 0; + int cb = 0; + HSV_to_RGB(currentHue, currentSaturation, value, &cr, &cg, &cb); + + g->blendpixel(value+offsetX, i+offsetY+127+5, cr, cg, cb, currentAlpha); + } + + //draw color square pointer + int currentHueX = clamp_flt(currentHue, 0, 359); + int currentSaturationY = ((255-currentSaturation)/2); + g->xor_line(offsetX+currentHueX, offsetY+currentSaturationY-5, offsetX+currentHueX, offsetY+currentSaturationY-1); + g->xor_line(offsetX+currentHueX, offsetY+currentSaturationY+1, offsetX+currentHueX, offsetY+currentSaturationY+5); + g->xor_line(offsetX+currentHueX-5, offsetY+currentSaturationY, offsetX+currentHueX-1, offsetY+currentSaturationY); + g->xor_line(offsetX+currentHueX+1, offsetY+currentSaturationY, offsetX+currentHueX+5, offsetY+currentSaturationY); + + //draw brightness bar pointer + int currentValueX = restrict_flt(currentValue, 0, 254); + g->xor_line(offsetX+currentValueX, offsetY+4+128, offsetX+currentValueX, offsetY+13+128); + g->xor_line(offsetX+currentValueX+1, offsetY+4+128, offsetX+currentValueX+1, offsetY+13+128); +} + +ColourPickerActivity::~ColourPickerActivity() { + if(callback) + delete callback; +} + diff --git a/src/gui/colourpicker/ColourPickerActivity.h b/src/gui/colourpicker/ColourPickerActivity.h new file mode 100644 index 0000000..f1e62b7 --- /dev/null +++ b/src/gui/colourpicker/ColourPickerActivity.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include "Activity.h" +#include "gui/interface/Window.h" +#include "gui/interface/Textbox.h" + +class ColourPickedCallback +{ +public: + ColourPickedCallback() {} + virtual ~ColourPickedCallback() {} + virtual void ColourPicked(ui::Colour colour) {} +}; + +class ColourPickerActivity: public WindowActivity { + int currentHue; + int currentSaturation; + int currentValue; + int currentAlpha; + + bool mouseDown; + bool valueMouseDown; + + ui::Textbox * rValue; + ui::Textbox * gValue; + ui::Textbox * bValue; + ui::Textbox * aValue; + ui::Label * hexValue; + + ColourPickedCallback * callback; + + void UpdateTextboxes(int r, int g, int b, int a); +public: + virtual void OnMouseMove(int x, int y, int dx, int dy); + virtual void OnMouseDown(int x, int y, unsigned button); + virtual void OnMouseUp(int x, int y, unsigned button); + virtual void OnTryExit(ExitMethod method); + ColourPickerActivity(ui::Colour initialColour, ColourPickedCallback * callback = NULL); + virtual ~ColourPickerActivity(); + virtual void OnDraw(); +}; diff --git a/src/gui/console/ConsoleCommand.h b/src/gui/console/ConsoleCommand.h new file mode 100644 index 0000000..31e41b0 --- /dev/null +++ b/src/gui/console/ConsoleCommand.h @@ -0,0 +1,23 @@ +#ifndef CONSOLECOMMAND_H_ +#define CONSOLECOMMAND_H_ + +class ConsoleCommand +{ +public: + ConsoleCommand(std::string command, int returnStatus, std::string returnValue): + Command(command), ReturnStatus(returnStatus), ReturnValue(returnValue) + { + + } + std::string Command; + int ReturnStatus; + std::string ReturnValue; + + operator std::string() const + { + return Command; + } +}; + + +#endif /* CONSOLECOMMAND_H_ */ diff --git a/src/gui/console/ConsoleController.cpp b/src/gui/console/ConsoleController.cpp new file mode 100644 index 0000000..602636f --- /dev/null +++ b/src/gui/console/ConsoleController.cpp @@ -0,0 +1,74 @@ +#include +#include "ConsoleController.h" + +ConsoleController::ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface): + HasDone(false) +{ + consoleModel = new ConsoleModel(); + consoleView = new ConsoleView(); + consoleView->AttachController(this); + consoleModel->AddObserver(consoleView); + + this->callback = callback; + this->commandInterface = commandInterface; +} + +void ConsoleController::EvaluateCommand(std::string command) +{ + if (command.substr(0, 6) == "!load ") + CloseConsole(); + int returnCode = commandInterface->Command(command); + if(command.length()) + consoleModel->AddLastCommand(ConsoleCommand(command, returnCode, commandInterface->GetLastError())); + else + CloseConsole(); +} + +void ConsoleController::CloseConsole() +{ + if(ui::Engine::Ref().GetWindow() == consoleView) + ui::Engine::Ref().CloseWindow(); +} + +std::string ConsoleController::FormatCommand(std::string command) +{ + return commandInterface->FormatCommand(command); +} + +void ConsoleController::NextCommand() +{ + int cIndex = consoleModel->GetCurrentCommandIndex(); + if(cIndex < consoleModel->GetPreviousCommands().size()) + consoleModel->SetCurrentCommandIndex(cIndex+1); +} + +void ConsoleController::PreviousCommand() +{ + int cIndex = consoleModel->GetCurrentCommandIndex(); + if(cIndex > 0) + consoleModel->SetCurrentCommandIndex(cIndex-1); +} + +void ConsoleController::Exit() +{ + if(ui::Engine::Ref().GetWindow() == consoleView) + ui::Engine::Ref().CloseWindow(); + if(callback) + callback->ControllerExit(); + HasDone = true; +} + +ConsoleView * ConsoleController::GetView() +{ + return consoleView; +} + +ConsoleController::~ConsoleController() { + if(ui::Engine::Ref().GetWindow() == consoleView) + ui::Engine::Ref().CloseWindow(); + if(callback) + delete callback; + delete consoleModel; + delete consoleView; +} + diff --git a/src/gui/console/ConsoleController.h b/src/gui/console/ConsoleController.h new file mode 100644 index 0000000..cf8b36e --- /dev/null +++ b/src/gui/console/ConsoleController.h @@ -0,0 +1,31 @@ +#ifndef CONSOLECONTROLLER_H_ +#define CONSOLECONTROLLER_H_ + +#include +#include "Controller.h" +#include "ConsoleView.h" +#include "ConsoleModel.h" +#include "ConsoleCommand.h" +#include "cat/CommandInterface.h" + +class ConsoleModel; +class ConsoleView; +class ConsoleController { + ControllerCallback * callback; + ConsoleView * consoleView; + ConsoleModel * consoleModel; + CommandInterface * commandInterface; +public: + bool HasDone; + ConsoleController(ControllerCallback * callback, CommandInterface * commandInterface); + std::string FormatCommand(std::string command); + void EvaluateCommand(std::string command); + void NextCommand(); + void PreviousCommand(); + void Exit(); + void CloseConsole(); + ConsoleView * GetView(); + virtual ~ConsoleController(); +}; + +#endif /* CONSOLECONTROLLER_H_ */ diff --git a/src/gui/console/ConsoleModel.cpp b/src/gui/console/ConsoleModel.cpp new file mode 100644 index 0000000..ceb6b32 --- /dev/null +++ b/src/gui/console/ConsoleModel.cpp @@ -0,0 +1,75 @@ +#include "client/Client.h" +#include "ConsoleModel.h" + +ConsoleModel::ConsoleModel() { + std::vector previousHistory = Client::Ref().GetPrefStringArray("Console.History"); + for(std::vector::reverse_iterator iter = previousHistory.rbegin(), end = previousHistory.rend(); iter != end; ++iter) + { + if(previousCommands.size()<25) + { + previousCommands.push_front(ConsoleCommand(*iter, 0, "")); + currentCommandIndex = previousCommands.size(); + } + } +} + +void ConsoleModel::AddObserver(ConsoleView * observer) +{ + observers.push_back(observer); + observer->NotifyPreviousCommandsChanged(this); +} + +int ConsoleModel::GetCurrentCommandIndex() +{ + return currentCommandIndex; +} + +void ConsoleModel::SetCurrentCommandIndex(int index) +{ + currentCommandIndex = index; + notifyCurrentCommandChanged(); +} + +ConsoleCommand ConsoleModel::GetCurrentCommand() +{ + if(currentCommandIndex < 0 || currentCommandIndex >= previousCommands.size()) + { + return ConsoleCommand("", 0, ""); + } + return previousCommands[currentCommandIndex]; +} + +void ConsoleModel::AddLastCommand(ConsoleCommand command) +{ + previousCommands.push_back(command); + if(previousCommands.size()>25) + previousCommands.pop_front(); + currentCommandIndex = previousCommands.size(); + notifyPreviousCommandsChanged(); +} + +std::deque ConsoleModel::GetPreviousCommands() +{ + return previousCommands; +} + +void ConsoleModel::notifyPreviousCommandsChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyPreviousCommandsChanged(this); + } +} + +void ConsoleModel::notifyCurrentCommandChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyCurrentCommandChanged(this); + } +} + +ConsoleModel::~ConsoleModel() { + Client::Ref().SetPref("Console.History", std::vector(previousCommands.begin(), previousCommands.end())); +} + diff --git a/src/gui/console/ConsoleModel.h b/src/gui/console/ConsoleModel.h new file mode 100644 index 0000000..312739a --- /dev/null +++ b/src/gui/console/ConsoleModel.h @@ -0,0 +1,28 @@ +#ifndef CONSOLEMODEL_H_ +#define CONSOLEMODEL_H_ + +#include +#include +#include "ConsoleView.h" +#include "ConsoleCommand.h" + +class ConsoleView; +class ConsoleModel { + int currentCommandIndex; + std::vector observers; + std::deque previousCommands; + void notifyPreviousCommandsChanged(); + void notifyCurrentCommandChanged(); +public: + int GetCurrentCommandIndex(); + void SetCurrentCommandIndex(int index); + ConsoleCommand GetCurrentCommand(); + + std::deque GetPreviousCommands(); + ConsoleModel(); + void AddObserver(ConsoleView * observer); + void AddLastCommand(ConsoleCommand command); + virtual ~ConsoleModel(); +}; + +#endif /* CONSOLEMODEL_H_ */ diff --git a/src/gui/console/ConsoleView.cpp b/src/gui/console/ConsoleView.cpp new file mode 100644 index 0000000..cf398ae --- /dev/null +++ b/src/gui/console/ConsoleView.cpp @@ -0,0 +1,102 @@ +#include "ConsoleView.h" +#include "gui/interface/Keys.h" + +ConsoleView::ConsoleView(): + ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, 150)), + commandField(NULL) +{ + class CommandHighlighter: public ui::TextboxAction + { + ConsoleView * v; + public: + CommandHighlighter(ConsoleView * v_) { v = v_; } + virtual void TextChangedCallback(ui::Textbox * sender) + { + sender->SetDisplayText(v->c->FormatCommand(sender->GetText())); + } + }; + commandField = new ui::Textbox(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), ""); + commandField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + commandField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + commandField->SetActionCallback(new CommandHighlighter(this)); + AddComponent(commandField); + FocusComponent(commandField); + commandField->SetBorder(false); +} + +void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + switch(key) + { + case KEY_ESCAPE: + case '`': + if (character != '~') + c->CloseConsole(); + else + Window::DoKeyPress(key, character, shift, ctrl, alt); + break; + case KEY_RETURN: + case KEY_ENTER: + c->EvaluateCommand(commandField->GetText()); + commandField->SetText(""); + commandField->SetDisplayText(""); + break; + case KEY_DOWN: + c->NextCommand(); + break; + case KEY_UP: + c->PreviousCommand(); + break; + default: + Window::DoKeyPress(key, character, shift, ctrl, alt); + break; + } +} + +void ConsoleView::NotifyPreviousCommandsChanged(ConsoleModel * sender) +{ + for(int i = 0; i < commandList.size(); i++) + { + RemoveComponent(commandList[i]); + delete commandList[i]; + } + commandList.clear(); + std::deque commands = sender->GetPreviousCommands(); + int currentY = Size.Y - 32; + if(commands.size()) + for(int i = commands.size()-1; i >= 0; i--) + { + if(currentY <= 0) + break; + ui::Label * tempLabel = new ui::Label(ui::Point(Size.X/2, currentY), ui::Point(Size.X/2, 16), commands[i].ReturnValue); + tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + commandList.push_back(tempLabel); + AddComponent(tempLabel); + tempLabel = new ui::Label(ui::Point(0, currentY), ui::Point(Size.X/2, 16), commands[i].Command); + tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + commandList.push_back(tempLabel); + AddComponent(tempLabel); + currentY-=16; + } +} + +void ConsoleView::NotifyCurrentCommandChanged(ConsoleModel * sender) +{ + commandField->SetText(sender->GetCurrentCommand().Command); + commandField->SetDisplayText(c->FormatCommand(commandField->GetText())); +} + + +void ConsoleView::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 0, 0, 0, 110); + g->draw_line(Position.X, Position.Y+Size.Y-16, Position.X+Size.X, Position.Y+Size.Y-16, 255, 255, 255, 160); + g->draw_line(Position.X, Position.Y+Size.Y, Position.X+Size.X, Position.Y+Size.Y, 255, 255, 255, 200); +} + +ConsoleView::~ConsoleView() { +} + diff --git a/src/gui/console/ConsoleView.h b/src/gui/console/ConsoleView.h new file mode 100644 index 0000000..22f172c --- /dev/null +++ b/src/gui/console/ConsoleView.h @@ -0,0 +1,30 @@ +#ifndef CONSOLEVIEW_H_ +#define CONSOLEVIEW_H_ + +#include +#include +#include "gui/interface/Label.h" +#include "gui/interface/Window.h" +#include "ConsoleController.h" +#include "ConsoleModel.h" +#include "gui/interface/Textbox.h" +#include "ConsoleCommand.h" + + +class ConsoleController; +class ConsoleModel; +class ConsoleView: public ui::Window { + ConsoleController * c; + ui::Textbox * commandField; + std::vector commandList; +public: + ConsoleView(); + virtual void OnDraw(); + virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + void AttachController(ConsoleController * c_) { c = c_; } + void NotifyPreviousCommandsChanged(ConsoleModel * sender); + void NotifyCurrentCommandChanged(ConsoleModel * sender); + virtual ~ConsoleView(); +}; + +#endif /* CONSOLEVIEW_H_ */ diff --git a/src/gui/dialogues/ConfirmPrompt.cpp b/src/gui/dialogues/ConfirmPrompt.cpp new file mode 100644 index 0000000..d558195 --- /dev/null +++ b/src/gui/dialogues/ConfirmPrompt.cpp @@ -0,0 +1,152 @@ +#include "ConfirmPrompt.h" +#include "Style.h" +#include "gui/interface/Label.h" +#include "gui/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, 35)), + 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), "Continue"); + 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); +} + +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; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +ConfirmPrompt::~ConfirmPrompt() { + if(callback) + delete callback; +} + diff --git a/src/gui/dialogues/ConfirmPrompt.h b/src/gui/dialogues/ConfirmPrompt.h new file mode 100644 index 0000000..a2a3170 --- /dev/null +++ b/src/gui/dialogues/ConfirmPrompt.h @@ -0,0 +1,25 @@ +#ifndef CONFIRMPROMPT_H_ +#define CONFIRMPROMPT_H_ + +#include "gui/interface/Window.h" + +class ConfirmDialogueCallback; +class ConfirmPrompt: public ui::Window { +public: + enum DialogueResult { ResultCancel, ResultOkay }; + 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; +}; + +class ConfirmDialogueCallback +{ + public: + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) {} + virtual ~ConfirmDialogueCallback() {} +}; + +#endif /* CONFIRMPROMPT_H_ */ diff --git a/src/gui/dialogues/ErrorMessage.cpp b/src/gui/dialogues/ErrorMessage.cpp new file mode 100644 index 0000000..37a635c --- /dev/null +++ b/src/gui/dialogues/ErrorMessage.cpp @@ -0,0 +1,78 @@ +#include "Style.h" +#include "ErrorMessage.h" +#include "gui/interface/Button.h" +#include "gui/interface/Label.h" +#include "PowderToy.h" + +ErrorMessage::ErrorMessage(std::string title, std::string message, ErrorMessageCallback * callback_): + ui::Window(ui::Point(-1, -1), ui::Point(200, 35)), + callback(callback_) +{ + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 16), title); + titleLabel->SetTextColour(style::Colour::ErrorTitle); + 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, -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 DismissAction: public ui::ButtonAction + { + ErrorMessage * message; + public: + DismissAction(ErrorMessage * message_) { message = message_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(message->callback) + message->callback->DismissCallback(); + message->SelfDestruct(); + } + }; + + 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 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; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +ErrorMessage::~ErrorMessage() { + if(callback) + delete callback; +} + diff --git a/src/gui/dialogues/ErrorMessage.h b/src/gui/dialogues/ErrorMessage.h new file mode 100644 index 0000000..a7281b2 --- /dev/null +++ b/src/gui/dialogues/ErrorMessage.h @@ -0,0 +1,23 @@ +#ifndef ERRORMESSAGE_H_ +#define ERRORMESSAGE_H_ + +#include "gui/interface/Window.h" + +class ErrorMessageCallback; +class ErrorMessage: public ui::Window { + ErrorMessageCallback * callback; +public: + 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/gui/dialogues/InformationMessage.cpp b/src/gui/dialogues/InformationMessage.cpp new file mode 100644 index 0000000..2455b72 --- /dev/null +++ b/src/gui/dialogues/InformationMessage.cpp @@ -0,0 +1,77 @@ +#include "Style.h" +#include "InformationMessage.h" +#include "gui/interface/Button.h" +#include "gui/interface/Label.h" +#include "gui/interface/ScrollPanel.h" + +InformationMessage::InformationMessage(std::string title, std::string message, bool large): + ui::Window(ui::Point(-1, -1), ui::Point(200, 75)) +{ + if (large) //Maybe also use this large mode for changelogs eventually, or have it as a customizable size? + { + Size.X += 200; + Size.Y += 175; + } + + if (large) + { + ui::ScrollPanel *messagePanel = new ui::ScrollPanel(ui::Point(4, 24), ui::Point(Size.X-8, 206)); + AddComponent(messagePanel); + + ui::Label * messageLabel = new ui::Label(ui::Point(4, 0), ui::Point(Size.X-28, -1), message); + messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; + messageLabel->SetMultiline(true); + messagePanel->AddChild(messageLabel); + + messagePanel->InnerSize = ui::Point(messagePanel->Size.X, messageLabel->Size.Y+4); + } + else + { + 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); + } + + 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); + + 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+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +InformationMessage::~InformationMessage() { +} + diff --git a/src/gui/dialogues/InformationMessage.h b/src/gui/dialogues/InformationMessage.h new file mode 100644 index 0000000..6a26b38 --- /dev/null +++ b/src/gui/dialogues/InformationMessage.h @@ -0,0 +1,13 @@ +#ifndef INFORMATIONMESSAGE_H_ +#define INFORMATIONMESSAGE_H_ + +#include "gui/interface/Window.h" + +class InformationMessage: public ui::Window { +public: + InformationMessage(std::string title, std::string message, bool large); + virtual void OnDraw(); + virtual ~InformationMessage(); +}; + +#endif /* INFORMATIONMESSAGE_H_ */ diff --git a/src/gui/dialogues/LegacyDialogues.h b/src/gui/dialogues/LegacyDialogues.h new file mode 100644 index 0000000..7f6097d --- /dev/null +++ b/src/gui/dialogues/LegacyDialogues.h @@ -0,0 +1,11 @@ +#pragma once + +//Legacy blocking prompts +//This are not implemented here, but rather in the engine bootstrapper +bool ConfirmUI(std::string title, std::string message, std::string confirmText) {} + +void ErrorUI(std::string title, std::string message) {} + +void InformationUI(std::string title, std::string message) {} + +std::string MessagePromptUI(std::string title, std::string message, std::string text, std::string placeholder) {} \ No newline at end of file diff --git a/src/gui/dialogues/TextPrompt.cpp b/src/gui/dialogues/TextPrompt.cpp new file mode 100644 index 0000000..929bf06 --- /dev/null +++ b/src/gui/dialogues/TextPrompt.cpp @@ -0,0 +1,114 @@ +#include +#include "TextPrompt.h" +#include "gui/interface/Label.h" +#include "gui/interface/Button.h" +#include "Style.h" +#include "PowderToy.h" + +class CloseAction: public ui::ButtonAction +{ +public: + TextPrompt * prompt; + TextPrompt::DialogueResult result; + CloseAction(TextPrompt * prompt_, TextPrompt::DialogueResult result_) { prompt = prompt_; result = result_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->callback) + prompt->callback->TextCallback(result, prompt->textField->GetText()); + prompt->SelfDestruct(); //TODO: Fix component disposal + } +}; + +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, 65)), + callback(callback_) +{ + if(multiline) + Size.X += 100; + + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 18), 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+4; + + textField = new ui::Textbox(ui::Point(4, messageLabel->Position.Y + messageLabel->Size.Y + 7), ui::Point(Size.X-8, 16), text, placeholder); + if(multiline) + { + textField->SetMultiline(true); + textField->Size.Y = 60; + Size.Y += 45; + textField->Appearance.VerticalAlign = ui::Appearance::AlignTop; + } + else + { + textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + } + textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(textField); + FocusComponent(textField); + + ui::Button * cancelButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point((Size.X/2)+1, 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/2, Size.Y-16), ui::Point(Size.X/2, 16), "Okay"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignRight; + 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); +} + +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; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +TextPrompt::~TextPrompt() { + if(callback) + delete callback; +} + diff --git a/src/gui/dialogues/TextPrompt.h b/src/gui/dialogues/TextPrompt.h new file mode 100644 index 0000000..e88c977 --- /dev/null +++ b/src/gui/dialogues/TextPrompt.h @@ -0,0 +1,28 @@ +#ifndef TEXTPROMPT_H_ +#define TEXTPROMPT_H_ + +#include "gui/interface/Window.h" +#include "gui/interface/Textbox.h" + +class TextDialogueCallback; +class TextPrompt: public ui::Window { +protected: + ui::Textbox * textField; +public: + friend class CloseAction; + enum DialogueResult { ResultCancel, ResultOkay }; + 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; +}; + +class TextDialogueCallback +{ + public: + virtual void TextCallback(TextPrompt::DialogueResult result, std::string resultText) {} + virtual ~TextDialogueCallback() {} +}; + +#endif /* TEXTPROMPT_H_ */ diff --git a/src/gui/elementsearch/ElementSearchActivity.cpp b/src/gui/elementsearch/ElementSearchActivity.cpp new file mode 100644 index 0000000..6f49438 --- /dev/null +++ b/src/gui/elementsearch/ElementSearchActivity.cpp @@ -0,0 +1,184 @@ +#include +#include "ElementSearchActivity.h" +#include "gui/interface/Textbox.h" +#include "gui/interface/Label.h" +#include "gui/interface/Keys.h" +#include "game/Tool.h" +#include "Style.h" +#include "game/GameModel.h" + +class ElementSearchActivity::ToolAction: public ui::ButtonAction +{ + ElementSearchActivity * a; +public: + Tool * tool; + ToolAction(ElementSearchActivity * a, Tool * tool) : a(a), tool(tool) { } + void ActionCallback(ui::Button * sender_) + { + ToolButton *sender = (ToolButton*)sender_; + if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 2) + a->SetActiveTool(sender->GetSelectionState(), tool); + } +}; + +ElementSearchActivity::ElementSearchActivity(GameModel * gameModel, std::vector tools) : + WindowActivity(ui::Point(-1, -1), ui::Point(236, 302)), + gameModel(gameModel), + tools(tools), + firstResult(NULL) +{ + ui::Label * title = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), "Element Search"); + title->SetTextColour(style::Colour::InformationTitle); + title->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(title); + + class SearchAction : public ui::TextboxAction + { + private: + ElementSearchActivity * a; + public: + SearchAction(ElementSearchActivity * a) : a(a) {} + virtual void TextChangedCallback(ui::Textbox * sender) { + a->searchTools(sender->GetText()); + } + }; + + searchField = new ui::Textbox(ui::Point(8, 23), ui::Point(Size.X-16, 17), ""); + searchField->SetActionCallback(new SearchAction(this)); + searchField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + AddComponent(searchField); + FocusComponent(searchField); + + class CloseAction: public ui::ButtonAction + { + ElementSearchActivity * a; + public: + CloseAction(ElementSearchActivity * a) : a(a) { } + void ActionCallback(ui::Button * sender_) + { + a->Exit(); + } + }; + + class OKAction: public ui::ButtonAction + { + ElementSearchActivity * a; + public: + OKAction(ElementSearchActivity * a) : a(a) { } + void ActionCallback(ui::Button * sender_) + { + if(a->GetFirstResult()) + a->SetActiveTool(0, a->GetFirstResult()); + } + }; + + ui::Button * closeButton = new ui::Button(ui::Point(0, Size.Y-15), ui::Point((Size.X/2)+1, 15), "Close"); + closeButton->SetActionCallback(new CloseAction(this)); + ui::Button * okButton = new ui::Button(ui::Point(Size.X/2, Size.Y-15), ui::Point(Size.X/2, 15), "OK"); + okButton->SetActionCallback(new OKAction(this)); + + AddComponent(okButton); + AddComponent(closeButton); + + searchTools(""); +} + +void ElementSearchActivity::searchTools(std::string query) +{ + firstResult = NULL; + for(std::vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter != end; ++iter) { + delete *iter; + RemoveComponent(*iter); + } + toolButtons.clear(); + + ui::Point viewPosition = searchField->Position + ui::Point(2+0, searchField->Size.Y+2+8); + ui::Point current = ui::Point(0, 0); + + std::string queryLower = std::string(query); + std::transform(queryLower.begin(), queryLower.end(), queryLower.begin(), ::tolower); + + for(std::vector::iterator iter = tools.begin(), end = tools.end(); iter != end; ++iter) { + std::string nameLower = std::string((*iter)->GetName()); + std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower); + + if(strstr(nameLower.c_str(), queryLower.c_str())!=0) + { + Tool * tool = *iter; + + if(!firstResult) + firstResult = tool; + + VideoBuffer * tempTexture = tool->GetTexture(26, 14); + ToolButton * tempButton; + + if(tempTexture) + tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), "", tool->GetDescription()); + else + tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->GetName(), tool->GetDescription()); + + tempButton->Appearance.SetTexture(tempTexture); + tempButton->Appearance.BackgroundInactive = ui::Colour(tool->colRed, tool->colGreen, tool->colBlue); + tempButton->SetActionCallback(new ToolAction(this, tool)); + + if(gameModel->GetActiveTool(0) == tool) + { + tempButton->SetSelectionState(0); //Primary + } + else if(gameModel->GetActiveTool(1) == tool) + { + tempButton->SetSelectionState(1); //Secondary + } + else if(gameModel->GetActiveTool(2) == tool) + { + tempButton->SetSelectionState(2); //Tertiary + } + + toolButtons.push_back(tempButton); + AddComponent(tempButton); + + current.X += 31; + + if(current.X + 30 > searchField->Size.X) { + current.X = 0; + current.Y += 19; + } + + if(current.Y + viewPosition.Y + 18 > Size.Y-23) + break; + } + } +} + +void ElementSearchActivity::SetActiveTool(int selectionState, Tool * tool) +{ + gameModel->SetActiveTool(selectionState, tool); + Exit(); +} + +void ElementSearchActivity::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + + g->drawrect(Position.X+searchField->Position.X, Position.Y+searchField->Position.Y+searchField->Size.Y+8, searchField->Size.X, Size.Y-(searchField->Position.Y+searchField->Size.Y+8)-23, 255, 255, 255, 180); +} + +void ElementSearchActivity::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(key == KEY_ENTER || key == KEY_RETURN) + { + if(firstResult) + gameModel->SetActiveTool(0, firstResult); + Exit(); + } + if(key == KEY_ESCAPE) + { + Exit(); + } +} + +ElementSearchActivity::~ElementSearchActivity() { +} + diff --git a/src/gui/elementsearch/ElementSearchActivity.h b/src/gui/elementsearch/ElementSearchActivity.h new file mode 100644 index 0000000..da2b254 --- /dev/null +++ b/src/gui/elementsearch/ElementSearchActivity.h @@ -0,0 +1,32 @@ +#ifndef ELEMENTSEARCHACTIVITY_H_ +#define ELEMENTSEARCHACTIVITY_H_ + +#include +#include +#include "Activity.h" +#include "gui/interface/Window.h" +#include "gui/interface/Textbox.h" +#include "game/ToolButton.h" + +class Tool; + +class GameModel; + +class ElementSearchActivity: public WindowActivity { + Tool * firstResult; + GameModel * gameModel; + std::vector tools; + ui::Textbox * searchField; + std::vector toolButtons; + void searchTools(std::string query); +public: + class ToolAction; + Tool * GetFirstResult() { return firstResult; } + ElementSearchActivity(GameModel * gameModel, std::vector tools); + void SetActiveTool(int selectionState, Tool * tool); + virtual ~ElementSearchActivity(); + virtual void OnDraw(); + virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); +}; + +#endif /* ELEMENTSEARCHACTIVITY_H_ */ diff --git a/src/gui/filebrowser/FileBrowserActivity.cpp b/src/gui/filebrowser/FileBrowserActivity.cpp new file mode 100644 index 0000000..5c97bae --- /dev/null +++ b/src/gui/filebrowser/FileBrowserActivity.cpp @@ -0,0 +1,336 @@ +#include +#include +#include "FileBrowserActivity.h" +#include "gui/interface/Label.h" +#include "gui/interface/Textbox.h" +#include "gui/interface/ScrollPanel.h" +#include "gui/interface/SaveButton.h" +#include "gui/interface/ProgressBar.h" +#include "client/Client.h" +#include "client/SaveFile.h" +#include "client/GameSave.h" +#include "Style.h" +#include "tasks/Task.h" +#include "simulation/SaveRenderer.h" +#include "dialogues/TextPrompt.h" +#include "dialogues/ConfirmPrompt.h" +#include "dialogues/ErrorMessage.h" + +class Thumbnail; + + +class SaveSelectedAction: public ui::SaveButtonAction +{ + FileBrowserActivity * a; +public: + SaveSelectedAction(FileBrowserActivity * _a) { a = _a; } + virtual void ActionCallback(ui::SaveButton * sender) + { + a->SelectSave(sender->GetSaveFile()); + } + virtual void AltActionCallback(ui::SaveButton * sender) + { + a->RenameSave(sender->GetSaveFile()); + } + virtual void AltActionCallback2(ui::SaveButton * sender) + { + a->DeleteSave(sender->GetSaveFile()); + } +}; + +//Currently, reading is done on another thread, we can't render outside the main thread due to some bullshit with OpenGL +class LoadFilesTask: public Task +{ + std::string directory; + std::string search; + std::vector saveFiles; + + virtual void before() + { + + } + + virtual void after() + { + + } + + virtual bool doWork() + { + std::vector files = Client::Ref().DirectorySearch(directory, search, ".cps"); + + + notifyProgress(-1); + for(std::vector::iterator iter = files.begin(), end = files.end(); iter != end; ++iter) + { + SaveFile * saveFile = new SaveFile(*iter); + try + { + std::vector data = Client::Ref().ReadFile(*iter); + GameSave * tempSave = new GameSave(data); + saveFile->SetGameSave(tempSave); + saveFiles.push_back(saveFile); + + std::string filename = *iter; + size_t folderPos = filename.rfind(PATH_SEP); + if(folderPos!=std::string::npos && folderPos+1 < filename.size()) + { + filename = filename.substr(folderPos+1); + } + size_t extPos = filename.rfind("."); + if(extPos!=std::string::npos) + { + filename = filename.substr(0, extPos); + } + saveFile->SetDisplayName(filename); + } + catch(std::exception & e) + { + //:( + } + } + return true; + } + +public: + std::vector GetSaveFiles() + { + return saveFiles; + } + + LoadFilesTask(std::string directory, std::string search): + directory(directory), + search(search) + { + + } +}; + +class FileBrowserActivity::SearchAction: public ui::TextboxAction +{ +public: + FileBrowserActivity * a; + SearchAction(FileBrowserActivity * a) : a(a) {} + virtual void TextChangedCallback(ui::Textbox * sender) { + a->DoSearch(sender->GetText()); + } +}; + +FileBrowserActivity::FileBrowserActivity(std::string directory, FileSelectedCallback * callback): + WindowActivity(ui::Point(-1, -1), ui::Point(450, 300)), + callback(callback), + directory(directory), + totalFiles(0) +{ + + ui::Label * titleLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 18), "Save Browser"); + titleLabel->SetTextColour(style::Colour::WarningTitle); + titleLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + titleLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(titleLabel); + + ui::Textbox * textField = new ui::Textbox(ui::Point(8, 25), ui::Point(Size.X-16, 16), "", "[search]"); + textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + textField->SetActionCallback(new SearchAction(this)); + AddComponent(textField); + + itemList = new ui::ScrollPanel(ui::Point(4, 45), ui::Point(Size.X-8, Size.Y-53)); + AddComponent(itemList); + + progressBar = new ui::ProgressBar(ui::Point((Size.X-200)/2, 45+(Size.Y-66)/2), ui::Point(200, 17)); + AddComponent(progressBar); + + infoText = new ui::Label(ui::Point((Size.X-200)/2, 45+(Size.Y-66)/2), ui::Point(200, 17), "No saves found"); + AddComponent(infoText); + + filesX = 4; + filesY = 3; + buttonPadding = 2; + fileX = 0; + fileY = 0; + + buttonXOffset = 0; + buttonYOffset = 0; + buttonAreaWidth = itemList->Size.X; + buttonAreaHeight = itemList->Size.Y;// - buttonYOffset - 18; + buttonWidth = (buttonAreaWidth/filesX) - buttonPadding*2; + buttonHeight = (buttonAreaHeight/filesY) - buttonPadding*2; + + loadDirectory(directory, ""); +} + +void FileBrowserActivity::DoSearch(std::string search) +{ + if(!loadFiles) + { + loadDirectory(directory, search); + } +} + +void FileBrowserActivity::SelectSave(SaveFile * file) +{ + if(callback) + callback->FileSelected(new SaveFile(*file)); + Exit(); +} + +void FileBrowserActivity::DeleteSave(SaveFile * file) +{ + std::string deleteMessage = "Are you sure you want to delete " + file->GetDisplayName() + ".cps?"; + if (ConfirmPrompt::Blocking("Delete Save", deleteMessage)) + { + remove(file->GetName().c_str()); + loadDirectory(directory, ""); + } +} + +void FileBrowserActivity::RenameSave(SaveFile * file) +{ + std::string newName = TextPrompt::Blocking("Rename", "Change save name", file->GetDisplayName(), "", 0); + if (newName.length()) + { + newName = directory + PATH_SEP + newName + ".cps"; + int ret = rename(file->GetName().c_str(), newName.c_str()); + if (ret) + ErrorMessage::Blocking("Error", "Could not rename file"); + else + loadDirectory(directory, ""); + } + else + ErrorMessage::Blocking("Error", "No save name given"); +} + +void FileBrowserActivity::loadDirectory(std::string directory, std::string search) +{ + for(int i = 0; i < components.size(); i++) + { + RemoveComponent(components[i]); + itemList->RemoveChild(components[i]); + } + + for(std::vector::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter) + { + delete *iter; + } + componentsQueue.clear(); + + for(std::vector::iterator iter = files.begin(), end = files.end(); iter != end; ++iter) + { + delete *iter; + } + files.clear(); + + infoText->Visible = false; + progressBar->Visible = true; + progressBar->SetProgress(-1); + progressBar->SetStatus("Loading files"); + loadFiles = new LoadFilesTask(directory, search); + loadFiles->AddTaskListener(this); + loadFiles->Start(); +} + +void FileBrowserActivity::NotifyDone(Task * task) +{ + fileX = 0; + fileY = 0; + files = ((LoadFilesTask*)task)->GetSaveFiles(); + totalFiles = files.size(); + delete loadFiles; + loadFiles = NULL; + if(!files.size()) + { + progressBar->Visible = false; + infoText->Visible = true; + } + for(int i = 0; i < components.size(); i++) + { + delete components[i]; + } + components.clear(); +} + +void FileBrowserActivity::OnMouseDown(int x, int y, unsigned button) +{ + if(!(x > Position.X && y > Position.Y && y < Position.Y+Size.Y && x < Position.X+Size.X)) //Clicked outside window + Exit(); +} + +void FileBrowserActivity::OnTryExit(ExitMethod method) +{ + Exit(); +} + +void FileBrowserActivity::NotifyError(Task * task) +{ + +} + +void FileBrowserActivity::NotifyProgress(Task * task) +{ + progressBar->SetProgress(task->GetProgress()); +} + +void FileBrowserActivity::NotifyStatus(Task * task) +{ + +} + +void FileBrowserActivity::OnTick(float dt) +{ + if(loadFiles) + loadFiles->Poll(); + + if(files.size()) + { + SaveFile * saveFile = files.back(); + files.pop_back(); + + if(fileX == filesX) + { + fileX = 0; + fileY++; + } + ui::SaveButton * saveButton = new ui::SaveButton( + ui::Point( + buttonXOffset + buttonPadding + fileX*(buttonWidth+buttonPadding*2), + buttonYOffset + buttonPadding + fileY*(buttonHeight+buttonPadding*2) + ), + ui::Point(buttonWidth, buttonHeight), + saveFile); + saveButton->AddContextMenu(1); + saveButton->Tick(dt); + saveButton->SetActionCallback(new SaveSelectedAction(this)); + progressBar->SetStatus("Rendering thumbnails"); + progressBar->SetProgress((float(totalFiles-files.size())/float(totalFiles))*100.0f); + componentsQueue.push_back(saveButton); + fileX++; + } + else if(componentsQueue.size()) + { + for(std::vector::iterator iter = componentsQueue.begin(), end = componentsQueue.end(); iter != end; ++iter) + { + components.push_back(*iter); + itemList->AddChild(*iter); + } + componentsQueue.clear(); + itemList->InnerSize.Y = (buttonHeight+(buttonPadding*2))*fileY; + if(!componentsQueue.size()) + progressBar->Visible = false; + } +} + +void FileBrowserActivity::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + + //Window Background+Outline + g->clearrect(Position.X-2, Position.Y-2, Size.X+4, Size.Y+4); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); +} + +FileBrowserActivity::~FileBrowserActivity() +{ + if(callback) + delete callback; +} \ No newline at end of file diff --git a/src/gui/filebrowser/FileBrowserActivity.h b/src/gui/filebrowser/FileBrowserActivity.h new file mode 100644 index 0000000..d607d49 --- /dev/null +++ b/src/gui/filebrowser/FileBrowserActivity.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include "Activity.h" +#include "gui/interface/Window.h" +#include "tasks/TaskListener.h" + + +class SaveFile; +class FileSelectedCallback +{ +public: + FileSelectedCallback() {} + virtual ~FileSelectedCallback() {} + virtual void FileSelected(SaveFile* file) {} +}; + +namespace ui +{ + class Label; + class ScrollPanel; + class ProgressBar; +} + +class LoadFilesTask; +class FileBrowserActivity: public TaskListener, public WindowActivity +{ + LoadFilesTask * loadFiles; + FileSelectedCallback * callback; + ui::ScrollPanel * itemList; + ui::Label * infoText; + std::vector files; + std::vector components; + std::vector componentsQueue; + std::string directory; + + ui::ProgressBar * progressBar; + + int totalFiles; + int filesX, filesY, buttonPadding; + int fileX, fileY; + int buttonWidth, buttonHeight, buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset; + + + class SearchAction; + void populateList(); +public: + FileBrowserActivity(std::string directory, FileSelectedCallback * callback); + virtual void OnDraw(); + virtual void OnTick(float dt); + virtual void OnTryExit(ExitMethod method); + virtual void OnMouseDown(int x, int y, unsigned button); + void loadDirectory(std::string directory, std::string search); + void SelectSave(SaveFile * file); + void DeleteSave(SaveFile * file); + void RenameSave(SaveFile * file); + void DoSearch(std::string search); + virtual ~FileBrowserActivity(); + + virtual void NotifyDone(Task * task); + virtual void NotifyError(Task * task); + virtual void NotifyProgress(Task * task); + virtual void NotifyStatus(Task * task); +}; \ No newline at end of file diff --git a/src/gui/game/BitmapBrush.h b/src/gui/game/BitmapBrush.h new file mode 100644 index 0000000..e1c0445 --- /dev/null +++ b/src/gui/game/BitmapBrush.h @@ -0,0 +1,96 @@ +/* + * BitmapBrush.h + * + * Created on: Nov 18, 2012 + * Author: Simon Robertshaw + */ + +#ifndef BTIMAPBRUSH_H_ +#define BTIMAPBRUSH_H_ + +#include +#include +#include "Brush.h" + +class BitmapBrush: public Brush +{ +protected: + ui::Point origSize; + unsigned char * origBitmap; +public: + BitmapBrush(std::vector newBitmap, ui::Point rectSize_): + Brush(ui::Point(0, 0)), + origSize(0, 0) + { + ui::Point newSize = rectSize_; + + //Ensure the rect has odd dimentions so we can pull an integer radius with a 1x1 centre + if(!(newSize.X % 2)) + newSize.X += 1; + if(!(newSize.Y % 2)) + newSize.Y += 1; + + radius = (newSize-ui::Point(1, 1))/2; + size = newSize; + origSize = size; + + origBitmap = new unsigned char[size.X*size.Y]; + std::fill(origBitmap, origBitmap+(size.X*size.Y), 0); + for(int y = 0; y < rectSize_.Y; y++) + { + for(int x = 0; x < rectSize_.X; x++) + { + if(newBitmap[(y*rectSize_.X)+x] >= 128) + origBitmap[(y*size.X)+x] = newBitmap[(y*rectSize_.X)+x]; + } + } + + SetRadius(radius); + }; + virtual void GenerateBitmap() + { + if(origBitmap) + { + if(bitmap) + delete[] bitmap; + bitmap = new unsigned char[size.X*size.Y]; + if(size == origSize) + std::copy(origBitmap, origBitmap+(origSize.X*origSize.Y), bitmap); + else + { + //Bilinear interpolation + float factorX = ((float)origSize.X)/((float)size.X); + float factorY = ((float)origSize.Y)/((float)size.Y); + for(int y = 0; y < size.Y; y++) + { + for(int x = 0; x < size.X; x++) + { + float originalY = ((float)y)*factorY; + float originalX = ((float)x)*factorX; + + int lowerX = std::floor(originalX); + int upperX = std::min((float)(origSize.X-1), std::floor(originalX+1.0f)); + int lowerY = std::floor(originalY); + int upperY = std::min((float)(origSize.Y-1), std::floor(originalY+1.0f)); + + unsigned char topRight = origBitmap[(lowerY*origSize.X)+upperX]; + unsigned char topLeft = origBitmap[(lowerY*origSize.X)+lowerX]; + unsigned char bottomRight = origBitmap[(upperY*origSize.X)+upperX]; + unsigned char bottomLeft = origBitmap[(upperY*origSize.X)+lowerX]; + float top = LinearInterpolate(topLeft, topRight, lowerX, upperX, originalX); + float bottom = LinearInterpolate(bottomLeft, bottomRight, lowerX, upperX, originalX); + float mid = LinearInterpolate(top, bottom, lowerY, upperY, originalY); + bitmap[(y*size.X)+x] = mid > 128 ? 255 : 0; + } + } + } + } + } + virtual ~BitmapBrush() + { + if(origBitmap) + delete[] origBitmap; + } +}; + +#endif /* BTIMAPBRUSH_H_ */ diff --git a/src/gui/game/Brush.cpp b/src/gui/game/Brush.cpp new file mode 100644 index 0000000..9f2f2a8 --- /dev/null +++ b/src/gui/game/Brush.cpp @@ -0,0 +1,51 @@ +#include "Brush.h" +#include "graphics/Renderer.h" + +void Brush::RenderRect(Renderer * ren, ui::Point position1, ui::Point position2) +{ + int width, height, t; + width = position2.X-position1.X; + height = position2.Y-position1.Y; + if(height<0) + { + position1.Y += height; + height *= -1; + } + if(width<0) + { + position1.X += width; + width *= -1; + } + + ren->xor_line(position1.X, position1.Y, position1.X+width, position1.Y); + if(height>0){ + ren->xor_line(position1.X, position1.Y+height, position1.X+width, position1.Y+height); + if(height>1){ + ren->xor_line(position1.X+width, position1.Y+1, position1.X+width, position1.Y+height-1); + if(width>0) + ren->xor_line(position1.X, position1.Y+1, position1.X, position1.Y+height-1); + } + } +} + +void Brush::RenderLine(Renderer * ren, ui::Point position1, ui::Point position2) +{ + ren->xor_line(position1.X, position1.Y, position2.X, position2.Y); +} + +void Brush::RenderPoint(Renderer * ren, ui::Point position) +{ + if(!outline) + updateOutline(); + if(!outline) + return; + ren->xor_bitmap(outline, position.X-radius.X, position.Y-radius.Y, size.X, size.Y); +} + +void Brush::RenderFill(Renderer * ren, ui::Point position) +{ + ren->xor_line(position.X-5, position.Y, position.X-1, position.Y); + ren->xor_line(position.X+5, position.Y, position.X+1, position.Y); + ren->xor_line(position.X, position.Y-5, position.X, position.Y-1); + ren->xor_line(position.X, position.Y+5, position.X, position.Y+1); +} diff --git a/src/gui/game/Brush.h b/src/gui/game/Brush.h new file mode 100644 index 0000000..a853609 --- /dev/null +++ b/src/gui/game/Brush.h @@ -0,0 +1,108 @@ +#ifndef BRUSH_H_ +#define BRUSH_H_ + +#include +#include "gui/interface/Point.h" + +class Renderer; +class Brush +{ +protected: + unsigned char * outline; + unsigned char * bitmap; + ui::Point size; + ui::Point radius; + void updateOutline() + { + if(!bitmap) + GenerateBitmap(); + if(!bitmap) + return; + if(outline) + delete[] outline; + outline = new unsigned char[size.X*size.Y]; + for(int x = 0; x < size.X; x++) + { + for(int y = 0; y < size.Y; y++) + { + if(bitmap[y*size.X+x] && (!y || !x || x == size.X-1 || y == size.Y-1 || !bitmap[y*size.X+(x+1)] || !bitmap[y*size.X+(x-1)] || !bitmap[(y-1)*size.X+x] || !bitmap[(y+1)*size.X+x])) + { + outline[y*size.X+x] = 255; + } + else + outline[y*size.X+x] = 0; + } + } + } +public: + Brush(ui::Point size_): + bitmap(NULL), + outline(NULL), + radius(0, 0), + size(0, 0) + { + SetRadius(size_); + }; + + //Radius of the brush 0x0 - infxinf (Radius of 0x0 would be 1x1, radius of 1x1 would be 3x3) + ui::Point GetRadius() + { + return radius; + } + + //Size of the brush bitmap mask, 1x1 - infxinf + ui::Point GetSize() + { + return size; + } + virtual void SetRadius(ui::Point radius) + { + this->radius = radius; + this->size = radius+radius+ui::Point(1, 1); + + GenerateBitmap(); + updateOutline(); + } + virtual ~Brush() { + if(bitmap) + delete[] bitmap; + if(outline) + delete[] outline; + } + virtual void RenderRect(Renderer * ren, ui::Point position1, ui::Point position2); + virtual void RenderLine(Renderer * ren, ui::Point position1, ui::Point position2); + virtual void RenderPoint(Renderer * ren, ui::Point position); + virtual void RenderFill(Renderer * ren, ui::Point position); + virtual void GenerateBitmap() + { + if(bitmap) + delete[] bitmap; + bitmap = new unsigned char[size.X*size.Y]; + for(int x = 0; x < size.X; x++) + { + for(int y = 0; y < size.Y; y++) + { + bitmap[(y*size.X)+x] = 255; + } + } + } + //Get a bitmap for drawing particles + unsigned char * GetBitmap() + { + if(!bitmap) + GenerateBitmap(); + return bitmap; + } + + unsigned char * GetOutline() + { + if(!outline) + updateOutline(); + if(!outline) + return NULL; + return outline; + } +}; + + +#endif /* BRUSH_H_ */ diff --git a/src/gui/game/DecorationTool.h b/src/gui/game/DecorationTool.h new file mode 100644 index 0000000..679c854 --- /dev/null +++ b/src/gui/game/DecorationTool.h @@ -0,0 +1,43 @@ + +#ifndef DECORATIONTOOL_H_ +#define DECORATIONTOOL_H_ + +#include "Tool.h" + +class DecorationTool: public Tool +{ +public: + enum ToolType { BlendAdd = DECO_ADD, BlendRemove = DECO_SUBTRACT, BlendMultiply = DECO_MULTIPLY, BlendDivide = DECO_DIVIDE, BlendSet = DECO_DRAW, BlendSmudge = DECO_SMUDGE, Remove = DECO_CLEAR }; + + ToolType decoMode; + + unsigned char Red; + unsigned char Green; + unsigned char Blue; + unsigned char Alpha; + + DecorationTool(ToolType decoMode_, string name, string description, int r, int g, int b, std::string identifier): + Tool(0, name, description, r, g, b, identifier), + decoMode(decoMode_), + Red(0), + Green(0), + Blue(0), + Alpha(0) + { + } + virtual ~DecorationTool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position){ + sim->ApplyDecorationPoint(position.X, position.Y, Red, Green, Blue, Alpha, decoMode, brush); + } + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + sim->ApplyDecorationLine(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode, brush); + } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + sim->ApplyDecorationBox(position1.X, position1.Y, position2.X, position2.Y, Red, Green, Blue, Alpha, decoMode); + } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { + + } +}; + +#endif diff --git a/src/gui/game/EllipseBrush.h b/src/gui/game/EllipseBrush.h new file mode 100644 index 0000000..ad28766 --- /dev/null +++ b/src/gui/game/EllipseBrush.h @@ -0,0 +1,56 @@ +#ifndef ELIPSEBRUSH_H_ +#define ELIPSEBRUSH_H_ + +#include +#include "Brush.h" + +class EllipseBrush: public Brush +{ +public: + EllipseBrush(ui::Point size_): + Brush(size_) + { + SetRadius(size_); + }; + virtual void GenerateBitmap() + { + if(bitmap) + delete[] bitmap; + bitmap = new unsigned char[size.X*size.Y]; + int rx = radius.X; + int ry = radius.Y; + + if (!rx) + { + for (int j = 0; j <= 2*ry; j++) + { + bitmap[j*(size.X)+rx] = 255; + } + } + else + { + int yTop = ry, yBottom, i, j; + for (i = 0; i <= rx; i++) + { + while (pow(i-rx,2.0f)*pow(ry,2.0f) + pow(yTop-ry,2.0f)*pow(rx,2.0f) <= pow(rx,2.0f)*pow(ry,2.0f)) + yTop++; + yBottom = 2*ry - yTop; + for (int j = 0; j <= ry*2; j++) + { + if (j > yBottom && j < yTop) + { + bitmap[j*(size.X)+i] = 255; + bitmap[j*(size.X)+2*rx-i] = 255; + } + else + { + bitmap[j*(size.X)+i] = 0; + bitmap[j*(size.X)+2*rx-i] = 0; + } + } + } + } + } +}; + +#endif /* ELIPSEBRUSH_H_ */ diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp new file mode 100644 index 0000000..db572e6 --- /dev/null +++ b/src/gui/game/GameController.cpp @@ -0,0 +1,1426 @@ + +#include +#include +#include "Config.h" +#include "Format.h" +#include "GameController.h" +#include "GameModel.h" +#include "client/SaveInfo.h" +#include "client/GameSave.h" +#include "search/SearchController.h" +#include "render/RenderController.h" +#include "login/LoginController.h" +#include "gui/interface/Point.h" +#include "dialogues/ErrorMessage.h" +#include "dialogues/InformationMessage.h" +#include "dialogues/ConfirmPrompt.h" +#include "GameModelException.h" +#include "simulation/Air.h" +#include "elementsearch/ElementSearchActivity.h" +#include "profile/ProfileActivity.h" +#include "colourpicker/ColourPickerActivity.h" +#include "update/UpdateActivity.h" +#include "Notification.h" +#include "filebrowser/FileBrowserActivity.h" +#include "save/LocalSaveActivity.h" +#include "save/ServerSaveActivity.h" +#include "gui/interface/Keys.h" +#include "simulation/Snapshot.h" +#include "debug/DebugInfo.h" +//#include "debug/ElementPopulation.h" + +using namespace std; + +class GameController::SearchCallback: public ControllerCallback +{ + GameController * cc; +public: + SearchCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + if(cc->search->GetLoadedSave()) + { + try + { + cc->gameModel->SetSave(cc->search->GetLoadedSave()); + cc->search->ReleaseLoadedSave(); + } + catch(GameModelException & ex) + { + new ErrorMessage("Cannot open save", ex.what()); + } + } + } +}; + +class GameController::SaveOpenCallback: public ControllerCallback +{ + GameController * cc; +public: + SaveOpenCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + if(cc->activePreview->GetDoOpen() && cc->activePreview->GetSave()) + { + try + { + cc->LoadSave(cc->activePreview->GetSave()); + } + catch(GameModelException & ex) + { + new ErrorMessage("Cannot open save", ex.what()); + } + } + } +}; + + +class GameController::RenderCallback: public ControllerCallback +{ + GameController * cc; +public: + RenderCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + //cc->gameModel->SetUser(cc->loginWindow->GetUser()); + } +}; + +class GameController::OptionsCallback: public ControllerCallback +{ + GameController * cc; +public: + OptionsCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + cc->gameModel->UpdateQuickOptions(); + //cc->gameModel->SetUser(cc->loginWindow->GetUser()); + } +}; + +class GameController::TagsCallback: public ControllerCallback +{ + GameController * cc; +public: + TagsCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + cc->gameView->NotifySaveChanged(cc->gameModel); + } +}; + +class GameController::StampsCallback: public ControllerCallback +{ + GameController * cc; +public: + StampsCallback(GameController * cc_) { cc = cc_; } + virtual void ControllerExit() + { + if(cc->localBrowser->GetSave()) + { + cc->gameModel->SetStamp(cc->localBrowser->GetSave()->GetGameSave()); + if (cc->localBrowser->GetMoveToFront()) + Client::Ref().MoveStampToFront(cc->localBrowser->GetSave()->GetName()); + cc->LoadStamp(); + } + } +}; + +GameController::GameController(): + search(NULL), + renderOptions(NULL), + loginWindow(NULL), + console(NULL), + tagsWindow(NULL), + options(NULL), + activePreview(NULL), + localBrowser(NULL), + HasDone(false), + firstTick(true) +{ + gameView = new GameView(); + gameModel = new GameModel(); + gameModel->BuildQuickOptionMenu(this); + + gameView->AttachController(this); + gameModel->AddObserver(gameView); + + commandInterface = new LuaScriptInterface(this, gameModel);//new TPTScriptInterface(); + ((LuaScriptInterface*)commandInterface)->SetWindow(gameView); + + commandInterface->OnBrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().X); + ActiveToolChanged(0, gameModel->GetActiveTool(0)); + ActiveToolChanged(1, gameModel->GetActiveTool(1)); + ActiveToolChanged(2, gameModel->GetActiveTool(2)); + + //sim = new Simulation(); + Client::Ref().AddListener(this); + + //debugInfo.push_back(new ElementPopulationDebug(gameModel->GetSimulation())); +} + +GameController::~GameController() +{ + if(search) + { + delete search; + } + if(renderOptions) + { + delete renderOptions; + } + if(loginWindow) + { + delete loginWindow; + } + if(tagsWindow) + { + delete tagsWindow; + } + if(console) + { + delete console; + } + if(activePreview) + { + delete activePreview; + } + if(localBrowser) + { + delete localBrowser; + } + if (options) + { + delete options; + } + if(ui::Engine::Ref().GetWindow() == gameView) + { + ui::Engine::Ref().CloseWindow(); + } + //deleted here because it refuses to be deleted when deleted from gameModel even with the same code + std::deque history = gameModel->GetHistory(); + for(std::deque::iterator iter = history.begin(), end = history.end(); iter != end; ++iter) + { + delete *iter; + } + std::vector quickOptions = gameModel->GetQuickOptions(); + for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) + { + delete *iter; + } + std::vector notifications = gameModel->GetNotifications(); + for(std::vector::iterator iter = notifications.begin(); iter != notifications.end(); ++iter) + { + delete *iter; + } + delete gameModel; + delete gameView; +} + +void GameController::HistoryRestore() +{ + std::deque history = gameModel->GetHistory(); + if(history.size()) + { + Snapshot * snap = history.back(); + gameModel->GetSimulation()->Restore(*snap); + if(history.size()>1) + { + history.pop_back(); + delete snap; + gameModel->SetHistory(history); + } + } +} + +void GameController::HistorySnapshot() +{ + std::deque history = gameModel->GetHistory(); + Snapshot * newSnap = gameModel->GetSimulation()->CreateSnapshot(); + if(newSnap) + { + if(history.size() >= 1) //History limit is current 1 + { + Snapshot * snap = history.front(); + history.pop_front(); + //snap->Particles.clear(); + delete snap; + } + history.push_back(newSnap); + gameModel->SetHistory(history); + } +} + +GameView * GameController::GetView() +{ + return gameView; +} + +void GameController::PlaceSave(ui::Point position) +{ + if(gameModel->GetPlaceSave()) + { + gameModel->GetSimulation()->Load(position.X, position.Y, gameModel->GetPlaceSave()); + gameModel->SetPaused(gameModel->GetPlaceSave()->paused | gameModel->GetPaused()); + } +} + +void GameController::Install() +{ +#if defined(MACOSX) + new InformationMessage("No Installation necessary", "You don't need to install The Powder Toy on Mac OS X", false); +#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!", false); + } + else + { + new ErrorMessage("Could not install", "The installation did not complete due to an error"); + } + } + } + virtual ~InstallConfirmation() { } + }; + new ConfirmPrompt("Install The Powder Toy", "Do you wish to install The Powder Toy on this computer?\nThis allows you to open save files and saves directly from the website.", new InstallConfirmation(this)); +#else + new ErrorMessage("Cannot install", "You cannot install The Powder Toy on this platform"); +#endif +} + +void GameController::AdjustGridSize(int direction) +{ + if(direction > 0) + gameModel->GetRenderer()->SetGridSize((gameModel->GetRenderer()->GetGridSize()+1)%10); + else + gameModel->GetRenderer()->SetGridSize((gameModel->GetRenderer()->GetGridSize()+9)%10); +} + +void GameController::InvertAirSim() +{ + gameModel->GetSimulation()->air->Invert(); +} + + +void GameController::AdjustBrushSize(int direction, bool logarithmic, bool xAxis, bool yAxis) +{ + if(xAxis && yAxis) + return; + + ui::Point newSize(0, 0); + ui::Point oldSize = gameModel->GetBrush()->GetRadius(); + if(logarithmic) + newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction * ((gameModel->GetBrush()->GetRadius().X/5)>0?gameModel->GetBrush()->GetRadius().X/5:1), direction * ((gameModel->GetBrush()->GetRadius().Y/5)>0?gameModel->GetBrush()->GetRadius().Y/5:1)); + else + newSize = gameModel->GetBrush()->GetRadius() + ui::Point(direction, direction); + if(newSize.X < 0) + newSize.X = 0; + if(newSize.Y < 0) + newSize.Y = 0; + if(newSize.X > 200) + newSize.X = 200; + if(newSize.Y > 200) + newSize.Y = 200; + + if(xAxis) + gameModel->GetBrush()->SetRadius(ui::Point(newSize.X, oldSize.Y)); + else if(yAxis) + gameModel->GetBrush()->SetRadius(ui::Point(oldSize.X, newSize.Y)); + else + gameModel->GetBrush()->SetRadius(newSize); + + BrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().Y); +} + +void GameController::AdjustZoomSize(int direction, bool logarithmic) +{ + int newSize; + if(logarithmic) + newSize = gameModel->GetZoomSize()+(((gameModel->GetZoomSize()/10)>0?(gameModel->GetZoomSize()/10):1)*direction); + else + newSize = gameModel->GetZoomSize()+direction; + if(newSize<5) + newSize = 5; + if(newSize>64) + newSize = 64; + gameModel->SetZoomSize(newSize); + + int newZoomFactor = 256/newSize; + if(newZoomFactor<3) + newZoomFactor = 3; + gameModel->SetZoomFactor(newZoomFactor); +} + +ui::Point GameController::PointTranslate(ui::Point point) +{ + if(point.X >= XRES) + point.X = XRES-1; + if(point.Y >= YRES) + point.Y = YRES-1; + if(point.Y < 0) + point.Y = 0; + if(point.X < 0) + point.X = 0; + + bool zoomEnabled = gameModel->GetZoomEnabled(); + if(!zoomEnabled) + return point; + //If we try to draw inside the zoom window, normalise the coordinates + int zoomFactor = gameModel->GetZoomFactor(); + ui::Point zoomWindowPosition = gameModel->GetZoomWindowPosition(); + ui::Point zoomWindowSize = ui::Point(gameModel->GetZoomSize()*zoomFactor, gameModel->GetZoomSize()*zoomFactor); + + if(point.X >= zoomWindowPosition.X && point.X >= zoomWindowPosition.Y && point.X <= zoomWindowPosition.X+zoomWindowSize.X && point.Y <= zoomWindowPosition.Y+zoomWindowSize.Y) + return ((point-zoomWindowPosition)/gameModel->GetZoomFactor())+gameModel->GetZoomPosition(); + return point; +} + +ui::Point GameController::NormaliseBlockCoord(ui::Point point) +{ + return (point/CELL)*CELL; +} + +void GameController::DrawRect(int toolSelection, ui::Point point1, ui::Point point2) +{ + Simulation * sim = gameModel->GetSimulation(); + Tool * activeTool = gameModel->GetActiveTool(toolSelection); + gameModel->SetLastTool(activeTool); + Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + return; + activeTool->SetStrength(gameModel->GetToolStrength()); + activeTool->DrawRect(sim, cBrush, point1, point2); +} + +void GameController::DrawLine(int toolSelection, ui::Point point1, ui::Point point2) +{ + Simulation * sim = gameModel->GetSimulation(); + Tool * activeTool = gameModel->GetActiveTool(toolSelection); + gameModel->SetLastTool(activeTool); + Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + return; + activeTool->SetStrength(gameModel->GetToolStrength()); + activeTool->DrawLine(sim, cBrush, point1, point2); +} + +void GameController::DrawFill(int toolSelection, ui::Point point) +{ + Simulation * sim = gameModel->GetSimulation(); + Tool * activeTool = gameModel->GetActiveTool(toolSelection); + gameModel->SetLastTool(activeTool); + Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + return; + activeTool->SetStrength(gameModel->GetToolStrength()); + activeTool->DrawFill(sim, cBrush, point); +} + +void GameController::DrawPoints(int toolSelection, queue & pointQueue) +{ + Simulation * sim = gameModel->GetSimulation(); + Tool * activeTool = gameModel->GetActiveTool(toolSelection); + gameModel->SetLastTool(activeTool); + Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + { + if(!pointQueue.empty()) + { + while(!pointQueue.empty()) + { + //delete pointQueue.front(); + pointQueue.pop(); + } + } + return; + } + + activeTool->SetStrength(gameModel->GetToolStrength()); + if(!pointQueue.empty()) + { + ui::Point sPoint(0, 0); + bool first = true; + while(!pointQueue.empty()) + { + ui::Point fPoint = pointQueue.front(); + //delete pointQueue.front(); + pointQueue.pop(); + if(!first) + { + activeTool->DrawLine(sim, cBrush, sPoint, fPoint, true); + } + else + { + first = false; + activeTool->Draw(sim, cBrush, fPoint); + } + sPoint = fPoint; + } + } +} + +void GameController::LoadClipboard() +{ + gameModel->SetPlaceSave(gameModel->GetClipboard()); + if(gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) + gameModel->GetPlaceSave()->Expand(); +} + +void GameController::LoadStamp() +{ + gameModel->SetPlaceSave(gameModel->GetStamp()); + if(gameModel->GetPlaceSave() && gameModel->GetPlaceSave()->Collapsed()) + gameModel->GetPlaceSave()->Expand(); +} + +void GameController::TranslateSave(ui::Point point) +{ + matrix2d transform = m2d_identity; + vector2d translate = v2d_new(point.X, point.Y); + gameModel->GetPlaceSave()->Transform(transform, translate); + gameModel->SetPlaceSave(gameModel->GetPlaceSave()); +} + +void GameController::TransformSave(matrix2d transform) +{ + vector2d translate = v2d_zero; + gameModel->GetPlaceSave()->Transform(transform, translate); + gameModel->SetPlaceSave(gameModel->GetPlaceSave()); +} + +void GameController::ToolClick(int toolSelection, ui::Point point) +{ + Simulation * sim = gameModel->GetSimulation(); + Tool * activeTool = gameModel->GetActiveTool(toolSelection); + Brush * cBrush = gameModel->GetBrush(); + if(!activeTool || !cBrush) + return; + activeTool->Click(sim, cBrush, point); +} + +void GameController::StampRegion(ui::Point point1, ui::Point point2) +{ + GameSave * newSave; + newSave = gameModel->GetSimulation()->Save(point1.X, point1.Y, point2.X, point2.Y); + if(newSave) + { + newSave->paused = gameModel->GetPaused(); + gameModel->AddStamp(newSave); + } + else + new ErrorMessage("Could not create stamp", "Error generating save file"); +} + +void GameController::CopyRegion(ui::Point point1, ui::Point point2) +{ + GameSave * newSave; + newSave = gameModel->GetSimulation()->Save(point1.X, point1.Y, point2.X, point2.Y); + if(newSave) + { + newSave->paused = gameModel->GetPaused(); + gameModel->SetClipboard(newSave); + } +} + +void GameController::CutRegion(ui::Point point1, ui::Point point2) +{ + CopyRegion(point1, point2); + gameModel->GetSimulation()->clear_area(point1.X, point1.Y, point2.X-point1.X, point2.Y-point1.Y); +} + +bool GameController::MouseMove(int x, int y, int dx, int dy) +{ + return commandInterface->OnMouseMove(x, y, dx, dy); +} + +bool GameController::BrushChanged(int brushType, int rx, int ry) +{ + return commandInterface->OnBrushChanged(brushType, rx, ry); +} + +bool GameController::MouseDown(int x, int y, unsigned button) +{ + return commandInterface->OnMouseDown(x, y, button); +} + +bool GameController::MouseUp(int x, int y, unsigned button) +{ + bool ret = commandInterface->OnMouseUp(x, y, button); + ui::Point point = PointTranslate(ui::Point(x, y)); + x = point.X; + y = point.Y; + if(ret && yGetActiveTool(0)->GetIdentifier() != "DEFAULT_UI_SIGN" || button != BUTTON_LEFT) //If it's not a sign tool or you are right/middle clicking + { + Simulation * sim = gameModel->GetSimulation(); + for (std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) + { + int signx, signy, signw, signh; + (*iter).pos((*iter).getText(sim), signx, signy, signw, signh); + if (x>=signx && x<=signx+signw && y>=signy && y<=signy+signh) + { + if (sregexp((*iter).text.c_str(), "^{[c|t]:[0-9]*|.*}$")==0) + { + const char * signText = (*iter).text.c_str(); + char buff[256]; + int sldr; + + memset(buff, 0, sizeof(buff)); + + for (sldr=3; signText[sldr] != '|'; sldr++) + buff[sldr-3] = signText[sldr]; + + buff[sldr-3] = '\0'; + + int tempSaveID = format::StringToNumber(std::string(buff)); + if (tempSaveID) + { + if ((*iter).text.c_str()[1] == 'c') + OpenSavePreview(tempSaveID, 0); + else if ((*iter).text.c_str()[1] == 't') + { + char url[256]; + sprintf(url, "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=%i", tempSaveID); + OpenURI(url); + } + } + break; + } + } + } + } + } + return ret; +} + +bool GameController::MouseWheel(int x, int y, int d) +{ + return commandInterface->OnMouseWheel(x, y, d); +} + +bool GameController::KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + bool ret = commandInterface->OnKeyPress(key, character, shift, ctrl, alt); + if(ret) + { + Simulation * sim = gameModel->GetSimulation(); + if (key == KEY_RIGHT) + { + sim->player.comm = (int)(sim->player.comm)|0x02; //Go right command + } + if (key == KEY_LEFT) + { + sim->player.comm = (int)(sim->player.comm)|0x01; //Go left command + } + if (key == KEY_DOWN && ((int)(sim->player.comm)&0x08)!=0x08) + { + sim->player.comm = (int)(sim->player.comm)|0x08; //Use element command + } + if (key == KEY_UP && ((int)(sim->player.comm)&0x04)!=0x04) + { + sim->player.comm = (int)(sim->player.comm)|0x04; //Jump command + } + + if (key == KEY_d) + { + sim->player2.comm = (int)(sim->player2.comm)|0x02; //Go right command + } + if (key == KEY_a) + { + sim->player2.comm = (int)(sim->player2.comm)|0x01; //Go left command + } + if (key == KEY_s && ((int)(sim->player2.comm)&0x08)!=0x08) + { + sim->player2.comm = (int)(sim->player2.comm)|0x08; //Use element command + } + if (key == KEY_w && ((int)(sim->player2.comm)&0x04)!=0x04) + { + sim->player2.comm = (int)(sim->player2.comm)|0x04; //Jump command + } + + if((!sim->elementCount[PT_STKM2] || ctrl) && gameView->GetSelectMode() == SelectNone) + { + switch(key) + { + case 'w': + SwitchGravity(); + break; + case 'd': + gameView->ToggleDebug(); + break; + case 's': + gameView->BeginStampSelection(); + break; + } + } + } + return ret; +} + +bool GameController::KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + bool ret = commandInterface->OnKeyRelease(key, character, shift, ctrl, alt); + if(ret) + { + Simulation * sim = gameModel->GetSimulation(); + if (key == KEY_RIGHT || key == KEY_LEFT) + { + sim->player.pcomm = sim->player.comm; //Saving last movement + sim->player.comm = (int)(sim->player.comm)&12; //Stop command + } + if (key == KEY_UP) + { + sim->player.comm = (int)(sim->player.comm)&11; + } + if (key == KEY_DOWN) + { + sim->player.comm = (int)(sim->player.comm)&7; + } + + if (key == KEY_d || key == KEY_a) + { + sim->player2.pcomm = sim->player2.comm; //Saving last movement + sim->player2.comm = (int)(sim->player2.comm)&12; //Stop command + } + if (key == KEY_w) + { + sim->player2.comm = (int)(sim->player2.comm)&11; + } + if (key == KEY_s) + { + sim->player2.comm = (int)(sim->player2.comm)&7; + } + } + return ret; +} + +void GameController::Tick() +{ + if(firstTick) + { + ((LuaScriptInterface*)commandInterface)->Init(); + if(!Client::Ref().GetPrefBool("InstallCheck", false)) + { + Client::Ref().SetPref("InstallCheck", true); + Install(); + } + firstTick = false; + } + for(std::vector::iterator iter = debugInfo.begin(), end = debugInfo.end(); iter != end; iter++) + { + (*iter)->Draw(ui::Point(10, 10)); + } + commandInterface->OnTick(); +} + +void GameController::Exit() +{ + if(ui::Engine::Ref().GetWindow() == gameView) + ui::Engine::Ref().CloseWindow(); + HasDone = true; +} + +void GameController::ResetAir() +{ + Simulation * sim = gameModel->GetSimulation(); + sim->air->Clear(); + for (int i = 0; i < NPART; i++) + { + if (sim->parts[i].type == PT_QRTZ || sim->parts[i].type == PT_GLAS) + { + sim->parts[i].pavg[0] = sim->parts[i].pavg[1] = 0; + } + } +} + +void GameController::ResetSpark() +{ + Simulation * sim = gameModel->GetSimulation(); + for (int i = 0; i < NPART; i++) + if (sim->parts[i].type == PT_SPRK) + { + if (sim->parts[i].ctype >= 0 && sim->parts[i].ctype < PT_NUM && sim->elements[sim->parts[i].ctype].Enabled) + { + sim->parts[i].type = sim->parts[i].ctype; + sim->parts[i].life = 0; + } + else + sim->kill_part(i); + } +} + +void GameController::SwitchGravity() +{ + gameModel->GetSimulation()->gravityMode = (gameModel->GetSimulation()->gravityMode+1)%3; + + switch (gameModel->GetSimulation()->gravityMode) + { + case 0: + gameModel->SetInfoTip("Gravity: Vertical"); + break; + case 1: + gameModel->SetInfoTip("Gravity: Off"); + break; + case 2: + gameModel->SetInfoTip("Gravity: Radial"); + break; + } +} + +void GameController::SwitchAir() +{ + gameModel->GetSimulation()->air->airMode = (gameModel->GetSimulation()->air->airMode+1)%5; + + switch (gameModel->GetSimulation()->air->airMode) + { + case 0: + gameModel->SetInfoTip("Air: On"); + break; + case 1: + gameModel->SetInfoTip("Air: Pressure Off"); + break; + case 2: + gameModel->SetInfoTip("Air: Velocity Off"); + break; + case 3: + gameModel->SetInfoTip("Air: Off"); + break; + case 4: + gameModel->SetInfoTip("Air: No Update"); + break; + } +} + +void GameController::ToggleAHeat() +{ + gameModel->SetAHeatEnable(!gameModel->GetAHeatEnable()); +} + + +void GameController::LoadRenderPreset(int presetNum) +{ + Renderer * renderer = gameModel->GetRenderer(); + RenderPreset preset = renderer->renderModePresets[presetNum]; + gameModel->SetInfoTip(preset.Name); + renderer->SetRenderMode(preset.RenderModes); + renderer->SetDisplayMode(preset.DisplayModes); + renderer->SetColourMode(preset.ColourMode); +} + +void GameController::Update() +{ + ui::Point pos = gameView->GetMousePosition(); + if(pos.X >= 0 && pos.Y >= 0 && pos.X < XRES && pos.Y < YRES) + { + gameModel->GetRenderer()->mousePosX = pos.X; + gameModel->GetRenderer()->mousePosY = pos.Y; + gameView->SetSample(gameModel->GetSimulation()->Get(pos.X, pos.Y)); + } + + gameModel->GetSimulation()->update_particles(); + if(renderOptions && renderOptions->HasExited) + { + delete renderOptions; + renderOptions = NULL; + } + + if(search && search->HasExited) + { + delete search; + search = NULL; + } + + if(activePreview && activePreview->HasExited) + { + delete activePreview; + activePreview = NULL; + } + + if(loginWindow && loginWindow->HasExited) + { + delete loginWindow; + loginWindow = NULL; + } + + if(localBrowser && localBrowser->HasDone) + { + delete localBrowser; + localBrowser = NULL; + } +} + +void GameController::SetZoomEnabled(bool zoomEnabled) +{ + gameModel->SetZoomEnabled(zoomEnabled); +} + +void GameController::SetToolStrength(float value) +{ + gameModel->SetToolStrength(value); +} + +void GameController::SetZoomPosition(ui::Point position) +{ + ui::Point zoomPosition = position-(gameModel->GetZoomSize()/2); + if(zoomPosition.X < 0) + zoomPosition.X = 0; + if(zoomPosition.Y < 0) + zoomPosition.Y = 0; + if(zoomPosition.X >= XRES-gameModel->GetZoomSize()) + zoomPosition.X = XRES-gameModel->GetZoomSize(); + if(zoomPosition.Y >= YRES-gameModel->GetZoomSize()) + zoomPosition.Y = YRES-gameModel->GetZoomSize(); + + ui::Point zoomWindowPosition = ui::Point(0, 0); + if(position.X < XRES/2) + zoomWindowPosition.X = XRES-(gameModel->GetZoomSize()*gameModel->GetZoomFactor()); + + gameModel->SetZoomPosition(zoomPosition); + gameModel->SetZoomWindowPosition(zoomWindowPosition); +} + +void GameController::SetPaused(bool pauseState) +{ + gameModel->SetPaused(pauseState); +} + +void GameController::SetPaused() +{ + gameModel->SetPaused(!gameModel->GetPaused()); +} + +void GameController::SetDecoration(bool decorationState) +{ + gameModel->SetDecoration(decorationState); +} + +void GameController::SetDecoration() +{ + gameModel->SetDecoration(!gameModel->GetDecoration()); +} + +void GameController::ShowGravityGrid() +{ + gameModel->ShowGravityGrid(!gameModel->GetGravityGrid()); + gameModel->UpdateQuickOptions(); +} + +void GameController::SetHudEnable(bool hudState) +{ + gameView->SetHudEnable(hudState); +} + +void GameController::SetActiveColourPreset(int preset) +{ + gameModel->SetActiveColourPreset(preset); +} + +void GameController::SetColour(ui::Colour colour) +{ + gameModel->SetColourSelectorColour(colour); + gameModel->SetPresetColour(colour); +} + +void GameController::SetActiveMenu(Menu * menu) +{ + gameModel->SetActiveMenu(menu); + vector menuList = gameModel->GetMenuList(); + bool set = false; + for(int i = 0; i < menuList.size(); i++) + { + if(menuList[i]==menu && i == SC_DECO) + { + gameModel->SetColourSelectorVisibility(true); + set = true; + } + } + if(!set) + gameModel->SetColourSelectorVisibility(false); +} + +std::vector GameController::GetMenuList() +{ + return gameModel->GetMenuList(); +} + +void GameController::ActiveToolChanged(int toolSelection, Tool *tool) +{ + commandInterface->OnActiveToolChanged(toolSelection, tool); +} + +void GameController::SetActiveTool(int toolSelection, Tool * tool) +{ + gameModel->SetActiveTool(toolSelection, tool); + gameModel->GetRenderer()->gravityZonesEnabled = false; + gameModel->SetLastTool(tool); + for(int i = 0; i < 3; i++) + { + if(gameModel->GetActiveTool(i) == gameModel->GetMenuList().at(SC_WALL)->GetToolList().at(WL_GRAV)) + { + gameModel->GetRenderer()->gravityZonesEnabled = true; + } + } +} + +void GameController::OpenSearch() +{ + if(!search) + search = new SearchController(new SearchCallback(this)); + ui::Engine::Ref().ShowWindow(search->GetView()); +} + +void GameController::OpenLocalSaveWindow(bool asCurrent) +{ + Simulation * sim = gameModel->GetSimulation(); + GameSave * gameSave = sim->Save(); + gameSave->paused = gameModel->GetPaused(); + gameSave->gravityMode = sim->gravityMode; + gameSave->airMode = sim->air->airMode; + gameSave->legacyEnable = sim->legacy_enable; + gameSave->waterEEnabled = sim->water_equal_test; + gameSave->gravityEnable = sim->grav->ngrav_enable; + if(!gameSave) + { + new ErrorMessage("Error", "Unable to build save."); + } + else + { + std::string filename = ""; + if (gameModel->GetSaveFile()) + filename = gameModel->GetSaveFile()->GetDisplayName(); + SaveFile tempSave(filename); + tempSave.SetGameSave(gameSave); + + if (!asCurrent || !gameModel->GetSaveFile()) + { + class LocalSaveCallback: public FileSavedCallback + { + GameController * c; + public: + LocalSaveCallback(GameController * _c): c(_c) {} + virtual ~LocalSaveCallback() {}; + virtual void FileSaved(SaveFile* file) + { + c->gameModel->SetSaveFile(file); + } + }; + + new LocalSaveActivity(tempSave, new LocalSaveCallback(this)); + } + else if (gameModel->GetSaveFile()) + { + Client::Ref().MakeDirectory(LOCAL_SAVE_DIR); + Client::Ref().WriteFile(gameSave->Serialise(), gameModel->GetSaveFile()->GetName()); + } + } +} + +void GameController::LoadSaveFile(SaveFile * file) +{ + gameModel->SetSaveFile(file); +} + + +void GameController::LoadSave(SaveInfo * save) +{ + gameModel->SetSave(save); +} + +void GameController::OpenSavePreview(int saveID, int saveDate) +{ + activePreview = new PreviewController(saveID, new SaveOpenCallback(this)); + ui::Engine::Ref().ShowWindow(activePreview->GetView()); +} + +void GameController::OpenSavePreview() +{ + if(gameModel->GetSave()) + { + activePreview = new PreviewController(gameModel->GetSave()->GetID(), new SaveOpenCallback(this)); + ui::Engine::Ref().ShowWindow(activePreview->GetView()); + } +} + +void GameController::OpenLocalBrowse() +{ + class LocalSaveOpenCallback: public FileSelectedCallback + { + GameController * c; + public: + LocalSaveOpenCallback(GameController * _c): c(_c) {} + virtual ~LocalSaveOpenCallback() {}; + virtual void FileSelected(SaveFile* file) + { + c->LoadSaveFile(file); + delete file; + } + }; + new FileBrowserActivity(LOCAL_SAVE_DIR PATH_SEP, new LocalSaveOpenCallback(this)); +} + +void GameController::OpenLogin() +{ + if(Client::Ref().GetAuthUser().ID) + { + new ProfileActivity(Client::Ref().GetAuthUser().Username); + } + else + { + loginWindow = new LoginController(); + ui::Engine::Ref().ShowWindow(loginWindow->GetView()); + } +} + +void GameController::OpenElementSearch() +{ + vector toolList; + vector menuList = gameModel->GetMenuList(); + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter!=end; ++iter) { + if(!(*iter)) + continue; + vector menuToolList = (*iter)->GetToolList(); + if(!menuToolList.size()) + continue; + toolList.insert(toolList.end(), menuToolList.begin(), menuToolList.end()); + } + vector hiddenTools = gameModel->GetUnlistedTools(); + toolList.insert(toolList.end(), hiddenTools.begin(), hiddenTools.end()); + new ElementSearchActivity(gameModel, toolList); +} + +void GameController::OpenColourPicker() +{ + class ColourPickerCallback: public ColourPickedCallback + { + GameController * c; + public: + ColourPickerCallback(GameController * _c): c(_c) {} + virtual ~ColourPickerCallback() {}; + virtual void ColourPicked(ui::Colour colour) + { + c->SetColour(colour); + } + }; + new ColourPickerActivity(gameModel->GetColourSelectorColour(), new ColourPickerCallback(this)); +} + +void GameController::OpenTags() +{ + if(gameModel->GetUser().ID) + { + if(gameModel->GetSave() && gameModel->GetSave()->GetID()) + { + tagsWindow = new TagsController(new TagsCallback(this), gameModel->GetSave()); + ui::Engine::Ref().ShowWindow(tagsWindow->GetView()); + } + else + { + new ErrorMessage("Error", "No save open"); + } + } + else + { + new ErrorMessage("Error", "You need to login to edit tags."); + } +} + +void GameController::OpenStamps() +{ + localBrowser = new LocalBrowserController(new StampsCallback(this)); + ui::Engine::Ref().ShowWindow(localBrowser->GetView()); +} + +void GameController::OpenOptions() +{ + options = new OptionsController(gameModel, new OptionsCallback(this)); + ui::Engine::Ref().ShowWindow(options->GetView()); + +} + +void GameController::ShowConsole() +{ + if(!console) + console = new ConsoleController(NULL, commandInterface); + if (console->GetView() != ui::Engine::Ref().GetWindow()) + ui::Engine::Ref().ShowWindow(console->GetView()); +} + +void GameController::HideConsole() +{ + if(!console) + return; + if (console->GetView() == ui::Engine::Ref().GetWindow()) + ui::Engine::Ref().CloseWindow(); +} + +void GameController::OpenRenderOptions() +{ + renderOptions = new RenderController(gameModel->GetRenderer(), new RenderCallback(this)); + ui::Engine::Ref().ShowWindow(renderOptions->GetView()); +} + +void GameController::OpenSaveWindow() +{ + class SaveUploadedCallback: public ServerSaveActivity::SaveUploadedCallback + { + GameController * c; + public: + SaveUploadedCallback(GameController * _c): c(_c) {} + virtual ~SaveUploadedCallback() {}; + virtual void SaveUploaded(SaveInfo save) + { + c->LoadSave(&save); + } + }; + if(gameModel->GetUser().ID) + { + Simulation * sim = gameModel->GetSimulation(); + GameSave * gameSave = sim->Save(); + gameSave->paused = gameModel->GetPaused(); + gameSave->gravityMode = sim->gravityMode; + gameSave->airMode = sim->air->airMode; + gameSave->legacyEnable = sim->legacy_enable; + gameSave->waterEEnabled = sim->water_equal_test; + gameSave->gravityEnable = sim->grav->ngrav_enable; + if(!gameSave) + { + new ErrorMessage("Error", "Unable to build save."); + } + else + { + if(gameModel->GetSave()) + { + SaveInfo tempSave(*gameModel->GetSave()); + tempSave.SetGameSave(gameSave); + new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); + } + else + { + SaveInfo tempSave(0, 0, 0, 0, gameModel->GetUser().Username, ""); + tempSave.SetGameSave(gameSave); + new ServerSaveActivity(tempSave, new SaveUploadedCallback(this)); + } + } + } + else + { + new ErrorMessage("Error", "You need to login to upload saves."); + } +} + +void GameController::SaveAsCurrent() +{ + + class SaveUploadedCallback: public ServerSaveActivity::SaveUploadedCallback + { + GameController * c; + public: + SaveUploadedCallback(GameController * _c): c(_c) {} + virtual ~SaveUploadedCallback() {}; + virtual void SaveUploaded(SaveInfo save) + { + c->LoadSave(&save); + } + }; + + if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetUser().Username == gameModel->GetSave()->GetUserName()) + { + Simulation * sim = gameModel->GetSimulation(); + GameSave * gameSave = sim->Save(); + gameSave->paused = gameModel->GetPaused(); + gameSave->gravityMode = sim->gravityMode; + gameSave->airMode = sim->air->airMode; + gameSave->legacyEnable = sim->legacy_enable; + gameSave->waterEEnabled = sim->water_equal_test; + gameSave->gravityEnable = sim->grav->ngrav_enable; + if(!gameSave) + { + new ErrorMessage("Error", "Unable to build save."); + } + else + { + if(gameModel->GetSave()) + { + SaveInfo tempSave(*gameModel->GetSave()); + tempSave.SetGameSave(gameSave); + new ServerSaveActivity(tempSave, true, new SaveUploadedCallback(this)); + } + else + { + SaveInfo tempSave(0, 0, 0, 0, gameModel->GetUser().Username, ""); + tempSave.SetGameSave(gameSave); + new ServerSaveActivity(tempSave, true, new SaveUploadedCallback(this)); + } + } + } + else if(gameModel->GetUser().ID) + { + OpenSaveWindow(); + } + else + { + new ErrorMessage("Error", "You need to login to upload saves."); + } +} + +void GameController::FrameStep() +{ + gameModel->FrameStep(1); + gameModel->SetPaused(true); +} + +void GameController::Vote(int direction) +{ + if(gameModel->GetSave() && gameModel->GetUser().ID && gameModel->GetSave()->GetID() && gameModel->GetSave()->GetVote()==0) + { + try + { + gameModel->SetVote(direction); + } + catch(GameModelException & ex) + { + new ErrorMessage("Error while voting", ex.what()); + } + } +} + +void GameController::ChangeBrush() +{ + gameModel->SetBrush(gameModel->GetBrushID()+1); + BrushChanged(gameModel->GetBrushID(), gameModel->GetBrush()->GetRadius().X, gameModel->GetBrush()->GetRadius().Y); +} + +void GameController::ClearSim() +{ + gameModel->SetSave(NULL); + gameModel->ClearSimulation(); +} + +void GameController::ReloadSim() +{ + if(gameModel->GetSave() && gameModel->GetSave()->GetGameSave()) + { + gameModel->SetSave(gameModel->GetSave()); + } + else if(gameModel->GetSaveFile() && gameModel->GetSaveFile()->GetGameSave()) + { + gameModel->SetSaveFile(gameModel->GetSaveFile()); + } +} + +std::string GameController::ElementResolve(int type) +{ + if(gameModel && gameModel->GetSimulation() && gameModel->GetSimulation()->elements && type >= 0 && type < PT_NUM) + return std::string(gameModel->GetSimulation()->elements[type].Name); + else + return ""; +} + +std::string GameController::WallName(int type) +{ + if(gameModel && gameModel->GetSimulation() && gameModel->GetSimulation()->wtypes && type >= 0 && type < UI_WALLCOUNT) + return std::string(gameModel->GetSimulation()->wtypes[type].name); + else + return ""; +} + +void GameController::NotifyAuthUserChanged(Client * sender) +{ + User newUser = sender->GetAuthUser(); + gameModel->SetUser(newUser); +} + +void GameController::NotifyNewNotification(Client * sender, std::pair notification) +{ + class LinkNotification : public Notification + { + std::string link; + public: + LinkNotification(std::string link_, std::string message) : link(link_), Notification(message) {} + virtual ~LinkNotification() {} + + virtual void Action() + { + OpenURI(link); + } + }; + gameModel->AddNotification(new LinkNotification(notification.second, notification.first)); +} + +void GameController::NotifyUpdateAvailable(Client * sender) +{ + class UpdateConfirmation: public ConfirmDialogueCallback { + public: + GameController * c; + UpdateConfirmation(GameController * c_) { c = c_; } + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + { + c->RunUpdater(); + } + } + virtual ~UpdateConfirmation() { } + }; + + class UpdateNotification : public Notification + { + GameController * c; + public: + UpdateNotification(GameController * c, std::string message) : c(c), Notification(message) {} + virtual ~UpdateNotification() {} + + virtual void Action() + { + std::string currentVersion, newVersion; +#ifdef BETA + currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Beta, Build " MTOS(BUILD_NUM); +#elif defined(SNAPSHOT) + currentVersion = "Snapshot " MTOS(SNAPSHOT_ID); +#else + currentVersion = MTOS(SAVE_VERSION) "." MTOS(MINOR_VERSION) " Stable, Build " MTOS(BUILD_NUM); +#endif + + UpdateInfo info = Client::Ref().GetUpdateInfo(); + if(info.Type == UpdateInfo::Beta) + newVersion = format::NumberToString(info.Major) + " " + format::NumberToString(info.Minor) + " Beta, Build " + format::NumberToString(info.Build); + else if(info.Type == UpdateInfo::Snapshot) + newVersion = "Snapshot " + format::NumberToString(info.Time); + else if(info.Type == UpdateInfo::Stable) + newVersion = format::NumberToString(info.Major) + " " + format::NumberToString(info.Minor) + " Stable, Build " + format::NumberToString(info.Build); + + new ConfirmPrompt("Run Updater", "Are you sure you want to run the updater, please save any changes before updating.\n\nCurrent version:\n " + currentVersion + "\nNew version:\n " + newVersion, new UpdateConfirmation(c)); + } + }; + + switch(sender->GetUpdateInfo().Type) + { + case UpdateInfo::Snapshot: + gameModel->AddNotification(new UpdateNotification(this, std::string("A new snapshot is available - click here to update"))); + break; + case UpdateInfo::Stable: + gameModel->AddNotification(new UpdateNotification(this, std::string("A new version is available - click here to update"))); + break; + case UpdateInfo::Beta: + gameModel->AddNotification(new UpdateNotification(this, std::string("A new beta is available - click here to update"))); + break; + } +} + +void GameController::RemoveNotification(Notification * notification) +{ + gameModel->RemoveNotification(notification); +} + +void GameController::RunUpdater() +{ + Exit(); + new UpdateActivity(); +} diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h new file mode 100644 index 0000000..644d25c --- /dev/null +++ b/src/gui/game/GameController.h @@ -0,0 +1,155 @@ + #ifndef GAMECONTROLLER_H +#define GAMECONTROLLER_H + +#include +#include "GameView.h" +#include "GameModel.h" +#include "simulation/Simulation.h" +#include "gui/interface/Point.h" +#include "gui/search/SearchController.h" +#include "gui/render/RenderController.h" +#include "gui/preview/PreviewController.h" +#include "gui/login/LoginController.h" +#include "gui/tags/TagsController.h" +#include "gui/console/ConsoleController.h" +#include "gui/localbrowser/LocalBrowserController.h" +#include "gui/options/OptionsController.h" +//#include "cat/TPTScriptInterface.h" +#include "cat/LuaScriptInterface.h" +#include "client/ClientListener.h" +#include "RenderPreset.h" +#include "Menu.h" + +using namespace std; + +class DebugInfo; +class Notification; +class GameModel; +class GameView; +class CommandInterface; +class ConsoleController; +class GameController: public ClientListener +{ +private: + //Simulation * sim; + bool firstTick; + int screenshotIndex; + PreviewController * activePreview; + GameView * gameView; + GameModel * gameModel; + SearchController * search; + RenderController * renderOptions; + LoginController * loginWindow; + ConsoleController * console; + TagsController * tagsWindow; + LocalBrowserController * localBrowser; + OptionsController * options; + CommandInterface * commandInterface; + vector debugInfo; +public: + bool HasDone; + class SearchCallback; + class RenderCallback; + class SSaveCallback; + class TagsCallback; + class StampsCallback; + class OptionsCallback; + class SaveOpenCallback; + friend class SaveOpenCallback; + GameController(); + ~GameController(); + GameView * GetView(); + + bool BrushChanged(int brushType, int rx, int ry); + bool MouseMove(int x, int y, int dx, int dy); + bool MouseDown(int x, int y, unsigned button); + bool MouseUp(int x, int y, unsigned button); + bool MouseWheel(int x, int y, int d); + bool KeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + bool KeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + void Tick(); + void Exit(); + + void Install(); + + void HistoryRestore(); + void HistorySnapshot(); + + void AdjustGridSize(int direction); + void InvertAirSim(); + void LoadRenderPreset(int presetNum); + void SetZoomEnabled(bool zoomEnable); + void SetZoomPosition(ui::Point position); + void AdjustBrushSize(int direction, bool logarithmic = false, bool xAxis = false, bool yAxis = false); + void AdjustZoomSize(int direction, bool logarithmic = false); + void ToolClick(int toolSelection, ui::Point point); + void DrawPoints(int toolSelection, queue & pointQueue); + void DrawRect(int toolSelection, ui::Point point1, ui::Point point2); + void DrawLine(int toolSelection, ui::Point point1, ui::Point point2); + void DrawFill(int toolSelection, ui::Point point); + void StampRegion(ui::Point point1, ui::Point point2); + void CopyRegion(ui::Point point1, ui::Point point2); + void CutRegion(ui::Point point1, ui::Point point2); + void Update(); + void SetPaused(bool pauseState); + void SetPaused(); + void SetDecoration(bool decorationState); + void SetDecoration(); + void ShowGravityGrid(); + void SetHudEnable(bool hudState); + void SetActiveMenu(Menu * menu); + std::vector GetMenuList(); + void SetActiveTool(int toolSelection, Tool * tool); + void ActiveToolChanged(int toolSelection, Tool *tool); + void SetActiveColourPreset(int preset); + void SetColour(ui::Colour colour); + void SetToolStrength(float value); + void LoadSaveFile(SaveFile * file); + void LoadSave(SaveInfo * save); + void OpenSearch(); + void OpenLogin(); + void OpenTags(); + void OpenSavePreview(int saveID, int saveDate); + void OpenSavePreview(); + void OpenLocalSaveWindow(bool asCurrent); + void OpenLocalBrowse(); + void OpenOptions(); + void OpenRenderOptions(); + void OpenSaveWindow(); + void SaveAsCurrent(); + void OpenStamps(); + void OpenElementSearch(); + void OpenColourPicker(); + void PlaceSave(ui::Point position); + void ClearSim(); + void ReloadSim(); + void Vote(int direction); + void ChangeBrush(); + void ShowConsole(); + void HideConsole(); + void FrameStep(); + void TranslateSave(ui::Point point); + void TransformSave(matrix2d transform); + ui::Point PointTranslate(ui::Point point); + ui::Point NormaliseBlockCoord(ui::Point point); + std::string ElementResolve(int type); + std::string WallName(int type); + + void ResetAir(); + void ResetSpark(); + void SwitchGravity(); + void SwitchAir(); + void ToggleAHeat(); + + void LoadClipboard(); + void LoadStamp(); + + void RemoveNotification(Notification * notification); + + virtual void NotifyUpdateAvailable(Client * sender); + virtual void NotifyAuthUserChanged(Client * sender); + virtual void NotifyNewNotification(Client * sender, std::pair notification); + void RunUpdater(); +}; + +#endif // GAMECONTROLLER_H diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp new file mode 100644 index 0000000..a72845c --- /dev/null +++ b/src/gui/game/GameModel.cpp @@ -0,0 +1,1151 @@ +#include "gui/interface/Engine.h" +#include "GameModel.h" +#include "GameView.h" +#include "simulation/Simulation.h" +#include "simulation/Air.h" +#include "simulation/Tools.h" +#include "graphics/Renderer.h" +#include "gui/interface/Point.h" +#include "Brush.h" +#include "EllipseBrush.h" +#include "TriangleBrush.h" +#include "BitmapBrush.h" +#include "client/Client.h" +#include "client/GameSave.h" +#include "game/DecorationTool.h" +#include "GameModelException.h" +#include "QuickOptions.h" +#include "Format.h" + +GameModel::GameModel(): + sim(NULL), + ren(NULL), + currentBrush(0), + currentUser(0, ""), + currentSave(NULL), + currentFile(NULL), + colourSelector(false), + clipboard(NULL), + stamp(NULL), + placeSave(NULL), + colour(255, 0, 0, 255), + toolStrength(1.0f), + activeColourPreset(-1), + activeMenu(NULL), + edgeMode(0) +{ + sim = new Simulation(); + ren = new Renderer(ui::Engine::Ref().g, sim); + + activeTools = regularToolset; + + std::fill(decoToolset, decoToolset+3, (Tool*)NULL); + std::fill(regularToolset, regularToolset+3, (Tool*)NULL); + + //Default render prefs + std::vector tempArray; + tempArray.push_back(RENDER_FIRE); + tempArray.push_back(RENDER_EFFE); + tempArray.push_back(RENDER_BASC); + ren->SetRenderMode(tempArray); + tempArray.clear(); + + ren->SetDisplayMode(tempArray); + + ren->SetColourMode(0); + + //Load config into renderer + try + { + ren->SetColourMode(Client::Ref().GetPrefUInteger("Renderer.ColourMode", 0)); + + vector tempArray = Client::Ref().GetPrefUIntegerArray("Renderer.DisplayModes"); + if(tempArray.size()) + { + std::vector displayModes(tempArray.begin(), tempArray.end()); + ren->SetDisplayMode(displayModes); + } + + tempArray = Client::Ref().GetPrefUIntegerArray("Renderer.RenderModes"); + if(tempArray.size()) + { + std::vector renderModes(tempArray.begin(), tempArray.end()); + ren->SetRenderMode(renderModes); + } + + ren->gravityFieldEnabled = Client::Ref().GetPrefBool("Renderer.GravityField", false); + ren->decorations_enable = Client::Ref().GetPrefBool("Renderer.Decorations", true); + } + catch(json::Exception & e) + { + } + + //Load config into simulation + edgeMode = Client::Ref().GetPrefInteger("Simulation.EdgeMode", 0); + sim->SetEdgeMode(edgeMode); + + //Load last user + if(Client::Ref().GetAuthUser().ID) + { + currentUser = Client::Ref().GetAuthUser(); + } + + //Set stamp to first stamp in list + vector stamps = Client::Ref().GetStamps(0, 1); + if(stamps.size()>0) + { + SaveFile * stampFile = Client::Ref().GetStamp(stamps[0]); + if(stampFile && stampFile->GetGameSave()) + stamp = stampFile->GetGameSave(); + } + + BuildMenus(); + + //Set default brush palette + brushList.push_back(new EllipseBrush(ui::Point(4, 4))); + brushList.push_back(new Brush(ui::Point(4, 4))); + brushList.push_back(new TriangleBrush(ui::Point(4, 4))); + + //Load more from brushes folder + std::vector brushFiles = Client::Ref().DirectorySearch(BRUSH_DIR, "", ".ptb"); + for(int i = 0; i < brushFiles.size(); i++) + { + std::vector brushData = Client::Ref().ReadFile(brushFiles[i]); + if(!brushData.size()) + { + std::cout << "Brushes: Skipping " << brushFiles[i] << ". Could not open" << std::endl; + continue; + } + int dimension = std::sqrt((float)brushData.size()); + if(dimension * dimension != brushData.size()) + { + std::cout << "Brushes: Skipping " << brushFiles[i] << ". Invalid bitmap size" << std::endl; + continue; + } + brushList.push_back(new BitmapBrush(brushData, ui::Point(dimension, dimension))); + } + + //Set default decoration colour + unsigned char colourR = min(Client::Ref().GetPrefInteger("Decoration.Red", 200), 255); + unsigned char colourG = min(Client::Ref().GetPrefInteger("Decoration.Green", 100), 255); + unsigned char colourB = min(Client::Ref().GetPrefInteger("Decoration.Blue", 50), 255); + unsigned char colourA = min(Client::Ref().GetPrefInteger("Decoration.Alpha", 255), 255); + + SetColourSelectorColour(ui::Colour(colourR, colourG, colourB, colourA)); + + colourPresets.push_back(ui::Colour(255, 255, 255)); + colourPresets.push_back(ui::Colour(0, 255, 255)); + colourPresets.push_back(ui::Colour(255, 0, 255)); + colourPresets.push_back(ui::Colour(255, 255, 0)); + colourPresets.push_back(ui::Colour(255, 0, 0)); + colourPresets.push_back(ui::Colour(0, 255, 0)); + colourPresets.push_back(ui::Colour(0, 0, 255)); +} + +GameModel::~GameModel() +{ + //Save to config: + Client::Ref().SetPref("Renderer.ColourMode", ren->GetColourMode()); + + std::vector displayModes = ren->GetDisplayMode(); + Client::Ref().SetPref("Renderer.DisplayModes", std::vector(displayModes.begin(), displayModes.end())); + + std::vector renderModes = ren->GetRenderMode(); + Client::Ref().SetPref("Renderer.RenderModes", std::vector(renderModes.begin(), renderModes.end())); + + Client::Ref().SetPref("Renderer.GravityField", (bool)ren->gravityFieldEnabled); + Client::Ref().SetPref("Renderer.Decorations", (bool)ren->decorations_enable); + + Client::Ref().SetPref("Simulation.EdgeMode", sim->edgeMode); + + Client::Ref().SetPref("Decoration.Red", (int)colour.Red); + Client::Ref().SetPref("Decoration.Green", (int)colour.Green); + Client::Ref().SetPref("Decoration.Blue", (int)colour.Blue); + Client::Ref().SetPref("Decoration.Alpha", (int)colour.Alpha); + + for(int i = 0; i < menuList.size(); i++) + { + delete menuList[i]; + } + for(std::vector::iterator iter = extraElementTools.begin(), end = extraElementTools.end(); iter != end; ++iter) + { + delete *iter; + } + for(int i = 0; i < brushList.size(); i++) + { + delete brushList[i]; + } + delete sim; + delete ren; + if(placeSave) + delete placeSave; + if(clipboard) + delete clipboard; + if(stamp) + delete stamp; + if(currentSave) + delete currentSave; + if(currentFile) + delete currentFile; + //if(activeTools) + // delete[] activeTools; +} + +void GameModel::UpdateQuickOptions() +{ + for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) + { + QuickOption * option = *iter; + option->Update(); + } +} + +void GameModel::BuildQuickOptionMenu(GameController * controller) +{ + for(std::vector::iterator iter = quickOptions.begin(), end = quickOptions.end(); iter != end; ++iter) + { + delete *iter; + } + quickOptions.clear(); + + quickOptions.push_back(new SandEffectOption(this)); + quickOptions.push_back(new DrawGravOption(this)); + quickOptions.push_back(new DecorationsOption(this)); + quickOptions.push_back(new NGravityOption(this)); + quickOptions.push_back(new AHeatOption(this)); + quickOptions.push_back(new ConsoleShowOption(this, controller)); + + notifyQuickOptionsChanged(); + UpdateQuickOptions(); +} + +void GameModel::BuildMenus() +{ + char lastMenu = 0; + if(activeMenu) + lastMenu = activeMenu->GetIcon(); + + std::string activeToolIdentifiers[3]; + if(regularToolset[0]) + activeToolIdentifiers[0] = regularToolset[0]->GetIdentifier(); + if(regularToolset[1]) + activeToolIdentifiers[1] = regularToolset[1]->GetIdentifier(); + if(regularToolset[2]) + activeToolIdentifiers[2] = regularToolset[2]->GetIdentifier(); + + //Empty current menus + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) + { + delete *iter; + } + menuList.clear(); + toolList.clear(); + + for(std::vector::iterator iter = extraElementTools.begin(), end = extraElementTools.end(); iter != end; ++iter) + { + delete *iter; + } + extraElementTools.clear(); + elementTools.clear(); + + //Create menus + for(int i = 0; i < SC_TOTAL; i++) + { + menuList.push_back(new Menu((const char)sim->msections[i].icon[0], sim->msections[i].name)); + } + + //Build menus from Simulation elements + for(int i = 0; i < PT_NUM; i++) + { + if(sim->elements[i].Enabled) + { + Tool * tempTool; + if(i == PT_LIGH) + { + tempTool = new Element_LIGH_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); + } + else if(i == PT_TESC) + { + tempTool = new Element_TESC_Tool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); + } + else if(i == PT_STKM || i == PT_FIGH || i == PT_STKM2) + { + tempTool = new PlopTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); + } + else + { + tempTool = new ElementTool(i, sim->elements[i].Name, sim->elements[i].Description, PIXR(sim->elements[i].Colour), PIXG(sim->elements[i].Colour), PIXB(sim->elements[i].Colour), sim->elements[i].Identifier, sim->elements[i].IconGenerator); + } + + if(sim->elements[i].MenuSection < SC_TOTAL && sim->elements[i].MenuVisible) + { + menuList[sim->elements[i].MenuSection]->AddTool(tempTool); + } + else + { + extraElementTools.push_back(tempTool); + } + elementTools.push_back(tempTool); + } + } + + //Build menu for GOL types + for(int i = 0; i < NGOL; i++) + { + Tool * tempTool = new GolTool(i, sim->gmenu[i].name, std::string(sim->gmenu[i].description), PIXR(sim->gmenu[i].colour), PIXG(sim->gmenu[i].colour), PIXB(sim->gmenu[i].colour), "DEFAULT_PT_LIFE_"+std::string(sim->gmenu[i].name)); + menuList[SC_LIFE]->AddTool(tempTool); + } + + //Build other menus from wall data + for(int i = 0; i < UI_WALLCOUNT; i++) + { + Tool * tempTool = new WallTool(i, "", std::string(sim->wtypes[i].descs), PIXR(sim->wtypes[i].colour), PIXG(sim->wtypes[i].colour), PIXB(sim->wtypes[i].colour), "DEFAULT_WL_"+format::NumberToString(i), sim->wtypes[i].textureGen); + menuList[SC_WALL]->AddTool(tempTool); + //sim->wtypes[i] + } + + //Add special sign and prop tools + menuList[SC_TOOL]->AddTool(new SampleTool(this)); + menuList[SC_TOOL]->AddTool(new SignTool()); + menuList[SC_TOOL]->AddTool(new PropertyTool()); + menuList[SC_TOOL]->AddTool(new WindTool(0, "WIND", "Create air movement", 64, 64, 64, "DEFAULT_UI_WIND")); + + //Build menu for simtools + for(int i = 0; i < sim->tools.size(); i++) + { + Tool * tempTool; + tempTool = new Tool(i, sim->tools[i]->Name, sim->tools[i]->Description, PIXR(sim->tools[i]->Colour), PIXG(sim->tools[i]->Colour), PIXB(sim->tools[i]->Colour), sim->tools[i]->Identifier); + menuList[SC_TOOL]->AddTool(tempTool); + } + + //Add decoration tools to menu + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendAdd, "ADD", "Colour blending: Add", 0, 0, 0, "DEFAULT_DECOR_ADD")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendRemove, "SUB", "Colour blending: Subtract", 0, 0, 0, "DEFAULT_DECOR_SUB")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendMultiply, "MUL", "Colour blending: Multiply", 0, 0, 0, "DEFAULT_DECOR_MUL")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendDivide, "DIV", "Colour blending: Divide" , 0, 0, 0, "DEFAULT_DECOR_DIV")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSmudge, "SMDG", "Smudge colour", 0, 0, 0, "DEFAULT_DECOR_SMDG")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::BlendSet, "SET", "Set colour (No blending)", 0, 0, 0, "DEFAULT_DECOR_SET")); + menuList[SC_DECO]->AddTool(new DecorationTool(DecorationTool::Remove, "CLR", "Clear any set decoration", 0, 0, 0, "DEFAULT_DECOR_CLR")); + decoToolset[0] = GetToolFromIdentifier("DEFAULT_DECOR_SET"); + decoToolset[1] = GetToolFromIdentifier("DEFAULT_DECOR_CLR"); + decoToolset[2] = GetToolFromIdentifier("DEFAULT_UI_SAMPLE"); + + //Set default tools + regularToolset[0] = GetToolFromIdentifier("DEFAULT_PT_DUST"); + regularToolset[1] = GetToolFromIdentifier("DEFAULT_PT_NONE"); + regularToolset[2] = GetToolFromIdentifier("DEFAULT_UI_SAMPLE"); + + + if(activeToolIdentifiers[0].length()) + regularToolset[0] = GetToolFromIdentifier(activeToolIdentifiers[0]); + if(activeToolIdentifiers[1].length()) + regularToolset[1] = GetToolFromIdentifier(activeToolIdentifiers[1]); + if(activeToolIdentifiers[2].length()) + regularToolset[2] = GetToolFromIdentifier(activeToolIdentifiers[2]); + + lastTool = activeTools[0]; + + //Set default menu + activeMenu = menuList[SC_POWDERS]; + + if(lastMenu) + { + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) + { + if((*iter)->GetIcon() == lastMenu) + activeMenu = *iter; + } + } + + if(activeMenu) + toolList = activeMenu->GetToolList(); + else + toolList = std::vector(); + + notifyMenuListChanged(); + notifyToolListChanged(); + notifyActiveToolsChanged(); + notifyLastToolChanged(); +} + +Tool * GameModel::GetToolFromIdentifier(std::string identifier) +{ + for(std::vector::iterator iter = menuList.begin(), end = menuList.end(); iter != end; ++iter) + { + std::vector menuTools = (*iter)->GetToolList(); + for(std::vector::iterator titer = menuTools.begin(), tend = menuTools.end(); titer != tend; ++titer) + { + if(identifier == (*titer)->GetIdentifier()) + return *titer; + } + } + return NULL; +} + +void GameModel::SetEdgeMode(int edgeMode) +{ + this->edgeMode = edgeMode; + sim->SetEdgeMode(edgeMode); +} + +int GameModel::GetEdgeMode() +{ + return this->edgeMode; +} + +std::deque GameModel::GetHistory() +{ + return history; +} +void GameModel::SetHistory(std::deque newHistory) +{ + history = newHistory; +} + +void GameModel::SetVote(int direction) +{ + if(currentSave) + { + RequestStatus status = Client::Ref().ExecVote(currentSave->GetID(), direction); + if(status == RequestOkay) + { + currentSave->vote = direction; + notifySaveChanged(); + } + else + { + throw GameModelException("Could not vote: "+Client::Ref().GetLastError()); + } + } +} + +Brush * GameModel::GetBrush() +{ + return brushList[currentBrush]; +} + +int GameModel::GetBrushID() +{ + return currentBrush; +} + +void GameModel::SetBrush(int i) +{ + currentBrush = i%brushList.size(); + notifyBrushChanged(); +} + +void GameModel::AddObserver(GameView * observer){ + observers.push_back(observer); + + observer->NotifySimulationChanged(this); + observer->NotifyRendererChanged(this); + observer->NotifyPausedChanged(this); + observer->NotifySaveChanged(this); + observer->NotifyBrushChanged(this); + observer->NotifyMenuListChanged(this); + observer->NotifyToolListChanged(this); + observer->NotifyUserChanged(this); + observer->NotifyZoomChanged(this); + observer->NotifyColourSelectorVisibilityChanged(this); + observer->NotifyColourSelectorColourChanged(this); + observer->NotifyColourPresetsChanged(this); + observer->NotifyColourActivePresetChanged(this); + observer->NotifyQuickOptionsChanged(this); + observer->NotifyLastToolChanged(this); + UpdateQuickOptions(); +} + +void GameModel::SetToolStrength(float value) +{ + toolStrength = value; +} + +float GameModel::GetToolStrength() +{ + return toolStrength; +} + +void GameModel::SetActiveMenu(Menu * menu) +{ + for(int i = 0; i < menuList.size(); i++) + { + if(menuList[i]==menu) + { + activeMenu = menu; + toolList = menu->GetToolList(); + notifyToolListChanged(); + + if(menu == menuList[SC_DECO]) + { + if(activeTools != decoToolset) + { + activeTools = decoToolset; + notifyActiveToolsChanged(); + } + } + else + { + if(activeTools != regularToolset) + { + activeTools = regularToolset; + notifyActiveToolsChanged(); + } + } + } + } +} + +vector GameModel::GetUnlistedTools() +{ + return extraElementTools; +} + +vector GameModel::GetToolList() +{ + return toolList; +} + +Menu * GameModel::GetActiveMenu() +{ + return activeMenu; +} + +Tool * GameModel::GetElementTool(int elementID) +{ + std::cout << elementID << std::endl; + for(std::vector::iterator iter = elementTools.begin(), end = elementTools.end(); iter != end; ++iter) + { + if((*iter)->GetToolID() == elementID) + return *iter; + } + return NULL; +} + +Tool * GameModel::GetActiveTool(int selection) +{ + return activeTools[selection]; +} + +void GameModel::SetActiveTool(int selection, Tool * tool) +{ + activeTools[selection] = tool; + notifyActiveToolsChanged(); +} + +vector GameModel::GetQuickOptions() +{ + return quickOptions; +} + +vector GameModel::GetMenuList() +{ + return menuList; +} + +SaveInfo * GameModel::GetSave() +{ + return currentSave; +} + +void GameModel::SetSave(SaveInfo * newSave) +{ + if(currentSave != newSave) + { + if(currentSave) + delete currentSave; + if(newSave == NULL) + currentSave = NULL; + else + currentSave = new SaveInfo(*newSave); + } + if(currentFile) + delete currentFile; + currentFile = NULL; + + if(currentSave && currentSave->GetGameSave()) + { + GameSave * saveData = currentSave->GetGameSave(); + SetPaused(saveData->paused | GetPaused()); + sim->gravityMode = saveData->gravityMode; + sim->air->airMode = saveData->airMode; + sim->legacy_enable = saveData->legacyEnable; + sim->water_equal_test = saveData->waterEEnabled; + if(saveData->gravityEnable) + sim->grav->start_grav_async(); + else + sim->grav->stop_grav_async(); + sim->SetEdgeMode(0); + sim->clear_sim(); + ren->ClearAccumulation(); + sim->Load(saveData); + } + notifySaveChanged(); + UpdateQuickOptions(); +} + +SaveFile * GameModel::GetSaveFile() +{ + return currentFile; +} + +void GameModel::SetSaveFile(SaveFile * newSave) +{ + if(currentFile != newSave) + { + if(currentFile) + delete currentFile; + if(newSave == NULL) + currentFile = NULL; + else + currentFile = new SaveFile(*newSave); + } + if (currentSave) + delete currentSave; + currentSave = NULL; + + if(newSave && newSave->GetGameSave()) + { + GameSave * saveData = newSave->GetGameSave(); + SetPaused(saveData->paused | GetPaused()); + sim->gravityMode = saveData->gravityMode; + sim->air->airMode = saveData->airMode; + sim->legacy_enable = saveData->legacyEnable; + sim->water_equal_test = saveData->waterEEnabled; + if(saveData->gravityEnable && !sim->grav->ngrav_enable) + { + sim->grav->start_grav_async(); + } + else if(!saveData->gravityEnable && sim->grav->ngrav_enable) + { + sim->grav->stop_grav_async(); + } + sim->SetEdgeMode(0); + sim->clear_sim(); + ren->ClearAccumulation(); + sim->Load(saveData); + } + + notifySaveChanged(); + UpdateQuickOptions(); +} + +Simulation * GameModel::GetSimulation() +{ + return sim; +} + +Renderer * GameModel::GetRenderer() +{ + return ren; +} + +User GameModel::GetUser() +{ + return currentUser; +} + +Tool * GameModel::GetLastTool() +{ + return lastTool; +} + +void GameModel::SetLastTool(Tool * newTool) +{ + if(lastTool != newTool) + { + lastTool = newTool; + notifyLastToolChanged(); + } +} + +void GameModel::SetZoomEnabled(bool enabled) +{ + ren->zoomEnabled = enabled; + notifyZoomChanged(); +} + +bool GameModel::GetZoomEnabled() +{ + return ren->zoomEnabled; +} + +void GameModel::SetZoomPosition(ui::Point position) +{ + ren->zoomScopePosition = position; + notifyZoomChanged(); +} + +ui::Point GameModel::GetZoomPosition() +{ + return ren->zoomScopePosition; +} + +void GameModel::SetZoomWindowPosition(ui::Point position) +{ + ren->zoomWindowPosition = position; + notifyZoomChanged(); +} + +ui::Point GameModel::GetZoomWindowPosition() +{ + return ren->zoomWindowPosition; +} + +void GameModel::SetZoomSize(int size) +{ + ren->zoomScopeSize = size; + notifyZoomChanged(); +} + +int GameModel::GetZoomSize() +{ + return ren->zoomScopeSize; +} + +void GameModel::SetZoomFactor(int factor) +{ + ren->ZFACTOR = factor; + notifyZoomChanged(); +} + +int GameModel::GetZoomFactor() +{ + return ren->ZFACTOR; +} + +void GameModel::SetActiveColourPreset(int preset) +{ + activeColourPreset = preset; + notifyColourActivePresetChanged(); +} + +int GameModel::GetActiveColourPreset() +{ + return activeColourPreset; +} + +void GameModel::SetPresetColour(ui::Colour colour) +{ + if(activeColourPreset >= 0 && activeColourPreset < colourPresets.size()) + { + colourPresets[activeColourPreset] = colour; + notifyColourPresetsChanged(); + } +} + +std::vector GameModel::GetColourPresets() +{ + return colourPresets; +} + +void GameModel::SetColourSelectorVisibility(bool visibility) +{ + if(colourSelector != visibility) + { + colourSelector = visibility; + notifyColourSelectorVisibilityChanged(); + } +} + +bool GameModel::GetColourSelectorVisibility() +{ + return colourSelector; +} + +void GameModel::SetColourSelectorColour(ui::Colour colour_) +{ + colour = colour_; + notifyColourSelectorColourChanged(); + + vector tools = GetMenuList()[SC_DECO]->GetToolList(); + for(int i = 0; i < tools.size(); i++) + { + ((DecorationTool*)tools[i])->Red = colour.Red; + ((DecorationTool*)tools[i])->Green = colour.Green; + ((DecorationTool*)tools[i])->Blue = colour.Blue; + ((DecorationTool*)tools[i])->Alpha = colour.Alpha; + } +} + +ui::Colour GameModel::GetColourSelectorColour() +{ + return colour; +} + +void GameModel::SetUser(User user) +{ + currentUser = user; + //Client::Ref().SetAuthUser(user); + notifyUserChanged(); +} + +void GameModel::SetPaused(bool pauseState) +{ + sim->sys_pause = pauseState?1:0; + notifyPausedChanged(); +} + +bool GameModel::GetPaused() +{ + return sim->sys_pause?true:false; +} + +void GameModel::SetDecoration(bool decorationState) +{ + ren->decorations_enable = decorationState?1:0; + notifyDecorationChanged(); + UpdateQuickOptions(); + if (decorationState) + SetInfoTip("Decorations Layer: On"); + else + SetInfoTip("Decorations Layer: Off"); +} + +bool GameModel::GetDecoration() +{ + return ren->decorations_enable?true:false; +} + +void GameModel::SetAHeatEnable(bool aHeat) +{ + sim->aheat_enable = aHeat; + UpdateQuickOptions(); + if (aHeat) + SetInfoTip("Ambient Heat: On"); + else + SetInfoTip("Ambient Heat: Off"); +} + +bool GameModel::GetAHeatEnable() +{ + return sim->aheat_enable; +} + +void GameModel::ShowGravityGrid(bool showGrid) +{ + ren->gravityFieldEnabled = showGrid; + if (showGrid) + SetInfoTip("Gravity Grid: On"); + else + SetInfoTip("Gravity Grid: Off"); +} + +bool GameModel::GetGravityGrid() +{ + return ren->gravityFieldEnabled; +} + +void GameModel::FrameStep(int frames) +{ + sim->framerender += frames; +} + +void GameModel::ClearSimulation() +{ + //Load defaults + sim->gravityMode = 0; + sim->air->airMode = 0; + sim->legacy_enable = false; + sim->water_equal_test = false; + sim->SetEdgeMode(edgeMode); + + sim->clear_sim(); + ren->ClearAccumulation(); + + notifySaveChanged(); + UpdateQuickOptions(); +} + +void GameModel::SetStamp(GameSave * save) +{ + if(stamp != save) + { + if(stamp) + delete stamp; + if(save) + stamp = new GameSave(*save); + else + stamp = NULL; + } +} + +void GameModel::SetPlaceSave(GameSave * save) +{ + if(save != placeSave) + { + if(placeSave) + delete placeSave; + if(save) + placeSave = new GameSave(*save); + else + placeSave = NULL; + } + notifyPlaceSaveChanged(); +} + +void GameModel::AddStamp(GameSave * save) +{ + if(stamp) + delete stamp; + stamp = save; + Client::Ref().AddStamp(save); +} + +void GameModel::SetClipboard(GameSave * save) +{ + if(clipboard) + delete clipboard; + clipboard = save; +} + +GameSave * GameModel::GetClipboard() +{ + return clipboard; +} + +GameSave * GameModel::GetPlaceSave() +{ + return placeSave; +} + +GameSave * GameModel::GetStamp() +{ + return stamp; +} + +void GameModel::Log(string message) +{ + consoleLog.push_front(message); + if(consoleLog.size()>100) + consoleLog.pop_back(); + notifyLogChanged(message); +} + +deque GameModel::GetLog() +{ + return consoleLog; +} + +std::vector GameModel::GetNotifications() +{ + return notifications; +} + +void GameModel::AddNotification(Notification * notification) +{ + notifications.push_back(notification); + notifyNotificationsChanged(); +} + +void GameModel::RemoveNotification(Notification * notification) +{ + for(std::vector::iterator iter = notifications.begin(); iter != notifications.end(); ++iter) + { + if(*iter == notification) + { + delete *iter; + notifications.erase(iter); + break; + } + } + notifyNotificationsChanged(); +} + +void GameModel::SetToolTip(std::string text) +{ + toolTip = text; + notifyToolTipChanged(); +} + +void GameModel::SetInfoTip(std::string text) +{ + infoTip = text; + notifyInfoTipChanged(); +} + +std::string GameModel::GetToolTip() +{ + return toolTip; +} + +std::string GameModel::GetInfoTip() +{ + return infoTip; +} + +void GameModel::notifyNotificationsChanged() +{ + for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) + { + (*iter)->NotifyNotificationsChanged(this); + } +} + +void GameModel::notifyColourPresetsChanged() +{ + for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) + { + (*iter)->NotifyColourPresetsChanged(this); + } +} + +void GameModel::notifyColourActivePresetChanged() +{ + for(std::vector::iterator iter = observers.begin(); iter != observers.end(); ++iter) + { + (*iter)->NotifyColourActivePresetChanged(this); + } +} + +void GameModel::notifyColourSelectorColourChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyColourSelectorColourChanged(this); + } +} + +void GameModel::notifyColourSelectorVisibilityChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyColourSelectorVisibilityChanged(this); + } +} + +void GameModel::notifyRendererChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyRendererChanged(this); + } +} + +void GameModel::notifySaveChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifySaveChanged(this); + } +} + +void GameModel::notifySimulationChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifySimulationChanged(this); + } +} + +void GameModel::notifyPausedChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyPausedChanged(this); + } +} + +void GameModel::notifyDecorationChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + //observers[i]->NotifyPausedChanged(this); + } +} + +void GameModel::notifyBrushChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyBrushChanged(this); + } +} + +void GameModel::notifyMenuListChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyMenuListChanged(this); + } +} + +void GameModel::notifyToolListChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyToolListChanged(this); + } +} + +void GameModel::notifyActiveToolsChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyActiveToolsChanged(this); + } +} + +void GameModel::notifyUserChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyUserChanged(this); + } +} + +void GameModel::notifyZoomChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyZoomChanged(this); + } +} + +void GameModel::notifyPlaceSaveChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyPlaceSaveChanged(this); + } +} + +void GameModel::notifyLogChanged(string entry) +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyLogChanged(this, entry); + } +} + +void GameModel::notifyInfoTipChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyInfoTipChanged(this); + } +} + +void GameModel::notifyToolTipChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyToolTipChanged(this); + } +} + +void GameModel::notifyQuickOptionsChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyQuickOptionsChanged(this); + } +} + +void GameModel::notifyLastToolChanged() +{ + for(int i = 0; i < observers.size(); i++) + { + observers[i]->NotifyLastToolChanged(this); + } +} diff --git a/src/gui/game/GameModel.h b/src/gui/game/GameModel.h new file mode 100644 index 0000000..0ade162 --- /dev/null +++ b/src/gui/game/GameModel.h @@ -0,0 +1,206 @@ +#ifndef GAMEMODEL_H +#define GAMEMODEL_H + +#include +#include +#include "client/SaveInfo.h" +#include "simulation/Simulation.h" +#include "gui/interface/Colour.h" +#include "graphics/Renderer.h" +#include "GameView.h" +#include "GameController.h" +#include "Brush.h" +#include "client/User.h" +#include "Notification.h" + +#include "Tool.h" +#include "Menu.h" + +using namespace std; + +class GameView; +class GameController; +class Simulation; +class Renderer; + +class QuickOption; +class ToolSelection +{ +public: + enum + { + ToolPrimary, ToolSecondary, ToolTertiary + }; +}; + +class GameModel +{ +private: + vector notifications; + //int clipboardSize; + //unsigned char * clipboardData; + GameSave * stamp; + GameSave * clipboard; + GameSave * placeSave; + deque consoleLog; + vector observers; + vector toolList; + + //All tools that are associated with elements + vector elementTools; + //Tools that are present in elementTools, but don't have an associated menu and need to be freed manually + vector extraElementTools; + + vector menuList; + vector quickOptions; + Menu * activeMenu; + int currentBrush; + vector brushList; + SaveInfo * currentSave; + SaveFile * currentFile; + Simulation * sim; + Renderer * ren; + Tool * lastTool; + Tool ** activeTools; + Tool * decoToolset[3]; + Tool * regularToolset[3]; + User currentUser; + float toolStrength; + std::deque history; + + int activeColourPreset; + std::vector colourPresets; + bool colourSelector; + ui::Colour colour; + + int edgeMode; + + std::string infoTip; + std::string toolTip; + //bool zoomEnabled; + void notifyRendererChanged(); + void notifySimulationChanged(); + void notifyPausedChanged(); + void notifyDecorationChanged(); + void notifySaveChanged(); + void notifyBrushChanged(); + void notifyMenuListChanged(); + void notifyToolListChanged(); + void notifyActiveToolsChanged(); + void notifyUserChanged(); + void notifyZoomChanged(); + void notifyClipboardChanged(); + void notifyPlaceSaveChanged(); + void notifyColourSelectorColourChanged(); + void notifyColourSelectorVisibilityChanged(); + void notifyColourPresetsChanged(); + void notifyColourActivePresetChanged(); + void notifyNotificationsChanged(); + void notifyLogChanged(string entry); + void notifyInfoTipChanged(); + void notifyToolTipChanged(); + void notifyQuickOptionsChanged(); + void notifyLastToolChanged(); +public: + GameModel(); + ~GameModel(); + + Tool * GetToolFromIdentifier(std::string identifier); + + void SetEdgeMode(int edgeMode); + int GetEdgeMode(); + + void SetActiveColourPreset(int preset); + int GetActiveColourPreset(); + + void SetPresetColour(ui::Colour colour); + + std::vector GetColourPresets(); + + void SetColourSelectorVisibility(bool visibility); + bool GetColourSelectorVisibility(); + + void SetColourSelectorColour(ui::Colour colour); + ui::Colour GetColourSelectorColour(); + + void SetToolTip(std::string text); + void SetInfoTip(std::string text); + std::string GetToolTip(); + std::string GetInfoTip(); + + void BuildMenus(); + void BuildQuickOptionMenu(GameController * controller); + + std::deque GetHistory(); + void SetHistory(std::deque newHistory); + + void UpdateQuickOptions(); + + void SetToolStrength(float value); + float GetToolStrength(); + + Tool * GetLastTool(); + void SetLastTool(Tool * newTool); + + void SetVote(int direction); + SaveInfo * GetSave(); + SaveFile * GetSaveFile(); + Brush * GetBrush(); + void SetSave(SaveInfo * newSave); + void SetSaveFile(SaveFile * newSave); + void AddObserver(GameView * observer); + + //Get an element tool from an element ID + Tool * GetElementTool(int elementID); + + Tool * GetActiveTool(int selection); + void SetActiveTool(int selection, Tool * tool); + + bool GetPaused(); + void SetPaused(bool pauseState); + bool GetDecoration(); + void SetDecoration(bool decorationState); + bool GetAHeatEnable(); + void SetAHeatEnable(bool aHeat); + bool GetGravityGrid(); + void ShowGravityGrid(bool showGrid); + void ClearSimulation(); + vector GetMenuList(); + vector GetUnlistedTools(); + vector GetToolList(); + vector GetQuickOptions(); + void SetActiveMenu(Menu * menu); + Menu * GetActiveMenu(); + void FrameStep(int frames); + User GetUser(); + void SetUser(User user); + void SetBrush(int i); + int GetBrushID(); + Simulation * GetSimulation(); + Renderer * GetRenderer(); + void SetZoomEnabled(bool enabled); + bool GetZoomEnabled(); + void SetZoomSize(int size); + int GetZoomSize(); + void SetZoomFactor(int factor); + int GetZoomFactor(); + void SetZoomPosition(ui::Point position); + ui::Point GetZoomPosition(); + void SetZoomWindowPosition(ui::Point position); + ui::Point GetZoomWindowPosition(); + void SetStamp(GameSave * newStamp); + void AddStamp(GameSave * save); + void SetClipboard(GameSave * save); + void SetPlaceSave(GameSave * save); + void Log(string message); + deque GetLog(); + GameSave * GetClipboard(); + GameSave * GetStamp(); + GameSave * GetPlaceSave(); + + std::vector GetNotifications(); + void AddNotification(Notification * notification); + void RemoveNotification(Notification * notification); +}; + +#endif // GAMEMODEL_H diff --git a/src/gui/game/GameModelException.h b/src/gui/game/GameModelException.h new file mode 100644 index 0000000..c95f7e6 --- /dev/null +++ b/src/gui/game/GameModelException.h @@ -0,0 +1,19 @@ +#ifndef GAMEMODELEXCEPTION_H_ +#define GAMEMODELEXCEPTION_H_ + +#include +#include +using namespace std; + +struct GameModelException: public exception { + string message; +public: + GameModelException(string message_): message(message_) {} + const char * what() const throw() + { + return message.c_str(); + } + ~GameModelException() throw() {}; +}; + +#endif /* GAMEMODELEXCEPTION_H_ */ diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp new file mode 100644 index 0000000..b7db87f --- /dev/null +++ b/src/gui/game/GameView.cpp @@ -0,0 +1,2140 @@ +#include +#include + +#include "Config.h" +#include "Style.h" +#include "GameView.h" +#include "graphics/Graphics.h" +#include "gui/interface/Window.h" +#include "gui/interface/Button.h" +#include "gui/interface/Colour.h" +#include "gui/interface/Keys.h" +#include "gui/interface/Slider.h" +#include "search/Thumbnail.h" +#include "simulation/SaveRenderer.h" +#include "simulation/SimulationData.h" +#include "dialogues/ConfirmPrompt.h" +#include "Format.h" +#include "QuickOption.h" +#include "IntroText.h" + + +class SplitButton; +class SplitButtonAction +{ +public: + virtual void ActionCallbackLeft(ui::Button * sender) {} + virtual void ActionCallbackRight(ui::Button * sender) {} + virtual ~SplitButtonAction() {} +}; +class SplitButton : public ui::Button +{ +private: + bool rightDown; + bool leftDown; + bool showSplit; + int splitPosition; + std::string toolTip2; + SplitButtonAction * splitActionCallback; +public: + SplitButton(ui::Point position, ui::Point size, std::string buttonText, std::string toolTip, std::string toolTip2, int split) : + Button(position, size, buttonText, toolTip), + toolTip2(toolTip2), + splitPosition(split), + splitActionCallback(NULL), + showSplit(true) + { + + } + bool GetShowSplit() { return showSplit; } + void SetShowSplit(bool split) { showSplit = split; } + SplitButtonAction * GetSplitActionCallback() { return splitActionCallback; } + void SetSplitActionCallback(SplitButtonAction * newAction) { splitActionCallback = newAction; } + virtual void OnMouseUnclick(int x, int y, unsigned int button) + { + if(isButtonDown) + { + if(leftDown) + DoLeftAction(); + else if(rightDown) + DoRightAction(); + } + ui::Button::OnMouseUnclick(x, y, button); + + } + virtual void OnMouseMovedInside(int x, int y, int dx, int dy) + { + if(x >= splitPosition || !showSplit) + { + if(toolTip.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip2); + } + } + else if(x < splitPosition) + { + if(toolTip2.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); + } + } + } + virtual void OnMouseEnter(int x, int y) + { + isMouseInside = true; + if(!Enabled) + return; + if(x >= splitPosition || !showSplit) + { + if(toolTip.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip2); + } + } + else if(x < splitPosition) + { + if(toolTip2.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); + } + } + } + virtual void TextPosition() + { + ui::Button::TextPosition(); + textPosition.X += 3; + } + virtual void OnMouseClick(int x, int y, unsigned int button) + { + ui::Button::OnMouseClick(x, y, button); + rightDown = false; + leftDown = false; + if(x >= splitPosition) + rightDown = true; + else if(x < splitPosition) + leftDown = true; + } + void DoRightAction() + { + if(!Enabled) + return; + if(splitActionCallback) + splitActionCallback->ActionCallbackRight(this); + } + void DoLeftAction() + { + if(!Enabled) + return; + if(splitActionCallback) + splitActionCallback->ActionCallbackLeft(this); + } + void Draw(const ui::Point& screenPos) + { + ui::Button::Draw(screenPos); + Graphics * g = ui::Engine::Ref().g; + drawn = true; + + if(showSplit) + g->draw_line(splitPosition+screenPos.X, screenPos.Y+1, splitPosition+screenPos.X, screenPos.Y+Size.Y-2, 180, 180, 180, 255); + } + virtual ~SplitButton() + { + if(splitActionCallback) + delete splitActionCallback; + } +}; + + +GameView::GameView(): + ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)), + pointQueue(queue()), + isMouseDown(false), + ren(NULL), + activeBrush(NULL), + currentMouse(0, 0), + toolIndex(0), + zoomEnabled(false), + zoomCursorFixed(false), + drawPoint1(0, 0), + drawPoint2(0, 0), + drawMode(DrawPoints), + drawModeReset(false), + selectMode(SelectNone), + selectPoint1(0, 0), + selectPoint2(0, 0), + placeSaveThumb(NULL), + mousePosition(0, 0), + lastOffset(0), + drawSnap(false), + toolTip(""), + infoTip(""), + infoTipPresence(0), + buttonTipShow(0), + toolTipPosition(-1, -1), + shiftBehaviour(false), + ctrlBehaviour(false), + altBehaviour(false), + showHud(true), + showDebug(false), + introText(2048), + introTextMessage(introTextData), + wallBrush(false), + doScreenshot(false), + recording(false), + screenshotIndex(0), + recordingIndex(0), + toolTipPresence(0), + currentSaveType(0), + lastLogEntry(0.0f), + lastMenu(NULL) +{ + + int currentX = 1; + //Set up UI + class SearchAction : public ui::ButtonAction + { + GameView * v; + public: + SearchAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + if(v->CtrlBehaviour()) + v->c->OpenLocalBrowse(); + else + v->c->OpenSearch(); + } + }; + + scrollBar = new ui::Button(ui::Point(0,YRES+21), ui::Point(XRES, 2), ""); + scrollBar->Appearance.BackgroundInactive = ui::Colour(255, 255, 255); + scrollBar->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; + scrollBar->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(scrollBar); + + searchButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(17, 15), "", "Find & open a simulation"); //Open + searchButton->SetIcon(IconOpen); + currentX+=18; + searchButton->SetTogglable(false); + searchButton->SetActionCallback(new SearchAction(this)); + AddComponent(searchButton); + + class ReloadAction : public ui::ButtonAction + { + GameView * v; + public: + ReloadAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->ReloadSim(); + } + void AltActionCallback(ui::Button * sender) + { + v->c->OpenSavePreview(); + } + }; + reloadButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(17, 15), "", "Reload the simulation"); + reloadButton->SetIcon(IconReload); + reloadButton->Appearance.Margin.Left+=2; + currentX+=18; + reloadButton->SetActionCallback(new ReloadAction(this)); + AddComponent(reloadButton); + + class SaveSimulationAction : public SplitButtonAction + { + GameView * v; + public: + SaveSimulationAction(GameView * _v) { v = _v; } + void ActionCallbackRight(ui::Button * sender) + { + if(v->CtrlBehaviour()) + v->c->OpenLocalSaveWindow(false); + else + v->c->OpenSaveWindow(); + } + void ActionCallbackLeft(ui::Button * sender) + { + if(v->CtrlBehaviour()) + v->c->OpenLocalSaveWindow(true); + else + v->c->SaveAsCurrent(); + } + }; + saveSimulationButton = new SplitButton(ui::Point(currentX, Size.Y-16), ui::Point(150, 15), "[untitled simulation]", "Save game as current name", "Save game as new name", 19); + saveSimulationButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + saveSimulationButton->SetIcon(IconSave); + currentX+=151; + ((SplitButton*)saveSimulationButton)->SetSplitActionCallback(new SaveSimulationAction(this)); + AddComponent(saveSimulationButton); + + class UpVoteAction : public ui::ButtonAction + { + GameView * v; + public: + UpVoteAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->Vote(1); + } + }; + upVoteButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(15, 15), "", "Like this save"); + upVoteButton->SetIcon(IconVoteUp); + upVoteButton->Appearance.Margin.Top+=2; + upVoteButton->Appearance.Margin.Left+=2; + currentX+=14; + upVoteButton->SetActionCallback(new UpVoteAction(this)); + AddComponent(upVoteButton); + + class DownVoteAction : public ui::ButtonAction + { + GameView * v; + public: + DownVoteAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->Vote(-1); + } + }; + downVoteButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(15, 15), "", "Dislike this save"); + downVoteButton->SetIcon(IconVoteDown); + downVoteButton->Appearance.Margin.Bottom+=2; + downVoteButton->Appearance.Margin.Left+=2; + currentX+=16; + downVoteButton->SetActionCallback(new DownVoteAction(this)); + AddComponent(downVoteButton); + + class TagSimulationAction : public ui::ButtonAction + { + GameView * v; + public: + TagSimulationAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenTags(); + } + }; + tagSimulationButton = new ui::Button(ui::Point(currentX, Size.Y-16), ui::Point(251, 15), "[no tags set]", "Add simulation tags"); + tagSimulationButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + tagSimulationButton->SetIcon(IconTag); + currentX+=252; + tagSimulationButton->SetActionCallback(new TagSimulationAction(this)); + AddComponent(tagSimulationButton); + + class ClearSimAction : public ui::ButtonAction + { + GameView * v; + public: + ClearSimAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->ClearSim(); + } + }; + clearSimButton = new ui::Button(ui::Point(Size.X-159, Size.Y-16), ui::Point(17, 15), "", "Erase everything"); + clearSimButton->SetIcon(IconNew); + clearSimButton->Appearance.Margin.Left+=2; + clearSimButton->SetActionCallback(new ClearSimAction(this)); + AddComponent(clearSimButton); + + class LoginAction : public ui::ButtonAction + { + GameView * v; + public: + LoginAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenLogin(); + } + }; + loginButton = new ui::Button(ui::Point(Size.X-141, Size.Y-16), ui::Point(92, 15), "[sign in]", "Sign into simulation server"); + loginButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + loginButton->SetIcon(IconLogin); + loginButton->SetActionCallback(new LoginAction(this)); + AddComponent(loginButton); + + class SimulationOptionAction : public ui::ButtonAction + { + GameView * v; + public: + SimulationOptionAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenOptions(); + } + }; + simulationOptionButton = new ui::Button(ui::Point(Size.X-48, Size.Y-16), ui::Point(15, 15), "", "Simulation options"); + simulationOptionButton->SetIcon(IconSimulationSettings); + simulationOptionButton->Appearance.Margin.Left+=2; + simulationOptionButton->SetActionCallback(new SimulationOptionAction(this)); + AddComponent(simulationOptionButton); + + class DisplayModeAction : public ui::ButtonAction + { + GameView * v; + public: + DisplayModeAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenRenderOptions(); + } + }; + displayModeButton = new ui::Button(ui::Point(Size.X-32, Size.Y-16), ui::Point(15, 15), "", "Renderer options"); + displayModeButton->SetIcon(IconRenderSettings); + displayModeButton->Appearance.Margin.Left+=2; + displayModeButton->SetActionCallback(new DisplayModeAction(this)); + AddComponent(displayModeButton); + + class PauseAction : public ui::ButtonAction + { + GameView * v; + public: + PauseAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->SetPaused(sender->GetToggleState()); + } + }; + 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->SetTogglable(true); + pauseButton->SetActionCallback(new PauseAction(this)); + AddComponent(pauseButton); + + class ElementSearchAction : public ui::ButtonAction + { + GameView * v; + public: + ElementSearchAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenElementSearch(); + } + }; + ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, YRES+MENUSIZE-32), ui::Point(15, 15), "\xE5", "Search for elements"); + tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2); + tempButton->SetActionCallback(new ElementSearchAction(this)); + AddComponent(tempButton); + + class ColourPickerAction : public ui::ButtonAction + { + GameView * v; + public: + ColourPickerAction(GameView * _v) { v = _v; } + void ActionCallback(ui::Button * sender) + { + v->c->OpenColourPicker(); + } + }; + colourPicker = new ui::Button(ui::Point((XRES/2)-8, YRES+1), ui::Point(16, 16), "", "Pick Colour"); + colourPicker->SetActionCallback(new ColourPickerAction(this)); +} + +GameView::~GameView() +{ + if(!colourPicker->GetParentWindow()) + delete colourPicker; + + for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + if(!button->GetParentWindow()) + { + delete button; + } + + } + + if(placeSaveThumb) + delete placeSaveThumb; +} + +class GameView::MenuAction: public ui::ButtonAction +{ + GameView * v; +public: + Menu * menu; + bool needsClick; + MenuAction(GameView * _v, Menu * menu_) + { + v = _v; + menu = menu_; + if (v->c->GetMenuList()[SC_DECO] == menu) + needsClick = true; + else + needsClick = false; + } + void MouseEnterCallback(ui::Button * sender) + { + if(!needsClick && !ui::Engine::Ref().GetMouseButton()) + v->c->SetActiveMenu(menu); + } + void ActionCallback(ui::Button * sender) + { + if (needsClick) + v->c->SetActiveMenu(menu); + else + MouseEnterCallback(sender); + } +}; + +class GameView::OptionAction: public ui::ButtonAction +{ + QuickOption * option; +public: + OptionAction(QuickOption * _option) { option = _option; } + void ActionCallback(ui::Button * sender) + { + option->Perform(); + } +}; + +class GameView::OptionListener: public QuickOptionListener +{ + ui::Button * button; +public: + OptionListener(ui::Button * _button) { button = _button; } + virtual void OnValueChanged(QuickOption * option) + { + switch(option->GetType()) + { + case QuickOption::Toggle: + button->SetTogglable(true); + button->SetToggleState(option->GetToggle()); + } + } +}; + +class GameView::ToolAction: public ui::ButtonAction +{ + GameView * v; +public: + Tool * tool; + ToolAction(GameView * _v, Tool * tool_) { v = _v; tool = tool_; } + void ActionCallback(ui::Button * sender_) + { + ToolButton *sender = (ToolButton*)sender_; + if(sender->GetSelectionState() >= 0 && sender->GetSelectionState() <= 2) + v->c->SetActiveTool(sender->GetSelectionState(), tool); + } +}; + +void GameView::NotifyQuickOptionsChanged(GameModel * sender) +{ + for(int i = 0; i < quickOptionButtons.size(); i++) + { + RemoveComponent(quickOptionButtons[i]); + delete quickOptionButtons[i]; + } + + int currentY = 1; + vector optionList = sender->GetQuickOptions(); + for(vector::iterator iter = optionList.begin(), end = optionList.end(); iter != end; ++iter) + { + QuickOption * option = *iter; + ui::Button * tempButton = new ui::Button(ui::Point(XRES+BARSIZE-16, currentY), ui::Point(15, 15), option->GetIcon(), option->GetDescription()); + //tempButton->Appearance.Margin = ui::Border(0, 2, 3, 2); + tempButton->SetTogglable(true); + tempButton->SetActionCallback(new OptionAction(option)); + option->AddListener(new OptionListener(tempButton)); + AddComponent(tempButton); + + quickOptionButtons.push_back(tempButton); + currentY += 16; + } +} + +void GameView::NotifyMenuListChanged(GameModel * sender) +{ + int currentY = YRES+MENUSIZE-48;//-(sender->GetMenuList().size()*16); + for(int i = 0; i < menuButtons.size(); i++) + { + RemoveComponent(menuButtons[i]); + delete menuButtons[i]; + } + menuButtons.clear(); + for(int i = 0; i < toolButtons.size(); i++) + { + RemoveComponent(toolButtons[i]); + delete toolButtons[i]; + } + toolButtons.clear(); + vector menuList = sender->GetMenuList(); + for(vector::reverse_iterator iter = menuList.rbegin(), end = menuList.rend(); iter != end; ++iter) + { + std::string tempString = ""; + Menu * item = *iter; + tempString += item->GetIcon(); + 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->SetTogglable(true); + tempButton->SetActionCallback(new MenuAction(this, item)); + currentY-=16; + AddComponent(tempButton); + menuButtons.push_back(tempButton); + } +} + +void GameView::SetSample(SimulationSample sample) +{ + this->sample = sample; +} + +void GameView::SetHudEnable(bool hudState) +{ + showHud = hudState; +} + +ui::Point GameView::GetMousePosition() +{ + return mousePosition; +} + +void GameView::NotifyActiveToolsChanged(GameModel * sender) +{ + for(int i = 0; i < toolButtons.size(); i++) + { + Tool * tool = ((ToolAction*)toolButtons[i]->GetActionCallback())->tool; + if(sender->GetActiveTool(0) == tool) + { + toolButtons[i]->SetSelectionState(0); //Primary + c->ActiveToolChanged(0, tool); + } + else if(sender->GetActiveTool(1) == tool) + { + toolButtons[i]->SetSelectionState(1); //Secondary + c->ActiveToolChanged(1, tool); + } + else if(sender->GetActiveTool(2) == tool) + { + toolButtons[i]->SetSelectionState(2); //Tertiary + c->ActiveToolChanged(2, tool); + } + else + { + toolButtons[i]->SetSelectionState(-1); + } + } +} + +void GameView::NotifyLastToolChanged(GameModel * sender) +{ + if(sender->GetLastTool() && sender->GetLastTool()->GetResolution() == CELL) + { + wallBrush = true; + } + else + { + wallBrush = false; + } +} + +void GameView::NotifyToolListChanged(GameModel * sender) +{ + //int currentY = YRES+MENUSIZE-36; + lastOffset = 0; + int currentX = XRES+BARSIZE-56; + int totalColour; + for(int i = 0; i < menuButtons.size(); i++) + { + if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu()) + { + menuButtons[i]->SetToggleState(true); + } + else + { + menuButtons[i]->SetToggleState(false); + } + } + for(int i = 0; i < toolButtons.size(); i++) + { + RemoveComponent(toolButtons[i]); + delete toolButtons[i]; + } + toolButtons.clear(); + vector toolList = sender->GetToolList(); + for(int i = 0; i < toolList.size(); i++) + { + //ToolButton * tempButton = new ToolButton(ui::Point(XRES+1, currentY), ui::Point(28, 15), toolList[i]->GetName()); + VideoBuffer * tempTexture = toolList[i]->GetTexture(26, 14); + ToolButton * tempButton; + + if(tempTexture) + tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), "", toolList[i]->GetDescription()); + else + tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), toolList[i]->GetName(), toolList[i]->GetDescription()); + + //currentY -= 17; + currentX -= 31; + tempButton->SetActionCallback(new ToolAction(this, toolList[i])); + + tempButton->Appearance.SetTexture(tempTexture); + if(tempTexture) + delete tempTexture; + + tempButton->Appearance.BackgroundInactive = ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue); + + if(sender->GetActiveTool(0) == toolList[i]) + { + tempButton->SetSelectionState(0); //Primary + } + else if(sender->GetActiveTool(1) == toolList[i]) + { + tempButton->SetSelectionState(1); //Secondary + } + else if(sender->GetActiveTool(2) == toolList[i]) + { + tempButton->SetSelectionState(2); //Tertiary + } + + tempButton->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; + tempButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(tempButton); + toolButtons.push_back(tempButton); + } + if (sender->GetActiveMenu() != sender->GetMenuList()[SC_DECO]) + lastMenu = sender->GetActiveMenu(); +} + +void GameView::NotifyColourSelectorVisibilityChanged(GameModel * sender) +{ + for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + RemoveComponent(button); + button->SetParentWindow(NULL); + } + + RemoveComponent(colourPicker); + colourPicker->SetParentWindow(NULL); + + if(sender->GetColourSelectorVisibility()) + { + for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + AddComponent(button); + } + AddComponent(colourPicker); + } +} + +void GameView::NotifyColourPresetsChanged(GameModel * sender) +{ + class ColourPresetAction: public ui::ButtonAction + { + GameView * v; + public: + int preset; + ColourPresetAction(GameView * _v, int preset) : preset(preset) { v = _v; } + void ActionCallback(ui::Button * sender_) + { + ToolButton *sender = (ToolButton*)sender_; + if(sender->GetSelectionState() == 0) + { + v->c->SetActiveColourPreset(preset); + v->c->SetColour(sender->Appearance.BackgroundInactive); + } + else + sender->SetSelectionState(0); + } + }; + + + for(std::vector::iterator iter = colourPresets.begin(), end = colourPresets.end(); iter != end; ++iter) + { + ToolButton * button = *iter; + RemoveComponent(button); + delete button; + } + colourPresets.clear(); + + int currentX = 5; + std::vector colours = sender->GetColourPresets(); + int i = 0; + for(std::vector::iterator iter = colours.begin(), end = colours.end(); iter != end; ++iter) + { + ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(30, 18), ""); + tempButton->Appearance.BackgroundInactive = *iter; + tempButton->SetActionCallback(new ColourPresetAction(this, i)); + + currentX += 31; + + if(sender->GetColourSelectorVisibility()) + AddComponent(tempButton); + colourPresets.push_back(tempButton); + + i++; + } + NotifyColourActivePresetChanged(sender); +} + +void GameView::NotifyColourActivePresetChanged(GameModel * sender) +{ + for(int i = 0; i < colourPresets.size(); i++) + { + if(sender->GetActiveColourPreset() == i) + { + colourPresets[i]->SetSelectionState(0); //Primary + } + else + { + colourPresets[i]->SetSelectionState(-1); + } + } +} + +void GameView::NotifyColourSelectorColourChanged(GameModel * sender) +{ + colourPicker->Appearance.BackgroundInactive = sender->GetColourSelectorColour(); + colourPicker->Appearance.BackgroundHover = sender->GetColourSelectorColour(); +} + +void GameView::NotifyRendererChanged(GameModel * sender) +{ + ren = sender->GetRenderer(); +} + +void GameView::NotifySimulationChanged(GameModel * sender) +{ + +} +void GameView::NotifyUserChanged(GameModel * sender) +{ + if(!sender->GetUser().ID) + { + loginButton->SetText("[sign in]"); + } + else + { + loginButton->SetText(sender->GetUser().Username); + } + NotifySaveChanged(sender); +} + + +void GameView::NotifyPausedChanged(GameModel * sender) +{ + pauseButton->SetToggleState(sender->GetPaused()); +} + +void GameView::NotifyToolTipChanged(GameModel * sender) +{ + toolTip = sender->GetToolTip(); +} + +void GameView::NotifyInfoTipChanged(GameModel * sender) +{ + infoTip = sender->GetInfoTip(); + infoTipPresence = 120; +} + +void GameView::NotifySaveChanged(GameModel * sender) +{ + if(sender->GetSave()) + { + if(introText > 50) + introText = 50; + + saveSimulationButton->SetText(sender->GetSave()->GetName()); + if(sender->GetSave()->GetUserName() == sender->GetUser().Username) + ((SplitButton*)saveSimulationButton)->SetShowSplit(true); + else + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); + reloadButton->Enabled = true; + upVoteButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==0); + if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==1) + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 108, 10, 255)); + else + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + + downVoteButton->Enabled = upVoteButton->Enabled; + if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==-1) + downVoteButton->Appearance.BackgroundDisabled = (ui::Colour(108, 0, 10, 255)); + else + downVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + + if (sender->GetUser().ID) + { + upVoteButton->Appearance.BorderDisabled = upVoteButton->Appearance.BorderInactive; + downVoteButton->Appearance.BorderDisabled = downVoteButton->Appearance.BorderInactive; + } + else + { + upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); + downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); + } + + tagSimulationButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID); + if(sender->GetSave()->GetID()) + { + std::stringstream tagsStream; + std::vector tags = sender->GetSave()->GetTags(); + if(tags.size()) + { + for(int i = 0; i < tags.size(); i++) + { + tagsStream << sender->GetSave()->GetTags()[i]; + if(i < tags.size()-1) + tagsStream << " "; + } + tagSimulationButton->SetText(tagsStream.str()); + } + else + { + tagSimulationButton->SetText("[no tags set]"); + } + } + else + { + tagSimulationButton->SetText("[no tags set]"); + } + currentSaveType = 1; + } + else if (sender->GetSaveFile()) + { + if (ctrlBehaviour) + ((SplitButton*)saveSimulationButton)->SetShowSplit(true); + else + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); + saveSimulationButton->SetText(sender->GetSaveFile()->GetDisplayName()); + reloadButton->Enabled = true; + upVoteButton->Enabled = false; + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); + downVoteButton->Enabled = false; + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100); + tagSimulationButton->Enabled = false; + tagSimulationButton->SetText("[no tags set]"); + currentSaveType = 2; + } + else + { + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); + saveSimulationButton->SetText("[untitled simulation]"); + reloadButton->Enabled = false; + upVoteButton->Enabled = false; + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + upVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100), + downVoteButton->Enabled = false; + upVoteButton->Appearance.BackgroundDisabled = (ui::Colour(0, 0, 0)); + downVoteButton->Appearance.BorderDisabled = ui::Colour(100, 100, 100), + tagSimulationButton->Enabled = false; + tagSimulationButton->SetText("[no tags set]"); + currentSaveType = 0; + } +} + +void GameView::NotifyBrushChanged(GameModel * sender) +{ + activeBrush = sender->GetBrush(); +} + +void GameView::screenshot() +{ + doScreenshot = true; +} + +void GameView::record() +{ + if(recording) + { + recording = false; + } + else + { + class RecordingConfirmation: public ConfirmDialogueCallback { + public: + GameView * v; + RecordingConfirmation(GameView * v): v(v) {} + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + { + v->recording = true; + } + } + virtual ~RecordingConfirmation() { } + }; + new ConfirmPrompt("Recording", "You're about to start recording all drawn frames. This may use a load of hard disk space.", new RecordingConfirmation(this)); + } +} + +void GameView::setToolButtonOffset(int offset) +{ + int offset_ = offset; + offset = offset-lastOffset; + lastOffset = offset_; + + for(vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter!=end; ++iter) + { + ToolButton * button = *iter; + button->Position.X -= offset; + if(button->Position.X <= 0 || (button->Position.X+button->Size.X) > XRES-2) { + button->Visible = false; + } else { + button->Visible = true; + } + } +} + +void GameView::OnMouseMove(int x, int y, int dx, int dy) +{ + mousePosition = c->PointTranslate(ui::Point(x, y)); + if(selectMode!=SelectNone) + { + if(selectMode==PlaceSave) + selectPoint1 = c->PointTranslate(ui::Point(x, y)); + if(selectPoint1.X!=-1) + selectPoint2 = c->PointTranslate(ui::Point(x, y)); + return; + } + currentMouse = ui::Point(x, y); + if(isMouseDown && drawMode == DrawPoints) + { + pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x-dx, y-dy)))); + pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); + } +} + +void GameView::OnMouseDown(int x, int y, unsigned button) +{ + if(altBehaviour && !shiftBehaviour && !ctrlBehaviour) + button = BUTTON_MIDDLE; + if(selectMode!=SelectNone) + { + if(button==BUTTON_LEFT) + { + selectPoint1 = c->PointTranslate(ui::Point(x, y)); + selectPoint2 = selectPoint1; + } + return; + } + if(currentMouse.X >= 0 && currentMouse.X < XRES && currentMouse.Y >= 0 && currentMouse.Y < YRES && !(zoomEnabled && !zoomCursorFixed)) + { + if(button == BUTTON_LEFT) + toolIndex = 0; + if(button == BUTTON_RIGHT) + toolIndex = 1; + if(button == BUTTON_MIDDLE) + toolIndex = 2; + isMouseDown = true; + if(!pointQueue.size()) + c->HistorySnapshot(); + if(drawMode == DrawRect || drawMode == DrawLine) + { + drawPoint1 = c->PointTranslate(ui::Point(x, y)); + } + if(drawMode == DrawPoints) + { + pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); + } + } +} + +void GameView::OnMouseUp(int x, int y, unsigned button) +{ + if(selectMode!=SelectNone) + { + if(button==BUTTON_LEFT) + { + if(selectMode==PlaceSave) + { + if(placeSaveThumb) + { + int thumbX = selectPoint2.X - (placeSaveThumb->Width/2); + int thumbY = selectPoint2.Y - (placeSaveThumb->Height/2); + + if(thumbX<0) + thumbX = 0; + if(thumbX+(placeSaveThumb->Width)>=XRES) + thumbX = XRES-placeSaveThumb->Width; + + if(thumbY<0) + thumbY = 0; + if(thumbY+(placeSaveThumb->Height)>=YRES) + thumbY = YRES-placeSaveThumb->Height; + + c->PlaceSave(ui::Point(thumbX, thumbY)); + } + } + else + { + int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; + int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; + int x1 = (selectPoint2.XCopyRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + else if(selectMode==SelectCut) + c->CutRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + else if(selectMode==SelectStamp) + c->StampRegion(ui::Point(x1, y1), ui::Point(x2, y2)); + } + } + currentMouse = ui::Point(x, y); + selectMode = SelectNone; + return; + } + + if(zoomEnabled && !zoomCursorFixed) + zoomCursorFixed = true; + else + { + if(isMouseDown) + { + isMouseDown = false; + if(drawMode == DrawRect || drawMode == DrawLine) + { + ui::Point finalDrawPoint2(0, 0); + drawPoint2 = c->PointTranslate(ui::Point(x, y)); + finalDrawPoint2 = drawPoint2; + + if(drawSnap && drawMode == DrawLine) + { + finalDrawPoint2 = lineSnapCoords(c->PointTranslate(drawPoint1), drawPoint2); + } + + if(drawSnap && drawMode == DrawRect) + { + finalDrawPoint2 = rectSnapCoords(c->PointTranslate(drawPoint1), drawPoint2); + } + + if(drawMode == DrawRect) + { + c->DrawRect(toolIndex, c->PointTranslate(drawPoint1), finalDrawPoint2); + } + if(drawMode == DrawLine) + { + c->DrawLine(toolIndex, c->PointTranslate(drawPoint1), finalDrawPoint2); + } + } + if(drawMode == DrawPoints) + { + c->ToolClick(toolIndex, c->PointTranslate(ui::Point(x, y))); + //pointQueue.push(ui::Point(x, y)); + } + if(drawModeReset) + { + drawModeReset = false; + drawMode = DrawPoints; + } + } + } +} + +void GameView::ExitPrompt() +{ + class ExitConfirmation: public ConfirmDialogueCallback { + public: + ExitConfirmation() {} + virtual void ConfirmCallback(ConfirmPrompt::DialogueResult result) { + if (result == ConfirmPrompt::ResultOkay) + { + ui::Engine::Ref().Exit(); + } + } + virtual ~ExitConfirmation() { } + }; + new ConfirmPrompt("You are about to quit", "Are you sure you want to exit the game?", new ExitConfirmation()); +} + +void GameView::ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip) +{ + if(sender->Position.Y > Size.Y-17) + { + buttonTip = toolTip; + buttonTipShow = 120; + } + else if(sender->Position.X > Size.X-BARSIZE)// < Size.Y-(quickOptionButtons.size()+1)*16) + { + this->toolTip = toolTip; + toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), sender->Position.Y+3); + if(toolTipPosition.Y+10 > Size.Y-MENUSIZE) + toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), Size.Y-MENUSIZE-10); + toolTipPresence = 120; + } + else + { + this->toolTip = toolTip; + toolTipPosition = ui::Point(Size.X-27-Graphics::textwidth((char*)toolTip.c_str()), Size.Y-MENUSIZE-10); + toolTipPresence = 160; + } +} + +void GameView::OnMouseWheel(int x, int y, int d) +{ + if(!d) + return; + if(selectMode!=SelectNone) + { + return; + } + if(zoomEnabled && !zoomCursorFixed) + { + c->AdjustZoomSize(d); + } + else + { + c->AdjustBrushSize(d, false, shiftBehaviour, ctrlBehaviour); + if(isMouseDown) + { + pointQueue.push(ui::Point(c->PointTranslate(ui::Point(x, y)))); + } + } +} + +void GameView::ToggleDebug() +{ + showDebug = !showDebug; + if (ren) + ren->debugLines = showDebug; +} + +void GameView::BeginStampSelection() +{ + selectMode = SelectStamp; + selectPoint1 = ui::Point(-1, -1); + infoTip = "\x0F\xEF\xEF\x10Select an area to create a stamp"; + infoTipPresence = 120; +} + +void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(introText > 50) + { + introText = 50; + } + + if(selectMode!=SelectNone) + { + if(selectMode==PlaceSave) + { + switch(key) + { + case KEY_RIGHT: + case 'd': + c->TranslateSave(ui::Point(1, 0)); + break; + case KEY_LEFT: + case 'a': + c->TranslateSave(ui::Point(-1, 0)); + break; + case KEY_UP: + case 'w': + c->TranslateSave(ui::Point(0, -1)); + break; + case KEY_DOWN: + case 's': + c->TranslateSave(ui::Point(0, 1)); + break; + case 'r': + if(shift) + { + //Flip + c->TransformSave(m2d_new(-1,0,0,1)); + } + else + { + //Rotate 90deg + c->TransformSave(m2d_new(0,1,-1,0)); + } + break; + } + } + if(key != ' ') + return; + } + switch(key) + { + case KEY_ALT: + drawSnap = true; + enableAltBehaviour(); + break; + case KEY_CTRL: + if(!isMouseDown) + { + if(drawModeReset) + drawModeReset = false; + else + drawPoint1 = currentMouse; + if(shift) + drawMode = DrawFill; + else + drawMode = DrawRect; + } + enableCtrlBehaviour(); + break; + case KEY_SHIFT: + if(!isMouseDown) + { + if(drawModeReset) + drawModeReset = false; + else + drawPoint1 = currentMouse; + if(ctrl) + drawMode = DrawFill; + else + drawMode = DrawLine; + } + enableShiftBehaviour(); + break; + case ' ': //Space + c->SetPaused(); + break; + case KEY_TAB: //Tab + c->ChangeBrush(); + break; + case 'z': + if(ctrl) + { + c->HistoryRestore(); + } + else + { + if (drawMode != DrawLine) + isMouseDown = false; + zoomCursorFixed = false; + c->SetZoomEnabled(true); + } + break; + case '`': + c->ShowConsole(); + break; + case 'p': + screenshot(); + break; + case 'r': + record(); + break; + case 'e': + c->OpenElementSearch(); + break; + case 'f': + c->FrameStep(); + break; + case 'g': + if (ctrl) + c->ShowGravityGrid(); + else if(shift) + c->AdjustGridSize(-1); + else + c->AdjustGridSize(1); + break; + case KEY_F1: + if(!introText) + introText = 8047; + else + introText = 0; + break; + case 'h': + if(ctrl) + { + if(!introText) + introText = 8047; + else + introText = 0; + } + else + showHud = !showHud; + break; + case 'b': + if(ctrl) + c->SetDecoration(); + else + if (colourPicker->GetParentWindow()) + c->SetActiveMenu(lastMenu); + else + { + c->SetDecoration(true); + c->SetPaused(true); + c->SetActiveMenu(c->GetMenuList()[SC_DECO]); + } + break; + case 'y': + c->SwitchAir(); + break; + case KEY_ESCAPE: + case 'q': + ExitPrompt(); + break; + case 'u': + c->ToggleAHeat(); + break; + case '=': + if(ctrl) + c->ResetSpark(); + else + c->ResetAir(); + break; + case 'c': + if(ctrl) + { + selectMode = SelectCopy; + selectPoint1 = ui::Point(-1, -1); + infoTip = "\x0F\xEF\xEF\x10Select an area to copy"; + infoTipPresence = 120; + } + break; + case 'x': + if(ctrl) + { + selectMode = SelectCut; + selectPoint1 = ui::Point(-1, -1); + infoTip = "\x0F\xEF\xEF\x10Select an area to cut"; + infoTipPresence = 120; + } + break; + case 'v': + if(ctrl) + { + c->LoadClipboard(); + selectPoint2 = mousePosition; + selectPoint1 = selectPoint2; + } + break; + case 'l': + c->LoadStamp(); + selectPoint2 = mousePosition; + selectPoint1 = selectPoint2; + isMouseDown = false; + drawMode = DrawPoints; + break; + case 'k': + selectPoint2 = ui::Point(-1, -1); + selectPoint1 = selectPoint2; + c->OpenStamps(); + break; + case ']': + if(zoomEnabled && !zoomCursorFixed) + c->AdjustZoomSize(1, !alt); + else + c->AdjustBrushSize(1, !alt, shiftBehaviour, ctrlBehaviour); + break; + case '[': + if(zoomEnabled && !zoomCursorFixed) + c->AdjustZoomSize(-1, !alt); + else + c->AdjustBrushSize(-1, !alt, shiftBehaviour, ctrlBehaviour); + break; + case 'i': + if(ctrl) + c->Install(); + else + c->InvertAirSim(); + break; + } + + if (shift && showDebug && key == '1') + c->LoadRenderPreset(10); + else if(key >= '0' && key <= '9') + { + c->LoadRenderPreset(key-'0'); + } +} + +void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(ctrl && shift) + drawMode = DrawFill; + else if (ctrl) + drawMode = DrawRect; + else if (shift) + drawMode = DrawLine; + else if(!isMouseDown) + drawMode = DrawPoints; + else + drawModeReset = true; + switch(key) + { + case KEY_ALT: + drawSnap = false; + disableAltBehaviour(); + break; + case KEY_CTRL: + disableCtrlBehaviour(); + break; + case KEY_SHIFT: + disableShiftBehaviour(); + break; + case 'z': + if(!ctrl) + { + if(!zoomCursorFixed && !alt) + c->SetZoomEnabled(false); + } + break; + } +} + +void GameView::OnBlur() +{ + disableAltBehaviour(); + disableCtrlBehaviour(); + disableShiftBehaviour(); + isMouseDown = false; + drawMode = DrawPoints; +} + +void GameView::OnTick(float dt) +{ + if(selectMode==PlaceSave && !placeSaveThumb) + selectMode = SelectNone; + if(zoomEnabled && !zoomCursorFixed) + c->SetZoomPosition(currentMouse); + if(drawMode == DrawPoints) + { + if(isMouseDown) + { + pointQueue.push(ui::Point(c->PointTranslate(currentMouse))); + } + if(!pointQueue.empty()) + { + c->DrawPoints(toolIndex, pointQueue); + } + } + if(drawMode == DrawFill && isMouseDown) + { + c->DrawFill(toolIndex, c->PointTranslate(currentMouse)); + } + if(introText) + { + introText -= int(dt)>0?((int)dt < 5? dt:5):1; + if(introText < 0) + introText = 0; + } + if(infoTipPresence>0) + { + infoTipPresence -= int(dt)>0?int(dt):1; + if(infoTipPresence<0) + infoTipPresence = 0; + } + if(buttonTipShow>0) + { + buttonTipShow -= int(dt)>0?int(dt):1; + if(buttonTipShow<0) + buttonTipShow = 0; + } + if(toolTipPresence>0) + { + toolTipPresence -= int(dt)>0?int(dt):1; + if(toolTipPresence<0) + toolTipPresence = 0; + } + c->Update(); + if(lastLogEntry > -0.1f) + lastLogEntry -= 0.16*dt; +} + + +void GameView::DoMouseMove(int x, int y, int dx, int dy) +{ + if(c->MouseMove(x, y, dx, dy)) + Window::DoMouseMove(x, y, dx, dy); + + if(toolButtons.size()) + { + int totalWidth = (toolButtons[0]->Size.X+1)*toolButtons.size(); + int scrollSize = (int)(((float)(XRES-15))/((float)totalWidth) * ((float)XRES-15)); + if (scrollSize>XRES) + scrollSize = XRES; + if(totalWidth > XRES-15) + { + int mouseX = x; + if(mouseX > XRES) + mouseX = XRES; + + scrollBar->Position.X = (int)(((float)mouseX/((float)XRES-15))*(float)(XRES-scrollSize)); + + float overflow = totalWidth-(XRES-15), mouseLocation = float(XRES)/float(mouseX-(XRES)); + setToolButtonOffset(overflow/mouseLocation); + + //Ensure that mouseLeave events are make their way to the buttons should they move from underneith the mouse pointer + if(toolButtons[0]->Position.Y < y && toolButtons[0]->Position.Y+toolButtons[0]->Size.Y > y) + { + for(vector::iterator iter = toolButtons.begin(), end = toolButtons.end(); iter!=end; ++iter) + { + ToolButton * button = *iter; + if(button->Position.X < x && button->Position.X+button->Size.X > x) + button->OnMouseEnter(x, y); + else + button->OnMouseLeave(x, y); + } + } + } + else + { + scrollBar->Position.X = 0; + } + scrollBar->Size.X=scrollSize; + } +} + +void GameView::DoMouseDown(int x, int y, unsigned button) +{ + if(introText > 50) + introText = 50; + if(c->MouseDown(x, y, button)) + Window::DoMouseDown(x, y, button); +} + +void GameView::DoMouseUp(int x, int y, unsigned button) +{ + if(c->MouseUp(x, y, button)) + Window::DoMouseUp(x, y, button); +} + +void GameView::DoMouseWheel(int x, int y, int d) +{ + if(c->MouseWheel(x, y, d)) + Window::DoMouseWheel(x, y, d); +} + +void GameView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(c->KeyPress(key, character, shift, ctrl, alt)) + Window::DoKeyPress(key, character, shift, ctrl, alt); +} + +void GameView::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if(c->KeyRelease(key, character, shift, ctrl, alt)) + Window::DoKeyRelease(key, character, shift, ctrl, alt); +} + +void GameView::DoDraw() +{ + Window::DoDraw(); + c->Tick(); +} + +void GameView::NotifyNotificationsChanged(GameModel * sender) +{ + class NotificationButtonAction : public ui::ButtonAction + { + GameView * v; + Notification * notification; + public: + NotificationButtonAction(GameView * v, Notification * notification) : v(v), notification(notification) { } + void ActionCallback(ui::Button * sender) + { + notification->Action(); + //v->c->RemoveNotification(notification); + } + }; + class CloseNotificationButtonAction : public ui::ButtonAction + { + GameView * v; + Notification * notification; + public: + CloseNotificationButtonAction(GameView * v, Notification * notification) : v(v), notification(notification) { } + void ActionCallback(ui::Button * sender) + { + v->c->RemoveNotification(notification); + } + void AltActionCallback(ui::Button * sender) + { + v->c->RemoveNotification(notification); + } + }; + + for(std::vector::const_iterator iter = notificationComponents.begin(), end = notificationComponents.end(); iter != end; ++iter) { + ui::Component * cNotification = *iter; + RemoveComponent(cNotification); + delete cNotification; + } + notificationComponents.clear(); + + std::vector notifications = sender->GetNotifications(); + + int currentY = YRES-23; + for(std::vector::iterator iter = notifications.begin(), end = notifications.end(); iter != end; ++iter) + { + int width = (Graphics::textwidth((*iter)->Message.c_str()))+8; + ui::Button * tempButton = new ui::Button(ui::Point(XRES-width-22, currentY), ui::Point(width, 15), (*iter)->Message); + tempButton->SetActionCallback(new NotificationButtonAction(this, *iter)); + tempButton->Appearance.BorderInactive = style::Colour::WarningTitle; + tempButton->Appearance.TextInactive = style::Colour::WarningTitle; + tempButton->Appearance.BorderHover = ui::Colour(255, 175, 0); + tempButton->Appearance.TextHover = ui::Colour(255, 175, 0); + AddComponent(tempButton); + notificationComponents.push_back(tempButton); + + tempButton = new ui::Button(ui::Point(XRES-20, currentY), ui::Point(15, 15)); + tempButton->SetIcon(IconClose); + tempButton->SetActionCallback(new CloseNotificationButtonAction(this, *iter)); + tempButton->Appearance.Margin.Left+=2; + tempButton->Appearance.Margin.Top+=2; + tempButton->Appearance.BorderInactive = style::Colour::WarningTitle; + tempButton->Appearance.TextInactive = style::Colour::WarningTitle; + tempButton->Appearance.BorderHover = ui::Colour(255, 175, 0); + tempButton->Appearance.TextHover = ui::Colour(255, 175, 0); + AddComponent(tempButton); + notificationComponents.push_back(tempButton); + + currentY -= 17; + } +} + +void GameView::NotifyZoomChanged(GameModel * sender) +{ + zoomEnabled = sender->GetZoomEnabled(); +} + +void GameView::NotifyLogChanged(GameModel * sender, string entry) +{ + logEntries.push_front(entry); + lastLogEntry = 100.0f; + if(logEntries.size()>20) + logEntries.pop_back(); +} + +void GameView::NotifyPlaceSaveChanged(GameModel * sender) +{ + if(placeSaveThumb) + delete placeSaveThumb; + if(sender->GetPlaceSave()) + { + placeSaveThumb = SaveRenderer::Ref().Render(sender->GetPlaceSave()); + selectMode = PlaceSave; + selectPoint2 = mousePosition; + } + else + { + placeSaveThumb = NULL; + selectMode = SelectNone; + } +} + +void GameView::enableShiftBehaviour() +{ + if(!shiftBehaviour) + { + shiftBehaviour = true; + if(isMouseDown) + { + c->SetToolStrength(10.0f); + } + } +} + +void GameView::disableShiftBehaviour() +{ + if(shiftBehaviour) + { + shiftBehaviour = false; + if(!ctrlBehaviour) + c->SetToolStrength(1.0f); + else + c->SetToolStrength(.1f); + } +} + +void GameView::enableAltBehaviour() +{ + if(!altBehaviour) + { + altBehaviour = true; + } +} + +void GameView::disableAltBehaviour() +{ + if(altBehaviour) + { + altBehaviour = false; + } +} + +void GameView::enableCtrlBehaviour() +{ + if(!ctrlBehaviour) + { + ctrlBehaviour = true; + + //Show HDD save & load buttons + saveSimulationButton->Appearance.BackgroundInactive = saveSimulationButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); + saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(0, 0, 0); + searchButton->Appearance.BackgroundInactive = searchButton->Appearance.BackgroundHover = ui::Colour(255, 255, 255); + searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(0, 0, 0); + if (currentSaveType == 2) + ((SplitButton*)saveSimulationButton)->SetShowSplit(true); + if(isMouseDown) + { + if(!shiftBehaviour) + c->SetToolStrength(.1f); + else + c->SetToolStrength(10.0f); + } + } +} + +void GameView::disableCtrlBehaviour() +{ + if(ctrlBehaviour) + { + ctrlBehaviour = false; + + //Hide HDD save & load buttons + saveSimulationButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); + saveSimulationButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); + saveSimulationButton->Appearance.TextInactive = saveSimulationButton->Appearance.TextHover = ui::Colour(255, 255, 255); + searchButton->Appearance.BackgroundInactive = ui::Colour(0, 0, 0); + searchButton->Appearance.BackgroundHover = ui::Colour(20, 20, 20); + searchButton->Appearance.TextInactive = searchButton->Appearance.TextHover = ui::Colour(255, 255, 255); + if (currentSaveType == 2) + ((SplitButton*)saveSimulationButton)->SetShowSplit(false); + if(!shiftBehaviour) + c->SetToolStrength(1.0f); + else + c->SetToolStrength(10.0f); + } +} + +void GameView::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + if(ren) + { + ren->clearScreen(1.0f); + ren->RenderBegin(); + ren->SetSample(c->PointTranslate(currentMouse).X, c->PointTranslate(currentMouse).Y); + if(selectMode == SelectNone && (!zoomEnabled || zoomCursorFixed) && activeBrush && currentMouse.X >= 0 && currentMouse.X < XRES && currentMouse.Y >= 0 && currentMouse.Y < YRES) + { + ui::Point finalCurrentMouse = c->PointTranslate(currentMouse); + ui::Point initialDrawPoint = drawPoint1; + + if(wallBrush) + { + finalCurrentMouse = c->NormaliseBlockCoord(finalCurrentMouse); + initialDrawPoint = c->NormaliseBlockCoord(initialDrawPoint); + } + + if(drawMode==DrawRect && isMouseDown) + { + if(drawSnap) + { + finalCurrentMouse = rectSnapCoords(c->PointTranslate(initialDrawPoint), finalCurrentMouse); + } + activeBrush->RenderRect(ren, c->PointTranslate(initialDrawPoint), finalCurrentMouse); + } + else if(drawMode==DrawLine && isMouseDown) + { + if(drawSnap) + { + finalCurrentMouse = lineSnapCoords(c->PointTranslate(initialDrawPoint), finalCurrentMouse); + } + activeBrush->RenderLine(ren, c->PointTranslate(initialDrawPoint), finalCurrentMouse); + } + else if(drawMode==DrawFill)// || altBehaviour) + { + activeBrush->RenderFill(ren, finalCurrentMouse); + } + if(drawMode == DrawPoints || drawMode==DrawLine || (drawMode == DrawRect && !isMouseDown)) + { + if(wallBrush) + { + ui::Point finalBrushRadius = c->NormaliseBlockCoord(activeBrush->GetRadius()); + ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y-finalBrushRadius.Y, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y-finalBrushRadius.Y); + ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-1, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-1); + + ren->xor_line(finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y-finalBrushRadius.Y+1, finalCurrentMouse.X-finalBrushRadius.X, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-2); + ren->xor_line(finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y-finalBrushRadius.Y+1, finalCurrentMouse.X+finalBrushRadius.X+CELL-1, finalCurrentMouse.Y+finalBrushRadius.Y+CELL-2); + } + else + { + activeBrush->RenderPoint(ren, finalCurrentMouse); + } + } + } + + if(selectMode!=SelectNone) + { + if(selectMode==PlaceSave) + { + if(placeSaveThumb && selectPoint2.X!=-1) + { + int thumbX = selectPoint2.X - (placeSaveThumb->Width/2); + int thumbY = selectPoint2.Y - (placeSaveThumb->Height/2); + + ui::Point thumbPos = c->NormaliseBlockCoord(ui::Point(thumbX, thumbY)); + + if(thumbPos.X<0) + thumbPos.X = 0; + if(thumbPos.X+(placeSaveThumb->Width)>=XRES) + thumbPos.X = XRES-placeSaveThumb->Width; + + if(thumbPos.Y<0) + thumbPos.Y = 0; + if(thumbPos.Y+(placeSaveThumb->Height)>=YRES) + thumbPos.Y = YRES-placeSaveThumb->Height; + + ren->draw_image(placeSaveThumb, thumbPos.X, thumbPos.Y, 128); + + ren->xor_rect(thumbPos.X, thumbPos.Y, placeSaveThumb->Width, placeSaveThumb->Width); + } + } + else + { + if(selectPoint1.X==-1) + { + ren->fillrect(0, 0, XRES, YRES, 0, 0, 0, 100); + } + else + { + int x2 = (selectPoint1.X>selectPoint2.X)?selectPoint1.X:selectPoint2.X; + int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; + int x1 = (selectPoint2.XXRES-1) + x2 = XRES-1; + if(y2>YRES-1) + y2 = YRES-1; + + ren->fillrect(0, 0, XRES, y1, 0, 0, 0, 100); + ren->fillrect(0, y2+1, XRES, YRES-y2-1, 0, 0, 0, 100); + + ren->fillrect(0, y1, x1, (y2-y1)+1, 0, 0, 0, 100); + ren->fillrect(x2+1, y1, XRES-x2-1, (y2-y1)+1, 0, 0, 0, 100); + + ren->xor_rect(x1, y1, (x2-x1)+1, (y2-y1)+1); + } + } + } + + ren->RenderEnd(); + + if(doScreenshot) + { + VideoBuffer screenshot(ren->DumpFrame()); + std::vector data = format::VideoBufferToPNG(screenshot); + + std::stringstream filename; + filename << "screenshot_"; + filename << std::setfill('0') << std::setw(6) << (screenshotIndex++); + filename << ".png"; + + Client::Ref().WriteFile(data, filename.str()); + doScreenshot = false; + } + + if(recording) + { + VideoBuffer screenshot(ren->DumpFrame()); + std::vector data = format::VideoBufferToPPM(screenshot); + + std::stringstream filename; + filename << "frame_"; + filename << std::setfill('0') << std::setw(6) << (recordingIndex++); + filename << ".ppm"; + + Client::Ref().WriteFile(data, filename.str()); + } + + int startX = 20; + int startY = YRES-20; + int startAlpha; + if(lastLogEntry>0.1f && logEntries.size()) + { + startAlpha = 2.55f*lastLogEntry; + deque::iterator iter; + for(iter = logEntries.begin(); iter != logEntries.end() && startAlpha>0; iter++) + { + string message = (*iter); + startY -= 13; + g->fillrect(startX-3, startY-3, Graphics::textwidth((char*)message.c_str())+6, 14, 0, 0, 0, 100); + g->drawtext(startX, startY, message.c_str(), 255, 255, 255, startAlpha); + startAlpha-=14; + } + } + } + + if(recording) + { + std::stringstream sampleInfo; + sampleInfo << recordingIndex; + sampleInfo << ". \x8E REC"; + + int textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); + g->fillrect(XRES-20-textWidth, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); + g->drawtext(XRES-16-textWidth, 16, (const char*)sampleInfo.str().c_str(), 255, 50, 20, 255); + } + else if(showHud && !introText) + { + //Draw info about simulation under cursor + int wavelengthGfx = 0; + std::stringstream sampleInfo; + sampleInfo.precision(2); + if(sample.particle.type) + { + if(showDebug) + { + int ctype = sample.particle.ctype; + if (sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) + ctype = sample.particle.tmp; + + if (sample.particle.type == PT_LAVA && ctype > 0 && ctype < PT_NUM) + sampleInfo << "Molten " << c->ElementResolve(ctype); + else if((sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) && ctype > 0 && ctype < PT_NUM) + sampleInfo << c->ElementResolve(sample.particle.type) << " with " << c->ElementResolve(ctype); + else + { + sampleInfo << c->ElementResolve(sample.particle.type); + if(ctype > 0 && ctype < PT_NUM) + sampleInfo << " (" << c->ElementResolve(ctype) << ")"; + else + sampleInfo << " ()"; + } + sampleInfo << ", Temp: " << std::fixed << sample.particle.temp -273.15f; + sampleInfo << ", Life: " << sample.particle.life; + sampleInfo << ", Tmp: " << sample.particle.tmp; + sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; + } + else + { + if (sample.particle.type == PT_LAVA && sample.particle.ctype > 0 && sample.particle.ctype < PT_NUM) + sampleInfo << "Molten " << c->ElementResolve(sample.particle.ctype); + else if((sample.particle.type == PT_PIPE || sample.particle.type == PT_PPIP) && sample.particle.tmp > 0 && sample.particle.tmp < PT_NUM) + sampleInfo << c->ElementResolve(sample.particle.type) << " with " << c->ElementResolve(sample.particle.tmp); + else + sampleInfo << c->ElementResolve(sample.particle.type); + sampleInfo << ", Temp: " << std::fixed << sample.particle.temp -273.15f; + sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; + } + if(sample.particle.type == PT_PHOT) + wavelengthGfx = sample.particle.ctype; + } + else if (sample.WallType) + { + sampleInfo << c->WallName(sample.WallType); + sampleInfo << ", Pressure: " << std::fixed << sample.AirPressure; + } + else + { + sampleInfo << "Empty, Pressure: " << std::fixed << sample.AirPressure; + } + + int textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); + g->fillrect(XRES-20-textWidth, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); + g->drawtext(XRES-16-textWidth, 16, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255*0.75); + +#ifndef OGLI + if(wavelengthGfx) + { + int i, cr, cg, cb, j, h = 3, x = XRES-19-textWidth, y = 10; + int tmp; + g->fillrect(x, y, 30, h, 64, 64, 64, 255); // coords -1 size +1 to work around bug in fillrect - TODO: fix fillrect + for (i = 0; i < 30; i++) + { + if ((wavelengthGfx >> i)&1) + { + // Need a spread of wavelengths to get a smooth spectrum, 5 bits seems to work reasonably well + if (i>2) tmp = 0x1F << (i-2); + else tmp = 0x1F >> (2-i); + + cg = 0; + cb = 0; + cr = 0; + + for (j=0; j<12; j++) + { + cr += (tmp >> (j+18)) & 1; + cb += (tmp >> j) & 1; + } + for (j=0; j<13; j++) + cg += (tmp >> (j+9)) & 1; + + tmp = 624/(cr+cg+cb+1); + cr *= tmp; + cg *= tmp; + cb *= tmp; + for (j=0; jblendpixel(x+29-i,y+j,cr>255?255:cr,cg>255?255:cg,cb>255?255:cb,255); + } + } + } +#endif + + if(showDebug) + { + sampleInfo.str(std::string()); + + if(sample.particle.type) + { + sampleInfo << "#" << sample.ParticleID << ", "; + } + sampleInfo << "X:" << sample.PositionX << " Y:" << sample.PositionY; + if (sample.Gravity) + sampleInfo << " GX: " << sample.GravityVelocityX << " GY: " << sample.GravityVelocityY; + + textWidth = Graphics::textwidth((char*)sampleInfo.str().c_str()); + g->fillrect(XRES-20-textWidth, 26, textWidth+8, 15, 0, 0, 0, 255*0.5); + g->drawtext(XRES-16-textWidth, 30, (const char*)sampleInfo.str().c_str(), 255, 255, 255, 255*0.75); + } + + + //FPS and some version info +#ifndef DEBUG //In debug mode, the Engine will draw FPS and other info instead + std::stringstream fpsInfo; + fpsInfo.precision(2); +#ifdef SNAPSHOT + fpsInfo << "Snapshot " << SNAPSHOT_ID << ", "; +#elif defined(BETA) + fpsInfo << "Beta " << SAVE_VERSION << "." << MINOR_VERSION << "." << BUILD_NUM << ", "; +#endif + fpsInfo << "FPS: " << std::fixed << ui::Engine::Ref().GetFps(); + + if (showDebug) + fpsInfo << " Parts: " << sample.NumParts; + if (ren->GetGridSize()) + fpsInfo << " [GRID: " << ren->GetGridSize() << "]"; + + textWidth = Graphics::textwidth((char*)fpsInfo.str().c_str()); + g->fillrect(12, 12, textWidth+8, 15, 0, 0, 0, 255*0.5); + g->drawtext(16, 16, (const char*)fpsInfo.str().c_str(), 32, 216, 255, 255*0.75); +#endif + } + + //Tooltips + if(infoTipPresence) + { + int infoTipAlpha = (infoTipPresence>50?50:infoTipPresence)*5; + g->drawtext((XRES-Graphics::textwidth((char*)infoTip.c_str()))/2, (YRES/2)-2, (char*)infoTip.c_str(), 255, 255, 255, infoTipAlpha); + } + + if(toolTipPresence && toolTipPosition.X!=-1 && toolTipPosition.Y!=-1 && toolTip.length()) + { + g->drawtext(toolTipPosition.X, toolTipPosition.Y, (char*)toolTip.c_str(), 255, 255, 255, toolTipPresence>51?255:toolTipPresence*5); + } + + if(buttonTipShow > 0) + { + g->drawtext(6, Size.Y-MENUSIZE-10, (char*)buttonTip.c_str(), 255, 255, 255, buttonTipShow>51?255:buttonTipShow*5); + } + + //Introduction text + if(introText) + { + g->fillrect(0, 0, XRES+BARSIZE, YRES+MENUSIZE, 0, 0, 0, introText>51?102:introText*2); + g->drawtext(16, 20, (char*)introTextMessage.c_str(), 255, 255, 255, introText>51?255:introText*5); + } +} + +ui::Point GameView::lineSnapCoords(ui::Point point1, ui::Point point2) +{ + ui::Point newPoint(0, 0); + float snapAngle = floor(atan2((float)point2.Y-point1.Y, point2.X-point1.X)/(M_PI*0.25)+0.5)*M_PI*0.25; + float lineMag = sqrtf(pow((float)(point2.X-point1.X),2)+pow((float)(point2.Y-point1.Y),2)); + newPoint.X = (int)(lineMag*cos(snapAngle)+point1.X+0.5f); + newPoint.Y = (int)(lineMag*sin(snapAngle)+point1.Y+0.5f); + return newPoint; +} + +ui::Point GameView::rectSnapCoords(ui::Point point1, ui::Point point2) +{ + ui::Point newPoint(0, 0); + float snapAngle = floor((atan2((float)point2.Y-point1.Y, point2.X-point1.X)+M_PI*0.25)/(M_PI*0.5)+0.5)*M_PI*0.5 - M_PI*0.25; + float lineMag = sqrtf(pow((float)(point2.X-point1.X),2)+pow((float)(point2.Y-point1.Y),2)); + newPoint.X = (int)(lineMag*cos(snapAngle)+point1.X+0.5f); + newPoint.Y = (int)(lineMag*sin(snapAngle)+point1.Y+0.5f); + return newPoint; +} diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h new file mode 100644 index 0000000..093d80b --- /dev/null +++ b/src/gui/game/GameView.h @@ -0,0 +1,189 @@ +#ifndef GAMEVIEW_H +#define GAMEVIEW_H + +#include +#include +#include +#include +#include "GameController.h" +#include "GameModel.h" +#include "gui/interface/Window.h" +#include "gui/interface/Point.h" +#include "gui/interface/Button.h" +#include "gui/interface/Slider.h" +#include "gui/interface/Textbox.h" +#include "ToolButton.h" +#include "Brush.h" +#include "simulation/Sample.h" + +using namespace std; + +enum DrawMode +{ + DrawPoints, DrawLine, DrawRect, DrawFill +}; + +enum SelectMode +{ + SelectNone, SelectStamp, SelectCopy, SelectCut, PlaceSave +}; + +class GameController; +class GameModel; +class GameView: public ui::Window +{ +private: + DrawMode drawMode; + + bool doScreenshot; + bool recording; + int screenshotIndex; + int recordingIndex; + + bool isMouseDown; + bool zoomEnabled; + bool zoomCursorFixed; + bool drawSnap; + bool shiftBehaviour; + bool ctrlBehaviour; + bool altBehaviour; + bool showHud; + bool showDebug; + bool wallBrush; + int introText; + int buttonTipShow; + std::string buttonTip; + std::string introTextMessage; + int toolIndex; + int currentSaveType; + Menu * lastMenu; + + int infoTipPresence; + std::string toolTip; + ui::Point toolTipPosition; + std::string infoTip; + int toolTipPresence; + + queue pointQueue; + GameController * c; + Renderer * ren; + Brush * activeBrush; + //UI Elements + vector quickOptionButtons; + vector menuButtons; + vector toolButtons; + vector notificationComponents; + deque logEntries; + float lastLogEntry; + ui::Button * scrollBar; + ui::Button * searchButton; + ui::Button * reloadButton; + ui::Button * saveSimulationButton; + ui::Button * downVoteButton; + ui::Button * upVoteButton; + ui::Button * tagSimulationButton; + ui::Button * clearSimButton; + ui::Button * loginButton; + ui::Button * simulationOptionButton; + ui::Button * displayModeButton; + ui::Button * pauseButton; + ui::Point currentMouse; + + ui::Button * colourPicker; + vector colourPresets; + + bool drawModeReset; + ui::Point drawPoint1; + ui::Point drawPoint2; + + SelectMode selectMode; + ui::Point selectPoint1; + ui::Point selectPoint2; + + ui::Point mousePosition; + + VideoBuffer * placeSaveThumb; + + SimulationSample sample; + + int lastOffset; + void setToolButtonOffset(int offset); + virtual ui::Point lineSnapCoords(ui::Point point1, ui::Point point2); + virtual ui::Point rectSnapCoords(ui::Point point1, ui::Point point2); + + void screenshot(); + void record(); + + void enableShiftBehaviour(); + void disableShiftBehaviour(); + void enableCtrlBehaviour(); + void disableCtrlBehaviour(); + void enableAltBehaviour(); + void disableAltBehaviour(); +public: + GameView(); + virtual ~GameView(); + + //Breaks MVC, but any other way is going to be more of a mess. + ui::Point GetMousePosition(); + void SetSample(SimulationSample sample); + void SetHudEnable(bool hudState); + bool CtrlBehaviour(){ return ctrlBehaviour; } + bool ShiftBehaviour(){ return shiftBehaviour; } + void ExitPrompt(); + void ToggleDebug(); + SelectMode GetSelectMode() { return selectMode; } + void BeginStampSelection(); + + void AttachController(GameController * _c){ c = _c; } + void NotifyRendererChanged(GameModel * sender); + void NotifySimulationChanged(GameModel * sender); + void NotifyPausedChanged(GameModel * sender); + void NotifySaveChanged(GameModel * sender); + void NotifyBrushChanged(GameModel * sender); + void NotifyMenuListChanged(GameModel * sender); + void NotifyToolListChanged(GameModel * sender); + void NotifyActiveToolsChanged(GameModel * sender); + void NotifyUserChanged(GameModel * sender); + void NotifyZoomChanged(GameModel * sender); + void NotifyColourSelectorVisibilityChanged(GameModel * sender); + void NotifyColourSelectorColourChanged(GameModel * sender); + void NotifyColourPresetsChanged(GameModel * sender); + void NotifyColourActivePresetChanged(GameModel * sender); + void NotifyPlaceSaveChanged(GameModel * sender); + void NotifyNotificationsChanged(GameModel * sender); + void NotifyLogChanged(GameModel * sender, string entry); + void NotifyToolTipChanged(GameModel * sender); + void NotifyInfoTipChanged(GameModel * sender); + void NotifyQuickOptionsChanged(GameModel * sender); + void NotifyLastToolChanged(GameModel * sender); + + + virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip); + + virtual void OnMouseMove(int x, int y, int dx, int dy); + virtual void OnMouseDown(int x, int y, unsigned button); + virtual void OnMouseUp(int x, int y, unsigned button); + virtual void OnMouseWheel(int x, int y, int d); + virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void OnTick(float dt); + virtual void OnDraw(); + virtual void OnBlur(); + + //Top-level handlers, for Lua interface + virtual void DoDraw(); + virtual void DoMouseMove(int x, int y, int dx, int dy); + virtual void DoMouseDown(int x, int y, unsigned button); + virtual void DoMouseUp(int x, int y, unsigned button); + virtual void DoMouseWheel(int x, int y, int d); + virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + + class MenuAction; + class ToolAction; + class OptionAction; + class OptionListener; +}; + +#endif // GAMEVIEW_H diff --git a/src/gui/game/Menu.h b/src/gui/game/Menu.h new file mode 100644 index 0000000..3f955e1 --- /dev/null +++ b/src/gui/game/Menu.h @@ -0,0 +1,51 @@ +#ifndef MENU_H_ +#define MENU_H_ + +#include "Tool.h" + +class Menu +{ + char icon; + string description; + vector tools; +public: + Menu(char icon_, string description_): + icon(icon_), + description(description_), + tools(vector()) + { + + } + + virtual ~Menu() + { + for(int i = 0; i < tools.size(); i++) + { + delete tools[i]; + } + tools.clear(); + } + + vector GetToolList() + { + return tools; + } + + char GetIcon() + { + return icon; + } + + string GetDescription() + { + return description; + } + + void AddTool(Tool * tool_) + { + tools.push_back(tool_); + } +}; + + +#endif /* MENU_H_ */ diff --git a/src/gui/game/Notification.h b/src/gui/game/Notification.h new file mode 100644 index 0000000..b2bf6f8 --- /dev/null +++ b/src/gui/game/Notification.h @@ -0,0 +1,16 @@ +#ifndef NOTIFICATION_H_ +#define NOTIFICATION_H_ + +#include + +class Notification +{ +public: + Notification(std::string message) : Message(message) {} + virtual ~Notification() {}; + std::string Message; + + virtual void Action() { } +}; + +#endif /* NOTIFICATION_H_ */ diff --git a/src/gui/game/PropertyTool.cpp b/src/gui/game/PropertyTool.cpp new file mode 100644 index 0000000..fda3e90 --- /dev/null +++ b/src/gui/game/PropertyTool.cpp @@ -0,0 +1,235 @@ +#include +#include +#include "Style.h" +#include "simulation/Simulation.h" +#include "Tool.h" +#include "gui/interface/Window.h" +#include "gui/interface/Button.h" +#include "gui/interface/Label.h" +#include "gui/interface/Textbox.h" +#include "gui/interface/DropDown.h" +#include "gui/interface/Keys.h" +#include "dialogues/ErrorMessage.h" + +class PropertyWindow: public ui::Window +{ +public: + ui::DropDown * property; + ui::Textbox * textField; + SignTool * tool; + Simulation * sim; + int signID; + ui::Point position; + std::vector properties; + PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_); + void SetProperty(); + virtual void OnDraw(); + virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + virtual void OnTryExit(ExitMethod method); + virtual ~PropertyWindow() {} + class OkayAction: public ui::ButtonAction + { + public: + PropertyWindow * prompt; + OkayAction(PropertyWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->textField->GetText().length()) + prompt->SetProperty(); + prompt->SelfDestruct(); + return; + } + }; +}; + +PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation * sim_, ui::Point position_): +ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), +sim(sim_), +position(position_) +{ + properties = Particle::GetProperties(); + + ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 14), "Edit property"); + messageLabel->SetTextColour(style::Colour::InformationTitle); + messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignTop; + AddComponent(messageLabel); + + ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-17), ui::Point(Size.X, 17), "OK"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); + okayButton->SetActionCallback(new OkayAction(this)); + AddComponent(okayButton); + SetOkayButton(okayButton); + + class PropertyChanged: public ui::DropDownAction + { + PropertyWindow * w; + public: + PropertyChanged(PropertyWindow * w): w(w) { } + virtual void OptionChanged(ui::DropDown * sender, std::pair option) + { + w->FocusComponent(w->textField); + } + }; + property = new ui::DropDown(ui::Point(8, 25), ui::Point(Size.X-16, 17)); + property->SetActionCallback(new PropertyChanged(this)); + AddComponent(property); + for(int i = 0; i < properties.size(); i++) + { + property->AddOption(std::pair(properties[i].Name, i)); + } + property->SetOption(0); + + textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "", "[value]"); + textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(textField); + FocusComponent(textField); + + ui::Engine::Ref().ShowWindow(this); +} +void PropertyWindow::SetProperty() +{ + if(property->GetOption().second!=-1 && textField->GetText().length() > 0) + { + void * propValue; + int tempInt; + unsigned int tempUInt; + float tempFloat; + std::string value = textField->GetText(); + try { + switch(properties[property->GetOption().second].Type) + { + case StructProperty::Integer: + case StructProperty::ParticleType: + if(value.length() > 2 && value.substr(0, 2) == "0x") + { + //0xC0FFEE + std::stringstream buffer; + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer << std::hex << value.substr(2); + buffer >> tempInt; + } + else if(value.length() > 1 && value[0] == '#') + { + //#C0FFEE + std::stringstream buffer; + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer << std::hex << value.substr(1); + buffer >> tempInt; + } + else + { + if(properties[property->GetOption().second].Type == StructProperty::ParticleType) + { + int type = sim->GetParticleType(value); + if(type != -1) + { +#ifdef DEBUG + std::cout << "Got type from particle name" << std::endl; +#endif + tempInt = type; + } + else + { + std::stringstream buffer(value); + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer >> tempInt; + } + } + else + { + std::stringstream buffer(value); + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer >> tempInt; + } + } +#ifdef DEBUG + std::cout << "Got int value " << tempInt << std::endl; +#endif + propValue = &tempInt; + break; + case StructProperty::UInteger: + if(value.length() > 2 && value.substr(0, 2) == "0x") + { + //0xC0FFEE + std::stringstream buffer; + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer << std::hex << value.substr(2); + buffer >> tempUInt; + } + else if(value.length() > 1 && value[0] == '#') + { + //#C0FFEE + std::stringstream buffer; + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer << std::hex << value.substr(1); + buffer >> tempUInt; + } + else + { + std::stringstream buffer(value); + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer >> tempUInt; + } +#ifdef DEBUG + std::cout << "Got uint value " << tempUInt << std::endl; +#endif + propValue = &tempUInt; + break; + case StructProperty::Float: + { + std::stringstream buffer(value); + buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit); + buffer >> tempFloat; +#ifdef DEBUG + std::cout << "Got float value " << tempFloat << std::endl; +#endif + propValue = &tempFloat; + } + break; + default: + new ErrorMessage("Could not set property", "Invalid property"); + } + sim->flood_prop( + position.X, + position.Y, + properties[property->GetOption().second].Offset, + propValue, + properties[property->GetOption().second].Type + ); + } catch (const std::exception& ex) { + new ErrorMessage("Could not set property", "Invalid value provided"); + } + } +} + +void PropertyWindow::OnTryExit(ExitMethod method) +{ + ui::Engine::Ref().CloseWindow(); + SelfDestruct(); +} + +void PropertyWindow::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +void PropertyWindow::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ + if (key == KEY_UP) + property->SetOption(property->GetOption().second-1); + else if (key == KEY_DOWN) + property->SetOption(property->GetOption().second+1); +} + +void PropertyTool::Click(Simulation * sim, Brush * brush, ui::Point position) +{ + new PropertyWindow(this, sim, position); +} \ No newline at end of file diff --git a/src/gui/game/QuickOption.h b/src/gui/game/QuickOption.h new file mode 100644 index 0000000..4bf27ff --- /dev/null +++ b/src/gui/game/QuickOption.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include + +class GameModel; +class QuickOption; +class QuickOptionListener +{ +protected: + QuickOptionListener() {} +public: + virtual ~QuickOptionListener() {} + virtual void OnValueChanged(QuickOption * sender) {} +}; +class QuickOption +{ +public: + enum Type { + Toggle, Multi + }; +protected: + std::vector listeners; + GameModel * m; + Type type; + std::string icon; + std::string description; + QuickOption(std::string icon, std::string description, GameModel * m, Type type) : + icon(icon), + description(description), + m(m), + type(type) + { + + } + virtual void perform() {} +public: + virtual ~QuickOption() + { + //for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + // delete *iter; + } + + std::vector GetListeners() + { + return listeners; + } + + void AddListener(QuickOptionListener * listener) + { + listeners.push_back(listener); + } + + Type GetType() { return type; } + + virtual bool GetToggle() { return true;} + virtual int GetMutli() { return 0;} + virtual int GetMultiCount() { return 0;} + + std::string GetIcon() { return icon; } + void SetIcon(std::string icon) { this->icon = icon; } + std::string GetDescription() { return description; } + void SetDescription(std::string description) { this->description = description; } + void Perform() + { + perform(); + for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + (*iter)->OnValueChanged(this); + } + void Update() + { + for(std::vector::iterator iter = listeners.begin(), end = listeners.end(); iter != end; ++iter) + (*iter)->OnValueChanged(this); + } +}; + diff --git a/src/gui/game/QuickOptions.h b/src/gui/game/QuickOptions.h new file mode 100644 index 0000000..7c31f9c --- /dev/null +++ b/src/gui/game/QuickOptions.h @@ -0,0 +1,120 @@ +#include "QuickOption.h" +#include "GameModel.h" + +class SandEffectOption: public QuickOption +{ +public: + SandEffectOption(GameModel * m): + QuickOption("P", "Sand effect", m, Toggle) + { + + } + virtual bool GetToggle() + { + return m->GetSimulation()->pretty_powder; + } + virtual void perform() + { + m->GetSimulation()->pretty_powder = !m->GetSimulation()->pretty_powder; + } +}; + +class DrawGravOption: public QuickOption +{ +public: + DrawGravOption(GameModel * m): + QuickOption("G", "Draw gravity field", m, Toggle) + { + + } + virtual bool GetToggle() + { + return m->GetGravityGrid(); + } + virtual void perform() + { + m->ShowGravityGrid(!m->GetGravityGrid()); + } +}; + +class DecorationsOption: public QuickOption +{ +public: + DecorationsOption(GameModel * m): + QuickOption("D", "Draw decorations", m, Toggle) + { + + } + virtual bool GetToggle() + { + return m->GetDecoration(); + } + virtual void perform() + { + m->SetDecoration(!m->GetDecoration()); + } +}; + +class NGravityOption: public QuickOption +{ +public: + NGravityOption(GameModel * m): + QuickOption("N", "Newtonian Gravity", m, Toggle) + { + + } + virtual bool GetToggle() + { + return m->GetSimulation()->grav->ngrav_enable; + } + virtual void perform() + { + if(m->GetSimulation()->grav->ngrav_enable) + { + m->GetSimulation()->grav->stop_grav_async(); + m->SetInfoTip("Newtonian Gravity: Off"); + } + else + { + m->GetSimulation()->grav->start_grav_async(); + m->SetInfoTip("Newtonian Gravity: On"); + } + } +}; + +class AHeatOption: public QuickOption +{ +public: + AHeatOption(GameModel * m): + QuickOption("A", "Ambient heat", m, Toggle) + { + + } + virtual bool GetToggle() + { + return m->GetAHeatEnable(); + } + virtual void perform() + { + m->SetAHeatEnable(!m->GetAHeatEnable()); + } +}; + +class ConsoleShowOption: public QuickOption +{ + GameController * c; +public: + ConsoleShowOption(GameModel * m, GameController * c_): + QuickOption("C", "Show Console", m, Toggle) + { + c = c_; + } + virtual bool GetToggle() + { + return 0; + } + virtual void perform() + { + c->ShowConsole(); + } +}; diff --git a/src/gui/game/RenderPreset.h b/src/gui/game/RenderPreset.h new file mode 100644 index 0000000..9cc9f4c --- /dev/null +++ b/src/gui/game/RenderPreset.h @@ -0,0 +1,19 @@ +#ifndef RENDER_PRESET_H +#define RENDER_PRESET_H +class RenderPreset +{ +public: + std::string Name; + std::vector RenderModes; + std::vector DisplayModes; + unsigned int ColourMode; + + RenderPreset(): Name(""), ColourMode(0) {} + RenderPreset(std::string name, std::vector renderModes, std::vector displayModes, unsigned int colourMode): + Name(name), + RenderModes(renderModes), + DisplayModes(displayModes), + ColourMode(colourMode) + {} +}; +#endif \ No newline at end of file diff --git a/src/gui/game/SampleTool.cpp b/src/gui/game/SampleTool.cpp new file mode 100644 index 0000000..b914b1c --- /dev/null +++ b/src/gui/game/SampleTool.cpp @@ -0,0 +1,67 @@ +#include +#include "graphics/Graphics.h" +#include "Tool.h" +#include "GameModel.h" +#include "gui/interface/Colour.h" + +VideoBuffer * SampleTool::GetIcon(int toolID, int width, int height) +{ + VideoBuffer * newTexture = new VideoBuffer(width, height); + for (int y=0; ySetPixel(x, y, PIXR(pc), PIXG(pc), PIXB(pc), 255); + } + } + newTexture->SetCharacter((width/2)-5, (height/2)-5, 0xE6, 255, 255, 255, 255); + newTexture->BlendCharacter((width/2)-5, (height/2)-5, 0xE7, 100, 180, 255, 255); + return newTexture; +} + +void SampleTool::Draw(Simulation * sim, Brush * brush, ui::Point position) +{ + if(gameModel->GetColourSelectorVisibility()) + { + pixel colour = gameModel->GetRenderer()->sampleColor; + gameModel->SetColourSelectorColour(ui::Colour(PIXR(colour), PIXG(colour), PIXB(colour), 255)); + } + else + { + int particleType = 0; + int particleCtype = 0; + if (sim->photons[position.Y][position.X]) + { + particleType = sim->parts[sim->photons[position.Y][position.X]>>8].type; + particleCtype = sim->parts[sim->pmap[position.Y][position.X]>>8].ctype; + } + else if (sim->pmap[position.Y][position.X]) + { + particleType = sim->parts[sim->pmap[position.Y][position.X]>>8].type; + particleCtype = sim->parts[sim->pmap[position.Y][position.X]>>8].ctype; + } + + if(particleType) + { + if(particleType == PT_LIFE) + { + Menu * lifeMenu = gameModel->GetMenuList()[SC_LIFE]; + std::vector elementTools = lifeMenu->GetToolList(); + + for(std::vector::iterator iter = elementTools.begin(), end = elementTools.end(); iter != end; ++iter) + { + Tool * elementTool = *iter; + if(elementTool && elementTool->GetToolID() == particleCtype) + gameModel->SetActiveTool(0, elementTool); + } + } + else + { + Tool * elementTool = gameModel->GetElementTool(particleType); + if(elementTool) + gameModel->SetActiveTool(0, elementTool); + } + } + } +} diff --git a/src/gui/game/SignTool.cpp b/src/gui/game/SignTool.cpp new file mode 100644 index 0000000..10de6ed --- /dev/null +++ b/src/gui/game/SignTool.cpp @@ -0,0 +1,278 @@ +#include +#include "Style.h" +#include "simulation/Simulation.h" +#include "Tool.h" +#include "gui/interface/Window.h" +#include "gui/interface/Button.h" +#include "gui/interface/Label.h" +#include "gui/interface/Textbox.h" +#include "gui/interface/DropDown.h" + +class SignWindow: public ui::Window +{ +public: + ui::DropDown * justification; + ui::Textbox * textField; + SignTool * tool; + sign * movingSign; + bool signMoving; + Simulation * sim; + int signID; + ui::Point signPosition; + SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_); + virtual void OnDraw(); + virtual void DoDraw(); + virtual void DoMouseMove(int x, int y, int dx, int dy); + virtual void DoMouseDown(int x, int y, unsigned button); + virtual void DoMouseUp(int x, int y, unsigned button) { if(!signMoving) ui::Window::DoMouseUp(x, y, button); } + virtual void DoMouseWheel(int x, int y, int d) { if(!signMoving) ui::Window::DoMouseWheel(x, y, d); } + virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyPress(key, character, shift, ctrl, alt); }; + virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) { if(!signMoving) ui::Window::DoKeyRelease(key, character, shift, ctrl, alt); }; + virtual ~SignWindow() {} + virtual void OnTryExit(ui::Window::ExitMethod method); + class OkayAction: public ui::ButtonAction + { + public: + SignWindow * prompt; + OkayAction(SignWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->signID==-1 && prompt->textField->GetText().length()) + { + prompt->sim->signs.push_back(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); + } + else if(prompt->signID!=-1 && prompt->textField->GetText().length()) + { + prompt->sim->signs[prompt->signID] = sign(sign(prompt->textField->GetText(), prompt->signPosition.X, prompt->signPosition.Y, (sign::Justification)prompt->justification->GetOption().second)); + } + prompt->SelfDestruct(); + } + }; + class DeleteAction: public ui::ButtonAction + { + public: + SignWindow * prompt; + DeleteAction(SignWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) + { + ui::Engine::Ref().CloseWindow(); + if(prompt->signID!=-1) + { + prompt->sim->signs.erase(prompt->sim->signs.begin()+prompt->signID); + } + prompt->SelfDestruct(); + } + }; + + class SignTextAction: public ui::TextboxAction + { + public: + SignWindow * prompt; + SignTextAction(SignWindow * prompt_) { prompt = prompt_; } + virtual void TextChangedCallback(ui::Textbox * sender) + { + if(prompt->signID!=-1) + { + prompt->sim->signs[prompt->signID].text = sender->GetText(); + prompt->sim->signs[prompt->signID].ju = (sign::Justification)prompt->justification->GetOption().second; + } + } + }; + + class MoveAction: public ui::ButtonAction + { + public: + SignWindow * prompt; + MoveAction(SignWindow * prompt_) { prompt = prompt_; } + void ActionCallback(ui::Button * sender) + { + if(prompt->signID!=-1) + { + prompt->movingSign = &prompt->sim->signs[prompt->signID]; + prompt->sim->signs[prompt->signID].ju = (sign::Justification)prompt->justification->GetOption().second; + prompt->signMoving = true; + } + } + }; +}; + +SignWindow::SignWindow(SignTool * tool_, Simulation * sim_, int signID_, ui::Point position_): + ui::Window(ui::Point(-1, -1), ui::Point(200, 87)), + tool(tool_), + signID(signID_), + sim(sim_), + signPosition(position_), + movingSign(NULL), + signMoving(false) +{ + ui::Label * messageLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), "New sign"); + messageLabel->SetTextColour(style::Colour::InformationTitle); + messageLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + messageLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(messageLabel); + + ui::Button * okayButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "OK"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + okayButton->Appearance.BorderInactive = (ui::Colour(200, 200, 200)); + okayButton->SetActionCallback(new OkayAction(this)); + AddComponent(okayButton); + SetOkayButton(okayButton); + + ui::Label * tempLabel = new ui::Label(ui::Point(8, 48), ui::Point(40, 15), "Justify:"); + okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + okayButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(tempLabel); + + justification = new ui::DropDown(ui::Point(52, 48), ui::Point(50, 16)); + AddComponent(justification); + justification->AddOption(std::pair("\x9D Left", (int)sign::Left)); + justification->AddOption(std::pair("\x9E Centre", (int)sign::Centre)); + justification->AddOption(std::pair("\x9F Right", (int)sign::Right)); + justification->SetOption(1); + justification->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + + textField = new ui::Textbox(ui::Point(8, 25), ui::Point(Size.X-16, 17), "", "[message]"); + textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + textField->SetActionCallback(new SignTextAction(this)); + AddComponent(textField); + FocusComponent(textField); + + if(signID!=-1) + { + messageLabel->SetText("Edit sign"); + + textField->SetText(sim->signs[signID].text); + justification->SetOption(sim->signs[signID].ju); + + ui::Point position = ui::Point(justification->Position.X+justification->Size.X+3, 48); + ui::Button * moveButton = new ui::Button(position, ui::Point(((Size.X-position.X-8)/2)-2, 16), "Move"); + moveButton->SetActionCallback(new MoveAction(this)); + AddComponent(moveButton); + + position = ui::Point(justification->Position.X+justification->Size.X+3, 48)+ui::Point(moveButton->Size.X+3, 0); + ui::Button * deleteButton = new ui::Button(position, ui::Point((Size.X-position.X-8)-1, 16), "Delete"); + //deleteButton->SetIcon(IconDelete); + deleteButton->SetActionCallback(new DeleteAction(this)); + + signPosition.X = sim->signs[signID].x; + signPosition.Y = sim->signs[signID].y; + + AddComponent(deleteButton); + } + + ui::Engine::Ref().ShowWindow(this); +} + +void SignWindow::OnTryExit(ui::Window::ExitMethod method) +{ + ui::Engine::Ref().CloseWindow(); + SelfDestruct(); +} + +void SignWindow::DoDraw() +{ + for(std::vector::iterator iter = sim->signs.begin(), end = sim->signs.end(); iter != end; ++iter) + { + sign & currentSign = *iter; + int x, y, w, h, dx, dy; + Graphics * g = ui::Engine::Ref().g; + std::string text = currentSign.getText(sim); + currentSign.pos(text, x, y, w, h); + g->clearrect(x, y, w+1, h); + g->drawrect(x, y, w+1, h, 192, 192, 192, 255); + if (sregexp(currentSign.text.c_str(), "^{[c|t]:[0-9]*|.*}$")) + g->drawtext(x+3, y+3, text, 255, 255, 255, 255); + else + g->drawtext(x+3, y+3, text, 0, 191, 255, 255); + + x = currentSign.x; + y = currentSign.y; + dx = 1 - currentSign.ju; + dy = (currentSign.y > 18) ? -1 : 1; +#ifdef OGLR + glBegin(GL_LINES); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + glVertex2i(x, y); + glVertex2i(x+(dx*4), y+(dy*4)); + glEnd(); +#else + for (int j=0; j<4; j++) + { + g->blendpixel(x, y, 192, 192, 192, 255); + x+=dx; + y+=dy; + } +#endif + } + if(!signMoving) + { + ui::Window::DoDraw(); + } +} + +void SignWindow::DoMouseMove(int x, int y, int dx, int dy) { + if(!signMoving) + ui::Window::DoMouseMove(x, y, dx, dy); + else + { + if(x < XRES && y < YRES) + { + movingSign->x = x; + movingSign->y = y; + signPosition.X = x; + signPosition.Y = y; + } + } +} + +void SignWindow::DoMouseDown(int x, int y, unsigned button) +{ + if(!signMoving) + ui::Window::DoMouseDown(x, y, button); + else + { + signMoving = false; + } +} + +void SignWindow::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255); +} + +VideoBuffer * SignTool::GetIcon(int toolID, int width, int height) +{ + VideoBuffer * newTexture = new VideoBuffer(width, height); + for (int y=0; ySetPixel(x, y, PIXR(pc), PIXG(pc), PIXB(pc), 255); + } + } + newTexture->SetCharacter((width/2)-5, (height/2)-5, 0xA1, 32, 64, 128, 255); + newTexture->BlendCharacter((width/2)-5, (height/2)-5, 0xA0, 255, 255, 255, 255); + return newTexture; +} + +void SignTool::Click(Simulation * sim, Brush * brush, ui::Point position) +{ + int signX, signY, signW, signH, signIndex = -1; + for(int i = 0; i < sim->signs.size(); i++){ + sim->signs[i].pos(sim->signs[i].getText(sim), signX, signY, signW, signH); + if(position.X > signX && position.X < signX+signW && position.Y > signY && position.Y < signY+signH) + { + signIndex = i; + break; + } + } + if (signIndex != -1 || sim->signs.size() < MAXSIGNS) + new SignWindow(this, sim, signIndex, position); +} diff --git a/src/gui/game/Tool.cpp b/src/gui/game/Tool.cpp new file mode 100644 index 0000000..42f9b59 --- /dev/null +++ b/src/gui/game/Tool.cpp @@ -0,0 +1,215 @@ +#include +#include "Tool.h" +#include "game/Brush.h" + +#include "simulation/Simulation.h" + +using namespace std; + +Tool::Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + toolID(id), + toolName(name), + toolDescription(description), + colRed(r), + colGreen(g), + colBlue(b), + textureGen(textureGen), + strength(1.0f), + resolution(1), + identifier(identifier) +{ +} + +VideoBuffer * Tool::GetTexture(int width, int height) +{ + if(textureGen) + { + return textureGen(toolID, width, height); + } + return NULL; +} +void Tool::SetTextureGen(VideoBuffer * (*textureGen)(int, int, int)) +{ + this->textureGen = textureGen; +} +std::string Tool::GetIdentifier() { return identifier; } +string Tool::GetName() { return toolName; } +string Tool::GetDescription() { return toolDescription; } +Tool::~Tool() {} + +void Tool::Click(Simulation * sim, Brush * brush, ui::Point position) { } +void Tool::Draw(Simulation * sim, Brush * brush, ui::Point position) { + sim->ToolBrush(position.X, position.Y, toolID, brush, strength); +} +void Tool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + sim->ToolLine(position1.X, position1.Y, position2.X, position2.Y, toolID, brush, strength); +} +void Tool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + sim->ToolBox(position1.X, position1.Y, position2.X, position2.Y, toolID, brush, strength); +} +void Tool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) {}; + + +ElementTool::ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) +{ +} +ElementTool::~ElementTool() {} +void ElementTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ + sim->CreateParts(position.X, position.Y, toolID, brush); +} +void ElementTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, toolID, brush); +} +void ElementTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, toolID, 0); +} +void ElementTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { + sim->FloodParts(position.X, position.Y, toolID, -1, -1, 0); +} + + +WallTool::WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): +Tool(id, name, description, r, g, b, identifier, textureGen) +{ + resolution = CELL; +} +WallTool::~WallTool() {} +void WallTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ + sim->CreateWalls(position.X, position.Y, 1, 1, toolID, 0, brush); +} +void WallTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + int wallX = position1.X/CELL; + int wallY = position1.Y/CELL; + if(dragging == false && toolID == WL_FAN && sim->bmap[wallY][wallX]==WL_FAN) + { + float newFanVelX = (position2.X-position1.X)*0.005f; + newFanVelX *= strength; + float newFanVelY = (position2.Y-position1.Y)*0.005f; + newFanVelY *= strength; + sim->FloodWalls(position1.X, position1.Y, WL_FLOODHELPER, -1, WL_FAN, 0); + for (int j = 0; j < YRES/CELL; j++) + for (int i = 0; i < XRES/CELL; i++) + if (sim->bmap[j][i] == WL_FLOODHELPER) + { + sim->fvx[j][i] = newFanVelX; + sim->fvy[j][i] = newFanVelY; + sim->bmap[j][i] = WL_FAN; + } + } + else + { + sim->CreateWallLine(position1.X, position1.Y, position2.X, position2.Y, 1, 1, toolID, 0, brush); + } +} +void WallTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + sim->CreateWallBox(position1.X, position1.Y, position2.X, position2.Y, toolID, 0); +} +void WallTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { + if (toolID != WL_STREAM) + sim->FloodWalls(position.X, position.Y, toolID, -1, -1, 0); +} + + +GolTool::GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) +{ +} +GolTool::~GolTool() {} +void GolTool::Draw(Simulation * sim, Brush * brush, ui::Point position){ + sim->CreateParts(position.X, position.Y, PT_LIFE|(toolID<<8), brush); +} +void GolTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, PT_LIFE|(toolID<<8), brush); +} +void GolTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, PT_LIFE|(toolID<<8), 0); +} +void GolTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { + sim->FloodParts(position.X, position.Y, PT_LIFE|(toolID<<8), -1, -1, 0); +} + + +WindTool::WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + Tool(id, name, description, r, g, b, identifier, textureGen) +{ +} +WindTool::~WindTool() {} +void WindTool::Draw(Simulation * sim, Brush * brush, ui::Point position) +{ + +} +void WindTool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) +{ + int radiusX, radiusY, sizeX, sizeY; + + float strength = dragging?0.01f:0.002f; + strength *= this->strength; + + radiusX = brush->GetRadius().X; + radiusY = brush->GetRadius().Y; + + sizeX = brush->GetSize().X; + sizeY = brush->GetSize().Y; + + unsigned char *bitmap = brush->GetBitmap(); + + for(int y = 0; y < sizeY; y++) + { + for(int x = 0; x < sizeX; x++) + { + if(bitmap[(y*sizeX)+x] && (position1.X+(x-radiusX) >= 0 && position1.Y+(y-radiusY) >= 0 && position1.X+(x-radiusX) < XRES && position1.Y+(y-radiusY) < YRES)) + { + sim->vx[(position1.Y+(y-radiusY))/CELL][(position1.X+(x-radiusX))/CELL] += (position2.X-position1.X)*strength; + sim->vy[(position1.Y+(y-radiusY))/CELL][(position1.X+(x-radiusX))/CELL] += (position2.Y-position1.Y)*strength; + } + } + } +} +void WindTool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) {} +void WindTool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) {} + + +void Element_LIGH_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position) +{ + if(sim->currentTick >= nextUse) + { + int p = sim->create_part(-2, position.X, position.Y, toolID); + if (p != -1) + { + sim->parts[p].life = brush->GetRadius().X+brush->GetRadius().Y; + if (sim->parts[p].life > 55) + sim->parts[p].life = 55; + sim->parts[p].temp = sim->parts[p].life*150; // temperature of the lighting shows the power of the lighting + nextUse = sim->currentTick+sim->parts[p].life/4; + } + } +} + + +Element_TESC_Tool::Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int)): + ElementTool(id, name, description, r, g, b, identifier, textureGen) + { + } +void Element_TESC_Tool::Draw(Simulation * sim, Brush * brush, ui::Point position){ + int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; + sim->CreateParts(position.X, position.Y, toolID | (radiusInfo << 8), brush); +} +void Element_TESC_Tool::DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging) { + int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; + sim->CreateLine(position1.X, position1.Y, position2.X, position2.Y, toolID | (radiusInfo << 8), brush); +} +void Element_TESC_Tool::DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { + int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; + sim->CreateBox(position1.X, position1.Y, position2.X, position2.Y, toolID | (radiusInfo << 8), 0); +} +void Element_TESC_Tool::DrawFill(Simulation * sim, Brush * brush, ui::Point position) { + int radiusInfo = brush->GetRadius().X*4+brush->GetRadius().Y*4+7; + sim->FloodParts(position.X, position.Y, toolID | (radiusInfo << 8), -1, -1, 0); +} + + +void PlopTool::Click(Simulation * sim, Brush * brush, ui::Point position) +{ + sim->create_part(-1, position.X, position.Y, toolID); +} \ No newline at end of file diff --git a/src/gui/game/Tool.h b/src/gui/game/Tool.h new file mode 100644 index 0000000..ea38fbc --- /dev/null +++ b/src/gui/game/Tool.h @@ -0,0 +1,183 @@ +#ifndef TOOL_H_ +#define TOOL_H_ + +#include + +using namespace std; + +#include "gui/interface/Point.h" + +class Simulation; +class Brush; +class VideoBuffer; + +class Tool +{ +protected: + VideoBuffer * (*textureGen)(int, int, int); + int toolID; + string toolName; + string toolDescription; + float strength; + int resolution; + std::string identifier; +public: + Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + int GetToolID() { return toolID; } + string GetName(); + string GetDescription(); + std::string GetIdentifier(); + int GetResolution() { return resolution; } + void SetStrength(float value) { strength = value; } + float GetStrength() { return strength; } + VideoBuffer * GetTexture(int width, int height); + void SetTextureGen(VideoBuffer * (*textureGen)(int, int, int)); + virtual ~Tool(); + virtual void Click(Simulation * sim, Brush * brush, ui::Point position); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); + int colRed, colBlue, colGreen; +}; + +class SignTool: public Tool +{ +public: + SignTool(): + Tool(0, "SIGN", "Sign. Click a sign to edit or anywhere else to create a new one", 0, 0, 0, "DEFAULT_UI_SIGN", SignTool::GetIcon) + { + } + static VideoBuffer * GetIcon(int toolID, int width, int height); + virtual ~SignTool() {} + virtual void Click(Simulation * sim, Brush * brush, ui::Point position); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) { } + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } +}; + +class GameModel; + +class SampleTool: public Tool +{ + GameModel * gameModel; +public: + SampleTool(GameModel * model): + Tool(0, "SMPL", "Sample an element on the screen", 0, 0, 0, "DEFAULT_UI_SAMPLE", SampleTool::GetIcon), + gameModel(model) + { + } + static VideoBuffer * GetIcon(int toolID, int width, int height); + virtual ~SampleTool() {} + virtual void Click(Simulation * sim, Brush * brush, ui::Point position) { } + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } +}; + +class PropertyTool: public Tool +{ +public: + PropertyTool(): + Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL) + { + } + virtual ~PropertyTool() {} + virtual void Click(Simulation * sim, Brush * brush, ui::Point position); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {}; + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } +}; + +class Element_LIGH_Tool: public Tool +{ + int nextUse; +public: + Element_LIGH_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): + Tool(id, name, description, r, g, b, identifier, textureGen), + nextUse(0) + { + } + virtual ~Element_LIGH_Tool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void Click(Simulation * sim, Brush * brush, ui::Point position) { } + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } +}; + + +class ElementTool: public Tool +{ +public: + ElementTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + virtual ~ElementTool(); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); +}; + +class Element_TESC_Tool: public ElementTool +{ +public: + Element_TESC_Tool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + virtual ~Element_TESC_Tool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); +}; + +class PlopTool: public ElementTool +{ +public: + PlopTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL): + ElementTool(id, name, description, r, g, b, identifier, textureGen) + { + } + virtual ~PlopTool() {} + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position) {} + virtual void Click(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false) { } + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2) { } + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position) { } +}; + +class WallTool: public Tool +{ +public: + WallTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + virtual ~WallTool(); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); +}; + +class GolTool: public Tool +{ +public: + GolTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + virtual ~GolTool(); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); +}; + +class WindTool: public Tool +{ +public: + WindTool(int id, string name, string description, int r, int g, int b, std::string identifier, VideoBuffer * (*textureGen)(int, int, int) = NULL); + virtual ~WindTool(); + virtual void Draw(Simulation * sim, Brush * brush, ui::Point position); + virtual void DrawLine(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2, bool dragging = false); + virtual void DrawRect(Simulation * sim, Brush * brush, ui::Point position1, ui::Point position2); + virtual void DrawFill(Simulation * sim, Brush * brush, ui::Point position); +}; + +#endif /* TOOL_H_ */ diff --git a/src/gui/game/ToolButton.cpp b/src/gui/game/ToolButton.cpp new file mode 100644 index 0000000..8539ac7 --- /dev/null +++ b/src/gui/game/ToolButton.cpp @@ -0,0 +1,91 @@ +#include "ToolButton.h" +#include "gui/interface/Keys.h" + +ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip): + ui::Button(position, size, text_, toolTip) +{ + SetSelectionState(-1); + Appearance.BorderActive = ui::Colour(255, 0, 0); +} + +void ToolButton::OnMouseClick(int x, int y, unsigned int button) +{ + isButtonDown = true; +} + +void ToolButton::OnMouseUnclick(int x, int y, unsigned int button) +{ + if(isButtonDown) + { + isButtonDown = false; + if(button == BUTTON_LEFT) + SetSelectionState(0); + if(button == BUTTON_RIGHT) + SetSelectionState(1); + if(button == BUTTON_MIDDLE) + SetSelectionState(2); + DoAction(); + } +} + +void ToolButton::Draw(const ui::Point& screenPos) +{ + Graphics * g = ui::Engine::Ref().g; + int totalColour = Appearance.BackgroundInactive.Blue + (3*Appearance.BackgroundInactive.Green) + (2*Appearance.BackgroundInactive.Red); + + if(Appearance.GetTexture()) + { + g->draw_image(Appearance.GetTexture(), screenPos.X+2, screenPos.Y+2, 255); + } + else + { + g->fillrect(screenPos.X+2, screenPos.Y+2, Size.X-4, Size.Y-4, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, Appearance.BackgroundInactive.Alpha); + } + + if(isMouseInside && currentSelection == -1) + { + g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, Appearance.BorderActive.Alpha); + } + else + { + g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, Appearance.BorderInactive.Alpha); + } + + if (totalColour<544) + { + g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 255, 255, 255, 255); + } + else + { + g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 0, 0, 0, 255); + } +} + +void ToolButton::SetSelectionState(int state) +{ + currentSelection = state; + switch(state) + { + case 0: + Appearance.BorderInactive = ui::Colour(255, 0, 0); + break; + case 1: + Appearance.BorderInactive = ui::Colour(0, 0, 255); + break; + case 2: + Appearance.BorderInactive = ui::Colour(0, 255, 0); + break; + default: + Appearance.BorderInactive = ui::Colour(0, 0, 0); + break; + } +} + +int ToolButton::GetSelectionState() +{ + return currentSelection; +} + +ToolButton::~ToolButton() { +} + diff --git a/src/gui/game/ToolButton.h b/src/gui/game/ToolButton.h new file mode 100644 index 0000000..86c8970 --- /dev/null +++ b/src/gui/game/ToolButton.h @@ -0,0 +1,18 @@ +#ifndef TOOLBUTTON_H_ +#define TOOLBUTTON_H_ + +#include "gui/interface/Button.h" + +class ToolButton: public ui::Button { + int currentSelection; +public: + ToolButton(ui::Point position, ui::Point size, std::string text_, std::string toolTip = ""); + virtual void OnMouseUnclick(int x, int y, unsigned int button); + virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void Draw(const ui::Point& screenPos); + void SetSelectionState(int state); + int GetSelectionState(); + virtual ~ToolButton(); +}; + +#endif /* TOOLBUTTON_H_ */ diff --git a/src/gui/game/TriangleBrush.h b/src/gui/game/TriangleBrush.h new file mode 100644 index 0000000..7d537bd --- /dev/null +++ b/src/gui/game/TriangleBrush.h @@ -0,0 +1,46 @@ +/* + * TriangleBrush.h + * + * Created on: Jan 26, 2012 + * Author: Savely Skresanov + */ + +#ifndef TRIANGLEBRUSH_H_ +#define TRIANGLEBRUSH_H_ + +#include +#include "Brush.h" + +class TriangleBrush: public Brush +{ +public: + TriangleBrush(ui::Point size_): + Brush(size_) + { + SetRadius(size_); + }; + virtual void GenerateBitmap() + { + if(bitmap) + delete[] bitmap; + bitmap = new unsigned char[size.X*size.Y]; + int rx = radius.X; + int ry = radius.Y; + for(int x = -rx; x <= rx; x++) + { + for(int y = -ry; y <= ry; y++) + { + if ((abs((rx+2*x)*ry+rx*y) + abs(2*rx*(y-ry)) + abs((rx-2*x)*ry+rx*y))<=(4*rx*ry)) + { + bitmap[(y+ry)*(size.X)+x+rx] = 255; + } + else + { + bitmap[(y+ry)*(size.X)+x+rx] = 0; + } + } + } + } +}; + +#endif /* TRIANGLEBRUSH_H_ */ diff --git a/src/gui/interface/Appearance.cpp b/src/gui/interface/Appearance.cpp new file mode 100644 index 0000000..a4c21d0 --- /dev/null +++ b/src/gui/interface/Appearance.cpp @@ -0,0 +1,54 @@ +#include +#include "Appearance.h" + +namespace ui +{ + Appearance::Appearance(): + HorizontalAlign(AlignCentre), + VerticalAlign(AlignMiddle), + + BackgroundHover(20, 20, 20), + BackgroundInactive(0, 0, 0), + BackgroundActive(255, 255, 255), + BackgroundDisabled(10, 10, 10), + + TextHover(255, 255, 255), + TextInactive(255, 255, 255), + TextActive(0, 0, 0), + TextDisabled(100, 100, 100), + + BorderHover(255, 255, 255), + BorderInactive(200, 200, 200), + BorderActive(235, 235, 235), + BorderDisabled(100, 100, 100), + + Margin(1, 4), + Border(1), + + icon(NoIcon), + + texture(NULL) + {}; + + VideoBuffer * Appearance::GetTexture() + { + return texture; + } + + void Appearance::SetTexture(VideoBuffer * texture) + { + if(this->texture) + delete this->texture; + if(texture) + this->texture = new VideoBuffer(texture); + else + this->texture = NULL; + } + + Appearance::~Appearance() + { + if(texture) + delete texture; + } + +} diff --git a/src/gui/interface/Appearance.h b/src/gui/interface/Appearance.h new file mode 100644 index 0000000..1c1bb5a --- /dev/null +++ b/src/gui/interface/Appearance.h @@ -0,0 +1,57 @@ +#ifndef APPEARANCE_H_ +#define APPEARANCE_H_ + +#include "Border.h" +#include "Colour.h" +#include "graphics/Graphics.h" + +namespace ui +{ + class Appearance + { + private: + VideoBuffer * texture; + public: + enum HorizontalAlignment + { + AlignLeft, AlignCentre, AlignRight + }; + + enum VerticalAlignment + { + AlignTop, AlignMiddle, AlignBottom + }; + + VerticalAlignment VerticalAlign; + HorizontalAlignment HorizontalAlign; + + ui::Colour BackgroundHover; + ui::Colour BackgroundInactive; + ui::Colour BackgroundActive; + ui::Colour BackgroundDisabled; + + ui::Colour TextHover; + ui::Colour TextInactive; + ui::Colour TextActive; + ui::Colour TextDisabled; + + ui::Colour BorderHover; + ui::Colour BorderInactive; + ui::Colour BorderActive; + ui::Colour BorderDisabled; + + ui::Border Margin; + + ui::Border Border; + + Icon icon; + + VideoBuffer * GetTexture(); + void SetTexture(VideoBuffer * texture); + + Appearance(); + ~Appearance(); + }; +} + +#endif diff --git a/src/gui/interface/AvatarButton.cpp b/src/gui/interface/AvatarButton.cpp new file mode 100644 index 0000000..e385c77 --- /dev/null +++ b/src/gui/interface/AvatarButton.cpp @@ -0,0 +1,117 @@ +#include +#include + +#include "AvatarButton.h" +#include "Format.h" +#include "Engine.h" +#include "client/Client.h" +#include "client/requestbroker/RequestBroker.h" +#include "graphics/Graphics.h" +#include "ContextMenu.h" +#include "Keys.h" + +namespace ui { + +AvatarButton::AvatarButton(Point position, Point size, std::string username): + Component(position, size), + name(username), + actionCallback(NULL), + avatar(NULL), + tried(false) +{ + +} + +AvatarButton::~AvatarButton() +{ + RequestBroker::Ref().DetachRequestListener(this); + if(avatar) + delete avatar; + if(actionCallback) + delete actionCallback; +} + +void AvatarButton::Tick(float dt) +{ + if(!avatar && !tried && name.size() > 0) + { + tried = true; + RequestBroker::Ref().RetrieveAvatar(name, Size.X, Size.Y, this); + } +} + +void AvatarButton::OnResponseReady(void * imagePtr) +{ + VideoBuffer * image = (VideoBuffer*)imagePtr; + if(image) + { + if(avatar) + delete avatar; + avatar = image; + } +} + +void AvatarButton::Draw(const Point& screenPos) +{ + Graphics * g = ui::Engine::Ref().g; + + if(avatar) + { + g->draw_image(avatar, screenPos.X, screenPos.Y, 255); + } +} + +void AvatarButton::OnMouseUnclick(int x, int y, unsigned int button) +{ + if(button != 1) + { + return; //left click only! + } + + if(isButtonDown) + { + isButtonDown = false; + DoAction(); + } +} + +void AvatarButton::OnContextMenuAction(int item) +{ + //Do nothing +} + +void AvatarButton::OnMouseClick(int x, int y, unsigned int button) +{ + if(button == BUTTON_RIGHT) + { + if(menu) + menu->Show(GetScreenPos() + ui::Point(x, y)); + } + else + { + isButtonDown = true; + } +} + +void AvatarButton::OnMouseEnter(int x, int y) +{ + isMouseInside = true; +} + +void AvatarButton::OnMouseLeave(int x, int y) +{ + isMouseInside = false; +} + +void AvatarButton::DoAction() +{ + if(actionCallback) + actionCallback->ActionCallback(this); +} + +void AvatarButton::SetActionCallback(AvatarButtonAction * action) +{ + actionCallback = action; +} + +} /* namespace ui */ diff --git a/src/gui/interface/AvatarButton.h b/src/gui/interface/AvatarButton.h new file mode 100644 index 0000000..26b4348 --- /dev/null +++ b/src/gui/interface/AvatarButton.h @@ -0,0 +1,54 @@ +#ifndef AVATARBUTTON_H_ +#define AVATARBUTTON_H_ + +#include + +#include "Component.h" +#include "graphics/Graphics.h" +#include "gui/interface/Colour.h" +#include "client/requestbroker/RequestListener.h" + +namespace ui +{ +class AvatarButton; +class AvatarButtonAction +{ +public: + virtual void ActionCallback(ui::AvatarButton * sender) {} + virtual ~AvatarButtonAction() {} +}; + +class AvatarButton : public Component, public RequestListener +{ + VideoBuffer * avatar; + std::string name; + bool tried; +public: + AvatarButton(Point position, Point size, std::string username); + virtual ~AvatarButton(); + + virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void OnMouseUnclick(int x, int y, unsigned int button); + + virtual void OnMouseEnter(int x, int y); + virtual void OnMouseLeave(int x, int y); + + virtual void OnContextMenuAction(int item); + + virtual void Draw(const Point& screenPos); + virtual void Tick(float dt); + + virtual void OnResponseReady(void * imagePtr); + + virtual void DoAction(); + + void SetUsername(std::string username) { name = username; } + std::string GetUsername() { return name; } + void SetActionCallback(AvatarButtonAction * action); +protected: + bool isMouseInside, isButtonDown; + AvatarButtonAction * actionCallback; +}; +} +#endif /* AVATARBUTTON_H_ */ + diff --git a/src/gui/interface/Border.h b/src/gui/interface/Border.h new file mode 100644 index 0000000..a1ceb81 --- /dev/null +++ b/src/gui/interface/Border.h @@ -0,0 +1,69 @@ +#pragma once +#include "Platform.h" + +namespace ui +{ + + struct Border + { +#if ENABLE_FLOAT_UI +# define BORDER_T float +#else +# define BORDER_T int +#endif + + BORDER_T Top; + BORDER_T Right; + BORDER_T Bottom; + BORDER_T Left; + + Border(BORDER_T all): + Top(all), + Right(all), + Bottom(all), + Left(all) + { + } + + Border(BORDER_T v, BORDER_T h): + Top(v), + Right(h), + Bottom(v), + Left(h) + { + } + + Border(BORDER_T top, BORDER_T right, BORDER_T bottom, BORDER_T left): + Top(top), + Right(right), + Bottom(bottom), + Left(left) + { + } + + inline bool operator == (const int& v) const + { + return (Top == v && Right == v && Bottom == v && Left == v); + } + + inline bool operator == (const Border& v) const + { + return (Top == v.Top && Right == v.Right && Bottom == v.Bottom && Left == v.Left); + } + + inline bool operator != (const Border& v) const + { + return (Top != v.Top || Right != v.Right || Bottom != v.Bottom || Left != v.Left); + } + + inline void operator = (const Border& v) + { + Top = v.Top; + Right = v.Right; + Bottom = v.Bottom; + Left = v.Left; + } + + }; + +} diff --git a/src/gui/interface/Button.cpp b/src/gui/interface/Button.cpp new file mode 100644 index 0000000..3f08153 --- /dev/null +++ b/src/gui/interface/Button.cpp @@ -0,0 +1,237 @@ +#include +#include "gui/interface/Button.h" +#include "graphics/Graphics.h" +#include "Engine.h" +#include "Misc.h" + +namespace ui { + +Button::Button(Point position, Point size, std::string buttonText, std::string toolTip): + Component(position, size), + ButtonText(buttonText), + isMouseInside(false), + isButtonDown(false), + isTogglable(false), + toggle(false), + actionCallback(NULL), + Enabled(true), + toolTip(toolTip) +{ + TextPosition(); +} + +void Button::TextPosition() +{ + buttonDisplayText = ButtonText; + if(buttonDisplayText.length()) + { + if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (Appearance.icon? 22 : 0)) + { + int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (Appearance.icon? 38 : 22)); + buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position); + buttonDisplayText += "..."; + } + } + + Component::TextPosition(buttonDisplayText); +} + +void Button::SetIcon(Icon icon) +{ + Appearance.icon = icon; + TextPosition(); +} + +void Button::SetText(std::string buttonText) +{ + ButtonText = buttonText; + TextPosition(); +} + +void Button::SetTogglable(bool togglable) +{ + toggle = false; + isTogglable = togglable; +} + +bool Button::GetTogglable() +{ + return isTogglable; +} + +TPT_NO_INLINE bool Button::GetToggleState() +{ + return toggle; +} + +TPT_NO_INLINE void Button::SetToggleState(bool state) +{ + toggle = state; +} + +void Button::Draw(const Point& screenPos) +{ + if(!drawn) + { + TextPosition(); + drawn = true; + } + Graphics * g = ui::Engine::Ref().g; + Point Position = screenPos; + ui::Colour bgColour(0, 0, 0); + + ui::Colour textColour = Appearance.TextInactive; + ui::Colour borderColour = Appearance.BorderInactive; + ui::Colour backgroundColour = Appearance.BackgroundInactive; + + if(Enabled) + { + if(isButtonDown || (isTogglable && toggle)) + { + textColour = Appearance.TextActive; + borderColour = Appearance.BorderActive; + backgroundColour = Appearance.BackgroundActive; + } + else if (isMouseInside) + { + textColour = Appearance.TextHover; + borderColour = Appearance.BorderHover; + backgroundColour = Appearance.BackgroundHover; + } + else + { + textColour = Appearance.TextInactive; + borderColour = Appearance.BorderInactive; + backgroundColour = Appearance.BackgroundInactive; + } + } + else + { + textColour = Appearance.TextDisabled; + borderColour = Appearance.BorderDisabled; + backgroundColour = Appearance.BackgroundDisabled; + } + + bgColour = Appearance.BackgroundInactive; + g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, backgroundColour.Red, backgroundColour.Green, backgroundColour.Blue, backgroundColour.Alpha); + if(Appearance.Border == 1) + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + else + { + if(Appearance.Border.Top) + g->draw_line(Position.X, Position.Y, Position.X+Size.X-1, Position.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Bottom) + g->draw_line(Position.X, Position.Y+Size.Y-1, Position.X+Size.X-1, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Left) + g->draw_line(Position.X, Position.Y, Position.X, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Right) + g->draw_line(Position.X+Size.X-1, Position.Y, Position.X+Size.X-1, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + } + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, textColour.Red, textColour.Green, textColour.Blue, textColour.Alpha); + + bool iconInvert = (backgroundColour.Blue + (3*backgroundColour.Green) + (2*backgroundColour.Red))>544?true:false; + + if(Appearance.icon) + { + if(Enabled) + if(isButtonDown || (isTogglable && toggle)) + { + g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 255, iconInvert); + } + else + { + g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 255, iconInvert); + } + else + g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 180, iconInvert); + } +} + +void Button::OnMouseUnclick(int x, int y, unsigned int button) +{ + if(button == 1) + { + if(isButtonDown) + { + isButtonDown = false; + DoAction(); + } + } + else if(button == 3) + { + if(isAltButtonDown) + { + isAltButtonDown = false; + DoAltAction(); + } + } +} + +void Button::OnMouseClick(int x, int y, unsigned int button) +{ + if(!Enabled) + return; + if(button == 1) + { + if(isTogglable) + { + toggle = !toggle; + } + isButtonDown = true; + } + else if(button == 3) + { + isAltButtonDown = true; + } +} + +void Button::OnMouseEnter(int x, int y) +{ + isMouseInside = true; + if(!Enabled) + return; + if(actionCallback) + actionCallback->MouseEnterCallback(this); + if(toolTip.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); + } +} + + +void Button::OnMouseLeave(int x, int y) +{ + isMouseInside = false; + isButtonDown = false; +} + +void Button::DoAction() +{ + if(!Enabled) + return; + if(actionCallback) + actionCallback->ActionCallback(this); +} + +void Button::DoAltAction() +{ + if(!Enabled) + return; + if(actionCallback) + actionCallback->AltActionCallback(this); +} + +void Button::SetActionCallback(ButtonAction * action) +{ + if(actionCallback) + delete actionCallback; + actionCallback = action; +} + +Button::~Button() +{ + if(actionCallback) + delete actionCallback; +} + +} /* namespace ui */ diff --git a/src/gui/interface/Button.h b/src/gui/interface/Button.h new file mode 100644 index 0000000..05f466d --- /dev/null +++ b/src/gui/interface/Button.h @@ -0,0 +1,63 @@ +#ifndef BUTTON_H_ +#define BUTTON_H_ + +#include +#include "Misc.h" +#include "Component.h" +#include "Colour.h" + +namespace ui +{ +class Button; +class ButtonAction +{ +public: + virtual void ActionCallback(ui::Button * sender) {} + virtual void AltActionCallback(ui::Button * sender) {} + virtual void MouseEnterCallback(ui::Button * sender) {} + virtual ~ButtonAction() {} +}; + +class Button : public Component +{ +public: + Button(Point position = Point(0, 0), Point size = Point(0, 0), std::string buttonText = "", std::string toolTip = ""); + virtual ~Button(); + + bool Toggleable; + bool Enabled; + + virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void OnMouseUnclick(int x, int y, unsigned int button); + //virtual void OnMouseUp(int x, int y, unsigned int button); + + virtual void OnMouseEnter(int x, int y); + virtual void OnMouseLeave(int x, int y); + + virtual void Draw(const Point& screenPos); + + virtual void TextPosition(); + inline bool GetState() { return state; } + virtual void DoAction(); //action of button what ever it may be + virtual void DoAltAction(); //action of button what ever it may be + void SetTogglable(bool isTogglable); + bool GetTogglable(); + TPT_NO_INLINE bool GetToggleState(); + TPT_NO_INLINE void SetToggleState(bool state); + void SetActionCallback(ButtonAction * action); + ButtonAction * GetActionCallback() { return actionCallback; } + void SetText(std::string buttonText); + void SetIcon(Icon icon); + inline std::string GetText() { return ButtonText; }; +protected: + + std::string toolTip; + std::string buttonDisplayText; + std::string ButtonText; + + bool isButtonDown, isAltButtonDown, state, isMouseInside, isTogglable, toggle; + ButtonAction * actionCallback; + +}; +} +#endif /* BUTTON_H_ */ diff --git a/src/gui/interface/Checkbox.cpp b/src/gui/interface/Checkbox.cpp new file mode 100644 index 0000000..7caf18b --- /dev/null +++ b/src/gui/interface/Checkbox.cpp @@ -0,0 +1,104 @@ +#include "Checkbox.h" + +using namespace ui; + +Checkbox::Checkbox(ui::Point position, ui::Point size, std::string text, std::string toolTip): + Component(position, size), + text(text), + toolTip(toolTip), + isMouseOver(false), + checked(false), + actionCallback(NULL) +{ + +} + +void Checkbox::SetText(std::string text) +{ + this->text = text; +} + +std::string Checkbox::GetText() +{ + return text; +} + +void Checkbox::SetIcon(Icon icon) +{ + Appearance.icon = icon; + iconPosition.X = 16; + iconPosition.Y = 3; +} + +void Checkbox::OnMouseClick(int x, int y, unsigned int button) +{ + if(checked) + { + checked = false; + } + else + { + checked = true; + } + if(actionCallback) + actionCallback->ActionCallback(this); +} + +void Checkbox::OnMouseUp(int x, int y, unsigned int button) +{ + +} + + +void Checkbox::OnMouseEnter(int x, int y) +{ + isMouseOver = true; + if(toolTip.length()>0 && GetParentWindow()) + { + GetParentWindow()->ToolTip(this, ui::Point(x, y), toolTip); + } +} + +void Checkbox::OnMouseLeave(int x, int y) +{ + isMouseOver = false; +} + +void Checkbox::Draw(const Point& screenPos) +{ + Graphics * g = Engine::Ref().g; + if(checked) + { + g->fillrect(screenPos.X+5, screenPos.Y+5, 6, 6, 255, 255, 255, 255); + } + if(isMouseOver) + { + g->drawrect(screenPos.X+2, screenPos.Y+2, 12, 12, 255, 255, 255, 255); + g->fillrect(screenPos.X+5, screenPos.Y+5, 6, 6, 255, 255, 255, 170); + if (!Appearance.icon) + g->drawtext(screenPos.X+18, screenPos.Y+4, text, 255, 255, 255, 255); + else + g->draw_icon(screenPos.X+iconPosition.X, screenPos.Y+iconPosition.Y, Appearance.icon, 255); + } + else + { + g->drawrect(screenPos.X+2, screenPos.Y+2, 12, 12, 255, 255, 255, 200); + if (!Appearance.icon) + g->drawtext(screenPos.X+18, screenPos.Y+4, text, 255, 255, 255, 200); + else + g->draw_icon(screenPos.X+iconPosition.X, screenPos.Y+iconPosition.Y, Appearance.icon, 200); + } +} + +void Checkbox::SetActionCallback(CheckboxAction * action) +{ + if(actionCallback) + delete actionCallback; + actionCallback = action; +} + +Checkbox::~Checkbox() { + if(actionCallback) + delete actionCallback; +} + diff --git a/src/gui/interface/Checkbox.h b/src/gui/interface/Checkbox.h new file mode 100644 index 0000000..f82cef7 --- /dev/null +++ b/src/gui/interface/Checkbox.h @@ -0,0 +1,39 @@ +#ifndef CHECKBOX_H_ +#define CHECKBOX_H_ + +#include +#include "Component.h" +namespace ui +{ +class Checkbox; +class CheckboxAction +{ +public: + virtual void ActionCallback(ui::Checkbox * sender) {} + virtual ~CheckboxAction() {} +}; +class Checkbox: public ui::Component { + std::string text; + std::string toolTip; + bool checked; + bool isMouseOver; + CheckboxAction * actionCallback; +public: + Checkbox(ui::Point position, ui::Point size, std::string text, std::string toolTip); + void SetText(std::string text); + std::string GetText(); + void SetIcon(Icon icon); + void Draw(const Point& screenPos); + virtual void OnMouseEnter(int x, int y); + virtual void OnMouseLeave(int x, int y); + virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void OnMouseUp(int x, int y, unsigned int button); + void SetActionCallback(CheckboxAction * action); + CheckboxAction * GetActionCallback() { return actionCallback; } + bool GetChecked() { return checked; } + void SetChecked(bool checked_) { checked = checked_; } + virtual ~Checkbox(); +}; +} + +#endif /* CHECKBOX_H_ */ diff --git a/src/gui/interface/Colour.h b/src/gui/interface/Colour.h new file mode 100644 index 0000000..194b9c9 --- /dev/null +++ b/src/gui/interface/Colour.h @@ -0,0 +1,24 @@ +#ifndef COLOUR_H +#define COLOUR_H + +namespace ui +{ +class Colour +{ +public: + unsigned char Red, Green, Blue, Alpha; + Colour(unsigned char red, unsigned char green, unsigned char blue): + Red(red), Green(green), Blue(blue), Alpha(255) + { + } + Colour(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha): + Red(red), Green(green), Blue(blue), Alpha(alpha) + { + } + Colour() + { + } +}; +} + +#endif diff --git a/src/gui/interface/Component.cpp b/src/gui/interface/Component.cpp new file mode 100644 index 0000000..6c4c548 --- /dev/null +++ b/src/gui/interface/Component.cpp @@ -0,0 +1,244 @@ +//#include "Platform.h" +#include +#include "gui/interface/Component.h" +#include "gui/interface/Engine.h" +#include "gui/interface/Point.h" +#include "gui/interface/Window.h" +#include "gui/interface/Panel.h" +#include "gui/interface/ContextMenu.h" + +using namespace ui; + +Component::Component(Window* parent_state): + parentstate_(parent_state), + _parent(NULL), + Position(Point(0,0)), + Size(Point(0,0)), + Locked(false), + Visible(true), + textPosition(0, 0), + textSize(0, 0), + iconPosition(0, 0), + drawn(false), + menu(NULL) +{ + +} + +Component::Component(Point position, Point size): + parentstate_(0), + _parent(NULL), + Position(position), + Size(size), + Locked(false), + Visible(true), + textPosition(0, 0), + textSize(0, 0), + iconPosition(0, 0), + drawn(false), + menu(NULL) +{ + +} + +Component::Component(): + parentstate_(NULL), + _parent(NULL), + Position(Point(0,0)), + Size(Point(0,0)), + Locked(false), + Visible(true), + textPosition(0, 0), + textSize(0, 0), + iconPosition(0, 0), + drawn(false), + menu(NULL) +{ + +} + +void Component::Refresh() +{ + drawn = false; +} + +void Component::TextPosition(std::string displayText) +{ + + textPosition = ui::Point(0, 0); + + int textWidth, textHeight = 10; + Graphics::textsize((char*)displayText.c_str(), textWidth, textHeight); + textSize.X = textWidth; textSize.Y = textHeight; + textHeight-=3; + textWidth-=1; + if(Appearance.icon) + { + textWidth += 13; + } + + int textAreaWidth = Size.X-(Appearance.Margin.Right+Appearance.Margin.Left); + int textAreaHeight = Size.Y-(Appearance.Margin.Top+Appearance.Margin.Bottom); + + switch(Appearance.VerticalAlign) + { + case ui::Appearance::AlignTop: + textPosition.Y = Appearance.Margin.Top+2; + break; + case ui::Appearance::AlignMiddle: + textPosition.Y = Appearance.Margin.Top+((textAreaHeight-textHeight)/2); + break; + case ui::Appearance::AlignBottom: + textPosition.Y = Size.Y-(textHeight+Appearance.Margin.Bottom); + break; + } + + switch(Appearance.HorizontalAlign) + { + case ui::Appearance::AlignLeft: + textPosition.X = Appearance.Margin.Left; + break; + case ui::Appearance::AlignCentre: + textPosition.X = Appearance.Margin.Left+((textAreaWidth-textWidth)/2); + break; + case ui::Appearance::AlignRight: + textPosition.X = Size.X-(textWidth+Appearance.Margin.Right); + break; + } + if(Appearance.icon) + { + iconPosition = textPosition-ui::Point(0, 1); + textPosition.X += 15; + } +} + +bool Component::IsFocused() const +{ + if(parentstate_) + return parentstate_->IsFocused(this); + return false; +} + +void Component::SetParentWindow(Window* window) +{ + parentstate_ = window; +} + +void Component::SetParent(Panel* new_parent) +{ + if(new_parent == NULL) + { + if(_parent != NULL) + { + // remove from current parent and send component to parent state + for(int i = 0; i < _parent->GetChildCount(); ++i) + { + if(_parent->GetChild(i) == this) + { + // remove ourself from parent component + _parent->RemoveChild(i, false); + + // add ourself to the parent state + GetParentWindow()->AddComponent(this); + + //done in this loop. + break; + } + } + } + } + else + { + // remove from parent state (if in parent state) and place in new parent + if(GetParentWindow()) + GetParentWindow()->RemoveComponent(this); + new_parent->children.push_back(this); + } + this->_parent = new_parent; +} + +Point Component::GetScreenPos() +{ + Point newPos(0,0); + if(GetParentWindow()) + newPos += GetParentWindow()->Position; + if(GetParent()) + newPos += GetParent()->Position + GetParent()->ViewportPosition; + newPos += Position; + return newPos; +} + +// ***** OVERRIDEABLES ***** +// Kept empty. + +void Component::OnContextMenuAction(int item) +{ + +} + +void Component::Draw(const Point& screenPos) +{ + drawn = true; +} + +void Component::Tick(float dt) +{ +} + +void Component::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ +} + +void Component::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) +{ +} + +void Component::OnMouseClick(int localx, int localy, unsigned button) +{ +} + +void Component::OnMouseDown(int x, int y, unsigned button) +{ +} + +void Component::OnMouseHover(int localx, int localy) +{ +} + +void Component::OnMouseMoved(int localx, int localy, int dx, int dy) +{ +} + +void Component::OnMouseMovedInside(int localx, int localy, int dx, int dy) +{ +} + +void Component::OnMouseEnter(int localx, int localy) +{ +} + +void Component::OnMouseLeave(int localx, int localy) +{ +} + +void Component::OnMouseUnclick(int localx, int localy, unsigned button) +{ +} + +void Component::OnMouseUp(int x, int y, unsigned button) +{ +} + +void Component::OnMouseWheel(int localx, int localy, int d) +{ +} + +void Component::OnMouseWheelInside(int localx, int localy, int d) +{ +} + +Component::~Component() +{ + if(menu) + delete menu; +} diff --git a/src/gui/interface/Component.h b/src/gui/interface/Component.h new file mode 100644 index 0000000..c034952 --- /dev/null +++ b/src/gui/interface/Component.h @@ -0,0 +1,224 @@ +#pragma once + +#include "Appearance.h" +#include "Point.h" +#include "Window.h" +#include "Platform.h" + +namespace ui +{ + class ContextMenu; + class Window; + class Panel; + + /* class Component + * + * An interactive UI component that can be added to a state or an XComponent*. + * *See sys::XComponent + */ + class Component + { + private: + Window* parentstate_; + Panel* _parent; + protected: + bool drawn; + ui::Point textPosition; + ui::Point textSize; + ui::Point iconPosition; + ui::ContextMenu * menu; + public: + Component(Window* parent_state); + Component(Point position, Point size); + Component(); + virtual ~Component(); + + void* UserData; + inline Window* const GetParentWindow() const { return parentstate_; } + bool IsFocused() const; + + void Invalidate() { drawn = false; } + + Point Position; + Point Size; + bool Locked; + bool Visible; + + ui::Appearance Appearance; + //virtual void SetAppearance(ui::Appearance); + //ui::Appearance GetAppearance(); + virtual void TextPosition(std::string); + + void Refresh(); + + Point GetScreenPos(); + + /* See the parent of this component. + * If new_parent is NULL, this component will have no parent. (THIS DOES NOT delete THE COMPONENT. See XComponent::RemoveChild) + */ + void SetParentWindow(Window* window); + void SetParent(Panel* new_parent); + + //Get the parent component. + inline Panel* const GetParent() const { return _parent; } + + virtual void OnContextMenuAction(int item); + + //UI functions: + /* + void Tick(float dt); + void Draw(const Point& screenPos); + + void OnMouseHover(int localx, int localy); + void OnMouseMoved(int localx, int localy, int dx, int dy); + void OnMouseMovedInside(int localx, int localy, int dx, int dy); + void OnMouseEnter(int localx, int localy); + void OnMouseLeave(int localx, int localy); + void OnMouseDown(int x, int y, unsigned int button); + void OnMouseUp(int x, int y, unsigned int button); + void OnMouseClick(int localx, int localy, unsigned int button); + void OnMouseUnclick(int localx, int localy, unsigned int button); + void OnMouseWheel(int localx, int localy, int d); + void OnMouseWheelInside(int localx, int localy, int d); + void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + */ + + /// + // Called: Every tick. + // Params: + // dt: The change in time. + /// + virtual void Tick(float dt); + + /// + // Called: When ready to draw. + // Params: + // None + /// + virtual void Draw(const Point& screenPos); + + + + + /// + // Called: When the mouse is currently hovering over the item. (Called every tick) + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + /// + virtual void OnMouseHover(int localx, int localy); + + /// + // Called: When the mouse moves. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + // dx: Mouse X delta. + // dy: Mouse Y delta. + /// + virtual void OnMouseMoved(int localx, int localy, int dx, int dy); + + /// + // Called: When the mouse moves. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + // dx: Mouse X delta. + // dy: Mouse Y delta. + /// + virtual void OnMouseMovedInside(int localx, int localy, int dx, int dy); + + /// + // Called: When the mouse moves on top of the item. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + // dx: Mouse X delta. + // dy: Mouse Y delta. + /// + virtual void OnMouseEnter(int localx, int localy); + + /// + // Called: When the mouse leaves the item. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + /// + virtual void OnMouseLeave(int localx, int localy); + + /// + // Called: When a mouse button is pressed. + // Params: + // x: X position of the mouse. + // y: Y position of the mouse. + // button: The button that is being held down. + /// + virtual void OnMouseDown(int x, int y, unsigned button); + + /// + // Called: When a mouse button is released. + // Params: + // x: X position of the mouse. + // y: Y position of the mouse. + // button: The button that is being released. + /// + virtual void OnMouseUp(int x, int y, unsigned button); + + /// + // Called: When a mouse button is pressed on top of the item. + // Params: + // x: X position of the mouse. + // y: Y position of the mouse. + // button: The button that is being held down. + /// + virtual void OnMouseClick(int localx, int localy, unsigned button); + + /// + // Called: When a mouse button is released on top of the item. + // Params: + // x: X position of the mouse. + // y: Y position of the mouse. + // button: The button that is being released. + /// + virtual void OnMouseUnclick(int localx, int localy, unsigned button); + + /// + // Called: When the mouse wheel moves/changes. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + // d: The mouse wheel movement value. + /// + virtual void OnMouseWheel(int localx, int localy, int d); + + /// + // Called: When the mouse wheel moves/changes on top of the item. + // Params: + // localx: Local mouse X position. + // localy: Local mouse Y position. + // d: The mouse wheel movement value. + /// + virtual void OnMouseWheelInside(int localx, int localy, int d); + + /// + // Called: When a key is pressed. + // Params: + // key: The value of the key that is being pressed. + // shift: Shift key is down. + // ctrl: Control key is down. + // alt: Alternate key is down. + /// + virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt); + + /// + // Called: When a key is released. + // Params: + // key: The value of the key that is being released. + // shift: Shift key is released. + // ctrl: Control key is released. + // alt: Alternate key is released. + /// + virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); + }; +} diff --git a/src/gui/interface/ContextMenu.cpp b/src/gui/interface/ContextMenu.cpp new file mode 100644 index 0000000..0d34e19 --- /dev/null +++ b/src/gui/interface/ContextMenu.cpp @@ -0,0 +1,99 @@ +#include "ContextMenu.h" + +using namespace ui; + +class ContextMenu::ItemSelectedAction: public ButtonAction +{ + ContextMenu * window; + int item; +public: + ItemSelectedAction(ContextMenu * window, int itemID): window(window), item(itemID) { } + virtual void ActionCallback(ui::Button *sender) + { + window->ActionCallback(sender, item); + } +}; + +ContextMenu::ContextMenu(Component * source): + Window(ui::Point(0, 0), ui::Point(0, 0)), + Appearance(source->Appearance), + source(source) +{ +} + +void ContextMenu::Show(ui::Point position) +{ + for(int i = 0; i < buttons.size(); i++) + { + RemoveComponent(buttons[i]); + delete buttons[i]; + } + buttons.clear(); + + Position = position; + Size.Y = items.size()*16; + Size.X = 100; + + int currentY = 1; + for(int i = 0; i < items.size(); i++) + { + Button * tempButton = new Button(Point(1, currentY), Point(Size.X-2, 16), items[i].Text); + tempButton->Appearance = Appearance; + tempButton->Enabled = items[i].Enabled; + tempButton->SetActionCallback(new ItemSelectedAction(this, items[i].ID)); + buttons.push_back(tempButton); + AddComponent(tempButton); + currentY += 15; + } + + ui::Engine::Ref().ShowWindow(this); +} + +void ContextMenu::ActionCallback(ui::Button *sender, int item) +{ + ui::Engine::Ref().CloseWindow(); + Halt(); + source->OnContextMenuAction(item); +} + +void ContextMenu::OnMouseDown(int x, int y, unsigned button) +{ + if(!(x > Position.X && y > Position.Y && y < Position.Y+Size.Y && x < Position.X+Size.X)) //Clicked outside window + ui::Engine::Ref().CloseWindow(); +} + +void ContextMenu::SetItem(int id, std::string text) +{ + for(int i = 0; i < items.size(); i++) + { + if(items[i].ID == id) + { + items[i].Text = text; + break; + } + } +} + +void ContextMenu::RemoveItem(int id) +{ + for(int i = 0; i < items.size(); i++) + { + if(items[i].ID == id) + { + items.erase(items.begin()+i); + break; + } + } +} + +void ContextMenu::AddItem(ContextMenuItem item) +{ + items.push_back(item); +} + +void ContextMenu::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 100, 100, 100, 255); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, Appearance.BackgroundInactive.Alpha); +} \ No newline at end of file diff --git a/src/gui/interface/ContextMenu.h b/src/gui/interface/ContextMenu.h new file mode 100644 index 0000000..e5549d3 --- /dev/null +++ b/src/gui/interface/ContextMenu.h @@ -0,0 +1,40 @@ +#ifndef The_Powder_Toy_ContextMenu_h +#define The_Powder_Toy_ContextMenu_h + +#include "Window.h" +#include "Appearance.h" +#include "Button.h" + +namespace ui +{ + +class ContextMenuItem +{ +public: + int ID; + std::string Text; + bool Enabled; + ContextMenuItem(std::string text, int id, bool enabled) : Text(text), ID(id), Enabled(enabled) {} +}; + +class ContextMenu: public ui::Window, public ButtonAction { + std::vector buttons; + std::vector items; + bool isMouseInside; + ui::Component * source; +public: + ui::Appearance Appearance; + class ItemSelectedAction; + ContextMenu(Component * source); + virtual void ActionCallback(ui::Button *sender, int item); + virtual void AddItem(ContextMenuItem item); + virtual void RemoveItem(int id); + virtual void SetItem(int id, std::string text); + virtual void Show(ui::Point position); + virtual void OnDraw(); + virtual void OnMouseDown(int x, int y, unsigned button); + virtual ~ContextMenu() {} +}; +} + +#endif \ No newline at end of file diff --git a/src/gui/interface/DropDown.cpp b/src/gui/interface/DropDown.cpp new file mode 100644 index 0000000..4c1d761 --- /dev/null +++ b/src/gui/interface/DropDown.cpp @@ -0,0 +1,197 @@ +#include +#include "Style.h" +#include "Button.h" +#include "DropDown.h" + +namespace ui { + +class ItemSelectedAction; +class DropDownWindow: public ui::Window { + friend class ItemSelectedAction; + Appearance appearance; + DropDown * dropDown; + std::vector