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

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