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 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 ( require (
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 // indirect 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-20221124013405-5a76bd0c223d h1:2uDDq+lHbhODq+6hmosmCcAaLOg55a7LOIhiJMzeSds=
git.tebibyte.media/sashakoshka/stone v0.0.0-20221122052135-ae514f5ae2db/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A= 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 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA=
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ= 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= 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) { func onPress (button stone.Button, modifiers stone.Modifiers) {
switch button { switch button {
case stone.KeyUp: case ',':
if selectedExpression == nil { break } if selectedExpression == nil { break }
newSelection := selectedExpression.Parent() newSelection := selectedExpression.Parent()
@ -71,13 +71,22 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
application.Draw() application.Draw()
case '[': 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() redraw()
application.Draw() application.Draw()
case case
'+', '-', '*', '/', 'p', 'r', '%', '|', '~', '&', '^', 'm', '+', '-', '*', '/', 'p', 'P', '%', '|', '~', '&', '^', 'm',
'<', '>': 'l', 'r':
insertOperation(rune(button), modifiers.Alt) insertOperation(rune(button), modifiers.Alt)
redraw() redraw()
@ -137,9 +146,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
application.Draw() application.Draw()
case ' ': case ' ':
insertGeneric ( insertNumber(0, modifiers.Alt, modifiers.Shift)
&IntegerLiteral { displayRadix: 10 },
modifiers.Alt)
redraw() redraw()
application.Draw() application.Draw()
@ -157,10 +164,7 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
switch selectedExpression.(type) { switch selectedExpression.(type) {
case *Operation, nil: case *Operation, nil:
insertGeneric (&IntegerLiteral { insertNumber(int64(value), modifiers.Alt, false)
displayRadix: 10,
value: int64(value),
}, modifiers.Alt)
case *IntegerLiteral: case *IntegerLiteral:
integer := selectedExpression.(*IntegerLiteral) integer := selectedExpression.(*IntegerLiteral)
@ -228,12 +232,12 @@ func onPress (button stone.Button, modifiers stone.Modifiers) {
redraw() redraw()
application.Draw() application.Draw()
case 'R': case 'q':
showEndResult = !showEndResult showEndResult = !showEndResult
redraw() redraw()
application.Draw() application.Draw()
case 'F': case 'Z':
showFloat = !showFloat showFloat = !showFloat
redraw() redraw()
application.Draw() application.Draw()
@ -248,14 +252,14 @@ func insertOperation (symbol rune, swap bool) {
case '*': opcode = OpcodeMultiply case '*': opcode = OpcodeMultiply
case '/': opcode = OpcodeDivide case '/': opcode = OpcodeDivide
case 'p': opcode = OpcodePower case 'p': opcode = OpcodePower
case 'r': opcode = OpcodeRoot case 'P': opcode = OpcodeRoot
case '%': opcode = OpcodeModulo case '%': opcode = OpcodeModulo
case '|': opcode = OpcodeOr case '|': opcode = OpcodeOr
case '~': opcode = OpcodeNot case '~': opcode = OpcodeNot
case '&': opcode = OpcodeAnd case '&': opcode = OpcodeAnd
case '^': opcode = OpcodeXor case '^': opcode = OpcodeXor
case '<': opcode = OpcodeLeftShift case 'l': opcode = OpcodeLeftShift
case '>': opcode = OpcodeRightShift case 'r': opcode = OpcodeRightShift
case 'm': opcode = OpcodeMean case 'm': opcode = OpcodeMean
} }
@ -268,48 +272,41 @@ func insertOperation (symbol rune, swap bool) {
} }
newExpression := &Operation { opcode: opcode, floating: showFloat } newExpression := &Operation { opcode: opcode, floating: showFloat }
insertGeneric(newExpression, swap) insertGeneric(newExpression, swap, false, true)
} }
func insertGeneric (expression Expression, swap bool) { func insertNumber (value int64, swap, before bool) {
defer func () { 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 selectedExpression = expression
} ()
if expressionRoot == nil {
// if there are no expressions, place this one down in the empty
// void
expressionRoot = expression
return return
} }
parent := selectedExpression.Parent()
if swap { // prefer filling an empty operation
// if alt is held, swap the selected expression for the new one
if parent == nil {
expressionRoot = expression
} else {
selectedExpression.Parent().Swap(selectedExpression, expression)
}
return
}
operation, isOperation := selectedExpression.(*Operation) operation, isOperation := selectedExpression.(*Operation)
if fillIn && isOperation && len(operation.operands) == 0 {
if isOperation && (len(operation.operands) < 1 || parent == nil) {
// if we have an empty operation selected, always insert the
// new expression into it
operation.Adopt(expression) operation.Adopt(expression)
selectedExpression = expression
return return
} }
// else, insert the new expression after the selected expression // insert expression into the parent operation
if parent == nil { parent := selectedExpression.Parent()
expressionRoot = expression if parent == nil { return }
if swap {
parent.Swap(selectedExpression, expression)
} else { } else {
selectedExpression.Parent().Insert(selectedExpression, expression) parent.Insert(selectedExpression, expression, !before)
} }
selectedExpression = expression
} }
func onStart () { 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 operand Expression
var index int var index int
var found bool var found bool
for index, operand = range operation.operands { for index, operand = range operation.operands {
if operand == after { found = true; break } if operand == around { found = true; break }
} }
if !found { return } if !found { return }
index ++ if after { index ++ }
operation.operands = append(operation.operands, nil) operation.operands = append(operation.operands, nil)
copy(operation.operands[index + 1:], operation.operands[index:]) copy(operation.operands[index + 1:], operation.operands[index:])
operation.operands[index] = child operation.operands[index] = child
operation.adopt(child) operation.adopt(child)
} }
func (operation *Operation) Disown (child Expression) { func (operation *Operation) Disown (child Expression) {