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 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 1.0f
  12. #define SCALE_DRAW 20.0f
  13. unsigned int FontImGui::widthText(float scale, std::string s) {
  14. ImGuiIO& io = ImGui::GetIO();
  15. ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, io.DisplaySize.y, s.c_str(),
  16. s.c_str() + s.length());
  17. return size.y;
  18. }
  19. void FontImGui::drawText(unsigned int x, unsigned int y, float scale,
  20. const unsigned char color[4], std::string s) {
  21. ImGuiIO& io = ImGui::GetIO();
  22. ImVec2 pos = ImVec2(x, y);
  23. ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
  24. ImDrawList dl;
  25. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  26. dl.AddText(io.Font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
  27. ImDrawList* dlp = &dl;
  28. UI::renderImGui(&dlp, 1);
  29. }
  30. unsigned int FontImGui::heightText(float scale, unsigned int maxWidth, std::string s) {
  31. ImGuiIO& io = ImGui::GetIO();
  32. ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, maxWidth, s.c_str(),
  33. s.c_str() + s.length());
  34. return size.x;
  35. }
  36. void FontImGui::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  37. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  38. ImGuiIO& io = ImGui::GetIO();
  39. ImVec2 pos = ImVec2(x, y);
  40. ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
  41. ImDrawList dl;
  42. dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
  43. dl.AddText(io.Font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length(), maxWidth);
  44. ImDrawList* dlp = &dl;
  45. UI::renderImGui(&dlp, 1);
  46. }