Point.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibGfx/Point.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibIPC/Decoder.h>
  10. #include <LibIPC/Encoder.h>
  11. namespace Gfx {
  12. template<typename T>
  13. void Point<T>::constrain(Rect<T> const& rect)
  14. {
  15. if (x() < rect.left()) {
  16. set_x(rect.left());
  17. } else if (x() > rect.right()) {
  18. set_x(rect.right());
  19. }
  20. if (y() < rect.top()) {
  21. set_y(rect.top());
  22. } else if (y() > rect.bottom()) {
  23. set_y(rect.bottom());
  24. }
  25. }
  26. template<>
  27. String IntPoint::to_string() const
  28. {
  29. return String::formatted("[{},{}]", x(), y());
  30. }
  31. template<>
  32. String FloatPoint::to_string() const
  33. {
  34. return String::formatted("[{},{}]", x(), y());
  35. }
  36. }
  37. namespace IPC {
  38. bool encode(Encoder& encoder, Gfx::IntPoint const& point)
  39. {
  40. encoder << point.x() << point.y();
  41. return true;
  42. }
  43. bool decode(Decoder& decoder, Gfx::IntPoint& point)
  44. {
  45. int x = 0;
  46. int y = 0;
  47. if (!decoder.decode(x))
  48. return false;
  49. if (!decoder.decode(y))
  50. return false;
  51. point = { x, y };
  52. return true;
  53. }
  54. }
  55. template class Gfx::Point<int>;
  56. template class Gfx::Point<float>;