Skip to content

Commit

Permalink
Fixed cloth position on reset after window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Oct 29, 2023
1 parent 4c65b6e commit b136932
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 19 deletions.
60 changes: 58 additions & 2 deletions hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

const Version = "v1.0.3"

var hudControlBtnColor = color.NRGBA{R: 0xd9, G: 0x03, B: 0x68, A: 0xff}

type (
D = layout.Dimensions
C = layout.Context
Expand Down Expand Up @@ -211,7 +213,9 @@ func (h *Hud) ShowControlPanel(gtx layout.Context, th *material.Theme, m *Mouse,
return layout.UniformInset(unit.Dp(5)).Layout(gtx, material.CheckBox(th, &h.debug, "Show Frame Rates").Layout)
}),
layout.Rigid(func(gtx C) D {
return layout.UniformInset(unit.Dp(10)).Layout(gtx, material.Button(th, &h.reset, "Reset").Layout)
btnTheme := material.NewTheme()
btnTheme.Palette.ContrastBg = hudControlBtnColor
return layout.UniformInset(unit.Dp(10)).Layout(gtx, material.Button(btnTheme, &h.reset, "Reset").Layout)
}),
)
}),
Expand Down Expand Up @@ -305,7 +309,7 @@ func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isAc
pointer.CursorPointer.Add(gtx.Ops)
h.activator.Add(gtx.Ops)

paint.ColorOp{Color: color.NRGBA{R: 0xd9, G: 0x03, B: 0x68, A: 0xff}}.Add(gtx.Ops)
paint.ColorOp{Color: hudControlBtnColor}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)

return layout.Dimensions{}
Expand All @@ -314,3 +318,55 @@ func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isAc
)
offStack.Pop()
}

func (h *Hud) ShowHelpDialog(gtx layout.Context, th *material.Theme, m *Mouse, isActive bool) {
if !isActive {
return
}

layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
centerX := gtx.Constraints.Max.X / 2
centerY := gtx.Constraints.Max.Y / 2

dialogWidth := gtx.Constraints.Max.X / 3
dialogHeight := gtx.Constraints.Max.Y / 3

px := gtx.Dp(unit.Dp(dialogWidth / 2))
py := gtx.Dp(unit.Dp(dialogHeight / 2))

dx, dy := centerX-px, centerY-py
fmt.Println(dialogWidth, dialogHeight)

// This offset will apply to the rest of the content laid out in this function.
defer op.Offset(image.Point{X: dx, Y: dy}).Push(gtx.Ops).Pop()

paint.FillShape(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
clip.Rect{Max: image.Point{
X: dx,
Y: dy,
}}.Op())
paint.FillShape(gtx.Ops, color.NRGBA{A: 0xff},
clip.Stroke{
Path: clip.Rect{Max: image.Point{
X: dx,
Y: dy,
}}.Path(),
Width: 0.2,
}.Op(),
)

pointer.InputOp{
Tag: &h.hudTag,
Types: pointer.Scroll | pointer.Move | pointer.Press | pointer.Drag | pointer.Release | pointer.Leave,
}.Add(gtx.Ops)
h.controls.Add(gtx.Ops)

pointer.CursorPointer.Add(gtx.Ops)

return layout.Dimensions{
Size: gtx.Constraints.Max,
}
}),
)
}
40 changes: 23 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import (
const (
hudTimeout = 2.5
delta = 0.022

defaultWindowWidth = 940
defaultWindowHeigth = 580
)

var (
windowWidth = 940
windowHeight = 580
windowWidth = defaultWindowWidth
windowHeight = defaultWindowHeigth

// Gio Ops related variables
ops op.Ops
Expand Down Expand Up @@ -62,7 +65,6 @@ func main() {
app.Title("Gio - 2D Cloth Simulation"),
app.Size(unit.Dp(windowWidth), unit.Dp(windowHeight)),
)
//w.Perform(system.ActionMaximize)
if err := loop(w); err != nil {
log.Fatal(err)
}
Expand All @@ -89,8 +91,8 @@ func loop(w *app.Window) error {

isDragging := false

var clothW int = int(float64(windowWidth) * 1.3)
var clothH int = int(float64(windowHeight) * 0.4)
var clothW int = int(unit.Dp(windowWidth) * 1.2)
var clothH int = int(unit.Dp(windowHeight) * 0.4)

cloth := NewCloth(clothW, clothH, 8, 0.99, defaultColor)
hud := NewHud()
Expand Down Expand Up @@ -129,8 +131,9 @@ func loop(w *app.Window) error {
width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y

startX := width/2 - clothW/2
startY := int(float64(height) * 0.2)
startX := (width - clothW) / 2
startY := int(unit.Dp(height) * 0.2)

cloth.Init(startX, startY, hud)
}

Expand All @@ -144,21 +147,21 @@ func loop(w *app.Window) error {
mouse.increaseForce(deltaTime.Seconds())
}

resetCloth := func() {
width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y

startX := width/2 - clothW/2
startY := int(float64(height) * 0.2)
cloth.Reset(startX, startY, hud)
}

for _, ev := range gtx.Queue.Events(&keyTag) {
if e, ok := ev.(key.Event); ok {
if e.State == key.Press {
switch e.Name {
case key.NameSpace:
resetCloth()
width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y

startX := (width - clothW) / 2
startY := int(unit.Dp(height) * 0.2)

cloth.width = clothW
cloth.height = clothH

cloth.Reset(startX, startY, hud)
case key.NameF1:
hud.showHelp = !hud.showHelp
}
Expand All @@ -185,9 +188,11 @@ func loop(w *app.Window) error {

windowWidth = e.Size.X
windowHeight = e.Size.Y

cloth.width = windowWidth
cloth.height = windowHeight
}

fillBackground(gtx, color.NRGBA{R: 0xf2, G: 0xf2, B: 0xf2, A: 0xff})

layout.Stack{}.Layout(gtx,
Expand Down Expand Up @@ -298,6 +303,7 @@ func loop(w *app.Window) error {
}
hud.DrawCtrlBtn(gtx, th, mouse, hud.isActive)
hud.ShowControlPanel(gtx, th, mouse, hud.isActive)
hud.ShowHelpDialog(gtx, th, mouse, hud.showHelp)

return layout.Dimensions{}
}),
Expand Down

0 comments on commit b136932

Please sign in to comment.