summaryrefslogtreecommitdiff
path: root/src/preview
diff options
context:
space:
mode:
authorbuild.powdertoy.co.uk <admin@powdertoy.co.uk>2012-07-19 17:08:34 (GMT)
committer build.powdertoy.co.uk <admin@powdertoy.co.uk>2012-07-19 17:08:34 (GMT)
commitd328b84b1330b0e8f3a7f87ce48b9b20ea4b6d01 (patch)
treedb311c7849270ddd2510cbd65a192b059f8a3c77 /src/preview
parentd71af3706a7a14e8ae65523e1a062417818b8fe2 (diff)
parent4d961117bde4398ae4d72f2db96eef381371e2df (diff)
downloadpowder-d328b84b1330b0e8f3a7f87ce48b9b20ea4b6d01.zip
powder-d328b84b1330b0e8f3a7f87ce48b9b20ea4b6d01.tar.gz
Merge branch 'master' of github.com:FacialTurd/PowderToypp
Diffstat (limited to 'src/preview')
-rw-r--r--src/preview/PreviewController.cpp49
-rw-r--r--src/preview/PreviewController.h9
-rw-r--r--src/preview/PreviewModel.cpp26
-rw-r--r--src/preview/PreviewModel.h5
-rw-r--r--src/preview/PreviewView.cpp174
-rw-r--r--src/preview/PreviewView.h15
6 files changed, 267 insertions, 11 deletions
diff --git a/src/preview/PreviewController.cpp b/src/preview/PreviewController.cpp
index 63d37c3..a922bc6 100644
--- a/src/preview/PreviewController.cpp
+++ b/src/preview/PreviewController.cpp
@@ -12,11 +12,13 @@
#include "PreviewModel.h"
#include "PreviewModelException.h"
#include "dialogues/ErrorMessage.h"
+#include "login/LoginController.h"
#include "Controller.h"
PreviewController::PreviewController(int saveID, ControllerCallback * callback):
HasExited(false),
- saveId(saveID)
+ saveId(saveID),
+ loginWindow(NULL)
{
previewModel = new PreviewModel();
previewView = new PreviewView();
@@ -25,11 +27,24 @@ PreviewController::PreviewController(int saveID, ControllerCallback * callback):
previewModel->UpdateSave(saveID, 0);
+ if(Client::Ref().GetAuthUser().ID)
+ {
+ previewModel->SetCommentBoxEnabled(true);
+ }
+
+ Client::Ref().AddListener(this);
+
this->callback = callback;
}
void PreviewController::Update()
{
+ if(loginWindow && loginWindow->HasExited == true)
+ {
+ delete loginWindow;
+ loginWindow = NULL;
+ }
+
try
{
previewModel->Update();
@@ -45,6 +60,37 @@ void PreviewController::Update()
}
}
+void PreviewController::SubmitComment(std::string comment)
+{
+ if(comment.length() < 4)
+ {
+ new ErrorMessage("Error", "Comment is too short");
+ }
+ else
+ {
+ RequestStatus status = Client::Ref().AddComment(saveId, comment);
+ if(status != RequestOkay)
+ {
+ new ErrorMessage("Error Submitting comment", Client::Ref().GetLastError());
+ }
+ else
+ {
+ previewModel->UpdateComments(1);
+ }
+ }
+}
+
+void PreviewController::ShowLogin()
+{
+ loginWindow = new LoginController();
+ ui::Engine::Ref().ShowWindow(loginWindow->GetView());
+}
+
+void PreviewController::NotifyAuthUserChanged(Client * sender)
+{
+ previewModel->SetCommentBoxEnabled(sender->GetAuthUser().ID);
+}
+
SaveInfo * PreviewController::GetSave()
{
return previewModel->GetSave();
@@ -114,6 +160,7 @@ PreviewController::~PreviewController() {
{
ui::Engine::Ref().CloseWindow();
}
+ Client::Ref().RemoveListener(this);
delete previewModel;
delete previewView;
if(callback)
diff --git a/src/preview/PreviewController.h b/src/preview/PreviewController.h
index 815ca5d..e6b8caa 100644
--- a/src/preview/PreviewController.h
+++ b/src/preview/PreviewController.h
@@ -12,26 +12,33 @@
#include "preview/PreviewView.h"
#include "Controller.h"
#include "client/SaveInfo.h"
+#include "client/ClientListener.h"
+class LoginController;
class PreviewModel;
class PreviewView;
-class PreviewController {
+class PreviewController: public ClientListener {
int saveId;
PreviewModel * previewModel;
PreviewView * previewView;
+ LoginController * loginWindow;
ControllerCallback * callback;
public:
+ virtual void NotifyAuthUserChanged(Client * sender);
+
bool HasExited;
PreviewController(int saveID, ControllerCallback * callback);
void Exit();
void DoOpen();
void OpenInBrowser();
void Report(std::string message);
+ void ShowLogin();
bool GetDoOpen();
SaveInfo * GetSave();
PreviewView * GetView() { return previewView; }
void Update();
void FavouriteSave();
+ void SubmitComment(std::string comment);
void NextCommentPage();
void PrevCommentPage();
diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp
index f62e3f5..59c1c0b 100644
--- a/src/preview/PreviewModel.cpp
+++ b/src/preview/PreviewModel.cpp
@@ -21,7 +21,8 @@ PreviewModel::PreviewModel():
updateSaveCommentsWorking(false),
updateSaveCommentsFinished(false),
commentsTotal(0),
- commentsPageNumber(1)
+ commentsPageNumber(1),
+ commentBoxEnabled(false)
{
// TODO Auto-generated constructor stub
@@ -77,6 +78,20 @@ void PreviewModel::SetFavourite(bool favourite)
}
}
+bool PreviewModel::GetCommentBoxEnabled()
+{
+ return commentBoxEnabled;
+}
+
+void PreviewModel::SetCommentBoxEnabled(bool enabledState)
+{
+ if(enabledState != commentBoxEnabled)
+ {
+ commentBoxEnabled = enabledState;
+ notifyCommentBoxEnabledChanged();
+ }
+}
+
void PreviewModel::UpdateSave(int saveID, int saveDate)
{
this->tSaveID = saveID;
@@ -189,6 +204,14 @@ void PreviewModel::notifySaveChanged()
}
}
+void PreviewModel::notifyCommentBoxEnabledChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyCommentBoxEnabledChanged(this);
+ }
+}
+
void PreviewModel::notifyCommentsPageChanged()
{
for(int i = 0; i < observers.size(); i++)
@@ -210,6 +233,7 @@ void PreviewModel::AddObserver(PreviewView * observer) {
observer->NotifySaveChanged(this);
observer->NotifyCommentsChanged(this);
observer->NotifyCommentsPageChanged(this);
+ observer->NotifyCommentBoxEnabledChanged(this);
}
void PreviewModel::Update()
diff --git a/src/preview/PreviewModel.h b/src/preview/PreviewModel.h
index f00a418..11618a0 100644
--- a/src/preview/PreviewModel.h
+++ b/src/preview/PreviewModel.h
@@ -27,6 +27,7 @@ struct SaveData
class PreviewView;
class PreviewModel {
bool doOpen;
+ bool commentBoxEnabled;
vector<PreviewView*> observers;
SaveInfo * save;
vector<char> saveDataBuffer;
@@ -34,6 +35,7 @@ class PreviewModel {
void notifySaveChanged();
void notifySaveCommentsChanged();
void notifyCommentsPageChanged();
+ void notifyCommentBoxEnabledChanged();
//Background retrieval
int tSaveID;
@@ -66,6 +68,9 @@ public:
SaveInfo * GetSave();
std::vector<SaveComment*> * GetComments();
+ bool GetCommentBoxEnabled();
+ void SetCommentBoxEnabled(bool enabledState);
+
bool GetCommentsLoaded();
int GetCommentsPageNum();
int GetCommentsPageCount();
diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp
index b6b2e02..79d2cdd 100644
--- a/src/preview/PreviewView.cpp
+++ b/src/preview/PreviewView.cpp
@@ -13,9 +13,42 @@
#include "simulation/SaveRenderer.h"
#include "interface/Point.h"
#include "interface/Window.h"
+#include "interface/Textbox.h"
#include "Style.h"
#include "search/Thumbnail.h"
+class PreviewView::LoginAction: public ui::ButtonAction
+{
+ PreviewView * v;
+public:
+ LoginAction(PreviewView * v_){ v = v_; }
+ virtual void ActionCallback(ui::Button * sender)
+ {
+ v->c->ShowLogin();
+ }
+};
+
+class PreviewView::SubmitCommentAction: public ui::ButtonAction
+{
+ PreviewView * v;
+public:
+ SubmitCommentAction(PreviewView * v_){ v = v_; }
+ virtual void ActionCallback(ui::Button * sender)
+ {
+ v->submitComment();
+ }
+};
+
+class PreviewView::AutoCommentSizeAction: public ui::TextboxAction
+{
+ PreviewView * v;
+public:
+ AutoCommentSizeAction(PreviewView * v): v(v) {}
+ virtual void TextChangedCallback(ui::Textbox * sender) {
+ v->commentBoxAutoHeight();
+ }
+};
+
PreviewView::PreviewView():
ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+200, (YRES/2)+150)),
savePreview(NULL),
@@ -24,7 +57,10 @@ PreviewView::PreviewView():
commentsVel(0),
maxOffset(0),
commentsBegin(true),
- commentsEnd(false)
+ commentsEnd(false),
+ addCommentBox(NULL),
+ submitCommentButton(NULL),
+ commentBoxHeight(20)
{
class OpenAction: public ui::ButtonAction
{
@@ -118,11 +154,42 @@ PreviewView::PreviewView():
authorDateLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignBottom;
AddComponent(authorDateLabel);
- pageInfo = new ui::Label(ui::Point((XRES/2) + 5, Size.Y-15), ui::Point(Size.X-((XRES/2) + 10), 15), "Page 1 of 1");
+ pageInfo = new ui::Label(ui::Point((XRES/2) + 5, Size.Y+1), ui::Point(Size.X-((XRES/2) + 10), 15), "Page 1 of 1");
pageInfo->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; authorDateLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
+
AddComponent(pageInfo);
}
+void PreviewView::commentBoxAutoHeight()
+{
+ if(!addCommentBox)
+ return;
+ int textWidth = Graphics::textwidth(addCommentBox->GetText().c_str());
+ if(textWidth+5 > Size.X-(XRES/2)-48)
+ {
+ commentBoxHeight = 58;
+ addCommentBox->SetMultiline(true);
+ addCommentBox->Appearance.VerticalAlign = ui::Appearance::AlignTop;
+
+ commentBoxPositionX = (XRES/2)+4;
+ commentBoxPositionY = Size.Y-58;
+ commentBoxSizeX = Size.X-(XRES/2)-8;
+ commentBoxSizeY = 37;
+ }
+ else
+ {
+ commentBoxHeight = 20;
+ addCommentBox->SetMultiline(false);
+ addCommentBox->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
+
+ commentBoxPositionX = (XRES/2)+4;
+ commentBoxPositionY = Size.Y-19;
+ commentBoxSizeX = Size.X-(XRES/2)-48;
+ commentBoxSizeY = 17;
+ }
+ displayComments(commentsOffset);
+}
+
void PreviewView::DoDraw()
{
Window::DoDraw();
@@ -149,7 +216,7 @@ void PreviewView::OnDraw()
g->draw_image(savePreview->Data, (Position.X+1)+(((XRES/2)-savePreview->Size.X)/2), (Position.Y+1)+(((YRES/2)-savePreview->Size.Y)/2), savePreview->Size.X, savePreview->Size.Y, 255);
}
g->drawrect(Position.X, Position.Y, (XRES/2)+1, (YRES/2)+1, 255, 255, 255, 100);
- g->draw_line(Position.X+XRES/2, Position.Y+1, Position.X+XRES/2, Position.Y+Size.Y-2, 200, 200, 200, 255);
+ g->draw_line(Position.X+1+XRES/2, Position.Y+1, Position.X+XRES/2, Position.Y+Size.Y-2, 200, 200, 200, 255);
g->draw_line(Position.X+1, Position.Y+12+YRES/2, Position.X-1+XRES/2, Position.Y+12+YRES/2, 100, 100, 100,255);
@@ -213,6 +280,42 @@ void PreviewView::OnTick(float dt)
displayComments(commentsOffset);
}
+ if(addCommentBox)
+ {
+ ui::Point positionDiff = ui::Point(commentBoxPositionX, commentBoxPositionY)-addCommentBox->Position;
+ ui::Point sizeDiff = ui::Point(commentBoxSizeX, commentBoxSizeY)-addCommentBox->Size;
+
+ if(positionDiff.X!=0)
+ {
+ int xdiff = positionDiff.X/5;
+ if(xdiff == 0)
+ xdiff = 1*isign(positionDiff.X);
+ addCommentBox->Position.X += xdiff;
+ }
+ if(positionDiff.Y!=0)
+ {
+ int ydiff = positionDiff.Y/5;
+ if(ydiff == 0)
+ ydiff = 1*isign(positionDiff.Y);
+ addCommentBox->Position.Y += ydiff;
+ }
+
+ if(sizeDiff.X!=0)
+ {
+ int xdiff = sizeDiff.X/5;
+ if(xdiff == 0)
+ xdiff = 1*isign(sizeDiff.X);
+ addCommentBox->Size.X += xdiff;
+ }
+ if(sizeDiff.Y!=0)
+ {
+ int ydiff = sizeDiff.Y/5;
+ if(ydiff == 0)
+ ydiff = 1*isign(sizeDiff.Y);
+ addCommentBox->Size.Y += ydiff;
+ }
+ }
+
c->Update();
}
@@ -269,6 +372,23 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender)
}
}
+void PreviewView::submitComment()
+{
+ if(addCommentBox)
+ {
+ std::string comment = std::string(addCommentBox->GetText());
+ submitCommentButton->Enabled = false;
+ addCommentBox->SetText("");
+ addCommentBox->SetPlaceholder("Submitting comment");
+ FocusComponent(NULL);
+
+ c->SubmitComment(comment);
+
+ addCommentBox->SetPlaceholder("Add comment");
+ submitCommentButton->Enabled = true;
+ }
+}
+
void PreviewView::displayComments(int yOffset)
{
for(int i = 0; i < commentComponents.size(); i++)
@@ -289,10 +409,10 @@ void PreviewView::displayComments(int yOffset)
tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom;
currentY += 16;
- if(currentY > Size.Y || usernameY < 0)
+ if(currentY+5 > Size.Y-commentBoxHeight || usernameY < 0)
{
delete tempUsername;
- if(currentY > Size.Y)
+ if(currentY+5 > Size.Y-commentBoxHeight)
break;
}
else
@@ -308,10 +428,10 @@ void PreviewView::displayComments(int yOffset)
tempComment->SetTextColour(ui::Colour(180, 180, 180));
currentY += tempComment->Size.Y+4;
- if(currentY > Size.Y || commentY < 0)
+ if(currentY+5 > Size.Y-commentBoxHeight || commentY < 0)
{
delete tempComment;
- if(currentY > Size.Y)
+ if(currentY+5 > Size.Y-commentBoxHeight)
break;
}
else
@@ -323,6 +443,44 @@ void PreviewView::displayComments(int yOffset)
}
}
+void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender)
+{
+ if(addCommentBox)
+ {
+ RemoveComponent(addCommentBox);
+ addCommentBox = NULL;
+ delete addCommentBox;
+ }
+ if(submitCommentButton)
+ {
+ RemoveComponent(submitCommentButton);
+ submitCommentButton = NULL;
+ delete submitCommentButton;
+ }
+ if(sender->GetCommentBoxEnabled())
+ {
+ commentBoxPositionX = (XRES/2)+4;
+ commentBoxPositionY = Size.Y-19;
+ commentBoxSizeX = Size.X-(XRES/2)-48;
+ commentBoxSizeY = 17;
+
+ addCommentBox = new ui::Textbox(ui::Point((XRES/2)+4, Size.Y-19), ui::Point(Size.X-(XRES/2)-48, 17), "", "Add Comment");
+ addCommentBox->SetActionCallback(new AutoCommentSizeAction(this));
+ addCommentBox->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
+ AddComponent(addCommentBox);
+ submitCommentButton = new ui::Button(ui::Point(Size.X-40, Size.Y-19), ui::Point(40, 19), "Submit");
+ submitCommentButton->SetActionCallback(new SubmitCommentAction(this));
+ //submitCommentButton->Enabled = false;
+ AddComponent(submitCommentButton);
+ }
+ else
+ {
+ submitCommentButton = new ui::Button(ui::Point(XRES/2, Size.Y-19), ui::Point(Size.X-(XRES/2), 19), "Login to comment");
+ submitCommentButton->SetActionCallback(new LoginAction(this));
+ AddComponent(submitCommentButton);
+ }
+}
+
void PreviewView::NotifyCommentsPageChanged(PreviewModel * sender)
{
std::stringstream pageInfoStream;
@@ -361,7 +519,7 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender)
}
- maxOffset = (maxY-Size.Y)+16;
+ maxOffset = (maxY-(Size.Y-commentBoxHeight))+16;
commentsBegin = true;
commentsEnd = false;
commentsOffset = 0;
diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h
index fbc2adc..2e94b85 100644
--- a/src/preview/PreviewView.h
+++ b/src/preview/PreviewView.h
@@ -16,16 +16,22 @@
#include "interface/Button.h"
#include "search/Thumbnail.h"
#include "interface/Label.h"
+#include "interface/Textbox.h"
class PreviewModel;
class PreviewController;
class PreviewView: public ui::Window {
+ class SubmitCommentAction;
+ class LoginAction;
+ class AutoCommentSizeAction;
PreviewController * c;
Thumbnail * savePreview;
ui::Button * openButton;
ui::Button * browserOpenButton;
ui::Button * favButton;
ui::Button * reportButton;
+ ui::Button * submitCommentButton;
+ ui::Textbox * addCommentBox;
ui::Label * saveNameLabel;
ui::Label * authorDateLabel;
ui::Label * pageInfo;
@@ -43,13 +49,22 @@ class PreviewView: public ui::Window {
float commentsOffset;
float commentsVel;
+ int commentBoxHeight;
+ float commentBoxPositionX;
+ float commentBoxPositionY;
+ float commentBoxSizeX;
+ float commentBoxSizeY;
+
void displayComments(int yOffset);
+ void commentBoxAutoHeight();
+ void submitComment();
public:
void AttachController(PreviewController * controller) { c = controller;}
PreviewView();
void NotifySaveChanged(PreviewModel * sender);
void NotifyCommentsChanged(PreviewModel * sender);
void NotifyCommentsPageChanged(PreviewModel * sender);
+ void NotifyCommentBoxEnabledChanged(PreviewModel * sender);
virtual void OnDraw();
virtual void DoDraw();
virtual void OnTick(float dt);