From f2f4278932e0752c22d15185ce70198077d0b5fd Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 4 Aug 2012 16:39:24 +0100 Subject: [PATCH] Better exiting from windows (Escape and Enter) and 'q' for exit, fixes #35 and #60 --- src/console/ConsoleView.cpp | 1 + src/dialogues/ConfirmPrompt.cpp | 2 ++ src/dialogues/ErrorMessage.cpp | 3 +++ src/dialogues/TextPrompt.cpp | 2 ++ src/game/GameView.cpp | 21 +++++++++++++++++++++ src/game/GameView.h | 2 ++ src/interface/Window.cpp | 2 +- src/login/LoginView.cpp | 7 ++----- src/options/OptionsView.cpp | 2 ++ src/preview/PreviewView.cpp | 6 +++--- src/preview/PreviewView.h | 2 +- src/save/LocalSaveActivity.cpp | 2 ++ src/save/ServerSaveActivity.cpp | 2 ++ src/search/SearchController.cpp | 9 ++++++++- src/search/SearchModel.cpp | 11 ++++++++++- 15 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/console/ConsoleView.cpp b/src/console/ConsoleView.cpp index bbc624e77..32b366c0e 100644 --- a/src/console/ConsoleView.cpp +++ b/src/console/ConsoleView.cpp @@ -35,6 +35,7 @@ void ConsoleView::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, b { switch(key) { + case KEY_ESCAPE: case '`': c->CloseConsole(); break; diff --git a/src/dialogues/ConfirmPrompt.cpp b/src/dialogues/ConfirmPrompt.cpp index ed53800f0..d908a1ddb 100644 --- a/src/dialogues/ConfirmPrompt.cpp +++ b/src/dialogues/ConfirmPrompt.cpp @@ -52,6 +52,7 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial 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; @@ -59,6 +60,7 @@ ConfirmPrompt::ConfirmPrompt(std::string title, std::string message, ConfirmDial okayButton->Appearance.TextInactive = style::Colour::WarningTitle; okayButton->SetActionCallback(new CloseAction(this, ResultOkay)); AddComponent(okayButton); + SetOkayButton(okayButton); ui::Engine::Ref().ShowWindow(this); } diff --git a/src/dialogues/ErrorMessage.cpp b/src/dialogues/ErrorMessage.cpp index ccd4a8ebe..9bff8fbb6 100644 --- a/src/dialogues/ErrorMessage.cpp +++ b/src/dialogues/ErrorMessage.cpp @@ -42,6 +42,9 @@ ErrorMessage::ErrorMessage(std::string title, std::string message): okayButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); okayButton->SetActionCallback(new DismissAction(this)); AddComponent(okayButton); + SetOkayButton(okayButton); + SetCancelButton(okayButton); + ui::Engine::Ref().ShowWindow(this); } diff --git a/src/dialogues/TextPrompt.cpp b/src/dialogues/TextPrompt.cpp index 9db8176fb..d5b6c9567 100644 --- a/src/dialogues/TextPrompt.cpp +++ b/src/dialogues/TextPrompt.cpp @@ -60,6 +60,7 @@ TextPrompt::TextPrompt(std::string title, std::string message, bool multiline, T 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; @@ -67,6 +68,7 @@ TextPrompt::TextPrompt(std::string title, std::string message, bool multiline, T okayButton->Appearance.TextInactive = style::Colour::WarningTitle; okayButton->SetActionCallback(new CloseAction(this, ResultOkay)); AddComponent(okayButton); + SetOkayButton(okayButton); ui::Engine::Ref().ShowWindow(this); } diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp index f58b14564..5773be2c8 100644 --- a/src/game/GameView.cpp +++ b/src/game/GameView.cpp @@ -12,6 +12,7 @@ #include "interface/Slider.h" #include "search/Thumbnail.h" #include "simulation/SaveRenderer.h" +#include "dialogues/ConfirmPrompt.h" #include "Format.h" #include "QuickOption.h" @@ -888,6 +889,22 @@ void GameView::OnMouseUp(int x, int y, unsigned button) } } +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) { this->toolTip = toolTip; @@ -1024,6 +1041,10 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool case 'y': c->SwitchAir(); break; + case KEY_ESCAPE: + case 'q': + ExitPrompt(); + break; case 'u': c->ToggleAHeat(); break; diff --git a/src/game/GameView.h b/src/game/GameView.h index 15bf0dcc5..7cb8d6b88 100644 --- a/src/game/GameView.h +++ b/src/game/GameView.h @@ -142,6 +142,8 @@ public: void NotifyInfoTipChanged(GameModel * sender); void NotifyQuickOptionsChanged(GameModel * sender); + void ExitPrompt(); + virtual void ToolTip(ui::Component * sender, ui::Point mousePosition, std::string toolTip); virtual void OnMouseMove(int x, int y, int dx, int dy); diff --git a/src/interface/Window.cpp b/src/interface/Window.cpp index 39318eb1f..2914b1e5b 100644 --- a/src/interface/Window.cpp +++ b/src/interface/Window.cpp @@ -84,7 +84,7 @@ void Window::RemoveComponent(Component* c) void Window::OnTryExit(ExitMethod method) { - if(cancelButton) + if(cancelButton && method != MouseOutside) cancelButton->DoAction(); } diff --git a/src/login/LoginView.cpp b/src/login/LoginView.cpp index 5a31f4521..2902547ab 100644 --- a/src/login/LoginView.cpp +++ b/src/login/LoginView.cpp @@ -48,11 +48,13 @@ LoginView::LoginView(): AddComponent(infoLabel); AddComponent(loginButton); + SetOkayButton(loginButton); loginButton->Appearance.HorizontalAlign = ui::Appearance::AlignRight; loginButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; loginButton->Appearance.TextInactive = style::Colour::ConfirmButton; loginButton->SetActionCallback(new LoginAction(this)); AddComponent(cancelButton); + SetCancelButton(cancelButton); cancelButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; cancelButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; cancelButton->SetActionCallback(new CancelAction(this)); @@ -78,11 +80,6 @@ void LoginView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, boo else FocusComponent(usernameField); break; - case KEY_ENTER: - case KEY_RETURN: - if(IsFocused(passwordField)) - loginButton->DoAction(); - break; } } diff --git a/src/options/OptionsView.cpp b/src/options/OptionsView.cpp index c814c8ab8..1e0aea799 100644 --- a/src/options/OptionsView.cpp +++ b/src/options/OptionsView.cpp @@ -133,6 +133,8 @@ OptionsView::OptionsView(): ui::Button * tempButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "OK"); tempButton->SetActionCallback(new CloseAction(this)); AddComponent(tempButton); + SetCancelButton(tempButton); + SetOkayButton(tempButton); } void OptionsView::NotifySettingsChanged(OptionsModel * sender) diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index d134568de..920284869 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -75,6 +75,7 @@ PreviewView::PreviewView(): openButton->SetIcon(IconOpen); openButton->SetActionCallback(new OpenAction(this)); AddComponent(openButton); + SetOkayButton(openButton); class FavAction: public ui::ButtonAction { @@ -291,10 +292,9 @@ void PreviewView::OnTick(float dt) c->Update(); } -void PreviewView::OnMouseDown(int x, int y, unsigned button) +void PreviewView::OnTryExit(ExitMethod method) { - if(!(x > Position.X && y > Position.Y && y < Position.Y+Size.Y && x < Position.X+Size.X)) //Clicked outside window - c->Exit(); + c->Exit(); } void PreviewView::OnMouseWheel(int x, int y, int d) diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h index 312d4387d..5e777fb27 100644 --- a/src/preview/PreviewView.h +++ b/src/preview/PreviewView.h @@ -68,7 +68,7 @@ public: virtual void OnDraw(); virtual void DoDraw(); virtual void OnTick(float dt); - virtual void OnMouseDown(int x, int y, unsigned button); + virtual void OnTryExit(ExitMethod method); virtual void OnMouseWheel(int x, int y, int d); virtual ~PreviewView(); }; diff --git a/src/save/LocalSaveActivity.cpp b/src/save/LocalSaveActivity.cpp index 2c7d5b63e..45030822d 100644 --- a/src/save/LocalSaveActivity.cpp +++ b/src/save/LocalSaveActivity.cpp @@ -53,6 +53,7 @@ LocalSaveActivity::LocalSaveActivity(SaveFile save) : cancelButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); cancelButton->SetActionCallback(new CancelAction(this)); AddComponent(cancelButton); + SetCancelButton(cancelButton); ui::Button * okayButton = new ui::Button(ui::Point(Size.X-76, Size.Y-16), ui::Point(76, 16), "Save"); okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; @@ -60,6 +61,7 @@ LocalSaveActivity::LocalSaveActivity(SaveFile save) : okayButton->Appearance.TextInactive = style::Colour::InformationTitle; okayButton->SetActionCallback(new SaveAction(this)); AddComponent(okayButton); + SetOkayButton(okayButton); if(save.GetGameSave()) ThumbnailBroker::Ref().RenderThumbnail(save.GetGameSave(), Size.X-16, -1, this); diff --git a/src/save/ServerSaveActivity.cpp b/src/save/ServerSaveActivity.cpp index f77b95b31..fe750a34b 100644 --- a/src/save/ServerSaveActivity.cpp +++ b/src/save/ServerSaveActivity.cpp @@ -80,6 +80,7 @@ ServerSaveActivity::ServerSaveActivity(SaveInfo save, ServerSaveActivity::SaveUp cancelButton->Appearance.BorderInactive = ui::Colour(200, 200, 200); cancelButton->SetActionCallback(new CancelAction(this)); AddComponent(cancelButton); + SetCancelButton(cancelButton); ui::Button * okayButton = new ui::Button(ui::Point((Size.X/2)-76, Size.Y-16), ui::Point(76, 16), "Save"); okayButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; @@ -87,6 +88,7 @@ ServerSaveActivity::ServerSaveActivity(SaveInfo save, ServerSaveActivity::SaveUp okayButton->Appearance.TextInactive = style::Colour::InformationTitle; okayButton->SetActionCallback(new SaveAction(this)); AddComponent(okayButton); + SetOkayButton(okayButton); if(save.GetGameSave()) ThumbnailBroker::Ref().RenderThumbnail(save.GetGameSave(), (Size.X/2)-16, -1, this); diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp index 13a252a26..f1273548c 100644 --- a/src/search/SearchController.cpp +++ b/src/search/SearchController.cpp @@ -20,8 +20,13 @@ public: { if(cc->activePreview->GetDoOpen() && cc->activePreview->GetSave()) { - cc->searchModel->SetLoadedSave(new SaveInfo(*(cc->activePreview->GetSave()))); + cc->searchModel->SetLoadedSave(cc->activePreview->GetSave()); } + else + { + cc->searchModel->SetLoadedSave(NULL); + } + } }; @@ -161,6 +166,8 @@ void SearchController::Selected(int saveID, bool selected) void SearchController::OpenSave(int saveID) { + if(activePreview) + delete activePreview; activePreview = new PreviewController(saveID, new OpenCallback(this)); ui::Engine::Ref().ShowWindow(activePreview->GetView()); } diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp index 112adbae6..94496737c 100644 --- a/src/search/SearchModel.cpp +++ b/src/search/SearchModel.cpp @@ -57,7 +57,16 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query) void SearchModel::SetLoadedSave(SaveInfo * save) { - loadedSave = save; + if(loadedSave != save && loadedSave) + delete loadedSave; + if(save) + { + loadedSave = new SaveInfo(*save); + } + else + { + loadedSave = NULL; + } } SaveInfo * SearchModel::GetLoadedSave(){