summaryrefslogtreecommitdiff
path: root/src/interface
diff options
context:
space:
mode:
Diffstat (limited to 'src/interface')
-rw-r--r--src/interface/Component.cpp4
-rw-r--r--src/interface/Component.h1
-rw-r--r--src/interface/Label.cpp188
-rw-r--r--src/interface/Label.h24
-rw-r--r--src/interface/Textblock.cpp77
-rw-r--r--src/interface/Textblock.h34
6 files changed, 194 insertions, 134 deletions
diff --git a/src/interface/Component.cpp b/src/interface/Component.cpp
index 0094cf5..a0c55f4 100644
--- a/src/interface/Component.cpp
+++ b/src/interface/Component.cpp
@@ -16,6 +16,7 @@ Component::Component(Window* parent_state):
Locked(false),
Visible(true),
textPosition(0, 0),
+ textSize(0, 0),
iconPosition(0, 0),
drawn(false)
{
@@ -30,6 +31,7 @@ Component::Component(Point position, Point size):
Locked(false),
Visible(true),
textPosition(0, 0),
+ textSize(0, 0),
iconPosition(0, 0),
drawn(false)
{
@@ -44,6 +46,7 @@ Component::Component():
Locked(false),
Visible(true),
textPosition(0, 0),
+ textSize(0, 0),
iconPosition(0, 0),
drawn(false)
{
@@ -62,6 +65,7 @@ void Component::TextPosition(std::string displayText)
int textWidth, textHeight = 10;
Graphics::textsize((char*)displayText.c_str(), textWidth, textHeight);
+ textSize.X = textWidth; textSize.Y = textHeight;
textHeight-=3;
textWidth-=1;
if(Appearance.icon)
diff --git a/src/interface/Component.h b/src/interface/Component.h
index a4fde39..555be84 100644
--- a/src/interface/Component.h
+++ b/src/interface/Component.h
@@ -23,6 +23,7 @@ namespace ui
protected:
bool drawn;
ui::Point textPosition;
+ ui::Point textSize;
ui::Point iconPosition;
public:
Component(Window* parent_state);
diff --git a/src/interface/Label.cpp b/src/interface/Label.cpp
index ceb5a4e..755e3ce 100644
--- a/src/interface/Label.cpp
+++ b/src/interface/Label.cpp
@@ -5,57 +5,199 @@
using namespace ui;
-/*Label::Label(Window* parent_state, std::string labelText):
- Component(parent_state),
- text(labelText),
- textPosition(ui::Point(0, 0)),
- textVAlign(AlignMiddle),
- textHAlign(AlignCentre)
-{
- TextPosition();
-}*/
-
Label::Label(Point position, Point size, std::string labelText):
Component(position, size),
text(labelText),
- textColour(255, 255, 255)
+ textColour(255, 255, 255),
+ selectionIndex0(-1),
+ selectionIndex1(-1),
+ selectionXL(-1),
+ selectionXH(-1),
+ multiline(false),
+ selecting(false),
+ autoHeight(size.Y==-1?true:false),
+ caret(-1)
{
}
-/*Label::Label(std::string labelText):
- Component(),
- text(labelText),
- textPosition(ui::Point(0, 0)),
- textVAlign(AlignMiddle),
- textHAlign(AlignCentre)
-{
- TextPosition();
-}*/
-
Label::~Label()
{
}
+void Label::SetMultiline(bool status)
+{
+ multiline = status;
+ if(status)
+ {
+ updateMultiline();
+ updateSelection();
+ }
+}
+
void Label::SetText(std::string text)
{
this->text = text;
+ if(multiline)
+ {
+ updateMultiline();
+ updateSelection();
+ }
TextPosition(text);
}
+void Label::updateMultiline()
+{
+ char * rawText = new char[text.length()+1];
+ std::copy(text.begin(), text.end(), rawText);
+ rawText[text.length()] = 0;
+
+ int lines = 1;
+ 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';
+ lines++;
+ }
+ else
+ currentWidth += width;
+ if(nextSpace)
+ nextSpace[0] = ' ';
+ if(!(currentWord = strchr(currentWord+1, ' ')))
+ break;
+ }
+ if(autoHeight)
+ {
+ Size.Y = lines*12;
+ }
+ textLines = std::string(rawText);
+ delete[] rawText;
+}
+
std::string Label::GetText()
{
return this->text;
}
+void Label::OnMouseClick(int x, int y, unsigned button)
+{
+ if(x > textPosition.X && x < textPosition.X + textSize.X && y > textPosition.Y && y < textPosition.Y + textSize.Y)
+ {
+ selecting = true;
+ if(multiline)
+ selectionIndex0 = Graphics::CharIndexAtPosition((char*)textLines.c_str(), x-textPosition.X, y-textPosition.Y);
+ else
+ selectionIndex0 = Graphics::CharIndexAtPosition((char*)text.c_str(), x-textPosition.X, y-textPosition.Y);
+ selectionIndex1 = selectionIndex0;
+
+ updateSelection();
+ }
+}
+
+void Label::OnMouseUp(int x, int y, unsigned button)
+{
+ selecting = false;
+}
+
+void Label::OnMouseMoved(int localx, int localy, int dx, int dy)
+{
+ if(selecting)
+ {
+ if(multiline)
+ selectionIndex1 = Graphics::CharIndexAtPosition((char*)textLines.c_str(), localx-textPosition.X, localy-textPosition.Y);
+ else
+ selectionIndex1 = Graphics::CharIndexAtPosition((char*)text.c_str(), localx-textPosition.X, localy-textPosition.Y);
+ updateSelection();
+ }
+}
+
+void Label::updateSelection()
+{
+ std::string currentText;
+ if(multiline)
+ currentText = textLines;
+ else
+ currentText = text;
+ if(selectionIndex1 > selectionIndex0) {
+ selectionLineH = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex1, selectionXH, selectionYH);
+ selectionLineL = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex0, selectionXL, selectionYL);
+
+ textFragments = std::string(currentText);
+ textFragments.insert(selectionIndex1, "\x0E");
+ textFragments.insert(selectionIndex0, "\x0F\x01\x01\x01");
+ } else if(selectionIndex0 > selectionIndex1) {
+ selectionLineH = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex0, selectionXH, selectionYH);
+ selectionLineL = Graphics::PositionAtCharIndex((char*)currentText.c_str(), selectionIndex1, selectionXL, selectionYL);
+
+ textFragments = std::string(currentText);
+ textFragments.insert(selectionIndex0, "\x0E");
+ textFragments.insert(selectionIndex1, "\x0F\x01\x01\x01");
+ } else {
+ selectionXH = -1;
+ selectionXL = -1;
+
+ textFragments = std::string(currentText);
+ }
+}
+
void Label::Draw(const Point& screenPos)
{
if(!drawn)
{
- TextPosition(text);
+ if(multiline)
+ {
+ TextPosition(textLines);
+ updateMultiline();
+ updateSelection();
+ }
+ else
+ TextPosition(text);
drawn = true;
}
Graphics * g = Engine::Ref().g;
- g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, textColour.Red, textColour.Green, textColour.Blue, 255);
+
+ if(multiline)
+ {
+ if(selectionXL != -1 && selectionXH != -1)
+ {
+ if(selectionLineH - selectionLineL > 0)
+ {
+ g->fillrect(screenPos.X+textPosition.X+selectionXL, (screenPos.Y+textPosition.Y-1)+selectionYL, textSize.X-(selectionXL), 10, 255, 255, 255, 255);
+ for(int i = 1; i < selectionLineH-selectionLineL; i++)
+ {
+ g->fillrect(screenPos.X+textPosition.X, (screenPos.Y+textPosition.Y-1)+selectionYL+(i*12), textSize.X, 10, 255, 255, 255, 255);
+ }
+ g->fillrect(screenPos.X+textPosition.X, (screenPos.Y+textPosition.Y-1)+selectionYH, selectionXH, 10, 255, 255, 255, 255);
+
+ } else {
+ g->fillrect(screenPos.X+textPosition.X+selectionXL, screenPos.Y+selectionYL+textPosition.Y-1, selectionXH-(selectionXL), 10, 255, 255, 255, 255);
+ }
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, textFragments, textColour.Red, textColour.Green, textColour.Blue, 255);
+ }
+ else
+ {
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, textLines, textColour.Red, textColour.Green, textColour.Blue, 255);
+ }
+ } else {
+ if(selectionXL != -1 && selectionXH != -1)
+ {
+ g->fillrect(screenPos.X+textPosition.X+selectionXL, screenPos.Y+textPosition.Y-1, selectionXH-(selectionXL), 10, 255, 255, 255, 255);
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, textFragments, textColour.Red, textColour.Green, textColour.Blue, 255);
+ }
+ else
+ {
+ g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, text, textColour.Red, textColour.Green, textColour.Blue, 255);
+ }
+ }
}
diff --git a/src/interface/Label.h b/src/interface/Label.h
index 08c5fad..e154c0e 100644
--- a/src/interface/Label.h
+++ b/src/interface/Label.h
@@ -12,20 +12,44 @@ namespace ui
class Label : public Component
{
protected:
+ std::string textFragments;
+ std::string textLines;
+
std::string text;
Colour textColour;
+ int caret;
+ int selectionIndex0;
+ int selectionIndex1;
+
+ int selectionXL;
+ int selectionXH;
+ int selectionYL;
+ int selectionYH;
+ int selectionLineL;
+ int selectionLineH;
+
+ bool multiline;
+ bool selecting;
+ bool autoHeight;
+
+ void updateMultiline();
+ void updateSelection();
public:
//Label(Window* parent_state, std::string labelText);
Label(Point position, Point size, std::string labelText);
//Label(std::string labelText);
virtual ~Label();
+ virtual void SetMultiline(bool status);
virtual void SetText(std::string text);
virtual std::string GetText();
void SetTextColour(Colour textColour) { this->textColour = textColour; }
+ virtual void OnMouseClick(int x, int y, unsigned button);
+ virtual void OnMouseUp(int x, int y, unsigned button);
+ virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
virtual void Draw(const Point& screenPos);
};
}
diff --git a/src/interface/Textblock.cpp b/src/interface/Textblock.cpp
deleted file mode 100644
index f003a6a..0000000
--- a/src/interface/Textblock.cpp
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Textblock.cpp
- *
- * Created on: Jan 29, 2012
- * Author: Simon
- */
-
-#include <iostream>
-#include "Textblock.h"
-
-using namespace ui;
-
-Textblock::Textblock(Point position, Point size, std::string textboxText):
- Label(position, size, textboxText)
-{
- if(size.Y==-1)
- autoHeight = true;
- else
- autoHeight = false;
- updateMultiline();
-}
-
-void Textblock::SetText(std::string text)
-{
- this->text = text;
- updateMultiline();
-}
-
-void Textblock::updateMultiline()
-{
- char * rawText = new char[text.length()+1];
- std::copy(text.begin(), text.end(), rawText);
- rawText[text.length()] = 0;
-
- int lines = 1;
- 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';
- lines++;
- }
- else
- currentWidth += width;
- if(nextSpace)
- nextSpace[0] = ' ';
- if(!(currentWord = strchr(currentWord+1, ' ')))
- break;
- }
- if(autoHeight)
- {
- Size.Y = lines*12;
- }
- textLines = std::string(rawText);
- delete[] rawText;
-}
-
-void Textblock::Draw(const Point &screenPos)
-{
- Graphics * g = ui::Engine::Ref().g;
- //g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, textColour.Red, textColour.Green, textColour.Blue, 255);
- g->drawtext(screenPos.X+3, screenPos.Y+3, textLines, textColour.Red, textColour.Green, textColour.Blue, 255);
-}
-
-Textblock::~Textblock() {
- // TODO Auto-generated destructor stub
-}
-
diff --git a/src/interface/Textblock.h b/src/interface/Textblock.h
deleted file mode 100644
index 7a94c77..0000000
--- a/src/interface/Textblock.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Textblock.h
- *
- * Created on: Jan 29, 2012
- * Author: Simon
- */
-
-#ifndef TEXTBLOCK_H_
-#define TEXTBLOCK_H_
-
-#include <vector>
-#include <string>
-#include <sstream>
-#include "Label.h"
-
-namespace ui
-{
-
-class Textblock: public ui::Label
-{
- bool autoHeight;
- void updateMultiline();
- std::string textLines;
-public:
- Textblock(Point position, Point size, std::string textboxText);
- virtual void TextPosition() {}
- virtual void SetText(std::string text);
- virtual std::string GetDisplayText() { return textLines; }
- virtual void Draw(const Point& screenPos);
- virtual ~Textblock();
-};
-}
-
-#endif /* TEXTBLOCK_H_ */