/
resolution.go
167 lines (143 loc) · 3.26 KB
/
resolution.go
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
package launcher
import (
"encoding/json"
"math"
"strconv"
"strings"
)
type Resolution struct {
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
}
func (r *Resolution) MarshalJSON() ([]byte, error) {
if r == nil {
return json.Marshal(nil)
}
m := map[string]int{}
if r.Width > 0 {
m["width"] = r.Width
}
if r.Height > 0 {
m["height"] = r.Height
}
return json.Marshal(m)
}
func (r *Resolution) UnmarshalJSON(data []byte) error {
var m map[string]int
if err := json.Unmarshal(data, &m); err != nil {
return err
}
if h, ok := m["height"]; ok {
r.Height = h
} else {
r.Height = 0
}
if w, ok := m["width"]; ok {
r.Width = w
} else {
r.Width = 0
}
return nil
}
type AspectRatio struct {
Width int
Height int
}
// CalculateMissingSides calculates sizes for missing sides of resolution given the aspect ratio.
// If both sides are missing, then returns the initial value.
func (r *Resolution) CalculateMissingSides(ratio AspectRatio) (res Resolution) {
if r != nil {
if r.Width > 0 {
res.Height = ratio.Height * (r.Width / ratio.Width)
} else if r.Height > 0 {
res.Width = ratio.Width * (r.Height / ratio.Height)
}
}
return
}
func (r *Resolution) Max(c Resolution) (res Resolution) {
if r != nil {
res.Width = int(math.Max(float64(r.Width), float64(c.Width)))
res.Height = int(math.Max(float64(r.Height), float64(c.Height)))
}
return
}
func (r *Resolution) Min(c Resolution) (res Resolution) {
if r != nil {
res.Width = int(math.Min(float64(r.Width), float64(c.Width)))
res.Height = int(math.Min(float64(r.Height), float64(c.Height)))
}
return
}
// ParseResolution parses resolution in format WIDTHxHEIGHT or HEIGHTp, which returns LauncherProfileResolution with
// width and height fields set accordingly. Height is optional and can be omitted.
//
// For value "auto" this function will return nothing (neither resolution, nor error).
func ParseResolution(v string) (*Resolution, error) {
if v == "auto" {
return nil, nil
}
var w int
var h int
if strings.HasSuffix(v, "p") {
p := strings.TrimSuffix(v, "p")
if i, err := strconv.Atoi(p); err != nil {
return nil, &ResolutionParseError{
Input: v,
Err: &IllegalDimensionValueError{
Value: p,
Dimension: "height",
Err: err,
},
}
} else {
h = i
}
} else {
d := strings.Split(v, "x")
l := len(d)
switch {
case l > 2:
return nil, &ResolutionParseError{
Input: v,
Err: &IllegalDimensionsNumberError{l},
}
case l == 2:
if i, err := strconv.Atoi(d[1]); err != nil {
return nil, &ResolutionParseError{
Input: v,
Err: &IllegalDimensionValueError{
Value: d[1],
Dimension: "height",
Err: err,
},
}
} else {
h = i
}
fallthrough
case l == 1:
if i, err := strconv.Atoi(d[0]); err == nil {
w = i
} else {
return nil, &ResolutionParseError{
Input: v,
Err: &IllegalDimensionValueError{
Value: d[0],
Dimension: "width",
Err: err,
},
}
}
case l == 0:
return nil, &ResolutionParseError{
Input: v,
Err: &IllegalDimensionsNumberError{-1},
}
}
}
return &Resolution{
Width: w, // FIXME: maybe you should not use pointers for this one bruv
Height: h,
}, nil
}