summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-20 22:07:49 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-20 22:07:49 (GMT)
commitc8073657fcbfd1bfa72538d7babe4964857e7101 (patch)
treef3488e993c9828121b2f89ed2f639d2ebbe54dc9 /src
parentc5e8b345219cd7d8ca4b0aa638f59a1fed2cd83b (diff)
downloadpowder-c8073657fcbfd1bfa72538d7babe4964857e7101.zip
powder-c8073657fcbfd1bfa72538d7babe4964857e7101.tar.gz
More stuff, need to fix memory leak
Diffstat (limited to 'src')
-rw-r--r--src/PowderToy.cpp22
-rw-r--r--src/client/Client.cpp1
-rw-r--r--src/interface/Button.cpp30
-rw-r--r--src/interface/Keys.h8
-rw-r--r--src/interface/Label.cpp63
-rw-r--r--src/interface/Label.h14
-rw-r--r--src/interface/SaveButton.cpp1
-rw-r--r--src/interface/Textbox.cpp192
-rw-r--r--src/interface/Textbox.h44
-rw-r--r--src/search/SearchController.cpp8
-rw-r--r--src/search/SearchController.h1
-rw-r--r--src/search/SearchModel.cpp4
-rw-r--r--src/search/SearchModel.h2
-rw-r--r--src/search/SearchView.cpp29
-rw-r--r--src/search/SearchView.h3
15 files changed, 355 insertions, 67 deletions
diff --git a/src/PowderToy.cpp b/src/PowderToy.cpp
index 5d1db34..c6f5caf 100644
--- a/src/PowderToy.cpp
+++ b/src/PowderToy.cpp
@@ -63,28 +63,14 @@ int main(int argc, char * argv[])
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
float fps = 0, fpsLimit = 30, delta = 1.0f;
- //Renderer * ren;
- //Simulation * sim = new Simulation();
- //ren = new Renderer(g, sim);
-
ui::Engine::Ref().g = new Graphics();
ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
- ui::Engine * engine = &ui::Engine::Ref();//new ui::Engine();
- //ui::State * engineState = new ui::State();
- ui::Label * fpsLabel = new ui::Label(ui::Point(2, 2), ui::Point(200, 14), std::string("FPS: 0"));
- //engineState->AddComponent(fpsLabel);
+ ui::Engine * engine = &ui::Engine::Ref();
engine->Begin(XRES, YRES);
-// engine->SetState(engineState);
GameController * gameController = new GameController();
engine->ShowWindow(gameController->GetView());
- /*ui::Sandbox * sandbox = new ui::Sandbox();
- ui::Button * button = new ui::Button(ui::Point(100, 100), ui::Point(100, 100), std::string("poP"));
- engineState->AddComponent(fpsLabel);
- engineState->AddComponent(sandbox);
- engineState->AddComponent(button);
- engineState->AddComponent(ControlFactory::MainMenu(0, YRES+MENUSIZE-17, XRES+BARSIZE, 16));*/
SDL_Event event;
while(engine->Running())
@@ -98,6 +84,7 @@ int main(int argc, char * argv[])
engine->Exit();
break;
case SDL_KEYDOWN:
+ engine->onKeyPress(event.key.keysym.sym, false, false, false);
break;
case SDL_KEYUP:
break;
@@ -113,11 +100,6 @@ int main(int argc, char * argv[])
}
event.type = 0; //Clear last event
}
- //mouseButton = SDL_GetMouseState(&mouseX, &mouseY);
- fpsLabel->LabelText = "";
- stringstream fpsText;
- fpsText << "FPS: " << fps;
- fpsLabel->LabelText = fpsText.str();
engine->Tick(delta);
engine->Draw();
diff --git a/src/client/Client.cpp b/src/client/Client.cpp
index 1e2e76f..a3319b6 100644
--- a/src/client/Client.cpp
+++ b/src/client/Client.cpp
@@ -217,7 +217,6 @@ Thumbnail * Client::GetThumbnail(int saveID, int saveDate)
activeThumbRequests[i] = http_async_req_start(NULL, (char *)urlStream.str().c_str(), NULL, 0, 1);
activeThumbRequestTimes[i] = currentTime;
activeThumbRequestCompleteTimes[i] = 0;
- std::cout << "ThumbCache: Requesting " << urlStream.str() << " : " << idString << std::endl;
activeThumbRequestIDs[i] = idString;
return NULL;
}
diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp
index affa75d..783c297 100644
--- a/src/interface/Button.cpp
+++ b/src/interface/Button.cpp
@@ -130,34 +130,6 @@ void Button::Draw(const Point& screenPos)
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);
}
- /*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god
-
- //Draw component here
- sf::Text textGraphic(ButtonText);
- textGraphic.SetCharacterSize(11);
- if(isButtonDown)
- textGraphic.SetColor(sf::Color::Black);
- else
- textGraphic.SetColor(sf::Color::White);
- sf::FloatRect tempRect = textGraphic.GetRect();
- textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2));
-
- if(isMouseInside)
- {
- if(isButtonDown)
- rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
- else
- rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White));
- }
- else
- {
- if(isButtonDown)
- rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
- else
- rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White));
- }
-
- rw->Draw(textGraphic);*/
}
void Button::OnMouseUnclick(int x, int y, unsigned int button)
@@ -218,6 +190,8 @@ void Button::SetActionCallback(ButtonAction * action)
Button::~Button()
{
+ if(actionCallback)
+ delete actionCallback;
}
} /* namespace ui */
diff --git a/src/interface/Keys.h b/src/interface/Keys.h
new file mode 100644
index 0000000..e923703
--- /dev/null
+++ b/src/interface/Keys.h
@@ -0,0 +1,8 @@
+#define KEY_UP SDLK_UP
+#define KEY_DOWN SDLK_DOWN
+#define KEY_RIGHT SDLK_RIGHT
+#define KEY_LEFT SDLK_LEFT
+#define KEY_HOME SDLK_HOME
+#define KEY_END SDLK_END
+#define KEY_BACKSPACE SDLK_BACKSPACE
+#define KEY_DELETE SDLK_DELETE
diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp
index cf09d4e..6fc47e2 100644
--- a/src/interface/Label.cpp
+++ b/src/interface/Label.cpp
@@ -1,30 +1,39 @@
#include <string>
#include "Config.h"
#include "Global.h"
-#include "interface/Point.h"
-#include "interface/Label.h"
+#include "Point.h"
+#include "Label.h"
using namespace ui;
Label::Label(Window* parent_state, std::string labelText):
Component(parent_state),
- LabelText(labelText)
+ text(labelText),
+ textPosition(ui::Point(0, 0)),
+ textVAlign(AlignMiddle),
+ textHAlign(AlignCentre)
{
-
+ TextPosition();
}
Label::Label(Point position, Point size, std::string labelText):
Component(position, size),
- LabelText(labelText)
+ text(labelText),
+ textPosition(ui::Point(0, 0)),
+ textVAlign(AlignMiddle),
+ textHAlign(AlignCentre)
{
-
+ TextPosition();
}
Label::Label(std::string labelText):
Component(),
- LabelText(labelText)
+ text(labelText),
+ textPosition(ui::Point(0, 0)),
+ textVAlign(AlignMiddle),
+ textHAlign(AlignCentre)
{
-
+ TextPosition();
}
Label::~Label()
@@ -32,9 +41,45 @@ Label::~Label()
}
+void Label::TextPosition()
+{
+ //Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2
+ switch(textVAlign)
+ {
+ case AlignTop:
+ textPosition.Y = 3;
+ break;
+ case AlignMiddle:
+ textPosition.Y = (Size.Y-10)/2;
+ break;
+ case AlignBottom:
+ textPosition.Y = Size.Y-11;
+ break;
+ }
+
+ switch(textHAlign)
+ {
+ case AlignLeft:
+ textPosition.X = 3;
+ break;
+ case AlignCentre:
+ textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))/2;
+ break;
+ case AlignRight:
+ textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2;
+ break;
+ }
+}
+
+void Label::SetText(std::string text)
+{
+ this->text = text;
+ TextPosition();
+}
void Label::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
- g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)LabelText.c_str()))/2, Position.Y+(Size.Y-10)/2, LabelText, 255, 255, 255, 255);
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255);
}
+
diff --git a/src/interface/Label.h b/src/interface/Label.h
index e56852e..9b5a454 100644
--- a/src/interface/Label.h
+++ b/src/interface/Label.h
@@ -4,20 +4,28 @@
#include <string>
#include "Component.h"
+#include "Misc.h"
namespace ui
{
class Label : public Component
{
+ std::string text;
+ ui::Point textPosition;
+ HorizontalAlignment textHAlign;
+ VerticalAlignment textVAlign;
public:
Label(Window* parent_state, std::string labelText);
-
Label(Point position, Point size, std::string labelText);
-
Label(std::string labelText);
virtual ~Label();
- std::string LabelText;
+ void TextPosition();
+ void SetText(std::string text);
+ HorizontalAlignment GetHAlignment() { return textHAlign; }
+ VerticalAlignment GetVAlignment() { return textVAlign; }
+ void SetAlignment(HorizontalAlignment hAlign, VerticalAlignment vAlign) { textHAlign = hAlign; textVAlign = vAlign; TextPosition(); }
+
virtual void Draw(const Point& screenPos);
};
diff --git a/src/interface/SaveButton.cpp b/src/interface/SaveButton.cpp
index 18f3281..cf1c0f2 100644
--- a/src/interface/SaveButton.cpp
+++ b/src/interface/SaveButton.cpp
@@ -58,6 +58,7 @@ void SaveButton::Tick(float dt)
if(tempThumb)
{
thumbnail = tempThumb; //Store a local copy of the thumbnail
+ cout << (void *)(tempThumb) << " " << (void *)(&thumbnail) << endl;
if(thumbnail->Data)
{
if(thumbnail->Size.Y > (Size.Y-25))
diff --git a/src/interface/Textbox.cpp b/src/interface/Textbox.cpp
new file mode 100644
index 0000000..3cedc15
--- /dev/null
+++ b/src/interface/Textbox.cpp
@@ -0,0 +1,192 @@
+#include <string>
+#include <stdexcept>
+#include "Config.h"
+#include "Global.h"
+#include "interface/Point.h"
+#include "interface/Textbox.h"
+#include "interface/Keys.h"
+
+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)
+{
+ TextPosition();
+ cursor = text.length();
+}
+
+Textbox::Textbox(Point position, Point size, std::string textboxText):
+ Component(position, size),
+ text(textboxText),
+ textPosition(ui::Point(0, 0)),
+ textVAlign(AlignMiddle),
+ textHAlign(AlignCentre),
+ actionCallback(NULL)
+{
+ TextPosition();
+ cursor = text.length();
+}
+
+Textbox::Textbox(std::string textboxText):
+ Component(),
+ text(textboxText),
+ textPosition(ui::Point(0, 0)),
+ textVAlign(AlignMiddle),
+ textHAlign(AlignCentre),
+ actionCallback(NULL)
+{
+ TextPosition();
+ cursor = text.length();
+}
+
+Textbox::~Textbox()
+{
+ if(actionCallback)
+ delete actionCallback;
+}
+
+void Textbox::TextPosition()
+{
+ std::string tempText = text;
+ if(tempText.length() && cursor)
+ {
+ tempText.erase(cursor, tempText.length()-cursor);
+ cursorPosition = Graphics::textwidth((char *)tempText.c_str());
+ }
+ else
+ {
+ cursorPosition = 0;
+ }
+ //Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2
+ switch(textVAlign)
+ {
+ case AlignTop:
+ textPosition.Y = 3;
+ break;
+ case AlignMiddle:
+ textPosition.Y = (Size.Y-10)/2;
+ break;
+ case AlignBottom:
+ textPosition.Y = Size.Y-11;
+ break;
+ }
+
+ switch(textHAlign)
+ {
+ case AlignLeft:
+ textPosition.X = 3;
+ break;
+ case AlignCentre:
+ textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))/2;
+ break;
+ case AlignRight:
+ textPosition.X = (Size.X-Graphics::textwidth((char *)text.c_str()))-2;
+ break;
+ }
+}
+
+void Textbox::SetText(std::string text)
+{
+ this->text = text;
+ TextPosition();
+}
+
+std::string Textbox::GetText()
+{
+ return text;
+}
+
+void Textbox::OnKeyPress(int key, bool shift, bool ctrl, bool alt)
+{
+ bool changed = false;
+ try
+ {
+ switch(key)
+ {
+ case KEY_HOME:
+ cursor = 0;
+ break;
+ case KEY_END:
+ cursor = text.length();
+ break;
+ case KEY_LEFT:
+ if(cursor > 0)
+ cursor--;
+ break;
+ case KEY_RIGHT:
+ if(cursor < text.length())
+ cursor++;
+ break;
+ case KEY_DELETE:
+ if(text.length() && cursor < text.length())
+ {
+ if(ctrl)
+ text.erase(cursor, text.length()-cursor);
+ else
+ text.erase(cursor, 1);
+ changed = true;
+ }
+ break;
+ case KEY_BACKSPACE:
+ if(text.length() && cursor > 0)
+ {
+ if(ctrl)
+ {
+ text.erase(0, cursor);
+ cursor = 0;
+ }
+ else
+ {
+ text.erase(cursor-1, 1);
+ cursor--;
+ }
+ changed = true;
+ }
+ break;
+ default:
+ if(key >= ' ' && key < 127)
+ {
+ if(cursor == text.length())
+ {
+ text += key;
+ }
+ else
+ {
+ text.insert(cursor, 1, (char)key);
+ }
+ cursor++;
+ changed = true;
+ }
+ }
+ if(changed && actionCallback)
+ {
+ actionCallback->TextChangedCallback(this);
+ }
+ }
+ catch(std::out_of_range e)
+ {
+ cursor = 0;
+ text = "";
+ }
+ TextPosition();
+}
+
+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);
+ 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);
+ }
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, 255, 255, 255, 255);
+}
diff --git a/src/interface/Textbox.h b/src/interface/Textbox.h
new file mode 100644
index 0000000..5cd14f4
--- /dev/null
+++ b/src/interface/Textbox.h
@@ -0,0 +1,44 @@
+#ifndef TEXTBOX_H
+#define TEXTBOX_H
+
+#include <string>
+
+#include "Component.h"
+#include "Misc.h"
+
+namespace ui
+{
+class Textbox;
+class TextboxAction
+{
+public:
+ virtual void TextChangedCallback(ui::Textbox * sender) {}
+};
+class Textbox : public Component
+{
+ std::string text;
+ ui::Point textPosition;
+ HorizontalAlignment textHAlign;
+ VerticalAlignment textVAlign;
+ int cursor, cursorPosition;
+ TextboxAction *actionCallback;
+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);
+ 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 Draw(const Point& screenPos);
+};
+}
+
+#endif // TEXTBOX_H
diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp
index 90750e0..11871fd 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -1,3 +1,4 @@
+#include <string>
#include "SearchController.h"
#include "SearchModel.h"
#include "SearchView.h"
@@ -10,8 +11,13 @@ SearchController::SearchController()
searchModel->AddObserver(searchView);
searchView->AttachController(this);
- searchModel->UpdateSaveList();
+ searchModel->UpdateSaveList("");
//Set up interface
//windowPanel.AddChild();
}
+
+void SearchController::DoSearch(std::string query)
+{
+ searchModel->UpdateSaveList(query);
+}
diff --git a/src/search/SearchController.h b/src/search/SearchController.h
index 10dd9bd..8676145 100644
--- a/src/search/SearchController.h
+++ b/src/search/SearchController.h
@@ -14,6 +14,7 @@ private:
public:
SearchController();
SearchView * GetView() { return searchView; }
+ void DoSearch(std::string query);
};
#endif // SEARCHCONTROLLER_H
diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp
index 16a99d4..96d340a 100644
--- a/src/search/SearchModel.cpp
+++ b/src/search/SearchModel.cpp
@@ -7,12 +7,12 @@ SearchModel::SearchModel()
{
}
-void SearchModel::UpdateSaveList()
+void SearchModel::UpdateSaveList(std::string query)
{
lastError = "";
saveList.clear();
notifySaveListChanged();
- saveList = Client::Ref().SearchSaves(0, 12, "", "");
+ saveList = Client::Ref().SearchSaves(0, 12, query, "");
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();
diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h
index e65c15e..ca6a293 100644
--- a/src/search/SearchModel.h
+++ b/src/search/SearchModel.h
@@ -19,7 +19,7 @@ private:
public:
SearchModel();
void AddObserver(SearchView * observer);
- void UpdateSaveList();
+ void UpdateSaveList(std::string query);
vector<Save> GetSaveList();
string GetLastError() { return lastError; }
};
diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp
index 63a579e..2ddd76c 100644
--- a/src/search/SearchView.cpp
+++ b/src/search/SearchView.cpp
@@ -1,6 +1,7 @@
#include "SearchView.h"
#include "interface/SaveButton.h"
#include "interface/Label.h"
+#include "interface/Textbox.h"
#include "Misc.h"
SearchView::SearchView():
@@ -11,10 +12,34 @@ SearchView::SearchView():
nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95");
previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev");
+ class SearchAction : public ui::TextboxAction
+ {
+ SearchView * v;
+ public:
+ SearchAction(SearchView * _v) { v = _v; }
+ void TextChangedCallback(ui::Textbox * sender)
+ {
+ v->doSearch();
+ }
+ };
+ searchField = new ui::Textbox(ui::Point(60, 10), ui::Point((XRES+BARSIZE)-((50*2)+16+10+50+10), 16), "");
+ searchField->SetAlignment(AlignLeft, AlignBottom);
+ searchField->SetActionCallback(new SearchAction(this));
+
nextButton->SetAlignment(AlignRight, AlignBottom);
previousButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(nextButton);
AddComponent(previousButton);
+ AddComponent(searchField);
+
+ ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:");
+ searchPrompt->SetAlignment(AlignLeft, AlignBottom);
+ AddComponent(searchPrompt);
+}
+
+void SearchView::doSearch()
+{
+ c->DoSearch(searchField->GetText());
}
SearchView::~SearchView()
@@ -36,9 +61,9 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
AddComponent(errorLabel);
}
if(sender->GetLastError().length())
- errorLabel->LabelText = sender->GetLastError();
+ errorLabel->SetText(sender->GetLastError());
else
- errorLabel->LabelText = "No saves found";
+ errorLabel->SetText("No saves found");
}
else
{
diff --git a/src/search/SearchView.h b/src/search/SearchView.h
index 795fb74..8777c30 100644
--- a/src/search/SearchView.h
+++ b/src/search/SearchView.h
@@ -6,6 +6,7 @@
#include "interface/SaveButton.h"
#include "interface/Button.h"
#include "interface/Label.h"
+#include "interface/Textbox.h"
using namespace std;
@@ -20,6 +21,8 @@ private:
ui::Button * nextButton;
ui::Button * previousButton;
ui::Label * errorLabel;
+ ui::Textbox * searchField;
+ void doSearch();
public:
void NotifySaveListChanged(SearchModel * sender);
SearchView();