draw

package
v1.0.6-0...-3b43990 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package draw contains the RunWindow function which you call from runners to open a window. You pass it a callback which is called at 60 frames per second. The callback gives you a Window which you use to do input handling, rendering and audio output. See the documentation for Window for more details on what you can do.

Index

Constants

This section is empty.

Variables

View Source
var (
	Black       = Color{0, 0, 0, 1}
	White       = Color{1, 1, 1, 1}
	Gray        = Color{0.5, 0.5, 0.5, 1}
	LightGray   = Color{0.75, 0.75, 0.75, 1}
	DarkGray    = Color{0.25, 0.25, 0.25, 1}
	Red         = Color{1, 0, 0, 1}
	LightRed    = Color{1, 0.5, 0.5, 1}
	DarkRed     = Color{0.5, 0, 0, 1}
	Green       = Color{0, 1, 0, 1}
	LightGreen  = Color{0.5, 1, 0.5, 1}
	DarkGreen   = Color{0, 0.5, 0, 1}
	Blue        = Color{0, 0, 1, 1}
	LightBlue   = Color{0.5, 0.5, 1, 1}
	DarkBlue    = Color{0, 0, 0.5, 1}
	Purple      = Color{1, 0, 1, 1}
	LightPurple = Color{1, 0.5, 1, 1}
	DarkPurple  = Color{0.5, 0, 0.5, 1}
	Yellow      = Color{1, 1, 0, 1}
	LightYellow = Color{1, 1, 0.5, 1}
	DarkYellow  = Color{0.5, 0.5, 0, 1}
	Cyan        = Color{0, 1, 1, 1}
	LightCyan   = Color{0.5, 1, 1, 1}
	DarkCyan    = Color{0, 0.5, 0.5, 1}
	Brown       = Color{0.5, 0.2, 0, 1}
	LightBrown  = Color{0.75, 0.3, 0, 1}
)

These are predefined colors for intuitive use, no need to set color channels.

Functions

func RunWindow

func RunWindow(title string, width, height int, update UpdateFunction) error

RunWindow creates a new window and calls update 60 times per second.

Types

type Color

type Color struct{ R, G, B, A float32 }

Color consists of four channels ranging from 0 to 1 each. A specifies the opacity, 1 being fully opaque and 0 being fully transparent.

func RGB

func RGB(r, g, b float32) Color

RGB creates a color with full opacity. All values are in the range from 0 to 1.

func RGBA

func RGBA(r, g, b, a float32) Color

RGBA creates a color from the given channel values. All values are in the range from 0 to 1.

type Key

type Key int

Key represents a key on the keyboard.

const (
	KeyA Key = 1 + iota
	KeyB
	KeyC
	KeyD
	KeyE
	KeyF
	KeyG
	KeyH
	KeyI
	KeyJ
	KeyK
	KeyL
	KeyM
	KeyN
	KeyO
	KeyP
	KeyQ
	KeyR
	KeyS
	KeyT
	KeyU
	KeyV
	KeyW
	KeyX
	KeyY
	KeyZ
	Key0
	Key1
	Key2
	Key3
	Key4
	Key5
	Key6
	Key7
	Key8
	Key9
	KeyNum0
	KeyNum1
	KeyNum2
	KeyNum3
	KeyNum4
	KeyNum5
	KeyNum6
	KeyNum7
	KeyNum8
	KeyNum9
	KeyF1
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyF13
	KeyF14
	KeyF15
	KeyF16
	KeyF17
	KeyF18
	KeyF19
	KeyF20
	KeyF21
	KeyF22
	KeyF23
	KeyF24
	KeyEnter
	KeyNumEnter
	KeyLeftControl
	KeyRightControl
	KeyLeftShift
	KeyRightShift
	KeyLeftAlt
	KeyRightAlt
	KeyLeft
	KeyRight
	KeyUp
	KeyDown
	KeyEscape
	KeySpace
	KeyBackspace
	KeyTab
	KeyHome
	KeyEnd
	KeyPageDown
	KeyPageUp
	KeyDelete
	KeyInsert
	KeyNumAdd
	KeyNumSubtract
	KeyNumMultiply
	KeyNumDivide
	KeyCapslock
	KeyPrint
	KeyPause
)

These are all available keyboard keys.

func (Key) String

func (k Key) String() string

type MouseButton

type MouseButton int

MouseButton is one of the three buttons typically present on a mouse.

const (
	LeftButton MouseButton = iota
	MiddleButton
	RightButton
)

These are the possible values for MouseButton.

type MouseClick

type MouseClick struct {
	// X and Y are the screen position in pixels, relative to the drawing area.
	// X goes from left to right, starting at 0 and Y goes from top to bottom
	// starting at 0.
	// This means that pixel 0,0 is the top-left pixel in the drawing area (not
	// the title bar).
	X, Y   int
	Button MouseButton
}

MouseClick is used to store mouse click events.

type UpdateFunction

type UpdateFunction func(window Window)

UpdateFunction is used as a callback when creating a window. It is called at 60Hz and you do all your event handling and drawing in it.

type Window

type Window interface {
	// Close closes the window which will stop the update loop after the current
	// frame (you will usually want to return from the update function after
	// calling Close or another frame will be displayed).
	Close()

	// Size returns the window's size in pixels.
	Size() (width, height int)

	// SetFullscreen toggles between the fixed-size window with title and border
	// and going full screen on the monitor that the window is placed on when
	// the call to SetFullscreen(true) occurs.
	// Use Window.Size to get the new size after this.
	// By default the window is not fullscreen. It always starts windowed.
	SetFullscreen(f bool)

	// ShowCursor set the OS' mouse cursor to visible or invisible. It defaults
	// to visible if you do not call ShowCursor.
	ShowCursor(show bool)

	// WasKeyPressed reports whether the specified key was pressed at any time
	// during the last frame. If the user presses a key and releases it in the
	// same frame, this function stores that information and will return true.
	// See the Key... constants for the available keys that can be queried.
	// NOTE do not use this for text input, use Characters instead.
	WasKeyPressed(key Key) bool

	// IsKeyDown reports whether the specified key is being held down at the
	// moment of calling this function.
	// See the Key... constants for the available keys that can be queried.
	IsKeyDown(key Key) bool

	// Characters returns all pressed keys translated to characters that
	// happened in the last frame. The runes in the string are ordered by the
	// time that the keys were entered.
	Characters() string

	// IsMouseDown reports whether the specified button is down at the time of
	// the function call
	IsMouseDown(button MouseButton) bool

	// Clicks returns all MouseClicks that occurred during the last frame.
	Clicks() []MouseClick

	// MousePositoin returns the current mouse position in pixels at the time of
	// the function call. It is relative to the drawing area of the window.
	MousePosition() (x, y int)

	// MouseWheelY returns the aggregate vertical mouse wheel rotation during
	// the last frame. A value of 1 typically corresponds to one tick of the
	// wheel. A positive value means the wheel was rotated forward, away from
	// the user, a negative value means the wheel was rotated backward towards
	// the user.
	MouseWheelY() float64

	// MouseWheelX returns the aggregate horizontal mouse wheel rotation during
	// the last frame. A value of 1 typically corresponds to one tick of the
	// wheel. A positive value means the wheel was rotated right, a negative
	// value means the wheel was rotated left.
	MouseWheelX() float64

	// DrawPoint draws a single point at the given screen position in pixels.
	DrawPoint(x, y int, color Color)

	// DrawLine draws a one pixel wide line from the first point to the second
	// (inclusive).
	DrawLine(fromX, fromY, toX, toY int, color Color)

	// DrawRect draws a one pixel wide rectangle outline.
	DrawRect(x, y, width, height int, color Color)

	// FillRect draws a filled rect.
	FillRect(x, y, width, height int, color Color)

	// DrawEllipse draws a one pixel wide ellipse. The top-left corner of the
	// surrounding rectangle is given by x and y, the horizontal and vertical
	// diameters are given by width and height.
	DrawEllipse(x, y, width, height int, color Color)

	// FillEllipse behaves like DrawEllipse but fills the ellipse with the color
	// instaed of only drawing the outline.
	FillEllipse(x, y, width, height int, color Color)

	DrawTriangle(x0, y0, x1, y1, x2, y2 int, color Color)

	FillTriangle(x0, y0, x1, y1, x2, y2 int, color Color)

	// DrawImageFile draws the untransformed image at the give position. If the
	// image file is not found or has the wrong format an error is returned.
	DrawImageFile(path string, x, y int) error

	// DrawImageFileTo draws the image to the given screen rectangle, possibly
	// scaling it in either direction, and rotates it around the rectangles
	// center point by the given angle. The rotation is clockwise.
	// If the image file is not found or has the wrong format an error is
	// returned.
	DrawImageFileTo(path string, x, y, w, h, rotationCWDeg int) error

	// DrawImageFileRotated draws the image with its top-left corner at the
	// given coordinates but roatated clockwise about the given angle in degrees
	// around its center. This means its top-left corner will only actually be
	// at the given location if the rotation is 0.
	// If the image file is not found or has the wrong format an error is
	// returned.
	DrawImageFileRotated(path string, x, y, rotationCWDeg int) error

	// DrawImageFilePart lets you specify the source and destination rectangle
	// for the image file to be drawn. The image is rotated about its center by
	// the given angle in degrees, clockwise. You may flip the image by
	// specifying a negative width or height. E.g. to flip in x direction,
	// instead of
	//
	//     DrawImageFilePart("x.png", 0, 0, 100, 100, 0, 0, 100, 100, 0)
	//
	// you would do
	//
	//     DrawImageFilePart("x.png", 100, 0, -100, 100, 0, 0, 100, 100, 0)
	//
	// swapping the source rectangle's left and right positions.
	//
	// If the image file is not found or has the wrong format an error is
	// returned.
	DrawImageFilePart(
		path string,
		sourceX, sourceY, sourceWidth, sourceHeight int,
		destX, destY, destWidth, destHeight int,
		rotationCWDeg int,
	) error

	// GetTextSize returns the size the given text would have when being drawn.
	GetTextSize(text string) (w, h int)

	// GetScaledTextSize returns the size the given text would have when being
	// drawn at the given scale.
	GetScaledTextSize(text string, scale float32) (w, h int)

	// DrawText draws a text string. New line characters ('\n') are not drawn
	// but force a line break and the next character is drawn on the line below
	// starting again at x.
	DrawText(text string, x, y int, color Color)

	// DrawScaledText behaves as DrawText, but the text is scaled. If scale = 1
	// this behaves exactly like DrawText, scales > 1 make the text bigger,
	// scales < 1 shrink it. Scales <= 0 will draw no text at all.
	DrawScaledText(text string, x, y int, scale float32, color Color)

	// PlaySoundFile only plays WAV sounds. If the file is not found or has the
	// wrong format an error is returned.
	PlaySoundFile(path string) error
}

Window provides functions to draw simple primitives and images, handle keyboard and mouse events and play sounds. All drawing functions that have width and height as input expect those to be positive. Objects with negative width or height will silently be ignored and not drawn.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL