aboutsummaryrefslogtreecommitdiff
path: root/include/Text.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-17 09:47:45 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-17 09:59:29 +0100
commit453eac7f1f5ef70390ec51087fc1f190811a7507 (patch)
tree21a32ef6de9a3d7c29562484104b56c12518a6f0 /include/Text.hpp
parentfc49d40c0d2f6edbbe9dde1f1b53d6a17e9d9f7d (diff)
Replace sfml with mgl
Diffstat (limited to 'include/Text.hpp')
-rw-r--r--include/Text.hpp84
1 files changed, 37 insertions, 47 deletions
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 <SFML/Graphics/VertexArray.hpp>
-#include <SFML/Graphics/VertexBuffer.hpp>
-#include <SFML/System/String.hpp>
-#include <SFML/System/Clock.hpp>
+#include <mglpp/system/FloatRect.hpp>
+#include <mglpp/system/vec.hpp>
+#include <mglpp/graphics/Color.hpp>
+#include <mglpp/graphics/VertexBuffer.hpp>
+#include <mglpp/graphics/Vertex.hpp>
+#include <mglpp/system/Clock.hpp>
#include <vector>
+#include <string_view>
+#include <array>
#include "types.hpp"
#include <assert.h>
-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<sf::VertexArray, 4> vertices;
- std::array<sf::VertexBuffer, 4> vertex_buffers;
+ std::array<std::vector<mgl::Vertex>, 4> vertices;
+ std::array<mgl::VertexBuffer, 4> 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<VertexRef> vertices_linear; // TODO: Use textElements instead