2023-01-03 13:34:32 +01:00
|
|
|
package ui
|
2022-11-24 21:49:10 +01:00
|
|
|
|
|
|
|
import (
|
2023-01-04 13:38:16 +01:00
|
|
|
"fmt"
|
|
|
|
|
2022-11-24 21:49:10 +01:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/container"
|
2023-05-12 09:10:24 +02:00
|
|
|
"gitea.mmo.to/ppForge/ppforge/protocol"
|
|
|
|
"gitea.mmo.to/ppForge/ppforge/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
|
2023-01-03 15:44:52 +01:00
|
|
|
Fields []*FieldEditor
|
2022-11-24 21:49:10 +01:00
|
|
|
|
2023-01-03 15:44:52 +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
|
|
|
|
}
|
|
|
|
|
2023-01-03 15:44:52 +01:00
|
|
|
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() {
|
2023-01-04 13:38:16 +01:00
|
|
|
json := protocolctl.ToJson(ed.Reference)
|
|
|
|
fmt.Printf("%s", json)
|
2023-03-31 15:38:20 +02:00
|
|
|
ed.FieldContainer.RemoveAll()
|
|
|
|
// existing fields
|
2023-01-04 13:38:16 +01:00
|
|
|
for i, v := range ed.Reference.Structure {
|
2023-03-31 15:38:20 +02:00
|
|
|
ed.FieldContainer.Add(CreateFieldEditor(ed, i, v).Representation)
|
2023-01-04 13:38:16 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|