forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_bounds.cpp
86 lines (77 loc) · 2.23 KB
/
fit_bounds.cpp
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
// Aseprite UI Library
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "base/clamp.h"
#include "gfx/rect.h"
#include "ui/base.h"
#include "ui/system.h"
namespace ui {
int fit_bounds(int arrowAlign, const gfx::Rect& target, gfx::Rect& bounds)
{
bounds.x = target.x;
bounds.y = target.y;
int trycount = 0;
for (; trycount < 4; ++trycount) {
switch (arrowAlign) {
case TOP | LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y + target.h;
break;
case TOP | RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y + target.h;
break;
case BOTTOM | LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y - bounds.h;
break;
case BOTTOM | RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y - bounds.h;
break;
case TOP:
bounds.x = target.x + target.w/2 - bounds.w/2;
bounds.y = target.y + target.h;
break;
case BOTTOM:
bounds.x = target.x + target.w/2 - bounds.w/2;
bounds.y = target.y - bounds.h;
break;
case LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y + target.h/2 - bounds.h/2;
break;
case RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y + target.h/2 - bounds.h/2;
break;
}
bounds.x = base::clamp(bounds.x, 0, ui::display_w()-bounds.w);
bounds.y = base::clamp(bounds.y, 0, ui::display_h()-bounds.h);
if (target.intersects(bounds)) {
switch (trycount) {
case 0:
case 2:
// Switch position
if (arrowAlign & (TOP | BOTTOM)) arrowAlign ^= TOP | BOTTOM;
if (arrowAlign & (LEFT | RIGHT)) arrowAlign ^= LEFT | RIGHT;
break;
case 1:
// Rotate positions
if (arrowAlign & (TOP | LEFT)) arrowAlign ^= TOP | LEFT;
if (arrowAlign & (BOTTOM | RIGHT)) arrowAlign ^= BOTTOM | RIGHT;
break;
}
}
else
break;
}
return arrowAlign;
}
} // namespace ui