forked from aykevl/tinygl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.go
185 lines (170 loc) · 4.97 KB
/
screen.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package tinygl
import (
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/pixel"
)
const showStats = false
// The Displayer that is drawn to.
type Displayer[T pixel.Color] interface {
Size() (int16, int16)
DrawBitmap(x, y int16, bitmap pixel.Image[T]) error
Display() error
Rotation() drivers.Rotation
}
// ScrollableDisplay is a display that supports hardware scrolling.
// A display doesn't have to support it, but if it does, scrolling can become a
// lot more smooth.
type ScrollableDisplay[T pixel.Color] interface {
Displayer[T]
SetScrollArea(topFixedArea, bottomFixedArea int16)
SetScroll(line int16)
StopScroll()
}
type Screen[T pixel.Color] struct {
display Displayer[T]
scrollableDisplay ScrollableDisplay[T]
child Object[T]
buffer pixel.Image[T]
statPixels int
statBuffers uint16
ppi int16
touchX int16
touchY int16
touchEvent Event
scrolling bool
}
// NewScreen creates a new screen to fill the whole display.
// The buffer needs to be big enough to fill at least horizontal row of pixels,
// but should preferably be bigger (10% of the screen for example).
// The ppi parameter is the number of pixels per inch, which is important
// for touch events.
func NewScreen[T pixel.Color](display Displayer[T], buffer pixel.Image[T], ppi int) *Screen[T] {
width, height := display.Size()
maxSize := width
if height > width {
maxSize = height
}
if buffer.Len() < int(maxSize) {
panic("buffer too small")
}
hwscroll, _ := display.(ScrollableDisplay[T])
return &Screen[T]{
display: display,
scrollableDisplay: hwscroll,
buffer: buffer,
ppi: int16(ppi),
}
}
// Size returns the size of the screen in pixels.
func (s *Screen[T]) Size() (width, height int) {
w, h := s.display.Size()
return int(w), int(h)
}
// SetChild sets the root child. This will typically be a container of some
// sort.
func (s *Screen[T]) SetChild(child Object[T]) {
if child == s.child {
return // nothing to do
}
if s.scrolling {
// TODO: use StopScroll only after updating the child.
// This makes it possible to update the screen with a monochrome
// animation that hides the StopScroll flash.
s.scrollableDisplay.StopScroll()
s.scrolling = false
}
s.child = child
child.RequestUpdate()
}
// Layout determines the size for all objects in the screen.
// This is called from Update() so it normally doesn't need to be called
// manually, but it can sometimes be helpful to know the size of objects before
// doing further initialization, for example when drawing on a canvas.
func (s *Screen[T]) Layout() {
width, height := s.display.Size()
s.child.Layout(int(width), int(height))
}
// Update sends all changes in the screen to the (hardware) display.
func (s *Screen[T]) Update() error {
var start time.Time
s.statPixels = 0
if showStats {
s.statBuffers = 0
start = time.Now()
}
s.Layout()
width, height := s.display.Size()
s.child.Update(s, 0, 0, int(width), int(height), 0, 0)
s.child.MarkUpdated()
if s.statPixels != 0 {
if showStats {
duration := time.Since(start)
println("sent", s.statPixels, "pixels using", s.statBuffers, "buffers in", duration.String())
}
err := s.display.Display()
if err != nil {
return err
}
}
return nil
}
// Buffer returns the pixel buffer used for sending data to the screen. It can
// be used inside an Update call.
func (s *Screen[T]) Buffer() pixel.Image[T] {
return s.buffer
}
// Send an image buffer to the given coordinates.
//
// This function is used internally, and should only be used to implement custom
// widgets.
func (s *Screen[T]) Send(x, y int, buffer pixel.Image[T]) {
var start time.Time
s.statPixels += buffer.Len()
if showStats {
s.statBuffers++
start = time.Now()
}
s.display.DrawBitmap(int16(x), int16(y), buffer)
if showStats {
duration := time.Since(start)
println("buffer send:", len(buffer.RawBuffer()), duration.String())
}
}
func (s *Screen[T]) setScroll(topFixed, bottomFixed, line int16) bool {
if !s.scrolling {
if s.scrollableDisplay == nil {
return false // not scrollable
}
switch s.display.Rotation() {
case drivers.Rotation0, drivers.Rotation180:
// good
default:
return false // scrolls in the wrong direction
}
s.scrolling = true
s.scrollableDisplay.SetScrollArea(topFixed, bottomFixed)
}
s.scrollableDisplay.SetScroll(line)
return true
}
// Internal function. Do not use directly except in custom widgets.
//
// It paints the given area on screen with the given color.
func PaintSolidColor[T pixel.Color](s *Screen[T], color T, x, y, width, height int) {
linesPerChunk := s.buffer.Len() / width
if linesPerChunk > height {
linesPerChunk = height
}
img := s.buffer.Rescale(width, linesPerChunk)
img.FillSolidColor(color)
lineStart := 0
for lineStart < height {
lines := linesPerChunk
if lineStart+lines > height {
lines = height - lineStart
}
s.Send(x, y+lineStart, img.LimitHeight(lines))
lineStart += linesPerChunk
}
}