aboutsummaryrefslogtreecommitdiff
path: root/src/SearchBar.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-08-04 02:28:33 +0200
committerdec05eba <dec05eba@protonmail.com>2019-08-04 02:28:36 +0200
commit4b24638802385816fb5f90c95f175b30ae2398a8 (patch)
treeab6c7cbfd7d20c2065e160f6e8f20be02e4cc1b5 /src/SearchBar.cpp
parentd9fb89269fd30fa44d2b3728b9ae3c7b896a77d3 (diff)
Add youtube video playing, page navigation
Diffstat (limited to 'src/SearchBar.cpp')
-rw-r--r--src/SearchBar.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp
new file mode 100644
index 0000000..dad9e7c
--- /dev/null
+++ b/src/SearchBar.cpp
@@ -0,0 +1,93 @@
+#include "../include/SearchBar.hpp"
+
+const sf::Color text_placeholder_color(255, 255, 255, 100);
+const sf::Color front_color(43, 45, 47);
+const sf::Color back_color(33, 35, 37);
+const float background_margin_horizontal = 8.0f;
+const float background_margin_vertical = 4.0f;
+const float padding_horizontal = 10.0f;
+const float padding_vertical = 10.0f;
+
+namespace QuickMedia {
+ SearchBar::SearchBar(sf::Font &font) :
+ onTextUpdateCallback(nullptr),
+ onTextSubmitCallback(nullptr),
+ text("Search...", font, 18),
+ show_placeholder(true),
+ updated_search(false)
+ {
+ text.setFillColor(text_placeholder_color);
+ background.setFillColor(front_color);
+ background.setPosition(padding_horizontal, padding_vertical);
+ }
+
+ void SearchBar::draw(sf::RenderWindow &window) {
+ window.draw(background);
+ window.draw(text);
+ }
+
+ void SearchBar::update() {
+ if(updated_search && time_since_search_update.getElapsedTime().asMilliseconds() >= 150) {
+ updated_search = false;
+ sf::String str = text.getString();
+ if(show_placeholder)
+ str.clear();
+ if(onTextUpdateCallback)
+ onTextUpdateCallback(str);
+ }
+ }
+
+ void SearchBar::onWindowResize(const sf::Vector2f &window_size) {
+ float font_height = text.getCharacterSize() + 8.0f;
+ float rect_height = font_height + background_margin_vertical * 2.0f;
+ background.setSize(sf::Vector2f(window_size.x - padding_horizontal * 2.0f, rect_height));
+ text.setPosition(padding_horizontal + background_margin_horizontal, padding_vertical + background_margin_vertical);
+ }
+
+ void SearchBar::onTextEntered(sf::Uint32 codepoint) {
+ if(codepoint == 8 && !show_placeholder) { // Backspace
+ sf::String str = text.getString();
+ if(str.getSize() > 0) {
+ str.erase(str.getSize() - 1, 1);
+ text.setString(str);
+ if(str.getSize() == 0) {
+ show_placeholder = true;
+ text.setString("Search...");
+ text.setFillColor(text_placeholder_color);
+ }
+ updated_search = true;
+ time_since_search_update.restart();
+ }
+ } else if(codepoint == 13) { // Return
+ if(onTextSubmitCallback)
+ onTextSubmitCallback(text.getString());
+
+ if(!show_placeholder) {
+ show_placeholder = true;
+ text.setString("Search...");
+ text.setFillColor(text_placeholder_color);
+ }
+ } else if(codepoint > 31) { // Non-control character
+ if(show_placeholder) {
+ show_placeholder = false;
+ text.setString("");
+ text.setFillColor(sf::Color::White);
+ }
+ sf::String str = text.getString();
+ str += codepoint;
+ text.setString(str);
+ updated_search = true;
+ time_since_search_update.restart();
+ }
+ }
+
+ void SearchBar::clear() {
+ show_placeholder = true;
+ text.setString("Search...");
+ text.setFillColor(text_placeholder_color);
+ }
+
+ float SearchBar::getBottom() const {
+ return background.getPosition().y + background.getSize().y;
+ }
+} \ No newline at end of file