atomize-element-interface #2
|
@ -13,6 +13,7 @@ type Button struct {
|
||||||
|
|
||||||
pressed bool
|
pressed bool
|
||||||
enabled bool
|
enabled bool
|
||||||
|
selected bool
|
||||||
onClick func ()
|
onClick func ()
|
||||||
|
|
||||||
text string
|
text string
|
||||||
|
@ -24,89 +25,102 @@ func NewButton (text string) (element *Button) {
|
||||||
element = &Button { enabled: true }
|
element = &Button { enabled: true }
|
||||||
element.Core, element.core = core.NewCore(element)
|
element.Core, element.core = core.NewCore(element)
|
||||||
element.drawer.SetFace(theme.FontFaceRegular())
|
element.drawer.SetFace(theme.FontFaceRegular())
|
||||||
element.core.SetSelectable(true)
|
|
||||||
element.SetText(text)
|
element.SetText(text)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle handles an event.
|
func (element *Button) Resize (width, height int) {
|
||||||
func (element *Button) Handle (event tomo.Event) {
|
element.core.AllocateCanvas(width, height)
|
||||||
switch event.(type) {
|
element.draw()
|
||||||
case tomo.EventResize:
|
}
|
||||||
resizeEvent := event.(tomo.EventResize)
|
|
||||||
element.core.AllocateCanvas (
|
func (element *Button) HandleMouseDown (x, y int, button tomo.Button) {
|
||||||
resizeEvent.Width,
|
element.Select()
|
||||||
resizeEvent.Height)
|
if button != tomo.ButtonLeft { return }
|
||||||
|
element.pressed = true
|
||||||
|
if element.core.HasImage() {
|
||||||
element.draw()
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case tomo.EventMouseDown:
|
func (element *Button) HandleMouseUp (x, y int, button tomo.Button) {
|
||||||
if !element.enabled { break }
|
if button != tomo.ButtonLeft { return }
|
||||||
|
element.pressed = false
|
||||||
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
|
|
||||||
mouseDownEvent := event.(tomo.EventMouseDown)
|
within := image.Point { x, y }.
|
||||||
element.Select()
|
In(element.Bounds())
|
||||||
if mouseDownEvent.Button != tomo.ButtonLeft { break }
|
|
||||||
|
if within && element.onClick != nil {
|
||||||
|
element.onClick()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Button) HandleMouseMove (x, y int) { }
|
||||||
|
func (element *Button) HandleScroll (x, y int, deltaX, deltaY float64) { }
|
||||||
|
|
||||||
|
func (element *Button) HandleKeyDown (
|
||||||
|
key tomo.Key,
|
||||||
|
modifiers tomo.Modifiers,
|
||||||
|
repeated bool,
|
||||||
|
) {
|
||||||
|
if key == tomo.KeyEnter {
|
||||||
element.pressed = true
|
element.pressed = true
|
||||||
if element.core.HasImage() {
|
if element.core.HasImage() {
|
||||||
element.draw()
|
element.draw()
|
||||||
element.core.PushAll()
|
element.core.PushAll()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case tomo.EventKeyDown:
|
func (element *Button) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) {
|
||||||
keyDownEvent := event.(tomo.EventKeyDown)
|
if key == tomo.KeyEnter && element.pressed {
|
||||||
if keyDownEvent.Key == tomo.KeyEnter {
|
|
||||||
element.pressed = true
|
|
||||||
if element.core.HasImage() {
|
|
||||||
element.draw()
|
|
||||||
element.core.PushAll()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case tomo.EventMouseUp:
|
|
||||||
if !element.enabled { break }
|
|
||||||
|
|
||||||
mouseUpEvent := event.(tomo.EventMouseUp)
|
|
||||||
if mouseUpEvent.Button != tomo.ButtonLeft { break }
|
|
||||||
element.pressed = false
|
element.pressed = false
|
||||||
if element.core.HasImage() {
|
if element.core.HasImage() {
|
||||||
element.draw()
|
element.draw()
|
||||||
element.core.PushAll()
|
element.core.PushAll()
|
||||||
}
|
}
|
||||||
|
if element.onClick != nil {
|
||||||
within := image.Point { mouseUpEvent.X, mouseUpEvent.Y }.
|
|
||||||
In(element.Bounds())
|
|
||||||
|
|
||||||
if within && element.onClick != nil {
|
|
||||||
element.onClick()
|
element.onClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
case tomo.EventKeyUp:
|
|
||||||
keyDownEvent := event.(tomo.EventKeyUp)
|
|
||||||
if keyDownEvent.Key == tomo.KeyEnter && element.pressed {
|
|
||||||
element.pressed = false
|
|
||||||
if element.core.HasImage() {
|
|
||||||
element.draw()
|
|
||||||
element.core.PushAll()
|
|
||||||
}
|
|
||||||
if element.onClick != nil {
|
|
||||||
element.onClick()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case tomo.EventSelect:
|
|
||||||
element.core.SetSelected(true)
|
|
||||||
if element.core.HasImage() {
|
|
||||||
element.draw()
|
|
||||||
element.core.PushAll()
|
|
||||||
}
|
|
||||||
|
|
||||||
case tomo.EventDeselect:
|
|
||||||
element.core.SetSelected(false)
|
|
||||||
if element.core.HasImage() {
|
|
||||||
element.draw()
|
|
||||||
element.core.PushAll()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
}
|
||||||
|
|
||||||
|
func (element *Button) Selected () (selected bool) {
|
||||||
|
return element.selected
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Button) Select () {
|
||||||
|
element.core.RequestSelection()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Button) HandleSelection (
|
||||||
|
direction tomo.SelectionDirection,
|
||||||
|
) (
|
||||||
|
accepted bool,
|
||||||
|
) {
|
||||||
|
if direction == tomo.SelectionDirectionNeutral {
|
||||||
|
element.selected = true
|
||||||
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Button) HandleDeselection () {
|
||||||
|
element.selected = false
|
||||||
|
if element.core.HasImage() {
|
||||||
|
element.draw()
|
||||||
|
element.core.PushAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnClick sets the function to be called when the button is clicked.
|
// OnClick sets the function to be called when the button is clicked.
|
||||||
|
@ -114,17 +128,10 @@ func (element *Button) OnClick (callback func ()) {
|
||||||
element.onClick = callback
|
element.onClick = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select requests that this button's parent container send it a selection
|
|
||||||
// event.
|
|
||||||
func (element *Button) Select () {
|
|
||||||
element.core.Select()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetEnabled sets whether this button can be clicked or not.
|
// SetEnabled sets whether this button can be clicked or not.
|
||||||
func (element *Button) SetEnabled (enabled bool) {
|
func (element *Button) SetEnabled (enabled bool) {
|
||||||
if element.enabled == enabled { return }
|
if element.enabled == enabled { return }
|
||||||
element.enabled = enabled
|
element.enabled = enabled
|
||||||
element.core.SetSelectable(enabled)
|
|
||||||
if element.core.HasImage () {
|
if element.core.HasImage () {
|
||||||
element.draw()
|
element.draw()
|
||||||
element.core.PushAll()
|
element.core.PushAll()
|
||||||
|
|
|
@ -73,6 +73,13 @@ type CoreControl struct {
|
||||||
core *Core
|
core *Core
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequestSelection requests that the element's parent send it a selection
|
||||||
|
// event. If the request was granted, it returns true. If it was denied, it
|
||||||
|
// returns false.
|
||||||
|
func (control CoreControl) RequestSelection () (granted bool) {
|
||||||
|
return control.core.hooks.RunSelectionRequest()
|
||||||
|
}
|
||||||
|
|
||||||
// HasImage returns true if the core has an allocated image buffer, and false if
|
// HasImage returns true if the core has an allocated image buffer, and false if
|
||||||
// it doesn't.
|
// it doesn't.
|
||||||
func (control CoreControl) HasImage () (has bool) {
|
func (control CoreControl) HasImage () (has bool) {
|
||||||
|
|
Reference in New Issue
Block a user