otto_emoji_display.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "otto_emoji_display.h"
  2. #include <esp_log.h>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include "display/lcd_display.h"
  7. #include "font_awesome_symbols.h"
  8. #define TAG "OttoEmojiDisplay"
  9. // 表情映射表 - 将原版21种表情映射到现有6个GIF
  10. const OttoEmojiDisplay::EmotionMap OttoEmojiDisplay::emotion_maps_[] = {
  11. // 中性/平静类表情 -> staticstate
  12. {"neutral", &staticstate},
  13. {"relaxed", &staticstate},
  14. {"sleepy", &staticstate},
  15. // 积极/开心类表情 -> happy
  16. {"happy", &happy},
  17. {"laughing", &happy},
  18. {"funny", &happy},
  19. {"loving", &happy},
  20. {"confident", &happy},
  21. {"winking", &happy},
  22. {"cool", &happy},
  23. {"delicious", &happy},
  24. {"kissy", &happy},
  25. {"silly", &happy},
  26. // 悲伤类表情 -> sad
  27. {"sad", &sad},
  28. {"crying", &sad},
  29. // 愤怒类表情 -> anger
  30. {"angry", &anger},
  31. // 惊讶类表情 -> scare
  32. {"surprised", &scare},
  33. {"shocked", &scare},
  34. // 思考/困惑类表情 -> buxue
  35. {"thinking", &buxue},
  36. {"confused", &buxue},
  37. {"embarrassed", &buxue},
  38. {nullptr, nullptr} // 结束标记
  39. };
  40. OttoEmojiDisplay::OttoEmojiDisplay(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
  41. int width, int height, int offset_x, int offset_y, bool mirror_x,
  42. bool mirror_y, bool swap_xy, DisplayFonts fonts)
  43. : SpiLcdDisplay(panel_io, panel, width, height, offset_x, offset_y, mirror_x, mirror_y, swap_xy,
  44. fonts),
  45. emotion_gif_(nullptr) {
  46. SetupGifContainer();
  47. };
  48. void OttoEmojiDisplay::SetupGifContainer() {
  49. DisplayLockGuard lock(this);
  50. if (emotion_label_) {
  51. lv_obj_del(emotion_label_);
  52. }
  53. if (chat_message_label_) {
  54. lv_obj_del(chat_message_label_);
  55. }
  56. if (content_) {
  57. lv_obj_del(content_);
  58. }
  59. content_ = lv_obj_create(container_);
  60. lv_obj_set_scrollbar_mode(content_, LV_SCROLLBAR_MODE_OFF);
  61. lv_obj_set_size(content_, LV_HOR_RES, LV_HOR_RES);
  62. lv_obj_set_style_bg_opa(content_, LV_OPA_TRANSP, 0);
  63. lv_obj_set_style_border_width(content_, 0, 0);
  64. lv_obj_set_flex_grow(content_, 1);
  65. lv_obj_center(content_);
  66. emotion_label_ = lv_label_create(content_);
  67. lv_label_set_text(emotion_label_, "");
  68. lv_obj_set_width(emotion_label_, 0);
  69. lv_obj_set_style_border_width(emotion_label_, 0, 0);
  70. lv_obj_add_flag(emotion_label_, LV_OBJ_FLAG_HIDDEN);
  71. emotion_gif_ = lv_gif_create(content_);
  72. int gif_size = LV_HOR_RES;
  73. lv_obj_set_size(emotion_gif_, gif_size, gif_size);
  74. lv_obj_set_style_border_width(emotion_gif_, 0, 0);
  75. lv_obj_set_style_bg_opa(emotion_gif_, LV_OPA_TRANSP, 0);
  76. lv_obj_center(emotion_gif_);
  77. lv_gif_set_src(emotion_gif_, &staticstate);
  78. chat_message_label_ = lv_label_create(content_);
  79. lv_label_set_text(chat_message_label_, "");
  80. lv_obj_set_width(chat_message_label_, LV_HOR_RES * 0.9);
  81. lv_label_set_long_mode(chat_message_label_, LV_LABEL_LONG_SCROLL_CIRCULAR);
  82. lv_obj_set_style_text_align(chat_message_label_, LV_TEXT_ALIGN_CENTER, 0);
  83. lv_obj_set_style_text_color(chat_message_label_, lv_color_white(), 0);
  84. lv_obj_set_style_border_width(chat_message_label_, 0, 0);
  85. lv_obj_set_style_bg_opa(chat_message_label_, LV_OPA_70, 0);
  86. lv_obj_set_style_bg_color(chat_message_label_, lv_color_black(), 0);
  87. lv_obj_set_style_pad_ver(chat_message_label_, 5, 0);
  88. lv_obj_align(chat_message_label_, LV_ALIGN_BOTTOM_MID, 0, 0);
  89. LcdDisplay::SetTheme("dark");
  90. }
  91. void OttoEmojiDisplay::SetEmotion(const char* emotion) {
  92. if (!emotion || !emotion_gif_) {
  93. return;
  94. }
  95. DisplayLockGuard lock(this);
  96. for (const auto& map : emotion_maps_) {
  97. if (map.name && strcmp(map.name, emotion) == 0) {
  98. lv_gif_set_src(emotion_gif_, map.gif);
  99. ESP_LOGI(TAG, "设置表情: %s", emotion);
  100. return;
  101. }
  102. }
  103. lv_gif_set_src(emotion_gif_, &staticstate);
  104. ESP_LOGI(TAG, "未知表情'%s',使用默认", emotion);
  105. }
  106. void OttoEmojiDisplay::SetChatMessage(const char* role, const char* content) {
  107. DisplayLockGuard lock(this);
  108. if (chat_message_label_ == nullptr) {
  109. return;
  110. }
  111. if (content == nullptr || strlen(content) == 0) {
  112. lv_obj_add_flag(chat_message_label_, LV_OBJ_FLAG_HIDDEN);
  113. return;
  114. }
  115. lv_label_set_text(chat_message_label_, content);
  116. lv_obj_clear_flag(chat_message_label_, LV_OBJ_FLAG_HIDDEN);
  117. ESP_LOGI(TAG, "设置聊天消息 [%s]: %s", role, content);
  118. }
  119. void OttoEmojiDisplay::SetIcon(const char* icon) {
  120. if (!icon) {
  121. return;
  122. }
  123. DisplayLockGuard lock(this);
  124. if (chat_message_label_ != nullptr) {
  125. std::string icon_message = std::string(icon) + " ";
  126. if (strcmp(icon, FONT_AWESOME_DOWNLOAD) == 0) {
  127. icon_message += "正在升级...";
  128. } else {
  129. icon_message += "系统状态";
  130. }
  131. lv_label_set_text(chat_message_label_, icon_message.c_str());
  132. lv_obj_clear_flag(chat_message_label_, LV_OBJ_FLAG_HIDDEN);
  133. ESP_LOGI(TAG, "设置图标: %s", icon);
  134. }
  135. }