From 80aa7219a2d47632efa5d72b79bbb4fc65721631 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sun, 29 Jan 2012 17:12:35 +0000 Subject: [PATCH] Vote view in save preview, better handling of controller destruction --- src/PowderToy.cpp | 2 +- src/game/GameController.cpp | 11 ++---- src/interface/SaveButton.cpp | 61 ++++++++++++++++++------------- src/interface/SaveButton.h | 7 ++-- src/login/LoginController.cpp | 6 ++- src/preview/PreviewController.cpp | 5 ++- src/preview/PreviewModel.cpp | 2 + src/preview/PreviewView.cpp | 22 ++++++++++- src/preview/PreviewView.h | 2 + src/render/RenderController.cpp | 5 ++- src/search/Save.h | 3 +- src/search/SearchController.cpp | 8 +++- 12 files changed, 87 insertions(+), 47 deletions(-) diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp index 9174ed0a5..80eddb253 100644 --- a/src/PowderToy.cpp +++ b/src/PowderToy.cpp @@ -75,7 +75,7 @@ int main(int argc, char * argv[]) GameController * gameController = new GameController(); engine->ShowWindow(gameController->GetView()); - new ErrorMessage("Error", "This is a test error message"); + //new ErrorMessage("Error", "This is a test error message"); SDL_Event event; while(engine->Running()) diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp index 1e4154226..65556db5b 100644 --- a/src/game/GameController.cpp +++ b/src/game/GameController.cpp @@ -68,23 +68,20 @@ GameController::~GameController() { if(search) { - if(ui::Engine::Ref().GetWindow() == search->GetView()) - ui::Engine::Ref().CloseWindow(); delete search; } if(renderOptions) { - if(ui::Engine::Ref().GetWindow() == renderOptions->GetView()) - ui::Engine::Ref().CloseWindow(); delete renderOptions; } if(loginWindow) { - if(ui::Engine::Ref().GetWindow() == loginWindow->GetView()) - ui::Engine::Ref().CloseWindow(); delete loginWindow; } - delete gameView; + if(ui::Engine::Ref().GetWindow() == gameView) + { + ui::Engine::Ref().CloseWindow(); + } delete gameModel; } diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index 877dfdae3..fa848bf30 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -9,36 +9,29 @@ namespace ui { -SaveButton::SaveButton(Window* parent_state, Save * save): - Component(parent_state), - save(save), - thumbnail(NULL), - isMouseInside(false), - isButtonDown(false), - actionCallback(NULL) -{ - -} - SaveButton::SaveButton(Point position, Point size, Save * save): Component(position, size), save(save), thumbnail(NULL), isMouseInside(false), isButtonDown(false), - actionCallback(NULL) + actionCallback(NULL), + voteColour(255, 0, 0) { + if(save->votesUp==0) + voteRatio = 0.0f; + else if(save->votesDown==0) + voteRatio = 1.0f; + else + voteRatio = 1.0f-(float)(((float)(save->votesDown))/((float)(save->votesUp))); + if(voteRatio < 0.0f) + voteRatio = 0.0f; + if(voteRatio > 1.0f) //Not possible, but just in case the server were to give a negative value or something + voteRatio = 1.0f; -} -SaveButton::SaveButton(Save * save): - Component(), - save(save), - thumbnail(NULL), - isMouseInside(false), - isButtonDown(false), - actionCallback(NULL) -{ + voteColour.Red = (1.0f-voteRatio)*255; + voteColour.Green = voteRatio*255; } @@ -68,9 +61,9 @@ void SaveButton::Tick(float dt) { scaleFactorY = ((float)(Size.Y-25))/((float)thumbnail->Size.Y); } - if(thumbnail->Size.X > Size.X) + if(thumbnail->Size.X > Size.X-3) { - scaleFactorX = ((float)Size.X)/((float)thumbnail->Size.X); + scaleFactorX = ((float)Size.X-3)/((float)thumbnail->Size.X); } if(scaleFactorY < 1.0f || scaleFactorX < 1.0f) { @@ -90,16 +83,32 @@ void SaveButton::Draw(const Point& screenPos) { Graphics * g = ui::Engine::Ref().g; float scaleFactor; + ui::Point thumbBoxSize(0, 0); if(thumbnail) { - g->draw_image(thumbnail->Data, screenPos.X+(Size.X-thumbnail->Size.X)/2, screenPos.Y+((Size.Y-25)-thumbnail->Size.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); - g->drawrect(screenPos.X+(Size.X-thumbnail->Size.X)/2, screenPos.Y+((Size.Y-25)-thumbnail->Size.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 180, 180, 180, 255); + thumbBoxSize = ui::Point(thumbnail->Size.X, thumbnail->Size.Y); + if(save->id) + g->draw_image(thumbnail->Data, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-25-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); + else + g->draw_image(thumbnail->Data, screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-25-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); } else { scaleFactor = (Size.Y-25)/((float)YRES); - g->drawrect(screenPos.X+(Size.X-((float)XRES)*scaleFactor)/2, screenPos.Y+((Size.Y-21)-((float)YRES)*scaleFactor)/2, ((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor, 180, 180, 180, 255); + thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor); + } + if(save->id) + { + g->drawrect(screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); + g->drawrect(screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 6, thumbBoxSize.Y, 180, 180, 180, 255); + + int voteBar = max(10.0f, ((float)(thumbBoxSize.Y))*voteRatio); + g->fillrect(screenPos.X-3+thumbBoxSize.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(thumbBoxSize.Y-voteBar)+(Size.Y-21-thumbBoxSize.Y)/2, 6, voteBar, voteColour.Red, voteColour.Green, voteColour.Blue, 255); + } + else + { + g->drawrect(screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbBoxSize.X, thumbBoxSize.Y, 180, 180, 180, 255); } if(isMouseInside) diff --git a/src/interface/SaveButton.h b/src/interface/SaveButton.h index 65cff52ed..d4b257015 100644 --- a/src/interface/SaveButton.h +++ b/src/interface/SaveButton.h @@ -7,6 +7,7 @@ #include "search/Save.h" #include "Graphics.h" #include "search/Thumbnail.h" +#include "interface/Colour.h" namespace ui { @@ -23,11 +24,7 @@ class SaveButton : public Component Save * save; Thumbnail * thumbnail; public: - SaveButton(Window* parent_state, Save * save); - SaveButton(Point position, Point size, Save * save); - - SaveButton(Save * save); virtual ~SaveButton(); virtual void OnMouseClick(int x, int y, unsigned int button); @@ -45,6 +42,8 @@ public: void SetActionCallback(SaveButtonAction * action); protected: bool isButtonDown, state, isMouseInside; + float voteRatio; + Colour voteColour; SaveButtonAction * actionCallback; }; } diff --git a/src/login/LoginController.cpp b/src/login/LoginController.cpp index 1a21fc6fc..af1a66a8e 100644 --- a/src/login/LoginController.cpp +++ b/src/login/LoginController.cpp @@ -44,8 +44,10 @@ void LoginController::Exit() } LoginController::~LoginController() { - if(loginView) - delete loginView; + if(ui::Engine::Ref().GetWindow() == loginView) + { + ui::Engine::Ref().CloseWindow(); + } delete loginModel; } diff --git a/src/preview/PreviewController.cpp b/src/preview/PreviewController.cpp index ac68e0738..558d70502 100644 --- a/src/preview/PreviewController.cpp +++ b/src/preview/PreviewController.cpp @@ -55,7 +55,10 @@ void PreviewController::Exit() } PreviewController::~PreviewController() { - delete previewView; + if(ui::Engine::Ref().GetWindow() == previewView) + { + ui::Engine::Ref().CloseWindow(); + } delete previewModel; } diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp index 658a76743..dba7aa1b0 100644 --- a/src/preview/PreviewModel.cpp +++ b/src/preview/PreviewModel.cpp @@ -118,6 +118,7 @@ void PreviewModel::Update() { if(updateSavePreviewFinished) { + updateSavePreviewWorking = false; pthread_join(updateSavePreviewThread, (void**)(&savePreview)); notifyPreviewChanged(); } @@ -127,6 +128,7 @@ void PreviewModel::Update() { if(updateSaveInfoFinished) { + updateSaveInfoWorking = false; pthread_join(updateSaveInfoThread, (void**)(&save)); notifySaveChanged(); } diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index de5869219..68a224bfc 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -31,11 +31,11 @@ PreviewView::PreviewView(): openButton->SetActionCallback(new OpenAction(this)); AddComponent(openButton); - saveNameLabel = new ui::Label(ui::Point(5, (YRES/2)+5), ui::Point(100, 16), ""); + saveNameLabel = new ui::Label(ui::Point(5, (YRES/2)+15), ui::Point(100, 16), ""); saveNameLabel->SetAlignment(AlignLeft, AlignBottom); AddComponent(saveNameLabel); - authorDateLabel = new ui::Label(ui::Point(5, (YRES/2)+5+14), ui::Point(100, 16), ""); + authorDateLabel = new ui::Label(ui::Point(5, (YRES/2)+15+14), ui::Point(100, 16), ""); authorDateLabel->SetAlignment(AlignLeft, AlignBottom); AddComponent(authorDateLabel); } @@ -55,6 +55,20 @@ void PreviewView::OnDraw() } g->drawrect(Position.X, Position.Y, XRES/2, YRES/2, 255, 255, 255, 100); g->draw_line(Position.X+XRES/2, Position.Y, Position.X+XRES/2, Position.Y+Size.Y, 255, 255, 255, XRES+BARSIZE); + + + g->draw_line(Position.X+1, Position.Y+10+YRES/2, Position.X-2+XRES/2, Position.Y+10+YRES/2, 100, 100, 100, XRES+BARSIZE); + float factor; + if(!votesUp && !votesDown) + return; + else + factor = (float)(((float)(XRES/2))/((float)(votesUp+votesDown))); + g->fillrect(Position.X, Position.Y+YRES/2, XRES/2, 10, 200, 50, 50, 255); + g->fillrect(Position.X, Position.Y+YRES/2, (int)(((float)votesUp)*factor), 10, 50, 200, 50, 255); + g->fillrect(Position.X, Position.Y+(YRES/2), 14, 10, 0, 0, 0, 100); + g->fillrect(Position.X+(XRES/2)-14, Position.Y+(YRES/2), 14, 10, 0, 0, 0, 100); + g->draw_icon(Position.X+2, Position.Y+(YRES/2)+2, IconVoteUp); + g->draw_icon(Position.X+(XRES/2)-12, Position.Y+(YRES/2), IconVoteDown); } void PreviewView::OnTick(float dt) @@ -73,11 +87,15 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender) Save * save = sender->GetSave(); if(save) { + votesUp = save->votesUp; + votesDown = save->votesDown; saveNameLabel->SetText(save->name); authorDateLabel->SetText("\bgAuthor:\bw " + save->userName + " \bgDate:\bw "); } else { + votesUp = 0; + votesDown = 0; saveNameLabel->SetText(""); authorDateLabel->SetText(""); } diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h index a526ef76a..dbc32482f 100644 --- a/src/preview/PreviewView.h +++ b/src/preview/PreviewView.h @@ -22,6 +22,8 @@ class PreviewView: public ui::Window { ui::Button * openButton; ui::Label * saveNameLabel; ui::Label * authorDateLabel; + int votesUp; + int votesDown; public: void AttachController(PreviewController * controller) { c = controller;} PreviewView(); diff --git a/src/render/RenderController.cpp b/src/render/RenderController.cpp index e92f03905..c77f3818a 100644 --- a/src/render/RenderController.cpp +++ b/src/render/RenderController.cpp @@ -57,7 +57,10 @@ void RenderController::Exit() } RenderController::~RenderController() { - delete renderView; + if(ui::Engine::Ref().GetWindow() == renderView) + { + ui::Engine::Ref().CloseWindow(); + } delete renderModel; } diff --git a/src/search/Save.h b/src/search/Save.h index 5c471e184..4176e1331 100644 --- a/src/search/Save.h +++ b/src/search/Save.h @@ -11,12 +11,13 @@ using namespace std; class Save { private: +public: int id; int date; int votesUp, votesDown; unsigned char * data; int dataLength; -public: + Save(Save & save); Save(int _id, int _date, int _votesUp, int _votesDown, string _userName, string _name); diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp index 6aff9622c..efd33ac8c 100644 --- a/src/search/SearchController.cpp +++ b/src/search/SearchController.cpp @@ -68,9 +68,13 @@ void SearchController::Exit() SearchController::~SearchController() { + if(activePreview) + delete activePreview; + if(ui::Engine::Ref().GetWindow() == searchView) + { + ui::Engine::Ref().CloseWindow(); + } delete searchModel; - if(searchView) - delete searchView; } void SearchController::DoSearch(std::string query)