added gmath dependency

This commit is contained in:
milarin 2023-01-22 12:40:56 +01:00
parent b832628da8
commit 3341553234
5 changed files with 11 additions and 24 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module git.milar.in/milarin/gui
go 1.18
require (
git.milar.in/milarin/gmath v0.0.3
github.com/hajimehoshi/ebiten v1.12.12
github.com/hajimehoshi/ebiten/v2 v2.3.1
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9

2
go.sum
View File

@ -1,3 +1,5 @@
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20220320163800-277f93cfa958 h1:TL70PMkdPCt9cRhKTqsm+giRpgrd0IGEj763nNr2VFY=

View File

@ -1,23 +1,5 @@
package views
func min[T int | float64](x, y T) T {
if x < y {
return x
}
return y
}
func max[T int | float64](x, y T) T {
if x > y {
return x
}
return y
}
func limit[T int | float64](v, minv, maxv T) T {
return min(max(v, minv), maxv)
}
func iff[T any](condition bool, trueValue, falseValue T) T {
if condition {
return trueValue

View File

@ -1,6 +1,7 @@
package views
import (
"git.milar.in/milarin/gmath"
"git.milar.in/milarin/gui"
)
@ -32,13 +33,13 @@ func (v *ConstrainView) Layout(ctx gui.AppContext) (prefWidth, prefHeight int) {
vw, vh := v.View().Layout(ctx)
if v.MaxWidth >= 0 {
prefWidth = iff(vw >= 0, min(vw, v.MaxWidth), v.MaxWidth)
prefWidth = iff(vw >= 0, gmath.Min(vw, v.MaxWidth), v.MaxWidth)
} else {
prefWidth = vw
}
if v.MaxHeight >= 0 {
prefHeight = iff(vh >= 0, min(vh, v.MaxHeight), v.MaxHeight)
prefHeight = iff(vh >= 0, gmath.Min(vh, v.MaxHeight), v.MaxHeight)
} else {
prefHeight = vh
}

View File

@ -5,6 +5,7 @@ import (
"math"
"time"
"git.milar.in/milarin/gmath"
"git.milar.in/milarin/gui"
"github.com/hajimehoshi/ebiten/v2"
)
@ -130,10 +131,10 @@ func (v *ScrollView) Draw(img *gui.Image, ctx gui.AppContext) {
func (v *ScrollView) limit() {
if v.buf != nil {
v.targetOffset.Y = limit(v.targetOffset.Y, 0, max(v.buf.Bounds().Dy()-v.viewportHeight, 0))
v.targetOffset.X = limit(v.targetOffset.X, 0, max(v.buf.Bounds().Dx()-v.viewportWidth, 0))
v.VerticalScrollOffset = limit(v.VerticalScrollOffset, 0, float64(max(v.buf.Bounds().Dy()-v.viewportHeight, 0)))
v.HorizontalScrollOffset = limit(v.HorizontalScrollOffset, 0, float64(max(v.buf.Bounds().Dx()-v.viewportWidth, 0)))
v.targetOffset.Y = gmath.Clamp(v.targetOffset.Y, 0, gmath.Max(v.buf.Bounds().Dy()-v.viewportHeight, 0))
v.targetOffset.X = gmath.Clamp(v.targetOffset.X, 0, gmath.Max(v.buf.Bounds().Dx()-v.viewportWidth, 0))
v.VerticalScrollOffset = gmath.Clamp(v.VerticalScrollOffset, 0, float64(gmath.Max(v.buf.Bounds().Dy()-v.viewportHeight, 0)))
v.HorizontalScrollOffset = gmath.Clamp(v.HorizontalScrollOffset, 0, float64(gmath.Max(v.buf.Bounds().Dx()-v.viewportWidth, 0)))
}
}