ppforge/internal/ui/fieldeditor.go

129 lines
3.5 KiB
Go

package ui
import (
"fmt"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"gitea.mmo.to/ppForge/ppforge/protocol"
"gitea.mmo.to/ppForge/ppforge/protocolctl"
)
type FieldEditor struct {
Representation *fyne.Container
NameValue *widget.Entry
DescValue *widget.Entry
RegExValue *widget.Entry
SizeLabel *widget.Label
SizeValue *widget.Entry
SizeInBits bool
OptionalCheck *widget.Check
PayloadCheck *widget.Check
// to be filled with needed things
Reference *protocol.Field
Index int
}
func CreateFieldEditor(ed *ProtocolEditor, index int, ref *protocol.Field) *FieldEditor {
fc := &FieldEditor{}
fc.Reference = ref
fc.Index = index
setfunc := func(s string) {
bits := 0
if !fc.SizeInBits {
bits, _ = strconv.Atoi(fc.SizeValue.Text)
bits *= 8
} else {
bits, _ = strconv.Atoi(fc.SizeValue.Text)
}
protocolctl.UpdateFieldByElement(
ed.Reference,
fc.Index,
protocolctl.NewField(
fc.NameValue.Text,
fc.DescValue.Text,
fc.RegExValue.Text,
bits,
nil, //TODO: implement subfields
fc.OptionalCheck.Checked,
fc.PayloadCheck.Checked,
),
)
fmt.Printf("%s", protocolctl.ToJson(ed.Reference))
}
fc.Representation = container.New(layout.NewFormLayout())
toolbar := widget.NewToolbar()
toolbar.Append(widget.NewToolbarAction(theme.DeleteIcon(), func() {
protocolctl.RemoveFieldByElement(ed.Reference, fc.Index)
ed.Redraw()
}))
fc.Representation.Add(toolbar)
addtool := widget.NewToolbar()
addtool.Append(widget.NewToolbarSpacer())
addtool.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() {
protocolctl.AddField(ed.Reference, fc.Index+1, protocolctl.NewEmptyField())
ed.Redraw()
}))
fc.Representation.Add(container.NewBorder(nil, nil, nil, addtool, widget.NewLabel("Field Values")))
fc.Representation.Add(widget.NewLabel("Name"))
fc.NameValue = widget.NewEntry()
fc.NameValue.OnChanged = setfunc
fc.Representation.Add(fc.NameValue)
fc.Representation.Add(widget.NewLabel("Description"))
fc.DescValue = widget.NewEntry()
fc.DescValue.MultiLine = true
fc.DescValue.OnChanged = setfunc
fc.DescValue.SetMinRowsVisible(3)
fc.Representation.Add(fc.DescValue)
fc.Representation.Add(widget.NewLabel("RegEx"))
fc.RegExValue = widget.NewEntry()
fc.RegExValue.OnChanged = setfunc
fc.Representation.Add(fc.RegExValue)
fc.SizeLabel = widget.NewLabel("Bytes")
toggleBtn := widget.NewButton("T", func() {
if fc.SizeInBits {
fc.SizeLabel.SetText("Bytes")
} else {
fc.SizeLabel.SetText("Bits")
}
fc.SizeInBits = !fc.SizeInBits
setfunc("") // bogus call to reflect the change
})
fc.Representation.Add(container.NewHBox(toggleBtn, fc.SizeLabel))
fc.SizeValue = widget.NewEntry()
fc.SizeValue.OnChanged = setfunc
fc.Representation.Add(fc.SizeValue)
fc.Representation.Add(widget.NewLabel("Settings"))
fc.OptionalCheck = widget.NewCheck("Optional", func(b bool) {
fc.Reference.Optional = b
setfunc("")
})
fc.PayloadCheck = widget.NewCheck("Payload", func(b bool) {
fc.Reference.Payload = b
setfunc("")
})
fc.Representation.Add(container.NewHBox(fc.OptionalCheck, fc.PayloadCheck))
fc.NameValue.Text = fc.Reference.Name
fc.DescValue.Text = fc.Reference.Desc
fc.RegExValue.Text = fc.Reference.Regex
fc.SizeValue.Text = strconv.Itoa(fc.Reference.Size)
fc.SizeInBits = true
fc.SizeLabel.SetText("Bits")
fc.OptionalCheck.Checked = fc.Reference.Optional
fc.PayloadCheck.Checked = fc.Reference.Payload
fc.Representation.Refresh()
return fc
}