aboutsummaryrefslogtreecommitdiff
path: root/src/Entry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Entry.cpp')
-rw-r--r--src/Entry.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Entry.cpp b/src/Entry.cpp
new file mode 100644
index 0000000..977feab
--- /dev/null
+++ b/src/Entry.cpp
@@ -0,0 +1,72 @@
+#include "../include/Entry.hpp"
+#include <SFML/Graphics/RenderWindow.hpp>
+#include <SFML/Graphics/RectangleShape.hpp>
+#include <SFML/Window/Event.hpp>
+#include <cmath>
+
+const float background_margin_horizontal = 15.0f;
+const float padding_vertical = 5.0f;
+const float background_margin_vertical = 4.0f;
+
+namespace QuickMedia {
+ Entry::Entry(const std::string &placeholder_text, sf::Font *font, sf::Font *cjk_font) :
+ on_submit_callback(nullptr),
+ text("", font, cjk_font, 18, 0.0f),
+ width(0.0f),
+ background(sf::Vector2f(1.0f, 1.0f), 10.0f, 10),
+ placeholder(placeholder_text, *font, 18)
+ {
+ text.setEditable(true);
+ background.setFillColor(sf::Color(55, 60, 68));
+ placeholder.setFillColor(sf::Color(255, 255, 255, 100));
+ }
+
+ void Entry::process_event(sf::Event &event) {
+ text.processEvent(event);
+ if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter && !event.key.shift) {
+ if(on_submit_callback) {
+ bool clear_text = on_submit_callback(text.getString());
+ if(clear_text)
+ text.setString("");
+ }
+ }
+ }
+
+ // 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.setSize(sf::Vector2f(width, get_height()));
+ window.draw(background);
+ if(text.getString().isEmpty() && !text.isEditable()) {
+ window.draw(placeholder);
+ //sf::Vector2f placeholder_pos = placeholder.getPosition();
+ //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));
+ //window.draw(caret_rect);
+ } else {
+ text.draw(window);
+ }
+ }
+
+ void Entry::set_editable(bool editable) {
+ text.setEditable(editable);
+ }
+
+ void Entry::set_position(const sf::Vector2f &pos) {
+ background.setPosition(pos);
+ text.setPosition(pos + sf::Vector2f(background_margin_horizontal, background_margin_vertical));
+ placeholder.setPosition(pos + sf::Vector2f(background_margin_horizontal, background_margin_vertical + 7.0f));
+ }
+
+ void Entry::set_max_width(float width) {
+ this->width = width;
+ text.setMaxWidth(this->width - background_margin_horizontal * 2.0f);
+ }
+
+ float Entry::get_height() {
+ text.updateGeometry();
+ return std::floor(text.getHeight() + background_margin_vertical * 2.0f + padding_vertical *2.0f);
+ }
+} \ No newline at end of file