| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package mouse defines an event for mouse input. |
| 6 | // |
| 7 | // See the golang.org/x/mobile/app package for details on the event model. |
| 8 | package mouse |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
| 12 | |
| 13 | "golang.org/x/mobile/event/key" |
| 14 | ) |
| 15 | |
| 16 | // Event is a mouse event. |
| 17 | type Event struct { |
| 18 | // X and Y are the mouse location, in pixels. |
| 19 | X, Y float32 |
| 20 | |
| 21 | // Button is the mouse button being pressed or released. Its value may be |
| 22 | // zero, for a mouse move or drag without any button change. |
| 23 | Button Button |
| 24 | |
| 25 | // TODO: have a field to hold what other buttons are down, for detecting |
| 26 | // drags or button-chords. |
| 27 | |
| 28 | // Modifiers is a bitmask representing a set of modifier keys: |
| 29 | // key.ModShift, key.ModAlt, etc. |
| 30 | Modifiers key.Modifiers |
| 31 | |
| 32 | // Direction is the direction of the mouse event: DirPress, DirRelease, |
| 33 | // or DirNone (for mouse moves or drags). |
| 34 | Direction Direction |
| 35 | |
| 36 | // TODO: add a Device ID, for multiple input devices? |
| 37 | // TODO: add a time.Time? |
| 38 | } |
| 39 | |
| 40 | // Button is a mouse button. |
| 41 | type Button int32 |
| 42 | |
| 43 | // IsWheel reports whether the button is for a scroll wheel. |
| 44 | func (b Button) IsWheel() bool { |
| 45 | return b < 0 |
| 46 | } |
| 47 | |
| 48 | // TODO: have a separate axis concept for wheel up/down? How does that relate |
| 49 | // to joystick events? |
| 50 | |
| 51 | const ( |
| 52 | ButtonNone Button = +0 |
| 53 | ButtonLeft Button = +1 |
| 54 | ButtonMiddle Button = +2 |
| 55 | ButtonRight Button = +3 |
| 56 | |
| 57 | ButtonWheelUp Button = -1 |
| 58 | ButtonWheelDown Button = -2 |
| 59 | ButtonWheelLeft Button = -3 |
| 60 | ButtonWheelRight Button = -4 |
| 61 | ) |
| 62 | |
| 63 | // Direction is the direction of the mouse event. |
| 64 | type Direction uint8 |
| 65 | |
| 66 | const ( |
| 67 | DirNone Direction = 0 |
| 68 | DirPress Direction = 1 |
| 69 | DirRelease Direction = 2 |
| 70 | // DirStep is a simultaneous press and release, such as a single step of a |
| 71 | // mouse wheel. |
| 72 | // |
| 73 | // Its value equals DirPress | DirRelease. |
| 74 | DirStep Direction = 3 |
| 75 | ) |
| 76 | |
| 77 | func (d Direction) String() string { |
| 78 | switch d { |
| 79 | case DirNone: |
| 80 | return "None" |
| 81 | case DirPress: |
| 82 | return "Press" |
| 83 | case DirRelease: |
| 84 | return "Release" |
| 85 | case DirStep: |
| 86 | return "Step" |
| 87 | default: |
| 88 | return fmt.Sprintf("mouse.Direction(%d)", d) |
| 89 | } |
| 90 | } |