types.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef __MAUDIT_TYPES_H
  2. #define __MAUDIT_TYPES_H
  3. #include <string.h>
  4. #include <cstdint>
  5. #include <string>
  6. #include <stdexcept>
  7. namespace maudit {
  8. enum class color : uint8_t {
  9. none = 0,
  10. dim_black,
  11. dim_red,
  12. dim_green,
  13. dim_yellow,
  14. dim_blue,
  15. dim_magenta,
  16. dim_cyan,
  17. dim_white,
  18. bright_black,
  19. bright_red,
  20. bright_green,
  21. bright_yellow,
  22. bright_blue,
  23. bright_magenta,
  24. bright_cyan,
  25. bright_white
  26. };
  27. struct glyph {
  28. uint64_t text;
  29. color fore;
  30. color back;
  31. bool underline;
  32. static uint64_t get_sym(const char* c, size_t len) {
  33. if (len > 7)
  34. throw std::runtime_error("Glyph character too large: " + std::string(c));
  35. uint64_t ret;
  36. ::memcpy((void*)(&ret), (void*)c, len + 1);
  37. return ret;
  38. }
  39. void set_text(const std::string& c) {
  40. text = get_sym(c.c_str(), c.size());
  41. }
  42. void set_text(unsigned char c) {
  43. unsigned char tmp[2] = { c, '\0' };
  44. text = get_sym((const char*)tmp, 1);
  45. }
  46. const char* get_text() const {
  47. return (const char*)(&text);
  48. }
  49. glyph() : fore(color::none),
  50. back(color::bright_black),
  51. underline(false)
  52. { set_text(" "); }
  53. glyph(const std::string& c, color f, color b, bool u = false) :
  54. fore(f),
  55. back(b),
  56. underline(u)
  57. { set_text(c); }
  58. glyph(uint64_t t, color f, color b, bool u = false) :
  59. text(t),
  60. fore(f),
  61. back(b),
  62. underline(u) {}
  63. };
  64. enum class keycode : uint32_t {
  65. none = 0,
  66. left,
  67. right,
  68. up,
  69. down,
  70. kp_7,
  71. kp_9,
  72. kp_1,
  73. kp_3,
  74. del,
  75. esc,
  76. resize
  77. };
  78. struct keypress {
  79. unsigned char letter;
  80. keycode key;
  81. unsigned int w;
  82. unsigned int h;
  83. keypress(unsigned char l = 0) : letter(l), key(keycode::none), w(0), h(0) {}
  84. };
  85. }
  86. #endif