Keyboard.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef UTIL3DS_KEYBOARD_HPP
  2. #define UTIL3DS_KEYBOARD_HPP
  3. #include <cpp3ds/Graphics.hpp>
  4. #include "tinyxml2.h"
  5. #include "../TweenObjects.hpp"
  6. namespace util3ds {
  7. class Keyboard: public cpp3ds::Drawable, public TweenTransformable<cpp3ds::Transformable> {
  8. public:
  9. Keyboard();
  10. void loadFromFile(const std::string& filename);
  11. bool processEvents(const cpp3ds::Event& event);
  12. void update(float delta);
  13. bool popString(cpp3ds::String& string);
  14. void setCurrentInput(const cpp3ds::String &input);
  15. cpp3ds::String getCurrentInput() const;
  16. protected:
  17. void draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const;
  18. void updateVertices();
  19. void enterText();
  20. void processActiveKey();
  21. private:
  22. struct State {
  23. State(): fontSize(12), offsetX(6), offsetY(5) {}
  24. unsigned int fontSize;
  25. int offsetX;
  26. int offsetY;
  27. cpp3ds::Color color;
  28. cpp3ds::IntRect textureRect;
  29. };
  30. struct Style {
  31. std::string name;
  32. State defaultState;
  33. State activeState;
  34. };
  35. enum ButtonType {
  36. Key,
  37. Enter,
  38. Backspace,
  39. LayoutMenu,
  40. LayoutChange,
  41. LayoutChangeTemp,
  42. };
  43. struct Button {
  44. ButtonType type;
  45. cpp3ds::String data;
  46. unsigned char styleIndex;
  47. cpp3ds::IntRect rect;
  48. cpp3ds::Text text;
  49. };
  50. struct Layout {
  51. std::string name;
  52. std::string label;
  53. std::vector<Button> buttons;
  54. };
  55. struct Input : Button {
  56. Input(): maxWidth(200), cursorPosition(0) {}
  57. int maxWidth;
  58. int cursorPosition;
  59. };
  60. void updateButtonVertices(Button& button);
  61. bool fillState(State& state, const tinyxml2::XMLElement* styleNode, const char* stateName);
  62. void loadStyles(const tinyxml2::XMLElement* styleNode);
  63. void loadButtons(Layout& layout, const tinyxml2::XMLElement* layoutNode);
  64. cpp3ds::Texture m_texture;
  65. cpp3ds::VertexArray m_vertices;
  66. cpp3ds::Font m_font;
  67. bool m_usingFont;
  68. tinyxml2::XMLDocument m_xml;
  69. std::vector<Layout> m_layouts;
  70. std::vector<Style> m_styles;
  71. Button* m_activeButton;
  72. Input m_input;
  73. cpp3ds::RectangleShape m_cursor;
  74. mutable cpp3ds::Clock m_cursorClock;
  75. std::string m_filename;
  76. unsigned char m_layoutIndex;
  77. unsigned char m_tempLayoutIndex;
  78. bool m_usingTempLayout;
  79. bool m_loaded;
  80. bool m_needsUpdate;
  81. cpp3ds::Clock m_clockKeyPressStart;
  82. cpp3ds::Clock m_clockKeyRepeat;
  83. std::queue<cpp3ds::String> m_strings;
  84. };
  85. } // namesapce util3ds
  86. #endif // UTIL3DS_KEYBOARD_HPP