Some lighter refactoring

This commit is contained in:
Sasha Koshka 2022-11-28 17:33:31 -05:00
parent f2cc5e8099
commit 924430617e
3 changed files with 41 additions and 48 deletions

15
draw.go
View File

@ -6,13 +6,14 @@ import "git.tebibyte.media/sashakoshka/stone"
func redraw () {
width, height := application.Size()
columns = (width - 20) / 23 + 1
rows = height / 10 + 1
state.columns = (width - 20) / 23 + 1
state.rows = height / 10 + 1
state.pageSize = state.columns * state.rows
monthIter := viewingMonth
xOffset := (width - columns * 23) / 2 + 1
for y := 0; y < rows; y ++ {
for x := 0; x < columns; x ++ {
monthIter := state.viewingMonth
xOffset := (width - state.columns * 23) / 2 + 1
for y := 0; y < state.rows; y ++ {
for x := 0; x < state.columns; x ++ {
drawMonth (
x * 23 + xOffset, y * 10,
monthIter / 12,
@ -23,7 +24,7 @@ func redraw () {
}
func drawMonth (xOffset, yOffset, year int, month time.Month) {
current := int(month) - 1 + year * 12 == currentMonth
current := int(month) - 1 + year * 12 == state.currentMonth
bce := year < 0
if bce { year *= -1 }

View File

@ -1,52 +1,46 @@
package main
import "sync"
import "time"
import "git.tebibyte.media/sashakoshka/stone"
var lock sync.Mutex
func onScroll (x, y int) {
drawMutex.Lock()
defer drawMutex.Unlock()
lock.Lock()
defer lock.Unlock()
viewingMonth += y * columns
state.viewingMonth += y * state.columns
application.Clear()
redraw()
application.Draw()
}
func onPress (button stone.Button, modifiers stone.Modifiers) {
lock.Lock()
defer lock.Unlock()
switch button {
case stone.KeyUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns
state.viewingMonth -= state.columns
application.Clear()
redraw()
application.Draw()
case stone.KeyDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns
state.viewingMonth += state.columns
application.Clear()
redraw()
application.Draw()
case stone.KeyPageUp:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth -= columns * rows
state.viewingMonth -= state.pageSize
application.Clear()
redraw()
application.Draw()
case stone.KeyPageDown:
drawMutex.Lock()
defer drawMutex.Unlock()
viewingMonth += columns * rows
state.viewingMonth += state.pageSize
application.Clear()
redraw()
application.Draw()
@ -55,11 +49,11 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
}
func onStart () {
drawMutex.Lock()
defer drawMutex.Unlock()
lock.Lock()
defer lock.Unlock()
currentMonth = canonMonth(time.Now())
viewingMonth = currentMonth
state.currentMonth = canonMonth(time.Now())
state.viewingMonth = state.currentMonth
redraw()
go func () {
for {
@ -70,30 +64,29 @@ func onStart () {
}
func onResize () {
drawMutex.Lock()
defer drawMutex.Unlock()
lock.Lock()
defer lock.Unlock()
redraw()
}
func onTick () {
drawMutex.Lock()
defer drawMutex.Unlock()
lock.Lock()
defer lock.Unlock()
pageSize := columns * rows
within :=
currentMonth >= viewingMonth &&
currentMonth < viewingMonth + pageSize
state.currentMonth >= state.viewingMonth &&
state.currentMonth < state.viewingMonth + state.pageSize
newMonth := canonMonth(time.Now())
if currentMonth != newMonth {
currentMonth = newMonth
if state.currentMonth != newMonth {
state.currentMonth = newMonth
if within {
if currentMonth < viewingMonth {
viewingMonth -= pageSize
} else if currentMonth >= viewingMonth + pageSize {
viewingMonth += pageSize
if state.currentMonth < state.viewingMonth {
state.viewingMonth -= state.pageSize
} else if state.currentMonth >= state.pageSize {
state.viewingMonth += state.pageSize
}
}

View File

@ -1,6 +1,5 @@
package main
import "sync"
import "bytes"
import "image"
import _ "embed"
@ -13,10 +12,10 @@ var iconBytes []byte
var application = &stone.Application { }
var viewingMonth int
var currentMonth int
var columns, rows int
var drawMutex sync.Mutex
var state struct {
viewingMonth, currentMonth int
columns, rows, pageSize int
}
var shortMonthNames = []string {
"",