aboutsummaryrefslogtreecommitdiffstats
path: root/skins/skin_rect.c
blob: d60dbd2f15e8c435913abd95ee5ace4c4520b0fb (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
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include "skin_rect.h"
#include <limits.h>

#define  SKIN_POS_INITIALIZER   { 0, 0 }

void
skin_pos_rotate( SkinPos*  dst, SkinPos*  src, SkinRotation  rotation )
{
    int  x = src->x;
    int  y = src->y;

    switch ( rotation & 3 ) {
    case SKIN_ROTATION_0:
        dst->x = x;
        dst->y = y;
        break;

    case SKIN_ROTATION_90:
        dst->x = -y;
        dst->y =  x;
        break;

    case SKIN_ROTATION_180:
        dst->x = -x;
        dst->y = -y;
        break;

    default:
        dst->x =  y;
        dst->y = -x;
    }
}


#define  SKIN_SIZE_INITIALIZER  { 0, 0 }

int
skin_size_contains( SkinSize*  size, int  x, int  y )
{
    return ( (unsigned) x < (unsigned) size->w &&
             (unsigned) y < (unsigned) size->h );
}

void
skin_size_rotate( SkinSize*  dst, SkinSize*  src, SkinRotation  rot )
{
    int  w = src->w;
    int  h = src->h;

    if ((rot & 1) != 0) {
        dst->w = h;
        dst->h = w;
    } else {
        dst->w = w;
        dst->h = h;
    }
}

/** SKIN RECTANGLES
 **/
#define  SKIN_RECT_INITIALIZER  { SKIN_POS_INITIALIZER, SKIN_SIZE_INITIALIZER }

void
skin_rect_init( SkinRect*  r, int x, int  y, int  w, int  h )
{
    if (w < 0 || h < 0)
        x = y = w = h = 0;

    r->pos.x  = x;
    r->pos.y  = y;
    r->size.w = w;
    r->size.h = h;
}


void
skin_rect_translate( SkinRect*  r, int  dx, int  dy )
{
    r->pos.x += dx;
    r->pos.y += dy;
}


void
skin_rect_rotate( SkinRect*  dst, SkinRect*  src, SkinRotation  rot )
{
    int  x, y, w, h;

    switch (rot & 3) {
        case SKIN_ROTATION_90:
            x = src->pos.x;
            y = src->pos.y;
            w = src->size.w;
            h = src->size.h;
            dst->pos.x  = -(y + h);
            dst->pos.y  = x;
            dst->size.w = h;
            dst->size.h = w;
            break;

        case SKIN_ROTATION_180:
            dst->pos.x = -(src->pos.x + src->size.w);
            dst->pos.y = -(src->pos.y + src->size.h);
            dst->size  = src->size;
            break;

        case SKIN_ROTATION_270:
            x = src->pos.x;
            y = src->pos.y;
            w = src->size.w;
            h = src->size.h;
            dst->pos.x  = y;
            dst->pos.y  = -(x + w);
            dst->size.w = h;
            dst->size.h = w;
            break;

        default:
            dst[0] = src[0];
    }
}


int
skin_rect_contains( SkinRect*  r, int  x, int  y )
{
    return ( (unsigned)(x - r->pos.x) < (unsigned)r->size.w &&
             (unsigned)(y - r->pos.y) < (unsigned)r->size.h );
}

SkinOverlap
skin_rect_contains_rect( SkinRect  *r1, SkinRect  *r2 )
{
    SkinBox  a, b;

    skin_box_from_rect( &a, r1 );
    skin_box_from_rect( &b, r2 );

    if (a.x2 <= b.x1 || b.x2 <= a.x1 || a.y2 <= b.y1 || b.y2 <= a.y1) {
        return SKIN_OUTSIDE;
    }

    if (b.x1 >= a.x1 && b.x2 <= a.x2 && b.y1 >= a.y1 && b.y2 <= a.y2) {
        return SKIN_INSIDE;
    }

    return SKIN_OVERLAP;
}


int
skin_rect_intersect( SkinRect*  result, SkinRect*  r1, SkinRect*  r2 )
{
    SkinBox  a, b, r;

    skin_box_from_rect( &a, r1 );
    skin_box_from_rect( &b, r2 );

    if (a.x2 <= b.x1 || b.x2 <= a.x1 || a.y2 <= b.y1 || b.y2 <= a.y1) {
        result->pos.x = result->pos.y = result->size.w = result->size.h = 0;
        return 0;
    }

    r.x1 = (a.x1 > b.x1) ? a.x1 : b.x1;
    r.x2 = (a.x2 < b.x2) ? a.x2 : b.x2;
    r.y1 = (a.y1 > b.y1) ? a.y1 : b.y1;
    r.y2 = (a.y2 < b.y2) ? a.y2 : b.y2;

    skin_box_to_rect( &r, result );
    return 1;
}

int
skin_rect_equals( SkinRect*  r1, SkinRect*  r2 )
{
    return (r1->pos.x  == r2->pos.x  && r1->pos.y  == r2->pos.y &&
            r1->size.w == r2->size.w && r2->size.h == r2->size.h);
}

/** SKIN BOXES
 **/
void
skin_box_minmax_init( SkinBox*  box )
{
    box->x1 = box->y1 = INT_MAX;
    box->x2 = box->y2 = INT_MIN;
}

void
skin_box_minmax_update( SkinBox*  a, SkinRect*  r )
{
    SkinBox  b[1];

    skin_box_from_rect(b, r);

    if (b->x1 < a->x1) a->x1 = b->x1;
    if (b->y1 < a->y1) a->y1 = b->y1;
    if (b->x2 > a->x2) a->x2 = b->x2;
    if (b->y2 > a->y2) a->y2 = b->y2;
}

int
skin_box_minmax_to_rect( SkinBox*  box, SkinRect*  r )
{
    if (box->x1 > box->x2) {
        r->pos.x = r->pos.y = r->size.w = r->size.h = 0;
        return 0;
    }
    skin_box_to_rect( box, r );
    return 1;
}

void
skin_box_from_rect( SkinBox*  box, SkinRect*  r )
{
    box->x1 = r->pos.x;
    box->y1 = r->pos.y;
    box->x2 = r->size.w + box->x1;
    box->y2 = r->size.h + box->y1;
}

void
skin_box_to_rect( SkinBox*  box, SkinRect*  r )
{
    r->pos.x  = box->x1;
    r->pos.y  = box->y1;
    r->size.w = box->x2 - box->x1;
    r->size.h = box->y2 - box->y1;
}