From 10415d417abfab1035c9b19f53f9f29f372b1860 Mon Sep 17 00:00:00 2001 From: Artem Yadelskyi Date: Thu, 3 Nov 2022 08:33:09 +0200 Subject: [PATCH] all: replace `interface{}` with `any` (#2430) Closes #2429 --- .../workflows/vettools/imageimportcheck.go | 2 +- examples/drag/main.go | 6 ++-- examples/keyboard/keyboard/gen.go | 2 +- examples/shader/main.go | 2 +- genevents.go | 2 +- image.go | 8 ++--- internal/debug/debug_ebitenginedebug.go | 2 +- internal/debug/debug_notebitenginedebug.go | 2 +- internal/gamepad/gamepad.go | 2 +- internal/glfwwin/api_windows.go | 2 +- internal/graphicscommand/image_js.go | 4 +-- .../metal/mtl/example_darwin_test.go | 2 +- .../graphicsdriver/opengl/gl/conversions.go | 2 +- internal/processtest/testdata/issue1753.go | 4 +-- internal/ui/shader.go | 2 +- internal/ui/ui_js.go | 36 +++++++++---------- shader.go | 2 +- shader_test.go | 20 +++++------ 18 files changed, 51 insertions(+), 51 deletions(-) diff --git a/.github/workflows/vettools/imageimportcheck.go b/.github/workflows/vettools/imageimportcheck.go index cc8398e65..7ec40cb9f 100644 --- a/.github/workflows/vettools/imageimportcheck.go +++ b/.github/workflows/vettools/imageimportcheck.go @@ -43,7 +43,7 @@ type imageImportCheckError struct { Import string } -func runImageImportCheck(pass *analysis.Pass) (interface{}, error) { +func runImageImportCheck(pass *analysis.Pass) (any, error) { pkgPath := pass.Pkg.Path() if strings.HasPrefix(pkgPath, "github.com/hajimehoshi/ebiten/v2/examples/") { return nil, nil diff --git a/examples/drag/main.go b/examples/drag/main.go index c37548885..a306b2b7a 100644 --- a/examples/drag/main.go +++ b/examples/drag/main.go @@ -133,7 +133,7 @@ type Stroke struct { // draggingObject represents a object (sprite in this case) // that is being dragged. - draggingObject interface{} + draggingObject any } func NewStroke(source StrokeSource) *Stroke { @@ -174,11 +174,11 @@ func (s *Stroke) PositionDiff() (int, int) { return dx, dy } -func (s *Stroke) DraggingObject() interface{} { +func (s *Stroke) DraggingObject() any { return s.draggingObject } -func (s *Stroke) SetDraggingObject(object interface{}) { +func (s *Stroke) SetDraggingObject(object any) { s.draggingObject = object } diff --git a/examples/keyboard/keyboard/gen.go b/examples/keyboard/keyboard/gen.go index 15091de97..ee1c5e8a0 100644 --- a/examples/keyboard/keyboard/gen.go +++ b/examples/keyboard/keyboard/gen.go @@ -295,7 +295,7 @@ func outputKeyRectsGo(k map[ebiten.Key]image.Rectangle) error { if err != nil { return err } - return tmpl.Execute(f, map[string]interface{}{ + return tmpl.Execute(f, map[string]any{ "License": license, "KeyRectsMap": k, }) diff --git a/examples/shader/main.go b/examples/shader/main.go index bdb5d0564..017b1b0ad 100644 --- a/examples/shader/main.go +++ b/examples/shader/main.go @@ -147,7 +147,7 @@ func (g *Game) Draw(screen *ebiten.Image) { cx, cy := ebiten.CursorPosition() op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Time": float32(g.time) / 60, "Cursor": []float32{float32(cx), float32(cy)}, "ScreenSize": []float32{float32(w), float32(h)}, diff --git a/genevents.go b/genevents.go index 2b5252d84..98fcbb507 100644 --- a/genevents.go +++ b/genevents.go @@ -325,7 +325,7 @@ var eventTmpl = template.Must(template.New("event.go").Parse(`{{.License}} package {{.Package}} -type Event interface{} +type Event any {{range .Events}}// {{.Comment}} type {{.Name}} struct { diff --git a/image.go b/image.go index a6210c2fa..3365c7a09 100644 --- a/image.go +++ b/image.go @@ -243,7 +243,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { var body [16]float32 var translation [4]float32 colorm.Elements(body[:], translation[:]) - uniforms = shader.convertUniforms(map[string]interface{}{ + uniforms = shader.convertUniforms(map[string]any{ builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMTranslation: translation[:], }) @@ -474,7 +474,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o var body [16]float32 var translation [4]float32 colorm.Elements(body[:], translation[:]) - uniforms = shader.convertUniforms(map[string]interface{}{ + uniforms = shader.convertUniforms(map[string]any{ builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMTranslation: translation[:], }) @@ -502,7 +502,7 @@ type DrawTrianglesShaderOptions struct { // If the uniform variable type is an array, a vector or a matrix, // you have to specify linearly flattened values as a slice. // For example, if the uniform variable type is [4]vec4, the number of the slice values will be 16. - Uniforms map[string]interface{} + Uniforms map[string]any // Images is a set of the source images. // All the images' sizes must be the same. @@ -662,7 +662,7 @@ type DrawRectShaderOptions struct { // If the uniform variable type is an array, a vector or a matrix, // you have to specify linearly flattened values as a slice. // For example, if the uniform variable type is [4]vec4, the number of the slice values will be 16. - Uniforms map[string]interface{} + Uniforms map[string]any // Images is a set of the source images. // All the images' sizes must be the same. diff --git a/internal/debug/debug_ebitenginedebug.go b/internal/debug/debug_ebitenginedebug.go index d82afb8c0..10a8ac910 100644 --- a/internal/debug/debug_ebitenginedebug.go +++ b/internal/debug/debug_ebitenginedebug.go @@ -22,6 +22,6 @@ import ( const IsDebug = true -func Logf(format string, args ...interface{}) { +func Logf(format string, args ...any) { fmt.Printf(format, args...) } diff --git a/internal/debug/debug_notebitenginedebug.go b/internal/debug/debug_notebitenginedebug.go index 86259fc9e..0da084bbf 100644 --- a/internal/debug/debug_notebitenginedebug.go +++ b/internal/debug/debug_notebitenginedebug.go @@ -18,5 +18,5 @@ package debug const IsDebug = false -func Logf(format string, args ...interface{}) { +func Logf(format string, args ...any) { } diff --git a/internal/gamepad/gamepad.go b/internal/gamepad/gamepad.go index 00dd019fb..fb1b451a2 100644 --- a/internal/gamepad/gamepad.go +++ b/internal/gamepad/gamepad.go @@ -173,7 +173,7 @@ func (g *gamepads) setNativeWindow(nativeWindow uintptr) { g.m.Lock() defer g.m.Unlock() - var n interface{} = g.native + var n any = g.native if n, ok := n.(interface{ setNativeWindow(uintptr) }); ok { n.setNativeWindow(nativeWindow) } diff --git a/internal/glfwwin/api_windows.go b/internal/glfwwin/api_windows.go index af98cd1bb..ec6e79453 100644 --- a/internal/glfwwin/api_windows.go +++ b/internal/glfwwin/api_windows.go @@ -1268,7 +1268,7 @@ func _GetMessageTime() int32 { return int32(r) } -func _GetModuleHandleExW(dwFlags uint32, lpModuleName interface{}) (_HMODULE, error) { +func _GetModuleHandleExW(dwFlags uint32, lpModuleName any) (_HMODULE, error) { var ptr unsafe.Pointer switch moduleName := lpModuleName.(type) { case string: diff --git a/internal/graphicscommand/image_js.go b/internal/graphicscommand/image_js.go index 0122a8111..d3810a495 100644 --- a/internal/graphicscommand/image_js.go +++ b/internal/graphicscommand/image_js.go @@ -31,8 +31,8 @@ func download(buf *bytes.Buffer, mime string, path string) { a := global.Get("document").Call("createElement", "a") blob := global.Get("Blob").New( - []interface{}{jsData}, - map[string]interface{}{"type": mime}, + []any{jsData}, + map[string]any{"type": mime}, ) a.Set("href", global.Get("URL").Call("createObjectURL", blob)) a.Set("download", path) diff --git a/internal/graphicsdriver/metal/mtl/example_darwin_test.go b/internal/graphicsdriver/metal/mtl/example_darwin_test.go index 6a0d77b14..f96d70b25 100644 --- a/internal/graphicsdriver/metal/mtl/example_darwin_test.go +++ b/internal/graphicsdriver/metal/mtl/example_darwin_test.go @@ -86,7 +86,7 @@ func Example_listDevices() { // printJSON prints label, then v as JSON encoded with indent to stdout. It panics on any error. // It's meant to be used by examples to print the output. -func printJSON(label string, v interface{}) { +func printJSON(label string, v any) { fmt.Print(label) w := json.NewEncoder(os.Stdout) w.SetIndent("", "\t") diff --git a/internal/graphicsdriver/opengl/gl/conversions.go b/internal/graphicsdriver/opengl/gl/conversions.go index 9bdb1755b..e9c1a5f74 100644 --- a/internal/graphicsdriver/opengl/gl/conversions.go +++ b/internal/graphicsdriver/opengl/gl/conversions.go @@ -17,7 +17,7 @@ import ( // var data []uint8 // ... // gl.TexImage2D(glconst.TEXTURE_2D, ..., glconst.UNSIGNED_BYTE, gl.Ptr(&data[0])) -func Ptr(data interface{}) unsafe.Pointer { +func Ptr(data any) unsafe.Pointer { if data == nil { return unsafe.Pointer(nil) } diff --git a/internal/processtest/testdata/issue1753.go b/internal/processtest/testdata/issue1753.go index a1ca668a4..3e702df2a 100644 --- a/internal/processtest/testdata/issue1753.go +++ b/internal/processtest/testdata/issue1753.go @@ -48,7 +48,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { op := &ebiten.DrawRectShaderOptions{} op.Blend = ebiten.BlendCopy - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Color": []float32{1, 1, 1, 1}, } g.dst.DrawRectShader(w, h, s, op) @@ -78,7 +78,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { op := &ebiten.DrawRectShaderOptions{} op.Blend = ebiten.BlendCopy - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Dummy": float32(0), "R": float32(0.5), "G": float32(1), diff --git a/internal/ui/shader.go b/internal/ui/shader.go index 5aedb08a8..af3222597 100644 --- a/internal/ui/shader.go +++ b/internal/ui/shader.go @@ -44,7 +44,7 @@ func (s *Shader) MarkDisposed() { s.shader = nil } -func (s *Shader) ConvertUniforms(uniforms map[string]interface{}) [][]float32 { +func (s *Shader) ConvertUniforms(uniforms map[string]any) [][]float32 { nameToF32s := map[string][]float32{} for name, v := range uniforms { switch v := v.(type) { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 2021381fd..c6b47a586 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -359,7 +359,7 @@ func (u *userInterfaceImpl) loop(game Game) <-chan error { } // TODO: Should cf be released after the game ends? - cf = js.FuncOf(func(this js.Value, args []js.Value) interface{} { + cf = js.FuncOf(func(this js.Value, args []js.Value) any { // f can be blocked but callbacks must not be blocked. Create a goroutine (#1161). go f() return nil @@ -419,7 +419,7 @@ func init() { if !document.Get("body").Truthy() { ch := make(chan struct{}) - window.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + window.Call("addEventListener", "load", js.FuncOf(func(this js.Value, args []js.Value) any { close(ch) return nil })) @@ -465,7 +465,7 @@ func init() { setCanvasEventHandlers(canvas) // Pointer Lock - document.Call("addEventListener", "pointerlockchange", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + document.Call("addEventListener", "pointerlockchange", js.FuncOf(func(this js.Value, args []js.Value) any { if document.Get("pointerLockElement").Truthy() { return nil } @@ -478,22 +478,22 @@ func init() { theUI.input.recoverCursorPosition() return nil })) - document.Call("addEventListener", "pointerlockerror", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + document.Call("addEventListener", "pointerlockerror", js.FuncOf(func(this js.Value, args []js.Value) any { js.Global().Get("console").Call("error", "pointerlockerror event is fired. 'sandbox=\"allow-pointer-lock\"' might be required at an iframe. This function on browsers must be called as a result of a gestural interaction or orientation change.") return nil })) - document.Call("addEventListener", "fullscreenerror", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + document.Call("addEventListener", "fullscreenerror", js.FuncOf(func(this js.Value, args []js.Value) any { js.Global().Get("console").Call("error", "fullscreenerror event is fired. 'allow=\"fullscreen\"' or 'allowfullscreen' might be required at an iframe. This function on browsers must be called as a result of a gestural interaction or orientation change.") return nil })) - document.Call("addEventListener", "webkitfullscreenerror", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + document.Call("addEventListener", "webkitfullscreenerror", js.FuncOf(func(this js.Value, args []js.Value) any { js.Global().Get("console").Call("error", "webkitfullscreenerror event is fired. 'allow=\"fullscreen\"' or 'allowfullscreen' might be required at an iframe. This function on browsers must be called as a result of a gestural interaction or orientation change.") return nil })) } func setWindowEventHandlers(v js.Value) { - v.Call("addEventListener", "resize", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "resize", js.FuncOf(func(this js.Value, args []js.Value) any { theUI.updateScreenSize() // updateImpl can block. Use goroutine. @@ -510,7 +510,7 @@ func setWindowEventHandlers(v js.Value) { func setCanvasEventHandlers(v js.Value) { // Keyboard - v.Call("addEventListener", "keydown", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "keydown", js.FuncOf(func(this js.Value, args []js.Value) any { // Focus the canvas explicitly to activate tha game (#961). v.Call("focus") @@ -522,7 +522,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "keyup", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "keyup", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -533,7 +533,7 @@ func setCanvasEventHandlers(v js.Value) { })) // Mouse - v.Call("addEventListener", "mousedown", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "mousedown", js.FuncOf(func(this js.Value, args []js.Value) any { // Focus the canvas explicitly to activate tha game (#961). v.Call("focus") @@ -545,7 +545,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "mouseup", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "mouseup", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -554,7 +554,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "mousemove", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "mousemove", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -563,7 +563,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "wheel", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "wheel", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -574,7 +574,7 @@ func setCanvasEventHandlers(v js.Value) { })) // Touch - v.Call("addEventListener", "touchstart", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "touchstart", js.FuncOf(func(this js.Value, args []js.Value) any { // Focus the canvas explicitly to activate tha game (#961). v.Call("focus") @@ -586,7 +586,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "touchend", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "touchend", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -595,7 +595,7 @@ func setCanvasEventHandlers(v js.Value) { } return nil })) - v.Call("addEventListener", "touchmove", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "touchmove", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") if err := theUI.input.updateFromEvent(e); err != nil && theUI.err != nil { @@ -606,14 +606,14 @@ func setCanvasEventHandlers(v js.Value) { })) // Context menu - v.Call("addEventListener", "contextmenu", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "contextmenu", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") return nil })) // Context - v.Call("addEventListener", "webglcontextlost", js.FuncOf(func(this js.Value, args []js.Value) interface{} { + v.Call("addEventListener", "webglcontextlost", js.FuncOf(func(this js.Value, args []js.Value) any { e := args[0] e.Call("preventDefault") window.Get("location").Call("reload") diff --git a/shader.go b/shader.go index 5871256ae..201c305fe 100644 --- a/shader.go +++ b/shader.go @@ -52,7 +52,7 @@ func (s *Shader) Dispose() { s.shader = nil } -func (s *Shader) convertUniforms(uniforms map[string]interface{}) [][]float32 { +func (s *Shader) convertUniforms(uniforms map[string]any) [][]float32 { return s.shader.ConvertUniforms(uniforms) } diff --git a/shader_test.go b/shader_test.go index ef6572ff0..d31f55dc6 100644 --- a/shader_test.go +++ b/shader_test.go @@ -499,7 +499,7 @@ func TestShaderUniformFirstElement(t *testing.T) { shaders := []struct { Name string Shader string - Uniforms map[string]interface{} + Uniforms map[string]any }{ { Name: "float array", @@ -510,7 +510,7 @@ var C [2]float func Fragment(position vec4, texCoord vec2, color vec4) vec4 { return vec4(C[0], 1, 1, 1) }`, - Uniforms: map[string]interface{}{ + Uniforms: map[string]any{ "C": []float32{1, 1}, }, }, @@ -523,7 +523,7 @@ var C [1]float func Fragment(position vec4, texCoord vec2, color vec4) vec4 { return vec4(C[0], 1, 1, 1) }`, - Uniforms: map[string]interface{}{ + Uniforms: map[string]any{ "C": []float32{1}, }, }, @@ -536,7 +536,7 @@ var C [2]mat2 func Fragment(position vec4, texCoord vec2, color vec4) vec4 { return vec4(C[0][0][0], 1, 1, 1) }`, - Uniforms: map[string]interface{}{ + Uniforms: map[string]any{ "C": []float32{1, 0, 0, 0, 0, 0, 0, 0}, }, }, @@ -745,7 +745,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat2": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, @@ -783,7 +783,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat2": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, @@ -823,7 +823,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat3": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, 5.0 / 256.0, 6.0 / 256.0, @@ -862,7 +862,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat3": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, 5.0 / 256.0, 6.0 / 256.0, @@ -904,7 +904,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat4": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, 5.0 / 256.0, 6.0 / 256.0, 7.0 / 256.0, 8.0 / 256.0, @@ -944,7 +944,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } op := &ebiten.DrawRectShaderOptions{} - op.Uniforms = map[string]interface{}{ + op.Uniforms = map[string]any{ "Mat4": []float32{ 1.0 / 256.0, 2.0 / 256.0, 3.0 / 256.0, 4.0 / 256.0, 5.0 / 256.0, 6.0 / 256.0, 7.0 / 256.0, 8.0 / 256.0,