diff options
Diffstat (limited to 'src/interface')
| -rw-r--r-- | src/interface/AvatarButton.cpp | 117 | ||||
| -rw-r--r-- | src/interface/AvatarButton.h | 54 | ||||
| -rw-r--r-- | src/interface/SaveButton.cpp | 25 | ||||
| -rw-r--r-- | src/interface/SaveButton.h | 9 |
4 files changed, 189 insertions, 16 deletions
diff --git a/src/interface/AvatarButton.cpp b/src/interface/AvatarButton.cpp new file mode 100644 index 0000000..0a55ffa --- /dev/null +++ b/src/interface/AvatarButton.cpp @@ -0,0 +1,117 @@ +#include <iostream> +#include <typeinfo> + +#include "AvatarButton.h" +#include "Format.h" +#include "Engine.h" +#include "client/Client.h" +#include "client/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/interface/AvatarButton.h b/src/interface/AvatarButton.h new file mode 100644 index 0000000..d946ddf --- /dev/null +++ b/src/interface/AvatarButton.h @@ -0,0 +1,54 @@ +#ifndef AVATARBUTTON_H_ +#define AVATARBUTTON_H_ + +#include <string> + +#include "Component.h" +#include "graphics/Graphics.h" +#include "interface/Colour.h" +#include "client/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/interface/SaveButton.cpp b/src/interface/SaveButton.cpp index c5840c8..87b80a6 100644 --- a/src/interface/SaveButton.cpp +++ b/src/interface/SaveButton.cpp @@ -5,7 +5,7 @@ #include "client/SaveInfo.h" #include "graphics/Graphics.h" #include "Engine.h" -#include "client/ThumbnailBroker.h" +#include "client/RequestBroker.h" #include "simulation/SaveRenderer.h" #include "Format.h" #include "ContextMenu.h" @@ -117,7 +117,7 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file): SaveButton::~SaveButton() { - ThumbnailBroker::Ref().DetachThumbnailListener(this); + RequestBroker::Ref().DetachRequestListener(this); if(thumbnail) delete thumbnail; @@ -129,13 +129,14 @@ SaveButton::~SaveButton() delete file; } -void SaveButton::OnThumbnailReady(Thumbnail * thumb) +void SaveButton::OnResponseReady(void * imagePtr) { - if(thumb) + VideoBuffer * image = (VideoBuffer*)imagePtr; + if(image) { if(thumbnail) delete thumbnail; - thumbnail = thumb; + thumbnail = image; waitingForThumb = false; } } @@ -144,23 +145,25 @@ void SaveButton::Tick(float dt) { if(!thumbnail && !waitingForThumb) { + float scaleFactor = (Size.Y-25)/((float)YRES); + ui::Point thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor); if(save) { if(save->GetGameSave()) { waitingForThumb = true; - ThumbnailBroker::Ref().RenderThumbnail(save->GetGameSave(), Size.X-3, Size.Y-25, this); + RequestBroker::Ref().RenderThumbnail(save->GetGameSave(), thumbBoxSize.X, thumbBoxSize.Y, this); } else if(save->GetID()) { waitingForThumb = true; - ThumbnailBroker::Ref().RetrieveThumbnail(save->GetID(), save->GetVersion(), Size.X-3, Size.Y-25, this); + RequestBroker::Ref().RetrieveThumbnail(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y, this); } } else if(file && file->GetGameSave()) { waitingForThumb = true; - ThumbnailBroker::Ref().RenderThumbnail(file->GetGameSave(), Size.X-3, Size.Y-25, this); + RequestBroker::Ref().RenderThumbnail(file->GetGameSave(), thumbBoxSize.X, thumbBoxSize.Y, this); } } } @@ -180,11 +183,11 @@ void SaveButton::Draw(const Point& screenPos) if(thumbnail) { - thumbBoxSize = ui::Point(thumbnail->Size.X, thumbnail->Size.Y); + thumbBoxSize = ui::Point(thumbnail->Width, thumbnail->Height); if(save && save->id) - g->draw_image(thumbnail->Data, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); + g->draw_image(thumbnail, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255); else - g->draw_image(thumbnail->Data, screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, thumbnail->Size.X, thumbnail->Size.Y, 255); + g->draw_image(thumbnail, screenPos.X+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255); } else { diff --git a/src/interface/SaveButton.h b/src/interface/SaveButton.h index 7a75973..e03d48a 100644 --- a/src/interface/SaveButton.h +++ b/src/interface/SaveButton.h @@ -6,9 +6,8 @@ #include "Component.h" #include "client/SaveFile.h" #include "client/SaveInfo.h" -#include "client/ThumbnailListener.h" +#include "client/RequestListener.h" #include "graphics/Graphics.h" -#include "search/Thumbnail.h" #include "interface/Colour.h" namespace ui @@ -24,11 +23,11 @@ public: virtual ~SaveButtonAction() {} }; -class SaveButton : public Component, public ThumbnailListener +class SaveButton : public Component, public RequestListener { SaveFile * file; SaveInfo * save; - Thumbnail * thumbnail; + VideoBuffer * thumbnail; std::string name; std::string votesString; std::string votesBackground; @@ -59,7 +58,7 @@ public: virtual void Draw(const Point& screenPos); virtual void Tick(float dt); - virtual void OnThumbnailReady(Thumbnail * thumb); + virtual void OnResponseReady(void * imagePtr); void SetSelected(bool selected_) { selected = selected_; } bool GetSelected() { return selected; } |
