Better element buttons, Save preview WIP

This commit is contained in:
Simon Robertshaw 2012-01-23 22:53:57 +00:00
parent 2bd571e159
commit df72f2580f
15 changed files with 330 additions and 43 deletions

View File

@ -39,6 +39,97 @@ Client::~Client()
http_done();
}
Save * Client::GetSave(int saveID, int saveDate)
{
lastError = "";
std::stringstream urlStream;
urlStream << "http://" << SERVER << "/Browse/View.json?ID=" << saveID;
if(saveDate)
{
urlStream << "&Date=" << saveDate;
}
char * data;
int dataStatus, dataLength;
//Save(int _id, int _votesUp, int _votesDown, string _userName, string _name, string description_, string date_, bool published_):
data = http_simple_get((char *)urlStream.str().c_str(), &dataStatus, &dataLength);
if(dataStatus == 200 && data)
{
try
{
std::istringstream dataStream(data);
json::Object objDocument;
json::Reader::Read(objDocument, dataStream);
json::Number tempID = objDocument["ID"];
json::Number tempScoreUp = objDocument["ScoreUp"];
json::Number tempScoreDown = objDocument["ScoreDown"];
json::String tempUsername = objDocument["Username"];
json::String tempName = objDocument["Name"];
json::String tempDescription = objDocument["Description"];
json::String tempDate = objDocument["Date"];
json::Boolean tempPublished = objDocument["Published"];
return new Save(
tempID.Value(),
tempScoreUp.Value(),
tempScoreDown.Value(),
tempUsername.Value(),
tempName.Value(),
tempDescription.Value(),
tempDate.Value(),
tempPublished.Value()
);
}
catch (json::Exception &e)
{
lastError = "Could not read response";
return NULL;
}
}
else
{
lastError = http_ret_text(dataStatus);
}
return NULL;
}
Thumbnail * Client::GetPreview(int saveID, int saveDate)
{
std::stringstream urlStream;
urlStream << "http://" << SERVER << "/Get.api?Op=thumblarge&ID=" << saveID;
if(saveDate)
{
urlStream << "&Date=" << saveDate;
}
pixel * thumbData;
char * data;
int status, data_size, imgw, imgh;
data = http_simple_get((char *)urlStream.str().c_str(), &status, &data_size);
if (status == 200 && data)
{
thumbData = Graphics::ptif_unpack(data, data_size, &imgw, &imgh);
if(data)
{
free(data);
}
if(thumbData)
{
return new Thumbnail(saveID, saveDate, thumbData, ui::Point(imgw, imgh));
}
else
{
return new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128));
}
}
else
{
if(data)
{
free(data);
}
return new Thumbnail(saveID, saveDate, (pixel *)malloc((128*128) * PIXELSIZE), ui::Point(128, 128));
}
}
std::vector<Save*> * Client::SearchSaves(int start, int count, string query, string sort, int & resultCount)
{
lastError = "";
@ -66,7 +157,7 @@ std::vector<Save*> * Client::SearchSaves(int start, int count, string query, str
{
try
{
std::istringstream dataStream(data); // missing comma!
std::istringstream dataStream(data);
json::Object objDocument;
json::Reader::Read(objDocument, dataStream);

View File

@ -25,7 +25,9 @@ public:
~Client();
void ClearThumbnailRequests();
std::vector<Save*> * SearchSaves(int start, int count, string query, string sort, int & resultCount);
Thumbnail * GetPreview(int saveID, int saveDate);
Thumbnail * GetThumbnail(int saveID, int saveDate);
Save * GetSave(int saveID, int saveDate);
std::string GetLastError() { return lastError; }
};

View File

@ -2,6 +2,7 @@
#include "GameView.h"
#include "interface/Window.h"
#include "interface/Button.h"
#include "interface/Colour.h"
GameView::GameView():
ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, YRES+MENUSIZE)),
@ -246,6 +247,7 @@ void GameView::NotifyActiveToolChanged(GameModel * sender)
void GameView::NotifyToolListChanged(GameModel * sender)
{
int currentX = XRES+BARSIZE-56;
int totalColour;
for(int i = 0; i < menuButtons.size(); i++)
{
if(((MenuAction*)menuButtons[i]->GetActionCallback())->menu==sender->GetActiveMenu())
@ -270,7 +272,22 @@ void GameView::NotifyToolListChanged(GameModel * sender)
currentX -= 36;
tempButton->SetTogglable(true);
tempButton->SetActionCallback(new ToolAction(this, toolList[i]));
tempButton->SetBackgroundColour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue);
totalColour = toolList[i]->colRed + 3*toolList[i]->colGreen + 2*toolList[i]->colBlue;
tempButton->SetBackgroundColour(ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue));
if (totalColour<544)
{
tempButton->SetTextColour(ui::Colour(255, 255, 255));
}
else
{
tempButton->SetTextColour(ui::Colour(0, 0, 0));
}
tempButton->SetBorderColour(ui::Colour(0, 0, 0));
tempButton->SetActiveBackgroundColour(ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue));
tempButton->SetActiveBorderColour(ui::Colour(0, 0, 255));
tempButton->SetAlignment(AlignCentre, AlignBottom);
AddComponent(tempButton);
toolButtons.push_back(tempButton);
@ -366,6 +383,7 @@ void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
{
case ' ':
c->SetPaused();
break;
}
}

View File

@ -26,11 +26,10 @@ Button::Button(Window* parent_state, std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true),
colr(0),
colg(0),
colb(0)
Enabled(true)
{
activeText = background = Colour(0, 0, 0);
text = activeBackground = border = activeBorder = Colour(255, 255, 255);
TextPosition();
}
@ -45,11 +44,10 @@ Button::Button(Point position, Point size, std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true),
colr(0),
colg(0),
colb(0)
Enabled(true)
{
activeText = background = Colour(0, 0, 0);
text = activeBackground = border = activeBorder = Colour(255, 255, 255);
TextPosition();
}
@ -64,11 +62,10 @@ Button::Button(std::string buttonText):
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
Enabled(true),
colr(0),
colg(0),
colb(0)
Enabled(true)
{
activeText = background = Colour(0, 0, 0);
text = activeBackground = border = activeBorder = Colour(255, 255, 255);
TextPosition();
}
@ -137,17 +134,15 @@ void Button::Draw(const Point& screenPos)
{
if(isButtonDown || (isTogglable && toggle))
{
g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, 255, 255, 255, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 0, 0, 0, 255);
g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, activeBackground.Red, activeBackground.Green, activeBackground.Blue, 255);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, activeBorder.Red, activeBorder.Green, activeBorder.Blue, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, activeText.Red, activeText.Green, activeText.Blue, 255);
}
else
{
if(isMouseInside)
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 20, 20, 20, 255);
else
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, colr, colg, colb, 255);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255);
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 255);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, text.Red, text.Green, text.Blue, 255);
}
}
else

View File

@ -11,6 +11,7 @@
#include <string>
#include "Misc.h"
#include "Component.h"
#include "Colour.h"
namespace ui
{
@ -56,12 +57,22 @@ public:
ButtonAction * GetActionCallback() { return actionCallback; }
void TextPosition();
void SetText(std::string buttonText);
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetBackgroundColour(int colr, int colg, int colb) { this->colr = colr; this->colg = colg; this->colb = colb; }
void SetBackgroundColour(Colour background) { this->background = background; }
void SetActiveBackgroundColour(Colour background) { this->activeBackground = background; }
void SetBorderColour(Colour border) { this->border = border; }
void SetActiveBorderColour(Colour border) { this->activeBorder = border; }
void SetTextColour(Colour text) { this->text = text; }
void SetActiveTextColour(Colour text) { this->activeText = text; }
protected:
int colr, colg, colb;
Colour background, activeBackground;
Colour border, activeBorder;
Colour text, activeText;
bool isButtonDown, state, isMouseInside, isTogglable, toggle;
ButtonAction * actionCallback;
ui::Point textPosition;

20
src/interface/Colour.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef COLOUR_H
#define COLOUR_H
namespace ui
{
class Colour
{
public:
unsigned char Red, Green, Blue;
Colour(unsigned char red, unsigned char green, unsigned char blue):
Red(red), Green(green), Blue(blue)
{
}
Colour()
{
}
};
}
#endif

View File

@ -21,7 +21,8 @@ Engine::Engine():
FpsLimit(60.0f),
windows(stack<Window*>()),
lastBuffer(NULL),
prevBuffers(stack<pixel*>())
prevBuffers(stack<pixel*>()),
windowTargetPosition(0, 0)
{
}
@ -53,6 +54,7 @@ void Engine::Exit()
void Engine::ShowWindow(Window * window)
{
windowOpenState = 0.0f;
if(window->Position.X==-1)
{
window->Position.X = (width_-window->Size.X)/2;
@ -61,6 +63,11 @@ void Engine::ShowWindow(Window * window)
{
window->Position.Y = (height_-window->Size.Y)/2;
}
/*if(window->Position.Y > 0)
{
windowTargetPosition = window->Position;
window->Position = Point(windowTargetPosition.X, height_);
}*/
if(state_)
{
if(lastBuffer)
@ -68,7 +75,6 @@ void Engine::ShowWindow(Window * window)
prevBuffers.push(lastBuffer);
}
lastBuffer = (pixel*)malloc((width_ * height_) * PIXELSIZE);
g->fillrect(0, 0, width_, height_, 0, 0, 0, 100);
memcpy(lastBuffer, g->vid, (width_ * height_) * PIXELSIZE);
windows.push(state_);
@ -123,6 +129,24 @@ void Engine::Tick(float dt)
if(state_ != NULL)
state_->DoTick(dt);
if(windowOpenState<1.0f)
{
if(lastBuffer)
{
pixel * vid = g->vid;
g->vid = lastBuffer;
g->fillrect(0, 0, width_, height_, 0, 0, 0, 5);
g->vid = vid;
}
/*if(windowTargetPosition.Y < state_->Position.Y)
{
state_->Position.Y += windowTargetPosition.Y/20;
}*/
windowOpenState += 0.05f*dt;
}
/*if(statequeued_ != NULL)
{
if(state_ != NULL)

View File

@ -59,6 +59,8 @@ namespace ui
std::stack<Window*> windows;
//Window* statequeued_;
Window* state_;
Point windowTargetPosition;
float windowOpenState;
bool running_;

View File

@ -90,13 +90,14 @@ void Window::DoInitialized()
void Window::DoDraw()
{
OnDraw();
//draw
for(int i = 0, sz = Components.size(); i < sz; ++i)
if(Components[i]->Visible)
{
if(AllowExclusiveDrawing)
{
Point scrpos(Components[i]->Position.X, Components[i]->Position.Y);
Point scrpos(Components[i]->Position.X + Position.X, Components[i]->Position.Y + Position.Y);
Components[i]->Draw(scrpos);
}
else
@ -112,7 +113,6 @@ void Window::DoDraw()
}
}
OnDraw();
}
void Window::DoTick(float dt)
@ -164,9 +164,11 @@ void Window::DoKeyRelease(int key, bool shift, bool ctrl, bool alt)
OnKeyRelease(key, shift, ctrl, alt);
}
void Window::DoMouseDown(int x, int y, unsigned button)
void Window::DoMouseDown(int x_, int y_, unsigned button)
{
//on mouse click
int x = x_ - Position.X;
int y = y_ - Position.Y;
bool clickState = false;
for(int i = Components.size() - 1; i > -1 ; --i)
{
@ -192,12 +194,14 @@ void Window::DoMouseDown(int x, int y, unsigned button)
Components[i]->OnMouseDown(x, y, button);
}
OnMouseDown(x, y, button);
OnMouseDown(x_, y_, button);
}
void Window::DoMouseMove(int x, int y, int dx, int dy)
void Window::DoMouseMove(int x_, int y_, int dx, int dy)
{
//on mouse move (if true, and inside)
int x = x_ - Position.X;
int y = y_ - Position.Y;
for(int i = Components.size() - 1; i > -1 ; --i)
{
if(!Components[i]->Locked)
@ -239,11 +243,13 @@ void Window::DoMouseMove(int x, int y, int dx, int dy)
}
}
OnMouseMove(x, y, dx, dy);
OnMouseMove(x_, y_, dx, dy);
}
void Window::DoMouseUp(int x, int y, unsigned button)
void Window::DoMouseUp(int x_, int y_, unsigned button)
{
int x = x_ - Position.X;
int y = y_ - Position.Y;
//on mouse unclick
for(int i = Components.size() - 1; i >= 0 ; --i)
{
@ -264,11 +270,13 @@ void Window::DoMouseUp(int x, int y, unsigned button)
Components[i]->OnMouseUp(x, y, button);
}
OnMouseUp(x, y, button);
OnMouseUp(x_, y_, button);
}
void Window::DoMouseWheel(int x, int y, int d)
void Window::DoMouseWheel(int x_, int y_, int d)
{
int x = x_ - Position.X;
int y = y_ - Position.Y;
//on mouse wheel focused
for(int i = Components.size() - 1; i >= 0 ; --i)
{
@ -287,6 +295,6 @@ void Window::DoMouseWheel(int x, int y, int d)
Components[i]->OnMouseWheel(x - Components[i]->Position.X, y - Components[i]->Position.Y, d);
}
OnMouseWheel(x, y, d);
OnMouseWheel(x_, y_, d);
}

View File

@ -16,7 +16,7 @@ PreviewController::PreviewController(int saveID) {
previewModel->AddObserver(previewView);
previewView->AttachController(this);
previewModel->UpdateSave(saveID);
previewModel->UpdateSave(saveID, 0);
}
PreviewController::~PreviewController() {

View File

@ -6,21 +6,54 @@
*/
#include "PreviewModel.h"
#include "client/Client.h"
PreviewModel::PreviewModel():
save(NULL)
save(NULL),
savePreview(NULL)
{
// TODO Auto-generated constructor stub
}
void PreviewModel::UpdateSave(int saveID)
void PreviewModel::UpdateSave(int saveID, int saveDate)
{
save = Client::Ref().GetSave(saveID, saveDate);
notifySaveChanged();
savePreview = Client::Ref().GetPreview(saveID, saveDate);
notifyPreviewChanged();
}
Thumbnail * PreviewModel::GetPreview()
{
return savePreview;
}
Save * PreviewModel::GetSave()
{
return save;
}
void PreviewModel::notifyPreviewChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifyPreviewChanged(this);
}
}
void PreviewModel::notifySaveChanged()
{
for(int i = 0; i < observers.size(); i++)
{
observers[i]->NotifySaveChanged(this);
}
}
void PreviewModel::AddObserver(PreviewView * observer) {
observers.push_back(observer);
observer->NotifyPreviewChanged(this);
observer->NotifySaveChanged(this);
}
PreviewModel::~PreviewModel() {

View File

@ -11,6 +11,7 @@
#include <vector>
#include "PreviewView.h"
#include "search/Save.h"
#include "search/Thumbnail.h"
using namespace std;
@ -18,10 +19,15 @@ class PreviewView;
class PreviewModel {
vector<PreviewView*> observers;
Save * save;
Thumbnail * savePreview;
void notifyPreviewChanged();
void notifySaveChanged();
public:
PreviewModel();
Thumbnail * GetPreview();
Save * GetSave();
void AddObserver(PreviewView * observer);
void UpdateSave(int saveID);
void UpdateSave(int saveID, int saveDate);
virtual ~PreviewModel();
};

View File

@ -8,22 +8,66 @@
#include "PreviewView.h"
#include "interface/Point.h"
#include "interface/Window.h"
#include "search/Thumbnail.h"
PreviewView::PreviewView():
ui::Window(ui::Point(-1, -1), ui::Point(200, 200))
ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+200, (YRES/2)+150)),
savePreview(NULL)
{
// TODO Auto-generated constructor stub
openButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(100, 16), "Open");
AddComponent(openButton);
saveNameLabel = new ui::Label(ui::Point(0, 0), ui::Point(50, 50), "");
AddComponent(saveNameLabel);
}
void PreviewView::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);
//Save preview (top-left)
if(savePreview && savePreview->Data)
{
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, YRES/2, 255, 255, 255, 100);
}
void PreviewView::NotifySaveChanged(PreviewModel * sender)
{
Save * save = sender->GetSave();
if(save)
{
saveNameLabel->SetText(save->name);
}
else
{
saveNameLabel->SetText("");
}
}
void PreviewView::NotifyPreviewChanged(PreviewModel * sender)
{
savePreview = sender->GetPreview();
if(savePreview && savePreview->Data && !(savePreview->Size.X == XRES/2 && savePreview->Size.Y == YRES/2))
{
int newSizeX, newSizeY;
float factorX = ((float)XRES/2)/((float)savePreview->Size.X);
float factorY = ((float)YRES/2)/((float)savePreview->Size.Y);
float scaleFactor = factorY < factorX ? factorY : factorX;
savePreview->Data = Graphics::resample_img(savePreview->Data, savePreview->Size.X, savePreview->Size.Y, savePreview->Size.X*scaleFactor, savePreview->Size.Y*scaleFactor);
savePreview->Size.X *= scaleFactor;
savePreview->Size.Y *= scaleFactor;
}
}
PreviewView::~PreviewView() {
// TODO Auto-generated destructor stub
delete openButton;
delete saveNameLabel;
}

View File

@ -9,13 +9,23 @@
#define PREVIEWVIEW_H_
#include "interface/Window.h"
#include "preview/PreviewController.h"
#include "preview/PreviewModel.h"
#include "interface/Button.h"
#include "search/Thumbnail.h"
#include "interface/Label.h"
class PreviewModel;
class PreviewController;
class PreviewView: public ui::Window {
PreviewController * c;
Thumbnail * savePreview;
ui::Button * openButton;
ui::Label * saveNameLabel;
public:
void AttachController(PreviewController * controller) { c = controller;}
PreviewView();
void NotifyPreviewChanged(PreviewModel * sender);
void NotifySaveChanged(PreviewModel * sender);
virtual void OnDraw();
virtual ~PreviewView();
};

View File

@ -10,19 +10,40 @@ class Save
private:
int id;
int votesUp, votesDown;
unsigned char * data;
public:
Save(int _id, int _votesUp, int _votesDown, string _userName, string _name):
id(_id),
votesUp(_votesUp),
votesDown(_votesDown),
userName(_userName),
name(_name)
name(_name),
Description("No description provided"),
Date("0/0/0"),
Published(true)
{
}
Save(int _id, int _votesUp, int _votesDown, string _userName, string _name, string description_, string date_, bool published_):
id(_id),
votesUp(_votesUp),
votesDown(_votesDown),
userName(_userName),
name(_name),
Description(description_),
Date(date_),
Published(published_)
{
}
string userName;
string name;
string Description;
string Date;
bool Published;
void SetName(string name){ this->name = name; }
string GetName(){ return name; }
@ -37,6 +58,8 @@ public:
void SetVotesDown(int votesDown){ this->votesDown = votesDown; }
int GetVotesDown(){ return votesDown; }
unsigned char * GetData() { return data; }
};
#endif // SAVE_H