From 7a2cb2c4b81a8a0696d3a11ce8781542f181bb12 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 6 May 2018 17:15:51 +0200 Subject: Make dchat Text editable Not finished yet. Currently text can be entered, removed and you can move caret using arrow keys (up, down, left, right), home and end. Need to implement text selection and remove focus from chatbar when editing message board text. Chatbar should be replaced with dchat Text for proper multiline editable text. --- include/Text.hpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/Text.hpp b/include/Text.hpp index daea7ca..9df973d 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,6 @@ namespace dchat Text(const sf::String &str, const sf::Font *font, unsigned int characterSize, float maxWidth, bool plainText = true); void setString(const sf::String &str); - void appendStringNewLine(const sf::String &str); void setPosition(float x, float y); void setPosition(const sf::Vector2f &position); @@ -43,16 +43,36 @@ namespace dchat void setCharacterSize(unsigned int characterSize); void setFillColor(sf::Color color); void setLineSpacing(float lineSpacing); + void setEditable(bool editable); // Warning: won't update until @draw is called float getHeight() const; + void processEvent(const sf::Event &event); + // Performs culling. @updateGeometry is called even if text is not visible if text is dirty, because updateGeometry might change the dimension of the text and make is visible void draw(sf::RenderTarget &target, Cache &cache); private: + enum class CaretMoveDirection : u8 + { + NONE, + UP, + DOWN, + HOME, + END + }; + void stringSplitElements(sf::String &stringToSplit, usize startIndex); void updateGeometry(); - private: + void updateCaret(); + bool isCaretAtEnd() const; + int getStartOfLine(int startIndex) const; + int getEndOfLine(int startIndex) const; + int getRowByPosition(const sf::Vector2f &position) const; + + int getPreviousLineClosestPosition(int startIndex) const; + int getNextLineClosestPosition(int startIndex) const; + private: sf::String str; const sf::Font *font; unsigned int characterSize; @@ -62,9 +82,16 @@ namespace dchat sf::Color color; sf::Color urlColor; bool dirty; + bool dirtyText; + bool dirtyCaret; bool plainText; + bool editable; + CaretMoveDirection caretMoveDirection; float totalHeight; float lineSpacing; std::vector textElements; + + int caretIndex; + sf::Vector2f caretPosition; }; } -- cgit v1.2.3