Skip to content

Commit

Permalink
feat: cube gui component
Browse files Browse the repository at this point in the history
  • Loading branch information
virus-rpi committed Feb 8, 2024
1 parent 49b2fb7 commit 207f8dc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
52 changes: 52 additions & 0 deletions customComponents.go
Original file line number Diff line number Diff line change
@@ -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()
}
39 changes: 33 additions & 6 deletions gui.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package main

import (
"image/color"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
)

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",
Expand All @@ -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()
}

0 comments on commit 207f8dc

Please sign in to comment.