ppforge/internal/ui/protocoleditor.go

49 lines
1.3 KiB
Go
Raw Normal View History

package ui
2022-11-24 21:49:10 +01:00
import (
"fmt"
2022-11-24 21:49:10 +01:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl"
2022-11-24 21:49:10 +01:00
)
type ProtocolEditor struct {
Representation *fyne.Container
2023-03-31 15:38:20 +02:00
FieldContainer *fyne.Container
Metadata *ProtocolMetadata
Fields []*FieldEditor
2022-11-24 21:49:10 +01:00
Reference *protocol.ProtocolStructure
}
2022-11-24 21:49:10 +01:00
2023-01-05 14:16:41 +01:00
func NewProtocolEditor(ref *protocol.ProtocolStructure) *ProtocolEditor {
2023-03-31 15:38:20 +02:00
metadata := NewMetadataProtocol()
fields := container.NewGridWrap(fyne.NewSize(300, 400))
container := container.NewBorder(nil, nil, metadata.Representation, nil, container.NewVScroll(fields))
protocolEditor := &ProtocolEditor{container, fields, metadata, []*FieldEditor{}, ref}
2023-01-05 14:16:41 +01:00
protocolEditor.Redraw()
return protocolEditor
}
func (ed *ProtocolEditor) AddFieldCreator(fieldCreator *FieldEditor) {
ed.Fields = append(ed.Fields, fieldCreator)
2022-11-24 21:49:10 +01:00
ed.Redraw()
}
func (ed *ProtocolEditor) Redraw() {
json := protocolctl.ToJson(ed.Reference)
fmt.Printf("%s", json)
2023-03-31 15:38:20 +02:00
ed.FieldContainer.RemoveAll()
// existing fields
for i, v := range ed.Reference.Structure {
2023-03-31 15:38:20 +02:00
ed.FieldContainer.Add(CreateFieldEditor(ed, i, v).Representation)
}
if len(ed.Reference.Structure) == 0 {
2023-03-31 15:38:20 +02:00
ed.FieldContainer.Add(GetAdder(ed))
2022-11-24 21:49:10 +01:00
}
2023-03-31 15:38:20 +02:00
// Metadata
ed.Metadata.SetProtocol(ed.Reference)
2022-11-24 21:49:10 +01:00
}