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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const unsigned char color[4], 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 = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
  26. ImDrawList dl;
  27. dl.PushTextureID(font->ContainerAtlas->TexID);
  28. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  29. dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
  30. ImDrawList* dlp = &dl;
  31. UI::renderImGui(&dlp, 1);
  32. }
  33. unsigned int FontImGui::heightText(float scale, unsigned int maxWidth, std::string s) {
  34. ImGuiIO& io = ImGui::GetIO();
  35. ImFont* font = io.Fonts->Fonts.at(0);
  36. ImVec2 size = font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, maxWidth, s.c_str(),
  37. s.c_str() + s.length());
  38. return size.y;
  39. }
  40. void FontImGui::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  41. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  42. ImGuiIO& io = ImGui::GetIO();
  43. ImFont* font = io.Fonts->Fonts.at(0);
  44. ImVec2 pos = ImVec2(x, y);
  45. ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
  46. ImDrawList dl;
  47. dl.PushTextureID(font->ContainerAtlas->TexID);
  48. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  49. dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length(), maxWidth);
  50. ImDrawList* dlp = &dl;
  51. UI::renderImGui(&dlp, 1);
  52. }