Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 611705fa0d | |||
| 16645eeeda | |||
| 3219cb712c | |||
| 7d14a25482 | |||
| e4857da22d | |||
| 114cbb346d | |||
| 43ec7a0311 | |||
| 3d28c8fea1 | |||
| 669c638fad | |||
| 2fe433991d | |||
| acec0f6222 | |||
| 0865c28965 | |||
| 2546c338ad | |||
| b3e7178176 | |||
| 080e4511f2 | |||
| f1ac74dcbc | |||
| ce0bc5be3b | |||
| eb0bf58961 | |||
| 8068036219 |
16
button.go
16
button.go
@@ -89,23 +89,23 @@ func (this *Button) applyLayout () {
|
||||
}
|
||||
|
||||
func (this *Button) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
if !isClickingKey(key) { return false }
|
||||
this.on.click.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if !isClickingKey(key) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Button) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.on.click.Broadcast()
|
||||
}
|
||||
|
||||
@@ -54,23 +54,23 @@ func (this *Checkbox) OnValueChange (callback func ()) event.Cookie {
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
if !isClickingKey(key) { return false }
|
||||
this.Toggle()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false}
|
||||
if !isClickingKey(key) { return false}
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Checkbox) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.Toggle()
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
import "git.tebibyte.media/tomo/objects/internal"
|
||||
|
||||
// ColorPicker allows the user to pick a color by controlling its HSBA
|
||||
// HSVAColorPicker allows the user to pick a color by controlling its HSVA
|
||||
// parameters.
|
||||
type ColorPicker struct {
|
||||
type HSVAColorPicker struct {
|
||||
tomo.ContainerBox
|
||||
value internal.HSVA
|
||||
|
||||
pickerMap *colorPickerMap
|
||||
pickerMap *hsvaColorPickerMap
|
||||
hueSlider *Slider
|
||||
alphaSlider *Slider
|
||||
|
||||
@@ -23,14 +23,14 @@ type ColorPicker struct {
|
||||
}
|
||||
}
|
||||
|
||||
// NewColorPicker creates a new color picker with the specified color.
|
||||
func NewColorPicker (value color.Color) *ColorPicker {
|
||||
picker := &ColorPicker {
|
||||
// NewHSVAColorPicker creates a new color picker with the specified color.
|
||||
func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
|
||||
picker := &HSVAColorPicker {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
}
|
||||
picker.SetRole(tomo.R("objects", "ColorPicker"))
|
||||
picker.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
|
||||
picker.pickerMap = newColorPickerMap(picker)
|
||||
picker.pickerMap = newHsvaColorPickerMap(picker)
|
||||
picker.Add(picker.pickerMap)
|
||||
|
||||
picker.hueSlider = NewVerticalSlider(0.0)
|
||||
@@ -44,7 +44,7 @@ func NewColorPicker (value color.Color) *ColorPicker {
|
||||
picker.alphaSlider = NewVerticalSlider(0.0)
|
||||
picker.Add(picker.alphaSlider)
|
||||
picker.alphaSlider.OnValueChange(func () {
|
||||
picker.value.A = uint8(picker.alphaSlider.Value() * 255)
|
||||
picker.value.A = uint16(picker.alphaSlider.Value() * 0xFFFF)
|
||||
picker.on.valueChange.Broadcast()
|
||||
picker.pickerMap.Invalidate()
|
||||
})
|
||||
@@ -55,37 +55,38 @@ func NewColorPicker (value color.Color) *ColorPicker {
|
||||
}
|
||||
|
||||
// Value returns the color of the picker.
|
||||
func (this *ColorPicker) Value () color.Color {
|
||||
func (this *HSVAColorPicker) Value () color.Color {
|
||||
return this.value
|
||||
}
|
||||
|
||||
// SetValue sets the color of the picker.
|
||||
func (this *ColorPicker) SetValue (value color.Color) {
|
||||
func (this *HSVAColorPicker) SetValue (value color.Color) {
|
||||
if value == nil { value = color.Transparent }
|
||||
this.value = internal.RGBAToHSVA(value.RGBA())
|
||||
this.value = internal.HSVAModel.Convert(value).(internal.HSVA)
|
||||
this.hueSlider.SetValue(this.value.H)
|
||||
this.alphaSlider.SetValue(float64(this.value.A) / 255)
|
||||
this.alphaSlider.SetValue(float64(this.value.A) / 0xFFFF)
|
||||
this.pickerMap.Invalidate()
|
||||
}
|
||||
|
||||
// OnValueChange specifies a function to be called when the user changes the
|
||||
// swatch's color.
|
||||
func (this *ColorPicker) OnValueChange (callback func ()) event.Cookie {
|
||||
func (this *HSVAColorPicker) OnValueChange (callback func ()) event.Cookie {
|
||||
return this.on.valueChange.Connect(callback)
|
||||
}
|
||||
|
||||
// RGBA satisfies the color.Color interface
|
||||
func (this *ColorPicker) RGBA () (r, g, b, a uint32) {
|
||||
func (this *HSVAColorPicker) RGBA () (r, g, b, a uint32) {
|
||||
return this.value.RGBA()
|
||||
}
|
||||
|
||||
type colorPickerMap struct {
|
||||
type hsvaColorPickerMap struct {
|
||||
tomo.CanvasBox
|
||||
dragging bool
|
||||
parent *ColorPicker
|
||||
parent *HSVAColorPicker
|
||||
}
|
||||
|
||||
func newColorPickerMap (parent *ColorPicker) *colorPickerMap {
|
||||
picker := &colorPickerMap {
|
||||
func newHsvaColorPickerMap (parent *HSVAColorPicker) *hsvaColorPickerMap {
|
||||
picker := &hsvaColorPickerMap {
|
||||
CanvasBox: tomo.NewCanvasBox(),
|
||||
parent: parent,
|
||||
}
|
||||
@@ -97,26 +98,26 @@ func newColorPickerMap (parent *ColorPicker) *colorPickerMap {
|
||||
return picker
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleButtonDown (button input.Button) bool {
|
||||
func (this *hsvaColorPickerMap) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
this.dragging = true
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleButtonUp (button input.Button) bool {
|
||||
func (this *hsvaColorPickerMap) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
this.dragging = false
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) handleMouseMove () bool {
|
||||
func (this *hsvaColorPickerMap) handleMouseMove () bool {
|
||||
if !this.dragging { return false }
|
||||
this.drag()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) drag () {
|
||||
func (this *hsvaColorPickerMap) drag () {
|
||||
pointer := this.Window().MousePosition()
|
||||
bounds := this.InnerBounds()
|
||||
this.parent.value.S = float64(pointer.X - bounds.Min.X) / float64(bounds.Dx())
|
||||
@@ -126,7 +127,7 @@ func (this *colorPickerMap) drag () {
|
||||
this.Invalidate()
|
||||
}
|
||||
|
||||
func (this *colorPickerMap) Draw (can canvas.Canvas) {
|
||||
func (this *hsvaColorPickerMap) Draw (can canvas.Canvas) {
|
||||
bounds := can.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
||||
@@ -137,7 +138,7 @@ func (this *colorPickerMap) Draw (can canvas.Canvas) {
|
||||
H: this.parent.value.H,
|
||||
S: float64(xx) / float64(bounds.Dx()),
|
||||
V: 1 - float64(yy) / float64(bounds.Dy()),
|
||||
A: 255,
|
||||
A: 0xFFFF,
|
||||
}
|
||||
|
||||
sPos := int( this.parent.value.S * float64(bounds.Dx()))
|
||||
|
||||
114
dropdown.go
Normal file
114
dropdown.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package objects
|
||||
|
||||
// import "image"
|
||||
import "git.tebibyte.media/tomo/tomo"
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
|
||||
// Dropdown is a non-editable text input that allows the user to pick a value
|
||||
// from a list.
|
||||
type Dropdown struct {
|
||||
tomo.ContainerBox
|
||||
label *Label
|
||||
|
||||
value string
|
||||
items []string
|
||||
menu *Menu
|
||||
|
||||
on struct {
|
||||
valueChange event.FuncBroadcaster
|
||||
}
|
||||
}
|
||||
|
||||
// NewDropdown creates a new dropdown input with the specified items
|
||||
func NewDropdown (items ...string) *Dropdown {
|
||||
dropdown := &Dropdown {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(""),
|
||||
}
|
||||
dropdown.SetRole(tomo.R("objects", "Dropdown"))
|
||||
dropdown.SetAttr(tomo.ALayout(layouts.Row { true, false }))
|
||||
dropdown.Add(dropdown.label)
|
||||
dropdown.Add(NewIcon(tomo.IconListChoose, tomo.IconSizeSmall))
|
||||
|
||||
dropdown.SetItems(items...)
|
||||
if len(items) > 0 {
|
||||
dropdown.SetValue(items[0])
|
||||
}
|
||||
|
||||
dropdown.SetInputMask(true)
|
||||
dropdown.OnButtonDown(dropdown.handleButtonDown)
|
||||
dropdown.OnButtonUp(dropdown.handleButtonUp)
|
||||
dropdown.OnKeyDown(dropdown.handleKeyDown)
|
||||
dropdown.OnKeyUp(dropdown.handleKeyUp)
|
||||
dropdown.SetFocusable(true)
|
||||
return dropdown
|
||||
}
|
||||
|
||||
// Value returns the value of the dropdown. This does not necissarily have to be
|
||||
// in the list of items.
|
||||
func (this *Dropdown) Value () string {
|
||||
return this.value
|
||||
}
|
||||
|
||||
// SetValue sets the value of the dropdown. This does not necissarily have to be
|
||||
// in the list of items.
|
||||
func (this *Dropdown) SetValue (value string) {
|
||||
this.value = value
|
||||
this.label.SetText(value)
|
||||
}
|
||||
|
||||
// SetItems sets the items from which the user is able to pick.
|
||||
func (this *Dropdown) SetItems (items ...string) {
|
||||
this.items = items
|
||||
}
|
||||
|
||||
// Choose creates a menu that allows the user to pick a value.
|
||||
func (this *Dropdown) Choose () {
|
||||
if this.menu != nil {
|
||||
this.menu.Close()
|
||||
}
|
||||
menu, err := NewAnchoredMenu(this, this.itemList()...)
|
||||
if err != nil { return }
|
||||
this.menu = menu
|
||||
menu.SetVisible(true)
|
||||
}
|
||||
|
||||
func (this *Dropdown) itemList () []tomo.Object {
|
||||
items := make([]tomo.Object, len(this.items))
|
||||
for index, value := range this.items {
|
||||
value := value
|
||||
item := NewMenuItem(value)
|
||||
item.OnClick(func () {
|
||||
this.SetValue(value)
|
||||
this.on.valueChange.Broadcast()
|
||||
})
|
||||
items[index] = item
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (this *Dropdown) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if !isClickingKey(key) { return false }
|
||||
this.Choose()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Dropdown) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if !isClickingKey(key) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Dropdown) handleButtonDown (button input.Button) bool {
|
||||
if !isClickingButton(button) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Dropdown) handleButtonUp (button input.Button) bool {
|
||||
if !isClickingButton(button) { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.Choose()
|
||||
}
|
||||
return true
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -2,4 +2,4 @@ module git.tebibyte.media/tomo/objects
|
||||
|
||||
go 1.20
|
||||
|
||||
require git.tebibyte.media/tomo/tomo v0.45.0
|
||||
require git.tebibyte.media/tomo/tomo v0.46.1
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1,2 +1,2 @@
|
||||
git.tebibyte.media/tomo/tomo v0.45.0 h1:fQH0WIPidW275hOq9dE6R7p064xG1RGx2QU68Avlr84=
|
||||
git.tebibyte.media/tomo/tomo v0.45.0/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
|
||||
git.tebibyte.media/tomo/tomo v0.46.1 h1:/8fT6I9l4TK529zokrThbNDHGRvUsNgif1Zs++0PBSQ=
|
||||
git.tebibyte.media/tomo/tomo v0.46.1/go.mod h1:WrtilgKB1y8O2Yu7X4mYcRiqOlPR8NuUnoA/ynkQWrs=
|
||||
|
||||
@@ -23,3 +23,9 @@ func NewHeading (level int, text string) *Heading {
|
||||
return this
|
||||
}
|
||||
|
||||
// NewMenuHeading creatss a new heading for use in menus.
|
||||
func NewMenuHeading (text string) *Heading {
|
||||
heading := NewHeading(0, text)
|
||||
heading.SetTag("menu", true)
|
||||
return heading
|
||||
}
|
||||
|
||||
1
icon.go
1
icon.go
@@ -33,6 +33,7 @@ func NewIcon (icon tomo.Icon, size tomo.IconSize) *Icon {
|
||||
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
|
||||
if this.icon == icon { return }
|
||||
this.icon = icon
|
||||
this.size = size
|
||||
this.setTexture(icon.Texture(size))
|
||||
}
|
||||
|
||||
|
||||
19
input.go
Normal file
19
input.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package objects
|
||||
|
||||
import "git.tebibyte.media/tomo/tomo/input"
|
||||
|
||||
func isClickingKey (key input.Key) bool {
|
||||
return key == input.KeyEnter || key == input.Key(' ')
|
||||
}
|
||||
|
||||
func isConfirmationKey (key input.Key) bool {
|
||||
return key == input.KeyEnter
|
||||
}
|
||||
|
||||
func isClickingButton (button input.Button) bool {
|
||||
return button == input.ButtonLeft
|
||||
}
|
||||
|
||||
func isMenuButton (button input.Button) bool {
|
||||
return button == input.ButtonLeft
|
||||
}
|
||||
@@ -1,13 +1,32 @@
|
||||
package internal
|
||||
|
||||
import "fmt"
|
||||
import "image/color"
|
||||
|
||||
// HSV represents a color with hue, saturation, and value components. Each
|
||||
// component C is in range 0 <= C <= 1.
|
||||
type HSV struct {
|
||||
H float64
|
||||
S float64
|
||||
V float64
|
||||
}
|
||||
|
||||
// HSVA is an HSV color with an added 8-bit alpha component. The alpha component
|
||||
// ranges from 0x0000 (fully transparent) to 0xFFFF (opaque), and has no bearing
|
||||
// on the other components.
|
||||
type HSVA struct {
|
||||
H float64
|
||||
S float64
|
||||
V float64
|
||||
A uint8
|
||||
A uint16
|
||||
}
|
||||
|
||||
func (hsva HSVA) RGBA () (r, g, b, a uint32) {
|
||||
var (
|
||||
HSVModel color.Model = color.ModelFunc(hsvModel)
|
||||
HSVAModel color.Model = color.ModelFunc(hsvaModel)
|
||||
)
|
||||
|
||||
func (hsv HSV) RGBA () (r, g, b, a uint32) {
|
||||
// Adapted from:
|
||||
// https://www.cs.rit.edu/~ncs/color/t_convert.html
|
||||
|
||||
@@ -15,34 +34,57 @@ func (hsva HSVA) RGBA () (r, g, b, a uint32) {
|
||||
return uint32(float64(0xFFFF) * x)
|
||||
}
|
||||
|
||||
ca := uint32(hsva.A) << 8
|
||||
s := clamp01(hsva.S)
|
||||
v := clamp01(hsva.V)
|
||||
s := clamp01(hsv.S)
|
||||
v := clamp01(hsv.V)
|
||||
if s == 0 {
|
||||
light := component(v)
|
||||
return light, light, light, ca
|
||||
return light, light, light, 0xFFFF
|
||||
}
|
||||
|
||||
h := clamp01(hsva.H) * 360
|
||||
h := clamp01(hsv.H) * 360
|
||||
sector := int(h / 60)
|
||||
// otherwise when given 1.0 for H, sector would overflow to 6
|
||||
if sector > 5 { sector = 5 }
|
||||
offset := (h / 60) - float64(sector)
|
||||
|
||||
fac := float64(hsva.A) / 255
|
||||
p := component(fac * v * (1 - s))
|
||||
q := component(fac * v * (1 - s * offset))
|
||||
t := component(fac * v * (1 - s * (1 - offset)))
|
||||
p := component(v * (1 - s))
|
||||
q := component(v * (1 - s * offset))
|
||||
t := component(v * (1 - s * (1 - offset)))
|
||||
va := component(v)
|
||||
|
||||
switch sector {
|
||||
case 0: return va, t, p, ca
|
||||
case 1: return q, va, p, ca
|
||||
case 2: return p, va, t, ca
|
||||
case 3: return p, q, va, ca
|
||||
case 4: return t, p, va, ca
|
||||
default: return va, p, q, ca
|
||||
case 0: return va, t, p, 0xFFFF
|
||||
case 1: return q, va, p, 0xFFFF
|
||||
case 2: return p, va, t, 0xFFFF
|
||||
case 3: return p, q, va, 0xFFFF
|
||||
case 4: return t, p, va, 0xFFFF
|
||||
default: return va, p, q, 0xFFFF
|
||||
}
|
||||
}
|
||||
|
||||
func (hsva HSVA) RGBA () (r, g, b, a uint32) {
|
||||
r, g, b, a = HSV {
|
||||
H: hsva.H,
|
||||
S: hsva.S,
|
||||
V: hsva.V,
|
||||
}.RGBA()
|
||||
a = uint32(hsva.A)
|
||||
// alpha premultiplication
|
||||
r = (r * a) / 0xFFFF
|
||||
g = (g * a) / 0xFFFF
|
||||
b = (b * a) / 0xFFFF
|
||||
return
|
||||
}
|
||||
|
||||
// Canon returns the color but with the H, S, and V fields are constrained to
|
||||
// the range 0.0-1.0
|
||||
func (hsv HSV) Canon () HSV {
|
||||
hsv.H = clamp01(hsv.H)
|
||||
hsv.S = clamp01(hsv.S)
|
||||
hsv.V = clamp01(hsv.V)
|
||||
return hsv
|
||||
}
|
||||
|
||||
// Canon returns the color but with the H, S, and V fields are constrained to
|
||||
// the range 0.0-1.0
|
||||
func (hsva HSVA) Canon () HSVA {
|
||||
@@ -58,7 +100,38 @@ func clamp01 (x float64) float64 {
|
||||
return x
|
||||
}
|
||||
|
||||
func RGBAToHSVA (r, g, b, a uint32) HSVA {
|
||||
func hsvModel (c color.Color) color.Color {
|
||||
switch c := c.(type) {
|
||||
case HSV: return c
|
||||
case HSVA: return HSV { H: c.H, S: c.S, V: c.V }
|
||||
default:
|
||||
r, g, b, a := c.RGBA()
|
||||
// alpha unpremultiplication
|
||||
r = (r / a) * 0xFFFF
|
||||
g = (g / a) * 0xFFFF
|
||||
b = (b / a) * 0xFFFF
|
||||
return rgbToHSV(r, g, b)
|
||||
}
|
||||
}
|
||||
|
||||
func hsvaModel (c color.Color) color.Color {
|
||||
switch c := c.(type) {
|
||||
case HSV: return HSVA { H: c.H, S: c.S, V: c.V, A: 0xFFFF }
|
||||
case HSVA: return c
|
||||
default:
|
||||
r, g, b, a := c.RGBA()
|
||||
hsv := rgbToHSV(r, g, b)
|
||||
|
||||
return HSVA {
|
||||
H: hsv.H,
|
||||
S: hsv.S,
|
||||
V: hsv.V,
|
||||
A: uint16(a),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func rgbToHSV (r, g, b uint32) HSV {
|
||||
// Adapted from:
|
||||
// https://www.cs.rit.edu/~ncs/color/t_convert.html
|
||||
|
||||
@@ -78,27 +151,81 @@ func RGBAToHSVA (r, g, b, a uint32) HSVA {
|
||||
if cg < minComponent { minComponent = cg }
|
||||
if cb < minComponent { minComponent = cb }
|
||||
|
||||
hsva := HSVA {
|
||||
hsv := HSV {
|
||||
V: maxComponent,
|
||||
A: uint8(a >> 8),
|
||||
}
|
||||
|
||||
delta := maxComponent - minComponent
|
||||
if delta == 0 {
|
||||
// hsva.S is undefined, so hue doesn't matter
|
||||
return hsva
|
||||
return hsv
|
||||
}
|
||||
hsva.S = delta / maxComponent
|
||||
hsv.S = delta / maxComponent
|
||||
|
||||
switch {
|
||||
case cr == maxComponent: hsva.H = (cg - cb) / delta
|
||||
case cg == maxComponent: hsva.H = 2 + (cb - cr) / delta
|
||||
case cb == maxComponent: hsva.H = 4 + (cr - cg) / delta
|
||||
case cr == maxComponent: hsv.H = (cg - cb) / delta
|
||||
case cg == maxComponent: hsv.H = 2 + (cb - cr) / delta
|
||||
case cb == maxComponent: hsv.H = 4 + (cr - cg) / delta
|
||||
}
|
||||
|
||||
hsva.H *= 60
|
||||
if hsva.H < 0 { hsva.H += 360 }
|
||||
hsva.H /= 360
|
||||
hsv.H *= 60
|
||||
if hsv.H < 0 { hsv.H += 360 }
|
||||
hsv.H /= 360
|
||||
|
||||
return hsva
|
||||
return hsv
|
||||
}
|
||||
|
||||
// FormatNRGBA formats an NRGBA value into a hex string.
|
||||
func FormatNRGBA (nrgba color.NRGBA) string {
|
||||
return fmt.Sprintf("%02X%02X%02X%02X", nrgba.R, nrgba.G, nrgba.B, nrgba.A)
|
||||
}
|
||||
|
||||
// ParseNRGBA parses an NRGBA value from a hex string. It can be of the format:
|
||||
// - RGB
|
||||
// - RGBA
|
||||
// - RRGGBB
|
||||
// - RRGGBBAA
|
||||
// If none of these are specified, this function will return an opaque black
|
||||
// color. Hex digits may either be upper case or lower case.
|
||||
func ParseNRGBA (str string) color.NRGBA {
|
||||
runes := []rune(str)
|
||||
c := color.NRGBA { A: 255 }
|
||||
switch len(runes) {
|
||||
case 3:
|
||||
c.R = fillOctet(hexDigit(runes[0]))
|
||||
c.G = fillOctet(hexDigit(runes[1]))
|
||||
c.B = fillOctet(hexDigit(runes[2]))
|
||||
case 4:
|
||||
c.R = fillOctet(hexDigit(runes[0]))
|
||||
c.G = fillOctet(hexDigit(runes[1]))
|
||||
c.B = fillOctet(hexDigit(runes[2]))
|
||||
c.A = fillOctet(hexDigit(runes[3]))
|
||||
case 6:
|
||||
c.R = hexOctet(runes[0], runes[1])
|
||||
c.G = hexOctet(runes[2], runes[3])
|
||||
c.B = hexOctet(runes[4], runes[5])
|
||||
case 8:
|
||||
c.R = hexOctet(runes[0], runes[1])
|
||||
c.G = hexOctet(runes[2], runes[3])
|
||||
c.B = hexOctet(runes[4], runes[5])
|
||||
c.A = hexOctet(runes[6], runes[7])
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func hexDigit (r rune) uint8 {
|
||||
switch {
|
||||
case r >= '0' && r <= '9': return uint8(r - '0')
|
||||
case r >= 'A' && r <= 'F': return uint8(r - 'A') + 10
|
||||
case r >= 'a' && r <= 'f': return uint8(r - 'a') + 10
|
||||
default: return 0
|
||||
}
|
||||
}
|
||||
|
||||
func fillOctet (low uint8) uint8 {
|
||||
return low << 4 | low
|
||||
}
|
||||
|
||||
func hexOctet (high, low rune) uint8 {
|
||||
return hexDigit(high) << 4 | hexDigit(low)
|
||||
}
|
||||
|
||||
@@ -55,12 +55,12 @@ func (this *LabelCheckbox) OnValueChange (callback func ()) event.Cookie {
|
||||
}
|
||||
|
||||
func (this *LabelCheckbox) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *LabelCheckbox) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.checkbox.SetFocused(true)
|
||||
this.checkbox.Toggle()
|
||||
|
||||
30
menu.go
30
menu.go
@@ -18,22 +18,27 @@ type Menu struct {
|
||||
}
|
||||
|
||||
// NewMenu creates a new menu with the specified items. The menu will appear
|
||||
// directly under the anchor Object. If the anchor is nil, it will appear
|
||||
// directly under the mouse pointer instead.
|
||||
func NewMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
|
||||
// directly under the mouse pointer.
|
||||
func NewMenu (parent tomo.Window, items ...tomo.Object) (*Menu, error) {
|
||||
bounds := (image.Rectangle { }).Add(parent.MousePosition())
|
||||
return newMenu(parent, bounds, items...)
|
||||
}
|
||||
|
||||
// NewAnchoredMenu creates a new menu with the specified items. The menu will
|
||||
// appear directly under the anchor.
|
||||
func NewAnchoredMenu (anchor tomo.Object, items ...tomo.Object) (*Menu, error) {
|
||||
parent := anchor.GetBox().Window()
|
||||
bounds := menuBoundsFromAnchor(anchor)
|
||||
return newMenu(parent, bounds, items...)
|
||||
}
|
||||
|
||||
func newMenu (parent tomo.Window, bounds image.Rectangle, items ...tomo.Object) (*Menu, error) {
|
||||
menu := &Menu { }
|
||||
if anchor == nil {
|
||||
// TODO: *actually* put it under the mouse
|
||||
window, err := tomo.NewWindow(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
} else {
|
||||
menu.bounds = menuBoundsFromAnchor(anchor)
|
||||
menu.parent = anchor.GetBox().Window()
|
||||
menu.bounds = bounds
|
||||
menu.parent = parent
|
||||
window, err := menu.parent.NewMenu(menu.bounds)
|
||||
if err != nil { return nil, err }
|
||||
menu.Window = window
|
||||
}
|
||||
|
||||
menu.rootContainer = tomo.NewContainerBox()
|
||||
menu.rootContainer.SetAttr(tomo.ALayout(layouts.ContractVertical))
|
||||
@@ -74,6 +79,7 @@ func (this *Menu) TearOff () {
|
||||
this.Window.Close()
|
||||
|
||||
this.rootContainer.Remove(this.tearLine)
|
||||
this.rootContainer.SetTag("torn", true)
|
||||
|
||||
this.Window = window
|
||||
this.Window.SetRoot(this.rootContainer)
|
||||
|
||||
10
menuitem.go
10
menuitem.go
@@ -20,10 +20,15 @@ type MenuItem struct {
|
||||
|
||||
// NewMenuItem creates a new menu item with the specified text.
|
||||
func NewMenuItem (text string) *MenuItem {
|
||||
return NewIconMenuItem(tomo.IconUnknown, text)
|
||||
}
|
||||
|
||||
// NewIconMenuItem creates a new menu item with the specified icon and text.
|
||||
func NewIconMenuItem (icon tomo.Icon, text string) *MenuItem {
|
||||
box := &MenuItem {
|
||||
ContainerBox: tomo.NewContainerBox(),
|
||||
label: NewLabel(text),
|
||||
icon: NewIcon("", tomo.IconSizeSmall),
|
||||
icon: NewIcon(icon, tomo.IconSizeSmall),
|
||||
}
|
||||
box.SetRole(tomo.R("objects", "MenuItem"))
|
||||
box.label.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||
@@ -49,8 +54,7 @@ func (this *MenuItem) SetText (text string) {
|
||||
// SetIcon sets an icon for this item. Setting the icon to IconUnknown will
|
||||
// remove it.
|
||||
func (this *MenuItem) SetIcon (id tomo.Icon) {
|
||||
if this.icon != nil { this.Remove(this.icon) }
|
||||
this.Insert(NewIcon(id, tomo.IconSizeSmall), this.label)
|
||||
this.icon.SetIcon(id, tomo.IconSizeSmall)
|
||||
}
|
||||
|
||||
// OnClick specifies a function to be called when the menu item is clicked.
|
||||
|
||||
57
swatch.go
57
swatch.go
@@ -8,6 +8,7 @@ import "git.tebibyte.media/tomo/tomo/input"
|
||||
import "git.tebibyte.media/tomo/tomo/event"
|
||||
import "git.tebibyte.media/tomo/tomo/canvas"
|
||||
import "git.tebibyte.media/tomo/objects/layouts"
|
||||
import "git.tebibyte.media/tomo/objects/internal"
|
||||
|
||||
// Swatch displays a color, allowing the user to edit it by clicking on it.
|
||||
type Swatch struct {
|
||||
@@ -91,26 +92,38 @@ func (this *Swatch) Choose () {
|
||||
|
||||
committed := false
|
||||
|
||||
colorPicker := NewColorPicker(this.Value())
|
||||
colorPicker.OnValueChange(func () {
|
||||
this.userSetValue(colorPicker.Value())
|
||||
})
|
||||
|
||||
hexInput := NewTextInput("TODO")
|
||||
|
||||
colorPicker := NewHSVAColorPicker(this.Value())
|
||||
colorMemory := this.value
|
||||
hexInput := NewTextInput("")
|
||||
hexInput.SetFocused(true)
|
||||
cancelButton := NewButton("Cancel")
|
||||
cancelButton.SetIcon(tomo.IconDialogCancel)
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
|
||||
updateHexInput := func () {
|
||||
nrgba := color.NRGBAModel.Convert(colorPicker.Value()).(color.NRGBA)
|
||||
hexInput.SetValue(internal.FormatNRGBA(nrgba))
|
||||
}
|
||||
updateHexInput()
|
||||
commit := func () {
|
||||
committed = true
|
||||
window.Close()
|
||||
}
|
||||
colorPicker.OnValueChange(func () {
|
||||
this.userSetValue(colorPicker.Value())
|
||||
updateHexInput()
|
||||
})
|
||||
hexInput.OnConfirm(commit)
|
||||
hexInput.OnValueChange(func () {
|
||||
nrgba := internal.ParseNRGBA(hexInput.Value())
|
||||
this.userSetValue(nrgba)
|
||||
colorPicker.SetValue(nrgba)
|
||||
})
|
||||
cancelButton.OnClick(func () {
|
||||
window.Close()
|
||||
})
|
||||
okButton := NewButton("OK")
|
||||
okButton.SetFocused(true)
|
||||
okButton.SetIcon(tomo.IconDialogOkay)
|
||||
okButton.OnClick(func () {
|
||||
committed = true
|
||||
window.Close()
|
||||
})
|
||||
okButton.OnClick(commit)
|
||||
|
||||
controlRow := NewInnerContainer (
|
||||
layouts.ContractHorizontal,
|
||||
@@ -158,23 +171,23 @@ func (this *Swatch) userSetValue (value color.Color) {
|
||||
}
|
||||
|
||||
func (this *Swatch) handleKeyDown (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if key != input.KeyEnter && key != input.Key(' ') { return false }
|
||||
if !isClickingKey(key) { return false }
|
||||
this.Choose()
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleKeyUp (key input.Key, numberPad bool) bool {
|
||||
if !isClickingKey(key) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleButtonDown (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func (this *Swatch) handleButtonUp (button input.Button) bool {
|
||||
if button != input.ButtonLeft { return false }
|
||||
if !isClickingButton(button) { return false }
|
||||
if this.Window().MousePosition().In(this.Bounds()) {
|
||||
this.Choose()
|
||||
}
|
||||
|
||||
@@ -126,10 +126,10 @@ type tab struct {
|
||||
func (this *tab) setActive (active bool) {
|
||||
if active {
|
||||
this.SetRole(tomo.R("objects", "Tab"))
|
||||
this.SetTag("active", false)
|
||||
this.SetTag("active", true)
|
||||
} else {
|
||||
this.SetRole(tomo.R("objects", "Tab"))
|
||||
this.SetTag("active", true)
|
||||
this.SetTag("active", false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
30
textinput.go
30
textinput.go
@@ -31,15 +31,25 @@ func NewTextInput (text string) *TextInput {
|
||||
return this
|
||||
}
|
||||
|
||||
// SetText sets the text content of the input.
|
||||
func (this *TextInput) SetText (text string) {
|
||||
// SetValue sets the text content of the input.
|
||||
func (this *TextInput) SetValue (text string) {
|
||||
this.text = []rune(text)
|
||||
this.TextBox.SetText(text)
|
||||
}
|
||||
|
||||
// Value returns the text content of the input.
|
||||
func (this *TextInput) Value () string {
|
||||
return string(this.text)
|
||||
}
|
||||
|
||||
// SetText sets the text content of the input.
|
||||
func (this *TextInput) SetText (text string) {
|
||||
this.SetValue(text)
|
||||
}
|
||||
|
||||
// Text returns the text content of the input.
|
||||
func (this *TextInput) Text () string {
|
||||
return string(this.text)
|
||||
return this.Value()
|
||||
}
|
||||
|
||||
// OnConfirm specifies a function to be called when the user presses enter
|
||||
@@ -77,7 +87,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
} ()
|
||||
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
case isConfirmationKey(key):
|
||||
this.on.confirm.Broadcast()
|
||||
return true
|
||||
case key == input.KeyBackspace:
|
||||
@@ -88,11 +98,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
this.text, dot = text.Delete(this.text, dot, word)
|
||||
changed = true
|
||||
return true
|
||||
case key == input.Key('a') && modifiers.Control:
|
||||
dot.Start = 0
|
||||
dot.End = len(this.text)
|
||||
return true
|
||||
case key.Printable():
|
||||
case key.Printable() && !modifiers.Control:
|
||||
this.text, dot = text.Type(this.text, dot, rune(key))
|
||||
changed = true
|
||||
return true
|
||||
@@ -104,15 +110,13 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
||||
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
|
||||
modifiers := this.Window().Modifiers()
|
||||
switch {
|
||||
case key == input.KeyEnter:
|
||||
case isConfirmationKey(key):
|
||||
return true
|
||||
case key == input.KeyBackspace:
|
||||
return true
|
||||
case key == input.KeyDelete:
|
||||
return true
|
||||
case key == input.Key('a') && modifiers.Control:
|
||||
return true
|
||||
case key.Printable():
|
||||
case key.Printable() && !modifiers.Control:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user