4 Commits

Author SHA1 Message Date
552df5a9cd Add doc comments to the cut layout 2023-08-13 21:52:09 -04:00
e09761dd98 Added "cut" layout 2023-08-13 21:47:51 -04:00
992c242ba2 Add slider 2023-08-12 01:02:48 -04:00
ac70dce00f Fix separator 2023-08-12 01:02:26 -04:00
3 changed files with 396 additions and 12 deletions

212
layouts/cut.go Normal file
View File

@@ -0,0 +1,212 @@
package layouts
import "image"
import "git.tebibyte.media/tomo/tomo"
// Cut is a layout that can be divided into smaller and smaller sections.
type Cut struct {
branches [2]*Cut
expand [2]bool
vertical bool
}
// Vertical divides the layout in equal halves vertically.
func (this *Cut) Vertical () (top, bottom *Cut) {
this.fill()
this.even()
this.vertical = true
return this.Branches()
}
// Top divides the layout vertically, expanding the top half and contracting the
// bottom half.
func (this *Cut) Top () (top, bottom *Cut) {
this.fill()
this.first()
this.vertical = true
return this.Branches()
}
// Bottom divides the layout vertically, expanding the bottom half and
// contracting the top half.
func (this *Cut) Bottom () (top, bottom *Cut) {
this.fill()
this.second()
this.vertical = true
return this.Branches()
}
// Horizontal divides the layout in equal halves horizontally.
func (this *Cut) Horizontal () (top, bottom *Cut) {
this.fill()
this.even()
return this.Branches()
}
// Left divides the layout horizontally, expanding the left half and contracting
// the right half.
func (this *Cut) Left () (top, bottom *Cut) {
this.fill()
this.first()
return this.Branches()
}
// Right divides the layout horizontally, expanding the right half and
// contracting the left half.
func (this *Cut) Right () (top, bottom *Cut) {
this.fill()
this.second()
return this.Branches()
}
// Branches returns the two halves of this layout.
func (this *Cut) Branches () (first, second *Cut) {
return this.branches[0], this.branches[1]
}
func (this *Cut) real () bool {
return this != nil && this.branches[0] != nil && this.branches[1] != nil
}
func (this *Cut) fill () {
this.branches[0] = &Cut { }
this.branches[1] = &Cut { }
}
func (this *Cut) first () {
this.expand[0] = true
this.expand[1] = false
}
func (this *Cut) second () {
this.expand[0] = false
this.expand[1] = true
}
func (this *Cut) even () {
this.expand[0] = true
this.expand[1] = true
}
func (this *Cut) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
size, _ := this.minimumSize(hints, boxes)
return size
}
func (this *Cut) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
this.arrange(hints, boxes)
}
func (this *Cut) minimumSize (hints tomo.LayoutHints, boxes []tomo.Box) (image.Point, []tomo.Box) {
size := image.Point { }
for index, branch := range this.branches {
if len(boxes) == 0 { break }
var point image.Point
if branch.real() {
point, boxes = branch.minimumSize(hints, boxes)
} else {
point = boxes[0].MinimumSize()
boxes = boxes[1:]
}
if this.vertical {
if point.X > size.X { size.X = point.X }
if index > 0 { size.Y += hints.Gap.Y }
size.Y += point.Y
} else {
if point.Y > size.Y { size.Y = point.Y }
if index > 0 { size.X += hints.Gap.X }
size.X += point.X
}
}
return size, boxes
}
func (this *Cut) arrange (hints tomo.LayoutHints, boxes []tomo.Box) []tomo.Box {
// collect minimum sizes and physical endpoints
var minimums [2]image.Point
var leaves [2]tomo.Box
var nBranches int
remaining := boxes
for index, branch := range this.branches {
if branch.real() {
minimums[index], remaining = branch.minimumSize(hints, remaining)
} else {
if len(remaining) == 0 { break }
leaves[index] = remaining[0]
minimums[index] = remaining[0].MinimumSize()
remaining = remaining[1:]
}
nBranches ++
}
// determine the amount of space to divide among expanding branches
gaps := nBranches - 1
var freeSpace float64; if this.vertical {
freeSpace = float64(hints.Bounds.Dy() - hints.Gap.Y * gaps)
} else {
freeSpace = float64(hints.Bounds.Dx() - hints.Gap.X * gaps)
}
var nExpanding float64
for index, minimum := range minimums {
if this.expand[index] {
nExpanding ++
} else if this.vertical {
freeSpace -= float64(minimum.Y)
} else {
freeSpace -= float64(minimum.X)
}
}
expandingSize := freeSpace / nExpanding
// calculate the size and position of branches
var bounds [2]image.Rectangle
x := float64(hints.Bounds.Min.X)
y := float64(hints.Bounds.Min.Y)
for index, minimum := range minimums {
// get size along significant axis
var size float64; if this.expand[index] {
size = expandingSize
} else if this.vertical {
size = float64(minimum.Y)
} else {
size = float64(minimum.X)
}
// figure out bounds from size
if this.vertical {
bounds[index].Max = image.Pt (
int(hints.Bounds.Dx()),
int(size))
} else {
bounds[index].Max = image.Pt (
int(size),
int(hints.Bounds.Dy()))
}
bounds[index] = bounds[index].Add(image.Pt(int(x), int(y)))
// move along
if this.vertical {
y += float64(hints.Gap.Y) + size
} else {
x += float64(hints.Gap.X) + size
}
}
// apply the size and position
for index, bound := range bounds {
if leaves[index] != nil {
leaves[index].SetBounds(bound)
boxes = boxes[1:]
} else if this.branches[index] != nil {
newHints := hints
newHints.Bounds = bound
boxes = this.branches[index].arrange(newHints, boxes)
}
}
return boxes
}

View File

@@ -4,11 +4,15 @@ import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme" import "git.tebibyte.media/tomo/tomo/theme"
// Separator is a line for visually separating elements. // Separator is a line for visually separating elements.
type Separator tomo.Box type Separator struct {
tomo.Box
}
// NewSeparator creates a new text label. // NewSeparator creates a new separator line.
func NewSeparator () Separator { func NewSeparator () *Separator {
this := Separator(tomo.NewBox()) this := &Separator {
Box: tomo.NewBox(),
}
theme.Apply(this, theme.R("objects", "Separator", "")) theme.Apply(this, theme.R("objects", "Separator", ""))
return this return this
} }

184
slider.go
View File

@@ -1,38 +1,206 @@
package objects package objects
import "image"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/theme" import "git.tebibyte.media/tomo/tomo/theme"
import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event"
// UNDER CONSTRUCTION!
type Slider struct { type Slider struct {
tomo.ContainerBox tomo.ContainerBox
handle *SliderHandle handle *SliderHandle
vertical bool layout sliderLayout
dragging bool
dragOffset image.Point
on struct {
valueChange event.FuncBroadcaster
}
} }
type SliderHandle struct { type SliderHandle struct {
tomo.Box tomo.Box
} }
func newSlider (orient string) *Slider { func newSlider (orient string, value float64) *Slider {
this := &Slider { this := &Slider {
ContainerBox: tomo.NewContainerBox(), ContainerBox: tomo.NewContainerBox(),
handle: &SliderHandle { handle: &SliderHandle {
Box: tomo.NewBox(), Box: tomo.NewBox(),
}, },
vertical: orient == "vertical", layout: sliderLayout {
vertical: orient == "vertical",
},
} }
this.Add(this.handle) this.Add(this.handle)
this.SetFocusable(true)
this.SetPropagateEvents(false)
this.SetValue(value)
this.OnKeyDown(this.handleKeyDown)
this.OnMouseDown(this.handleMouseDown)
this.OnMouseUp(this.handleMouseUp)
this.OnMouseMove(this.handleMouseMove)
theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient)) theme.Apply(this.handle, theme.R("objects", "SliderHandle", orient))
theme.Apply(this, theme.R("objects", "Slider", orient)) theme.Apply(this, theme.R("objects", "Slider", orient))
return this return this
} }
func NewVerticalSlider () *Slider { func NewVerticalSlider (value float64) *Slider {
return newSlider("vertical") return newSlider("vertical", value)
} }
func NewHorizontalSlider () *Slider { func NewHorizontalSlider (value float64) *Slider {
return newSlider("horizontal") return newSlider("horizontal", value)
}
func (this *Slider) SetValue (value float64) {
if value < 0 { value = 0 }
if value > 1 { value = 1 }
if value == this.layout.value { return }
this.layout.value = value
this.SetLayout(this.layout)
this.on.valueChange.Broadcast()
}
func (this *Slider) Value () float64 {
return this.layout.value
}
func (this *Slider) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
func (this *Slider) handleKeyDown (key input.Key, numpad bool) {
switch key {
case input.KeyUp, input.KeyLeft:
if this.Modifiers().Alt {
this.SetValue(0)
} else {
this.SetValue(this.Value() - 0.05)
}
case input.KeyDown, input.KeyRight:
if this.Modifiers().Alt {
this.SetValue(1)
} else {
this.SetValue(this.Value() + 0.05)
}
case input.KeyHome:
this.SetValue(0)
case input.KeyEnd:
this.SetValue(1)
}
}
func (this *Slider) handleMouseDown (button input.Button) {
pointer := this.MousePosition()
handle := this.handle.Bounds()
var above, within bool
if pointer.In(handle) {
within = true
} else if this.layout.vertical {
above = pointer.Y < handle.Min.Y
} else {
above = pointer.X < handle.Min.X
}
switch button {
case input.ButtonLeft:
if within {
this.dragging = true
this.dragOffset =
pointer.Sub(this.handle.Bounds().Min).
Add(this.InnerBounds().Min)
this.drag()
} else {
this.dragOffset = this.fallbackDragOffset()
this.dragging = true
this.drag()
}
case input.ButtonMiddle:
if above {
this.SetValue(0)
} else {
this.SetValue(1)
}
case input.ButtonRight:
if above {
this.SetValue(this.Value() - 0.05)
} else {
this.SetValue(this.Value() + 0.05)
}
}
}
func (this *Slider) handleMouseUp (button input.Button) {
if button != input.ButtonLeft || !this.dragging { return }
this.dragging = false
}
func (this *Slider) handleMouseMove () {
if !this.dragging { return }
this.drag()
}
func (this *Slider) drag () {
pointer := this.MousePosition().Sub(this.dragOffset)
gutter := this.InnerBounds()
handle := this.handle.Bounds()
if this.layout.vertical {
this.SetValue (
float64(pointer.Y) /
float64(gutter.Dy() - handle.Dy()))
} else {
this.SetValue (
float64(pointer.X) /
float64(gutter.Dx() - handle.Dx()))
}
}
func (this *Slider) fallbackDragOffset () image.Point {
if this.layout.vertical {
return this.InnerBounds().Min.
Add(image.Pt(0, this.handle.Bounds().Dy() / 2))
} else {
return this.InnerBounds().Min.
Add(image.Pt(this.handle.Bounds().Dx() / 2, 0))
}
}
type sliderLayout struct {
vertical bool
value float64
}
func (sliderLayout) MinimumSize (hints tomo.LayoutHints, boxes []tomo.Box) image.Point {
if len(boxes) != 1 { return image.Pt(0, 0) }
return boxes[0].MinimumSize()
}
func (this sliderLayout) Arrange (hints tomo.LayoutHints, boxes []tomo.Box) {
if len(boxes) != 1 { return }
handle := image.Rectangle { Max: boxes[0].MinimumSize() }
gutter := hints.Bounds
if this.vertical {
height := gutter.Dy() - handle.Dy()
offset := int(float64(height) * this.value)
gutter.Max.X = handle.Max.X
boxes[0].SetBounds (
handle.
Add(image.Pt(0, offset)).
Add(gutter.Min))
} else {
width := gutter.Dx() - handle.Dx()
offset := int(float64(width) * this.value)
gutter.Max.Y = handle.Max.Y
boxes[0].SetBounds (
handle.
Add(image.Pt(offset, 0)).
Add(gutter.Min))
}
} }