internal/uidriver/glfw: Native macOS implementation for setting cursor shape (#1624)

Updates #1624
This commit is contained in:
Tom Lister 2021-05-02 15:50:50 +10:00 committed by GitHub
parent 7adf6aac27
commit bea5ab3335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 65 additions and 10 deletions

View File

@ -35,4 +35,6 @@ const (
CursorShapeText CursorShapeType = CursorShapeType(driver.CursorShapeText) CursorShapeText CursorShapeType = CursorShapeType(driver.CursorShapeText)
CursorShapeCrosshair CursorShapeType = CursorShapeType(driver.CursorShapeCrosshair) CursorShapeCrosshair CursorShapeType = CursorShapeType(driver.CursorShapeCrosshair)
CursorShapePointer CursorShapeType = CursorShapeType(driver.CursorShapePointer) CursorShapePointer CursorShapeType = CursorShapeType(driver.CursorShapePointer)
CursorShapeEWResize CursorShapeType = CursorShapeType(driver.CursorShapeEWResize)
CursorShapeNSResize CursorShapeType = CursorShapeType(driver.CursorShapeNSResize)
) )

View File

@ -61,6 +61,10 @@ func (g *Game) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrint(screen, "CursorShape: Crosshair") ebitenutil.DebugPrint(screen, "CursorShape: Crosshair")
case ebiten.CursorShapePointer: case ebiten.CursorShapePointer:
ebitenutil.DebugPrint(screen, "CursorShape: Pointer") ebitenutil.DebugPrint(screen, "CursorShape: Pointer")
case ebiten.CursorShapeEWResize:
ebitenutil.DebugPrint(screen, "CursorShape: EW Resize")
case ebiten.CursorShapeNSResize:
ebitenutil.DebugPrint(screen, "CursorShape: NS Resize")
} }
} }
@ -71,22 +75,21 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
func main() { func main() {
g := &Game{ g := &Game{
grids: map[image.Rectangle]ebiten.CursorShapeType{ grids: map[image.Rectangle]ebiten.CursorShapeType{
image.Rect(100, 100, 200, 300): ebiten.CursorShapeDefault, image.Rect(100, 100, 200, 200): ebiten.CursorShapeDefault,
image.Rect(200, 100, 300, 300): ebiten.CursorShapeText, image.Rect(200, 100, 300, 200): ebiten.CursorShapeText,
image.Rect(300, 100, 400, 300): ebiten.CursorShapeCrosshair, image.Rect(300, 100, 400, 200): ebiten.CursorShapeCrosshair,
image.Rect(400, 100, 500, 300): ebiten.CursorShapePointer, image.Rect(100, 200, 200, 300): ebiten.CursorShapePointer,
image.Rect(200, 200, 300, 300): ebiten.CursorShapeEWResize,
image.Rect(300, 200, 400, 300): ebiten.CursorShapeNSResize,
}, },
gridColors: map[image.Rectangle]color.Color{}, gridColors: map[image.Rectangle]color.Color{},
} }
for rect, c := range g.grids { for rect, c := range g.grids {
clr := color.RGBA{0x40, 0x40, 0x40, 0xff} clr := color.RGBA{0x40, 0x40, 0x40, 0xff}
switch c % 3 { if c%2 == 0 {
case 0:
clr.R = 0x80 clr.R = 0x80
case 1: } else {
clr.G = 0x80 clr.G = 0x80
case 2:
clr.B = 0x80
} }
g.gridColors[rect] = clr g.gridColors[rect] = clr
} }

View File

@ -29,4 +29,6 @@ const (
CursorShapeText CursorShapeText
CursorShapeCrosshair CursorShapeCrosshair
CursorShapePointer CursorShapePointer
CursorShapeEWResize
CursorShapeNSResize
) )

View File

@ -190,6 +190,8 @@ func initialize() error {
glfwSystemCursors[driver.CursorShapeText] = glfw.CreateStandardCursor(glfw.IBeamCursor) glfwSystemCursors[driver.CursorShapeText] = glfw.CreateStandardCursor(glfw.IBeamCursor)
glfwSystemCursors[driver.CursorShapeCrosshair] = glfw.CreateStandardCursor(glfw.CrosshairCursor) glfwSystemCursors[driver.CursorShapeCrosshair] = glfw.CreateStandardCursor(glfw.CrosshairCursor)
glfwSystemCursors[driver.CursorShapePointer] = glfw.CreateStandardCursor(glfw.HandCursor) glfwSystemCursors[driver.CursorShapePointer] = glfw.CreateStandardCursor(glfw.HandCursor)
glfwSystemCursors[driver.CursorShapeEWResize] = glfw.CreateStandardCursor(glfw.HResizeCursor)
glfwSystemCursors[driver.CursorShapeNSResize] = glfw.CreateStandardCursor(glfw.VResizeCursor)
return nil return nil
} }
@ -651,7 +653,7 @@ func (u *UserInterface) SetCursorShape(shape driver.CursorShape) {
return return
} }
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
u.window.SetCursor(glfwSystemCursors[shape]) u.setNativeCursor(shape)
return nil return nil
}) })
} }

View File

@ -43,9 +43,35 @@ package glfw
// return [[NSApplication sharedApplication] currentSystemPresentationOptions] & // return [[NSApplication sharedApplication] currentSystemPresentationOptions] &
// NSApplicationPresentationFullScreen; // NSApplicationPresentationFullScreen;
// } // }
//
// static void setNativeCursor(int cursorID) {
// id cursor = [[NSCursor class] performSelector:@selector(arrowCursor)];
// switch (cursorID) {
// case 0:
// cursor = [[NSCursor class] performSelector:@selector(arrowCursor)];
// break;
// case 1:
// cursor = [[NSCursor class] performSelector:@selector(IBeamCursor)];
// break;
// case 2:
// cursor = [[NSCursor class] performSelector:@selector(crosshairCursor)];
// break;
// case 3:
// cursor = [[NSCursor class] performSelector:@selector(pointingHandCursor)];
// break;
// case 4:
// cursor = [[NSCursor class] performSelector:@selector(_windowResizeEastWestCursor)];
// break;
// case 5:
// cursor = [[NSCursor class] performSelector:@selector(_windowResizeNorthSouthCursor)];
// break;
// }
// [cursor push];
// }
import "C" import "C"
import ( import (
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/glfw"
) )
@ -91,3 +117,7 @@ func (u *UserInterface) nativeWindow() uintptr {
func (u *UserInterface) isNativeFullscreen() bool { func (u *UserInterface) isNativeFullscreen() bool {
return bool(C.isNativeFullscreen()) return bool(C.isNativeFullscreen())
} }
func (u *UserInterface) setNativeCursor(shape driver.CursorShape) {
C.setNativeCursor(C.int(shape))
}

View File

@ -20,6 +20,7 @@ package glfw
import ( import (
"math" "math"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/glfw"
) )
@ -67,3 +68,8 @@ func (u *UserInterface) nativeWindow() uintptr {
func (u *UserInterface) isNativeFullscreen() bool { func (u *UserInterface) isNativeFullscreen() bool {
return false return false
} }
func (u *UserInterface) setNativeCursor(shape driver.CursorShape) {
// TODO: Use native API in the future (#1571)
u.window.SetCursor(glfwSystemCursors[shape])
}

View File

@ -20,6 +20,7 @@ import (
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/glfw"
) )
@ -186,3 +187,8 @@ func (u *UserInterface) nativeWindow() uintptr {
func (u *UserInterface) isNativeFullscreen() bool { func (u *UserInterface) isNativeFullscreen() bool {
return false return false
} }
func (u *UserInterface) setNativeCursor(shape driver.CursorShape) {
// TODO: Use native API in the future (#1571)
u.window.SetCursor(glfwSystemCursors[shape])
}

View File

@ -41,6 +41,10 @@ func driverCursorShapeToCSSCursor(cursor driver.CursorShape) string {
return "crosshair" return "crosshair"
case driver.CursorShapePointer: case driver.CursorShapePointer:
return "pointer" return "pointer"
case driver.CursorShapeEWResize:
return "ew-resize"
case driver.CursorShapeNSResize:
return "ns-resize"
} }
return "auto" return "auto"
} }