summaryrefslogtreecommitdiff
path: root/src/T2DLL/URect.cpp
blob: e2cd64f6195e4df84551666c7debd1680fc210f5 (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
#include "StdAfx.h"
#include "URect.h"

/*static*/ void URect::SetEmpty(RECT& outRect) {
    ::SetRect(&outRect, 0, 0, 0, 0);
}

/*static*/ void URect::Center(const RECT& inRect, RECT& ioRect, BOOL inFlag) {
#pragma var_order(yOffset, xRatio, xOffset, ratio, width1, height1, yRatio, width2, height2)
    int width1 = Width(inRect);
    int height1 = Height(inRect);
    int width2 = Width(ioRect);
    int height2 = Height(ioRect);
    int xRatio, yRatio, ratio;

    if (inFlag) {
        xRatio = width1 / width2;
        yRatio = height1 / height2;
        ratio = (xRatio > yRatio) ? yRatio : xRatio;
        width2 *= ratio;
        height2 *= ratio;
    }

    int xOffset = inRect.left + ((width1 - width2) / 2);
    int yOffset = inRect.top + ((height1 - height2) / 2);
    ioRect.left = xOffset;
    ioRect.top = yOffset;
    ioRect.right = ioRect.left + width2;
    ioRect.bottom = ioRect.top + height2;
}

inline /*static*/ int URect::Width(const RECT& inRect) {
    return inRect.right - inRect.left;
}

inline /*static*/ int URect::Height(const RECT& inRect) {
    return inRect.bottom - inRect.top;
}

inline /*static*/ void URect::Union(const RECT& inRect1, const RECT& inRect2, RECT& outRect) {
    if (::IsRectEmpty(&inRect1)) {
        outRect = inRect2;
    } else if (::IsRectEmpty(&inRect2)) {
        outRect = inRect1;
    } else {
        CRect result;
        result.IntersectRect(&inRect1, &inRect2);
        ::SetRect(&outRect, result.left, result.top, result.right, result.bottom);
    }
}

inline /*static*/ POINT URect::Center(const RECT& inRect) {
    POINT pt;
    pt.x = (inRect.left + inRect.right) / 2;
    pt.y = (inRect.top + inRect.bottom) / 2;
    return pt;
}