summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-02-01 18:45:59 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-02-01 18:45:59 (GMT)
commit038da72c61ea6a251d805e2de3662f240da52b02 (patch)
tree05170050b16f9d663ec44ae451211e686ba196ac /src
parent857b0cc1fc58f066acd59404d16ee5e566e20f00 (diff)
downloadpowder-038da72c61ea6a251d805e2de3662f240da52b02.zip
powder-038da72c61ea6a251d805e2de3662f240da52b02.tar.gz
Console UI, open in browser button, tab and enter shortcut for Login UI, various
Diffstat (limited to 'src')
-rw-r--r--src/Config.h2
-rw-r--r--src/Graphics.cpp10
-rw-r--r--src/Misc.cpp23
-rw-r--r--src/Misc.h3
-rw-r--r--src/console/ConsoleCommand.h25
-rw-r--r--src/console/ConsoleController.cpp102
-rw-r--r--src/console/ConsoleController.h35
-rw-r--r--src/console/ConsoleModel.cpp72
-rw-r--r--src/console/ConsoleModel.h35
-rw-r--r--src/console/ConsoleView.cpp90
-rw-r--r--src/console/ConsoleView.h37
-rw-r--r--src/game/GameController.cpp10
-rw-r--r--src/game/GameController.h3
-rw-r--r--src/game/GameModel.cpp13
-rw-r--r--src/game/GameView.cpp10
-rw-r--r--src/game/ToolButton.cpp4
-rw-r--r--src/interface/Button.cpp28
-rw-r--r--src/interface/Button.h5
-rw-r--r--src/interface/Keys.h2
-rw-r--r--src/interface/Textbox.cpp32
-rw-r--r--src/interface/Textbox.h5
-rw-r--r--src/login/LoginView.cpp18
-rw-r--r--src/login/LoginView.h1
-rw-r--r--src/preview/PreviewController.cpp11
-rw-r--r--src/preview/PreviewController.h1
-rw-r--r--src/preview/PreviewView.cpp18
-rw-r--r--src/preview/PreviewView.h1
27 files changed, 565 insertions, 31 deletions
diff --git a/src/Config.h b/src/Config.h
index 436e8a2..37a1a65 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -57,6 +57,8 @@
#define BARSIZE 0
#else
#define MENUSIZE 40
+//#define MENUSIZE 20
+//#define BARSIZE 50
#define BARSIZE 17
#endif
#define XRES 612
diff --git a/src/Graphics.cpp b/src/Graphics.cpp
index 678e138..9d9c36b 100644
--- a/src/Graphics.cpp
+++ b/src/Graphics.cpp
@@ -993,6 +993,11 @@ int Graphics::textnwidth(char *s, int n)
{
if (!n)
break;
+ if(((char)*s)=='\b')
+ {
+ s++;
+ continue;
+ }
x += font_data[font_ptrs[(int)(*(unsigned char *)s)]];
n--;
}
@@ -1035,6 +1040,11 @@ int Graphics::textwidthx(char *s, int w)
int x=0,n=0,cw;
for (; *s; s++)
{
+ if((char)*s == '\b')
+ {
+ s++;
+ continue;
+ }
cw = font_data[font_ptrs[(int)(*(unsigned char *)s)]];
if (x+(cw/2) >= w)
break;
diff --git a/src/Misc.cpp b/src/Misc.cpp
index a6ac711..1e73574 100644
--- a/src/Misc.cpp
+++ b/src/Misc.cpp
@@ -621,6 +621,29 @@ void HSV_to_RGB(int h,int s,int v,int *r,int *g,int *b)//convert 0-255(0-360 for
*b += m;
}
+void OpenURI(std::string uri) {
+#ifdef WIN32
+ ShellExecute(0, "OPEN", uri.c_str(), NULL, NULL, 0);
+#elif MACOSX
+ char *cmd = malloc(7+uri.length());
+ strcpy(cmd, "open ");
+ strappend(cmd, uri.c_str());
+ system(cmd);
+#elif LIN32
+ char *cmd = malloc(11+uri.length());
+ strcpy(cmd, "xdg-open ");
+ strappend(cmd, uri.c_str());
+ system(cmd);
+#elif LIN64
+ char *cmd = malloc(11+uri.length());
+ strcpy(cmd, "xdg-open ");
+ strappend(cmd, uri.c_str());
+ system(cmd);
+#else
+ printf("Cannot open browser\n");
+#endif
+}
+
void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v)//convert 0-255 RGB values to 0-255(0-360 for H) HSV
{
float rr, gg, bb, a,x,c,d;
diff --git a/src/Misc.h b/src/Misc.h
index 65ae5b3..2dd96b2 100644
--- a/src/Misc.h
+++ b/src/Misc.h
@@ -2,6 +2,7 @@
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
+#include <string>
enum HorizontalAlignment
{
@@ -86,6 +87,8 @@ void HSV_to_RGB(int h,int s,int v,int *r,int *g,int *b);
void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v);
+void OpenURI(std::string uri);
+
void membwand(void * dest, void * src, size_t destsize, size_t srcsize);
// a b
// c d
diff --git a/src/console/ConsoleCommand.h b/src/console/ConsoleCommand.h
new file mode 100644
index 0000000..f6f91a3
--- /dev/null
+++ b/src/console/ConsoleCommand.h
@@ -0,0 +1,25 @@
+/*
+ * ConsoleCommand.h
+ *
+ * Created on: Feb 1, 2012
+ * Author: Simon
+ */
+
+#ifndef CONSOLECOMMAND_H_
+#define CONSOLECOMMAND_H_
+
+class ConsoleCommand
+{
+public:
+ ConsoleCommand(std::string command, int returnStatus, std::string returnValue):
+ Command(command), ReturnStatus(returnStatus), ReturnValue(returnValue)
+ {
+
+ }
+ std::string Command;
+ int ReturnStatus;
+ std::string ReturnValue;
+};
+
+
+#endif /* CONSOLECOMMAND_H_ */
diff --git a/src/console/ConsoleController.cpp b/src/console/ConsoleController.cpp
new file mode 100644
index 0000000..cf4cdcd
--- /dev/null
+++ b/src/console/ConsoleController.cpp
@@ -0,0 +1,102 @@
+/*
+ * ConsoleController.cpp
+ *
+ * Created on: Jan 31, 2012
+ * Author: Simon
+ */
+
+#include <stack>
+#include "ConsoleController.h"
+
+ConsoleController::ConsoleController(ControllerCallback * callback):
+ HasDone(false)
+{
+ consoleModel = new ConsoleModel();
+ consoleView = new ConsoleView();
+ consoleView->AttachController(this);
+ consoleModel->AddObserver(consoleView);
+
+ this->callback = callback;
+}
+
+void ConsoleController::EvaluateCommand(std::string command)
+{
+ if(command.length())
+ consoleModel->AddLastCommand(ConsoleCommand(command, -1, "Syntax error"));
+ else
+ if(ui::Engine::Ref().GetWindow() == consoleView)
+ ui::Engine::Ref().CloseWindow();
+}
+
+std::string ConsoleController::FormatCommand(std::string command)
+{
+ char * rawText = (char*)command.c_str();
+ char * outputData = (char *)calloc(command.length()*6, 1);
+ int rawTextLoc = 0;
+ int outputDataLoc = 0;
+ std::stack<char> pstack;
+ while(rawText[rawTextLoc])
+ {
+ switch(rawText[rawTextLoc])
+ {
+ case '\\':
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ if(rawText[rawTextLoc])
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ break;
+ case '"':
+ if(pstack.size() && pstack.top() == '"')
+ {
+ pstack.pop();
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ outputData[outputDataLoc++] = '\b';
+ outputData[outputDataLoc++] = 'w';
+ }
+ else
+ {
+ pstack.push('"');
+ outputData[outputDataLoc++] = '\b';
+ outputData[outputDataLoc++] = 'o';
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ }
+ break;
+ default:
+ outputData[outputDataLoc++] = rawText[rawTextLoc++];
+ break;
+ }
+ }
+ return outputData;
+}
+
+void ConsoleController::NextCommand()
+{
+ int cIndex = consoleModel->GetCurrentCommandIndex();
+ if(cIndex < consoleModel->GetPreviousCommands().size())
+ consoleModel->SetCurrentCommandIndex(cIndex+1);
+}
+
+void ConsoleController::PreviousCommand()
+{
+ int cIndex = consoleModel->GetCurrentCommandIndex();
+ if(cIndex > 0)
+ consoleModel->SetCurrentCommandIndex(cIndex-1);
+}
+
+void ConsoleController::Exit()
+{
+ if(ui::Engine::Ref().GetWindow() == consoleView)
+ ui::Engine::Ref().CloseWindow();
+ if(callback)
+ callback->ControllerExit();
+ HasDone = true;
+}
+
+ConsoleView * ConsoleController::GetView()
+{
+ return consoleView;
+}
+
+ConsoleController::~ConsoleController() {
+ // TODO Auto-generated destructor stub
+}
+
diff --git a/src/console/ConsoleController.h b/src/console/ConsoleController.h
new file mode 100644
index 0000000..9316f71
--- /dev/null
+++ b/src/console/ConsoleController.h
@@ -0,0 +1,35 @@
+/*
+ * ConsoleController.h
+ *
+ * Created on: Jan 31, 2012
+ * Author: Simon
+ */
+
+#ifndef CONSOLECONTROLLER_H_
+#define CONSOLECONTROLLER_H_
+
+#include <string>
+#include "Controller.h"
+#include "ConsoleView.h"
+#include "ConsoleModel.h"
+#include "ConsoleCommand.h"
+
+class ConsoleModel;
+class ConsoleView;
+class ConsoleController {
+ ControllerCallback * callback;
+ ConsoleView * consoleView;
+ ConsoleModel * consoleModel;
+public:
+ bool HasDone;
+ ConsoleController(ControllerCallback * callback);
+ std::string FormatCommand(std::string command);
+ void EvaluateCommand(std::string command);
+ void NextCommand();
+ void PreviousCommand();
+ void Exit();
+ ConsoleView * GetView();
+ virtual ~ConsoleController();
+};
+
+#endif /* CONSOLECONTROLLER_H_ */
diff --git a/src/console/ConsoleModel.cpp b/src/console/ConsoleModel.cpp
new file mode 100644
index 0000000..fcbee80
--- /dev/null
+++ b/src/console/ConsoleModel.cpp
@@ -0,0 +1,72 @@
+/*
+ * ConsoleModel.cpp
+ *
+ * Created on: Feb 1, 2012
+ * Author: Simon
+ */
+
+#include "ConsoleModel.h"
+
+ConsoleModel::ConsoleModel() {
+
+}
+
+void ConsoleModel::AddObserver(ConsoleView * observer)
+{
+ observers.push_back(observer);
+}
+
+int ConsoleModel::GetCurrentCommandIndex()
+{
+ return currentCommandIndex;
+}
+
+void ConsoleModel::SetCurrentCommandIndex(int index)
+{
+ currentCommandIndex = index;
+ notifyCurrentCommandChanged();
+}
+
+ConsoleCommand ConsoleModel::GetCurrentCommand()
+{
+ if(currentCommandIndex < 0 || currentCommandIndex >= previousCommands.size())
+ {
+ return ConsoleCommand("", 0, "");
+ }
+ return previousCommands[currentCommandIndex];
+}
+
+void ConsoleModel::AddLastCommand(ConsoleCommand command)
+{
+ previousCommands.push_back(command);
+ if(previousCommands.size()>25)
+ previousCommands.pop_front();
+ currentCommandIndex = previousCommands.size();
+ notifyPreviousCommandsChanged();
+}
+
+std::deque<ConsoleCommand> ConsoleModel::GetPreviousCommands()
+{
+ return previousCommands;
+}
+
+void ConsoleModel::notifyPreviousCommandsChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyPreviousCommandsChanged(this);
+ }
+}
+
+void ConsoleModel::notifyCurrentCommandChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifyCurrentCommandChanged(this);
+ }
+}
+
+ConsoleModel::~ConsoleModel() {
+
+}
+
diff --git a/src/console/ConsoleModel.h b/src/console/ConsoleModel.h
new file mode 100644
index 0000000..b340ea8
--- /dev/null
+++ b/src/console/ConsoleModel.h
@@ -0,0 +1,35 @@
+/*
+ * ConsoleModel.h
+ *
+ * Created on: Feb 1, 2012
+ * Author: Simon
+ */
+
+#ifndef CONSOLEMODEL_H_
+#define CONSOLEMODEL_H_
+
+#include <vector>
+#include <deque>
+#include "ConsoleView.h"
+#include "ConsoleCommand.h"
+
+class ConsoleView;
+class ConsoleModel {
+ int currentCommandIndex;
+ std::vector<ConsoleView*> observers;
+ std::deque<ConsoleCommand> previousCommands;
+ void notifyPreviousCommandsChanged();
+ void notifyCurrentCommandChanged();
+public:
+ int GetCurrentCommandIndex();
+ void SetCurrentCommandIndex(int index);
+ ConsoleCommand GetCurrentCommand();
+
+ std::deque<ConsoleCommand> GetPreviousCommands();
+ ConsoleModel();
+ void AddObserver(ConsoleView * observer);
+ void AddLastCommand(ConsoleCommand command);
+ virtual ~ConsoleModel();
+};
+
+#endif /* CONSOLEMODEL_H_ */
diff --git a/src/console/ConsoleView.cpp b/src/console/ConsoleView.cpp
new file mode 100644
index 0000000..117d8cb
--- /dev/null
+++ b/src/console/ConsoleView.cpp
@@ -0,0 +1,90 @@
+/*
+ * ConsoleView.cpp
+ *
+ * Created on: Jan 31, 2012
+ * Author: Simon
+ */
+
+#include "ConsoleView.h"
+#include "interface/Keys.h"
+
+ConsoleView::ConsoleView():
+ ui::Window(ui::Point(0, 0), ui::Point(XRES+BARSIZE, 150)),
+ commandField(NULL)
+{
+ class CommandHighlighter: public ui::TextboxAction
+ {
+ ConsoleView * v;
+ public:
+ CommandHighlighter(ConsoleView * v_) { v = v_; }
+ void TextChangedCallback(ui::Textbox * sender)
+ {
+ sender->SetDisplayText(v->c->FormatCommand(sender->GetText()));
+ }
+ };
+ commandField = new ui::Textbox(ui::Point(0, Size.Y-16), ui::Point(Size.X, 16), "");
+ commandField->SetAlignment(AlignLeft, AlignBottom);
+ commandField->SetActionCallback(new CommandHighlighter(this));
+ AddComponent(commandField);
+ FocusComponent(commandField);
+ commandField->SetBorder(false);
+}
+
+void ConsoleView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
+{
+ switch(key)
+ {
+ case KEY_RETURN:
+ case KEY_ENTER:
+ c->EvaluateCommand(commandField->GetText());
+ commandField->SetText("");
+ break;
+ case KEY_DOWN:
+ c->NextCommand();
+ break;
+ case KEY_UP:
+ c->PreviousCommand();
+ break;
+ }
+}
+
+void ConsoleView::NotifyPreviousCommandsChanged(ConsoleModel * sender)
+{
+ for(int i = 0; i < commandList.size(); i++)
+ {
+ RemoveComponent(commandList[i]);
+ delete commandList[i];
+ }
+ commandList.clear();
+ std::deque<ConsoleCommand> commands = sender->GetPreviousCommands();
+ int currentY = Size.Y - 32;
+ if(commands.size())
+ for(int i = commands.size()-1; i >= 0; i--)
+ {
+ if(currentY <= 0)
+ break;
+ ui::Label * tempLabel = new ui::Label(ui::Point(0, currentY), ui::Point(Size.X, 16), commands[i].Command);
+ tempLabel->SetAlignment(AlignLeft, AlignMiddle);
+ commandList.push_back(tempLabel);
+ AddComponent(tempLabel);
+ currentY-=16;
+ }
+}
+
+void ConsoleView::NotifyCurrentCommandChanged(ConsoleModel * sender)
+{
+ commandField->SetText(sender->GetCurrentCommand().Command);
+}
+
+
+void ConsoleView::OnDraw()
+{
+ Graphics * g = ui::Engine::Ref().g;
+ g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 0, 0, 0, 110);
+ g->blend_line(Position.X, Position.Y+Size.Y-16, Position.X+Size.X, Position.Y+Size.Y-16, 255, 255, 255, 160);
+ g->blend_line(Position.X, Position.Y+Size.Y, Position.X+Size.X, Position.Y+Size.Y, 255, 255, 255, 200);
+}
+
+ConsoleView::~ConsoleView() {
+}
+
diff --git a/src/console/ConsoleView.h b/src/console/ConsoleView.h
new file mode 100644
index 0000000..bb1a98e
--- /dev/null
+++ b/src/console/ConsoleView.h
@@ -0,0 +1,37 @@
+/*
+ * ConsoleView.h
+ *
+ * Created on: Jan 31, 2012
+ * Author: Simon
+ */
+
+#ifndef CONSOLEVIEW_H_
+#define CONSOLEVIEW_H_
+
+#include <vector>
+#include <queue>
+#include "interface/Label.h"
+#include "interface/Window.h"
+#include "ConsoleController.h"
+#include "ConsoleModel.h"
+#include "interface/Textbox.h"
+#include "ConsoleCommand.h"
+
+
+class ConsoleController;
+class ConsoleModel;
+class ConsoleView: public ui::Window {
+ ConsoleController * c;
+ ui::Textbox * commandField;
+ std::vector<ui::Label*> commandList;
+public:
+ ConsoleView();
+ virtual void OnDraw();
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void AttachController(ConsoleController * c_) { c = c_; }
+ void NotifyPreviousCommandsChanged(ConsoleModel * sender);
+ void NotifyCurrentCommandChanged(ConsoleModel * sender);
+ virtual ~ConsoleView();
+};
+
+#endif /* CONSOLEVIEW_H_ */
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp
index 9e5f53b..a4e0ccc 100644
--- a/src/game/GameController.cpp
+++ b/src/game/GameController.cpp
@@ -95,6 +95,10 @@ GameController::~GameController()
{
delete loginWindow;
}
+ if(console)
+ {
+ delete console;
+ }
if(ui::Engine::Ref().GetWindow() == gameView)
{
ui::Engine::Ref().CloseWindow();
@@ -301,6 +305,12 @@ void GameController::OpenDisplayOptions()
//TODO: Implement
}
+void GameController::ShowConsole()
+{
+ console = new ConsoleController(NULL);
+ ui::Engine::Ref().ShowWindow(console->GetView());
+}
+
void GameController::OpenRenderOptions()
{
renderOptions = new RenderController(gameModel->GetRenderer(), new RenderCallback(this));
diff --git a/src/game/GameController.h b/src/game/GameController.h
index df5f9fc..84af343 100644
--- a/src/game/GameController.h
+++ b/src/game/GameController.h
@@ -10,6 +10,7 @@
#include "render/RenderController.h"
#include "login/LoginController.h"
#include "ssave/SSaveController.h"
+#include "console/ConsoleController.h"
#include "Menu.h"
using namespace std;
@@ -26,6 +27,7 @@ private:
RenderController * renderOptions;
LoginController * loginWindow;
SSaveController * ssave;
+ ConsoleController * console;
public:
class LoginCallback;
class SearchCallback;
@@ -57,6 +59,7 @@ public:
void ReloadSim();
void Vote(int direction);
void ChangeBrush();
+ void ShowConsole();
ui::Point PointTranslate(ui::Point point);
};
diff --git a/src/game/GameModel.cpp b/src/game/GameModel.cpp
index 2ff9e0b..dcad405 100644
--- a/src/game/GameModel.cpp
+++ b/src/game/GameModel.cpp
@@ -41,12 +41,19 @@ GameModel::GameModel():
//sim->wtypes[i]
}
+ //Set default brush palette
brushList.push_back(new Brush(ui::Point(4, 4)));
brushList.push_back(new EllipseBrush(ui::Point(4, 4)));
- activeTools[0] = new ElementTool(1, "TURD", 0, 0, 0);
- activeTools[1] = new ElementTool(0, "TURD", 0, 0, 0);
- //activeTool[1] = new ElementTool(0, "TURD", 0, 0, 0);
+ //Set default tools
+ activeTools[0] = menuList[SC_POWDERS]->GetToolList()[0];
+ activeTools[1] = menuList[SC_SPECIAL]->GetToolList()[0];
+
+ //Set default menu
+ activeMenu = menuList[SC_POWDERS];
+ toolList = menuList[SC_POWDERS]->GetToolList();
+
+ //Load last user
std::cout << Client::Ref().GetAuthUser().Username << std::endl;
if(Client::Ref().GetAuthUser().ID)
{
diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp
index a443025..e93b9b7 100644
--- a/src/game/GameView.cpp
+++ b/src/game/GameView.cpp
@@ -277,6 +277,7 @@ void GameView::NotifyActiveToolsChanged(GameModel * sender)
void GameView::NotifyToolListChanged(GameModel * sender)
{
+ //int currentY = YRES+MENUSIZE-36;
int currentX = XRES+BARSIZE-56;
int totalColour;
for(int i = 0; i < menuButtons.size(); i++)
@@ -299,8 +300,10 @@ void GameView::NotifyToolListChanged(GameModel * sender)
vector<Tool*> toolList = sender->GetToolList();
for(int i = 0; i < toolList.size(); i++)
{
- ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES), ui::Point(32, 16), toolList[i]->GetName());
- currentX -= 36;
+ //ToolButton * tempButton = new ToolButton(ui::Point(XRES+1, currentY), ui::Point(28, 15), toolList[i]->GetName());
+ ToolButton * tempButton = new ToolButton(ui::Point(currentX, YRES+1), ui::Point(28, 15), toolList[i]->GetName());
+ //currentY -= 17;
+ currentX -= 32;
tempButton->SetActionCallback(new ToolAction(this, toolList[i]));
tempButton->SetBackgroundColour(ui::Colour(toolList[i]->colRed, toolList[i]->colGreen, toolList[i]->colBlue));
@@ -507,6 +510,9 @@ void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
zoomCursorFixed = false;
c->SetZoomEnabled(true);
break;
+ case '`':
+ c->ShowConsole();
+ break;
}
}
diff --git a/src/game/ToolButton.cpp b/src/game/ToolButton.cpp
index 374d424..27bbab6 100644
--- a/src/game/ToolButton.cpp
+++ b/src/game/ToolButton.cpp
@@ -43,11 +43,11 @@ void ToolButton::Draw(const ui::Point& screenPos)
if (totalColour<544)
{
- g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, ButtonText.c_str(), 255, 255, 255, 255);
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 255, 255, 255, 255);
}
else
{
- g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, ButtonText.c_str(), 0, 0, 0, 255);
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 0, 0, 0, 255);
}
if(currentSelection!=-1)
{
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp
index fc3eda5..095ef30 100644
--- a/src/interface/Button.cpp
+++ b/src/interface/Button.cpp
@@ -25,7 +25,7 @@ Button::Button(Point position, Point size, std::string buttonText):
actionCallback(NULL),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
- textHAlign(AlignCentre),
+ textHAlign(AlignLeft),
Enabled(true),
icon(NoIcon)
{
@@ -36,6 +36,16 @@ Button::Button(Point position, Point size, std::string buttonText):
void Button::TextPosition()
{
+ buttonDisplayText = ButtonText;
+ if(buttonDisplayText.length())
+ {
+ if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (icon? 22 : 0))
+ {
+ int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (icon? 38 : 22));
+ buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position);
+ buttonDisplayText += "...";
+ }
+ }
switch(textVAlign)
{
case AlignTop:
@@ -45,7 +55,7 @@ void Button::TextPosition()
textPosition.Y = (Size.Y-10)/2;
break;
case AlignBottom:
- textPosition.Y = Size.Y-11;
+ textPosition.Y = Size.Y-12;
break;
}
@@ -57,10 +67,10 @@ void Button::TextPosition()
textPosition.X = 3+17;
break;
case AlignCentre:
- textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)ButtonText.c_str()))/2)+17;
+ textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2)+17;
break;
case AlignRight:
- textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)ButtonText.c_str()))-2)+17;
+ textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2)+17;
break;
}
}
@@ -72,10 +82,10 @@ void Button::TextPosition()
textPosition.X = 3;
break;
case AlignCentre:
- textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2;
+ textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2;
break;
case AlignRight:
- textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))-2;
+ textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2;
break;
}
}
@@ -124,20 +134,20 @@ void Button::Draw(const Point& screenPos)
{
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);
+ g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, activeText.Red, activeText.Green, activeText.Blue, 255);
}
else
{
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);
+ g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, text.Red, text.Green, text.Blue, 255);
}
}
else
{
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, background.Red, background.Green, background.Blue, 180);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 180, 180, 180, 255);
- g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, ButtonText, 180, 180, 180, 255);
+ g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, 180, 180, 180, 255);
}
if(icon)
g->draw_icon(Position.X+3, Position.Y+textPosition.Y, icon);
diff --git a/src/interface/Button.h b/src/interface/Button.h
index 4537e1f..6f485cb 100644
--- a/src/interface/Button.h
+++ b/src/interface/Button.h
@@ -33,8 +33,6 @@ public:
bool Toggleable;
bool Enabled;
- std::string ButtonText;
-
virtual void OnMouseClick(int x, int y, unsigned int button);
virtual void OnMouseUp(int x, int y, unsigned int button);
//virtual void OnMouseUp(int x, int y, unsigned int button);
@@ -72,6 +70,9 @@ protected:
Colour border, activeBorder;
Colour text, activeText;
+ std::string buttonDisplayText;
+ std::string ButtonText;
+
bool isButtonDown, state, isMouseInside, isTogglable, toggle;
ButtonAction * actionCallback;
ui::Point textPosition;
diff --git a/src/interface/Keys.h b/src/interface/Keys.h
index b5ae97b..5350b06 100644
--- a/src/interface/Keys.h
+++ b/src/interface/Keys.h
@@ -7,6 +7,8 @@
#define KEY_BACKSPACE SDLK_BACKSPACE
#define KEY_DELETE SDLK_DELETE
#define KEY_TAB SDLK_TAB
+#define KEY_RETURN SDLK_RETURN
+#define KEY_ENTER SDLK_KP_ENTER
#define KEY_CTRL SDLK_LCTRL
#define KEY_ALT SDLK_LALT
diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp
index 5985ce1..0341ff3 100644
--- a/src/interface/Textbox.cpp
+++ b/src/interface/Textbox.cpp
@@ -16,7 +16,8 @@ Textbox::Textbox(Point position, Point size, std::string textboxText):
textVAlign(AlignMiddle),
textHAlign(AlignCentre),
actionCallback(NULL),
- masked(false)
+ masked(false),
+ border(true)
{
SetText(textboxText);
TextPosition();
@@ -31,11 +32,9 @@ Textbox::~Textbox()
void Textbox::TextPosition()
{
- std::string tempText = displayText;
- if(tempText.length() && cursor)
+ if(cursor)
{
- tempText.erase(cursor, tempText.length()-cursor);
- cursorPosition = Graphics::textwidth((char *)tempText.c_str());
+ cursorPosition = Graphics::textnwidth((char *)displayText.c_str(), cursor);
}
else
{
@@ -71,6 +70,7 @@ void Textbox::TextPosition()
void Textbox::SetText(std::string text)
{
+ cursor = text.length();
if(masked)
{
char tempText[text.length()];
@@ -86,6 +86,13 @@ void Textbox::SetText(std::string text)
TextPosition();
}
+
+void Textbox::SetDisplayText(std::string text)
+{
+ displayText = text;
+ TextPosition();
+}
+
std::string Textbox::GetText()
{
return text;
@@ -153,17 +160,18 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
cursor++;
changed = true;
}
- if(changed && actionCallback)
- {
- actionCallback->TextChangedCallback(this);
- }
}
catch(std::out_of_range &e)
{
cursor = 0;
text = "";
}
- SetText(text);
+ if(changed)
+ {
+ SetText(text);
+ if(actionCallback)
+ actionCallback->TextChangedCallback(this);
+ }
TextPosition();
}
@@ -172,12 +180,12 @@ void Textbox::Draw(const Point& screenPos)
Graphics * g = Engine::Ref().g;
if(IsFocused())
{
- g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
+ if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->draw_line(screenPos.X+textPosition.X+cursorPosition, screenPos.Y+3, screenPos.X+textPosition.X+cursorPosition, screenPos.Y+12, 255, 255, 255, XRES+BARSIZE);
}
else
{
- g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
+ if(border) g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
}
g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, displayText, 255, 255, 255, 255);
}
diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h
index 889a4e8..c29e01b 100644
--- a/src/interface/Textbox.h
+++ b/src/interface/Textbox.h
@@ -17,6 +17,7 @@ public:
};
class Textbox : public Component
{
+ friend class TextboxAction;
protected:
std::string text;
std::string displayText;
@@ -26,12 +27,14 @@ protected:
int cursor, cursorPosition;
TextboxAction *actionCallback;
bool masked;
+ bool border;
public:
Textbox(Point position, Point size, std::string textboxText);
virtual ~Textbox();
virtual void TextPosition();
virtual void SetText(std::string text);
+ virtual void SetDisplayText(std::string text);
std::string GetText();
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
@@ -42,6 +45,8 @@ public:
void SetHidden(bool hidden) { masked = hidden; }
bool GetHidden() { return masked; }
+ void SetBorder(bool border) {this->border = border;}
+
virtual void Draw(const Point& screenPos);
};
}
diff --git a/src/login/LoginView.cpp b/src/login/LoginView.cpp
index e73d014..1ae9ef8 100644
--- a/src/login/LoginView.cpp
+++ b/src/login/LoginView.cpp
@@ -55,6 +55,24 @@ LoginView::LoginView():
AddComponent(infoLabel);
}
+void LoginView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
+{
+ switch(key)
+ {
+ case KEY_TAB:
+ if(IsFocused(usernameField))
+ FocusComponent(passwordField);
+ else
+ FocusComponent(usernameField);
+ break;
+ case KEY_ENTER:
+ case KEY_RETURN:
+ if(IsFocused(passwordField))
+ loginButton->DoAction();
+ break;
+ }
+}
+
void LoginView::NotifyStatusChanged(LoginModel * sender)
{
infoLabel->SetText(sender->GetStatusText());
diff --git a/src/login/LoginView.h b/src/login/LoginView.h
index 1099278..7ae5393 100644
--- a/src/login/LoginView.h
+++ b/src/login/LoginView.h
@@ -29,6 +29,7 @@ public:
class LoginAction;
class CancelAction;
LoginView();
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void AttachController(LoginController * c_) { c = c_; }
void NotifyStatusChanged(LoginModel * sender);
virtual void OnDraw();
diff --git a/src/preview/PreviewController.cpp b/src/preview/PreviewController.cpp
index 558d705..558ccb4 100644
--- a/src/preview/PreviewController.cpp
+++ b/src/preview/PreviewController.cpp
@@ -5,6 +5,7 @@
* Author: Simon
*/
+#include <sstream>
#include "PreviewController.h"
#include "PreviewView.h"
#include "PreviewModel.h"
@@ -43,6 +44,16 @@ void PreviewController::DoOpen()
previewModel->SetDoOpen(true);
}
+void PreviewController::OpenInBrowser()
+{
+ if(previewModel->GetSave())
+ {
+ std::stringstream uriStream;
+ uriStream << "http://" << SERVER << "/Browse/View.html?ID=" << previewModel->GetSave()->id;
+ OpenURI(uriStream.str());
+ }
+}
+
void PreviewController::Exit()
{
if(ui::Engine::Ref().GetWindow() == previewView)
diff --git a/src/preview/PreviewController.h b/src/preview/PreviewController.h
index e9b0fb5..595a30f 100644
--- a/src/preview/PreviewController.h
+++ b/src/preview/PreviewController.h
@@ -24,6 +24,7 @@ public:
PreviewController(int saveID, ControllerCallback * callback);
void Exit();
void DoOpen();
+ void OpenInBrowser();
bool GetDoOpen();
Save * GetSave();
PreviewView * GetView() { return previewView; }
diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp
index 68a224b..3a731dc 100644
--- a/src/preview/PreviewView.cpp
+++ b/src/preview/PreviewView.cpp
@@ -25,12 +25,28 @@ PreviewView::PreviewView():
v->c->Exit();
}
};
- openButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(75, 16), "Open");
+ openButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(50, 16), "Open");
openButton->SetAlignment(AlignLeft, AlignMiddle);
openButton->SetIcon(IconOpen);
openButton->SetActionCallback(new OpenAction(this));
AddComponent(openButton);
+ class BrowserOpenAction: public ui::ButtonAction
+ {
+ PreviewView * v;
+ public:
+ BrowserOpenAction(PreviewView * v_){ v = v_; }
+ virtual void ActionCallback(ui::Button * sender)
+ {
+ v->c->OpenInBrowser();
+ }
+ };
+ browserOpenButton = new ui::Button(ui::Point((XRES/2)-90, Size.Y-16), ui::Point(90, 16), "Open in browser");
+ browserOpenButton->SetAlignment(AlignLeft, AlignMiddle);
+ browserOpenButton->SetIcon(IconOpen);
+ browserOpenButton->SetActionCallback(new BrowserOpenAction(this));
+ AddComponent(browserOpenButton);
+
saveNameLabel = new ui::Label(ui::Point(5, (YRES/2)+15), ui::Point(100, 16), "");
saveNameLabel->SetAlignment(AlignLeft, AlignBottom);
AddComponent(saveNameLabel);
diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h
index dbc3248..9368732 100644
--- a/src/preview/PreviewView.h
+++ b/src/preview/PreviewView.h
@@ -20,6 +20,7 @@ class PreviewView: public ui::Window {
PreviewController * c;
Thumbnail * savePreview;
ui::Button * openButton;
+ ui::Button * browserOpenButton;
ui::Label * saveNameLabel;
ui::Label * authorDateLabel;
int votesUp;