2023-03-30 21:19:04 -06:00
|
|
|
package tomo
|
2023-02-01 23:47:01 -07:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
|
|
|
|
// Element represents a basic on-screen object.
|
|
|
|
type Element interface {
|
2023-04-12 21:21:34 -06:00
|
|
|
// Draw causes the element to draw to the specified canvas. The bounds
|
|
|
|
// of this canvas specify the area that is actually drawn to, while the
|
|
|
|
// Entity bounds specify the actual area of the element.
|
|
|
|
Draw (canvas.Canvas)
|
2023-04-14 23:14:36 -06:00
|
|
|
|
|
|
|
// Entity returns this element's entity.
|
|
|
|
Entity () Entity
|
2023-02-01 23:47:01 -07:00
|
|
|
}
|
|
|
|
|
2023-04-16 01:37:28 -06:00
|
|
|
// Layoutable represents an element that needs to perform layout calculations
|
|
|
|
// before it can draw itself.
|
|
|
|
type Layoutable interface {
|
2023-02-01 23:47:01 -07:00
|
|
|
Element
|
2023-04-16 01:37:28 -06:00
|
|
|
|
|
|
|
// Layout causes this element to perform a layout operation.
|
2023-04-12 21:21:34 -06:00
|
|
|
Layout ()
|
2023-04-16 01:37:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Container represents an element capable of containing child elements.
|
|
|
|
type Container interface {
|
|
|
|
Element
|
|
|
|
Layoutable
|
2023-02-01 23:47:01 -07:00
|
|
|
|
2023-04-14 23:14:36 -06:00
|
|
|
// DrawBackground causes the element to draw its background pattern to
|
|
|
|
// the specified canvas. The bounds of this canvas specify the area that
|
|
|
|
// is actually drawn to, while the Entity bounds specify the actual area
|
|
|
|
// of the element.
|
|
|
|
DrawBackground (canvas.Canvas)
|
2023-04-13 00:22:54 -06:00
|
|
|
|
|
|
|
// HandleChildMinimumSizeChange is called when a child's minimum size is
|
|
|
|
// changed.
|
2023-04-14 22:02:30 -06:00
|
|
|
HandleChildMinimumSizeChange (child Element)
|
2023-04-12 21:21:34 -06:00
|
|
|
}
|
2023-02-01 23:47:01 -07:00
|
|
|
|
2023-04-17 00:05:53 -06:00
|
|
|
// Enableable represents an element that can be enabled and disabled. Disabled
|
|
|
|
// elements typically appear greyed out.
|
|
|
|
type Enableable interface {
|
2023-04-12 21:21:34 -06:00
|
|
|
Element
|
2023-02-01 23:47:01 -07:00
|
|
|
|
2023-04-17 00:05:53 -06:00
|
|
|
// Enabled returns whether or not the element is enabled.
|
2023-04-15 19:49:40 -06:00
|
|
|
Enabled () bool
|
2023-04-17 00:05:53 -06:00
|
|
|
|
|
|
|
// SetEnabled sets whether or not the element is enabled.
|
|
|
|
SetEnabled (bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Focusable represents an element that has keyboard navigation support.
|
|
|
|
type Focusable interface {
|
|
|
|
Element
|
|
|
|
Enableable
|
2023-04-15 19:49:40 -06:00
|
|
|
|
2023-04-12 21:21:34 -06:00
|
|
|
// HandleFocusChange is called when the element is focused or unfocused.
|
|
|
|
HandleFocusChange ()
|
2023-02-01 23:47:01 -07:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:05:53 -06:00
|
|
|
// Selectable represents an element that can be selected. This includes things
|
|
|
|
// like list items, files, etc. The difference between this and Focusable is
|
|
|
|
// that multiple Selectable elements may be selected at the same time, whereas
|
|
|
|
// only one Focusable element may be focused at the same time. Containers who's
|
|
|
|
// purpose is to contain selectable elements can determine when to select them
|
|
|
|
// by implementing MouseTargetContainer and listening for HandleChildMouseDown
|
|
|
|
// events.
|
|
|
|
type Selectable interface {
|
|
|
|
Element
|
|
|
|
Enableable
|
|
|
|
|
|
|
|
// HandleSelectionChange is called when the element is selected or
|
|
|
|
// deselected.
|
|
|
|
HandleSelectionChange ()
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:47:01 -07:00
|
|
|
// KeyboardTarget represents an element that can receive keyboard input.
|
|
|
|
type KeyboardTarget interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// HandleKeyDown is called when a key is pressed down or repeated 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 input.Key, modifiers input.Modifiers)
|
|
|
|
|
|
|
|
// HandleKeyUp is called when a key is released while this element has
|
|
|
|
// keyboard focus.
|
|
|
|
HandleKeyUp (key input.Key, modifiers input.Modifiers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MouseTarget represents an element that can receive mouse events.
|
|
|
|
type MouseTarget interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// HandleMouseDown is called when a mouse button is pressed down on this
|
|
|
|
// element.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleMouseDown (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers)
|
2023-02-01 23:47:01 -07:00
|
|
|
|
|
|
|
// HandleMouseUp is called when a mouse button is released that was
|
|
|
|
// originally pressed down on this element.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleMouseUp (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers)
|
2023-03-14 15:08:39 -06:00
|
|
|
}
|
|
|
|
|
2023-04-17 00:05:53 -06:00
|
|
|
// MouseTargetContainer represents an element that wants to know when one
|
|
|
|
// of its children is clicked. Children do not have to implement MouseTarget for
|
|
|
|
// a container satisfying MouseTargetContainer to be notified that they have
|
|
|
|
// been clicked.
|
|
|
|
type MouseTargetContainer interface {
|
|
|
|
Container
|
|
|
|
|
|
|
|
// HandleMouseDown is called when a mouse button is pressed down on a
|
|
|
|
// child element.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleChildMouseDown (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
child Element)
|
2023-04-17 00:05:53 -06:00
|
|
|
|
|
|
|
// HandleMouseUp is called when a mouse button is released that was
|
|
|
|
// originally pressed down on a child element.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleChildMouseUp (
|
|
|
|
position image.Point,
|
|
|
|
button input.Button,
|
|
|
|
modifiers input.Modifiers,
|
|
|
|
child Element)
|
2023-04-17 00:05:53 -06:00
|
|
|
}
|
|
|
|
|
2023-03-14 15:08:39 -06:00
|
|
|
// MotionTarget represents an element that can receive mouse motion events.
|
|
|
|
type MotionTarget interface {
|
|
|
|
Element
|
2023-02-01 23:47:01 -07:00
|
|
|
|
2023-03-14 15:08:39 -06:00
|
|
|
// HandleMotion is called when the mouse is moved over this element,
|
2023-02-01 23:47:01 -07:00
|
|
|
// or the mouse is moving while being held down and originally pressed
|
|
|
|
// down on this element.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleMotion (position image.Point)
|
2023-03-14 15:08:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollTarget represents an element that can receive mouse scroll events.
|
|
|
|
type ScrollTarget interface {
|
|
|
|
Element
|
2023-02-01 23:47:01 -07:00
|
|
|
|
|
|
|
// HandleScroll is called when the mouse is scrolled. The X and Y
|
|
|
|
// direction of the scroll event are passed as deltaX and deltaY.
|
2023-04-20 12:44:54 -06:00
|
|
|
HandleScroll (
|
|
|
|
position image.Point,
|
|
|
|
deltaX, deltaY float64,
|
|
|
|
modifiers input.Modifiers)
|
2023-02-01 23:47:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flexible represents an element who's preferred minimum height can change in
|
|
|
|
// response to its width.
|
|
|
|
type Flexible interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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-04-20 12:44:54 -06:00
|
|
|
// flexible chilren, it itself will likely need to be either scrollable
|
|
|
|
// or flexible.
|
2023-03-14 15:08:39 -06:00
|
|
|
FlexibleHeightFor (width int) int
|
2023-02-01 23:47:01 -07:00
|
|
|
}
|
|
|
|
|
2023-04-13 00:22:54 -06:00
|
|
|
// FlexibleContainer represents an element that is capable of containing
|
|
|
|
// flexible children.
|
|
|
|
type FlexibleContainer interface {
|
|
|
|
Container
|
|
|
|
|
|
|
|
// HandleChildFlexibleHeightChange is called when the parameters
|
|
|
|
// affecting a child's flexible height are changed.
|
2023-04-14 22:02:30 -06:00
|
|
|
HandleChildFlexibleHeightChange (child Flexible)
|
2023-04-13 00:22:54 -06:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:47:01 -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.
|
2023-03-14 15:08:39 -06:00
|
|
|
ScrollContentBounds () image.Rectangle
|
2023-02-01 23:47:01 -07:00
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's
|
|
|
|
// viewport relative to ScrollBounds.
|
2023-03-14 15:08:39 -06:00
|
|
|
ScrollViewportBounds () image.Rectangle
|
2023-02-01 23:47:01 -07:00
|
|
|
|
|
|
|
// 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-02-02 23:14:03 -07:00
|
|
|
|
2023-04-13 00:22:54 -06:00
|
|
|
// ScrollableContainer represents an element that is capable of containing
|
|
|
|
// scrollable children.
|
|
|
|
type ScrollableContainer interface {
|
|
|
|
Container
|
|
|
|
|
|
|
|
// HandleChildScrollBoundsChange is called when the content bounds,
|
|
|
|
// viewport bounds, or scroll axes of a child are changed.
|
2023-04-14 22:02:30 -06:00
|
|
|
HandleChildScrollBoundsChange (child Scrollable)
|
2023-04-13 00:22:54 -06:00
|
|
|
}
|
|
|
|
|
2023-02-16 15:35:53 -07:00
|
|
|
// Collapsible represents an element who's minimum width and height can be
|
|
|
|
// manually resized. Scrollable elements should implement this if possible.
|
|
|
|
type Collapsible interface {
|
|
|
|
Element
|
|
|
|
|
|
|
|
// Collapse collapses the element's minimum width and height. A value of
|
|
|
|
// zero for either means that the element's normal value is used.
|
|
|
|
Collapse (width, height int)
|
|
|
|
}
|
|
|
|
|
2023-02-02 23:14:03 -07:00
|
|
|
// Themeable represents an element that can modify its appearance to fit within
|
|
|
|
// a theme.
|
|
|
|
type Themeable interface {
|
2023-02-02 23:35:59 -07:00
|
|
|
Element
|
|
|
|
|
2023-02-02 23:14:03 -07:00
|
|
|
// SetTheme sets the element's theme to something fulfilling the
|
|
|
|
// theme.Theme interface.
|
2023-03-30 23:06:29 -06:00
|
|
|
SetTheme (Theme)
|
2023-02-02 23:14:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configurable represents an element that can modify its behavior to fit within
|
|
|
|
// a set of configuration parameters.
|
|
|
|
type Configurable interface {
|
2023-02-02 23:35:59 -07:00
|
|
|
Element
|
|
|
|
|
2023-02-02 23:14:03 -07:00
|
|
|
// SetConfig sets the element's configuration to something fulfilling
|
|
|
|
// the config.Config interface.
|
2023-03-30 23:06:29 -06:00
|
|
|
SetConfig (Config)
|
2023-02-02 23:14:03 -07:00
|
|
|
}
|