EditorUtils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Math/Color.h>
  9. #include "EditorDefs.h"
  10. #include "EditorUtils.h"
  11. #include "EditorToolsApplicationAPI.h"
  12. // Qt
  13. #include <QColor>
  14. #include <QMessageBox>
  15. #define GetAValue(rgba) (LOBYTE((rgba)>>24)) // Microsoft does not provide this one so let's make our own.
  16. #define RGBA(r,g,b,a) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)|(((DWORD)(BYTE)(a))<<24)))
  17. //////////////////////////////////////////////////////////////////////////
  18. void HeapCheck::Check([[maybe_unused]] const char* file, [[maybe_unused]] int line)
  19. {
  20. #ifdef _DEBUG
  21. #ifdef Q_OS_WIN
  22. _ASSERTE(_CrtCheckMemory());
  23. #endif
  24. #endif
  25. }
  26. #ifdef LoadCursor
  27. #undef LoadCursor
  28. #endif
  29. QCursor CMFCUtils::LoadCursor(unsigned int nIDResource, int hotX, int hotY)
  30. {
  31. QString path;
  32. switch (nIDResource)
  33. {
  34. case IDC_HAND_INTERNAL:
  35. path = QStringLiteral("cursor1.cur");
  36. break;
  37. case IDC_ZOOM_INTERNAL:
  38. path = QStringLiteral("cur00001.cur");
  39. break;
  40. case IDC_BRUSH_INTERNAL:
  41. path = QStringLiteral("cur00002.cur");
  42. break;
  43. case IDC_ARRBLCK:
  44. path = QStringLiteral("cur00003.cur");
  45. break;
  46. case IDC_ARRBLCKCROSS:
  47. path = QStringLiteral("cur00004.cur");
  48. break;
  49. case IDC_ARRWHITE:
  50. path = QStringLiteral("cur00005.cur");
  51. break;
  52. case IDC_COLOR_PICKER:
  53. path = QStringLiteral("pick_cursor.cur");
  54. break;
  55. case IDC_HIT_CURSOR:
  56. path = QStringLiteral("hit.cur");
  57. break;
  58. case IDC_ARROW_ADDKEY:
  59. path = QStringLiteral("arr_addkey.cur");
  60. break;
  61. case IDC_LEFTRIGHT:
  62. path = QStringLiteral("leftright.cur");
  63. break;
  64. case IDC_POINTER_OBJHIT:
  65. path = QStringLiteral("pointerHit.cur");
  66. break;
  67. case IDC_POINTER_LINK:
  68. path = QStringLiteral("pointer_link.cur");
  69. break;
  70. case IDC_POINTER_LINKNOW:
  71. path = QStringLiteral("pointer_linknow.cur");
  72. break;
  73. case IDC_POINTER_OBJECT_ROTATE:
  74. path = QStringLiteral("object_rotate.cur");
  75. break;
  76. case IDC_POINTER_OBJECT_SCALE:
  77. path = QStringLiteral("object_scale.cur");
  78. break;
  79. case IDC_POINTER_OBJECT_MOVE:
  80. path = QStringLiteral("object_move.cur");
  81. break;
  82. case IDC_POINTER_PLUS:
  83. path = QStringLiteral("pointer_plus.cur");
  84. break;
  85. case IDC_POINTER_MINUS:
  86. path = QStringLiteral("pointer_minus.cur");
  87. break;
  88. case IDC_POINTER_FLATTEN:
  89. path = QStringLiteral("pointer_flatten.cur");
  90. break;
  91. case IDC_POINTER_SMOOTH:
  92. path = QStringLiteral("pointer_smooth.cur");
  93. break;
  94. case IDC_POINTER_SO_SELECT:
  95. path = QStringLiteral("pointer_so_select.cur");
  96. break;
  97. case IDC_POINTER_SO_SELECT_PLUS:
  98. path = QStringLiteral("pointer_so_sel_plus.cur");
  99. break;
  100. case IDC_POINTER_SO_SELECT_MINUS:
  101. path = QStringLiteral("pointer_.cur");
  102. break;
  103. case IDC_POINTER_DRAG_ITEM:
  104. path = QStringLiteral("pointerDragItem.cur");
  105. break;
  106. case IDC_CURSOR_HAND_DRAG:
  107. path = QStringLiteral("handDrag.cur");
  108. break;
  109. case IDC_CURSOR_HAND_FINGER:
  110. path = QStringLiteral("cursor2.cur");
  111. break;
  112. case IDC_ARROW_UP:
  113. path = QStringLiteral("arrow_up.cur");
  114. break;
  115. case IDC_ARROW_DOWN:
  116. path = QStringLiteral("arrow_down.cur");
  117. break;
  118. case IDC_ARROW_DOWNRIGHT:
  119. path = QStringLiteral("arrow_downright.cur");
  120. break;
  121. case IDC_ARROW_UPRIGHT:
  122. path = QStringLiteral("arrow_upright.cur");
  123. break;
  124. case IDC_POINTER_GET_HEIGHT:
  125. path = QStringLiteral("pointer_getheight.cur");
  126. break;
  127. default:
  128. return QCursor();
  129. }
  130. path = QStringLiteral(":/cursors/res/") + path;
  131. QPixmap pm(path);
  132. if (!pm.isNull() && (hotX < 0 || hotY < 0))
  133. {
  134. QFile f(path);
  135. f.open(QFile::ReadOnly);
  136. QDataStream stream(&f);
  137. stream.setByteOrder(QDataStream::LittleEndian);
  138. f.read(10);
  139. quint16 x;
  140. stream >> x;
  141. hotX = x;
  142. stream >> x;
  143. hotY = x;
  144. }
  145. return QCursor(pm, hotX, hotY);
  146. }
  147. //////////////////////////////////////////////////////////////////////////-
  148. QString TrimTrailingZeros(QString str)
  149. {
  150. if (str.contains('.'))
  151. {
  152. for (int p = str.size() - 1; p >= 0; --p)
  153. {
  154. if (str.at(p) == '.')
  155. {
  156. return str.left(p);
  157. }
  158. else if (str.at(p) != '0')
  159. {
  160. return str.left(p + 1);
  161. }
  162. }
  163. return QString("0");
  164. }
  165. return str;
  166. }
  167. //////////////////////////////////////////////////////////////////////////
  168. QColor ColorLinearToGamma(ColorF col)
  169. {
  170. float r = clamp_tpl(col.r, 0.0f, 1.0f);
  171. float g = clamp_tpl(col.g, 0.0f, 1.0f);
  172. float b = clamp_tpl(col.b, 0.0f, 1.0f);
  173. float a = clamp_tpl(col.a, 0.0f, 1.0f);
  174. r = AZ::Color::ConvertSrgbLinearToGamma(r);
  175. g = AZ::Color::ConvertSrgbLinearToGamma(g);
  176. b = AZ::Color::ConvertSrgbLinearToGamma(b);
  177. return QColor(int(r * 255.0f), int(g * 255.0f), int(b * 255.0f), int(a * 255.0f));
  178. }
  179. //////////////////////////////////////////////////////////////////////////
  180. ColorF ColorGammaToLinear(const QColor& col)
  181. {
  182. float r = (float)col.red() / 255.0f;
  183. float g = (float)col.green() / 255.0f;
  184. float b = (float)col.blue() / 255.0f;
  185. float a = (float)col.alpha() / 255.0f;
  186. return ColorF(AZ::Color::ConvertSrgbGammaToLinear(r),
  187. AZ::Color::ConvertSrgbGammaToLinear(g),
  188. AZ::Color::ConvertSrgbGammaToLinear(b), a);
  189. }
  190. QColor ColorToQColor(uint32 color)
  191. {
  192. return QColor::fromRgbF((float)GetRValue(color) / 255.0f,
  193. (float)GetGValue(color) / 255.0f,
  194. (float)GetBValue(color) / 255.0f);
  195. }
  196. namespace EditorUtils
  197. {
  198. AZ_PUSH_DISABLE_WARNING(4273, "-Wunknown-warning-option")
  199. AzWarningAbsorber::AzWarningAbsorber(const char* window)
  200. : m_window(window)
  201. AZ_POP_DISABLE_WARNING
  202. {
  203. BusConnect();
  204. }
  205. AzWarningAbsorber::~AzWarningAbsorber()
  206. {
  207. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  208. }
  209. bool AzWarningAbsorber::OnPreWarning(const char* window, const char*, int, const char*, const char*)
  210. {
  211. if ((window)&&(m_window == window))
  212. {
  213. return true;
  214. }
  215. return false;
  216. }
  217. const char* LevelFile::GetOldCryFileExtension()
  218. {
  219. const char* oldCryExtension = nullptr;
  220. EditorInternal::EditorToolsApplicationRequestBus::BroadcastResult(
  221. oldCryExtension, &EditorInternal::EditorToolsApplicationRequests::GetOldCryLevelExtension);
  222. AZ_Assert(oldCryExtension, "Cannot retrieve file extension");
  223. return oldCryExtension;
  224. }
  225. const char* LevelFile::GetDefaultFileExtension()
  226. {
  227. const char* levelExtension = nullptr;
  228. EditorInternal::EditorToolsApplicationRequestBus::BroadcastResult(
  229. levelExtension, &EditorInternal::EditorToolsApplicationRequests::GetLevelExtension);
  230. AZ_Assert(levelExtension, "Cannot retrieve file extension");
  231. return levelExtension;
  232. }
  233. } // namespace EditorUtils