From 4277763df5c1dac8ff389d3bfd138f03acc7f1e2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 28 Sep 2020 13:32:34 +0200 Subject: Implement text editing with navigation and multilingual fonts --- include/Body.hpp | 6 ++++-- include/Entry.hpp | 37 +++++++++++++++++++++++++++++++++++++ include/ImageViewer.hpp | 7 +++++-- include/SearchBar.hpp | 9 +++++++-- include/Text.hpp | 30 +++++++++++++++++++++--------- 5 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 include/Entry.hpp (limited to 'include') diff --git a/include/Body.hpp b/include/Body.hpp index 35b3f30..a5c346e 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -1,10 +1,8 @@ #pragma once #include "Text.hpp" -#include #include #include -#include #include #include #include "../external/RoundedRectangleShape.hpp" @@ -12,6 +10,10 @@ #include #include +namespace sf { + class RenderWindow; +} + namespace QuickMedia { class Program; diff --git a/include/Entry.hpp b/include/Entry.hpp new file mode 100644 index 0000000..6f96e58 --- /dev/null +++ b/include/Entry.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "Text.hpp" +#include "../external/RoundedRectangleShape.hpp" +#include +#include + +namespace sf { + class Font; + class Event; + class RenderWindow; +} + +namespace QuickMedia { + // Return true to clear the text + using OnEntrySubmit = std::function; + + class Entry { + public: + Entry(const std::string &placeholder_text, sf::Font *font, sf::Font *cjk_font); + void process_event(sf::Event &event); + void draw(sf::RenderWindow &window); + + void set_editable(bool editable); + void set_position(const sf::Vector2f &pos); + void set_max_width(float width); + + float get_height(); + + OnEntrySubmit on_submit_callback; + private: + Text text; + float width; + sf::RoundedRectangleShape background; + sf::Text placeholder; + }; +} \ No newline at end of file diff --git a/include/ImageViewer.hpp b/include/ImageViewer.hpp index fe0b6f3..b8063ee 100644 --- a/include/ImageViewer.hpp +++ b/include/ImageViewer.hpp @@ -3,14 +3,17 @@ #include "Path.hpp" #include #include -#include #include #include -#include #include #include +#include #include +namespace sf { + class RenderWindow; +} + namespace QuickMedia { class Manga; diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp index a90c69d..8a1a8a0 100644 --- a/include/SearchBar.hpp +++ b/include/SearchBar.hpp @@ -1,13 +1,18 @@ #pragma once -#include -#include #include #include #include +#include #include "../external/RoundedRectangleShape.hpp" #include +namespace sf { + class Font; + class RenderWindow; + class Event; +} + namespace QuickMedia { using TextUpdateCallback = std::function; // Return true to consume the search (clear the search field) diff --git a/include/Text.hpp b/include/Text.hpp index bc6f7ab..d34fd47 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -1,15 +1,18 @@ #pragma once #include -#include -#include -#include #include #include #include #include "types.hpp" #include +namespace sf { + class Font; + class Event; + class RenderTarget; +} + namespace QuickMedia { struct StringViewUtf32 { @@ -69,6 +72,7 @@ namespace QuickMedia void setLineSpacing(float lineSpacing); void setCharacterSpacing(float characterSpacing); void setEditable(bool editable); + bool isEditable() const; // Note: won't update until @draw is called float getWidth() const; @@ -92,21 +96,27 @@ namespace QuickMedia HOME, END }; - -#if 0 + + struct VertexRef { + int vertices_index; // index to |vertices| VertexArray + int index; // index within vertices[vertices_index] + int line; + sf::Uint32 codepoint; + }; + 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; -#endif void splitTextByFont(); + + float get_text_quad_left_side(const VertexRef &vertex_ref) const; + float get_text_quad_right_side(const VertexRef &vertex_ref) const; private: - sf::String str; + sf::String str; // TODO: Remove this for non-editable text??? const sf::Font *font; const sf::Font *cjk_font; unsigned int characterSize; @@ -131,5 +141,7 @@ namespace QuickMedia sf::Vector2f caretPosition; sf::Clock lastSeenTimer; sf::Vector2u renderTargetSize; + + std::vector vertices_linear; // TODO: Use textElements instead }; } -- cgit v1.2.3