FlexRegion.h 4.1 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 <stdint.h>
  26. #include "../math/IVector.h"
  27. #include "../math/IRect.h"
  28. #include "../api/stringAPI.h"
  29. #include "../persistent/ClassFactory.h"
  30. namespace dsr {
  31. struct FlexValue : public Persistent {
  32. PERSISTENT_DECLARATION(FlexValue)
  33. private:
  34. int32_t ratio = 0; // 0% to 100%
  35. int32_t offset = 0; // +- offset
  36. public:
  37. FlexValue() {}
  38. FlexValue(int ratio, int offset) : ratio(std::min(std::max(0, ratio), 100)), offset(offset) {}
  39. public:
  40. bool assignValue(const ReadableString &text, const ReadableString &fromPath) override;
  41. String& toStreamIndented(String& out, const ReadableString& indentation) const override;
  42. public:
  43. int32_t getRatio() const { return this->ratio; }
  44. int32_t getOffset() const { return this->offset; }
  45. int32_t getValue(int32_t parentValue) const { return (parentValue * this->ratio) / 100 + this->offset; }
  46. };
  47. inline bool operator==(const FlexValue &left, const FlexValue &right) {
  48. return left.getRatio() == right.getRatio() && left.getOffset() == right.getOffset();
  49. }
  50. inline bool operator!=(const FlexValue &left, const FlexValue &right) {
  51. return !(left == right);
  52. }
  53. struct FlexRegion {
  54. public:
  55. // Indices: 0 = left, 1 = top, 2 = right, 3 = bottom
  56. FlexValue sides[4];
  57. public:
  58. void setLeft(const FlexValue &left) { this->sides[0] = left; }
  59. void setTop(const FlexValue &top) { this->sides[1] = top; }
  60. void setRight(const FlexValue &right) { this->sides[2] = right; }
  61. void setBottom(const FlexValue &bottom) { this->sides[3] = bottom; }
  62. void setLeft(const ReadableString &left) { this->sides[0] = FlexValue(left, U""); }
  63. void setTop(const ReadableString &top) { this->sides[1] = FlexValue(top, U""); }
  64. void setRight(const ReadableString &right) { this->sides[2] = FlexValue(right, U""); }
  65. void setBottom(const ReadableString &bottom) { this->sides[3] = FlexValue(bottom, U""); }
  66. public:
  67. // Full region
  68. FlexRegion() {
  69. this->sides[0] = FlexValue(0, 0);
  70. this->sides[1] = FlexValue(0, 0);
  71. this->sides[2] = FlexValue(100, 0);
  72. this->sides[3] = FlexValue(100, 0);
  73. }
  74. // Upper left aligned region
  75. explicit FlexRegion(const IRect &location) {
  76. this->sides[0] = FlexValue(0, location.left());
  77. this->sides[1] = FlexValue(0, location.top());
  78. this->sides[2] = FlexValue(0, location.right());
  79. this->sides[3] = 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->sides[0] = FlexValue(leftRatio, leftOffset);
  84. this->sides[1] = FlexValue(topRatio, topOffset);
  85. this->sides[2] = FlexValue(rightRatio, rightOffset);
  86. this->sides[3] = 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 IVector2D &parentSize);
  97. };
  98. }
  99. #endif