Right click menu to go to history and user info for save buttons

This commit is contained in:
Simon Robertshaw 2012-09-16 17:09:23 +01:00
parent cd7fe454b2
commit bd02c36426
6 changed files with 126 additions and 42 deletions

View File

@ -52,6 +52,7 @@ void ContextMenu::Show(ui::Point position)
void ContextMenu::ActionCallback(ui::Button *sender, int item)
{
ui::Engine::Ref().CloseWindow();
Halt();
source->OnContextMenuAction(item);
}
@ -61,6 +62,18 @@ void ContextMenu::OnMouseDown(int x, int y, unsigned button)
ui::Engine::Ref().CloseWindow();
}
void ContextMenu::SetItem(int id, std::string text)
{
for(int i = 0; i < items.size(); i++)
{
if(items[i].ID == id)
{
items[i].Text = text;
break;
}
}
}
void ContextMenu::RemoveItem(int id)
{
for(int i = 0; i < items.size(); i++)

View File

@ -29,6 +29,7 @@ public:
virtual void ActionCallback(ui::Button *sender, int item);
virtual void AddItem(ContextMenuItem item);
virtual void RemoveItem(int id);
virtual void SetItem(int id, std::string text);
virtual void Show(ui::Point position);
virtual void OnDraw();
virtual void OnMouseDown(int x, int y, unsigned button);

View File

@ -7,6 +7,9 @@
#include "Engine.h"
#include "client/ThumbnailBroker.h"
#include "simulation/SaveRenderer.h"
#include "Format.h"
#include "ContextMenu.h"
#include "Keys.h"
namespace ui {
@ -23,7 +26,7 @@ SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
selected(false),
waitingForThumb(false),
isMouseInsideAuthor(false),
MouseInsideHistory(false),
isMouseInsideHistory(false),
showVotes(false)
{
if(save)
@ -44,6 +47,12 @@ SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
voteColour.Green = voteRatio*255;
}
menu = new ContextMenu(this);
menu->AddItem(ContextMenuItem("Open", 0, true));
menu->AddItem(ContextMenuItem("Select", 1, true));
menu->AddItem(ContextMenuItem("View History", 2, true));
menu->AddItem(ContextMenuItem("More by this user", 3, true));
if(save)
{
name = save->name;
@ -53,6 +62,29 @@ SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
name = name.erase(position, name.length()-position);
name += "...";
}
std::string votes, icon;
int j;
votes = format::NumberToString<int>(save->GetVotesUp()-save->GetVotesDown());
icon += 0xBB;
for (int j = 1; j < votes.length(); j++)
icon += 0xBC;
icon += 0xB9;
icon += 0xBA;
votesBackground = icon;
for (std::string::iterator iter = icon.begin(), end = icon.end(); iter != end; ++iter)
*iter -= 14;
votesBackground2 = icon;
for (std::string::iterator iter = votes.begin(), end = votes.end(); iter != end; ++iter)
if(*iter != '-')
*iter += 127;
votesString = votes;
}
}
@ -70,7 +102,7 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):
wantsDraw(false),
waitingForThumb(false),
isMouseInsideAuthor(false),
MouseInsideHistory(false),
isMouseInsideHistory(false),
showVotes(false)
{
if(file)
@ -198,32 +230,18 @@ void SaveButton::Draw(const Point& screenPos)
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth((char *)save->userName.c_str()))/2, screenPos.Y+Size.Y - 10, save->userName, 100, 130, 160, 255);
if (showVotes)// && !isMouseInside)
{
char icon[64], votestring[64];
int j;
sprintf(votestring, "%d", save->GetVotesUp()-save->GetVotesDown());
icon[0] = 0xBB;
for (j = 1; j <= strlen(votestring); j++)
icon[j] = 0xBC;
icon[j-1] = 0xB9;
icon[j] = 0xBA;
icon[j+1] = 0;
int x = screenPos.X-7+(Size.X-thumbBoxSize.X)/2+thumbBoxSize.X-Graphics::textwidth(icon);
int x = screenPos.X-7+(Size.X-thumbBoxSize.X)/2+thumbBoxSize.X-Graphics::textwidth(votesBackground.c_str());
int y = screenPos.Y-23+(Size.Y-thumbBoxSize.Y)/2+thumbBoxSize.Y;
g->drawtext(x, y, icon, 16, 72, 16, 255);
for (j=0; icon[j]; j++)
icon[j] -= 14;
g->drawtext(x, y, icon, 192, 192, 192, 255);
for (j=0; votestring[j]; j++)
if (votestring[j] != '-')
votestring[j] += 127;
g->drawtext(x+3, y, votestring, 255, 255, 255, 255);
g->drawtext(x, y, votesBackground, 16, 72, 16, 255);
g->drawtext(x, y, votesBackground2, 192, 192, 192, 255);
g->drawtext(x+3, y, votesString, 255, 255, 255, 255);
}
if (MouseInsideHistory && showVotes)
if (isMouseInsideHistory && showVotes)
{
int x = screenPos.X;
int y = screenPos.Y-15+(Size.Y-thumbBoxSize.Y)/2+thumbBoxSize.Y;
g->fillrect(x+1, y+1, 7, 8, 255, 255, 255, 255);
if (MouseInsideHistory) {
if (isMouseInsideHistory) {
g->drawtext(x, y, "\xA6", 200, 100, 80, 255);
} else {
g->drawtext(x, y, "\xA6", 160, 70, 50, 255);
@ -275,40 +293,63 @@ void SaveButton::OnMouseUnclick(int x, int y, unsigned int button)
isButtonDown = false;
if(isMouseInsideAuthor)
DoAuthorAction();
else if (MouseInsideHistory)
else if(isMouseInsideHistory)
DoHistoryAction();
else
DoAction();
}
}
void SaveButton::OnMouseClick(int x, int y, unsigned int button)
void SaveButton::OnContextMenuAction(int item)
{
if(button !=1 && selectable)
switch(item)
{
case 0:
DoAction();
break;
case 1:
selected = !selected;
DoSelection();
break;
case 2:
DoHistoryAction();
break;
case 3:
DoAuthorAction();
break;
}
if(button != 1) return; //left click only!
}
isButtonDown = true;
void SaveButton::OnMouseClick(int x, int y, unsigned int button)
{
if(button == BUTTON_RIGHT)
{
if(menu)
menu->Show(GetScreenPos() + ui::Point(x, y));
}
else
{
isButtonDown = true;
if(button !=1 && selectable)
{
selected = !selected;
DoSelection();
}
}
}
void SaveButton::OnMouseMovedInside(int x, int y, int dx, int dy)
{
if(y > Size.Y-11)
{
isMouseInsideAuthor = true;
}
else
isMouseInsideAuthor = false;
if(showVotes && y > Size.Y-29 && y < Size.Y - 18 && x > 0 && x < 9)
{
MouseInsideHistory = true;
}
isMouseInsideHistory = true;
else
MouseInsideHistory = false;
isMouseInsideHistory = false;
}
void SaveButton::OnMouseEnter(int x, int y)
@ -320,7 +361,7 @@ void SaveButton::OnMouseLeave(int x, int y)
{
isMouseInside = false;
isMouseInsideAuthor = false;
MouseInsideHistory = false;
isMouseInsideHistory = false;
}
void SaveButton::DoHistoryAction()
@ -343,7 +384,14 @@ void SaveButton::DoAction()
void SaveButton::DoSelection()
{
if(selectable)
if(menu)
{
if(selected)
menu->SetItem(1, "Deselect");
else
menu->SetItem(1, "Select");
}
if(selectable && actionCallback)
actionCallback->SelectedCallback(this);
}

View File

@ -30,10 +30,13 @@ class SaveButton : public Component, public ThumbnailListener
SaveInfo * save;
Thumbnail * thumbnail;
std::string name;
std::string votesString;
std::string votesBackground;
std::string votesBackground2;
bool wantsDraw;
bool waitingForThumb;
bool isMouseInsideAuthor;
bool MouseInsideHistory;
bool isMouseInsideHistory;
bool showVotes;
public:
SaveButton(Point position, Point size, SaveInfo * save);
@ -48,6 +51,8 @@ public:
virtual void OnMouseMovedInside(int x, int y, int dx, int dy);
virtual void OnContextMenuAction(int item);
virtual void Draw(const Point& screenPos);
virtual void Tick(float dt);

View File

@ -14,6 +14,7 @@ Window::Window(Point _position, Point _size):
AllowExclusiveDrawing(true),
halt(false),
destruct(false),
stop(false),
cancelButton(NULL),
okayButton(NULL)
#ifdef DEBUG
@ -232,6 +233,7 @@ void Window::DoTick(float dt)
}
halt = false;
stop = false;
OnTick(dt);
@ -292,7 +294,8 @@ void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool a
focusedComponent_->OnKeyPress(key, character, shift, ctrl, alt);
}
OnKeyPress(key, character, shift, ctrl, alt);
if(!stop)
OnKeyPress(key, character, shift, ctrl, alt);
if(key == KEY_ESCAPE)
OnTryExit(Escape);
@ -317,7 +320,8 @@ void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool
focusedComponent_->OnKeyRelease(key, character, shift, ctrl, alt);
}
OnKeyRelease(key, character, shift, ctrl, alt);
if(!stop)
OnKeyRelease(key, character, shift, ctrl, alt);
if(destruct)
finalise();
}
@ -360,7 +364,8 @@ void Window::DoMouseDown(int x_, int y_, unsigned button)
Components[i]->OnMouseDown(x, y, button);
}
OnMouseDown(x_, y_, button);
if(!stop)
OnMouseDown(x_, y_, button);
if(x_ < Position.X || y_ < Position.Y || x_ > Position.X+Size.X || y_ > Position.Y+Size.Y)
OnTryExit(MouseOutside);
@ -419,7 +424,8 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
}
}
OnMouseMove(x_, y_, dx, dy);
if(!stop)
OnMouseMove(x_, y_, dx, dy);
if(destruct)
finalise();
}
@ -452,7 +458,8 @@ void Window::DoMouseUp(int x_, int y_, unsigned button)
Components[i]->OnMouseUp(x, y, button);
}
OnMouseUp(x_, y_, button);
if(!stop)
OnMouseUp(x_, y_, button);
if(destruct)
finalise();
}
@ -483,7 +490,8 @@ 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);
if(!stop)
OnMouseWheel(x_, y_, d);
if(destruct)
finalise();
@ -498,5 +506,12 @@ void Window::SelfDestruct()
{
destruct = true;
halt = true;
stop = true;
}
void Window::Halt()
{
stop = true;
halt = true;
}

View File

@ -68,6 +68,7 @@ enum ChromeStyle
//Sets halt and destroy, this causes the Windows to stop sending events and remove itself.
void SelfDestruct();
void Halt();
bool IsFocused(const Component* c) const;
void FocusComponent(Component* c);
@ -105,6 +106,7 @@ enum ChromeStyle
void finalise();
bool halt;
bool destruct;
bool stop;
#ifdef DEBUG
bool debugMode;
#endif