WndGridHelper.h 5.1 KB

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