summaryrefslogtreecommitdiff
path: root/cirsim_fyne/node.go
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2022-04-22 20:25:47 +0300
committerAleksey Veresov <aleksey@veresov.pro>2022-04-22 20:25:47 +0300
commit571f9b73e79abc7116f29e557a38fc68ec045d3e (patch)
treeb833ba89dc23f03932669f37e86fae0087b89820 /cirsim_fyne/node.go
parent6d289b952d1b5c8b8e151ded0195e4658f5d6d0b (diff)
downloadcirsim-571f9b73e79abc7116f29e557a38fc68ec045d3e.tar
cirsim-571f9b73e79abc7116f29e557a38fc68ec045d3e.tar.xz
cirsim-571f9b73e79abc7116f29e557a38fc68ec045d3e.zip
Splits logic and graphics.
Diffstat (limited to 'cirsim_fyne/node.go')
-rw-r--r--cirsim_fyne/node.go94
1 files changed, 94 insertions, 0 deletions
diff --git a/cirsim_fyne/node.go b/cirsim_fyne/node.go
new file mode 100644
index 0000000..84a15eb
--- /dev/null
+++ b/cirsim_fyne/node.go
@@ -0,0 +1,94 @@
+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
+ voltageOverTime []float64
+ 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}
+}