No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

size.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef SIZE_H
  24. #define SIZE_H
  25. #include "prerequisites.h"
  26. #include "point.h"
  27. enum ESizeScaleMode {
  28. IGNORE_ASPECT_RATIO,
  29. KEEP_ASPECT_RATIO,
  30. KEEP_ASPECT_RATIO_BY_EXPANDING
  31. };
  32. template <class T>
  33. class TSize
  34. {
  35. public:
  36. TSize() : wd(-1), ht(-1) {};
  37. TSize(T width, T height) : wd(width), ht(height) { };
  38. TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
  39. inline TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
  40. inline bool isNull() const { return wd==0 && ht==0; }
  41. inline bool isEmpty() const { return wd<1 || ht<1; }
  42. inline bool isValid() const { return wd>=0 && ht>=0; }
  43. inline int width() const { return wd; }
  44. inline int height() const { return ht; }
  45. inline void setWidth(T w) { wd = w; }
  46. inline void setHeight(T h) { ht = h; }
  47. inline TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
  48. inline TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht); }
  49. inline TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
  50. inline TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht); }
  51. inline TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
  52. inline TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v); }
  53. inline TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
  54. inline TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v); }
  55. inline TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
  56. inline bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
  57. inline bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
  58. inline bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
  59. inline bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
  60. inline TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
  61. inline bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
  62. inline bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
  63. inline TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
  64. inline TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
  65. void scale(const TSize<T>& s, ESizeScaleMode mode) {
  66. if(mode == IGNORE_ASPECT_RATIO || wd == 0 || ht == 0) {
  67. wd = s.wd;
  68. ht = s.ht;
  69. } else {
  70. bool useHeight;
  71. T rw = (s.ht * wd) / ht;
  72. if(mode == KEEP_ASPECT_RATIO)
  73. useHeight = (rw <= s.wd);
  74. else // mode == KEEP_ASPECT_RATIO_BY_EXPANDING
  75. useHeight = (rw >= s.wd);
  76. if(useHeight) {
  77. wd = rw;
  78. ht = s.ht;
  79. } else {
  80. ht = (s.wd * ht)/wd;
  81. wd = s.wd;
  82. }
  83. }
  84. }
  85. void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
  86. inline float ratio() const { return (float)wd/ht; }
  87. inline T area() const { return wd*ht; }
  88. private:
  89. T wd, ht;
  90. };
  91. typedef TSize<int> Size;
  92. typedef TSize<float> SizeF;
  93. #endif