forked from Thinkofname/steven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
169 lines (159 loc) · 3.73 KB
/
image.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
168
169
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import "github.com/thinkofdeath/steven/render"
// Image is a drawable that draws a texture.
type Image struct {
baseElement
texture render.TextureInfo
x, y, w, h float64
tx, ty, tw, th float64
r, g, b, a int
}
// NewImage creates a new image drawable.
func NewImage(texture render.TextureInfo, x, y, w, h, tx, ty, tw, th float64, r, g, b int) *Image {
return &Image{
texture: texture,
r: r, g: g, b: b, a: 255,
x: x, y: y, w: w, h: h,
tx: tx, ty: ty, tw: tw, th: th,
baseElement: baseElement{
visible: true,
isNew: true,
},
}
}
// Attach changes the location where this is attached to.
func (i *Image) Attach(vAttach, hAttach AttachPoint) *Image {
i.vAttach, i.hAttach = vAttach, hAttach
return i
}
func (i *Image) Texture() render.TextureInfo { return i.texture }
func (i *Image) SetTexture(t render.TextureInfo) {
if i.texture != t {
i.texture = t
i.dirty = true
}
}
func (i *Image) X() float64 { return i.x }
func (i *Image) SetX(x float64) {
if i.x != x {
i.x = x
i.dirty = true
}
}
func (i *Image) Y() float64 { return i.y }
func (i *Image) SetY(y float64) {
if i.y != y {
i.y = y
i.dirty = true
}
}
func (i *Image) Width() float64 { return i.w }
func (i *Image) SetWidth(w float64) {
if i.w != w {
i.w = w
i.dirty = true
}
}
func (i *Image) Height() float64 { return i.h }
func (i *Image) SetHeight(h float64) {
if i.h != h {
i.h = h
i.dirty = true
}
}
func (i *Image) TextureX() float64 { return i.tx }
func (i *Image) SetTextureX(x float64) {
if i.tx != x {
i.tx = x
i.dirty = true
}
}
func (i *Image) TextureY() float64 { return i.ty }
func (i *Image) SetTextureY(y float64) {
if i.ty != y {
i.ty = y
i.dirty = true
}
}
func (i *Image) TextureWidth() float64 { return i.tw }
func (i *Image) SetTextureWidth(w float64) {
if i.tw != w {
i.tw = w
i.dirty = true
}
}
func (i *Image) TextureHeight() float64 { return i.th }
func (i *Image) SetTextureHeight(h float64) {
if i.th != h {
i.th = h
i.dirty = true
}
}
func (i *Image) R() int { return i.r }
func (i *Image) SetR(r int) {
if i.r != r {
i.r = r
i.dirty = true
}
}
func (i *Image) G() int { return i.g }
func (i *Image) SetG(g int) {
if i.g != g {
i.g = g
i.dirty = true
}
}
func (i *Image) B() int { return i.b }
func (i *Image) SetB(b int) {
if i.b != b {
i.b = b
i.dirty = true
}
}
func (i *Image) A() int { return i.a }
func (i *Image) SetA(a int) {
if i.a != a {
i.a = a
i.dirty = true
}
}
// Draw draws this to the target region.
func (i *Image) Draw(r Region, delta float64) {
if i.isNew || i.isDirty() || forceDirty {
i.isNew = false
e := render.NewUIElement(i.texture, r.X, r.Y, r.W, r.H, i.tx, i.ty, i.tw, i.th)
e.R = byte(i.r)
e.G = byte(i.g)
e.B = byte(i.b)
e.A = byte(i.a)
e.Layer = i.Layer()
i.data = e.Bytes()
}
render.UIAddBytes(i.data)
}
// Offset returns the offset of this drawable from the attachment
// point.
func (i *Image) Offset() (float64, float64) {
return i.x, i.y
}
// Size returns the size of this drawable.
func (i *Image) Size() (float64, float64) {
return i.w, i.h
}
// Remove removes the image element from the draw list.
func (i *Image) Remove() {
Remove(i)
}