From 305acea285b9db21d5184a9830f147096f40e2d8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 8 Mar 2023 20:24:43 -0500 Subject: [PATCH] Use ezprof to profile --- elements/basic/scrollbar.go | 168 ++++++++++++++++++++++++++++++++++++ elements/basic/slider.go | 3 + examples/artist/main.go | 7 +- examples/button/main.go | 7 -- examples/piano/main.go | 5 -- examples/raycaster/wall.png | Bin 1520 -> 1487 bytes go.mod | 1 + go.sum | 2 + 8 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 elements/basic/scrollbar.go diff --git a/elements/basic/scrollbar.go b/elements/basic/scrollbar.go new file mode 100644 index 0000000..5681896 --- /dev/null +++ b/elements/basic/scrollbar.go @@ -0,0 +1,168 @@ +package basicElements + +import "image" +import "git.tebibyte.media/sashakoshka/tomo/input" +import "git.tebibyte.media/sashakoshka/tomo/theme" +import "git.tebibyte.media/sashakoshka/tomo/config" +import "git.tebibyte.media/sashakoshka/tomo/artist" +import "git.tebibyte.media/sashakoshka/tomo/elements/core" + +type ScrollBar struct { + *core.Core + core core.CoreControl + + vertical bool + enabled bool + dragging bool + dragOffset int + track image.Rectangle + bar image.Rectangle + + contentBounds image.Rectangle + viewportBounds image.Rectangle + + config config.Wrapped + theme theme.Wrapped + + onSlide func () + onRelease func () +} + +func NewScrollBar (vertical bool) (element *ScrollBar) { + element = &ScrollBar { + vertical: vertical, + enabled: true, + } + if vertical { + element.theme.Case = theme.C("basic", "scrollBarHorizontal") + } else { + element.theme.Case = theme.C("basic", "scrollBarVertical") + } + element.Core, element.core = core.NewCore(element.draw) + element.updateMinimumSize() + return +} + +func (element *ScrollBar) HandleMouseDown (x, y int, button input.Button) { + +} + +func (element *ScrollBar) HandleMouseUp (x, y int, button input.Button) { + +} + +func (element *ScrollBar) HandleMouseMove (x, y int) { + +} + +func (element *ScrollBar) HandleMouseScroll (x, y int, deltaX, deltaY float64) { + +} + +// SetEnabled sets whether or not the scroll bar can be interacted with. +func (element *ScrollBar) SetEnabled (enabled bool) { + if element.enabled == enabled { return } + element.enabled = enabled + element.redo() +} + +// Enabled returns whether or not the element is enabled. +func (element *ScrollBar) Enabled () (enabled bool) { + return element.enabled +} + +// SetBounds sets the content and viewport bounds of the scroll bar. +func (element *ScrollBar) SetBounds (content, viewport image.Rectangle) { + element.contentBounds = content + element.viewportBounds = viewport + element.redo() +} + +// SetTheme sets the element's theme. +func (element *ScrollBar) SetTheme (new theme.Theme) { + if new == element.theme.Theme { return } + element.theme.Theme = new + element.redo() +} + +// SetConfig sets the element's configuration. +func (element *ScrollBar) SetConfig (new config.Config) { + if new == element.config.Config { return } + element.config.Config = new + element.updateMinimumSize() + element.redo() +} + +func (element *ScrollBar) recalculate () { + if element.vertical { + element.recalculateVertical() + } else { + element.recalculateHorizontal() + } +} + +func (element *ScrollBar) recalculateVertical () { + bounds := element.Bounds() + padding := element.theme.Padding(theme.PatternGutter) + element.track = padding.Apply(bounds) + + contentBounds := element.contentBounds + viewportBounds := element.viewportBounds + if element.Enabled() { + element.bar.Min.X = element.track.Min.X + element.bar.Max.X = element.track.Max.X + + scale := float64(element.track.Dy()) / + float64(contentBounds.Dy()) + element.bar.Min.Y = int(float64(viewportBounds.Min.Y) * scale) + element.bar.Max.Y = int(float64(viewportBounds.Max.Y) * scale) + + element.bar.Min.Y += element.track.Min.Y + element.bar.Max.Y += element.track.Min.Y + } + + // if the handle is out of bounds, don't display it + if element.bar.Dy() >= element.track.Dy() { + element.bar = image.Rectangle { } + } +} + +func (element *ScrollBar) recalculateHorizontal () { + +} + +func (element *ScrollBar) updateMinimumSize () { + padding := element.theme.Padding(theme.PatternGutter) + if element.vertical { + element.core.SetMinimumSize ( + padding.Horizontal() + element.config.HandleWidth(), + padding.Vertical() + element.config.HandleWidth() * 2) + } else { + element.core.SetMinimumSize ( + padding.Horizontal() + element.config.HandleWidth() * 2, + padding.Vertical() + element.config.HandleWidth()) + } +} + +func (element *ScrollBar) redo () { + if element.core.HasImage () { + element.draw() + element.core.DamageAll() + } +} + +func (element *ScrollBar) draw () { + bounds := element.Bounds() + state := theme.State { + Disabled: !element.Enabled(), + Pressed: element.dragging, + } + artist.DrawBounds ( + element.core, + element.theme.Pattern(theme.PatternGutter, state), + bounds) + artist.DrawBounds ( + element.core, + element.theme.Pattern(theme.PatternHandle, state), + element.bar) +} diff --git a/elements/basic/slider.go b/elements/basic/slider.go index 78450cc..67b6f65 100644 --- a/elements/basic/slider.go +++ b/elements/basic/slider.go @@ -123,6 +123,9 @@ func (element *Slider) SetValue (value float64) { if element.value == value { return } element.value = value + if element.onRelease != nil { + element.onRelease() + } element.redo() } diff --git a/examples/artist/main.go b/examples/artist/main.go index 3b4517f..01c78f4 100644 --- a/examples/artist/main.go +++ b/examples/artist/main.go @@ -3,8 +3,7 @@ package main import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/elements/testing" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" -import _ "net/http/pprof" -import "net/http" +import _ "git.tebibyte.media/sashakoshka/ezprof/hook" func main () { tomo.Run(run) @@ -15,8 +14,4 @@ func run () { window.SetTitle("Draw Test") window.Adopt(testing.NewArtist()) window.OnClose(tomo.Stop) - window.Show() - go func () { - http.ListenAndServe("localhost:9090", nil) - } () } diff --git a/examples/button/main.go b/examples/button/main.go index ab1b9b4..7e79a74 100644 --- a/examples/button/main.go +++ b/examples/button/main.go @@ -3,8 +3,6 @@ package main import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo/elements/basic" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" -import _ "net/http/pprof" -import "net/http" func main () { tomo.Run(run) @@ -28,9 +26,4 @@ func run () { window.Adopt(button) window.OnClose(tomo.Stop) window.Show() - - // just some stuff for profiling, this is not needed for tomo - go func () { - http.ListenAndServe("localhost:9090", nil) - } () } diff --git a/examples/piano/main.go b/examples/piano/main.go index 350c602..9eadf0a 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -11,8 +11,6 @@ import "git.tebibyte.media/sashakoshka/tomo/layouts/basic" import "git.tebibyte.media/sashakoshka/tomo/elements/basic" import "git.tebibyte.media/sashakoshka/tomo/elements/fun/music" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" -import _ "net/http/pprof" -import "net/http" const sampleRate = 44100 const bufferSize = 256 @@ -149,9 +147,6 @@ func run () { piano.Focus() window.OnClose(tomo.Stop) window.Show() - go func () { - http.ListenAndServe("localhost:9090", nil) - } () } type Patch struct { diff --git a/examples/raycaster/wall.png b/examples/raycaster/wall.png index ce40abc09e53d212275daa3d3dbef54e328b3032..0d41d7af2c76220463c1883f79f6fb00a77e7560 100644 GIT binary patch delta 1469 zcmV;u1w#7p3(pIXB!8AkL_t(o!=;wJn(RmsL{I*dr0%&qDldqK3nmT*9uEL4cc!JP z%1n&X&VFFhMw=}uBO~L4{P7RJuLO?6F*7!8T%&Vc9e3xvE`I!Ya$XnD$CJ`x$H{`U zBCp9m|Md$Z3I;KgjHLI0i12(Ih{3*XC`w6jo)?(VUjvUBjDMQ!hXasRm?)WW_KSVn zczs@MTZ1YplU@^5g($>~);2^SGAM+zcaGygMY#IFcc+P<2qndggn*C7fvO4w7z!l_ zFf)mos2ei^Gv%6tsN@=xF(&{Ns0v9E5eQ0TVdfN|s<=CqiAphYpePD7TPo_dZ!~wR z3Ml%VloYCt3V(b&A7my)5fNxXWszAFf)woD5LHA3bH&_vK6W0@9W%jA$*84p25N@7 z^6}$=S~J{?jAX{7M~5snRgQgU#H0+W5>;W&U}k`VQ~|=wV9be{03aeHMOM+8Ga`s8 z`WS#P=H%Enq7u(4rUaF6ot>=W<9JY+v|KWa2xdlAQGdW3lc+#SNs3v?zBM8fH(R@| z`@gEF7+;?+io(Td?odKTpjvSF#&vZHa5MVV0T^RaS%@gE#<(V%I;KiLJOB9Q7q08# zXdB{+nSlsmin%f)s7O=}cc6%@HJvIELBA$# zZ@kVI^M4wc%KcLi=)IGbOR+f-6$HWDm~#R}RiWo~@tSMm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GS7>jpZ{iD zCx;(MQ_LMi5Le8NSc_VTh+^X0?g!Txlt5@9Rj3riS}6NX$pW@hOa5%z*4)%ERbmC> zoV26!Jb%B5bD%1y3Z@1EV_drvz*-YgK@hANvF58L=y{!-xwX0XJ0HiLN}!6&q!g+O zLm+p{MHLbv61M{p!?dAphzn-BV=9KIfPpvXc06eAhzc4)Ria;?e?S)P-oT*{T2rid zn)^0qEJTIY4FX~=)P(!(0l>0E5mXFyP!ysN1b^?oJLg78(!JBXQrMi(CcPb)_|`raBuEtf zxRH^VIU?Ijs2~Dp?Umk~szhGn{`m1g9e<;>j+LBzA3}zu@Gw;QFjT{jl&iH`o0(=sVt6Oh!$f4m3U4P*{mus7cz@+C2qpU zhcahDC93{3RR$5 zw5A+4r4r^0wz`Vhu3>5v$yc1O!)?nm=46gwt-=N9O&GI?*zA8TL7E96irbcFr=TKN zFehXN%^|XwBQO=zk`%>Ewm}mzl4}Ob6;UN(OXn{97qrtU%hsbcIsgCw07*qoM6N<$ Ef`9nNZvX%Q diff --git a/go.mod b/go.mod index 9610c45..81f07ff 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.tebibyte.media/sashakoshka/tomo go 1.19 require ( + git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309011813-4cd4374e3830 github.com/faiface/beep v1.1.0 github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66 golang.org/x/image v0.3.0 diff --git a/go.sum b/go.sum index a6a4ecf..b91d013 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309011813-4cd4374e3830 h1:McIAkTzD4y0tS7YprTOwRu8a8NTsMKtomQnvxnCdOmg= +git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309011813-4cd4374e3830/go.mod h1:cpXX8SAUDEvZX5m7scoyruavUhEqQ1SByfWzPFHkTbg= github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA= github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ= github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 h1:lTG4HQym5oPKjL7nGs+csTgiDna685ZXjxijkne828g=