summaryrefslogtreecommitdiff
path: root/src/interface
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/interface
parentfe329e9127ebcb8c89c505c4c120e175810d280c (diff)
downloadpowder-259fc2bcf75d754af043a5d3fa39b6ee0c0b1dec.zip
powder-259fc2bcf75d754af043a5d3fa39b6ee0c0b1dec.tar.gz
ASCII for key events, save and Textarea (no caret, yet)
Diffstat (limited to 'src/interface')
-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
13 files changed, 169 insertions, 80 deletions
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;