summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Common/Code/Rct.h
blob: 218a34540ad28ec275146e836b59504b8148b654 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************************
**
** Copyright (C) 2016 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef INCLUDED_RCT_H
#define INCLUDED_RCT_H 1

#pragma once

#include "Pt.h"

#include <QRect>

#ifdef WIN32
#include <Windows.h>
#endif

class CRct
{
public:
    CRct();
    CRct(long inX, long inY, long inWidth, long inHeight);
    CRct(const CPt &inPos, const CPt &inSize);
    CRct(const CPt &inSize);
    CRct(const QRect &inRect);
#ifdef WIN32
    CRct(const RECT &inRect);
    operator RECT() const;
    CRct &operator=(const RECT &inRect);
#endif

    void Offset(const CPt &inOffset);

    bool IsInRect(const CPt &inPoint);

    void And(const CRct &inRect);
    void Or(const CRct &inRect);

    void Normalize();

    static long LesserOfTwo(const long &inA, const long &inB);
    static long GreaterOfTwo(const long &inA, const long &inB);

    operator QRect() const;

    CPt position;
    CPt size;
};

//=============================================================================
/**
 * Constructs a rect of points 0,0,0,0.
 */
inline CRct::CRct()
{
}

//=============================================================================
/**
 * Constructs a rect of the points.
 * @param inX the X position of the top left of the rectangle.
 * @param inY the Y position of the top left of the rectangle.
 * @param inWidth the width of the rectangle.
 * @param inHeight the height of the rectangle.
 */
inline CRct::CRct(long inX, long inY, long inWidth, long inHeight)
    : position(inX, inY)
    , size(inWidth, inHeight)
{
}

//=============================================================================
/**
 * Constructs a rect of the position and size.
 * @param inPos the position of the top left of the rectangle.
 * @param inSize the width and height of the rectangle.
 */
inline CRct::CRct(const CPt &inPos, const CPt &inSize)
    : position(inPos)
    , size(inSize)
{
}

//=============================================================================
/**
 * Constructs a rectangle at 0,0 with size inSize.
 * @param inSize the width and height of the rectangle.
 */
inline CRct::CRct(const CPt &inSize)
    : size(inSize)
{
}

inline CRct::CRct(const QRect &inRect)
    : position(inRect.x(), inRect.y())
    , size(inRect.width(), inRect.height())
{
}

#ifdef WIN32
inline CRct::CRct(const RECT &inRect)
    : position(inRect.left, inRect.top)
    , size(inRect.right - inRect.left, inRect.bottom - inRect.top)
{
}

inline CRct::operator RECT() const
{
    RECT theRect = { position.x, position.y, position.x + size.x, position.y + size.y };
    return theRect;
}

inline CRct &CRct::operator=(const RECT &inRect)
{
    position.x = inRect.left;
    position.y = inRect.top;
    size.x = inRect.right - inRect.left;
    size.y = inRect.bottom - inRect.top;

    return *this;
}
#endif // _WIN32

//=============================================================================
/**
 * Offset the rectangle.
 */
inline void CRct::Offset(const CPt &inOffset)
{
    position += inOffset;
    //	size += inOffset;
}

//=============================================================================
/**
 * @return true if the point is in this rectangle.
 */
inline bool CRct::IsInRect(const CPt &inPoint)
{
    if (inPoint.x >= position.x && inPoint.x < (position.x + size.x) && inPoint.y >= position.y
        && inPoint.y < (position.y + size.y))
        return true;
    return false;
}

//=============================================================================
/**
 * Ease method to return the larger of the two numbers.
 * @param inA the first number to check.
 * @param inB the second number to check.
 * @return the larger of the two numbers.
 */
inline long CRct::GreaterOfTwo(const long &inA, const long &inB)
{
    return (inA > inB) ? inA : inB;
}

//=============================================================================
/**
 * Ease method to return the smaller of the two numbers.
 * @param inA the first number to check.
 * @param inB the second number to check.
 * @return the smaller of the two numbers.
 */
inline long CRct::LesserOfTwo(const long &inA, const long &inB)
{
    return (inA < inB) ? inA : inB;
}

//=============================================================================
/**
 * Combine inRect with this rect, setting this rect to the union of the two.
 * This will modify this rect and make it only the area that exists in both
 * rects.
 * @param inRect the rect to merge with this one.
 */
inline void CRct::And(const CRct &inRect)
{
    long theXPos = GreaterOfTwo(position.x, inRect.position.x);
    long theXSize = LesserOfTwo(position.x + size.x, inRect.position.x + inRect.size.x) - theXPos;
    theXSize = GreaterOfTwo(0, theXSize);

    long theYPos = GreaterOfTwo(position.y, inRect.position.y);
    long theYSize = LesserOfTwo(position.y + size.y, inRect.position.y + inRect.size.y) - theYPos;
    theYSize = GreaterOfTwo(0, theYSize);

    position.x = theXPos;
    position.y = theYPos;
    size.x = theXSize;
    size.y = theYSize;
}

//=============================================================================
/**
 * Combine inRect with this rect, making this the smallest rect containing both.
 * This will modify this rect to make it the smallest possible rect that
 * contains the entire area of both rects. If this rect's size is 0 then it
 * this will be set only to inRect.
 * @param inRect the rect to merge with this.
 */
inline void CRct::Or(const CRct &inRect)
{
    long theXPos;
    long theXSize;
    if (size.x == 0) {
        theXPos = inRect.position.x;
        theXSize = inRect.size.x;
    } else {
        theXPos = LesserOfTwo(position.x, inRect.position.x);
        theXSize = GreaterOfTwo(position.x + size.x, inRect.position.x + inRect.size.x) - theXPos;
    }

    long theYPos;
    long theYSize;
    if (size.y == 0) {
        theYPos = inRect.position.y;
        theYSize = inRect.size.y;
    } else {
        theYPos = LesserOfTwo(position.y, inRect.position.y);
        theYSize = GreaterOfTwo(position.y + size.y, inRect.position.y + inRect.size.y) - theYPos;
    }

    position.x = theXPos;
    position.y = theYPos;
    size.x = theXSize;
    size.y = theYSize;
}

//=============================================================================
/**
 * Normalize this rectangle so that it's size is positive.
 * If the size is negative in either direction then this will modify the
 * position and the size so that the size becomes positive. The position will
 * be modified so this will still cover the same area.
 */
inline void CRct::Normalize()
{
    if (size.x < 0) {
        position.x += size.x;
        size.x = -size.x;
    }
    if (size.y < 0) {
        position.y += size.y;
        size.y = -size.y;
    }
}

inline CRct::operator QRect() const
{
    return QRect(position.x, position.y, size.x, size.y);
}

#endif // INCLUDED_RCT_H