Serenity Operating System
at hosted 117 lines 3.6 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 FloatSize { 36public: 37 FloatSize() {} 38 FloatSize(float w, float h) 39 : m_width(w) 40 , m_height(h) 41 { 42 } 43 44 bool is_null() const { return !m_width && !m_height; } 45 bool is_empty() const { return m_width <= 0 || m_height <= 0; } 46 47 float width() const { return m_width; } 48 float height() const { return m_height; } 49 50 float area() const { return width() * height(); } 51 52 void set_width(float w) { m_width = w; } 53 void set_height(float h) { m_height = h; } 54 55 bool operator==(const FloatSize& other) const 56 { 57 return m_width == other.m_width && m_height == other.m_height; 58 } 59 60 bool operator!=(const FloatSize& other) const 61 { 62 return !(*this == other); 63 } 64 65 FloatSize& operator-=(const FloatSize& other) 66 { 67 m_width -= other.m_width; 68 m_height -= other.m_height; 69 return *this; 70 } 71 72 FloatSize& operator+=(const FloatSize& other) 73 { 74 m_width += other.m_width; 75 m_height += other.m_height; 76 return *this; 77 } 78 79 float primary_size_for_orientation(Orientation orientation) const 80 { 81 return orientation == Orientation::Vertical ? height() : width(); 82 } 83 84 void set_primary_size_for_orientation(Orientation orientation, float value) 85 { 86 if (orientation == Orientation::Vertical) 87 set_height(value); 88 else 89 set_width(value); 90 } 91 92 float secondary_size_for_orientation(Orientation orientation) const 93 { 94 return orientation == Orientation::Vertical ? width() : height(); 95 } 96 97 void set_secondary_size_for_orientation(Orientation orientation, float value) 98 { 99 if (orientation == Orientation::Vertical) 100 set_width(value); 101 else 102 set_height(value); 103 } 104 105 String to_string() const { return String::format("[%gx%g]", m_width, m_height); } 106 107private: 108 float m_width { 0 }; 109 float m_height { 0 }; 110}; 111 112inline const LogStream& operator<<(const LogStream& stream, const FloatSize& value) 113{ 114 return stream << value.to_string(); 115} 116 117}