2023-01-03 13:34:32 +01:00
|
|
|
package ui
|
2022-11-23 20:33:07 +01:00
|
|
|
|
|
|
|
import (
|
2023-01-04 13:38:16 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2022-11-23 20:33:07 +01:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/layout"
|
2023-01-04 13:38:16 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-01-03 15:44:52 +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
|
2023-01-04 13:38:16 +01:00
|
|
|
OptionalCheck *widget.Check
|
|
|
|
PayloadCheck *widget.Check
|
2022-11-23 20:33:07 +01:00
|
|
|
// to be filled with needed things
|
2023-01-03 15:44:52 +01:00
|
|
|
|
|
|
|
Reference *protocol.Field
|
2023-01-04 13:38:16 +01:00
|
|
|
Index int
|
2022-11-23 20:33:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 13:38:16 +01:00
|
|
|
func CreateFieldEditor(ed *ProtocolEditor, index int, ref *protocol.Field) *FieldEditor {
|
2023-01-03 15:44:52 +01:00
|
|
|
fc := &FieldEditor{}
|
2023-01-04 13:38:16 +01:00
|
|
|
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())
|
|
|
|
|
2023-01-04 13:38:16 +01:00
|
|
|
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()
|
2023-01-04 13:38:16 +01:00
|
|
|
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
|
2023-01-04 13:38:16 +01:00
|
|
|
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()
|
2023-01-04 13:38:16 +01:00
|
|
|
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
|
2023-01-04 13:38:16 +01:00
|
|
|
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()
|
2023-01-04 13:38:16 +01:00
|
|
|
fc.SizeValue.OnChanged = setfunc
|
2022-11-24 21:49:10 +01:00
|
|
|
fc.Representation.Add(fc.SizeValue)
|
2023-01-04 13:38:16 +01:00
|
|
|
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
|
|
|
|
}
|