FloatSize.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <AK/LogStream.h>
  4. #include <LibDraw/Orientation.h>
  5. class FloatSize {
  6. public:
  7. FloatSize() {}
  8. FloatSize(float w, float 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. float width() const { return m_width; }
  16. float height() const { return m_height; }
  17. float area() const { return width() * height(); }
  18. void set_width(float w) { m_width = w; }
  19. void set_height(float h) { m_height = h; }
  20. bool operator==(const FloatSize& other) const
  21. {
  22. return m_width == other.m_width && m_height == other.m_height;
  23. }
  24. bool operator!=(const FloatSize& other) const
  25. {
  26. return !(*this == other);
  27. }
  28. FloatSize& operator-=(const FloatSize& other)
  29. {
  30. m_width -= other.m_width;
  31. m_height -= other.m_height;
  32. return *this;
  33. }
  34. FloatSize& operator+=(const FloatSize& other)
  35. {
  36. m_width += other.m_width;
  37. m_height += other.m_height;
  38. return *this;
  39. }
  40. float 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, float value)
  45. {
  46. if (orientation == Orientation::Vertical)
  47. set_height(value);
  48. else
  49. set_width(value);
  50. }
  51. float 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, float 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("[%gx%g]", m_width, m_height); }
  63. private:
  64. float m_width { 0 };
  65. float m_height { 0 };
  66. };
  67. inline const LogStream& operator<<(const LogStream& stream, const FloatSize& value)
  68. {
  69. return stream << value.to_string();
  70. }