summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-30 00:40:28 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-30 00:40:28 (GMT)
commit259fc2bcf75d754af043a5d3fa39b6ee0c0b1dec (patch)
treef0fe2c14499345121371bba0ecc3fe21d17e0953 /src
parentfe329e9127ebcb8c89c505c4c120e175810d280c (diff)
downloadpowder-259fc2bcf75d754af043a5d3fa39b6ee0c0b1dec.zip
powder-259fc2bcf75d754af043a5d3fa39b6ee0c0b1dec.tar.gz
ASCII for key events, save and Textarea (no caret, yet)
Diffstat (limited to 'src')
-rw-r--r--src/Config.h1
-rw-r--r--src/PowderToy.cpp4
-rw-r--r--src/client/Client.cpp55
-rw-r--r--src/client/Client.h1
-rw-r--r--src/game/GameController.cpp42
-rw-r--r--src/game/GameController.h3
-rw-r--r--src/game/GameView.cpp6
-rw-r--r--src/game/GameView.h8
-rw-r--r--src/interface/Component.cpp4
-rw-r--r--src/interface/Component.h8
-rw-r--r--src/interface/Engine.cpp8
-rw-r--r--src/interface/Engine.h4
-rw-r--r--src/interface/Keys.h5
-rw-r--r--src/interface/Panel.cpp12
-rw-r--r--src/interface/Panel.h12
-rw-r--r--src/interface/Textarea.cpp80
-rw-r--r--src/interface/Textarea.h33
-rw-r--r--src/interface/Textbox.cpp54
-rw-r--r--src/interface/Textbox.h9
-rw-r--r--src/interface/Window.cpp12
-rw-r--r--src/interface/Window.h8
-rw-r--r--src/search/SearchController.cpp14
-rw-r--r--src/search/SearchController.h4
-rw-r--r--src/simulation/Gravity.cpp1
-rw-r--r--src/ssave/SSaveController.cpp55
-rw-r--r--src/ssave/SSaveController.h34
-rw-r--r--src/ssave/SSaveModel.cpp85
-rw-r--r--src/ssave/SSaveModel.h36
-rw-r--r--src/ssave/SSaveView.cpp92
-rw-r--r--src/ssave/SSaveView.h39
30 files changed, 636 insertions, 93 deletions
diff --git a/src/Config.h b/src/Config.h
index b113143..436e8a2 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -148,4 +148,5 @@ extern unsigned char ZSIZE;
#define DEBUG_PERFORMANCE_CALC 0x0008
#define DEBUG_PERFORMANCE_FRAME 0x0010
+#include "interface/Keys.h"
//#endif /* CONFIG_H_ */
diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp
index 514fe9b..a64270f 100644
--- a/src/PowderToy.cpp
+++ b/src/PowderToy.cpp
@@ -118,10 +118,10 @@ int main(int argc, char * argv[])
engine->Exit();
break;
case SDL_KEYDOWN:
- engine->onKeyPress(event.key.keysym.unicode, false, false, false);
+ engine->onKeyPress(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_SHIFT, event.key.keysym.mod&KEY_MOD_CONTROL, event.key.keysym.mod&KEY_MOD_ALT);
break;
case SDL_KEYUP:
- engine->onKeyRelease(event.key.keysym.unicode, false, false, false);
+ engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_SHIFT, event.key.keysym.mod&KEY_MOD_CONTROL, event.key.keysym.mod&KEY_MOD_ALT);
break;
case SDL_MOUSEMOTION:
engine->onMouseMove(event.motion.x, event.motion.y);
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 9b8b9c2..75b78c5 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -51,6 +51,60 @@ User Client::GetAuthUser()
return authUser;
}
+RequestStatus Client::UploadSave(Save * save)
+{
+ lastError = "";
+ int dataStatus;
+ char * data;
+ int dataLength = 0;
+ std::stringstream userIDStream;
+ userIDStream << authUser.ID;
+ if(authUser.ID)
+ {
+ char * postNames[] = { "Name", "Description", "Data:save.bin", "Publish", NULL };
+ char * postDatas[] = { (char *)(save->name.c_str()), (char *)(save->Description.c_str()), (char *)(save->GetData()), (char *)(save->Published?"Public":"Private") };
+ int postLengths[] = { save->name.length(), save->Description.length(), save->GetDataLength(), save->Published?6:7 };
+ //std::cout << postNames[0] << " " << postDatas[0] << " " << postLengths[0] << std::endl;
+ data = http_multipart_post("http://" SERVER "/Save.api", postNames, postDatas, postLengths, (char *)(userIDStream.str().c_str()), NULL, (char *)(authUser.SessionID.c_str()), &dataStatus, &dataLength);
+ }
+ else
+ {
+ lastError = "Not authenticated";
+ return RequestFailure;
+ }
+ if(data && dataStatus == 200)
+ {
+ if(strncmp((const char *)data, "OK", 2)!=0)
+ {
+ free(data);
+ lastError = std::string((const char *)data);
+ return RequestFailure;
+ }
+ else
+ {
+ int tempID;
+ std::stringstream saveIDStream((char *)(data+3));
+ saveIDStream >> tempID;
+ if(!tempID)
+ {
+ lastError = "Server did not return Save ID";
+ return RequestFailure;
+ }
+ else
+ {
+ save->id = tempID;
+ }
+ }
+ free(data);
+ return RequestOkay;
+ }
+ else if(data)
+ {
+ free(data);
+ }
+ return RequestFailure;
+}
+
RequestStatus Client::ExecVote(int saveID, int direction)
{
lastError = "";
@@ -83,7 +137,6 @@ RequestStatus Client::ExecVote(int saveID, int direction)
lastError = "Not authenticated";
return RequestFailure;
}
- std::cout << data << std::endl;
if(data && dataStatus == 200)
{
if(strncmp((const char *)data, "OK", 2)!=0)
diff --git a/src/client/Client.h b/src/client/Client.h
index 89c4e64..0587d79 100644
--- a/src/client/Client.h
+++ b/src/client/Client.h
@@ -38,6 +38,7 @@ public:
~Client();
RequestStatus ExecVote(int saveID, int direction);
+ RequestStatus UploadSave(Save * save);
unsigned char * GetSaveData(int saveID, int saveDate, int & dataLength);
LoginStatus Login(string username, string password, User & user);
diff --git a/src/game/GameController.cpp b/src/game/GameController.cpp
index 65556db..fd3572f 100644
--- a/src/game/GameController.cpp
+++ b/src/game/GameController.cpp
@@ -9,6 +9,7 @@
#include "render/RenderController.h"
#include "login/LoginController.h"
#include "interface/Point.h"
+#include "dialogues/ErrorMessage.h"
using namespace std;
@@ -50,10 +51,26 @@ public:
}
};
+class GameController::SSaveCallback: public ControllerCallback
+{
+ GameController * cc;
+public:
+ SSaveCallback(GameController * cc_) { cc = cc_; }
+ virtual void ControllerExit()
+ {
+ if(cc->ssave->GetSaveUploaded())
+ {
+ cc->gameModel->SetSave(new Save(*(cc->ssave->GetSave())));
+ }
+ //cc->gameModel->SetUser(cc->loginWindow->GetUser());
+ }
+};
+
GameController::GameController():
search(NULL),
renderOptions(NULL),
- loginWindow(NULL)
+ loginWindow(NULL),
+ ssave(NULL)
{
gameView = new GameView();
gameModel = new GameModel();
@@ -267,7 +284,28 @@ void GameController::OpenRenderOptions()
void GameController::OpenSaveWindow()
{
- //TODO: Implement
+ if(gameModel->GetUser().ID)
+ {
+ if(gameModel->GetSave())
+ {
+ Save tempSave(*gameModel->GetSave());
+ int tempSaveLength;
+ tempSave.SetData(gameModel->GetSimulation()->Save(tempSaveLength));
+ ssave = new SSaveController(new SSaveCallback(this), tempSave);
+ }
+ else
+ {
+ Save tempSave(0, 0, 0, 0, gameModel->GetUser().Username, "");
+ int tempSaveLength;
+ tempSave.SetData(gameModel->GetSimulation()->Save(tempSaveLength));
+ ssave = new SSaveController(new SSaveCallback(this), tempSave);
+ }
+ ui::Engine::Ref().ShowWindow(ssave->GetView());
+ }
+ else
+ {
+ new ErrorMessage("Error", "You need to login to upload saves.");
+ }
}
void GameController::Vote(int direction)
diff --git a/src/game/GameController.h b/src/game/GameController.h
index 7498e47..43c9bb2 100644
--- a/src/game/GameController.h
+++ b/src/game/GameController.h
@@ -9,6 +9,7 @@
#include "search/SearchController.h"
#include "render/RenderController.h"
#include "login/LoginController.h"
+#include "ssave/SSaveController.h"
#include "Menu.h"
using namespace std;
@@ -24,10 +25,12 @@ private:
SearchController * search;
RenderController * renderOptions;
LoginController * loginWindow;
+ SSaveController * ssave;
public:
class LoginCallback;
class SearchCallback;
class RenderCallback;
+ class SSaveCallback;
GameController();
~GameController();
GameView * GetView();
diff --git a/src/game/GameView.cpp b/src/game/GameView.cpp
index 9a2eb2d..560b1d5 100644
--- a/src/game/GameView.cpp
+++ b/src/game/GameView.cpp
@@ -340,6 +340,7 @@ void GameView::NotifySaveChanged(GameModel * sender)
{
if(sender->GetSave())
{
+ saveSimulationButton->SetText(sender->GetSave()->GetName());
reloadButton->Enabled = true;
upVoteButton->Enabled = (sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==0);
if(sender->GetSave()->GetID() && sender->GetUser().ID && sender->GetSave()->GetVote()==1)
@@ -355,6 +356,7 @@ void GameView::NotifySaveChanged(GameModel * sender)
}
else
{
+ saveSimulationButton->SetText("");
reloadButton->Enabled = false;
upVoteButton->Enabled = false;
upVoteButton->SetBackgroundColour(ui::Colour(0, 0, 0));
@@ -420,7 +422,7 @@ void GameView::OnMouseWheel(int x, int y, int d)
}
}
-void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+void GameView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
switch(key)
{
@@ -438,7 +440,7 @@ void GameView::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
}
}
-void GameView::OnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void GameView::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
//switch(key)
//{
diff --git a/src/game/GameView.h b/src/game/GameView.h
index 610b953..1cd69c0 100644
--- a/src/game/GameView.h
+++ b/src/game/GameView.h
@@ -56,10 +56,10 @@ public:
virtual void OnMouseDown(int x, int y, unsigned button);
virtual void OnMouseUp(int x, int y, unsigned button);
virtual void OnMouseWheel(int x, int y, int d);
- virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
- virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
- //virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {}
- //virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {}
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ //virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
+ //virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
virtual void OnTick(float dt);
virtual void OnDraw();
class MenuAction;
diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp
index 75e9c40..0efc2b1 100644
--- a/src/interface/Component.cpp
+++ b/src/interface/Component.cpp
@@ -94,11 +94,11 @@ void Component::Tick(float dt)
{
}
-void Component::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Component::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
}
-void Component::OnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Component::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
}
diff --git a/src/interface/Component.h b/src/interface/Component.h
index 578aba6..2735736 100644
--- a/src/interface/Component.h
+++ b/src/interface/Component.h
@@ -56,8 +56,8 @@ namespace ui
void OnMouseUnclick(int localx, int localy, unsigned int button);
void OnMouseWheel(int localx, int localy, int d);
void OnMouseWheelInside(int localx, int localy, int d);
- void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
- void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
*/
///
@@ -185,7 +185,7 @@ namespace ui
// ctrl: Control key is down.
// alt: Alternate key is down.
///
- virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
///
// Called: When a key is released.
@@ -195,7 +195,7 @@ namespace ui
// ctrl: Control key is released.
// alt: Alternate key is released.
///
- virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
private:
Window* parentstate_;
diff --git a/src/interface/Engine.cpp b/src/interface/Engine.cpp
index a822079..01a3e72 100644
--- a/src/interface/Engine.cpp
+++ b/src/interface/Engine.cpp
@@ -178,16 +178,16 @@ void Engine::Draw()
g->Blit();
}
-void Engine::onKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Engine::onKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
if(state_)
- state_->DoKeyPress(key, shift, ctrl, alt);
+ state_->DoKeyPress(key, character, shift, ctrl, alt);
}
-void Engine::onKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Engine::onKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
if(state_)
- state_->DoKeyRelease(key, shift, ctrl, alt);
+ state_->DoKeyRelease(key, character, shift, ctrl, alt);
}
void Engine::onMouseClick(int x, int y, unsigned button)
diff --git a/src/interface/Engine.h b/src/interface/Engine.h
index 8f599b7..09fc222 100644
--- a/src/interface/Engine.h
+++ b/src/interface/Engine.h
@@ -29,8 +29,8 @@ namespace ui
void onMouseClick(int x, int y, unsigned button);
void onMouseUnclick(int x, int y, unsigned button);
void onMouseWheel(int x, int y, int delta);
- void onKeyPress(int key, bool shift, bool ctrl, bool alt);
- void onKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ void onKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void onKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void onResize(int newWidth, int newHeight);
void onClose();
diff --git a/src/interface/Keys.h b/src/interface/Keys.h
index 22fffa2..c0194ad 100644
--- a/src/interface/Keys.h
+++ b/src/interface/Keys.h
@@ -7,3 +7,8 @@
#define KEY_BACKSPACE SDLK_BACKSPACE
#define KEY_DELETE SDLK_DELETE
#define KEY_TAB SDLK_TAB
+
+#define KEY_MOD_CONTROL KMOD_CTRL
+#define KEY_MOD_ALT KMOD_ALT
+#define KEY_MOD_SHIFT KMOD_SHIFT
+
diff --git a/src/interface/Panel.cpp b/src/interface/Panel.cpp
index acfcf53..d2de97e 100644
--- a/src/interface/Panel.cpp
+++ b/src/interface/Panel.cpp
@@ -116,14 +116,14 @@ void Panel::Tick(float dt)
children[i]->Tick(dt);
}
-void Panel::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Panel::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
- XOnKeyPress(key, shift, ctrl, alt);
+ XOnKeyPress(key, character, shift, ctrl, alt);
}
-void Panel::OnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Panel::OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
- XOnKeyRelease(key, shift, ctrl, alt);
+ XOnKeyRelease(key, character, shift, ctrl, alt);
}
void Panel::OnMouseClick(int localx, int localy, unsigned button)
@@ -339,11 +339,11 @@ void Panel::XTick(float dt)
{
}
-void Panel::XOnKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Panel::XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
}
-void Panel::XOnKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Panel::XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
}
diff --git a/src/interface/Panel.h b/src/interface/Panel.h
index 51f52aa..a5d80e3 100644
--- a/src/interface/Panel.h
+++ b/src/interface/Panel.h
@@ -59,8 +59,8 @@ class Component;
void OnMouseUnclick(int localx, int localy, unsigned button);
void OnMouseWheel(int localx, int localy, int d);
void OnMouseWheelInside(int localx, int localy, int d);
- void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
- void OnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
protected:
// child components
@@ -82,8 +82,8 @@ class Component;
void XOnMouseUnclick(int localx, int localy, unsigned int button);
void XOnMouseWheel(int localx, int localy, int d);
void XOnMouseWheelInside(int localx, int localy, int d);
- void XOnKeyPress(int key, bool shift, bool ctrl, bool alt);
- void XOnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ void XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ void XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
*/
// Overridable. Called by XComponent::Tick()
@@ -127,10 +127,10 @@ class Component;
virtual void XOnMouseWheelInside(int localx, int localy, int d);
// Overridable. Called by XComponent::OnKeyPress()
- virtual void XOnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ virtual void XOnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
// Overridable. Called by XComponent::OnKeyRelease()
- virtual void XOnKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ virtual void XOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
};
}
diff --git a/src/interface/Textarea.cpp b/src/interface/Textarea.cpp
new file mode 100644
index 0000000..fecc390
--- /dev/null
+++ b/src/interface/Textarea.cpp
@@ -0,0 +1,80 @@
+/*
+ * Textarea.cpp
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#include <iostream>
+#include "Textarea.h"
+
+using namespace ui;
+
+Textarea::Textarea(Point position, Point size, std::string textboxText):
+ Textbox(position, size, textboxText)
+{
+ updateMultiline();
+}
+
+void Textarea::SetText(std::string text)
+{
+ this->text = text;
+ updateMultiline();
+}
+
+void Textarea::updateMultiline()
+{
+ char * rawText = (char*)malloc(text.length()+1);
+ memcpy(rawText, text.c_str(), text.length());
+ rawText[text.length()] = 0;
+
+ int currentWidth = 0;
+ char * lastSpace = NULL;
+ char * currentWord = rawText;
+ char * nextSpace;
+ while(true)
+ {
+ nextSpace = strchr(currentWord+1, ' ');
+ if(nextSpace)
+ nextSpace[0] = 0;
+ int width = Graphics::textwidth(currentWord);
+ if(width+currentWidth > Size.X-6)
+ {
+ currentWidth = width;
+ currentWord[0] = '\n';
+ }
+ else
+ currentWidth += width;
+ if(nextSpace)
+ nextSpace[0] = ' ';
+ if(!(currentWord = strchr(currentWord+1, ' ')))
+ break;
+ }
+ textLines = rawText;
+}
+
+void Textarea::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
+{
+ Textbox::OnKeyPress(key, character, shift, ctrl, alt);
+ updateMultiline();
+}
+
+void Textarea::Draw(const Point &screenPos)
+{
+ Graphics * g = ui::Engine::Ref().g;
+ if(IsFocused())
+ {
+ g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 255, 255, 255, 255);
+ g->drawtext(screenPos.X+3, screenPos.Y+3, textLines, 255, 255, 255, 255);
+ }
+ else
+ {
+ g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, 160, 160, 160, 255);
+ g->drawtext(screenPos.X+3, screenPos.Y+3, textLines, 160, 160, 160, 255);
+ }
+}
+
+Textarea::~Textarea() {
+ // TODO Auto-generated destructor stub
+}
+
diff --git a/src/interface/Textarea.h b/src/interface/Textarea.h
new file mode 100644
index 0000000..c5529d6
--- /dev/null
+++ b/src/interface/Textarea.h
@@ -0,0 +1,33 @@
+/*
+ * Textarea.h
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#ifndef TEXTAREA_H_
+#define TEXTAREA_H_
+
+#include <vector>
+#include <string>
+#include <sstream>
+#include "Textbox.h"
+
+namespace ui
+{
+
+class Textarea: public ui::Textbox
+{
+ void updateMultiline();
+ std::string textLines;
+public:
+ Textarea(Point position, Point size, std::string textboxText);
+ virtual void TextPosition() {}
+ virtual void SetText(std::string text);
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ virtual void Draw(const Point& screenPos);
+ virtual ~Textarea();
+};
+}
+
+#endif /* TEXTAREA_H_ */
diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp
index 328ccb5..1a54992 100644
--- a/src/interface/Textbox.cpp
+++ b/src/interface/Textbox.cpp
@@ -9,19 +9,6 @@
using namespace ui;
-Textbox::Textbox(Window* parent_state, std::string textboxText):
- Component(parent_state),
- text(textboxText),
- textPosition(ui::Point(0, 0)),
- textVAlign(AlignMiddle),
- textHAlign(AlignCentre),
- actionCallback(NULL),
- masked(false)
-{
- TextPosition();
- cursor = text.length();
-}
-
Textbox::Textbox(Point position, Point size, std::string textboxText):
Component(position, size),
text(textboxText),
@@ -35,19 +22,6 @@ Textbox::Textbox(Point position, Point size, std::string textboxText):
cursor = text.length();
}
-Textbox::Textbox(std::string textboxText):
- Component(),
- text(textboxText),
- textPosition(ui::Point(0, 0)),
- textVAlign(AlignMiddle),
- textHAlign(AlignCentre),
- actionCallback(NULL),
- masked(false)
-{
- TextPosition();
- cursor = text.length();
-}
-
Textbox::~Textbox()
{
if(actionCallback)
@@ -105,7 +79,7 @@ std::string Textbox::GetText()
return text;
}
-void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
bool changed = false;
try
@@ -152,22 +126,20 @@ void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
changed = true;
}
break;
- default:
- if(key >= ' ' && key < 127)
+ }
+ if(character >= ' ' && character < 127)
+ {
+ if(cursor == text.length())
{
- if(cursor == text.length())
- {
- text += key;
- //std::cout << key << std::endl;
- }
- else
- {
- text.insert(cursor, 1, (char)key);
- }
- cursor++;
- changed = true;
+ text += character;
+ //std::cout << key << std::endl;
}
- break;
+ else
+ {
+ text.insert(cursor, 1, (char)character);
+ }
+ cursor++;
+ changed = true;
}
if(changed && actionCallback)
{
diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h
index 82ed648..ac138f4 100644
--- a/src/interface/Textbox.h
+++ b/src/interface/Textbox.h
@@ -17,6 +17,7 @@ public:
};
class Textbox : public Component
{
+protected:
std::string text;
ui::Point textPosition;
HorizontalAlignment textHAlign;
@@ -25,19 +26,17 @@ class Textbox : public Component
TextboxAction *actionCallback;
bool masked;
public:
- Textbox(Window* parent_state, std::string textboxText);
Textbox(Point position, Point size, std::string textboxText);
- Textbox(std::string textboxText);
virtual ~Textbox();
- void TextPosition();
- void SetText(std::string text);
+ virtual void TextPosition();
+ virtual void SetText(std::string text);
std::string GetText();
HorizontalAlignment GetHAlignment() { return textHAlign; }
VerticalAlignment GetVAlignment() { return textVAlign; }
void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
void SetActionCallback(TextboxAction * action) { actionCallback = action; }
- virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt);
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
void SetHidden(bool hidden) { masked = hidden; }
bool GetHidden() { return masked; }
diff --git a/src/interface/Window.cpp b/src/interface/Window.cpp
index b399ab8..5b603b9 100644
--- a/src/interface/Window.cpp
+++ b/src/interface/Window.cpp
@@ -140,28 +140,28 @@ void Window::DoTick(float dt)
OnTick(dt);
}
-void Window::DoKeyPress(int key, bool shift, bool ctrl, bool alt)
+void Window::DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
//on key press
if(focusedComponent_ != NULL)
{
if(!focusedComponent_->Locked)
- focusedComponent_->OnKeyPress(key, shift, ctrl, alt);
+ focusedComponent_->OnKeyPress(key, character, shift, ctrl, alt);
}
- OnKeyPress(key, shift, ctrl, alt);
+ OnKeyPress(key, character, shift, ctrl, alt);
}
-void Window::DoKeyRelease(int key, bool shift, bool ctrl, bool alt)
+void Window::DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt)
{
//on key unpress
if(focusedComponent_ != NULL)
{
if(!focusedComponent_->Locked)
- focusedComponent_->OnKeyRelease(key, shift, ctrl, alt);
+ focusedComponent_->OnKeyRelease(key, character, shift, ctrl, alt);
}
- OnKeyRelease(key, shift, ctrl, alt);
+ OnKeyRelease(key, character, shift, ctrl, alt);
}
void Window::DoMouseDown(int x_, int y_, unsigned button)
diff --git a/src/interface/Window.h b/src/interface/Window.h
index 4fc2b14..5fb5eec 100644
--- a/src/interface/Window.h
+++ b/src/interface/Window.h
@@ -55,8 +55,8 @@ enum ChromeStyle
virtual void DoMouseDown(int x, int y, unsigned button);
virtual void DoMouseUp(int x, int y, unsigned button);
virtual void DoMouseWheel(int x, int y, int d);
- virtual void DoKeyPress(int key, bool shift, bool ctrl, bool alt);
- virtual void DoKeyRelease(int key, bool shift, bool ctrl, bool alt);
+ virtual void DoKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
+ virtual void DoKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
bool IsFocused(const Component* c) const;
void FocusComponent(Component* c);
@@ -73,8 +73,8 @@ enum ChromeStyle
virtual void OnMouseDown(int x, int y, unsigned button) {}
virtual void OnMouseUp(int x, int y, unsigned button) {}
virtual void OnMouseWheel(int x, int y, int d) {}
- virtual void OnKeyPress(int key, bool shift, bool ctrl, bool alt) {}
- virtual void OnKeyRelease(int key, bool shift, bool ctrl, bool alt) {}
+ virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
+ virtual void OnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt) {}
std::vector<Component*> Components;
Component* focusedComponent_;
ChromeStyle chrome;
diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp
index efd33ac..6fa9748 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -21,7 +21,9 @@ public:
SearchController::SearchController(ControllerCallback * callback):
activePreview(NULL),
- HasExited(false)
+ HasExited(false),
+ nextQueryTime(0.0f),
+ nextQueryDone(true)
{
searchModel = new SearchModel();
searchView = new SearchView();
@@ -43,6 +45,11 @@ Save * SearchController::GetLoadedSave()
void SearchController::Update()
{
+ if(!nextQueryDone && nextQueryTime < clock())
+ {
+ nextQueryDone = true;
+ searchModel->UpdateSaveList(1, nextQuery);
+ }
searchModel->Update();
if(activePreview && activePreview->HasExited)
{
@@ -79,7 +86,10 @@ SearchController::~SearchController()
void SearchController::DoSearch(std::string query)
{
- searchModel->UpdateSaveList(1, query);
+ nextQuery = query;
+ nextQueryTime = clock()+(0.6 * CLOCKS_PER_SEC);
+ nextQueryDone = false;
+ //searchModel->UpdateSaveList(1, query);
}
void SearchController::PrevPage()
diff --git a/src/search/SearchController.h b/src/search/SearchController.h
index e015648..cf34830 100644
--- a/src/search/SearchController.h
+++ b/src/search/SearchController.h
@@ -17,6 +17,10 @@ private:
SearchView * searchView;
PreviewController * activePreview;
ControllerCallback * callback;
+
+ double nextQueryTime;
+ std::string nextQuery;
+ bool nextQueryDone;
public:
class OpenCallback;
bool HasExited;
diff --git a/src/simulation/Gravity.cpp b/src/simulation/Gravity.cpp
index 43ca8e6..d2d7295 100644
--- a/src/simulation/Gravity.cpp
+++ b/src/simulation/Gravity.cpp
@@ -124,6 +124,7 @@ void Gravity::gravity_update_async()
void *Gravity::update_grav_async_helper(void * context)
{
((Gravity *)context)->update_grav_async();
+ return NULL;
}
void Gravity::update_grav_async()
diff --git a/src/ssave/SSaveController.cpp b/src/ssave/SSaveController.cpp
new file mode 100644
index 0000000..191a18c
--- /dev/null
+++ b/src/ssave/SSaveController.cpp
@@ -0,0 +1,55 @@
+/*
+ * SSaveController.cpp
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#include "SSaveController.h"
+
+SSaveController::SSaveController(ControllerCallback * callback, Save save):
+ HasExited(false)
+{
+ ssaveView = new SSaveView();
+ ssaveView->AttachController(this);
+ ssaveModel = new SSaveModel();
+ ssaveModel->AddObserver(ssaveView);
+ ssaveModel->SetSave(new Save(save));
+
+ this->callback = callback;
+}
+
+void SSaveController::UploadSave(std::string saveName, std::string saveDescription, bool publish)
+{
+ ssaveModel->UploadSave(saveName, saveDescription, publish);
+}
+
+Save * SSaveController::GetSave()
+{
+ return ssaveModel->GetSave();
+}
+
+bool SSaveController::GetSaveUploaded()
+{
+ return ssaveModel->GetSaveUploaded();
+}
+
+void SSaveController::Update()
+{
+ ssaveModel->Update();
+}
+
+void SSaveController::Exit()
+{
+ if(ui::Engine::Ref().GetWindow() == ssaveView)
+ ui::Engine::Ref().CloseWindow();
+ if(callback)
+ callback->ControllerExit();
+ HasExited = true;
+}
+
+SSaveController::~SSaveController() {
+ if(ui::Engine::Ref().GetWindow() == ssaveView)
+ ui::Engine::Ref().CloseWindow();
+}
+
diff --git a/src/ssave/SSaveController.h b/src/ssave/SSaveController.h
new file mode 100644
index 0000000..d1b2388
--- /dev/null
+++ b/src/ssave/SSaveController.h
@@ -0,0 +1,34 @@
+/*
+ * SSaveController.h
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#ifndef SSAVECONTROLLER_H_
+#define SSAVECONTROLLER_H_
+
+#include "SSaveModel.h"
+#include "SSaveView.h"
+#include "Controller.h"
+#include "search/Save.h"
+
+class SSaveView;
+class SSaveModel;
+class SSaveController {
+ SSaveView * ssaveView;
+ SSaveModel * ssaveModel;
+ ControllerCallback * callback;
+public:
+ bool HasExited;
+ SSaveController(ControllerCallback * callback, Save save);
+ Save * GetSave();
+ bool GetSaveUploaded();
+ void Exit();
+ void Update();
+ void UploadSave(std::string saveName, std::string saveDescription, bool publish);
+ SSaveView * GetView() { return ssaveView; }
+ virtual ~SSaveController();
+};
+
+#endif /* SSAVECONTROLLER_H_ */
diff --git a/src/ssave/SSaveModel.cpp b/src/ssave/SSaveModel.cpp
new file mode 100644
index 0000000..820c185
--- /dev/null
+++ b/src/ssave/SSaveModel.cpp
@@ -0,0 +1,85 @@
+/*
+ * SSaveModel.cpp
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#include "SSaveModel.h"
+#include "client/Client.h"
+
+SSaveModel::SSaveModel():
+ save(NULL),
+ saveUploaded(false)
+{
+ // TODO Auto-generated constructor stub
+
+}
+
+void SSaveModel::notifySaveChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifySaveChanged(this);
+ }
+}
+
+void SSaveModel::notifySaveUploadChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ observers[i]->NotifySaveUploadChanged(this);
+ }
+}
+
+void SSaveModel::UploadSave(std::string saveName, std::string saveDescription, bool publish)
+{
+ save->name = saveName;
+ save->Description = saveDescription;
+ save->Published = publish;
+ saveUploaded = false;
+ notifySaveUploadChanged();
+
+ if(Client::Ref().UploadSave(save) == RequestOkay)
+ {
+ saveUploaded = true;
+ }
+ else
+ {
+ saveUploaded = false;
+ }
+ notifySaveUploadChanged();
+}
+
+void SSaveModel::SetSave(Save * save)
+{
+ this->save = save;
+ notifySaveChanged();
+}
+
+Save * SSaveModel::GetSave()
+{
+ return this->save;
+}
+
+bool SSaveModel::GetSaveUploaded()
+{
+ return saveUploaded;
+}
+
+void SSaveModel::AddObserver(SSaveView * observer)
+{
+ observers.push_back(observer);
+ observer->NotifySaveChanged(this);
+ observer->NotifySaveUploadChanged(this);
+}
+
+void SSaveModel::Update()
+{
+
+}
+
+SSaveModel::~SSaveModel() {
+ delete save;
+}
+
diff --git a/src/ssave/SSaveModel.h b/src/ssave/SSaveModel.h
new file mode 100644
index 0000000..b2631bb
--- /dev/null
+++ b/src/ssave/SSaveModel.h
@@ -0,0 +1,36 @@
+/*
+ * SSaveModel.h
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#ifndef SSAVEMODEL_H_
+#define SSAVEMODEL_H_
+
+#include <vector>
+
+#include "SSaveView.h"
+#include "search/Save.h"
+
+using namespace std;
+
+class SSaveView;
+class SSaveModel {
+ vector<SSaveView*> observers;
+ Save * save;
+ void notifySaveChanged();
+ void notifySaveUploadChanged();
+ bool saveUploaded;
+public:
+ SSaveModel();
+ void AddObserver(SSaveView * observer);
+ void Update();
+ Save * GetSave();
+ void SetSave(Save * save);
+ void UploadSave(std::string saveName, std::string saveDescription, bool publish);
+ bool GetSaveUploaded();
+ virtual ~SSaveModel();
+};
+
+#endif /* SSAVEMODEL_H_ */
diff --git a/src/ssave/SSaveView.cpp b/src/ssave/SSaveView.cpp
new file mode 100644
index 0000000..8a4a482
--- /dev/null
+++ b/src/ssave/SSaveView.cpp
@@ -0,0 +1,92 @@
+/*
+ * SSaveView.cpp
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#include "SSaveView.h"
+
+SSaveView::SSaveView():
+ ui::Window(ui::Point(-1, -1), ui::Point(200, 200)),
+ publishCheckbox(NULL),
+ saveButton(NULL),
+ closeButton(NULL),
+ nameField(NULL),
+ titleLabel(NULL),
+ descriptionField(NULL)
+{
+ titleLabel = new ui::Label(ui::Point(2, 1), ui::Point(Size.X-4, 16), "Save to Server");
+ titleLabel->SetAlignment(AlignLeft, AlignBottom);
+ AddComponent(titleLabel);
+
+ nameField = new ui::Textbox(ui::Point(4, 18), ui::Point(Size.X-8, 16), "");
+ nameField->SetAlignment(AlignLeft, AlignBottom);
+ AddComponent(nameField);
+
+ descriptionField = new ui::Textarea(ui::Point(4, 54), ui::Point(Size.X-8, Size.Y-26-54), "");
+ AddComponent(descriptionField);
+
+ publishCheckbox = new ui::Checkbox(ui::Point(4, 36), ui::Point(Size.X-8, 16), "Publish");
+ AddComponent(publishCheckbox);
+
+ class CloseAction: public ui::ButtonAction
+ {
+ SSaveView * v;
+ public:
+ CloseAction(SSaveView * v_) { v = v_; };
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->Exit();
+ }
+ };
+ closeButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(50, 16), "Cancel");
+ closeButton->SetActionCallback(new CloseAction(this));
+ AddComponent(closeButton);
+
+ class SaveAction: public ui::ButtonAction
+ {
+ SSaveView * v;
+ public:
+ SaveAction(SSaveView * v_) { v = v_; };
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->UploadSave(v->nameField->GetText(), "", v->publishCheckbox->GetChecked());
+ }
+ };
+ saveButton = new ui::Button(ui::Point(Size.X-50, Size.Y-16), ui::Point(50, 16), "Save");
+ saveButton->SetActionCallback(new SaveAction(this));
+ AddComponent(saveButton);
+}
+
+void SSaveView::NotifySaveChanged(SSaveModel * sender)
+{
+ if(sender->GetSave())
+ {
+ nameField->SetText(sender->GetSave()->GetName());
+ publishCheckbox->SetChecked(sender->GetSave()->Published);
+ }
+ else
+ {
+ nameField->SetText("");
+ //publishCheckbox->SetChecked(sender->GetSave()->GetPublished());
+ }
+}
+
+void SSaveView::NotifySaveUploadChanged(SSaveModel * sender)
+{
+ if(sender->GetSaveUploaded())
+ c->Exit();
+}
+
+void SSaveView::OnDraw()
+{
+ Graphics * g = ui::Engine::Ref().g;
+
+ g->clearrect(Position.X, Position.Y, Size.X, Size.Y);
+ g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 200, 200, 200, 255);
+}
+
+SSaveView::~SSaveView() {
+}
+
diff --git a/src/ssave/SSaveView.h b/src/ssave/SSaveView.h
new file mode 100644
index 0000000..f8e051d
--- /dev/null
+++ b/src/ssave/SSaveView.h
@@ -0,0 +1,39 @@
+/*
+ * SSaveView.h
+ *
+ * Created on: Jan 29, 2012
+ * Author: Simon
+ */
+
+#ifndef SSAVEVIEW_H_
+#define SSAVEVIEW_H_
+
+#include "interface/Window.h"
+#include "interface/Label.h"
+#include "interface/Button.h"
+#include "interface/Textbox.h"
+#include "interface/Textarea.h"
+#include "interface/Checkbox.h"
+#include "SSaveModel.h"
+#include "SSaveController.h"
+
+class SSaveController;
+class SSaveModel;
+class SSaveView: public ui::Window {
+ SSaveController * c;
+ ui::Checkbox * publishCheckbox;
+ ui::Button * saveButton;
+ ui::Button * closeButton;
+ ui::Textbox * nameField;
+ ui::Label * titleLabel;
+ ui::Textarea * descriptionField;
+public:
+ SSaveView();
+ void AttachController(SSaveController * c_) { c = c_; };
+ void NotifySaveChanged(SSaveModel * sender);
+ void NotifySaveUploadChanged(SSaveModel * sender);
+ virtual void OnDraw();
+ virtual ~SSaveView();
+};
+
+#endif /* SSAVEVIEW_H_ */