Compare commits

...

2 Commits

Author SHA1 Message Date
Sasha Koshka 3c2cf76df2 Fixed bug relating to alt key 2022-11-21 18:39:33 -05:00
Sasha Koshka 13d8486370 Separated draw routines into a different file 2022-11-21 18:34:13 -05:00
2 changed files with 211 additions and 206 deletions

208
draw.go Normal file
View File

@ -0,0 +1,208 @@
package main
import "fmt"
import "git.tebibyte.media/sashakoshka/stone"
func clear (x, y, width, height int) {
for yy := 0; yy < height; yy ++ {
for xx := 0; xx < width; xx ++ {
application.SetRune(xx + x, yy + y, 0)
application.SetColor(xx + x, yy + y, stone.ColorForeground)
}}
}
func redraw () {
width, _ := application.Size()
showLeftColumn = width > 44
if showLeftColumn {
drawNumberReadouts()
}
drawInput()
}
func drawInput () {
width, height := application.Size()
xOffset := 0
if showLeftColumn {
xOffset = 26
}
y := height - 1
inputWidth, _ := inputBuffer.Size()
newWidth := width - xOffset
if newWidth != inputWidth {
inputBuffer.SetSize(newWidth, 1)
}
inputBuffer.Clear()
if expressionRoot != nil {
expressionRoot.Render(&inputBuffer, 0)
}
for x := 0; x < newWidth; x ++ {
character := '_'
cell := inputBuffer.Cell(x, 0)
if cell.Rune() > 0 {
character = cell.Rune()
}
application.SetRune(x + xOffset, y, character)
if character == '_' {
application.SetColor(x + xOffset, y, stone.ColorDim)
} else {
application.SetColor(x + xOffset, y, cell.Color())
}
}
}
func drawNumberReadouts () {
_, height := application.Size()
clear(0, height - 10, 25, 10)
var number int64
var err error
if selectedExpression != nil {
number, err = selectedExpression.Solution()
}
if err != nil {
application.SetDot((25 - len(err.Error())) / 2, height - 6)
fmt.Fprint(application, err.Error())
} else {
drawHexadecimal (0, height - 10, uint64(number))
drawDecimal (0, height - 8, uint64(number))
drawOctal (0, height - 6, uint64(number))
drawBinary (0, height - 4, uint64(number))
}
}
func drawBinary (xOffset, yOffset int, number uint64) {
bitOffset := 64
for y := 0; y < 4; y ++ {
application.SetDot(xOffset, y + yOffset)
fmt.Fprint(application, bitOffset)
application.SetColor(xOffset, y + yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, y + yOffset, stone.ColorBlue)
for x := 0; x < 16; x ++ {
bitOffset --
trueX := x + xOffset + x / 4 + 3
character := '0' + rune((number >> bitOffset) & 0x1)
application.SetRune(trueX, y + yOffset, character)
if character == '0' {
application.SetColor (
trueX,
y + yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
y + yOffset,
stone.ColorForeground)
}
}
application.SetDot(xOffset + 23, y + yOffset)
fmt.Fprint(application, bitOffset + 1)
application.SetColor(xOffset + 23, y + yOffset, stone.ColorBlue)
application.SetColor(xOffset + 24, y + yOffset, stone.ColorBlue)
}
}
func drawHexadecimal (xOffset, yOffset int, number uint64) {
bitOffset := 64
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "16")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 16; x ++ {
bitOffset -= 4
trueX := x + xOffset + x / 4 + 6
character := rune((number >> bitOffset) & 0xF)
if character < 10 {
character += '0'
} else {
character += 'A' - 10
}
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
}
}
func drawDecimal (xOffset, yOffset int, number uint64) {
var divisor uint64 = 10000000000000000000
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "10")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 20; x ++ {
trueX := x + xOffset + 5
character := rune((number / divisor) % 10) + '0'
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
divisor /= 10
}
}
func drawOctal (xOffset, yOffset int, number uint64) {
// fuck octal i hate it almost as much as decimal
bitOffset := 63
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "08")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 21; x ++ {
bitOffset -= 3
trueX := x + xOffset + 4
character := '0' + rune((number >> bitOffset) % 8)
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
}
}

209
main.go
View File

@ -1,6 +1,5 @@
package main
import "fmt"
import "bytes"
import "image"
import _ "embed"
@ -32,6 +31,7 @@ func main () {
application.OnStart(onStart)
application.OnResize(redraw)
application.OnPress(onPress)
application.OnRelease(onRelease)
err = application.Run()
if err != nil { panic(err) }
@ -159,7 +159,7 @@ func onPress (button stone.Button) {
}
switch selectedExpression.(type) {
case *Operation:
case *Operation, nil:
insertGeneric (&IntegerLiteral {
displayRadix: 10,
value: int64(value),
@ -218,6 +218,7 @@ func onRelease (button stone.Button) {
switch button {
case stone.KeyLeftAlt, stone.KeyRightAlt:
inputState.alt = false
println("alt up")
}
}
@ -296,207 +297,3 @@ func insertGeneric (expression Expression) {
func onStart () {
redraw()
}
func redraw () {
width, _ := application.Size()
showLeftColumn = width > 44
if showLeftColumn {
drawNumberReadouts()
}
drawInput()
}
func drawInput () {
width, height := application.Size()
xOffset := 0
if showLeftColumn {
xOffset = 26
}
y := height - 1
inputWidth, _ := inputBuffer.Size()
newWidth := width - xOffset
if newWidth != inputWidth {
inputBuffer.SetSize(newWidth, 1)
}
inputBuffer.Clear()
if expressionRoot != nil {
expressionRoot.Render(&inputBuffer, 0)
}
for x := 0; x < newWidth; x ++ {
character := '_'
cell := inputBuffer.Cell(x, 0)
if cell.Rune() > 0 {
character = cell.Rune()
}
application.SetRune(x + xOffset, y, character)
if character == '_' {
application.SetColor(x + xOffset, y, stone.ColorDim)
} else {
application.SetColor(x + xOffset, y, cell.Color())
}
}
}
func drawNumberReadouts () {
_, height := application.Size()
clear(0, height - 10, 25, 10)
var number int64
var err error
if selectedExpression != nil {
number, err = selectedExpression.Solution()
}
if err != nil {
application.SetDot((25 - len(err.Error())) / 2, height - 6)
fmt.Fprint(application, err.Error())
} else {
drawHexadecimal (0, height - 10, uint64(number))
drawDecimal (0, height - 8, uint64(number))
drawOctal (0, height - 6, uint64(number))
drawBinary (0, height - 4, uint64(number))
}
}
func clear (x, y, width, height int) {
for yy := 0; yy < height; yy ++ {
for xx := 0; xx < width; xx ++ {
application.SetRune(xx + x, yy + y, 0)
application.SetColor(xx + x, yy + y, stone.ColorForeground)
}}
}
func drawBinary (xOffset, yOffset int, number uint64) {
bitOffset := 64
for y := 0; y < 4; y ++ {
application.SetDot(xOffset, y + yOffset)
fmt.Fprint(application, bitOffset)
application.SetColor(xOffset, y + yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, y + yOffset, stone.ColorBlue)
for x := 0; x < 16; x ++ {
bitOffset --
trueX := x + xOffset + x / 4 + 3
character := '0' + rune((number >> bitOffset) & 0x1)
application.SetRune(trueX, y + yOffset, character)
if character == '0' {
application.SetColor (
trueX,
y + yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
y + yOffset,
stone.ColorForeground)
}
}
application.SetDot(xOffset + 23, y + yOffset)
fmt.Fprint(application, bitOffset + 1)
application.SetColor(xOffset + 23, y + yOffset, stone.ColorBlue)
application.SetColor(xOffset + 24, y + yOffset, stone.ColorBlue)
}
}
func drawHexadecimal (xOffset, yOffset int, number uint64) {
bitOffset := 64
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "16")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 16; x ++ {
bitOffset -= 4
trueX := x + xOffset + x / 4 + 6
character := rune((number >> bitOffset) & 0xF)
if character < 10 {
character += '0'
} else {
character += 'A' - 10
}
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
}
}
func drawDecimal (xOffset, yOffset int, number uint64) {
var divisor uint64 = 10000000000000000000
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "10")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 20; x ++ {
trueX := x + xOffset + 5
character := rune((number / divisor) % 10) + '0'
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
divisor /= 10
}
}
func drawOctal (xOffset, yOffset int, number uint64) {
// fuck octal i hate it almost as much as decimal
bitOffset := 63
application.SetDot(xOffset, yOffset)
fmt.Fprint(application, "08")
application.SetColor(xOffset, yOffset, stone.ColorBlue)
application.SetColor(xOffset + 1, yOffset, stone.ColorBlue)
for x := 0; x < 21; x ++ {
bitOffset -= 3
trueX := x + xOffset + 4
character := '0' + rune((number >> bitOffset) % 8)
application.SetRune(trueX, yOffset, character)
if character == '0' {
application.SetColor (
trueX,
yOffset,
stone.ColorDim)
} else {
application.SetColor (
trueX,
yOffset,
stone.ColorForeground)
}
}
}