forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoom.cpp
136 lines (120 loc) · 2.31 KB
/
zoom.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
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
// Aseprite Render Library
// Copyright (c) 2020-2022 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/debug.h"
#include "render/zoom.h"
#include <algorithm>
namespace render {
static int scales[][2] = {
{ 1, 64 },
{ 1, 48 },
{ 1, 32 },
{ 1, 24 },
{ 1, 16 },
{ 1, 12 },
{ 1, 8 },
{ 1, 6 },
{ 1, 5 },
{ 1, 4 },
{ 1, 3 },
{ 1, 2 },
{ 1, 1 }, // 100%
{ 2, 1 },
{ 3, 1 },
{ 4, 1 },
{ 5, 1 },
{ 6, 1 },
{ 8, 1 },
{ 12, 1 },
{ 16, 1 },
{ 24, 1 },
{ 32, 1 },
{ 48, 1 },
{ 64, 1 },
};
static int scales_size = sizeof(scales) / sizeof(scales[0]);
Zoom::Zoom(int num, int den)
: m_num(num)
, m_den(den)
{
ASSERT(m_num > 0);
ASSERT(m_den > 0);
m_internalScale = scale();
}
bool Zoom::in()
{
int i = linearScale();
if (i < scales_size-1) {
++i;
m_num = scales[i][0];
m_den = scales[i][1];
m_internalScale = scale();
return true;
}
else
return false;
}
bool Zoom::out()
{
int i = linearScale();
if (i > 0) {
--i;
m_num = scales[i][0];
m_den = scales[i][1];
m_internalScale = scale();
return true;
}
else
return false;
}
int Zoom::linearScale() const
{
for (int i=0; i<scales_size; ++i) {
// Exact match
if (scales[i][0] == m_num &&
scales[i][1] == m_den) {
return i;
}
}
return findClosestLinearScale(scale());
}
// static
Zoom Zoom::fromScale(double scale)
{
Zoom zoom = fromLinearScale(findClosestLinearScale(scale));
zoom.m_internalScale = scale;
return zoom;
}
// static
Zoom Zoom::fromLinearScale(int i)
{
i = std::clamp(i, 0, scales_size-1);
return Zoom(scales[i][0], scales[i][1]);
}
// static
int Zoom::findClosestLinearScale(double scale)
{
for (int i=1; i<scales_size-1; ++i) {
double min = double(scales[i-1][0]) / double(scales[i-1][1]);
double mid = double(scales[i ][0]) / double(scales[i ][1]);
double max = double(scales[i+1][0]) / double(scales[i+1][1]);
if (scale >= (min+mid)/2.0 &&
scale <= (mid+max)/2.0)
return i;
}
if (scale < 1.0)
return 0;
else
return scales_size-1;
}
int Zoom::linearValues()
{
return scales_size;
}
} // namespace render