PredefinedAspectRatios.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "EditorDefs.h"
  9. #include "PredefinedAspectRatios.h"
  10. // Editor
  11. #include "Settings.h"
  12. CPredefinedAspectRatios::CPredefinedAspectRatios()
  13. {
  14. m_aspectRatios.reserve(10);
  15. AddAspectRatio(5, 4);
  16. AddAspectRatio(4, 3);
  17. AddAspectRatio(3, 2);
  18. AddAspectRatio(16, 10);
  19. AddAspectRatio(16, 9);
  20. AddAspectRatio(1.85f, 1);
  21. AddAspectRatio(2.39f, 1);
  22. }
  23. CPredefinedAspectRatios::~CPredefinedAspectRatios()
  24. {
  25. }
  26. void CPredefinedAspectRatios::AddAspectRatio(float x, int y)
  27. {
  28. if (y == 0)
  29. {
  30. return;
  31. }
  32. SAspectRatio aspectRatio;
  33. aspectRatio.name = QStringLiteral("%1:%2").arg(x, 0, 'f', 2).arg(y);
  34. aspectRatio.value = x / y;
  35. m_aspectRatios.push_back(aspectRatio);
  36. }
  37. void CPredefinedAspectRatios::AddAspectRatio(int x, int y)
  38. {
  39. if (y == 0)
  40. {
  41. return;
  42. }
  43. SAspectRatio aspectRatio;
  44. aspectRatio.name = QStringLiteral("%1:%2").arg(x).arg(y);
  45. aspectRatio.value = float( x ) / y;
  46. m_aspectRatios.push_back(aspectRatio);
  47. }
  48. float CPredefinedAspectRatios::GetCurrentValue() const
  49. {
  50. return gSettings.viewports.fDefaultAspectRatio;
  51. }
  52. bool CPredefinedAspectRatios::IsEmpty() const
  53. {
  54. return m_aspectRatios.empty();
  55. }
  56. size_t CPredefinedAspectRatios::GetCount() const
  57. {
  58. return m_aspectRatios.size();
  59. }
  60. const QString& CPredefinedAspectRatios::GetName(size_t aspectRatioId) const
  61. {
  62. bool validAspectRatioId = (aspectRatioId < GetCount());
  63. assert(validAspectRatioId);
  64. if (!validAspectRatioId)
  65. {
  66. static QString dummyAspectRatioName("1:1");
  67. return dummyAspectRatioName;
  68. }
  69. const SAspectRatio& aspectRatio = m_aspectRatios[ aspectRatioId ];
  70. return aspectRatio.name;
  71. }
  72. float CPredefinedAspectRatios::GetValue(size_t aspectRatioId) const
  73. {
  74. bool validAspectRatioId = (aspectRatioId < GetCount());
  75. assert(validAspectRatioId);
  76. if (!validAspectRatioId)
  77. {
  78. return 1;
  79. }
  80. const SAspectRatio& aspectRatio = m_aspectRatios[ aspectRatioId ];
  81. return aspectRatio.value;
  82. }
  83. bool CPredefinedAspectRatios::IsCurrent(size_t aspectRatioId) const
  84. {
  85. float selectedValue = GetValue(aspectRatioId);
  86. float currentValue = GetCurrentValue();
  87. const float THRESHOLD = 0.01f;
  88. bool valuesCloseEnough = (fabs(selectedValue - currentValue) <= THRESHOLD);
  89. return valuesCloseEnough;
  90. }