ShishGL  1.0
A simple modular cross-platform graphics library
UITextInput.hpp
Go to the documentation of this file.
1 /*============================================================================*/
2 #ifndef SHISHGL_UI_TEXT_INPUT_HPP
3 #define SHISHGL_UI_TEXT_INPUT_HPP
4 /*============================================================================*/
5 #include <cctype>
6 
7 #include "UILabel.hpp"
8 #include "Clickable.hpp"
9 #include "KeyboardEvent.hpp"
10 /*============================================================================*/
11 namespace Sh {
12 
13  class TextField : public Clickable {
14  public:
15 
16  explicit TextField(UIWindow* target, char* input_buf,
17  size_t input_buf_size)
18  : Clickable(target)
19  , buf(input_buf)
20  , buf_size(input_buf_size)
21  , curr_pos(0) {
23  KEYBOARD);
24  }
25 
26  void reactOnPress(MouseButtonEvent&) override {
27  curr_active = target<UIWindow>();
28  }
29 
30  bool onKeyboard(KeyboardEvent& event) override {
31 
32  if (target<UIWindow>() != curr_active) {
33  return false;
34  }
35 
36  if (event.state() == Keyboard::UP) {
37  return false;
38  }
39 
40  if (curr_pos < buf_size && Keyboard::isConvertible(event.key())) {
41 
42  char to_set = Keyboard::convertToChar(event.key());
43 
44  if (event.modifiers() & Keyboard::L_SHIFT_MOD) {
45  to_set = static_cast<char>(toupper(to_set));
46  }
47 
48  buf[curr_pos++] = to_set;
49  return true;
50 
51  } else if (event.key() == Keyboard::BACKSPACE) {
52 
53  if (curr_pos) {
54  buf[--curr_pos] = 0;
55  }
56  return true;
57 
58  } else if (curr_pos < buf_size) {
59 
60  buf[curr_pos++] = '*';
61 
62  }
63 
64  return false;
65  }
66 
67  private:
68 
69  char* buf;
70  size_t buf_size;
71  size_t curr_pos;
72 
73  static UIWindow* curr_active;
74  };
75 
76  class UITextInput : public UILabel {
77  public:
78 
79  UITextInput(const Frame& frame, char* buffer, size_t buffer_size)
80  : UILabel(frame, std::string_view(buffer, buffer_size)) {
81  addBehavior<TextField>(buffer, buffer_size);
82  }
83 
84  };
85 
86 }
87 /*============================================================================*/
88 #endif //SHISHGL_UI_TEXT_INPUT_HPP
89 /*============================================================================*/
Sh::Keyboard::convertToChar
static uint8_t convertToChar(Keyboard::Key key)
Definition: Keyboard.cpp:13
Sh::Keyboard::L_SHIFT_MOD
static constexpr ModifierMask L_SHIFT_MOD
Definition: Keyboard.hpp:20
Sh::KeyboardEvent::modifiers
Keyboard::ModifierMask modifiers() const
Definition: KeyboardEvent.cpp:36
Sh::EventSystem::SystemEvents
static constexpr Listener * SystemEvents
Definition: EventSystem.hpp:12
Sh::KeyboardEvent::state
Keyboard::KeyState state() const
Definition: KeyboardEvent.cpp:30
Sh::Frame
Definition: Frame.hpp:9
Sh::TextField::TextField
TextField(UIWindow *target, char *input_buf, size_t input_buf_size)
Definition: UITextInput.hpp:16
Sh::UITextInput::UITextInput
UITextInput(const Frame &frame, char *buffer, size_t buffer_size)
Definition: UITextInput.hpp:79
UILabel.hpp
Sh::Behavior::target
SomeWindow * target() const
Definition: Behavior.hpp:17
Sh::Keyboard::UP
@ UP
Definition: Keyboard.hpp:14
Sh::TextField
Definition: UITextInput.hpp:13
Sh::UITextInput
Definition: UITextInput.hpp:76
Sh::UIWindow
Definition: UIWindow.hpp:16
Sh::TextField::onKeyboard
bool onKeyboard(KeyboardEvent &event) override
Definition: UITextInput.hpp:30
Clickable.hpp
KeyboardEvent.hpp
Sh::KeyboardEvent
Definition: KeyboardEvent.hpp:11
Sh
Definition: CoreApplication.hpp:10
Sh::Keyboard::isConvertible
static bool isConvertible(Keyboard::Key key)
Definition: Keyboard.cpp:7
Sh::UILabel
Definition: UILabel.hpp:11
Sh::Keyboard::BACKSPACE
@ BACKSPACE
Definition: Keyboard.hpp:76
Sh::Clickable
Definition: Clickable.hpp:11
Sh::SubscriptionManager::subscribe
static void subscribe(Listener *receiver, Listener *sender, EventMask mask)
Definition: SubscriptionManager.cpp:18
Sh::TextField::reactOnPress
void reactOnPress(MouseButtonEvent &) override
Definition: UITextInput.hpp:26
Sh::KeyboardEvent::key
Keyboard::Key key() const
Definition: KeyboardEvent.cpp:24
Sh::MouseButtonEvent
Definition: MouseEvent.hpp:38