-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositioner.go
165 lines (152 loc) · 5.17 KB
/
positioner.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
package counters
import (
"github.com/pkg/errors"
"github.com/thehivecorporation/log"
)
const (
PositionCenter = iota
PositionTopLeft
PositionTopCenterLeft
PositionTopCenter
PositionTopCenterRight
PositionTopRight
PositionRightTop
PositionRight
PositionRightBottom
PositionBottomRight
PositionBottomCenterRight
PositionBottomCenter
PositionBottomCenterLeft
PositionBottomLeft
PositionLeftBottom
PositionLeft
PositionLeftTop
)
type Positioner struct{}
func (p *Positioner) GetAnchorPointsAndMaxWidth(pos int, def *Settings) (float64, float64, float64, error) {
counterWidth := float64(def.Width)
offsetX := *def.Margins +
*def.BorderWidth +
*def.XShift +
*def.StrokeWidth
availableWidth := counterWidth - (offsetX * 2)
switch pos {
case 0:
// Center of the counter, space usually for image
return 0.5, 0.5, availableWidth, nil
case 1:
// Top left corner (10:30)
return 0, 0, (availableWidth) / 5, nil
case 2, 3, 4:
// Top left, at the right of the top-left corner
// Top, horizontally centered
// Top right, at the left of the top-right corner
return 0.5, 0, (availableWidth) / 5, nil
case 5:
// Top right corner
return 1, 0, (availableWidth) / 5, nil
case 6, 7, 8:
// Right slightly up, under the top right corner, over the vertical center
// Right, vertically centered
// Right slightly down, over the bottom right corner, under the vertical center
return 1, 0.5, (availableWidth) / 5, nil
case 9:
// Bottom right corner
return 1, 1, (availableWidth) / 5, nil
case 10, 11, 12:
// Bottom right, at the left of the bottom-right corner
// Bottom, horizontally centered
// Bottom left, at the right of the bottom-left corner
return 0.5, 1, (availableWidth) / 5, nil
case 13:
// Bottom left corner
return 0, 1, (availableWidth) / 5, nil
case 14, 15, 16:
// Left down, over the bottom-left corner
// Left, vertically centered
// Left up, under the upper-left corner
return 0, 0.5, (availableWidth) / 5, nil
default:
log.WithField("position", pos).Error("Position unknown. Valid positions are from 0 to 16 (both included)")
return 0, 0, 0, errors.New("the position provided is not in the range 0-16")
}
}
func (p *Positioner) GetXYPosition(pos int, def *Settings) (float64, float64, error) {
counterWidth := float64(def.Width)
counterHeight := float64(def.Height)
offsetX := *def.Margins + *def.BorderWidth + *def.StrokeWidth
offsetY := *def.Margins + *def.BorderWidth + *def.StrokeWidth
centerVertical := counterHeight / 2
centerHorizontal := counterWidth / 2
topLeft := (counterWidth * 0.25) + (offsetX / 2)
topRight := (counterWidth * 0.75) - (offsetX / 2)
leftAbove := (counterHeight * 0.25) + (offsetY / 2)
leftBelow := (counterHeight * 0.75) - (offsetY / 2)
switch pos {
case 0:
// Center of the counter, space usually for image
return centerHorizontal + *def.XShift, centerVertical + *def.YShift, nil
case 1:
// Top left corner
return offsetX + *def.XShift, offsetY + *def.YShift, nil
case 2:
// Top left, at the right of the top-left corner
return topLeft + *def.XShift, offsetY + *def.YShift, nil
case 3:
// Top center
return centerHorizontal + *def.XShift, offsetY + *def.YShift, nil
case 4:
// Top right, at the left of the top-right corner
return topRight + *def.XShift, offsetY + *def.YShift, nil
case 5:
// Top right corner
return counterWidth - offsetX + *def.XShift, offsetY + *def.YShift, nil
case 6:
// Right slightly up, under the top right corner
return counterWidth - offsetX + *def.XShift, leftAbove + *def.YShift, nil
case 7:
// Right
return counterWidth - offsetX + *def.XShift, centerVertical + *def.YShift, nil
case 8:
// Right slightly down, over the bottom right corner
return counterWidth - offsetX + *def.XShift, leftBelow + *def.YShift, nil
case 9:
// Bottom right corner
return counterWidth - offsetX + *def.XShift, counterHeight - offsetY + *def.YShift, nil
case 10:
// Bottom right, at the left of the bottom-right corner
return topRight + *def.XShift, counterHeight - offsetY + *def.YShift, nil
case 11:
// Bottom
return centerHorizontal + *def.XShift, counterHeight - offsetY + *def.YShift, nil
case 12:
//Bottom left, at the right of the bottom-left corner
return topLeft + *def.XShift, counterHeight - offsetY + *def.YShift, nil
case 13:
//Bottom left corner
return offsetX + *def.XShift, counterHeight - offsetY + *def.YShift, nil
case 14:
// Left down, over the bottom-left corner
return offsetX + *def.XShift, leftBelow + *def.YShift, nil
case 15:
// Left
return offsetX + *def.XShift, centerVertical + *def.YShift, nil
case 16:
// Left up, under the upper-left corner
return offsetX + *def.XShift, leftAbove + *def.YShift, nil
default:
log.WithField("position", pos).Error("Position unknown. Valid positions are from 0 to 16 (both included)")
return 0, 0, errors.New("the position provided is not in the range 0-16")
}
}
func (p *Positioner) getObjectPositions(pos int, def *Settings) (float64, float64, float64, float64, error) {
ax, ay, _, err := p.GetAnchorPointsAndMaxWidth(pos, def)
if err != nil {
return 0, 0, 0, 0, err
}
x, y, err := p.GetXYPosition(pos, def)
if err != nil {
return 0, 0, 0, 0, err
}
return x, y, ax, ay, nil
}