Skip to content

Commit

Permalink
Small code cleanup & adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Oct 17, 2023
1 parent eb6b6eb commit 31b3710
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions easing.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (e *Easing) Update(gtx layout.Context, isActive bool) float64 {
return e.Progress()
}

// InOutBack is the easing function used for the HUD position update.
func (e *Easing) InOutBack(t float64) float64 {
// Animate applies the In-Out-Back easing formula to a specific event.
func (e *Easing) Animate(t float64) float64 {
s := 1.70158
t *= 2
if t < 1 {
Expand Down
28 changes: 14 additions & 14 deletions hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Hud struct {
hudTag struct{}
sliders map[int]*slider
slide *Easing
hover *Easing
ctrlBtn *Easing
list layout.List
width int
height int
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewHud() *Hud {
hud.debug = widget.Bool{}
hud.debug.Value = false
hud.slide = slide
hud.hover = hover
hud.ctrlBtn = hover

return &hud
}
Expand All @@ -88,18 +88,18 @@ func (h *Hud) addSlider(index int, s slider) {
h.sliders[index] = &s
}

// ShowHideControls is responsible for showing or hiding the HUD control elements.
// ShowHideControlsArea is responsible for showing or hiding the HUD control elements.
// After hovering the mouse over the bottom part of the window a certain amount of time
// it shows the HUD control by invoking an easing function.
func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse, isActive bool) {
func (h *Hud) ShowHideControlsArea(gtx layout.Context, th *material.Theme, m *Mouse, isActive bool) {
if h.reset.Pressed() {
for _, s := range h.sliders {
s.widget.Value = s.value
}
}

progress := h.slide.Update(gtx, isActive)
pos := h.slide.InOutBack(progress) * float64(h.height)
pos := h.slide.Animate(progress) * float64(h.height)

// This offset will apply to the rest of the content laid out in this function.
defer op.Offset(image.Pt(0, gtx.Constraints.Max.Y+h.closeBtn-int(pos))).Push(gtx.Ops).Pop()
Expand All @@ -121,12 +121,12 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,

// Push this offset, but prepare to pop it after the button is drawn.
closeOffStack := op.Offset(image.Pt(10, -h.closeBtn)).Push(gtx.Ops)
paint.FillShape(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
paint.FillShape(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 170},
clip.Rect{Max: image.Pt(h.closeBtn, h.closeBtn)}.Op(),
)

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

var path clip.Path
path.Begin(gtx.Ops)
Expand All @@ -137,7 +137,7 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,

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

Expand Down Expand Up @@ -171,7 +171,7 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,
}

defer clip.Rect(r).Push(gtx.Ops).Pop()
paint.Fill(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 127})
paint.Fill(gtx.Ops, color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 170})
pointer.InputOp{
Tag: &h.hudTag,
Types: pointer.Scroll | pointer.Move | pointer.Press | pointer.Drag | pointer.Release | pointer.Leave,
Expand Down Expand Up @@ -234,7 +234,7 @@ func (h *Hud) ShowHideControls(gtx layout.Context, th *material.Theme, m *Mouse,
// DrawCtrlBtn draws the button which activates the main HUD area with the sliders.
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)
pos := h.slide.Animate(progress) * float64(h.height)
offset := gtx.Dp(unit.Dp(60))

offStack := op.Offset(image.Pt(0, gtx.Constraints.Max.Y-offset+int(pos))).Push(gtx.Ops)
Expand All @@ -248,13 +248,13 @@ 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(unit.Dp(2))
progress := h.ctrlBtn.Update(gtx, isActive || h.activator.Hovered())
btnWidth := h.ctrlBtn.Animate(progress) * float64(unit.Dp(3))

var path clip.Path

offset := float32(unit.Dp(10))
btnSize := float32(unit.Dp(h.btnSize))
btnSize := float32(h.btnSize)
spacing := btnSize / 4
startX := btnSize/2 - spacing

Expand Down Expand Up @@ -296,7 +296,7 @@ func (h *Hud) DrawCtrlBtn(gtx layout.Context, th *material.Theme, m *Mouse, isAc
Path: clip.UniformRRect(image.Rectangle{
Max: image.Pt(h.btnSize, h.btnSize),
}, gtx.Dp(10)).Path(gtx.Ops),
Width: 1.5 + float32(width),
Width: 1.5 + float32(btnWidth),
}.Op().Push(gtx.Ops).Pop()

pointer.CursorPointer.Add(gtx.Ops)
Expand Down
12 changes: 4 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func loop(w *app.Window) error {

if hud.isActive {
if !panelInit.IsZero() {
dt := time.Now().Sub(panelInit).Seconds()
dt := time.Since(panelInit).Seconds()
if dt > hudTimeout {
hud.isActive = false
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func loop(w *app.Window) error {
}.Add(gtx.Ops)

if mouse.getLeftButton() {
deltaTime = time.Now().Sub(initTime)
deltaTime = time.Since(initTime)
mouse.increaseForce(deltaTime.Seconds())
}

Expand Down Expand Up @@ -276,13 +276,9 @@ func loop(w *app.Window) error {
}
}
}

hud.ShowHideControls(gtx, th, mouse, true)
hud.DrawCtrlBtn(gtx, th, mouse, true)
} else {
hud.DrawCtrlBtn(gtx, th, mouse, false)
hud.ShowHideControls(gtx, th, mouse, false)
}
hud.DrawCtrlBtn(gtx, th, mouse, hud.isActive)
hud.ShowHideControlsArea(gtx, th, mouse, hud.isActive)

return layout.Dimensions{}
}),
Expand Down

0 comments on commit 31b3710

Please sign in to comment.