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

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