From 33415532340a4325881abff674fe43dd8672e17e Mon Sep 17 00:00:00 2001 From: milarin Date: Sun, 22 Jan 2023 12:40:56 +0100 Subject: [PATCH] added gmath dependency --- go.mod | 1 + go.sum | 2 ++ views/utils.go | 18 ------------------ views/view_constrain.go | 5 +++-- views/view_scroll.go | 9 +++++---- 5 files changed, 11 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index cc688b8..b6aece8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e62f30a..b60724f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/views/utils.go b/views/utils.go index 6c0cffb..ae1e744 100644 --- a/views/utils.go +++ b/views/utils.go @@ -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 diff --git a/views/view_constrain.go b/views/view_constrain.go index e23a6d9..7adf2bc 100644 --- a/views/view_constrain.go +++ b/views/view_constrain.go @@ -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 } diff --git a/views/view_scroll.go b/views/view_scroll.go index 9242777..da686e5 100644 --- a/views/view_scroll.go +++ b/views/view_scroll.go @@ -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))) } }