SearchBar.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "RoundedRectangle.hpp"
  3. #include <mglpp/window/Event.hpp>
  4. #include <mglpp/graphics/Text.hpp>
  5. #include <mglpp/graphics/Rectangle.hpp>
  6. #include <mglpp/graphics/Sprite.hpp>
  7. #include <mglpp/system/Clock.hpp>
  8. #include <functional>
  9. #include <string>
  10. namespace mgl {
  11. class Font;
  12. class Window;
  13. class Shader;
  14. }
  15. namespace QuickMedia {
  16. using TextUpdateCallback = std::function<void(const std::string &text)>;
  17. using TextSubmitCallback = std::function<void(const std::string &text)>;
  18. using TextBeginTypingCallback = std::function<void()>;
  19. enum class SearchBarType {
  20. Search,
  21. Text,
  22. Password
  23. };
  24. class SearchBar {
  25. public:
  26. SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, SearchBarType type);
  27. void draw(mgl::Window &window, mgl::vec2f size, bool draw_background);
  28. void on_event(mgl::Window &window, mgl::Event &event);
  29. void update();
  30. void onWindowResize(const mgl::vec2f &window_size);
  31. void clear();
  32. void set_text(const std::string &text, bool update_search = true);
  33. void append_text(const std::string &text_to_add);
  34. void set_position(mgl::vec2f pos);
  35. void set_editable(bool editable);
  36. bool is_editable() const;
  37. float getBottom() const;
  38. float getBottomWithoutShadow() const;
  39. const std::string& get_text() const;
  40. bool is_empty() const;
  41. TextUpdateCallback onTextUpdateCallback;
  42. TextSubmitCallback onTextSubmitCallback;
  43. TextBeginTypingCallback onTextBeginTypingCallback;
  44. int text_autosearch_delay_ms;
  45. bool caret_visible;
  46. float padding_top = 0.0f;
  47. float padding_bottom = 0.0f;
  48. float padding_x = 10.0f;
  49. private:
  50. void onTextEntered(const mgl::Event::TextEvent &text_event);
  51. private:
  52. mgl::Text text;
  53. mgl::Text placeholder_text;
  54. RoundedRectangle background;
  55. mgl::Rectangle shade;
  56. mgl::Rectangle caret;
  57. mgl::Sprite plugin_logo_sprite;
  58. mgl::Sprite search_icon_sprite;
  59. bool updated_search;
  60. bool draw_logo;
  61. bool needs_update;
  62. bool typing;
  63. bool backspace_pressed;
  64. bool mouse_left_inside;
  65. mgl::vec2f pos;
  66. mgl::Clock time_since_search_update;
  67. mgl::vec2f prev_size;
  68. bool editable = true;
  69. SearchBarType type;
  70. };
  71. }