mouse

package
Version: v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2020 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package mouse adds support for mouse input.

You can start using mouse by creating Mouse instance:

mouseState := mouse.New(window)
loop.Run(window, func(frame *loop.Frame) {
    mouseState.Update() // This is needed each frame
    if mouseState.Pressed(mouse.Left) {
        ...
    }
})

Index

Constants

This section is empty.

Variables

View Source
var EmptyEvent = Event{}

EmptyEvent should be returned by EventSource when it does not have more events.

Functions

This section is empty.

Types

type Button

type Button int

Button is a mouse button which was pressed or released.

const (
	// Left is a left mouse button
	Left Button = 1
	// Right is a right mouse button
	Right Button = 2
	// Middle is a middle mouse button
	Middle Button = 3
	// Button4 is 4th mouse button
	Button4 Button = 4
	// Button5 is 5th mouse button
	Button5 Button = 5
	// Button6 is 6th mouse button
	Button6 Button = 6
	// Button7 is 7th mouse button
	Button7 Button = 7
	// Button8 is 8th mouse button
	Button8 Button = 8
)

type Event

type Event struct {
	// contains filtered or unexported fields
}

Event describes what happened with the mouse.

Event can be constructed using NewXXXEvent function.

func NewMovedEvent

func NewMovedEvent(posX, posY int, realPosX, realPosY float64, insideWindow bool) Event

NewMovedEvent returns new instance of Event when mouse was moved.

realPosX and realPosY are the cursor position in real pixel coordinates. For systems supporting subpixel coordinates these may be fractional numbers. posX and posY should be virtual pixel coordinates taking into account current zoom. For zoom=2 and realPosX=2, posX should be 1.

func NewPressedEvent

func NewPressedEvent(button Button) Event

NewPressedEvent returns new instance of Event when button was pressed.

func NewReleasedEvent

func NewReleasedEvent(button Button) Event

NewReleasedEvent returns new instance of Event when button was released.

func NewScrolledEvent

func NewScrolledEvent(x, y float64) Event

NewScrolledEvent returns new instance of Event when mouse was scrolled.

type EventBuffer

type EventBuffer struct {
	// contains filtered or unexported fields
}

EventBuffer is a capped collection of accumulated events which can be used by libraries or in unit tests as a fake implementation of EventSource. The order of added events is preserved. EventBuffer is an EventSource and can be directly consumed by Mouse.

func NewEventBuffer

func NewEventBuffer(size int) *EventBuffer

NewEventBuffer creates EventBuffer of given size. The minimum size of buffer is 1. Size smaller than 1 is constrained to 1.

func (*EventBuffer) Add

func (q *EventBuffer) Add(event Event)

Add adds event to the buffer. If there is not enough space the oldest event will be replaced.

func (*EventBuffer) Poll

func (q *EventBuffer) Poll() (Event, bool)

Poll retrieves and removes event from the buffer. If there are no available events EmptyEvent and false is returned.

type EventSource

type EventSource interface {
	// PollMouseEvent retrieves and removes next mouse Event. If there are no more
	// events false is returned.
	PollMouseEvent() (Event, bool)
}

EventSource is a source of mouse Events. On each Update() Mouse polls the EventSource by executing PollMouseEvent method multiple times - until PollMouseEvent() returns false. In other words Mouse#Update drains the EventSource.

type Mouse

type Mouse struct {
	// contains filtered or unexported fields
}

Mouse provides a read-only information about the current state of the mouse, such as what buttons are currently pressed. Please note that updating the Mouse state retrieves and removes events from EventSource. Therefore only one Mouse instance can be created for specific EventSource.

func New

func New(source EventSource) *Mouse

New creates Mouse instance. It will consume all events from EventSource each time Update method is called. For this reason you can't have two Mouse instances for the same EventSource.

func (*Mouse) JustPressed

func (m *Mouse) JustPressed(button Button) bool

JustPressed returns true if the button was pressed between two last mouse.Update calls. If it was pressed and released at the same time between these calls this method return true.

func (*Mouse) JustReleased

func (m *Mouse) JustReleased(button Button) bool

JustReleased returns true if the button was released between two last mouse.Update calls. If it was released and pressed at the same time between these calls this method return true.

func (*Mouse) Position

func (m *Mouse) Position() Position

Position returns current mouse position

func (*Mouse) PositionChange

func (m *Mouse) PositionChange() PositionChange

PositionChange returns information about how the mouse position has changed between the last two Mouse.Update calls

func (*Mouse) PositionChanged

func (m *Mouse) PositionChanged() bool

PositionChanged returns true whether mouse position has changed between the last two Mouse.Update calls

func (*Mouse) Pressed

func (m *Mouse) Pressed(button Button) bool

Pressed returns true if given mouse button is currently pressed. If between two last mouse.Update calls the key was pressed and released then the this method returns false.

func (*Mouse) PressedButtons

func (m *Mouse) PressedButtons() []Button

PressedButtons returns a slice of all currently pressed buttons. It may be empty aka nil. This function can be used to get a button mapping for a given action in the game. If between two last mouse.Update calls the button was pressed and released then the button is not returned.

func (*Mouse) Scroll

func (m *Mouse) Scroll() Scroll

Scroll returns information about cumulative scroll between two last Mouse.Update calls.

func (*Mouse) Update

func (m *Mouse) Update()

Update updates the state of the mouse by polling events queued since last time the function was executed.

type Position

type Position struct {
	// contains filtered or unexported fields
}

Position contains information about current mouse position

func (Position) InsideWindow

func (p Position) InsideWindow() bool

InsideWindow returns true if mouse is pointing to a pixel inside window.

func (Position) RealX

func (p Position) RealX() float64

RealX returns the pixel x-coordinate where mouse is pointing to in a display resolution. If OS supports sub-pixel precision for mouse position then fractional number is returned.

func (Position) RealY

func (p Position) RealY() float64

RealY returns the pixel y-coordinate where mouse is pointing to in a display resolution. If OS supports sub-pixel precision for mouse position then fractional number is returned.

func (Position) X

func (p Position) X() int

X returns the pixel x-coordinate where mouse is pointing to.

func (Position) Y

func (p Position) Y() int

Y returns the pixel y-coordinate where mouse is pointing to.

type PositionChange

type PositionChange struct {
	// contains filtered or unexported fields
}

PositionChange returns information about how the mouse position has changed between the last two Mouse.Update calls

func (PositionChange) RealX

func (p PositionChange) RealX() float64

RealX returns the pixel x-coordinate difference in a display resolution. If OS supports sub-pixel precision for mouse position then fractional number is returned.

func (PositionChange) RealY

func (p PositionChange) RealY() float64

RealY returns the pixel y-coordinate difference in a display resolution. If OS supports sub-pixel precision for mouse position then fractional number is returned.

func (PositionChange) WindowEntered

func (p PositionChange) WindowEntered() bool

WindowEntered returns true if mouse just entered the window (entered the window between last two Mouse.Update calls).

func (PositionChange) WindowLeft

func (p PositionChange) WindowLeft() bool

WindowLeft returns true if mouse just left the window (left the window between last two Mouse.Update calls).

func (PositionChange) X

func (p PositionChange) X() int

X returns the pixel x-coordinate difference.

func (PositionChange) Y

func (p PositionChange) Y() int

Y returns the pixel y-coordinate difference.

type Scroll

type Scroll struct {
	// contains filtered or unexported fields
}

Scroll provides information about cumulative scroll between two last Mouse.Update calls.

func (Scroll) X

func (s Scroll) X() float64

X can be negative (scrolled left) or positive (scrolled right).

func (Scroll) Y

func (s Scroll) Y() float64

Y can be negative (scrolled down) or positive (scrolled up).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL