Skip to content

Commit

Permalink
Rendering improvements and fix HUD assets
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Oct 16, 2023
1 parent 47aa0f5 commit 8c00735
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 39 deletions.
Binary file modified cloth-sim.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions cloth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func (c *Cloth) Init(posX, posY int, hud *Hud) {
// Update is invoked on each frame event of the Gio internal window calls.
// It updates the cloth particles, which are the basic entities over the
// cloth constraints are applied and solved using Verlet integration.
func (cloth *Cloth) Update(gtx layout.Context, mouse *Mouse, hud *Hud, delta float64) {
func (cloth *Cloth) Update(gtx layout.Context, mouse *Mouse, hud *Hud, dt float64) {
dragForce := float32(mouse.getForce() * 0.75)
clothColor := color.NRGBA{R: 0x55, A: 0xff}
// Convert the RGB color to HSL based on the applied force over the mouse focus area.
col := LinearFromSRGB(clothColor).HSLA().Lighten(dragForce).RGBA().SRGB()

for _, p := range cloth.particles {
p.Update(gtx, mouse, hud, delta)
p.Update(gtx, mouse, hud, dt)
}

for _, c := range cloth.constraints {
Expand Down
50 changes: 27 additions & 23 deletions hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"gioui.org/widget/material"
)

const maxIconBorderWidth = unit.Dp(5)
const Version = "v1.0.2"
const Version = "v1.0.3"

type (
D = layout.Dimensions
Expand Down Expand Up @@ -71,7 +70,7 @@ func NewHud() *Hud {
}

slide := &Easing{duration: 600 * time.Millisecond}
hover := &Easing{duration: 300 * time.Millisecond}
hover := &Easing{duration: 700 * time.Millisecond}

h.debug = widget.Bool{}
h.debug.Value = false
Expand Down Expand Up @@ -127,16 +126,18 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,
)

{ // Draw close button
offset := float32(gtx.Dp(unit.Dp(20)))

var path clip.Path
path.Begin(gtx.Ops)
path.MoveTo(f32.Pt(10, 10))
path.LineTo(f32.Pt(float32(h.closeBtn)-10, float32(h.closeBtn)-10))
path.MoveTo(f32.Pt(float32(h.closeBtn)-10, 10))
path.LineTo(f32.Pt(10, float32(h.closeBtn)-10))
path.MoveTo(f32.Pt(offset, offset))
path.LineTo(f32.Pt(float32(h.closeBtn)-offset, float32(h.closeBtn)-offset))
path.MoveTo(f32.Pt(float32(h.closeBtn)-offset, offset))
path.LineTo(f32.Pt(offset, float32(h.closeBtn)-offset))

paint.FillShape(gtx.Ops, color.NRGBA{A: 0xff}, clip.Stroke{
Path: path.End(),
Width: 5,
Width: float32(gtx.Dp(unit.Dp(4))),
}.Op())
}

Expand Down Expand Up @@ -217,7 +218,7 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,
w := material.Body1(th, fmt.Sprintf("2D Cloth Simulation %s\nCopyright © 2023, Endre Simo", Version))
w.Alignment = text.End
w.Color = th.ContrastBg
w.TextSize = 10
w.TextSize = 14
txtOffs := h.height - (3 * h.closeBtn)

defer op.Offset(image.Point{Y: txtOffs}).Push(gtx.Ops).Pop()
Expand All @@ -234,7 +235,7 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,
func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isActive bool) {
progress := h.slide.Update(gtx, isActive)
pos := h.slide.InOutBack(progress) * float64(h.height)
offset := gtx.Dp(55)
offset := gtx.Dp(unit.Dp(60))

offStack := op.Offset(image.Pt(0, gtx.Constraints.Max.Y-offset+int(pos))).Push(gtx.Ops)
layout.Stack{}.Layout(gtx,
Expand All @@ -248,25 +249,29 @@ func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isAc
}

progress := h.hover.Update(gtx, isActive || h.activator.Hovered())
width := h.hover.InOutBack(progress) * float64(maxIconBorderWidth)
width := h.hover.InOutBack(progress) * float64(unit.Dp(2))

var path clip.Path

spacing := h.btnSize / 4
startX := h.btnSize/2 - spacing
for i := 0; i < 3; i++ {
offset := float32(gtx.Dp(unit.Dp(10)))
btnSize := float32(gtx.Dp(unit.Dp(h.btnSize)))
spacing := btnSize / 4
startX := btnSize/2 - spacing

// HUD controls button
for i := float32(0); i < 3; i++ {
{ // Draw Line
func(x, y float32) {
path.Begin(gtx.Ops)
path.MoveTo(f32.Pt(float32(startX+(spacing*i)), 10))
path.LineTo(f32.Pt(float32(startX+(spacing*i)), float32(h.btnSize)-10))
path.MoveTo(f32.Pt(x, offset))
path.LineTo(f32.Pt(x, btnSize-offset))
path.Close()

paint.FillShape(gtx.Ops, color.NRGBA{A: 0xff}, clip.Stroke{
Path: path.End(),
Width: 3,
Width: float32(gtx.Dp(unit.Dp(3))),
}.Op())
}(float32(startX+(spacing*i)), 10)
}(startX+(spacing*i), offset)
}
{ // Draw Circle
func(x, y, r float32) {
Expand All @@ -280,11 +285,10 @@ func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isAc
path.Arc(p1, p2, 2*math.Pi)
path.Close()

paint.FillShape(gtx.Ops, color.NRGBA{A: 0xff}, clip.Stroke{
Path: path.End(),
Width: 5,
}.Op())
}(float32(startX+(spacing*i)), float32(15+spacing*i), 4)
defer clip.Outline{Path: path.End()}.Op().Push(gtx.Ops).Pop()
paint.ColorOp{Color: color.NRGBA{A: 0xff}}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
}(startX+(spacing*i), offset+(spacing*i), 5)
}
}

Expand Down
43 changes: 29 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
)

const (
windowWidth = 940
windowHeight = 580
hudTimeout = 2.5
hudTimeout = 2.5
delta = 0.022
)

var (
windowWidth = 940
windowHeight = 580

profile string
f *os.File
err error
Expand All @@ -53,6 +55,7 @@ func main() {
app.Size(unit.Dp(windowWidth), unit.Dp(windowHeight)),
)
w.Perform(system.ActionCenter)
w.Perform(system.ActionMaximize)
if err := loop(w); err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -84,8 +87,9 @@ func loop(w *app.Window) error {
mouse := &Mouse{maxScrollY: unit.Dp(200)}
isDragging := false

var clothW int = windowWidth * 1.3
var clothH int = windowHeight * 0.4
var clothW int = int(float64(windowWidth) * 1.3)
var clothH int = int(float64(windowHeight) * 0.4)

cloth := NewCloth(clothW, clothH, 8, 0.99, defaultColor)
hud := NewHud()

Expand All @@ -101,8 +105,8 @@ func loop(w *app.Window) error {
start := hrtime.Now()
gtx := layout.NewContext(&ops, e)
hud.width = windowWidth
hud.btnSize = gtx.Dp(35)
hud.closeBtn = gtx.Dp(25)
hud.btnSize = gtx.Dp(unit.Dp(40))
hud.closeBtn = gtx.Dp(unit.Dp(25))

if hud.isActive {
if !panelInit.IsZero() {
Expand Down Expand Up @@ -138,23 +142,34 @@ func loop(w *app.Window) error {
mouse.increaseForce(deltaTime.Seconds())
}

resetAnimation := 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 {
if e.Name == key.NameSpace {
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)
resetAnimation()
}
}
if e.Name == key.NameEscape {
w.Perform(system.ActionClose)
}
}
}

// Reset cloth on window resize.
if e.Size.X != windowWidth || e.Size.Y != windowHeight {
windowWidth = e.Size.X
windowHeight = e.Size.Y
resetAnimation()
}
fillBackground(gtx, color.NRGBA{R: 0xf2, G: 0xf2, B: 0xf2, A: 0xff})

layout.Stack{}.Layout(gtx,
Expand Down Expand Up @@ -231,7 +246,7 @@ func loop(w *app.Window) error {
}
}

cloth.Update(gtx, mouse, hud, 0.022)
cloth.Update(gtx, mouse, hud, delta)
return layout.Dimensions{}
}),

Expand Down

0 comments on commit 8c00735

Please sign in to comment.