From 1cd53a4e6b8b7393357a9bc82753f01897821dcb Mon Sep 17 00:00:00 2001 From: Thomas Friedel Date: Mon, 28 May 2018 16:04:13 +0200 Subject: [PATCH] added a SetBounds function to replace the SetSize function --- canvas.go | 20 ++++++++++++++------ examples/android/android.go | 3 +-- examples/drawing/drawing.go | 2 +- examples/events/events.go | 2 +- examples/glfw/glfw.go | 2 +- examples/sdl/sdl.go | 2 +- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/canvas.go b/canvas.go index ef5bdf3..355bef1 100644 --- a/canvas.go +++ b/canvas.go @@ -96,12 +96,8 @@ const ( // the origin, since GL uses the bottom left coordinate, the // coordinates given here also use the bottom left as origin func New(x, y, w, h int) *Canvas { - cv := &Canvas{ - x: x, y: y, w: w, h: h, - fx: float64(x), fy: float64(y), - fw: float64(w), fh: float64(h), - stateStack: make([]drawState, 0, 20), - } + cv := &Canvas{stateStack: make([]drawState, 0, 20)} + cv.SetBounds(x, y, w, h) cv.state.lineWidth = 1 cv.state.globalAlpha = 1 cv.state.fill.color = glColor{a: 1} @@ -112,12 +108,24 @@ func New(x, y, w, h int) *Canvas { // SetSize changes the internal size of the canvas. This would // usually be called for example when the window is resized +// +// Deprecated: Use SetBounds instead func (cv *Canvas) SetSize(w, h int) { cv.w, cv.h = w, h cv.fw, cv.fh = float64(w), float64(h) activeCanvas = nil } +// SetBounds updates the bounds of the canvas. This would +// usually be called for example when the window is resized +func (cv *Canvas) SetBounds(x, y, w, h int) { + cv.x, cv.y = x, y + cv.w, cv.h = w, h + cv.fx, cv.fy = float64(x), float64(y) + cv.fw, cv.fh = float64(w), float64(h) + activeCanvas = nil +} + // Width returns the internal width of the canvas func (cv *Canvas) Width() int { return cv.w } diff --git a/examples/android/android.go b/examples/android/android.go index 510df0e..93ccf4a 100644 --- a/examples/android/android.go +++ b/examples/android/android.go @@ -24,8 +24,7 @@ func OnSurfaceChanged(w, h int) { time.Sleep(100 * time.Millisecond) panic(err) } - cv = canvas.New(0, 0, 0, 0) - cv.SetSize(w, h) + cv = canvas.New(0, 0, w, h) } func OnDrawFrame() { diff --git a/examples/drawing/drawing.go b/examples/drawing/drawing.go index 1edca58..4f36f7a 100644 --- a/examples/drawing/drawing.go +++ b/examples/drawing/drawing.go @@ -28,7 +28,7 @@ func main() { rg.AddColorStop(0.5, "#0000ff") wnd.SizeChange = func(w, h int) { - cv.SetSize(w, h) + cv.SetBounds(0, 0, w, h) } wnd.MainLoop(func() { diff --git a/examples/events/events.go b/examples/events/events.go index ead2d0a..9e08df3 100644 --- a/examples/events/events.go +++ b/examples/events/events.go @@ -44,7 +44,7 @@ func main() { } } wnd.SizeChange = func(w, h int) { - cv.SetSize(w, h) + cv.SetBounds(0, 0, w, h) } lastTime := time.Now() diff --git a/examples/glfw/glfw.go b/examples/glfw/glfw.go index 06967a8..2044042 100644 --- a/examples/glfw/glfw.go +++ b/examples/glfw/glfw.go @@ -60,7 +60,7 @@ func main() { // set canvas size ww, wh := window.GetSize() - cv.SetSize(ww, wh) + cv.SetBounds(0, 0, ww, wh) // call the run function to do all the drawing run(cv, float64(ww), float64(wh)) diff --git a/examples/sdl/sdl.go b/examples/sdl/sdl.go index 5974d06..759b343 100644 --- a/examples/sdl/sdl.go +++ b/examples/sdl/sdl.go @@ -101,7 +101,7 @@ func main() { // set canvas size ww, wh := window.GetSize() - cv.SetSize(int(ww), int(wh)) + cv.SetBounds(0, 0, int(ww), int(wh)) // call the run function to do all the drawing run(cv, float64(ww), float64(wh))