ppforge/internal/ui/fieldeditor.go

129 lines
3.5 KiB
Go
Raw Normal View History

package ui
2022-11-23 20:33:07 +01:00
import (
"fmt"
"strconv"
2022-11-23 20:33:07 +01:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
2022-11-23 20:33:07 +01:00
"fyne.io/fyne/v2/widget"
2023-05-12 09:10:24 +02:00
"gitea.mmo.to/ppForge/ppforge/protocol"
"gitea.mmo.to/ppForge/ppforge/protocolctl"
2022-11-23 20:33:07 +01:00
)
type FieldEditor struct {
2022-11-23 20:33:07 +01:00
Representation *fyne.Container
NameValue *widget.Entry
DescValue *widget.Entry
RegExValue *widget.Entry
2022-11-24 21:49:10 +01:00
SizeLabel *widget.Label
SizeValue *widget.Entry
SizeInBits bool
OptionalCheck *widget.Check
PayloadCheck *widget.Check
2022-11-23 20:33:07 +01:00
// to be filled with needed things
Reference *protocol.Field
Index int
2022-11-23 20:33:07 +01:00
}
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))
}
2022-11-24 21:49:10 +01:00
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")))
2022-11-23 20:33:07 +01:00
fc.Representation.Add(widget.NewLabel("Name"))
fc.NameValue = widget.NewEntry()
fc.NameValue.OnChanged = setfunc
2022-11-23 20:33:07 +01:00
fc.Representation.Add(fc.NameValue)
fc.Representation.Add(widget.NewLabel("Description"))
fc.DescValue = widget.NewEntry()
2022-11-24 21:49:10 +01:00
fc.DescValue.MultiLine = true
fc.DescValue.OnChanged = setfunc
2022-11-24 21:49:10 +01:00
fc.DescValue.SetMinRowsVisible(3)
2022-11-23 20:33:07 +01:00
fc.Representation.Add(fc.DescValue)
fc.Representation.Add(widget.NewLabel("RegEx"))
fc.RegExValue = widget.NewEntry()
fc.RegExValue.OnChanged = setfunc
2022-11-23 20:33:07 +01:00
fc.Representation.Add(fc.RegExValue)
2022-11-24 21:49:10 +01:00
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
2022-11-24 21:49:10 +01:00
})
fc.Representation.Add(container.NewHBox(toggleBtn, fc.SizeLabel))
fc.SizeValue = widget.NewEntry()
fc.SizeValue.OnChanged = setfunc
2022-11-24 21:49:10 +01:00
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()
2022-11-23 20:33:07 +01:00
return fc
}