9 Commits

10 changed files with 300 additions and 25 deletions

View File

@@ -7,13 +7,17 @@ import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
var buttonLayout = layouts.NewGrid([]bool { true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
var iconButtonLayout = layouts.NewGrid([]bool { false }, []bool { true })
var bothButtonLayout = layouts.NewGrid([]bool { false, true }, []bool { true })
// Button is a clickable button.
type Button struct {
tomo.ContainerBox
label *Label
icon *Icon
labelActive bool
on struct {
click event.FuncBroadcaster
}
@@ -27,8 +31,8 @@ func NewButton (text string) *Button {
}
theme.Apply(box, theme.R("objects", "Button", ""))
box.label.SetAlign(tomo.AlignMiddle, tomo.AlignMiddle)
box.Add(box.label)
box.SetLayout(buttonLayout)
box.SetText(text)
box.CaptureDND(true)
box.CaptureMouse(true)
@@ -44,6 +48,15 @@ func NewButton (text string) *Button {
// SetText sets the text of the button's label.
func (this *Button) SetText (text string) {
this.label.SetText(text)
if this.labelActive && text == "" {
this.Delete(this.label)
this.labelActive = false
}
if !this.labelActive && text != "" {
this.Add(this.label)
this.labelActive = true
}
this.applyLayout()
}
// SetIcon sets an icon for this button. Setting the icon to IconUnknown will
@@ -56,12 +69,10 @@ func (this *Button) SetIcon (id theme.Icon) {
}
this.icon = icon
if this.icon == nil {
this.SetLayout(buttonLayout)
} else {
this.SetLayout(iconButtonLayout)
if this.icon != nil {
this.Insert(this.icon, this.label)
}
this.applyLayout()
}
// OnClick specifies a function to be called when the button is clicked.
@@ -69,6 +80,16 @@ func (this *Button) OnClick (callback func ()) event.Cookie {
return this.on.click.Connect(callback)
}
func (this *Button) applyLayout () {
if this.labelActive && this.icon == nil {
this.SetLayout(buttonLayout)
} else if !this.labelActive && this.icon != nil {
this.SetLayout(iconButtonLayout)
} else {
this.SetLayout(bothButtonLayout)
}
}
func (this *Button) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
this.on.click.Broadcast()

69
checkbox.go Normal file
View File

@@ -0,0 +1,69 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
// Checkbox is a control that can be toggled.
type Checkbox struct {
tomo.Box
value bool
on struct {
valueChange event.FuncBroadcaster
}
}
// NewCheckbox creates a new checkbox with the specified value.
func NewCheckbox (value bool) *Checkbox {
box := &Checkbox {
Box: tomo.NewBox(),
}
theme.Apply(box, theme.R("objects", "Checkbox", ""))
box.SetValue(false)
box.OnMouseUp(box.handleMouseUp)
box.OnKeyUp(box.handleKeyUp)
box.SetFocusable(true)
return box
}
// SetValue sets the value of the checkbox.
func (this *Checkbox) SetValue (value bool) {
this.value = value
if this.value {
// TODO perhaps have IconStatusOkay/Cancel in actions, and have
// a status icon for checked checkboxes.
this.SetTexture(theme.IconStatusOkay.Texture(theme.IconSizeSmall))
} else {
this.SetTexture(nil)
}
}
// Toggle toggles the value of the checkbox between true and false.
func (this *Checkbox) Toggle () {
this.SetValue(!this.Value())
}
// Value returns the value of the checkbox.
func (this *Checkbox) Value () bool {
return this.value
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) {
if key != input.KeyEnter && key != input.Key(' ') { return }
this.Toggle()
}
func (this *Checkbox) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.Toggle()
}
}

View File

@@ -23,13 +23,23 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
return this
}
// NewOuterContainer creates a new container that has padding around it.
// NewOuterContainer creates a new container that has padding around it, as well
// as a solid background color. It is meant to be used as a root container for a
// window, tab pane, etc.
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)
theme.Apply(this, theme.R("objects", "Container", "outer"))
return this
}
// NewSunkenContainer creates a new container with a sunken style and padding
// around it. It is meant to be used as a root container for a ScrollContainer.
func NewSunkenContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)
theme.Apply(this, theme.R("objects", "Container", "sunken"))
return this
}
// NewInnerContainer creates a new container that has no padding around it.
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...)

View File

@@ -12,10 +12,10 @@ type Heading struct {
}
// NewHeading creates a new section heading. The level can be from 0 to 2.
func NewHeading (level int, text string) *Label {
func NewHeading (level int, text string) *Heading {
if level < 0 { level = 0 }
if level > 2 { level = 2 }
this := &Label { TextBox: tomo.NewTextBox() }
this := &Heading { TextBox: tomo.NewTextBox() }
theme.Apply(this, theme.R("objects", "Heading", fmt.Sprint(level)))
this.SetText(text)
return this

View File

@@ -13,6 +13,7 @@ type TextInput struct {
text []rune
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
}
}
@@ -47,6 +48,11 @@ func (this *TextInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input text.
func (this *TextInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
dot := this.Dot()
modifiers := this.Modifiers()
@@ -93,7 +99,10 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
}
this.Select(dot)
if changed { this.SetText(string(this.text)) }
if changed {
this.SetText(string(this.text))
this.on.edit.Broadcast()
}
}
func (this *TextInput) handleScroll (x, y float64) {

62
labelcheckbox.go Normal file
View File

@@ -0,0 +1,62 @@
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// LabelCheckbox is a checkbox with a label.
type LabelCheckbox struct {
tomo.ContainerBox
checkbox *Checkbox
label *Label
}
// NewLabelCheckbox creates a new labeled checkbox with the specified value and
// label text.
func NewLabelCheckbox (value bool, text string) *LabelCheckbox {
box := &LabelCheckbox {
ContainerBox: tomo.NewContainerBox(),
checkbox: NewCheckbox(value),
label: NewLabel(text),
}
theme.Apply(box, theme.R("objects", "LabelCheckbox", ""))
box.label.SetAlign(tomo.AlignStart, tomo.AlignMiddle)
box.Add(box.checkbox)
box.Add(box.label)
box.SetLayout(layouts.NewGrid([]bool { false, true }, []bool { true }))
box.OnMouseUp(box.handleMouseUp)
box.label.OnMouseUp(box.handleMouseUp)
return box
}
// SetValue sets the value of the checkbox.
func (this *LabelCheckbox) SetValue (value bool) {
this.checkbox.SetValue(value)
}
// Toggle toggles the value of the checkbox between true and false.
func (this *LabelCheckbox) Toggle () {
this.checkbox.Toggle()
}
// Value returns the value of the checkbox.
func (this *LabelCheckbox) Value () bool {
return this.checkbox.Value()
}
// OnValueChange specifies a function to be called when the checkbox's value
// changes.
func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
return this.checkbox.OnValueChange(callback)
}
func (this *LabelCheckbox) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {
this.checkbox.SetFocused(true)
this.checkbox.Toggle()
}
}

100
numberinput.go Normal file
View File

@@ -0,0 +1,100 @@
package objects
import "math"
import "strconv"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/objects/layouts"
// NumberInput is an editable text box which accepts only numbers, and has
// controls to increment and decrement its value.
type NumberInput struct {
tomo.ContainerBox
input *TextInput
increment *Button
decrement *Button
on struct {
enter event.FuncBroadcaster
edit event.FuncBroadcaster
}
}
// NewNumberInput creates a new number input with the specified value.
func NewNumberInput (value float64) *NumberInput {
box := &NumberInput {
ContainerBox: tomo.NewContainerBox(),
input: NewTextInput(""),
increment: NewButton(""),
decrement: NewButton(""),
}
theme.Apply(box, theme.R("objects", "NumberInput", ""))
box.Add(box.input)
box.Add(box.decrement)
box.Add(box.increment)
box.SetLayout(layouts.NewGrid([]bool { true, false, false }, []bool { true }))
box.increment.SetIcon(theme.IconActionIncrement)
box.decrement.SetIcon(theme.IconActionDecrement)
box.SetValue(value)
box.CaptureScroll(true)
box.CaptureKeyboard(true)
box.OnScroll(box.handleScroll)
box.OnKeyDown(box.handleKeyDown)
box.input.OnEnter(box.handleEnter)
box.input.OnEdit(box.on.edit.Broadcast)
box.increment.OnClick(func () { box.shift(1) })
box.decrement.OnClick(func () { box.shift(-1) })
return box
}
// SetValue sets the value of the input.
func (this *NumberInput) SetValue (value float64) {
this.input.SetText(strconv.FormatFloat(value, 'g', -1, 64))
}
// Value returns the value of the input.
func (this *NumberInput) Value () float64 {
value, _ := strconv.ParseFloat(this.input.Text(), 64)
return value
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *NumberInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// OnEdit specifies a function to be called when the user edits the input value.
func (this *NumberInput) OnEdit (callback func ()) event.Cookie {
return this.on.edit.Connect(callback)
}
func (this *NumberInput) shift (by int) {
this.SetValue(this.Value() + float64(by))
this.on.edit.Broadcast()
}
func (this *NumberInput) handleKeyDown (key input.Key, numpad bool) {
switch key {
case input.KeyUp: this.shift(1)
case input.KeyDown: this.shift(-1)
default: this.input.handleKeyDown(key, numpad)
}
}
func (this *NumberInput) handleScroll (x, y float64) {
if x == 0 {
this.shift(-int(math.Round(y)))
} else {
this.input.handleScroll(x, y)
}
}
func (this *NumberInput) handleEnter () {
this.SetValue(this.Value())
this.on.enter.Broadcast()
}

View File

@@ -98,9 +98,9 @@ func (this *Scrollbar) SetValue (value float64) {
position := trackLength * value
point := this.layout.linked.ContentBounds().Min
if this.layout.vertical {
point.Y = int(position)
point.Y = -int(position)
} else {
point.X = int(position)
point.X = -int(position)
}
this.layout.linked.ScrollTo(point)
@@ -173,15 +173,15 @@ func (this *Scrollbar) handleMouseDown (button input.Button) {
}
case input.ButtonMiddle:
if above {
this.scrollBy(-this.pageSize())
} else {
this.scrollBy(this.pageSize())
} else {
this.scrollBy(-this.pageSize())
}
case input.ButtonRight:
if above {
this.scrollBy(-this.stepSize())
} else {
this.scrollBy(this.stepSize())
} else {
this.scrollBy(-this.stepSize())
}
}
}
@@ -200,7 +200,7 @@ func (this *Scrollbar) handleScroll (x, y float64) {
if this.layout.linked == nil { return }
this.layout.linked.ScrollTo (
this.layout.linked.ContentBounds().Min.
Add(image.Pt(int(x), int(y))))
Sub(image.Pt(int(x), int(y))))
}
func (this *Scrollbar) drag () {
@@ -327,7 +327,7 @@ func (this scrollbarLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
} else {
handleOffset = image.Pt(int(handlePosition), 0)
}
handle = handle.Add(handleOffset).Add(gutter.Min)
handle = handle.Sub(handleOffset).Add(gutter.Min)
// place handle
boxes[0].SetBounds(handle)

View File

@@ -108,7 +108,7 @@ func (this *ScrollContainer) handleScroll (x, y float64) {
if this.layout.root == nil { return }
this.layout.root.ScrollTo (
this.layout.root.ContentBounds().Min.
Add(image.Pt(int(x), int(y))))
Sub(image.Pt(int(x), int(y))))
}
type scrollContainerLayout struct {

View File

@@ -15,7 +15,7 @@ type Slider struct {
dragOffset image.Point
on struct {
valueChange event.FuncBroadcaster
slide event.FuncBroadcaster
}
}
@@ -71,7 +71,6 @@ func (this *Slider) SetValue (value float64) {
if value == this.layout.value { return }
this.layout.value = value
this.SetLayout(this.layout)
this.on.valueChange.Broadcast()
}
// Value returns the value of the slider between 0 and 1.
@@ -79,10 +78,10 @@ func (this *Slider) Value () float64 {
return this.layout.value
}
// OnValueChange specifies a function to be called when the slider's value
// changes.
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
// OnValueChange specifies a function to be called when the user moves the
// slider.
func (this *Slider) OnSlide (callback func ()) event.Cookie {
return this.on.slide.Connect(callback)
}
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
@@ -139,14 +138,18 @@ func (this *Slider) handleMouseDown (button input.Button) {
case input.ButtonMiddle:
if above {
this.SetValue(0)
this.on.slide.Broadcast()
} else {
this.SetValue(1)
this.on.slide.Broadcast()
}
case input.ButtonRight:
if above {
this.SetValue(this.Value() - 0.05)
this.on.slide.Broadcast()
} else {
this.SetValue(this.Value() + 0.05)
this.on.slide.Broadcast()
}
}
}
@@ -176,6 +179,7 @@ func (this *Slider) drag () {
float64(pointer.X) /
float64(gutter.Dx() - handle.Dx()))
}
this.on.slide.Broadcast()
}
func (this *Slider) fallbackDragOffset () image.Point {