Skip to content

Commit

Permalink
Some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Nov 11, 2023
1 parent 3d201d5 commit 342812d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
17 changes: 8 additions & 9 deletions cloth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gioui.org/op/paint"
)

const lineWidth = 0.8
const lineWidth = 0.6

type Cloth struct {
constraints []*Constraint
Expand All @@ -25,13 +25,12 @@ type Cloth struct {

// NewCloth creates a new cloth which dimension is calculated based on
// the application window width and height and the spacing between the sticks.
func NewCloth(width, height, spacing int, friction float64, col color.NRGBA) *Cloth {
func NewCloth(width, height, spacing int, col color.NRGBA) *Cloth {
return &Cloth{
width: width,
height: height,
spacing: spacing,
friction: friction,
color: col,
width: width,
height: height,
spacing: spacing,
color: col,
}
}

Expand Down Expand Up @@ -67,7 +66,7 @@ func (c *Cloth) Init(posX, posY int, hud *Hud) {
c.constraints = append(c.constraints, constraint)
}

pinX := x % (clothX / 9)
pinX := x % (clothX / 10)
if y == 0 && pinX == 0 {
particle.pinX = true
}
Expand All @@ -81,7 +80,7 @@ func (c *Cloth) Init(posX, posY int, hud *Hud) {
// Update updates the cloth particles invoked on each frame event of the Gio internal window calls.
// The cloth contraints are solved by using the Verlet integration formulas.
func (cloth *Cloth) Update(gtx layout.Context, mouse *Mouse, hud *Hud, dt float64) {
dragForce := float32(mouse.getForce() * 0.25)
dragForce := float32(mouse.getForce() * 0.1)
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()
Expand Down
6 changes: 3 additions & 3 deletions hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewHud() *Hud {

sliders := []slider{
{title: "Drag force", min: 1.1, value: 2, max: 15},
{title: "Gravity force", min: 100, value: 250, max: 500},
{title: "Gravity", min: 100, value: 250, max: 500},
{title: "Elasticity", min: 10, value: 30, max: 50},
{title: "Stiffness", min: 0.95, value: 0.98, max: 0.99},
{title: "Tear distance", min: 5, value: 20, max: 80},
Expand All @@ -87,8 +87,8 @@ func NewHud() *Hud {
{"F1": "Toggle the quick help panel"},
{"Space": "Redraw the cloth"},
{"Right click": "Tear the cloth at mouse position"},
{"Click & hold": "Increase the mouse pressure"},
{"Scroll Up/Down": "Change the mouse focus area"},
{"Click & hold": "Increase cloth destruction"},
{"Scroll Up/Down": "Increase/decrease cloth destruction area"},
{"Ctrl+click": "Pin the cloth particle at mouse position"},
}

Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
windowSizeX = 1280
windowSizeY = 820

defaultWindowWidth = 940
defaultWindowHeigth = 580
defaultWindowWidth = 1024
defaultWindowHeigth = 640
)

var (
Expand All @@ -49,6 +49,8 @@ var (
clothW int
clothH int

clothSpacing = 6

// Gio Ops related variables
ops op.Ops
initTime time.Time
Expand Down Expand Up @@ -132,11 +134,17 @@ func loop(w *app.Window) error {
pprof.StartCPUProfile(file)
}

// Cloth is not initialized yet. Initialize it.
// Cloth is not initialized yet.
if cloth == nil {
clothW = gtx.Dp(unit.Dp(windowWidth))
clothH = gtx.Dp(unit.Dp(windowHeight) * 0.33)
cloth = NewCloth(clothW, clothH, 12, 0.98, defaultColor)
clothSpacing = func() int { // different cloth spacing for hi-res devices.
if clothW <= windowWidth {
return clothSpacing
}
return 2 * clothSpacing
}()
cloth = NewCloth(clothW, clothH, clothSpacing, defaultColor)

width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y
Expand All @@ -152,6 +160,8 @@ func loop(w *app.Window) error {
Keys: key.NameEscape + "|" + key.NameCtrl + "|" + key.NameAlt + "|" + key.NameSpace + "|" + key.NameF1,
}.Add(gtx.Ops)

pointer.CursorPointer.Add(gtx.Ops)

if mouse.getLeftButton() {
deltaTime = time.Since(initTime)
mouse.setForce(deltaTime.Seconds() * 5)
Expand Down

0 comments on commit 342812d

Please sign in to comment.