/
diagramElement.go
158 lines (137 loc) · 4.63 KB
/
diagramElement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package diagramwidget
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
// DiagramElementProperties are the rendering properties of a DiagramElement
type DiagramElementProperties struct {
ForegroundColor color.Color
BackgroundColor color.Color
HandleColor color.Color
PadColor color.Color
TextSize float32
CaptionTextSize float32
Padding float32
StrokeWidth float32
PadStrokeWidth float32
HandleStrokeWidth float32
}
// DiagramElement is a widget that can be placed directly in a diagram. The most common
// elements are Node and Link widgets.
type DiagramElement interface {
fyne.Widget
// GetBackgroundColor returns the background color for the widget
GetBackgroundColor() color.Color
// GetConnectionPads() returns all of the connection pads on the element
GetConnectionPads() map[string]ConnectionPad
// GetForegroundColor returns the foreground color for the widget
GetForegroundColor() color.Color
// GetDefaultConnectionPad returns the default pad for the DiagramElement
GetDefaultConnectionPad() ConnectionPad
// GetDiagram returns the DiagramWidget to which the DiagramElement belongs
GetDiagram() *DiagramWidget
// GetDiagramElementID returns the string identifier provided at the time the DiagramElement was created
GetDiagramElementID() string
// GetHandle returns the handle with the indicated index name
GetHandle(string) *Handle
// GetHandleColor returns the color for the element's handles
GetHandleColor() color.Color
// GetPadColor returns the color for the element's pads
GetPadColor() color.Color
// GetProperties returns the properties of the DiagramElement
GetProperties() DiagramElementProperties
// handleDragged responds to drag events
handleDragged(handle *Handle, event *fyne.DragEvent)
// handleDragEnd responds to the end of a drag
handleDragEnd(handle *Handle)
// HideHandles hides the handles on the DiagramElement
HideHandles()
// IsLink returns true if the diagram element is a link
IsLink() bool
// IsNode returns true of the diagram element is a node
IsNode() bool
// Position returns the position of the diagram element
Position() fyne.Position
// SetForegroundColor sets the foreground color for the widget
SetForegroundColor(color.Color)
// SetBackgroundColor sets the background color for the widget
SetBackgroundColor(color.Color)
// SetProperties sets the foreground, background, and handle colors
SetProperties(DiagramElementProperties)
// ShowHandles shows the handles on the DiagramElement
ShowHandles()
// Size returns the size of the diagram element
Size() fyne.Size
}
type diagramElement struct {
widget.BaseWidget
diagram *DiagramWidget
properties DiagramElementProperties
// foregroundColor color.Color
// backgroundColor color.Color
// handleColor color.Color
id string
handles map[string]*Handle
pads map[string]ConnectionPad
}
func (de *diagramElement) GetDiagram() *DiagramWidget {
return de.diagram
}
func (de *diagramElement) GetDiagramElementID() string {
return de.id
}
func (de *diagramElement) GetBackgroundColor() color.Color {
return de.properties.BackgroundColor
}
func (de *diagramElement) GetConnectionPads() map[string]ConnectionPad {
return de.pads
}
func (de *diagramElement) GetForegroundColor() color.Color {
return de.properties.ForegroundColor
}
func (de *diagramElement) GetHandle(handleName string) *Handle {
return de.handles[handleName]
}
func (de *diagramElement) GetHandleColor() color.Color {
return de.properties.HandleColor
}
func (de *diagramElement) GetPadColor() color.Color {
return de.properties.PadColor
}
func (de *diagramElement) GetProperties() DiagramElementProperties {
return de.properties
}
func (de *diagramElement) HideHandles() {
for _, handle := range de.handles {
handle.Hide()
}
}
func (de *diagramElement) initialize(diagram *DiagramWidget, id string) {
de.diagram = diagram
de.id = id
de.handles = make(map[string]*Handle)
de.properties = de.diagram.DefaultDiagramElementProperties
de.pads = make(map[string]ConnectionPad)
}
func (de *diagramElement) SetBackgroundColor(backgroundColor color.Color) {
de.properties.BackgroundColor = backgroundColor
de.Refresh()
}
func (de *diagramElement) SetForegroundColor(foregroundColor color.Color) {
de.properties.ForegroundColor = foregroundColor
de.Refresh()
}
func (de *diagramElement) SetHandleColor(handleColor color.Color) {
de.properties.HandleColor = handleColor
de.Refresh()
}
func (de *diagramElement) SetProperties(properties DiagramElementProperties) {
de.properties = properties
}
func (de *diagramElement) ShowHandles() {
for _, handle := range de.handles {
handle.Show()
de.Refresh()
}
}