You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.1KB

  1. #include "ValleyWidgets.hpp"
  2. DynamicText::DynamicText() {
  3. font = Font::load(assetPlugin(plugin, "res/din1451alt.ttf"));
  4. size = 16;
  5. visibility = nullptr;
  6. colorHandle = nullptr;
  7. viewMode = ACTIVE_HIGH_VIEW;
  8. }
  9. void DynamicText::draw(NVGcontext* vg) {
  10. nvgFontSize(vg, size);
  11. nvgFontFaceId(vg, font->handle);
  12. nvgTextLetterSpacing(vg, 0.f);
  13. Vec textPos = Vec(0.f, 0.f);
  14. if(colorHandle != nullptr) {
  15. switch((ColorMode)*colorHandle) {
  16. case COLOR_MODE_WHITE: textColor = nvgRGB(0xFF,0xFF,0xFF); break;
  17. case COLOR_MODE_BLACK: textColor = nvgRGB(0x14,0x14, 0x14); break;
  18. default: textColor = nvgRGB(0xFF,0xFF,0xFF);
  19. }
  20. }
  21. else {
  22. textColor = nvgRGB(0xFF,0xFF,0xFF);
  23. }
  24. nvgFillColor(vg, textColor);
  25. nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
  26. nvgText(vg, textPos.x, textPos.y, text->c_str(), NULL);
  27. }
  28. void DynamicText::step() {
  29. if(visibility != nullptr) {
  30. if(*visibility) {
  31. visible = true;
  32. }
  33. else {
  34. visible = false;
  35. }
  36. if(viewMode == ACTIVE_LOW_VIEW) {
  37. visible = !visible;
  38. }
  39. }
  40. }
  41. DynamicText* createDynamicText(const Vec& pos, int size, std::string text,
  42. int* visibilityHandle, DynamicViewMode viewMode) {
  43. DynamicText* dynText = new DynamicText();
  44. dynText->size = size;
  45. dynText->text = std::make_shared<std::string>(text);
  46. dynText->box.pos = pos;
  47. dynText->box.size = Vec(82,14);
  48. dynText->visibility = visibilityHandle;
  49. dynText->viewMode = viewMode;
  50. return dynText;
  51. }
  52. DynamicText* createDynamicText(const Vec& pos, int size, std::string text,
  53. int* visibilityHandle, int* colorHandle, DynamicViewMode viewMode) {
  54. DynamicText* dynText = new DynamicText();
  55. dynText->size = size;
  56. dynText->text = std::make_shared<std::string>(text);
  57. dynText->box.pos = pos;
  58. dynText->box.size = Vec(82,14);
  59. dynText->visibility = visibilityHandle;
  60. dynText->viewMode = viewMode;
  61. dynText->colorHandle = colorHandle;
  62. return dynText;
  63. }
  64. DynamicText* createDynamicText(const Vec& pos, int size, std::shared_ptr<std::string> text,
  65. int* visibilityHandle, DynamicViewMode viewMode) {
  66. DynamicText* dynText = new DynamicText();
  67. dynText->size = size;
  68. dynText->text = text;
  69. dynText->box.pos = pos;
  70. dynText->box.size = Vec(82,14);
  71. dynText->visibility = visibilityHandle;
  72. dynText->viewMode = viewMode;
  73. return dynText;
  74. }
  75. DynamicText* createDynamicText(const Vec& pos, int size, std::shared_ptr<std::string> text,
  76. int* visibilityHandle, int* colorHandle, DynamicViewMode viewMode) {
  77. DynamicText* dynText = new DynamicText();
  78. dynText->size = size;
  79. dynText->text = text;
  80. dynText->box.pos = pos;
  81. dynText->box.size = Vec(82,14);
  82. dynText->visibility = visibilityHandle;
  83. dynText->viewMode = viewMode;
  84. dynText->colorHandle = colorHandle;
  85. return dynText;
  86. }
  87. DynamicFrameText::DynamicFrameText() {
  88. itemHandle = nullptr;
  89. }
  90. void DynamicFrameText::addItem(const std::string& item) {
  91. textItem.push_back(item);
  92. }
  93. void DynamicFrameText::draw(NVGcontext* vg) {
  94. int item = -1;
  95. if(itemHandle != nullptr) {
  96. item = *itemHandle;
  97. }
  98. if((int)textItem.size() && item >= 0 && item < (int)textItem.size()) {
  99. nvgFontSize(vg, size);
  100. nvgFontFaceId(vg, font->handle);
  101. nvgTextLetterSpacing(vg, 0.f);
  102. Vec textPos = Vec(0.f, 0.f);
  103. if(colorHandle != nullptr) {
  104. switch((ColorMode)*colorHandle) {
  105. case COLOR_MODE_WHITE: textColor = nvgRGB(0xFF,0xFF,0xFF); break;
  106. case COLOR_MODE_BLACK: textColor = nvgRGB(0x14,0x14, 0x14); break;
  107. default: textColor = nvgRGB(0xFF,0xFF,0xFF);
  108. }
  109. }
  110. else {
  111. textColor = nvgRGB(0xFF,0xFF,0xFF);
  112. }
  113. nvgFillColor(vg, textColor);
  114. nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
  115. nvgText(vg, textPos.x, textPos.y, textItem[item].c_str(), NULL);
  116. }
  117. }