2023-01-14 19:24:39 -07:00
|
|
|
package tomo
|
|
|
|
|
2023-01-18 15:32:33 -07:00
|
|
|
import "image"
|
|
|
|
|
2023-01-14 19:24:39 -07:00
|
|
|
// Element represents a basic on-screen object.
|
|
|
|
type Element interface {
|
|
|
|
// Element must implement the Canvas interface. Elements should start
|
|
|
|
// out with a completely blank buffer, and only allocate memory and draw
|
|
|
|
// on it for the first time when sent an EventResize event.
|
|
|
|
Canvas
|
|
|
|
|
|
|
|
// MinimumSize specifies the minimum amount of pixels this element's
|
|
|
|
// width and height may be set to. If the element is given a resize
|
|
|
|
// event with dimensions smaller than this, it will use its minimum
|
|
|
|
// instead of the offending dimension(s).
|
|
|
|
MinimumSize () (width, height int)
|
2023-01-15 10:23:13 -07:00
|
|
|
|
|
|
|
// Resize resizes the element. This should only be called by the
|
|
|
|
// element's parent.
|
|
|
|
Resize (width, height int)
|
|
|
|
|
2023-01-19 12:28:56 -07:00
|
|
|
// OnDamage sets a function to be called when an area of the element is
|
|
|
|
// drawn on and should be pushed to the screen.
|
|
|
|
OnDamage (callback func (region Canvas))
|
|
|
|
|
|
|
|
// OnMinimumSizeChange sets a function to be called when the element's
|
|
|
|
// minimum size is changed.
|
|
|
|
OnMinimumSizeChange (callback func ())
|
2023-01-14 19:24:39 -07:00
|
|
|
}
|
2023-01-14 20:18:06 -07:00
|
|
|
|
|
|
|
// SelectionDirection represents a keyboard navigation direction.
|
|
|
|
type SelectionDirection int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SelectionDirectionNeutral SelectionDirection = 0
|
|
|
|
SelectionDirectionBackward SelectionDirection = -1
|
|
|
|
SelectionDirectionForward SelectionDirection = 1
|
|
|
|
)
|
|
|
|
|
2023-01-16 10:21:47 -07:00
|
|
|
// Canon returns a well-formed direction.
|
|
|
|
func (direction SelectionDirection) Canon () (canon SelectionDirection) {
|
|
|
|
if direction > 0 {
|
|
|
|
return SelectionDirectionForward
|
|
|
|
} else if direction == 0 {
|
|
|
|
return SelectionDirectionNeutral
|
|
|
|
} else {
|
|
|
|
return SelectionDirectionBackward
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 20:18:06 -07:00
|
|
|
// Selectable represents an element that has keyboard navigation support. This
|
|
|
|
// includes inputs, buttons, sliders, etc. as well as any elements that have
|
|
|
|
// children (so keyboard navigation events can be propagated downward).
|
|
|
|
type Selectable interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// Selected returns whether or not this element is currently selected.
|
|
|
|
Selected () (selected bool)
|
|
|
|
|
|
|
|
// Select selects this element, if its parent element grants the
|
|
|
|
// request.
|
|
|
|
Select ()
|
|
|
|
|
|
|
|
// HandleSelection causes this element to mark itself as selected, if it
|
|
|
|
// can currently be. Otherwise, it will return false and do nothing.
|
|
|
|
HandleSelection (direction SelectionDirection) (accepted bool)
|
|
|
|
|
|
|
|
// HandleDeselection causes this element to mark itself and all of its
|
|
|
|
// children as deselected.
|
|
|
|
HandleDeselection ()
|
2023-01-19 12:28:56 -07:00
|
|
|
|
|
|
|
// OnSelectionRequest sets a function to be called when this element
|
|
|
|
// wants its parent element to select it. Parent elements should return
|
|
|
|
// true if the request was granted, and false if it was not.
|
|
|
|
OnSelectionRequest (func () (granted bool))
|
|
|
|
|
|
|
|
// OnSelectionMotionRequest sets a function to be called when this
|
|
|
|
// element wants its parent element to select the element behind or in
|
|
|
|
// front of it, depending on the specified direction. Parent elements
|
|
|
|
// should return true if the request was granted, and false if it was
|
|
|
|
// not.
|
|
|
|
OnSelectionMotionRequest (func (SelectionDirection) (granted bool))
|
2023-01-14 20:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyboardTarget represents an element that can receive keyboard input.
|
|
|
|
type KeyboardTarget interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// HandleKeyDown is called when a key is pressed down while this element
|
|
|
|
// has keyboard focus. It is important to note that not every key down
|
|
|
|
// event is guaranteed to be paired with exactly one key up event. This
|
|
|
|
// is the reason a list of modifier keys held down at the time of the
|
|
|
|
// key press is given.
|
|
|
|
HandleKeyDown (key Key, modifiers Modifiers, repeated bool)
|
|
|
|
|
|
|
|
// HandleKeyUp is called when a key is released while this element has
|
|
|
|
// keyboard focus.
|
2023-01-15 10:23:13 -07:00
|
|
|
HandleKeyUp (key Key, modifiers Modifiers)
|
2023-01-14 20:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// MouseTarget represents an element that can receive mouse events.
|
|
|
|
type MouseTarget interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// Each of these handler methods is passed the position of the mouse
|
|
|
|
// cursor at the time of the event as x, y.
|
|
|
|
|
|
|
|
// HandleMouseDown is called when a mouse button is pressed down on this
|
|
|
|
// element.
|
|
|
|
HandleMouseDown (x, y int, button Button)
|
|
|
|
|
|
|
|
// HandleMouseUp is called when a mouse button is released that was
|
|
|
|
// originally pressed down on this element.
|
|
|
|
HandleMouseUp (x, y int, button Button)
|
|
|
|
|
|
|
|
// HandleMouseMove is called when the mouse is moved over this element,
|
|
|
|
// or the mouse is moving while being held down and originally pressed
|
|
|
|
// down on this element.
|
|
|
|
HandleMouseMove (x, y int)
|
|
|
|
|
|
|
|
// HandleScroll is called when the mouse is scrolled. The X and Y
|
|
|
|
// direction of the scroll event are passed as deltaX and deltaY.
|
2023-01-19 12:28:56 -07:00
|
|
|
HandleMouseScroll (x, y int, deltaX, deltaY float64)
|
2023-01-14 20:18:06 -07:00
|
|
|
}
|
2023-01-15 10:23:13 -07:00
|
|
|
|
2023-01-16 20:27:17 -07:00
|
|
|
// Flexible represents an element who's preferred minimum height can change in
|
2023-01-15 10:33:52 -07:00
|
|
|
// response to its width.
|
2023-01-16 20:27:17 -07:00
|
|
|
type Flexible interface {
|
2023-01-15 10:23:13 -07:00
|
|
|
Element
|
|
|
|
|
2023-01-19 12:28:56 -07:00
|
|
|
// FlexibleHeightFor returns what the element's minimum height would be
|
|
|
|
// if resized to a specified width. This does not actually alter the
|
|
|
|
// state of the element in any way, but it may perform significant work,
|
|
|
|
// so it should be called sparingly.
|
2023-01-15 10:33:52 -07:00
|
|
|
//
|
|
|
|
// It is reccomended that parent containers check for this interface and
|
|
|
|
// take this method's value into account in order to support things like
|
|
|
|
// flow layouts and text wrapping, but it is not absolutely necessary.
|
|
|
|
// The element's MinimumSize method will still return the absolute
|
|
|
|
// minimum size that the element may be resized to.
|
|
|
|
//
|
|
|
|
// It is important to note that if a parent container checks for
|
2023-01-16 20:27:17 -07:00
|
|
|
// flexible chilren, it itself will likely need to be flexible.
|
2023-01-19 12:28:56 -07:00
|
|
|
FlexibleHeightFor (width int) (height int)
|
|
|
|
|
|
|
|
// OnFlexibleHeightChange sets a function to be called when the
|
|
|
|
// parameters affecting this element's flexible height are changed.
|
|
|
|
OnFlexibleHeightChange (callback func ())
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-01-18 15:32:33 -07:00
|
|
|
|
|
|
|
// Scrollable represents an element that can be scrolled. It acts as a viewport
|
|
|
|
// through which its contents can be observed.
|
|
|
|
type Scrollable interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
ScrollContentBounds () (bounds image.Rectangle)
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's
|
|
|
|
// viewport relative to ScrollBounds.
|
|
|
|
ScrollViewportBounds () (bounds image.Rectangle)
|
|
|
|
|
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
ScrollTo (position image.Point)
|
|
|
|
|
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
ScrollAxes () (horizontal, vertical bool)
|
2023-01-19 12:28:56 -07:00
|
|
|
|
|
|
|
// OnScrollBoundsChange sets a function to be called when the element's
|
2023-01-20 13:35:43 -07:00
|
|
|
// ScrollContentBounds, ScrollViewportBounds, or ScrollAxes are changed.
|
2023-01-19 12:28:56 -07:00
|
|
|
OnScrollBoundsChange (callback func ())
|
2023-01-18 15:32:33 -07:00
|
|
|
}
|