-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.go
114 lines (98 loc) · 2.95 KB
/
transform.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
package engosdl
// ITransform represents the interface for any entity transformation.
type ITransform interface {
GetDim() *Vector
GetPosition() *Vector
GetRect() *Rect
GetRectExt() (float64, float64, float64, float64)
GetRotation() float64
GetScale() *Vector
SetDim(*Vector) ITransform
SetDimXY(float64, float64) ITransform
SetPosition(*Vector) ITransform
SetPositionXY(float64, float64) ITransform
SetRotation(float64) ITransform
SetScale(*Vector) ITransform
SetScaleXY(float64, float64) ITransform
}
// Transform is the default implementation for ITransform interface.
type Transform struct {
Position *Vector `json:"position"`
Rotation float64 `json:"rotation"`
Scale *Vector `json:"scale"`
Dim *Vector `json:"dimension"`
}
// NewTransform creates a new transform instance.
func NewTransform() *Transform {
return &Transform{
Position: NewVector(0, 0),
Rotation: 0,
Scale: NewVector(1, 1),
Dim: NewVector(0, 0),
}
}
//GetDim returns the transform original dimensions.
func (t *Transform) GetDim() *Vector {
return t.Dim
}
// GetPosition returns the transform position.
func (t *Transform) GetPosition() *Vector {
return t.Position
}
// GetRect returns a rectangle with real position and dimensions.
// Real dimensions are affected by the scale value.
func (t *Transform) GetRect() *Rect {
return &Rect{
X: t.GetPosition().X,
Y: t.GetPosition().Y,
W: t.GetDim().X * t.GetScale().X,
H: t.GetDim().Y * t.GetScale().Y,
}
}
// GetRectExt returns rectangle coordinates as x, y, w, and h.
func (t *Transform) GetRectExt() (float64, float64, float64, float64) {
return t.GetPosition().X, t.GetPosition().Y, t.GetDim().X * t.GetScale().X, t.GetDim().Y * t.GetScale().Y
}
// GetRotation returns the transform rotation.
func (t *Transform) GetRotation() float64 {
return t.Rotation
}
// GetScale returns the transform scale.
func (t *Transform) GetScale() *Vector {
return t.Scale
}
// SetDim sets the transform original dimensions.
func (t *Transform) SetDim(v *Vector) ITransform {
t.Dim = v
return t
}
// SetDimXY sets the transform original dimensions providing values for
// X-axis and Y-axis.
func (t *Transform) SetDimXY(x float64, y float64) ITransform {
return t.SetDim(NewVector(x, y))
}
// SetPosition sets the transform position.
func (t *Transform) SetPosition(v *Vector) ITransform {
t.Position = v
return t
}
// SetPositionXY sets the transform position providing values for X-axis
// and Y-axis.
func (t *Transform) SetPositionXY(x float64, y float64) ITransform {
return t.SetPosition(NewVector(x, y))
}
// SetRotation sets the transform rotation.
func (t *Transform) SetRotation(r float64) ITransform {
t.Rotation = r
return t
}
// SetScale sets the transform scale.
func (t *Transform) SetScale(v *Vector) ITransform {
t.Scale = v
return t
}
// SetScaleXY sets the transform scale providing values for X-axis and
// Y-scale.
func (t *Transform) SetScaleXY(x float64, y float64) ITransform {
return t.SetScale(NewVector(x, y))
}