Size.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <AK/LogStream.h>
  4. #include <LibDraw/Orientation.h>
  5. class Size {
  6. public:
  7. Size() {}
  8. Size(int w, int h)
  9. : m_width(w)
  10. , m_height(h)
  11. {
  12. }
  13. bool is_null() const { return !m_width && !m_height; }
  14. bool is_empty() const { return m_width <= 0 || m_height <= 0; }
  15. int width() const { return m_width; }
  16. int height() const { return m_height; }
  17. int area() const { return width() * height(); }
  18. void set_width(int w) { m_width = w; }
  19. void set_height(int h) { m_height = h; }
  20. bool operator==(const Size& other) const
  21. {
  22. return m_width == other.m_width && m_height == other.m_height;
  23. }
  24. bool operator!=(const Size& other) const
  25. {
  26. return !(*this == other);
  27. }
  28. Size& operator-=(const Size& other)
  29. {
  30. m_width -= other.m_width;
  31. m_height -= other.m_height;
  32. return *this;
  33. }
  34. Size& operator+=(const Size& other)
  35. {
  36. m_width += other.m_width;
  37. m_height += other.m_height;
  38. return *this;
  39. }
  40. int primary_size_for_orientation(Orientation orientation) const
  41. {
  42. return orientation == Orientation::Vertical ? height() : width();
  43. }
  44. void set_primary_size_for_orientation(Orientation orientation, int value)
  45. {
  46. if (orientation == Orientation::Vertical)
  47. set_height(value);
  48. else
  49. set_width(value);
  50. }
  51. int secondary_size_for_orientation(Orientation orientation) const
  52. {
  53. return orientation == Orientation::Vertical ? width() : height();
  54. }
  55. void set_secondary_size_for_orientation(Orientation orientation, int value)
  56. {
  57. if (orientation == Orientation::Vertical)
  58. set_width(value);
  59. else
  60. set_height(value);
  61. }
  62. String to_string() const { return String::format("[%dx%d]", m_width, m_height); }
  63. private:
  64. int m_width { 0 };
  65. int m_height { 0 };
  66. };
  67. inline const LogStream& operator<<(const LogStream& stream, const Size& value)
  68. {
  69. return stream << value.to_string();
  70. }