FlexRegion.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_GUI_FLEXREGION
  24. #define DFPSR_GUI_FLEXREGION
  25. #include <cstdint>
  26. #include "../../math/IVector.h"
  27. #include "../../math/IRect.h"
  28. #include "../math/scalar.h"
  29. #include "../../api/stringAPI.h"
  30. #include "../persistent/ClassFactory.h"
  31. namespace dsr {
  32. struct FlexValue : public Persistent {
  33. PERSISTENT_DECLARATION(FlexValue)
  34. private:
  35. int32_t ratio = 0; // 0% to 100%
  36. int32_t offset = 0; // +- offset
  37. public:
  38. FlexValue() {}
  39. FlexValue(int ratio, int offset) : ratio(min(max(0, ratio), 100)), offset(offset) {}
  40. public:
  41. bool assignValue(const ReadableString &text, const ReadableString &fromPath) override;
  42. String& toStreamIndented(String& out, const ReadableString& indentation) const override;
  43. public:
  44. int32_t getRatio() const { return this->ratio; }
  45. int32_t getOffset() const { return this->offset; }
  46. int32_t getValue(int32_t minimum, int32_t maximum) const { return ((minimum * (100 - this->ratio)) + (maximum * this->ratio)) / 100 + this->offset; }
  47. };
  48. inline bool operator==(const FlexValue &left, const FlexValue &right) {
  49. return left.getRatio() == right.getRatio() && left.getOffset() == right.getOffset();
  50. }
  51. inline bool operator!=(const FlexValue &left, const FlexValue &right) {
  52. return !(left == right);
  53. }
  54. struct FlexRegion {
  55. public:
  56. FlexValue left, top, right, bottom;
  57. public:
  58. void setLeft(const FlexValue &left) { this->left = left; }
  59. void setTop(const FlexValue &top) { this->top = top; }
  60. void setRight(const FlexValue &right) { this->right = right; }
  61. void setBottom(const FlexValue &bottom) { this->bottom = bottom; }
  62. void setLeft(const ReadableString &left) { this->left = FlexValue(left, U""); }
  63. void setTop(const ReadableString &top) { this->top = FlexValue(top, U""); }
  64. void setRight(const ReadableString &right) { this->right = FlexValue(right, U""); }
  65. void setBottom(const ReadableString &bottom) { this->bottom = FlexValue(bottom, U""); }
  66. public:
  67. // Full region
  68. FlexRegion() {
  69. this->left = FlexValue(0, 0);
  70. this->top = FlexValue(0, 0);
  71. this->right = FlexValue(100, 0);
  72. this->bottom = FlexValue(100, 0);
  73. }
  74. // Upper left aligned region
  75. explicit FlexRegion(const IRect &location) {
  76. this->left = FlexValue(0, location.left());
  77. this->top = FlexValue(0, location.top());
  78. this->right = FlexValue(0, location.right());
  79. this->bottom = FlexValue(0, location.bottom());
  80. }
  81. // Flexible region
  82. FlexRegion(int leftRatio, int leftOffset, int topRatio, int topOffset, int rightRatio, int rightOffset, int bottomRatio, int bottomOffset) {
  83. this->left = FlexValue(leftRatio, leftOffset);
  84. this->top = FlexValue(topRatio, topOffset);
  85. this->right = FlexValue(rightRatio, rightOffset);
  86. this->bottom = FlexValue(bottomRatio, bottomOffset);
  87. }
  88. // Parse individual flex values from text
  89. FlexRegion(const ReadableString &left, const ReadableString &top, const ReadableString &right, const ReadableString &bottom) {
  90. this->setLeft(left);
  91. this->setTop(top);
  92. this->setRight(right);
  93. this->setBottom(bottom);
  94. }
  95. public:
  96. virtual IRect getNewLocation(const IRect &givenSpace);
  97. };
  98. }
  99. #endif