InterfaceUtils.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "InterfaceUtils.h"
  2. CREATE_SERVICE(InterfaceUtils, 10000)
  3. ICoreStorageLong * InterfaceUtils::largeshot = null;
  4. ICoreStorageLong * InterfaceUtils::hideHUD = null;
  5. InterfaceUtils::InterfaceUtils()
  6. {
  7. if(!largeshot)
  8. {
  9. largeshot = api->Storage().GetItemLong("system.screenshot.Largeshot", _FL_);
  10. largeshot->Set(0);
  11. }
  12. if(!hideHUD)
  13. {
  14. hideHUD = api->Storage().GetItemLong("system.screenshot.hidegui", _FL_);
  15. hideHUD->Set(0);
  16. }
  17. }
  18. InterfaceUtils::~InterfaceUtils()
  19. {
  20. RELEASE(largeshot);
  21. RELEASE(hideHUD);
  22. }
  23. dword InterfaceUtils::WordWrapString(string &str, IFont *font, float outputPixelWidth)
  24. {
  25. if( !font )
  26. return 0;
  27. dword linesCount = 1;
  28. float length = font->GetLength(str);
  29. if( length > outputPixelWidth )
  30. {
  31. float outputWidth = outputPixelWidth;
  32. dword potentialWrapPos = 0; // возможное место переноса
  33. dword prevRealWrapPos = 0; // место где был сделан последний перенос
  34. dword strLength = str.Len();
  35. for( dword i = 0 ; i < strLength ; i++ )
  36. {
  37. if( str[i] == ' ' )
  38. {
  39. str.GetDataBuffer()[i] = 0;
  40. float length = font->GetLength(str.GetBuffer() + prevRealWrapPos);
  41. str.GetDataBuffer()[i] = ' ';
  42. if( length >= outputWidth )
  43. {
  44. if( potentialWrapPos == 0 )
  45. potentialWrapPos = i;
  46. linesCount++;
  47. str.GetDataBuffer()[potentialWrapPos] = '\n';
  48. prevRealWrapPos = potentialWrapPos;
  49. }
  50. potentialWrapPos = i;
  51. }
  52. }
  53. // обработка переноса последней строки
  54. float length = font->GetLength(str.GetBuffer() + prevRealWrapPos);
  55. // if( length >= outputWidth )
  56. if( length >= outputWidth && (str.GetBuffer()[i] || potentialWrapPos))
  57. {
  58. if( potentialWrapPos == 0 )
  59. potentialWrapPos = i;
  60. linesCount++;
  61. str.GetDataBuffer()[potentialWrapPos] = '\n';
  62. }
  63. }
  64. str += "\n";
  65. return linesCount;
  66. }
  67. dword InterfaceUtils::WordWrapString(char text[], IFont *font, float outputPixelWidth, dword textMaxSize)
  68. {
  69. if( !font )
  70. return 0;
  71. dword linesCount = 1;
  72. float length = font->GetLength(text);
  73. if( length > outputPixelWidth )
  74. {
  75. float outputWidth = outputPixelWidth;
  76. dword potentialWrapPos = 0; // возможное место переноса
  77. dword prevRealWrapPos = 0; // место где был сделан последний перенос
  78. dword strLength = strlen(text);
  79. for( dword i = 0 ; i < strLength ; i++ )
  80. {
  81. if( text[i] == ' ' )
  82. {
  83. text[i] = 0;
  84. float length = font->GetLength(text + prevRealWrapPos);
  85. text[i] = ' ';
  86. if( length >= outputWidth )
  87. {
  88. if( potentialWrapPos == 0 )
  89. potentialWrapPos = i;
  90. linesCount++;
  91. text[potentialWrapPos] = '\n';
  92. prevRealWrapPos = potentialWrapPos;
  93. }
  94. potentialWrapPos = i;
  95. }
  96. }
  97. // обработка переноса последней строки
  98. float length = font->GetLength(text + prevRealWrapPos);
  99. // if( length >= outputWidth )
  100. if( length >= outputWidth && (text[i] || potentialWrapPos))
  101. {
  102. if( potentialWrapPos == 0 )
  103. potentialWrapPos = i;
  104. linesCount++;
  105. text[potentialWrapPos] = '\n';
  106. }
  107. }
  108. dword strLength = strlen(text);
  109. Assert(strLength < textMaxSize - 1)
  110. text[strLength] = '\n';
  111. text[strLength + 1] = 0;
  112. return linesCount;
  113. }