summaryrefslogtreecommitdiff
path: root/cirsim_fyne/node.go
blob: 65c7976206fd3092554b7cf14d94ea5028cfee44 (plain)
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
package cirsim_fyne

import (
	"fmt"
	"io"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/widget"
	"github.com/wcharczuk/go-chart/v2"
	"github.com/wcharczuk/go-chart/v2/drawing"
)

type node struct {
	widget.BaseWidget
	pos          fyne.Position
	voltageRange chart.Range
	chart        *canvas.Image
}

func newNode(settings io.Reader, r chart.Range) *node {
	var n node
	_, err := fmt.Fscanf(settings, "%f %f\n", &n.pos.X, &n.pos.Y)
	if err != nil {
		return nil
	}
	n.voltageRange = r
	return &n
}

func (n *node) renderChart(voltageOverTime []float64) {
	graph := chart.Chart{
		Width:        chartWidth,
		Height:       chartHeight,
		ColorPalette: &nodeColorPalette{},
		XAxis:        chart.HideXAxis(),
		YAxis: chart.YAxis{
			Style: chart.Hidden(),
			Range: n.voltageRange,
		},
		Series: []chart.Series{
			chart.ContinuousSeries{
				XValues: chart.LinearRange(0, float64(len(voltageOverTime))-1),
				YValues: voltageOverTime,
			},
		},
	}
	writer := &chart.ImageWriter{}
	graph.Render(chart.PNG, writer)
	img, _ := writer.Image()
	n.chart = canvas.NewImageFromImage(img)
}

type nodeColorPalette struct{}

func (*nodeColorPalette) BackgroundColor() drawing.Color {
	return drawing.ColorTransparent
}
func (*nodeColorPalette) BackgroundStrokeColor() drawing.Color {
	return drawing.ColorTransparent
}
func (*nodeColorPalette) CanvasColor() drawing.Color {
	return drawing.Color{
		R: voltageCanvasR,
		G: voltageCanvasG,
		B: voltageCanvasB,
		A: voltageCanvasA,
	}
}
func (*nodeColorPalette) CanvasStrokeColor() drawing.Color {
	return drawing.ColorTransparent
}
func (*nodeColorPalette) AxisStrokeColor() drawing.Color {
	return drawing.ColorTransparent
}
func (*nodeColorPalette) TextColor() drawing.Color {
	return drawing.ColorTransparent
}
func (*nodeColorPalette) GetSeriesColor(index int) drawing.Color {
	return drawing.Color{R: voltageR, G: voltageG, B: voltageB, A: voltageA}
}

func (n *node) CreateRenderer() fyne.WidgetRenderer { return n }
func (n *node) Layout(s fyne.Size) {
	n.chart.Resize(s)
	n.chart.Move(fyne.NewPos(0, 0))
}
func (n *node) MinSize() fyne.Size { return n.chart.MinSize() }
func (n *node) Refresh()           {}
func (n *node) Destroy()           {}
func (n *node) Objects() []fyne.CanvasObject {
	return []fyne.CanvasObject{n.chart}
}