Open Source Tomb Raider Engine
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.

FontImGui.cpp 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*!
  2. * \file src/system/FontImGui.cpp
  3. * \brief Default Font Implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include "imgui/imgui.h"
  8. #include "global.h"
  9. #include "UI.h"
  10. #include "system/FontImGui.h"
  11. #define SCALE_CALC 30.0f
  12. #define SCALE_DRAW SCALE_CALC
  13. unsigned int FontImGui::widthText(float scale, std::string s) {
  14. ImGuiIO& io = ImGui::GetIO();
  15. ImFont* font = io.Fonts->Fonts.at(0);
  16. ImVec2 size = font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, io.DisplaySize.y, s.c_str(),
  17. s.c_str() + s.length());
  18. return size.x;
  19. }
  20. void FontImGui::drawText(unsigned int x, unsigned int y, float scale,
  21. glm::vec4 color, std::string s) {
  22. ImGuiIO& io = ImGui::GetIO();
  23. ImFont* font = io.Fonts->Fonts.at(0);
  24. ImVec2 pos = ImVec2(x, y);
  25. ImU32 col = (ImU32(color.r * 255)) | (ImU32(color.g * 255) << 8) | (ImU32(color.b * 255) << 16) |
  26. (ImU32(color.a * 255) << 24);
  27. ImDrawList dl;
  28. dl.PushTextureID(font->ContainerAtlas->TexID);
  29. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  30. dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
  31. ImDrawList* dlp = &dl;
  32. UI::renderImGui(&dlp, 1);
  33. }
  34. unsigned int FontImGui::heightText(float scale, unsigned int maxWidth, std::string s) {
  35. ImGuiIO& io = ImGui::GetIO();
  36. ImFont* font = io.Fonts->Fonts.at(0);
  37. ImVec2 size = font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, maxWidth, s.c_str(),
  38. s.c_str() + s.length());
  39. return size.y;
  40. }
  41. void FontImGui::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  42. glm::vec4 color, unsigned int maxWidth, std::string s) {
  43. ImGuiIO& io = ImGui::GetIO();
  44. ImFont* font = io.Fonts->Fonts.at(0);
  45. ImVec2 pos = ImVec2(x, y);
  46. ImU32 col = (ImU32(color.r * 255)) | (ImU32(color.g * 255) << 8) | (ImU32(color.b * 255) << 16) |
  47. (ImU32(color.a * 255) << 24);
  48. ImDrawList dl;
  49. dl.PushTextureID(font->ContainerAtlas->TexID);
  50. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  51. dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length(), maxWidth);
  52. ImDrawList* dlp = &dl;
  53. UI::renderImGui(&dlp, 1);
  54. }