mouse.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. */
  7. import "C"
  8. // import "unsafe"
  9. import "runtime"
  10. // Type that wraps a mouse
  11. type Mouse struct {
  12. handle *C.ALLEGRO_MOUSE
  13. }
  14. // Wraps a C Allegro mouse in a Mouse. Sets no finalizer.
  15. func wrapMouseRaw(handle *C.ALLEGRO_MOUSE) *Mouse {
  16. if handle == nil {
  17. return nil
  18. }
  19. return &Mouse{handle}
  20. }
  21. // Destroys a mouse. Use this only when really needed!
  22. func (self *Mouse) Destroy() {
  23. if self.handle != nil {
  24. // do nothing
  25. }
  26. self.handle = nil
  27. }
  28. // Wraps a C Allegro mouse cursor in a MouseCursor. Sets a finalizer that calls Destroy.
  29. func wrapMouse(handle *C.ALLEGRO_MOUSE) *MouseCursor {
  30. self := wrapMouse(handle)
  31. if self != nil {
  32. runtime.SetFinalizer(self, func(me *Mouse) { me.Destroy() })
  33. }
  34. return self
  35. }
  36. // Type that wraps a mouse cursor
  37. type MouseCursor struct {
  38. handle *C.ALLEGRO_MOUSE_CURSOR
  39. }
  40. // Returns low level hande for cursor
  41. func (cursor *MouseCursor) toC() *C.ALLEGRO_MOUSE_CURSOR {
  42. return cursor.handle
  43. }
  44. // Destroys a mouse cursor. Use this only when really needed!
  45. func (self *MouseCursor) Destroy() {
  46. if self.handle != nil {
  47. C.al_destroy_mouse_cursor(self.handle)
  48. }
  49. self.handle = nil
  50. }
  51. // Wraps a C Allegro mouse cursor in a MouseCursor. Sets no finalizer.
  52. func wrapMouseCursorRaw(handle *C.ALLEGRO_MOUSE_CURSOR) *MouseCursor {
  53. if handle == nil {
  54. return nil
  55. }
  56. return &MouseCursor{handle}
  57. }
  58. // Wraps a C Allegro mouse cursor in a MouseCursor. Sets a finalizer that calls Destroy.
  59. func wrapMouseCursor(handle *C.ALLEGRO_MOUSE_CURSOR) *MouseCursor {
  60. self := wrapMouseCursor(handle)
  61. if self != nil {
  62. runtime.SetFinalizer(self, func(me *MouseCursor) { me.Destroy() })
  63. }
  64. return self
  65. }
  66. // Mouse state type
  67. type MouseState C.ALLEGRO_MOUSE_STATE
  68. // Convert from C
  69. func wrapMouseState(state C.ALLEGRO_MOUSE_STATE) MouseState {
  70. return MouseState(state)
  71. }
  72. // Convert to C
  73. func (state MouseState) toC() C.ALLEGRO_MOUSE_STATE {
  74. return C.ALLEGRO_MOUSE_STATE(state)
  75. }
  76. // Convert to C
  77. func (state * MouseState) toCPointer() * C.ALLEGRO_MOUSE_STATE {
  78. return (* C.ALLEGRO_MOUSE_STATE)(state)
  79. }
  80. func (state MouseState) X() int {
  81. return int(state.x)
  82. }
  83. func (state MouseState) Y() int {
  84. return int(state.y)
  85. }
  86. func (state MouseState) Z() int {
  87. return int(state.z)
  88. }
  89. func (state MouseState) W() int {
  90. return int(state.w)
  91. }
  92. func (state MouseState) Buttons() int {
  93. return int(state.buttons)
  94. }
  95. func (state MouseState) Pressure() float32 {
  96. return float32(state.pressure)
  97. }
  98. func (state MouseState) Display() * Display {
  99. return wrapDisplayRaw(state.display)
  100. }
  101. type SystemMouseCursor int
  102. const (
  103. SYSTEM_MOUSE_CURSOR_NONE = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_NONE)
  104. SYSTEM_MOUSE_CURSOR_DEFAULT = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT)
  105. SYSTEM_MOUSE_CURSOR_ARROW = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_ARROW)
  106. SYSTEM_MOUSE_CURSOR_BUSY = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_BUSY)
  107. SYSTEM_MOUSE_CURSOR_QUESTION = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_QUESTION)
  108. SYSTEM_MOUSE_CURSOR_EDIT = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_EDIT)
  109. SYSTEM_MOUSE_CURSOR_MOVE = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE)
  110. SYSTEM_MOUSE_CURSOR_RESIZE_N = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_N)
  111. SYSTEM_MOUSE_CURSOR_RESIZE_W = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_W)
  112. SYSTEM_MOUSE_CURSOR_RESIZE_S = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_S)
  113. SYSTEM_MOUSE_CURSOR_RESIZE_E = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_E)
  114. SYSTEM_MOUSE_CURSOR_RESIZE_NW = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NW)
  115. SYSTEM_MOUSE_CURSOR_RESIZE_SW = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SW)
  116. SYSTEM_MOUSE_CURSOR_RESIZE_SE = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_SE)
  117. SYSTEM_MOUSE_CURSOR_RESIZE_NE = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_RESIZE_NE)
  118. SYSTEM_MOUSE_CURSOR_PROGRESS = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_PROGRESS)
  119. SYSTEM_MOUSE_CURSOR_PRECISION = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_PRECISION)
  120. SYSTEM_MOUSE_CURSOR_LINK = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_LINK)
  121. SYSTEM_MOUSE_CURSOR_ALT_SELECT = SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT)
  122. SYSTEM_MOUSE_CURSOR_UNAVAILABLE= SystemMouseCursor(C.ALLEGRO_SYSTEM_MOUSE_CURSOR_UNAVAILABLE)
  123. NUM_SYSTEM_MOUSE_CURSORS = SystemMouseCursor(C.ALLEGRO_NUM_SYSTEM_MOUSE_CURSORS)
  124. )
  125. func IsMouseInstalled() bool {
  126. return cb2b(C.al_is_mouse_installed())
  127. }
  128. func InstallMouse() bool {
  129. return cb2b(C.al_install_mouse())
  130. }
  131. func UninstallMouse() {
  132. C.al_uninstall_mouse()
  133. }
  134. func GetMouseNumButtons() uint {
  135. return uint(C.al_get_mouse_num_buttons())
  136. }
  137. func GetMouseNumAxes() uint {
  138. return uint(C.al_get_mouse_num_axes())
  139. }
  140. func (display * Display) SetMouseXY(x , y int) bool {
  141. return cb2b(C.al_set_mouse_xy(display.toC(), ci(x) , ci(y)))
  142. }
  143. func SetMouseZ(z int) bool {
  144. return cb2b(C.al_set_mouse_z(ci(z)))
  145. }
  146. func SetMouseW(w int) bool {
  147. return cb2b(C.al_set_mouse_w(ci(w)))
  148. }
  149. func SetMouseAxis(axis, value int) bool {
  150. return cb2b(C.al_set_mouse_axis(ci(axis), ci(value)))
  151. }
  152. func AlGetMouseState() MouseState {
  153. var state C.ALLEGRO_MOUSE_STATE
  154. C.al_get_mouse_state(&state)
  155. return wrapMouseState(state)
  156. }
  157. func (state * MouseState) ButtonDown(button int) bool {
  158. return cb2b(C.al_mouse_button_down(state.toCPointer(), ci(button)))
  159. }
  160. func (state * MouseState) Axis(axis int) int {
  161. return int(C.al_get_mouse_state_axis(state.toCPointer(), ci(axis)))
  162. }
  163. func GetMouseEventSource() * EventSource {
  164. return wrapEventSourceRaw(C.al_get_mouse_event_source())
  165. }
  166. func CreateMouseCursor(sprite * Bitmap, xfocus, yfocus int) * MouseCursor {
  167. return wrapMouseCursor(C.al_create_mouse_cursor(sprite.toC(), ci(xfocus), ci(yfocus)))
  168. }
  169. func (display * Display) SetMouseCursor(cursor * MouseCursor) bool {
  170. return cb2b(C.al_set_mouse_cursor(display.toC(), cursor.toC()))
  171. }
  172. func (cursor SystemMouseCursor) toC() C.ALLEGRO_SYSTEM_MOUSE_CURSOR {
  173. return C.ALLEGRO_SYSTEM_MOUSE_CURSOR(cursor)
  174. }
  175. func (display * Display) SetSystemMouseCursor(cursor SystemMouseCursor) bool {
  176. return cb2b(C.al_set_system_mouse_cursor(display.toC(), cursor.toC()))
  177. }
  178. func (display * Display) ShowMouseCursor() bool {
  179. return cb2b(C.al_show_mouse_cursor(display.toC()))
  180. }
  181. func (display * Display) HideMouseCursor() bool {
  182. return cb2b(C.al_hide_mouse_cursor(display.toC()))
  183. }
  184. func GetMouseCursorPosition() (ok bool, x, y int) {
  185. var cx, cy C.int
  186. ok = cb2b(C.al_get_mouse_cursor_position(&cx, &cy))
  187. x = int(cx)
  188. y = int(cy)
  189. return ok, x, y
  190. }
  191. func (display * Display) GrabMouse() bool {
  192. return cb2b(C.al_grab_mouse(display.toC()))
  193. }
  194. func UngrabMouse() bool {
  195. return cb2b(C.al_ungrab_mouse())
  196. }