From 453eac7f1f5ef70390ec51087fc1f190811a7507 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 17 Nov 2021 09:47:45 +0100 Subject: Replace sfml with mgl --- include/Text.hpp | 84 +++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 47 deletions(-) (limited to 'include/Text.hpp') diff --git a/include/Text.hpp b/include/Text.hpp index 8eb5c96..dc3a0f7 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -1,37 +1,26 @@ #pragma once #include "NetUtils.hpp" -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include +#include +#include #include "types.hpp" #include -namespace sf { +namespace mgl { class Font; class Event; - class RenderTarget; + class Window; } namespace QuickMedia { - struct StringViewUtf32 { - const u32 *data; - size_t size; - - StringViewUtf32() : data(nullptr), size(0) {} - StringViewUtf32(const u32 *data, usize size) : data(data), size(size) {} - - size_t find(const StringViewUtf32 &other, size_t offset = 0) const; - - u32 operator [] (usize index) const { - assert(index < size); - return data[index]; - } - }; - struct TextElement { enum class Type @@ -47,10 +36,9 @@ namespace QuickMedia }; TextElement() {} - TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type), text_type(TextType::LATIN) {} + TextElement(std::string_view text, Type type) : text(text), type(type), text_type(TextType::LATIN) {} - StringViewUtf32 text; - sf::Vector2f position; + std::string_view text; Type type; //bool ownLine; // Currently only used for emoji, to make emoji bigger when it's the only thing on a line TextType text_type; @@ -60,33 +48,34 @@ namespace QuickMedia int vertices_index; // index to |vertices| VertexArray int index; // index within vertices[vertices_index] int line; - sf::Uint32 codepoint; + uint32_t codepoint; }; class Text { public: - Text(sf::String str, bool bold_font, unsigned int characterSize, float maxWidth, bool highlight_urls = false); + Text(std::string str, bool bold_font, unsigned int characterSize, float maxWidth, bool highlight_urls = false); - void setString(const sf::String &str); - const sf::String& getString() const; - void appendText(const sf::String &str); + void setString(std::string str); + const std::string& getString() const; + void appendText(const std::string &str); + void insert_text_at_caret_position(const std::string &str); - void setPosition(float x, float y); - void setPosition(const sf::Vector2f &position); - sf::Vector2f getPosition() const; + void set_position(float x, float y); + void set_position(const mgl::vec2f &position); + mgl::vec2f get_position() const; void setMaxWidth(float maxWidth); float getMaxWidth() const; void setCharacterSize(unsigned int characterSize); - unsigned int getCharacterSize() const; + unsigned int get_character_size() const; - void replace(size_t start_index, size_t length, const sf::String &insert_str); + void replace(size_t start_index, size_t length, const std::string &insert_str); int getCaretIndex() const; - void setFillColor(sf::Color color); + void set_color(mgl::Color color); void setLineSpacing(float lineSpacing); void setCharacterSpacing(float characterSpacing); void setEditable(bool editable); @@ -100,11 +89,11 @@ namespace QuickMedia // Note: won't update until @draw is called float getHeight() const; - void processEvent(const sf::Event &event); + void processEvent(mgl::Window &window, const mgl::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. // Returns true if text was drawn on screen (if text is within window borders) - bool draw(sf::RenderTarget &target); + bool draw(mgl::Window &target); void updateGeometry(bool update_even_if_not_dirty = false); @@ -134,7 +123,7 @@ namespace QuickMedia void splitTextByFont(); - float font_get_real_height(sf::Font *font); + float font_get_real_height(mgl::Font *font); float get_text_quad_left_side(const VertexRef &vertex_ref) const; float get_text_quad_right_side(const VertexRef &vertex_ref) const; // If the index is past the end, then the caret offset is the right side of the last character, rather than the left side @@ -142,23 +131,24 @@ namespace QuickMedia VertexRef& get_vertex_ref_clamp(int index); // Takes into consideration if index is the last vertex and the last vertex is a newline, then it should be on its own line int get_vertex_line(int index) const; - sf::Uint32 get_vertex_codepoint(int index) const; + uint32_t get_vertex_codepoint(int index) const; + size_t get_string_index_from_caret_index(size_t caret_index) const; private: - sf::String str; // TODO: Remove this for non-editable text??? also replace with std::string? then we get more efficient editing of text + std::string str; // TODO: Remove this for non-editable text??? also replace with std::string? then we get more efficient editing of text const bool bold_font; unsigned int characterSize; - std::array vertices; - std::array vertex_buffers; + std::array, 4> vertices; + std::array vertex_buffers; float maxWidth; - sf::Vector2f position; - sf::Color color; + mgl::vec2f position; + mgl::Color color; bool dirty; bool dirtyText; bool dirtyCaret; bool editable; bool highlight_urls; CaretMoveDirection caretMoveDirection; - sf::FloatRect boundingBox; + mgl::FloatRect boundingBox; int num_lines; float lineSpacing; float characterSpacing; @@ -166,8 +156,8 @@ namespace QuickMedia int caretIndex; float caret_offset_x; - sf::Vector2f caretPosition; - sf::Vector2u renderTargetSize; + mgl::vec2f caretPosition; + mgl::vec2u renderTarget_size; std::vector vertices_linear; // TODO: Use textElements instead -- cgit v1.2.3