From 207f8dcf9ad39eddb668262fec904ce30f694db5 Mon Sep 17 00:00:00 2001 From: virus-rpi <72336443+virus-rpi@users.noreply.github.com> Date: Thu, 8 Feb 2024 18:55:02 +0100 Subject: [PATCH] feat: cube gui component --- customComponents.go | 52 +++++++++++++++++++++++++++++++++++++++++++++ gui.go | 39 ++++++++++++++++++++++++++++------ 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 customComponents.go diff --git a/customComponents.go b/customComponents.go new file mode 100644 index 0000000..15a004e --- /dev/null +++ b/customComponents.go @@ -0,0 +1,52 @@ +package main + +import ( + "math" + "time" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/canvas" + "fyne.io/fyne/v2/widget" +) + +type cube struct { + widget.Icon + x, y float32 + size float32 +} + +func newCube(textureUrl string) *cube { + texture, err := fyne.LoadResourceFromURLString(textureUrl) + if err != nil { + panic(err) + } + cubeImage := &cube{ + x: 400, + y: 300, + size: 150, + } + cubeImage.Resource = texture + cubeImage.Move(fyne.NewPos(cubeImage.x, cubeImage.y)) + cubeImage.Resize(fyne.NewSize(cubeImage.size, cubeImage.size)) + return cubeImage +} + +func (d *cube) Dragged(e *fyne.DragEvent) { + dx := d.x - (d.Position().X + e.Dragged.DX) + dy := d.y - (d.Position().Y + e.Dragged.DY) + distance := math.Sqrt(float64(dx*dx + dy*dy)) + scale := float32(1 / (1 + distance/50)) + d.Move(fyne.NewPos(d.Position().X+e.Dragged.DX*scale, d.Position().Y+e.Dragged.DY*scale)) + d.Refresh() +} + +func (d *cube) DragEnd() { + canvas.NewPositionAnimation( + d.Position(), + fyne.NewPos(d.x, d.y), + time.Second/2, + func(pos fyne.Position) { + d.Move(pos) + d.Refresh() + }).Start() +} diff --git a/gui.go b/gui.go index 99c341b..490f747 100644 --- a/gui.go +++ b/gui.go @@ -1,6 +1,9 @@ package main import ( + "image/color" + "time" + "fyne.io/fyne/v2" "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" @@ -8,9 +11,10 @@ import ( func gui(qubusApp fyne.App, defaults *Defaults) { qubusWindow := qubusApp.NewWindow("QUBUS core") - qubusWindow.Resize(fyne.NewSize(800, 600)) + qubusWindow.Resize(fyne.NewSize(1400, 900)) qubusWindow.CenterOnScreen() qubusWindow.SetIcon(qubusApp.Icon()) + qubusWindow.SetFixedSize(true) windowMenu := fyne.NewMainMenu( fyne.NewMenu("File", @@ -20,17 +24,40 @@ func gui(qubusApp fyne.App, defaults *Defaults) { fyne.NewMenuItem("Export config", func() { println("Export config") }), + fyne.NewMenuItem("Import config", func() { + println("Import config") + }), + fyne.NewMenuItem("Settings", func() { + println("Settings") + }), ), ) qubusWindow.SetMainMenu(windowMenu) - cubeImageResource, _ := fyne.LoadResourceFromURLString(defaults.CubeAssetURL) - cubeImage := canvas.NewImageFromResource(cubeImageResource) - cubeImage.Move(fyne.NewPos(400, 300)) - cubeImage.Resize(fyne.NewSize(150, 150)) + cubeImage := newCube(defaults.CubeAssetURL) - mainContainer := container.NewWithoutLayout(cubeImage) + infoContainerShape := canvas.NewRectangle(color.White) + infoContainerShape.Resize(fyne.NewSize(300, 825)) + infoContainerShape.Move(fyne.NewPos(1400, 25)) + infoContainerShape.CornerRadius = 12 + + mainContainer := container.NewWithoutLayout(cubeImage, infoContainerShape) qubusWindow.SetContent(mainContainer) + canvas.NewPositionAnimation( + infoContainerShape.Position(), + fyne.NewPos(1400-325, 25), + time.Second, + func(pos fyne.Position) { + infoContainerShape.Move(pos) + infoContainerShape.Refresh() + }).Start() + + go func() { + for { + mainContainer.Refresh() + } + }() + qubusWindow.ShowAndRun() }