WndGridHelper.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef CRYINCLUDE_EDITOR_CONTROLS_WNDGRIDHELPER_H
  9. #define CRYINCLUDE_EDITOR_CONTROLS_WNDGRIDHELPER_H
  10. #pragma once
  11. #include <QPoint>
  12. #include <QRect>
  13. #include "Cry_Vector2.h"
  14. #include <AzCore/Casting/numeric_cast.h>
  15. //////////////////////////////////////////////////////////////////////////
  16. class CWndGridHelper
  17. {
  18. public:
  19. Vec2 zoom;
  20. Vec2 origin;
  21. Vec2 step;
  22. Vec2 pixelsPerGrid;
  23. int nMajorLines;
  24. QRect rect;
  25. QPoint nMinPixelsPerGrid;
  26. QPoint nMaxPixelsPerGrid;
  27. //////////////////////////////////////////////////////////////////////////
  28. QPoint firstGridLine;
  29. QPoint numGridLines;
  30. //////////////////////////////////////////////////////////////////////////
  31. CWndGridHelper()
  32. {
  33. zoom = Vec2(1, 1);
  34. step = Vec2(10, 10);
  35. pixelsPerGrid = Vec2(10, 10);
  36. origin = Vec2(0, 0);
  37. nMajorLines = 10;
  38. nMinPixelsPerGrid = QPoint(50, 10);
  39. nMaxPixelsPerGrid = QPoint(100, 20);
  40. firstGridLine = QPoint(0, 0);
  41. numGridLines = QPoint(0, 0);
  42. }
  43. //////////////////////////////////////////////////////////////////////////
  44. Vec2 ClientToWorld(const QPoint& point)
  45. {
  46. Vec2 v;
  47. v.x = (point.x() - rect.left()) / zoom.x + origin.x;
  48. v.y = (point.y() - rect.top()) / zoom.y + origin.y;
  49. return v;
  50. }
  51. //////////////////////////////////////////////////////////////////////////
  52. QPoint WorldToClient(Vec2 v)
  53. {
  54. QPoint p(aznumeric_cast<int>(floor((v.x - origin.x) * zoom.x + 0.5f) + rect.left()),
  55. aznumeric_cast<int>(floor((v.y - origin.y) * zoom.y + 0.5f) + rect.top()));
  56. return p;
  57. }
  58. void SetOrigin(Vec2 neworigin)
  59. {
  60. origin = neworigin;
  61. }
  62. void SetZoom(Vec2 newzoom)
  63. {
  64. zoom = newzoom;
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. void SetZoom(Vec2 newzoom, const QPoint& center)
  68. {
  69. if (newzoom.x < 0.01f)
  70. {
  71. newzoom.x = 0.01f;
  72. }
  73. if (newzoom.y < 0.01f)
  74. {
  75. newzoom.y = 0.01f;
  76. }
  77. // Zoom to mouse position.
  78. float ofsx = origin.x;
  79. float ofsy = origin.y;
  80. Vec2 z1 = zoom;
  81. Vec2 z2 = newzoom;
  82. zoom = newzoom;
  83. // Calculate new offset to center zoom on mouse.
  84. float x2 = aznumeric_cast<float>(center.x() - rect.left());
  85. float y2 = aznumeric_cast<float>(center.y() - rect.top());
  86. ofsx = -(x2 / z2.x - x2 / z1.x - ofsx);
  87. ofsy = -(y2 / z2.y - y2 / z1.y - ofsy);
  88. origin.x = ofsx;
  89. origin.y = ofsy;
  90. }
  91. void CalculateGridLines()
  92. {
  93. pixelsPerGrid.x = zoom.x;
  94. pixelsPerGrid.y = zoom.y;
  95. step = Vec2(1.00f, 1.00f);
  96. nMajorLines = 2;
  97. int griditers;
  98. if (pixelsPerGrid.x <= nMinPixelsPerGrid.x())
  99. {
  100. griditers = 0;
  101. while (pixelsPerGrid.x <= nMinPixelsPerGrid.x() && griditers++ < 1000)
  102. {
  103. step.x = step.x * nMajorLines;
  104. pixelsPerGrid.x = step.x * zoom.x;
  105. }
  106. }
  107. else
  108. {
  109. griditers = 0;
  110. while (pixelsPerGrid.x >= nMaxPixelsPerGrid.x() && griditers++ < 1000)
  111. {
  112. step.x = step.x / nMajorLines;
  113. pixelsPerGrid.x = step.x * zoom.x;
  114. }
  115. }
  116. if (pixelsPerGrid.y <= nMinPixelsPerGrid.y())
  117. {
  118. griditers = 0;
  119. while (pixelsPerGrid.y <= nMinPixelsPerGrid.y() && griditers++ < 1000)
  120. {
  121. step.y = step.y * nMajorLines;
  122. pixelsPerGrid.y = step.y * zoom.y;
  123. }
  124. }
  125. else
  126. {
  127. griditers = 0;
  128. while (pixelsPerGrid.y >= nMaxPixelsPerGrid.y() && griditers++ < 1000)
  129. {
  130. step.y = step.y / nMajorLines;
  131. pixelsPerGrid.y = step.y * zoom.y;
  132. }
  133. }
  134. firstGridLine.rx() = aznumeric_cast<int>(origin.x / step.x);
  135. firstGridLine.ry() = aznumeric_cast<int>(origin.y / step.y);
  136. numGridLines.rx() = aznumeric_cast<int>((rect.width() / zoom.x) / step.x + 1);
  137. numGridLines.ry() = aznumeric_cast<int>((rect.height() / zoom.y) / step.y + 1);
  138. }
  139. int GetGridLineX(int nGridLineX) const
  140. {
  141. return aznumeric_cast<int>(floor((nGridLineX * step.x - origin.x) * zoom.x + 0.5f));
  142. }
  143. int GetGridLineY(int nGridLineY) const
  144. {
  145. return aznumeric_cast<int>(floor((nGridLineY * step.y - origin.y) * zoom.y + 0.5f));
  146. }
  147. float GetGridLineXValue(int nGridLineX) const
  148. {
  149. return (nGridLineX * step.x);
  150. }
  151. float GetGridLineYValue(int nGridLineY) const
  152. {
  153. return (nGridLineY * step.y);
  154. }
  155. };
  156. #endif // CRYINCLUDE_EDITOR_CONTROLS_WNDGRIDHELPER_H