code.witches.io/go/sdl2@v0.1.1/mouse.go (about)

     1  package sdl
     2  
     3  // #include <SDL2/SDL_mouse.h>
     4  import "C"
     5  import (
     6  	"unsafe"
     7  )
     8  
     9  const (
    10  	MouseButtonLeft   = 1
    11  	MouseButtonMiddle = 2
    12  	MouseButtonRight  = 3
    13  	MouseButtonExtra1 = 4
    14  	MouseButtonExtra2 = 5
    15  )
    16  
    17  type MouseButtons struct {
    18  	Left   bool
    19  	Middle bool
    20  	Right  bool
    21  	X1     bool
    22  	X2     bool
    23  }
    24  
    25  func (b MouseButtons) String() string {
    26  	s := []byte{'-', '-', '-', '-', '-'}
    27  	if b.Left {
    28  		s[0] = 'L'
    29  	}
    30  	if b.Middle {
    31  		s[1] = 'M'
    32  	}
    33  	if b.Right {
    34  		s[2] = 'R'
    35  	}
    36  	if b.X1 {
    37  		s[3] = 'X'
    38  	}
    39  	if b.X2 {
    40  		s[4] = 'X'
    41  	}
    42  	return string(s)
    43  }
    44  
    45  func GetMouseState(x, y *int) uint32 {
    46  	var _x, _y int32
    47  	buttons := uint32(C.SDL_GetMouseState((*C.int)(&_x), (*C.int)(&_y)))
    48  	if x != nil {
    49  		*x = int(_x)
    50  	}
    51  	if y != nil {
    52  		*y = int(_y)
    53  	}
    54  	return buttons
    55  	//return uint32(C.SDL_GetMouseState((*C.int)(unsafe.Pointer(x)), (*C.int)(unsafe.Pointer(y))))
    56  }
    57  
    58  func GetRelativeMouseState(x, y *int) uint32 {
    59  	var _x, _y C.int
    60  	buttons := uint32(C.SDL_GetRelativeMouseState(&_x, &_y))
    61  	if x != nil {
    62  		*x = int(_x)
    63  	}
    64  	if y != nil {
    65  		*y = int(_y)
    66  	}
    67  	return buttons
    68  }
    69  
    70  func WarpMouseInWindow(window *Window, x, y int) {
    71  	C.SDL_WarpMouseInWindow(
    72  		(*C.SDL_Window)(unsafe.Pointer(window)),
    73  		(C.int)(x),
    74  		(C.int)(y),
    75  	)
    76  }
    77  
    78  func GetRelativeMouseMode() bool {
    79  	return C.SDL_GetRelativeMouseMode() == C.SDL_TRUE
    80  }
    81  
    82  func SetRelativeMouseMode(enabled bool) error {
    83  	var _enabled C.SDL_bool = C.SDL_FALSE
    84  	if enabled {
    85  		_enabled = C.SDL_TRUE
    86  	}
    87  	if C.SDL_SetRelativeMouseMode(_enabled) != 0 {
    88  		return GetError()
    89  	}
    90  	return nil
    91  }