Redid input system to make it not terrible

This commit is contained in:
Sasha Koshka 2022-11-23 21:21:49 -05:00
parent cf10dde21a
commit 17c455b73d
5 changed files with 50 additions and 52 deletions

4
go.mod
View File

@ -1,8 +1,8 @@
module git.tebibyte.media/sashakoshka/mathpan
go 1.18
go 1.19
require git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db
require git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d
require (
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 // indirect

4
go.sum
View File

@ -1,5 +1,5 @@
git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db h1:2llsOVwxK5oK4E/RX21tzafQBc6N+H6MwRSQykCb6ss=
git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A=
git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d h1:2uDDq+lHbhODq+6hmosmCcAaLOg55a7LOIhiJMzeSds=
git.tebibyte.media/sashakoshka/stone v0.0.0-20221124013405-5a76bd0c223d/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A=
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA=
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ=
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 h1:lTG4HQym5oPKjL7nGs+csTgiDna685ZXjxijkne828g=

BIN
keyboard.xcf Normal file

Binary file not shown.

87
main.go
View File

@ -37,7 +37,7 @@ func main () {
func onPress (button stone.Button, modifiers stone.Modifiers) {
switch button {
case stone.KeyUp:
case ',':
if selectedExpression == nil { break }
newSelection := selectedExpression.Parent()
@ -71,13 +71,22 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
application.Draw()
case '[':
insertGeneric(&Operation { floating: showFloat }, modifiers.Alt)
insertGeneric (&Operation {
floating: showFloat,
}, modifiers.Alt, true, false)
redraw()
application.Draw()
case ']':
insertGeneric (&Operation {
floating: showFloat,
}, modifiers.Alt, false, false)
redraw()
application.Draw()
case
'+', '-', '*', '/', 'p', 'r', '%', '|', '~', '&', '^', 'm',
'<', '>':
'+', '-', '*', '/', 'p', 'P', '%', '|', '~', '&', '^', 'm',
'l', 'r':
insertOperation(rune(button), modifiers.Alt)
redraw()
@ -137,9 +146,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
application.Draw()
case ' ':
insertGeneric (
&IntegerLiteral { displayRadix: 10 },
modifiers.Alt)
insertNumber(0, modifiers.Alt, modifiers.Shift)
redraw()
application.Draw()
@ -157,10 +164,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
switch selectedExpression.(type) {
case *Operation, nil:
insertGeneric (&IntegerLiteral {
displayRadix: 10,
value: int64(value),
}, modifiers.Alt)
insertNumber(int64(value), modifiers.Alt, false)
case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral)
@ -228,12 +232,12 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
redraw()
application.Draw()
case 'R':
case 'q':
showEndResult = !showEndResult
redraw()
application.Draw()
case 'F':
case 'Z':
showFloat = !showFloat
redraw()
application.Draw()
@ -248,14 +252,14 @@ func insertOperation (symbol rune, swap bool) {
case '*': opcode = OpcodeMultiply
case '/': opcode = OpcodeDivide
case 'p': opcode = OpcodePower
case 'r': opcode = OpcodeRoot
case 'P': opcode = OpcodeRoot
case '%': opcode = OpcodeModulo
case '|': opcode = OpcodeOr
case '~': opcode = OpcodeNot
case '&': opcode = OpcodeAnd
case '^': opcode = OpcodeXor
case '<': opcode = OpcodeLeftShift
case '>': opcode = OpcodeRightShift
case 'l': opcode = OpcodeLeftShift
case 'r': opcode = OpcodeRightShift
case 'm': opcode = OpcodeMean
}
@ -268,48 +272,41 @@ func insertOperation (symbol rune, swap bool) {
}
newExpression := &Operation { opcode: opcode, floating: showFloat }
insertGeneric(newExpression, swap)
insertGeneric(newExpression, swap, false, true)
}
func insertGeneric (expression Expression, swap bool) {
defer func () {
func insertNumber (value int64, swap, before bool) {
insertGeneric (&IntegerLiteral {
displayRadix: 10,
value: value,
}, swap, before, true)
}
func insertGeneric (expression Expression, swap, before, fillIn bool) {
// if there are no expressions, place this at root
if selectedExpression == nil || expressionRoot == nil {
expressionRoot = expression
selectedExpression = expression
} ()
if expressionRoot == nil {
// if there are no expressions, place this one down in the empty
// void
expressionRoot = expression
return
}
parent := selectedExpression.Parent()
if swap {
// if alt is held, swap the selected expression for the new one
if parent == nil {
expressionRoot = expression
} else {
selectedExpression.Parent().Swap(selectedExpression, expression)
}
return
}
// prefer filling an empty operation
operation, isOperation := selectedExpression.(*Operation)
if isOperation && (len(operation.operands) < 1 || parent == nil) {
// if we have an empty operation selected, always insert the
// new expression into it
if fillIn && isOperation && len(operation.operands) == 0 {
operation.Adopt(expression)
selectedExpression = expression
return
}
// else, insert the new expression after the selected expression
if parent == nil {
expressionRoot = expression
// insert expression into the parent operation
parent := selectedExpression.Parent()
if parent == nil { return }
if swap {
parent.Swap(selectedExpression, expression)
} else {
selectedExpression.Parent().Insert(selectedExpression, expression)
parent.Insert(selectedExpression, expression, !before)
}
selectedExpression = expression
}
func onStart () {

View File

@ -96,21 +96,22 @@ func (operation *Operation) adopt (child Expression) {
}
}
func (operation *Operation) Insert (after Expression, child Expression) {
func (operation *Operation) Insert (around, child Expression, after bool) {
var operand Expression
var index int
var found bool
for index, operand = range operation.operands {
if operand == after { found = true; break }
if operand == around { found = true; break }
}
if !found { return }
index ++
if after { index ++ }
operation.operands = append(operation.operands, nil)
copy(operation.operands[index + 1:], operation.operands[index:])
operation.operands[index] = child
operation.adopt(child)
}
func (operation *Operation) Disown (child Expression) {