diff options
Diffstat (limited to 'src/Entry.cpp')
-rw-r--r-- | src/Entry.cpp | 67 |
1 files changed, 33 insertions, 34 deletions
diff --git a/src/Entry.cpp b/src/Entry.cpp index f0c02fd..590eade 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -3,9 +3,10 @@ #include "../include/Config.hpp" #include "../include/Utils.hpp" #include "../include/Theme.hpp" -#include <SFML/Graphics/RenderWindow.hpp> -#include <SFML/Graphics/RectangleShape.hpp> -#include <SFML/Window/Event.hpp> +#include <mglpp/system/FloatRect.hpp> +#include <mglpp/window/Window.hpp> +#include <mglpp/graphics/Rectangle.hpp> +#include <mglpp/window/Event.hpp> #include <math.h> namespace QuickMedia { @@ -13,29 +14,29 @@ namespace QuickMedia { static const float padding_vertical = std::floor(5.0f * get_config().scale); static const float background_margin_vertical = std::floor(0.0f * get_config().scale); - Entry::Entry(const std::string &placeholder_text, sf::Shader *rounded_rectangle_shader) : + Entry::Entry(const std::string &placeholder_text, mgl::Shader *rounded_rectangle_shader) : on_submit_callback(nullptr), draw_background(true), text("", false, std::floor(get_config().input.font_size * get_config().scale * get_config().font_scale), 0.0f), width(0.0f), - background(sf::Vector2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader), - placeholder(placeholder_text, *FontLoader::get_font(FontLoader::FontType::LATIN), std::floor(get_config().input.font_size * get_config().scale * get_config().font_scale)), + background(mgl::vec2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader), + placeholder(placeholder_text, *FontLoader::get_font(FontLoader::FontType::LATIN, get_config().input.font_size * get_config().scale * get_config().font_scale)), mouse_left_inside(false) { text.setEditable(true); - placeholder.setFillColor(get_theme().placeholder_text_color); + placeholder.set_color(get_theme().placeholder_text_color); } - void Entry::process_event(sf::Event &event) { - if(is_touch_enabled() && event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { - sf::FloatRect box(background.get_position(), background.get_size()); - if(box.contains(event.mouseButton.x, event.mouseButton.y)) + void Entry::process_event(mgl::Window &window, mgl::Event &event) { + if(is_touch_enabled() && event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) { + mgl::FloatRect box(background.get_position(), background.get_size()); + if(box.contains(mgl::vec2f(event.mouse_button.x, event.mouse_button.y))) mouse_left_inside = true; else mouse_left_inside = false; - } else if(is_touch_enabled() && event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) { - sf::FloatRect box(background.get_position(), background.get_size()); - if(mouse_left_inside && box.contains(event.mouseButton.x, event.mouseButton.y)) + } else if(is_touch_enabled() && event.type == mgl::Event::MouseButtonReleased && event.mouse_button.button == mgl::Mouse::Left) { + mgl::FloatRect box(background.get_position(), background.get_size()); + if(mouse_left_inside && box.contains(mgl::vec2f(event.mouse_button.x, event.mouse_button.y))) show_virtual_keyboard(); mouse_left_inside = false; } @@ -43,13 +44,11 @@ namespace QuickMedia { if(!text.isEditable()) return; - text.processEvent(event); + text.processEvent(window, event); - if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter && !event.key.shift) { + if(event.type == mgl::Event::KeyPressed && event.key.code == mgl::Keyboard::Enter && !event.key.shift) { if(on_submit_callback) { - auto u8 = text.getString().toUtf8(); - std::string *u8_str = (std::string*)&u8; - bool clear_text = on_submit_callback(*u8_str); + bool clear_text = on_submit_callback(text.getString()); if(clear_text) text.setString(""); } @@ -58,18 +57,18 @@ namespace QuickMedia { // TODO: Set the max number of visible lines and use glScissor to cut off the lines outsides // (and also split text into lines to not draw them at all once they are not inside the scissor box) - void Entry::draw(sf::RenderWindow &window) { - background.set_size(sf::Vector2f(width, get_height())); + void Entry::draw(mgl::Window &window) { + background.set_size(mgl::vec2f(width, get_height())); if(draw_background) background.draw(window); - if(text.getString().isEmpty() && !text.isEditable()) { + if(text.getString().empty() && !text.isEditable()) { window.draw(placeholder); - //sf::Vector2f placeholder_pos = placeholder.getPosition(); + //mgl::vec2f placeholder_pos = placeholder.get_position(); //const float caret_margin = 2.0f; //const float vspace = placeholder.getFont()->getLineSpacing(18); - //sf::RectangleShape caret_rect(sf::Vector2f(2.0f, floor(vspace - caret_margin * 2.0f))); - //caret_rect.setPosition(floor(placeholder_pos.x), floor(placeholder_pos.y + caret_margin)); + //mgl::Rectangle caret_rect(mgl::vec2f(2.0f, floor(vspace - caret_margin * 2.0f))); + //caret_rect.set_position(floor(placeholder_pos.x), floor(placeholder_pos.y + caret_margin)); //window.draw(caret_rect); } else { text.draw(window); @@ -84,8 +83,8 @@ namespace QuickMedia { text.setEditable(editable); } - void Entry::set_text(const std::string &new_text) { - text.setString(sf::String::fromUtf8(new_text.begin(), new_text.end())); + void Entry::set_text(std::string new_text) { + text.setString(std::move(new_text)); } void Entry::move_caret_to_end() { @@ -93,11 +92,11 @@ namespace QuickMedia { text.moveCaretToEnd(); } - void Entry::append_text(const std::string &str) { - text.appendText(sf::String::fromUtf8(str.begin(), str.end())); + void Entry::insert_text_at_caret_position(const std::string &str) { + text.insert_text_at_caret_position(str); } - void Entry::replace(size_t start_index, size_t length, const sf::String &insert_str) { + void Entry::replace(size_t start_index, size_t length, const std::string &insert_str) { text.replace(start_index, length, insert_str); } @@ -105,10 +104,10 @@ namespace QuickMedia { return text.getCaretIndex(); } - void Entry::set_position(const sf::Vector2f &pos) { + void Entry::set_position(const mgl::vec2f &pos) { background.set_position(pos); - text.setPosition(pos + sf::Vector2f(background_margin_horizontal, background_margin_vertical)); - placeholder.setPosition(pos + sf::Vector2f(background_margin_horizontal, background_margin_vertical + std::floor(5.0f * get_config().scale))); + text.set_position(pos + mgl::vec2f(background_margin_horizontal, background_margin_vertical)); + placeholder.set_position(pos + mgl::vec2f(background_margin_horizontal, background_margin_vertical + std::floor(-1.0f * get_config().scale))); } void Entry::set_max_width(float width) { @@ -125,7 +124,7 @@ namespace QuickMedia { return std::floor(text.getHeight() + background_margin_vertical * 2.0f + padding_vertical * 2.0f); } - const sf::String& Entry::get_text() const { + const std::string& Entry::get_text() const { return text.getString(); } }
\ No newline at end of file |