Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
keyboard.h
1 #pragma once
2 
3 #include <map>
4 #include "widget.h"
5 #include "../../keycodes.h"
6 
7 namespace smlt {
8 namespace ui {
9 
13 class Frame;
14 
15 enum KeyboardLayout {
16  KEYBOARD_LAYOUT_ALPHABETICAL,
17  KEYBOARD_LAYOUT_NUMERICAL,
18 };
19 
21  uint16_t character;
22  bool cancelled = false;
23 
24  void cancel() {
25  cancelled = true;
26  }
27 };
28 
30 typedef sig::signal<void ()> KeyboardDoneSignal;
31 
32 enum KeyboardMode {
33  KEYBOARD_MODE_UPPERCASE,
34  KEYBOARD_MODE_LOWERCASE
35 };
36 
37 class Keyboard:
38  public Widget {
39 
40  DEFINE_SIGNAL(KeyboardKeyPressedSignal, signal_key_pressed);
41  DEFINE_SIGNAL(KeyboardDoneSignal, signal_done);
42 public:
43  using Widget::init; // Pull in init to satisfy Managed<Image>
44  using Widget::clean_up;
45 
46  Keyboard(UIManager* owner, UIConfig* config, KeyboardLayout layout);
47  ~Keyboard();
48 
49  void move_up();
50  void move_down();
51  void move_right();
52  void move_left();
53 
55  void activate();
56  void set_target(smlt::ui::Widget* widget);
57  void set_mode(KeyboardMode mode);
58 
59  using Widget::set_font;
60 private:
61  void move_row(int dir);
62 
63  KeyboardLayout layout_ = KEYBOARD_LAYOUT_ALPHABETICAL;
64  KeyboardMode mode_ = KEYBOARD_MODE_UPPERCASE;
65  std::shared_ptr<WidgetStyle> default_style_;
66  std::shared_ptr<WidgetStyle> highlighted_style_;
67 
68  unicode limited_chars_;
69 
70  struct ButtonInfo {
71  Button* button = nullptr;
72  uint8_t option_count = 0;
73  uint16_t options[4] = {0};
74  };
75 
76  // utf-16
77  std::map<uint16_t, ButtonInfo> buttons_;
78  void set_enabled(Button* btn, bool value);
79 
80  Frame* main_frame_ = nullptr;
81  Frame* rows_[5] = {0, 0, 0, 0, 0};
82  smlt::ui::Widget* focused_ = nullptr;
83  smlt::ui::Widget* target_ = nullptr;
84  sig::Connection target_destroyed_;
85 
86  void clear();
87 
88  void generate_alphabetical_layout(bool uppercase=true);
89  void generate_numerical_layout();
90 
91  void focus(smlt::ui::Widget* widget);
92 
93  void unfocus(smlt::ui::Widget* widget);
94 
95  virtual UIDim calculate_content_dimensions(Px text_width, Px text_height) override;
96 
97  void on_transformation_change_attempted() override;
98  void set_font(FontPtr font) override;
99 
100  smlt::ui::Button* new_button(const unicode& label);
101 
102  enum ActionFlags {
103  ACTION_FLAGS_BACKSPACE = 0x2,
104  ACTION_FLAGS_CASE_TOGGLE = 0x4,
105  ACTION_FLAGS_SPACEBAR = 0x8,
106  ACTION_FLAGS_ENTER = 0xF,
107  ACTION_FLAGS_DEFAULT = ACTION_FLAGS_BACKSPACE
108  };
109 
110  void populate_action_row(Frame* target, uint32_t action_flags=ACTION_FLAGS_DEFAULT);
111 
112  std::vector<sig::connection> update_connections_;
113 
114  std::shared_ptr<bool> alive_marker_ = std::make_shared<bool>(true);
115 };
116 
117 }
118 }
smlt::ui::Keyboard
Definition: keyboard.h:38
smlt::ui::UIManager
Definition: ui_manager.h:54
smlt::sig::Connection
Definition: signal.h:65
smlt::ui::SoftKeyPressedEvent
Definition: keyboard.h:20
smlt::ui::Button
Definition: button.h:10
smlt
Definition: animation.cpp:25
smlt::ui::Widget
Definition: widget.h:84
smlt::ui::Keyboard::activate
void activate()
Definition: keyboard.cpp:141
smlt::ui::UIConfig
Definition: ui_config.h:172
smlt::ui::Px
Definition: ui_config.h:37
smlt::ui::Frame
Definition: frame.h:28
unicode
Definition: unicode.h:35
smlt::ui::UIDim
Definition: ui_config.h:120
smlt::sig::signal
Definition: signal.h:319