forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_region.cpp
101 lines (84 loc) · 2.32 KB
/
move_region.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Aseprite UI Library
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 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 "ui/manager.h"
#include "os/surface.h"
#include "os/system.h"
#include "os/window.h"
#include "ui/overlay_manager.h"
#include <vector>
namespace ui {
using namespace gfx;
void move_region(Display* display, const Region& region, int dx, int dy)
{
ASSERT(display);
if (!display)
return;
os::Window* window = display->nativeWindow();
ASSERT(window);
if (!window)
return;
auto overlays = ui::OverlayManager::instance();
gfx::Rect bounds = region.bounds();
bounds |= gfx::Rect(bounds).offset(dx, dy);
overlays->restoreOverlappedAreas(bounds);
os::Surface* surface = window->surface();
os::SurfaceLock lock(surface);
// Fast path, move one rectangle.
if (region.isRect()) {
gfx::Rect rc = region.bounds();
surface->scrollTo(rc, dx, dy);
rc.offset(dx, dy);
display->dirtyRect(rc);
}
// As rectangles in the region internals are separated by bands
// through the y-axis, we can sort the rectangles by y-axis and then
// by x-axis to move rectangle by rectangle depending on the dx/dy
// direction so we don't overlap each rectangle.
else if (region.isComplex()) {
std::size_t nrects = region.size();
std::vector<gfx::Rect> rcs(nrects);
std::copy(region.begin(), region.end(), rcs.begin());
std::sort(
rcs.begin(), rcs.end(),
[dx, dy](const gfx::Rect& a, const gfx::Rect& b){
if (dy < 0) {
if (a.y < b.y)
return true;
else if (a.y == b.y) {
if (dx < 0)
return a.x < b.x;
else
return a.x > b.x;
}
else
return false;
}
else {
if (a.y > b.y)
return true;
else if (a.y == b.y) {
if (dx < 0)
return a.x < b.x;
else
return a.x > b.x;
}
else
return false;
}
});
for (gfx::Rect& rc : rcs) {
surface->scrollTo(rc, dx, dy);
rc.offset(dx, dy);
display->dirtyRect(rc);
}
}
overlays->drawOverlays();
}
} // namespace ui