Serenity Operating System
at portability 147 lines 4.2 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/LogStream.h> 30#include <AK/String.h> 31#include <LibGfx/Orientation.h> 32 33namespace Gfx { 34 35class FloatRect; 36 37class FloatPoint { 38public: 39 FloatPoint() {} 40 FloatPoint(float x, float y) 41 : m_x(x) 42 , m_y(y) 43 { 44 } 45 float x() const { return m_x; } 46 float y() const { return m_y; } 47 48 void set_x(float x) { m_x = x; } 49 void set_y(float y) { m_y = y; } 50 51 void move_by(float dx, float dy) 52 { 53 m_x += dx; 54 m_y += dy; 55 } 56 57 void move_by(const FloatPoint& delta) 58 { 59 move_by(delta.x(), delta.y()); 60 } 61 62 FloatPoint translated(const FloatPoint& delta) const 63 { 64 FloatPoint point = *this; 65 point.move_by(delta); 66 return point; 67 } 68 69 FloatPoint translated(float dx, float dy) const 70 { 71 FloatPoint point = *this; 72 point.move_by(dx, dy); 73 return point; 74 } 75 76 void constrain(const FloatRect&); 77 78 bool operator==(const FloatPoint& other) const 79 { 80 return m_x == other.m_x 81 && m_y == other.m_y; 82 } 83 84 bool operator!=(const FloatPoint& other) const 85 { 86 return !(*this == other); 87 } 88 89 FloatPoint operator-() const { return { -m_x, -m_y }; } 90 91 FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; } 92 FloatPoint& operator-=(const FloatPoint& other) 93 { 94 m_x -= other.m_x; 95 m_y -= other.m_y; 96 return *this; 97 } 98 99 FloatPoint& operator+=(const FloatPoint& other) 100 { 101 m_x += other.m_x; 102 m_y += other.m_y; 103 return *this; 104 } 105 FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; } 106 107 String to_string() const { return String::format("[%g,%g]", x(), y()); } 108 109 bool is_null() const { return !m_x && !m_y; } 110 111 float primary_offset_for_orientation(Orientation orientation) const 112 { 113 return orientation == Orientation::Vertical ? y() : x(); 114 } 115 116 void set_primary_offset_for_orientation(Orientation orientation, float value) 117 { 118 if (orientation == Orientation::Vertical) 119 set_y(value); 120 else 121 set_x(value); 122 } 123 124 float secondary_offset_for_orientation(Orientation orientation) const 125 { 126 return orientation == Orientation::Vertical ? x() : y(); 127 } 128 129 void set_secondary_offset_for_orientation(Orientation orientation, float value) 130 { 131 if (orientation == Orientation::Vertical) 132 set_x(value); 133 else 134 set_y(value); 135 } 136 137private: 138 float m_x { 0 }; 139 float m_y { 0 }; 140}; 141 142inline const LogStream& operator<<(const LogStream& stream, const FloatPoint& value) 143{ 144 return stream << value.to_string(); 145} 146 147}