This repository was archived by the owner on Nov 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw.go
177 lines (157 loc) · 4.76 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
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
package main
import (
"container/list"
"fmt"
"hash/crc64"
"strconv"
"strings"
"time"
"github.com/veandco/go-sdl2/sdl"
)
const font = "Helvetica-Bold.ttf" // Helvetica font is beautiful for long distance reading.
const width, height = 1920, 1080
var (
// A bunch of standard colors from the official guidelines (somewhere) for road signs
// to ensure AAA accessiblity. (i.e. red is not pure red so protanomaly colorblind see it)
blue = sdl.Color{0, 67, 123, 255}
green = sdl.Color{0, 95, 77, 255}
purple = sdl.Color{157, 0, 113, 255}
black = sdl.Color{0, 0, 0, 255}
brown = sdl.Color{98, 51, 30, 255}
red = sdl.Color{199, 0, 43, 255}
orange = sdl.Color{255, 104, 2, 255}
yellow = sdl.Color{255, 178, 0, 255}
white = sdl.Color{255, 255, 255, 255}
)
func (s *SignState) draw() {
s.Renderer.SetDrawColor(s.BackgroundFill.R, s.BackgroundFill.G, s.BackgroundFill.B, s.BackgroundFill.A)
s.Renderer.Clear()
s.blitDesignStudio() // Draw the words "Design Studio"
s.blitWhetherOpen(s.Open) // Handles whether the studio is open
s.blitWhenOpens()
s.blitMentorOnDuty() // Mentor name if there is one on duty
s.blitTime()
s.Renderer.Present()
}
var desiredFontSizes = [3]int{120, 250, 580}
const (
studioSize = 250
titleSize = 580
subtitleSize = 120
timeSize = 120
mentorOnDutyStrf = "Mentor%v on Duty: "
mentorPrefixStrf = "Mentor%v: "
whetherOpensOpenAt = "Should Open At: "
whetherOpensNotOpen = "Not Open Today"
)
func makePluralHandlingMentorString(subtitle string, onDutyText bool) string {
multi := ""
if strings.ContainsRune(subtitle, '&') {
multi = "s"
}
if onDutyText {
return fmt.Sprintf(mentorOnDutyStrf, multi)
}
return fmt.Sprintf(mentorPrefixStrf, multi)
}
func (s *SignState) blitWhenOpens() {
if !s.Open && s.SwitchValue == stateShifts {
var subHeaderToBlit string
if s.Subtitle == "" {
subHeaderToBlit = whetherOpensNotOpen
} else if s.Subtitle == "?" {
return
} else {
subHeaderToBlit = whetherOpensOpenAt
}
// White text
s.blitLeft(subtitleSize, subHeaderToBlit, white, width*1/64, int32(height-s.Fonts[subtitleSize].Height()*2))
s.blitLeft(subtitleSize, s.Subtitle, white, width*1/64, int32(height-s.Fonts[subtitleSize].Height()))
}
}
func (s *SignState) blitDesignStudio() {
// Set the drawing color to be white
str := "Design Studio"
s.blitCentered(studioSize, str, white, width/2, int32(s.Fonts[studioSize].Height()/2))
}
func (s *SignState) blitCentered(size int, text string, color sdl.Color, x, y int32) {
sw, sh, err := s.Fonts[size].SizeUTF8(text)
if err != nil {
fmt.Println(err)
} else {
s.blitLeft(size, text, color, x-int32(sw)/2, y-int32(sh)/2)
}
}
var cacheList = list.New()
type cachedTexture struct {
tex *sdl.Texture
checksum uint64
}
var crc64Table = crc64.MakeTable(crc64.ISO)
func (s *SignState) blitLeft(size int, text string, color sdl.Color, x, y int32) {
checksum := crc64.Checksum([]byte(text+strconv.Itoa(size)+strconv.Itoa(int(color.Uint32()))), crc64Table)
var tex *sdl.Texture
for e := cacheList.Front(); e != cacheList.Back(); e = e.Next() {
if e.Value.(cachedTexture).checksum == checksum {
tex = e.Value.(cachedTexture).tex
cacheList.MoveToFront(e)
break
}
}
if tex == nil {
var err error
var surf *sdl.Surface
surf, err = s.Fonts[size].RenderUTF8Blended(text, color)
if err != nil {
fmt.Println(err)
if surf != nil {
surf.Free()
surf = nil
}
} else {
tex, err = s.Renderer.CreateTextureFromSurface(surf)
surf.Free()
if err != nil {
if tex != nil {
tex.Destroy()
tex = nil
}
} else {
if cacheList.Len() > 6 {
cacheList.Back().Value.(cachedTexture).tex.Destroy()
cacheList.Remove(cacheList.Back())
}
cacheList.PushFront(cachedTexture{tex, checksum})
}
}
}
if tex != nil {
s.Renderer.SetDrawColor(white.R, white.G, white.B, white.A)
_, _, w, h, err := tex.Query()
if err == nil {
s.Renderer.Copy(tex, nil, &sdl.Rect{x, y, w, h})
}
}
}
func (s *SignState) blitWhetherOpen(open bool) {
// White "Closed" on red background.
s.BackgroundFill = red
// White "Open" on green background.
if open {
s.BackgroundFill = green
}
// Draw that, centered and big.
s.blitCentered(titleSize, s.Title, white, width/2, height*7/16)
}
func (s *SignState) blitMentorOnDuty() {
// Open + normal operation.
if s.Open && s.SwitchValue == stateShifts {
// White text
s.blitLeft(subtitleSize, makePluralHandlingMentorString(s.Subtitle, true), white, width*1/64, int32(height-s.Fonts[subtitleSize].Height()*2))
s.blitLeft(subtitleSize, s.Subtitle, white, width*1/64, int32(height-s.Fonts[subtitleSize].Height()))
}
}
func (s *SignState) blitTime() {
now := time.Now()
s.blitCentered(timeSize, now.Format(time.Kitchen), white, width*13/16, height*15/16)
}