GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/rect.h Lines: 28 36 77.8 %
Date: 2021-03-17 Branches: 5 82 6.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/*      _______   __   __   __   ______   __   __   _______   __   __
23
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
24
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
25
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
26
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
27
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
28
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
29
 *
30
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
31
 *
32
 *
33
 * Per Larsson a.k.a finalman
34
 * Olof Naessén a.k.a jansem/yakslem
35
 *
36
 * Visit: http://guichan.sourceforge.net
37
 *
38
 * License: (BSD)
39
 * Redistribution and use in source and binary forms, with or without
40
 * modification, are permitted provided that the following conditions
41
 * are met:
42
 * 1. Redistributions of source code must retain the above copyright
43
 *    notice, this list of conditions and the following disclaimer.
44
 * 2. Redistributions in binary form must reproduce the above copyright
45
 *    notice, this list of conditions and the following disclaimer in
46
 *    the documentation and/or other materials provided with the
47
 *    distribution.
48
 * 3. Neither the name of Guichan nor the names of its contributors may
49
 *    be used to endorse or promote products derived from this software
50
 *    without specific prior written permission.
51
 *
52
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
58
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
59
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
60
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
61
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63
 */
64
65
#ifndef GUI_RECT_H
66
#define GUI_RECT_H
67
68
#include "localconsts.h"
69
70
/**
71
  * Represents a rectangle.
72
  */
73
class Rect notfinal
74
{
75
    public:
76
        /**
77
          * Constructor. The default rectangle is an empty rectangle
78
          * at the coordinates (0,0).
79
          */
80
2681
        constexpr Rect() :
81
            x(0),
82
            y(0),
83
            width(0),
84
2877
            height(0)
85
        {
86
        }
87
88
        /**
89
          * Constructor.
90
          *
91
          * @param x_ The x coordinate of the rectangle.
92
          * @param y_ The y coordinate of the rectangle.
93
          * @param width_ The width of the rectangle.
94
          * @param height_ The height of the rectangle.
95
          */
96
        constexpr Rect(const int x_,
97
                       const int y_,
98
                       const int width_,
99
201951
                       const int height_) :
100
            x(x_),
101
            y(y_),
102
            width(width_),
103
202891
            height(height_)
104
        {
105
        }
106
107
25291
        constexpr Rect(const Rect &r) :
108
22875
            x(r.x),
109
22677
            y(r.y),
110
19369
            width(r.width),
111
90144
            height(r.height)
112
        {
113
        }
114
115
        A_DEFAULT_COPY(Rect)
116
117
        Rect &operator=(const Rect &r)
118
        {
119
13048
            x = r.x;
120
13048
            y = r.y;
121
13048
            width = r.width;
122
13048
            height = r.height;
123
            return *this;
124
        }
125
126
        virtual ~Rect()
127
15729
        { }
128
129
        /**
130
          * Sets the dimension of a rectangle.
131
          *
132
          * @param x0 The x coordinate of the rectangle.
133
          * @param y0 The y coordinate of the rectangle.
134
          * @param width0 The width of the rectangle.
135
          * @param height0 The height of the rectangle.
136
          */
137
        void setAll(const int x0,
138
                    const int y0,
139
                    const int width0,
140
                    const int height0)
141
        {
142
            x = x0;
143
            y = y0;
144
            width = width0;
145
            height = height0;
146
        }
147
148
        /**
149
          * Checks if another rectangle intersects with the rectangle.
150
          *
151
          * @param rectangle Another rectangle to check for intersection.
152
          * @return True if the rectangles intersect, false otherwise.
153
          */
154
2
        bool isIntersecting(const Rect& rectangle) const A_WARN_UNUSED
155
        {
156
2
            int x_ = x;
157
2
            int y_ = y;
158
2
            int width_ = width;
159
2
            int height_ = height;
160
161
2
            x_ -= rectangle.x;
162
2
            y_ -= rectangle.y;
163
164
2
            if (x_ < 0)
165
            {
166
                width_ += x_;
167
            }
168
2
            else if (x_ + width_ > rectangle.width)
169
            {
170
2
                width_ = rectangle.width - x_;
171
            }
172
173
2
            if (y_ < 0)
174
            {
175
                height_ += y_;
176
            }
177
2
            else if (y_ + height_ > rectangle.height)
178
            {
179
2
                height_ = rectangle.height - y_;
180
            }
181
182
2
            if (width_ <= 0 || height_ <= 0)
183
            {
184
                return false;
185
            }
186
187
            return true;
188
        }
189
190
        /**
191
          * Checks if a point is inside the rectangle
192
          *
193
          * @param x_ The x coordinate of the point.
194
          * @param y_ The y coordinate of the point.
195
          * @return True if the point is inside the rectangle.
196
          */
197
        bool isPointInRect(const int x_,
198
                           const int y_) const A_WARN_UNUSED
199
        {
200
            return x_ >= x
201
                && y_ >= y
202
                && x_ < x + width
203
                && y_ < y + height;
204
        }
205
206
        /**
207
          * Holds the x coordinate of the rectangle.
208
          */
209
        int x;
210
211
        /**
212
          * Holds the x coordinate of the rectangle.
213
          */
214
        int y;
215
216
        /**
217
          * Holds the width of the rectangle.
218
          */
219
        int width;
220
221
        /**
222
          * Holds the height of the rectangle.
223
          */
224
        int height;
225
};
226
227
#endif  // GUI_RECT_H