This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraw.go
117 lines (107 loc) · 2.89 KB
/
Draw.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
package hebiten
import (
"hgo"
"runtime/debug"
"github.com/hajimehoshi/ebiten"
)
type TDraw struct {
Target *ebiten.Image
Image *ebiten.Image
AtlasTexture AtlasTexture
Position BigFloat2
Angle float64
Origin BigFloat2
Size BigFloat2
Scale BigFloat2
CenterOrigin bool
Mask TFloatColor
}
var GlobalDrawNumber int
var DebugImage *ebiten.Image
var DebugImageModeEnabled bool
func (this *TDraw) GetImageSize() BigFloat2 {
var w, h = this.Image.Size()
return BigFloat2{float64(w), float64(h)}
}
func (this *TDraw) InferScale(imageSize BigFloat2) {
this.Scale.X = this.Size.X / imageSize.X
this.Scale.Y = this.Size.Y / imageSize.Y
}
func (this *TDraw) Draw() {
var o = &ebiten.DrawImageOptions{}
var imageSize BigFloat2
if this.AtlasTexture.Exists() {
this.Image = this.AtlasTexture.Image
imageSize = this.AtlasTexture.Rect.GetSize().ToBigFloat()
var imageRect = this.AtlasTexture.Rect.ToImageRect()
o.SourceRect = &imageRect
if false {
println(GlobalIntRect.ImageRectToStr(*o.SourceRect))
var w, h = this.Image.Size()
o.SourceRect.Min.X = 0
o.SourceRect.Min.Y = 0
o.SourceRect.Max.X = w
o.SourceRect.Max.Y = h
}
} else {
hgo.AssertWT(this.Image != nil, func() string { return "this.Image field is nil" })
imageSize = this.GetImageSize()
if false {
println(string(debug.Stack()))
}
}
if this.Scale.Check0() {
this.InferScale(imageSize)
}
var halfW = imageSize.X / 2
var halfH = imageSize.Y / 2
if this.CenterOrigin {
this.Origin.X = halfW
this.Origin.Y = halfH
}
o.GeoM.Translate(-halfW, -halfH)
o.GeoM.Rotate(this.Angle)
o.GeoM.Translate(halfW, halfH)
o.GeoM.Translate(-this.Origin.X, -this.Origin.Y)
if false == this.Size.Check0() {
o.GeoM.Scale(this.Scale.X, this.Scale.Y)
}
o.GeoM.Translate(this.Position.X, this.Position.Y)
if false == this.Mask.CheckIfZero() {
o.ColorM.Scale(this.Mask.R, this.Mask.G, this.Mask.B, this.Mask.A)
}
if this.CheckVisibility() {
if DebugImageModeEnabled {
this.Target.DrawImage(DebugImage, o)
} else {
this.Target.DrawImage(this.Image, o)
}
GlobalDrawNumber++
}
}
func (this *TDraw) GetRect() TFloatRect {
return TFloatRect{
this.Position.X - this.Origin.X*this.Scale.X,
this.Position.Y - this.Origin.Y*this.Scale.X,
this.Size.X,
this.Size.Y}
}
func (this *TDraw) CheckVisibility() (result bool) {
var imageRect = this.GetRect()
var destinationRect TFloatRect
destinationRect.LoadImage(this.Target)
destinationRect = destinationRect.GetEnlarged(1)
for _, point := range imageRect.GetPoints() {
if destinationRect.CheckContainsPoint(point) {
result = true
break
}
}
return
}
func (this *TDraw) SetPosRect(rect TFloatRect) {
this.Position.X = rect.X
this.Position.Y = rect.Y
this.Size.X = rect.W
this.Size.Y = rect.H
}