mirror of
https://github.com/vsariola/sointu.git
synced 2026-03-30 10:43:02 -04:00
feat(tracker): enum-style values and menus to choose one option
This commit is contained in:
parent
ca4b87d43d
commit
4bb5df9c87
@ -4,6 +4,7 @@ import (
|
||||
"iter"
|
||||
"math"
|
||||
"math/bits"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Enabler is an interface that defines a single Enabled() method, which is used
|
||||
@ -111,7 +112,9 @@ type (
|
||||
// length, etc. It is a wrapper around an IntValue interface that provides
|
||||
// methods to manipulate the value, but Int guard that all changes are
|
||||
// within the range of the underlying IntValue implementation and that
|
||||
// SetValue is not called when the value is unchanged.
|
||||
// SetValue is not called when the value is unchanged. The IntValue can
|
||||
// optionally implement the StringOfer interface to provide custom string
|
||||
// representations of the integer values.
|
||||
Int struct {
|
||||
value IntValue
|
||||
}
|
||||
@ -121,6 +124,10 @@ type (
|
||||
SetValue(int) (changed bool)
|
||||
Range() RangeInclusive
|
||||
}
|
||||
|
||||
StringOfer interface {
|
||||
StringOf(value int) string
|
||||
}
|
||||
)
|
||||
|
||||
func MakeInt(value IntValue) Int { return Int{value} }
|
||||
@ -152,6 +159,27 @@ func (v Int) Value() int {
|
||||
return v.value.Value()
|
||||
}
|
||||
|
||||
func (v Int) Values(yield func(int, string) bool) {
|
||||
r := v.Range()
|
||||
for i := r.Min; i <= r.Max; i++ {
|
||||
s := v.StringOf(i)
|
||||
if !yield(i, s) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v Int) String() string {
|
||||
return v.StringOf(v.Value())
|
||||
}
|
||||
|
||||
func (v *Int) StringOf(value int) string {
|
||||
if s, ok := v.value.(StringOfer); ok {
|
||||
return s.StringOf(value)
|
||||
}
|
||||
return strconv.Itoa(value)
|
||||
}
|
||||
|
||||
// String
|
||||
|
||||
type (
|
||||
|
||||
@ -65,6 +65,20 @@ func (v *detectorWeighting) SetValue(value int) bool {
|
||||
func (v *detectorWeighting) Range() RangeInclusive {
|
||||
return RangeInclusive{0, int(NumWeightingTypes) - 1}
|
||||
}
|
||||
func (v *detectorWeighting) StringOf(value int) string {
|
||||
switch WeightingType(value) {
|
||||
case KWeighting:
|
||||
return "K-weighting (LUFS)"
|
||||
case AWeighting:
|
||||
return "A-weighting"
|
||||
case CWeighting:
|
||||
return "C-weighting"
|
||||
case NoWeighting:
|
||||
return "No weighting (RMS)"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
type WeightingType int
|
||||
|
||||
|
||||
@ -283,6 +283,10 @@ func (t *Tracker) KeyEvent(e key.Event, gtx C) {
|
||||
t.FocusNext(gtx, false)
|
||||
case "FocusNextInto":
|
||||
t.FocusNext(gtx, true)
|
||||
case "MIDIRefresh":
|
||||
t.MIDI().Refresh().Do()
|
||||
case "ToggleMIDIInputtingNotes":
|
||||
t.MIDI().InputtingNotes().Toggle()
|
||||
default:
|
||||
if len(action) > 4 && action[:4] == "Note" {
|
||||
val, err := strconv.Atoi(string(action[4:]))
|
||||
|
||||
@ -5,23 +5,30 @@ import (
|
||||
"image/color"
|
||||
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"github.com/vsariola/sointu/tracker"
|
||||
)
|
||||
|
||||
type (
|
||||
// MenuState is the part of the menu that needs to be retained between frames.
|
||||
MenuState struct {
|
||||
visible bool
|
||||
tags []bool
|
||||
hover int
|
||||
hoverOk bool
|
||||
list layout.List
|
||||
scrollBar ScrollBar
|
||||
|
||||
tag bool
|
||||
visible bool
|
||||
|
||||
itemTmp []menuItem
|
||||
}
|
||||
|
||||
// MenuStyle is the style for a menu that is stored in the theme.yml.
|
||||
@ -34,139 +41,267 @@ type (
|
||||
Height unit.Dp
|
||||
}
|
||||
|
||||
// ActionMenuItem is a menu item that has an icon, text, shortcut and an action.
|
||||
ActionMenuItem struct {
|
||||
Icon []byte
|
||||
Text string
|
||||
Shortcut string
|
||||
Action tracker.Action
|
||||
}
|
||||
|
||||
// MenuWidget has a Layout method to display a menu
|
||||
MenuWidget struct {
|
||||
Theme *Theme
|
||||
State *MenuState
|
||||
Style *MenuStyle
|
||||
}
|
||||
|
||||
// MenuButton displayes a button with text that opens a menu when clicked.
|
||||
MenuButton struct {
|
||||
Theme *Theme
|
||||
Title string
|
||||
Style *ButtonStyle
|
||||
Clickable *Clickable
|
||||
MenuState *MenuState
|
||||
Width unit.Dp
|
||||
State *MenuState
|
||||
Style *MenuStyle
|
||||
PopupStyle *PopupStyle
|
||||
}
|
||||
)
|
||||
|
||||
func Menu(th *Theme, state *MenuState) MenuWidget {
|
||||
return MenuWidget{
|
||||
Theme: th,
|
||||
State: state,
|
||||
Style: &th.Menu.Main,
|
||||
func Menu(state *MenuState) MenuWidget { return MenuWidget{State: state} }
|
||||
func (w MenuWidget) WithStyle(style *MenuStyle) MenuWidget { w.Style = style; return w }
|
||||
func (w MenuWidget) WithPopupStyle(style *PopupStyle) MenuWidget { w.PopupStyle = style; return w }
|
||||
|
||||
func (ms *MenuState) Tags(level int, yield TagYieldFunc) bool {
|
||||
if ms.visible {
|
||||
return yield(level, &ms.tag)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func MenuItem(act tracker.Action, text, shortcut string, icon []byte) ActionMenuItem {
|
||||
return ActionMenuItem{
|
||||
// MenuChild describes one or more menu items; if MenuChild is an Action or
|
||||
// Bool, it's one item per child, but Ints are treated as enumerations and
|
||||
// create one item per different possible values of the int.
|
||||
type MenuChild struct {
|
||||
Icon []byte
|
||||
Text string
|
||||
Shortcut string
|
||||
|
||||
kind menuChildKind
|
||||
action tracker.Action
|
||||
bool tracker.Bool
|
||||
int tracker.Int
|
||||
widget layout.Widget // these should be passive separators and such
|
||||
}
|
||||
|
||||
type menuChildKind int
|
||||
|
||||
const (
|
||||
menuChildAction menuChildKind = iota
|
||||
menuChildBool
|
||||
menuChildInt
|
||||
menuChildList
|
||||
menuChildDivider
|
||||
)
|
||||
|
||||
func ActionMenuChild(act tracker.Action, text, shortcut string, icon []byte) MenuChild {
|
||||
return MenuChild{
|
||||
Icon: icon,
|
||||
Text: text,
|
||||
Shortcut: shortcut,
|
||||
Action: act,
|
||||
|
||||
kind: menuChildAction,
|
||||
action: act,
|
||||
}
|
||||
}
|
||||
|
||||
func MenuBtn(th *Theme, ms *MenuState, cl *Clickable, title string) MenuButton {
|
||||
return MenuButton{
|
||||
Theme: th,
|
||||
Title: title,
|
||||
Clickable: cl,
|
||||
MenuState: ms,
|
||||
Style: &th.Button.Menu,
|
||||
func BoolMenuChild(b tracker.Bool, text, shortcut string, icon []byte) MenuChild {
|
||||
return MenuChild{
|
||||
Icon: icon,
|
||||
Text: text,
|
||||
Shortcut: shortcut,
|
||||
|
||||
kind: menuChildBool,
|
||||
bool: b,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MenuWidget) Layout(gtx C, items ...ActionMenuItem) D {
|
||||
func IntMenuChild(i tracker.Int, icon []byte) MenuChild {
|
||||
return MenuChild{
|
||||
Icon: icon,
|
||||
kind: menuChildInt,
|
||||
int: i,
|
||||
}
|
||||
}
|
||||
|
||||
func DividerMenuChild() MenuChild { return MenuChild{kind: menuChildDivider} }
|
||||
|
||||
// Layout the menu with the given items
|
||||
func (m MenuWidget) Layout(gtx C, children ...MenuChild) D {
|
||||
t := TrackerFromContext(gtx)
|
||||
if m.Style == nil {
|
||||
m.Style = &t.Theme.Menu.Main
|
||||
}
|
||||
// unfortunately, there was no way to include items into the MenuWidget
|
||||
// without causing heap escapes, so they are passed as a parameter to the Layout
|
||||
m.update(gtx, items...)
|
||||
popup := Popup(m.Theme, &m.State.visible)
|
||||
popup.Style = &m.Theme.Popup.Menu
|
||||
return popup.Layout(gtx, func(gtx C) D {
|
||||
m.State.itemTmp = m.State.itemTmp[:0]
|
||||
for i, c := range children {
|
||||
switch c.kind {
|
||||
case menuChildAction:
|
||||
m.State.itemTmp = append(m.State.itemTmp, menuItem{childIndex: i, icon: c.Icon, text: c.Text, shortcut: c.Shortcut, enabled: c.enabled()})
|
||||
case menuChildBool:
|
||||
mi := menuItem{childIndex: i, text: c.Text, shortcut: c.Shortcut, enabled: c.enabled()}
|
||||
if c.bool.Value() {
|
||||
mi.icon = c.Icon
|
||||
}
|
||||
m.State.itemTmp = append(m.State.itemTmp, mi)
|
||||
case menuChildInt:
|
||||
for v := c.int.Range().Min; v <= c.int.Range().Max; v++ {
|
||||
mi := menuItem{childIndex: i, text: c.int.StringOf(v), value: v, enabled: c.enabled()}
|
||||
if c.int.Value() == v {
|
||||
mi.icon = c.Icon
|
||||
}
|
||||
if v == c.int.Range().Min {
|
||||
mi.shortcut = c.Shortcut
|
||||
}
|
||||
m.State.itemTmp = append(m.State.itemTmp, mi)
|
||||
}
|
||||
case menuChildDivider:
|
||||
m.State.itemTmp = append(m.State.itemTmp, menuItem{childIndex: i, divider: true})
|
||||
}
|
||||
}
|
||||
m.update(gtx, children, m.State.itemTmp)
|
||||
listItem := func(gtx C, i int) D {
|
||||
item := m.State.itemTmp[i]
|
||||
if widget := children[item.childIndex].widget; widget != nil {
|
||||
return widget(gtx)
|
||||
}
|
||||
var icon *widget.Icon
|
||||
if item.icon != nil {
|
||||
icon = t.Theme.Icon(item.icon)
|
||||
}
|
||||
iconColor := m.Style.Text.Color
|
||||
iconInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(6)}
|
||||
textLabel := Label(t.Theme, &m.Style.Text, item.text)
|
||||
shortcutLabel := Label(t.Theme, &m.Style.Shortcut, item.shortcut)
|
||||
if !item.enabled {
|
||||
iconColor = m.Style.Disabled
|
||||
textLabel.Color = m.Style.Disabled
|
||||
shortcutLabel.Color = m.Style.Disabled
|
||||
}
|
||||
shortcutInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(12), Bottom: unit.Dp(2), Top: unit.Dp(2)}
|
||||
fg := func(gtx C) D {
|
||||
if item.divider {
|
||||
d := Divider{Thickness: 1, Axis: layout.Horizontal, Margin: 5}
|
||||
return d.Layout(gtx)
|
||||
}
|
||||
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return iconInset.Layout(gtx, func(gtx C) D {
|
||||
if icon == nil {
|
||||
return D{}
|
||||
}
|
||||
p := gtx.Dp(unit.Dp(m.Style.Text.TextSize))
|
||||
gtx.Constraints.Min = image.Pt(p, p)
|
||||
return icon.Layout(gtx, iconColor)
|
||||
})
|
||||
}),
|
||||
layout.Rigid(textLabel.Layout),
|
||||
layout.Flexed(1, func(gtx C) D { return D{Size: image.Pt(gtx.Constraints.Max.X, 1)} }),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return shortcutInset.Layout(gtx, shortcutLabel.Layout)
|
||||
}),
|
||||
)
|
||||
}
|
||||
bg := func(gtx C) D {
|
||||
rect := clip.Rect{Max: image.Pt(gtx.Constraints.Min.X, gtx.Constraints.Min.Y)}
|
||||
if item.enabled && m.State.hoverOk && m.State.hover == i {
|
||||
paint.FillShape(gtx.Ops, m.Style.Hover, rect.Op())
|
||||
}
|
||||
if item.enabled {
|
||||
area := rect.Push(gtx.Ops)
|
||||
event.Op(gtx.Ops, &m.State.tags[i])
|
||||
area.Pop()
|
||||
}
|
||||
return D{Size: rect.Max}
|
||||
}
|
||||
return layout.Background{}.Layout(gtx, bg, fg)
|
||||
}
|
||||
menuList := func(gtx C) D {
|
||||
gtx.Constraints.Max.X = gtx.Dp(m.Style.Width)
|
||||
gtx.Constraints.Max.Y = gtx.Dp(m.Style.Height)
|
||||
r := clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops)
|
||||
event.Op(gtx.Ops, &m.State.tag)
|
||||
r.Pop()
|
||||
m.State.list.Axis = layout.Vertical
|
||||
m.State.scrollBar.Axis = layout.Vertical
|
||||
return layout.Stack{Alignment: layout.SE}.Layout(gtx,
|
||||
layout.Expanded(func(gtx C) D { return m.State.list.Layout(gtx, len(m.State.itemTmp), listItem) }),
|
||||
layout.Expanded(func(gtx C) D {
|
||||
return m.State.list.Layout(gtx, len(items), func(gtx C, i int) D {
|
||||
defer op.Offset(image.Point{}).Push(gtx.Ops).Pop()
|
||||
var macro op.MacroOp
|
||||
item := &items[i]
|
||||
if i == m.State.hover-1 && item.Action.Enabled() {
|
||||
macro = op.Record(gtx.Ops)
|
||||
}
|
||||
icon := m.Theme.Icon(item.Icon)
|
||||
iconColor := m.Style.Text.Color
|
||||
iconInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(6)}
|
||||
textLabel := Label(m.Theme, &m.Style.Text, item.Text)
|
||||
shortcutLabel := Label(m.Theme, &m.Style.Shortcut, item.Shortcut)
|
||||
if !item.Action.Enabled() {
|
||||
iconColor = m.Style.Disabled
|
||||
textLabel.Color = m.Style.Disabled
|
||||
shortcutLabel.Color = m.Style.Disabled
|
||||
}
|
||||
shortcutInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(12), Bottom: unit.Dp(2), Top: unit.Dp(2)}
|
||||
dims := layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return iconInset.Layout(gtx, func(gtx C) D {
|
||||
p := gtx.Dp(unit.Dp(m.Style.Text.TextSize))
|
||||
gtx.Constraints.Min = image.Pt(p, p)
|
||||
return icon.Layout(gtx, iconColor)
|
||||
})
|
||||
}),
|
||||
layout.Rigid(textLabel.Layout),
|
||||
layout.Flexed(1, func(gtx C) D { return D{Size: image.Pt(gtx.Constraints.Max.X, 1)} }),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
return shortcutInset.Layout(gtx, shortcutLabel.Layout)
|
||||
}),
|
||||
)
|
||||
if i == m.State.hover-1 && item.Action.Enabled() {
|
||||
recording := macro.Stop()
|
||||
paint.FillShape(gtx.Ops, m.Style.Hover, clip.Rect{
|
||||
Max: image.Pt(dims.Size.X, dims.Size.Y),
|
||||
}.Op())
|
||||
recording.Add(gtx.Ops)
|
||||
}
|
||||
if item.Action.Enabled() {
|
||||
rect := image.Rect(0, 0, dims.Size.X, dims.Size.Y)
|
||||
area := clip.Rect(rect).Push(gtx.Ops)
|
||||
event.Op(gtx.Ops, &m.State.tags[i])
|
||||
area.Pop()
|
||||
}
|
||||
return dims
|
||||
})
|
||||
}),
|
||||
layout.Expanded(func(gtx C) D {
|
||||
return m.State.scrollBar.Layout(gtx, &m.Theme.ScrollBar, len(items), &m.State.list.Position)
|
||||
return m.State.scrollBar.Layout(gtx, &t.Theme.ScrollBar, len(m.State.itemTmp), &m.State.list.Position)
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
popup := Popup(t.Theme, &m.State.visible).WithStyle(m.PopupStyle)
|
||||
return popup.Layout(gtx, menuList)
|
||||
|
||||
}
|
||||
|
||||
func (m *MenuWidget) update(gtx C, items ...ActionMenuItem) {
|
||||
for i, item := range items {
|
||||
type menuItem struct {
|
||||
childIndex int
|
||||
value int
|
||||
icon []byte
|
||||
text, shortcut string
|
||||
enabled bool
|
||||
divider bool
|
||||
}
|
||||
|
||||
func (m *MenuWidget) update(gtx C, children []MenuChild, items []menuItem) {
|
||||
// handle keyboard events for the menu
|
||||
for {
|
||||
ev, ok := gtx.Event(
|
||||
key.FocusFilter{Target: &m.State.tag},
|
||||
key.Filter{Focus: &m.State.tag, Name: key.NameUpArrow},
|
||||
key.Filter{Focus: &m.State.tag, Name: key.NameDownArrow},
|
||||
key.Filter{Focus: &m.State.tag, Name: key.NameEnter},
|
||||
key.Filter{Focus: &m.State.tag, Name: key.NameReturn},
|
||||
)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
switch e := ev.(type) {
|
||||
case key.Event:
|
||||
if e.State != key.Press {
|
||||
continue
|
||||
}
|
||||
switch e.Name {
|
||||
case key.NameUpArrow:
|
||||
if !m.State.hoverOk {
|
||||
m.State.hover = 0 // if nothing is selected, select the first item before starting to move backwards
|
||||
}
|
||||
for i := 1; i < len(items); i++ {
|
||||
idx := (m.State.hover - i + len(items)) % len(items)
|
||||
child := &children[items[idx].childIndex]
|
||||
if child.enabled() {
|
||||
m.State.hover = idx
|
||||
m.State.hoverOk = true
|
||||
break
|
||||
}
|
||||
}
|
||||
case key.NameDownArrow:
|
||||
if !m.State.hoverOk {
|
||||
m.State.hover = len(items) - 1 // if nothing is selected, select the last item before starting to move backwards
|
||||
}
|
||||
for i := 1; i < len(items); i++ {
|
||||
idx := (m.State.hover + i) % len(items)
|
||||
child := &children[items[idx].childIndex]
|
||||
if child.enabled() {
|
||||
m.State.hover = idx
|
||||
m.State.hoverOk = true
|
||||
break
|
||||
}
|
||||
}
|
||||
case key.NameEnter, key.NameReturn:
|
||||
if m.State.hoverOk && m.State.hover >= 0 && m.State.hover < len(items) {
|
||||
m.activateItem(items[m.State.hover], children)
|
||||
}
|
||||
}
|
||||
case key.FocusEvent:
|
||||
if !m.State.hoverOk {
|
||||
m.State.hover = 0
|
||||
}
|
||||
m.State.hoverOk = e.Focus
|
||||
}
|
||||
}
|
||||
for i := range items {
|
||||
// make sure we have a tag for every item
|
||||
for len(m.State.tags) <= i {
|
||||
m.State.tags = append(m.State.tags, false)
|
||||
}
|
||||
// handle pointer events for this item
|
||||
for {
|
||||
ev, ok := gtx.Event(pointer.Filter{
|
||||
Target: &m.State.tags[i],
|
||||
Kinds: pointer.Press | pointer.Enter | pointer.Leave,
|
||||
})
|
||||
ev, ok := gtx.Event(pointer.Filter{Target: &m.State.tags[i], Kinds: pointer.Press | pointer.Enter | pointer.Leave})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
@ -176,27 +311,112 @@ func (m *MenuWidget) update(gtx C, items ...ActionMenuItem) {
|
||||
}
|
||||
switch e.Kind {
|
||||
case pointer.Press:
|
||||
item.Action.Do()
|
||||
m.State.visible = false
|
||||
m.activateItem(items[i], children)
|
||||
case pointer.Enter:
|
||||
m.State.hover = i + 1
|
||||
m.State.hover = i
|
||||
m.State.hoverOk = true
|
||||
if !gtx.Focused(&m.State.tag) {
|
||||
gtx.Execute(key.FocusCmd{Tag: &m.State.tag})
|
||||
}
|
||||
case pointer.Leave:
|
||||
if m.State.hover == i+1 {
|
||||
m.State.hover = 0
|
||||
if m.State.hover == i {
|
||||
m.State.hoverOk = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (mb MenuButton) Layout(gtx C, items ...ActionMenuItem) D {
|
||||
func (m *MenuWidget) activateItem(item menuItem, children []MenuChild) {
|
||||
if item.childIndex < 0 || item.childIndex >= len(children) {
|
||||
return
|
||||
}
|
||||
child := &children[item.childIndex]
|
||||
if !child.enabled() {
|
||||
return
|
||||
}
|
||||
switch child.kind {
|
||||
case menuChildAction:
|
||||
child.action.Do()
|
||||
case menuChildBool:
|
||||
child.bool.Toggle()
|
||||
case menuChildInt:
|
||||
child.int.SetValue(item.value)
|
||||
}
|
||||
m.State.visible = false
|
||||
}
|
||||
|
||||
func (c *MenuChild) enabled() bool {
|
||||
switch c.kind {
|
||||
case menuChildAction:
|
||||
return c.action.Enabled()
|
||||
case menuChildBool:
|
||||
return c.bool.Enabled()
|
||||
case menuChildDivider:
|
||||
return false // the dividers are passive separators
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// MenuButton displays a button with text that opens a menu when clicked.
|
||||
type MenuButton struct {
|
||||
Title string
|
||||
BtnStyle *ButtonStyle
|
||||
MenuStyle *MenuStyle
|
||||
PopupStyle *PopupStyle
|
||||
Clickable *Clickable
|
||||
MenuState *MenuState
|
||||
Width unit.Dp
|
||||
}
|
||||
|
||||
func MenuBtn(ms *MenuState, cl *Clickable, title string) MenuButton {
|
||||
return MenuButton{MenuState: ms, Clickable: cl, Title: title}
|
||||
}
|
||||
|
||||
func (mb MenuButton) WithBtnStyle(style *ButtonStyle) MenuButton { mb.BtnStyle = style; return mb }
|
||||
func (mb MenuButton) WithMenuStyle(style *MenuStyle) MenuButton { mb.MenuStyle = style; return mb }
|
||||
func (mb MenuButton) WithPopupStyle(style *PopupStyle) MenuButton { mb.PopupStyle = style; return mb }
|
||||
|
||||
func (mb MenuButton) Layout(gtx C, children ...MenuChild) D {
|
||||
for mb.Clickable.Clicked(gtx) {
|
||||
mb.MenuState.visible = true
|
||||
gtx.Execute(key.FocusCmd{Tag: &mb.MenuState.tag})
|
||||
}
|
||||
btn := Btn(mb.Theme, mb.Style, mb.Clickable, mb.Title, "")
|
||||
t := TrackerFromContext(gtx)
|
||||
if mb.BtnStyle == nil {
|
||||
mb.BtnStyle = &t.Theme.Button.Menu
|
||||
}
|
||||
if mb.PopupStyle == nil {
|
||||
mb.PopupStyle = &t.Theme.Popup.Menu
|
||||
}
|
||||
btn := Btn(t.Theme, mb.BtnStyle, mb.Clickable, mb.Title, "")
|
||||
dims := btn.Layout(gtx)
|
||||
defer op.Offset(image.Pt(0, dims.Size.Y)).Push(gtx.Ops).Pop()
|
||||
m := Menu(mb.Theme, mb.MenuState)
|
||||
m.Layout(gtx, items...)
|
||||
if mb.MenuState.visible {
|
||||
defer op.Offset(image.Pt(0, dims.Size.Y)).Push(gtx.Ops).Pop()
|
||||
m := Menu(mb.MenuState).WithPopupStyle(mb.PopupStyle)
|
||||
m.Layout(gtx, children...)
|
||||
}
|
||||
return dims
|
||||
}
|
||||
|
||||
type Divider struct {
|
||||
Thickness unit.Dp
|
||||
Axis layout.Axis
|
||||
Margin unit.Dp
|
||||
}
|
||||
|
||||
func (d *Divider) Layout(gtx C) D {
|
||||
thicknessPx := max(gtx.Dp(d.Thickness), 1)
|
||||
if d.Axis == layout.Horizontal {
|
||||
return layout.Inset{Top: d.Margin, Bottom: d.Margin}.Layout(gtx, func(gtx C) D {
|
||||
paint.FillShape(gtx.Ops, color.NRGBA{255, 255, 255, 12}, clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, thicknessPx)).Op())
|
||||
return D{Size: image.Pt(gtx.Constraints.Max.X, thicknessPx)}
|
||||
})
|
||||
} else {
|
||||
return layout.Inset{Left: d.Margin, Right: d.Margin}.Layout(gtx, func(gtx C) D {
|
||||
paint.FillShape(gtx.Ops, color.NRGBA{255, 255, 255, 12}, clip.Rect(image.Rect(0, 0, thicknessPx, gtx.Constraints.Max.X)).Op())
|
||||
return D{Size: image.Pt(thicknessPx, gtx.Constraints.Max.X)}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -178,7 +178,6 @@ func (te *NoteEditor) layoutButtons(gtx C, t *Tracker) D {
|
||||
}
|
||||
effectBtn := ToggleBtn(t.Track().Effect(), t.Theme, te.EffectBtn, "Hex", "Input notes as hex values")
|
||||
uniqueBtn := ToggleIconBtn(t.Note().UniquePatterns(), t.Theme, te.UniqueBtn, icons.ToggleStarBorder, icons.ToggleStar, te.uniqueOffTip, te.uniqueOnTip)
|
||||
midiInBtn := ToggleBtn(t.MIDI().InputtingNotes(), t.Theme, te.TrackMidiInBtn, "MIDI", "Input notes from MIDI keyboard")
|
||||
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
|
||||
layout.Rigid(func(gtx C) D { return layout.Dimensions{Size: image.Pt(gtx.Dp(unit.Dp(12)), 0)} }),
|
||||
layout.Rigid(addSemitoneBtn.Layout),
|
||||
@ -193,7 +192,6 @@ func (te *NoteEditor) layoutButtons(gtx C, t *Tracker) D {
|
||||
layout.Rigid(layout.Spacer{Width: 4}.Layout),
|
||||
layout.Rigid(trackVoicesInsetted),
|
||||
layout.Rigid(splitTrackBtn.Layout),
|
||||
layout.Rigid(midiInBtn.Layout),
|
||||
layout.Flexed(1, func(gtx C) D { return layout.Dimensions{Size: gtx.Constraints.Min} }),
|
||||
layout.Rigid(deleteTrackBtn.Layout),
|
||||
layout.Rigid(newTrackBtn.Layout))
|
||||
@ -276,7 +274,6 @@ func (te *NoteEditor) layoutTracks(gtx C, t *Tracker) D {
|
||||
cursor := te.scrollTable.Table.Cursor()
|
||||
drawSelection := cursor != te.scrollTable.Table.Cursor2()
|
||||
selection := te.scrollTable.Table.Range()
|
||||
hasTrackMidiIn := t.MIDI().InputtingNotes().Value()
|
||||
|
||||
patternNoOp := colorOp(gtx, t.Theme.NoteEditor.PatternNo.Color)
|
||||
uniqueOp := colorOp(gtx, t.Theme.NoteEditor.Unique.Color)
|
||||
@ -298,9 +295,6 @@ func (te *NoteEditor) layoutTracks(gtx C, t *Tracker) D {
|
||||
if gtx.Focused(te.scrollTable) {
|
||||
c = t.Theme.Cursor.Active
|
||||
}
|
||||
if hasTrackMidiIn {
|
||||
c = t.Theme.Cursor.ActiveAlt
|
||||
}
|
||||
te.paintColumnCell(gtx, x, t, c)
|
||||
}
|
||||
|
||||
|
||||
@ -6,15 +6,17 @@ import (
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/unit"
|
||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||
)
|
||||
|
||||
type (
|
||||
OscilloscopeState struct {
|
||||
onceBtn *Clickable
|
||||
wrapBtn *Clickable
|
||||
lengthInBeatsNumber *NumericUpDownState
|
||||
triggerChannelNumber *NumericUpDownState
|
||||
plot *Plot
|
||||
onceBtn *Clickable
|
||||
wrapBtn *Clickable
|
||||
triggerBtn *Clickable
|
||||
lengthInBeatsNumber *NumericUpDownState
|
||||
triggerMenuState *MenuState
|
||||
plot *Plot
|
||||
}
|
||||
|
||||
Oscilloscope struct {
|
||||
@ -25,11 +27,12 @@ type (
|
||||
|
||||
func NewOscilloscope() *OscilloscopeState {
|
||||
return &OscilloscopeState{
|
||||
plot: NewPlot(plotRange{0, 1}, plotRange{-1, 1}, 0),
|
||||
onceBtn: new(Clickable),
|
||||
wrapBtn: new(Clickable),
|
||||
lengthInBeatsNumber: NewNumericUpDownState(),
|
||||
triggerChannelNumber: NewNumericUpDownState(),
|
||||
plot: NewPlot(plotRange{0, 1}, plotRange{-1, 1}, 0),
|
||||
onceBtn: new(Clickable),
|
||||
wrapBtn: new(Clickable),
|
||||
lengthInBeatsNumber: NewNumericUpDownState(),
|
||||
triggerBtn: new(Clickable),
|
||||
triggerMenuState: &MenuState{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +48,6 @@ func (s *Oscilloscope) Layout(gtx C) D {
|
||||
leftSpacer := layout.Spacer{Width: unit.Dp(6), Height: unit.Dp(24)}.Layout
|
||||
rightSpacer := layout.Spacer{Width: unit.Dp(6)}.Layout
|
||||
|
||||
triggerChannel := NumUpDown(t.Scope().TriggerChannel(), s.Theme, s.State.triggerChannelNumber, "Trigger channel")
|
||||
lengthInBeats := NumUpDown(t.Scope().LengthInBeats(), s.Theme, s.State.lengthInBeatsNumber, "Buffer length in beats")
|
||||
|
||||
onceBtn := ToggleBtn(t.Scope().Once(), s.Theme, s.State.onceBtn, "Once", "Trigger once on next event")
|
||||
@ -107,7 +109,11 @@ func (s *Oscilloscope) Layout(gtx C) D {
|
||||
layout.Rigid(Label(s.Theme, &s.Theme.SongPanel.RowHeader, "Trigger").Layout),
|
||||
layout.Flexed(1, func(gtx C) D { return D{Size: gtx.Constraints.Min} }),
|
||||
layout.Rigid(onceBtn.Layout),
|
||||
layout.Rigid(triggerChannel.Layout),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
triggerBtn := MenuBtn(s.State.triggerMenuState, s.State.triggerBtn, t.Scope().TriggerChannel().String()).
|
||||
WithBtnStyle(&t.Theme.Button.Text).WithPopupStyle(&t.Theme.Popup.ContextMenu)
|
||||
return triggerBtn.Layout(gtx, IntMenuChild(t.Scope().TriggerChannel(), icons.NavigationCheck))
|
||||
}),
|
||||
layout.Rigid(rightSpacer),
|
||||
)
|
||||
}),
|
||||
|
||||
@ -38,6 +38,8 @@ func Popup(th *Theme, visible *bool) PopupWidget {
|
||||
}
|
||||
}
|
||||
|
||||
func (s PopupWidget) WithStyle(style *PopupStyle) PopupWidget { s.Style = style; return s }
|
||||
|
||||
func (s PopupWidget) Layout(gtx C, contents layout.Widget) D {
|
||||
s.update(gtx)
|
||||
|
||||
|
||||
@ -41,6 +41,8 @@ type SongPanel struct {
|
||||
Step *NumericUpDownState
|
||||
SongLength *NumericUpDownState
|
||||
|
||||
weightingMenuState *MenuState
|
||||
|
||||
List *layout.List
|
||||
ScrollBar *ScrollBar
|
||||
|
||||
@ -79,6 +81,8 @@ func NewSongPanel(tr *Tracker) *SongPanel {
|
||||
List: &layout.List{Axis: layout.Vertical},
|
||||
ScrollBar: &ScrollBar{Axis: layout.Vertical},
|
||||
|
||||
weightingMenuState: new(MenuState),
|
||||
|
||||
SpectrumState: NewSpectrumState(),
|
||||
SpectrumScaleBar: &ScaleBar{Axis: layout.Vertical, BarSize: 10, Size: 300},
|
||||
ScopeScaleBar: &ScaleBar{Axis: layout.Vertical, BarSize: 10, Size: 300},
|
||||
@ -87,9 +91,6 @@ func NewSongPanel(tr *Tracker) *SongPanel {
|
||||
}
|
||||
|
||||
func (s *SongPanel) Update(gtx C, t *Tracker) {
|
||||
for s.WeightingTypeBtn.Clicked(gtx) {
|
||||
t.Model.Detector().Weighting().SetValue((t.Detector().Weighting().Value() + 1) % int(tracker.NumWeightingTypes))
|
||||
}
|
||||
for s.OversamplingBtn.Clicked(gtx) {
|
||||
t.Model.Detector().Oversampling().SetValue(!t.Detector().Oversampling().Value())
|
||||
}
|
||||
@ -113,19 +114,8 @@ func (t *SongPanel) layoutSongOptions(gtx C) D {
|
||||
tr := TrackerFromContext(gtx)
|
||||
paint.FillShape(gtx.Ops, tr.Theme.SongPanel.Bg, clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Op())
|
||||
|
||||
var weightingTxt string
|
||||
switch tracker.WeightingType(tr.Model.Detector().Weighting().Value()) {
|
||||
case tracker.KWeighting:
|
||||
weightingTxt = "K-weight (LUFS)"
|
||||
case tracker.AWeighting:
|
||||
weightingTxt = "A-weight"
|
||||
case tracker.CWeighting:
|
||||
weightingTxt = "C-weight"
|
||||
case tracker.NoWeighting:
|
||||
weightingTxt = "No weight (RMS)"
|
||||
}
|
||||
|
||||
weightingBtn := Btn(tr.Theme, &tr.Theme.Button.Text, t.WeightingTypeBtn, weightingTxt, "")
|
||||
weightingBtn := MenuBtn(t.weightingMenuState, t.WeightingTypeBtn, tr.Detector().Weighting().String()).
|
||||
WithBtnStyle(&tr.Theme.Button.Text).WithPopupStyle(&tr.Theme.Popup.ContextMenu)
|
||||
|
||||
oversamplingTxt := "Sample peak"
|
||||
if tr.Model.Detector().Oversampling().Value() {
|
||||
@ -236,7 +226,7 @@ func (t *SongPanel) layoutSongOptions(gtx C) D {
|
||||
}),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
gtx.Constraints.Min.X = 0
|
||||
return weightingBtn.Layout(gtx)
|
||||
return weightingBtn.Layout(gtx, IntMenuChild(tr.Detector().Weighting(), icons.NavigationCheck))
|
||||
}),
|
||||
)
|
||||
},
|
||||
@ -465,8 +455,6 @@ type MenuBar struct {
|
||||
Clickables []Clickable
|
||||
MenuStates []MenuState
|
||||
|
||||
midiMenuItems []ActionMenuItem
|
||||
|
||||
panicHint string
|
||||
PanicBtn *Clickable
|
||||
}
|
||||
@ -478,11 +466,6 @@ func NewMenuBar(tr *Tracker) *MenuBar {
|
||||
PanicBtn: new(Clickable),
|
||||
panicHint: makeHint("Panic", " (%s)", "PanicToggle"),
|
||||
}
|
||||
for input := range tr.MIDI().InputDevices {
|
||||
ret.midiMenuItems = append(ret.midiMenuItems,
|
||||
MenuItem(tr.MIDI().Open(input), input, "", icons.ImageControlPoint),
|
||||
)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
@ -492,50 +475,65 @@ func (t *MenuBar) Layout(gtx C) D {
|
||||
gtx.Constraints.Min.Y = gtx.Dp(unit.Dp(36))
|
||||
|
||||
flex := layout.Flex{Axis: layout.Horizontal, Alignment: layout.End}
|
||||
fileBtn := MenuBtn(tr.Theme, &t.MenuStates[0], &t.Clickables[0], "File")
|
||||
fileBtn := MenuBtn(&t.MenuStates[0], &t.Clickables[0], "File")
|
||||
fileFC := layout.Rigid(func(gtx C) D {
|
||||
items := [...]ActionMenuItem{
|
||||
MenuItem(tr.Song().New(), "New Song", keyActionMap["NewSong"], icons.ContentClear),
|
||||
MenuItem(tr.Song().Open(), "Open Song", keyActionMap["OpenSong"], icons.FileFolder),
|
||||
MenuItem(tr.Song().Save(), "Save Song", keyActionMap["SaveSong"], icons.ContentSave),
|
||||
MenuItem(tr.Song().SaveAs(), "Save Song As...", keyActionMap["SaveSongAs"], icons.ContentSave),
|
||||
MenuItem(tr.Song().Export(), "Export Wav...", keyActionMap["ExportWav"], icons.ImageAudiotrack),
|
||||
MenuItem(tr.RequestQuit(), "Quit", keyActionMap["Quit"], icons.ActionExitToApp),
|
||||
items := [...]MenuChild{
|
||||
ActionMenuChild(tr.Song().New(), "New Song", keyActionMap["NewSong"], icons.ContentClear),
|
||||
ActionMenuChild(tr.Song().Open(), "Open Song", keyActionMap["OpenSong"], icons.FileFolder),
|
||||
ActionMenuChild(tr.Song().Save(), "Save Song", keyActionMap["SaveSong"], icons.ContentSave),
|
||||
ActionMenuChild(tr.Song().SaveAs(), "Save Song As...", keyActionMap["SaveSongAs"], icons.ContentSave),
|
||||
DividerMenuChild(),
|
||||
ActionMenuChild(tr.Song().Export(), "Export Wav...", keyActionMap["ExportWav"], icons.ImageAudiotrack),
|
||||
DividerMenuChild(),
|
||||
ActionMenuChild(tr.RequestQuit(), "Quit", keyActionMap["Quit"], icons.ActionExitToApp),
|
||||
}
|
||||
if !canQuit {
|
||||
return fileBtn.Layout(gtx, items[:len(items)-1]...)
|
||||
return fileBtn.Layout(gtx, items[:len(items)-2]...)
|
||||
}
|
||||
return fileBtn.Layout(gtx, items[:]...)
|
||||
})
|
||||
editBtn := MenuBtn(tr.Theme, &t.MenuStates[1], &t.Clickables[1], "Edit")
|
||||
editBtn := MenuBtn(&t.MenuStates[1], &t.Clickables[1], "Edit")
|
||||
editFC := layout.Rigid(func(gtx C) D {
|
||||
return editBtn.Layout(gtx,
|
||||
MenuItem(tr.History().Undo(), "Undo", keyActionMap["Undo"], icons.ContentUndo),
|
||||
MenuItem(tr.History().Redo(), "Redo", keyActionMap["Redo"], icons.ContentRedo),
|
||||
MenuItem(tr.Order().RemoveUnusedPatterns(), "Remove unused data", keyActionMap["RemoveUnused"], icons.ImageCrop),
|
||||
ActionMenuChild(tr.History().Undo(), "Undo", keyActionMap["Undo"], icons.ContentUndo),
|
||||
ActionMenuChild(tr.History().Redo(), "Redo", keyActionMap["Redo"], icons.ContentRedo),
|
||||
DividerMenuChild(),
|
||||
ActionMenuChild(tr.Order().RemoveUnusedPatterns(), "Remove unused data", keyActionMap["RemoveUnused"], icons.ImageCrop),
|
||||
)
|
||||
})
|
||||
midiBtn := MenuBtn(tr.Theme, &t.MenuStates[2], &t.Clickables[2], "MIDI")
|
||||
midiBtn := MenuBtn(&t.MenuStates[2], &t.Clickables[2], "MIDI")
|
||||
midiFC := layout.Rigid(func(gtx C) D {
|
||||
return midiBtn.Layout(gtx, t.midiMenuItems...)
|
||||
return midiBtn.Layout(gtx,
|
||||
ActionMenuChild(tr.MIDI().Refresh(), "Refresh port list", keyActionMap["MIDIRefresh"], icons.NavigationRefresh),
|
||||
BoolMenuChild(tr.MIDI().InputtingNotes(), "Use for note input", keyActionMap["ToggleMIDIInputtingNotes"], icons.NavigationCheck),
|
||||
DividerMenuChild(),
|
||||
IntMenuChild(tr.MIDI().Input(), icons.NavigationCheck),
|
||||
)
|
||||
})
|
||||
helpBtn := MenuBtn(tr.Theme, &t.MenuStates[3], &t.Clickables[3], "?")
|
||||
helpBtn := MenuBtn(&t.MenuStates[3], &t.Clickables[3], "?")
|
||||
helpFC := layout.Rigid(func(gtx C) D {
|
||||
return helpBtn.Layout(gtx,
|
||||
MenuItem(tr.ShowManual(), "Manual", keyActionMap["ShowManual"], icons.AVLibraryBooks),
|
||||
MenuItem(tr.AskHelp(), "Ask help", keyActionMap["AskHelp"], icons.ActionHelp),
|
||||
MenuItem(tr.ReportBug(), "Report bug", keyActionMap["ReportBug"], icons.ActionBugReport),
|
||||
MenuItem(tr.ShowLicense(), "License", keyActionMap["ShowLicense"], icons.ActionCopyright))
|
||||
ActionMenuChild(tr.ShowManual(), "Manual", keyActionMap["ShowManual"], icons.AVLibraryBooks),
|
||||
ActionMenuChild(tr.AskHelp(), "Ask help", keyActionMap["AskHelp"], icons.ActionHelp),
|
||||
ActionMenuChild(tr.ReportBug(), "Report bug", keyActionMap["ReportBug"], icons.ActionBugReport),
|
||||
DividerMenuChild(),
|
||||
ActionMenuChild(tr.ShowLicense(), "License", keyActionMap["ShowLicense"], icons.ActionCopyright))
|
||||
})
|
||||
panicBtn := ToggleIconBtn(tr.Play().Panicked(), tr.Theme, t.PanicBtn, icons.AlertErrorOutline, icons.AlertError, t.panicHint, t.panicHint)
|
||||
if tr.Play().Panicked().Value() {
|
||||
panicBtn.Style = &tr.Theme.IconButton.Error
|
||||
}
|
||||
panicFC := layout.Flexed(1, func(gtx C) D { return layout.E.Layout(gtx, panicBtn.Layout) })
|
||||
if len(t.midiMenuItems) > 0 {
|
||||
return flex.Layout(gtx, fileFC, editFC, midiFC, helpFC, panicFC)
|
||||
return flex.Layout(gtx, fileFC, editFC, midiFC, helpFC, panicFC)
|
||||
}
|
||||
|
||||
func (sp *SongPanel) Tags(level int, yield TagYieldFunc) bool {
|
||||
for i := range sp.MenuBar.MenuStates {
|
||||
if !sp.MenuBar.MenuStates[i].Tags(level, yield) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return flex.Layout(gtx, fileFC, editFC, helpFC, panicFC)
|
||||
return true
|
||||
}
|
||||
|
||||
type PlayBar struct {
|
||||
|
||||
@ -128,8 +128,9 @@ type Theme struct {
|
||||
Bg color.NRGBA
|
||||
}
|
||||
Popup struct {
|
||||
Menu PopupStyle
|
||||
Dialog PopupStyle
|
||||
Menu PopupStyle
|
||||
Dialog PopupStyle
|
||||
ContextMenu PopupStyle
|
||||
}
|
||||
Split SplitStyle
|
||||
ScrollBar ScrollBarStyle
|
||||
|
||||
@ -219,6 +219,10 @@ popup:
|
||||
color: { r: 50, g: 50, b: 51, a: 255 }
|
||||
cornerradii: { nw: 0, ne: 0, se: 6, sw: 6 }
|
||||
shadow: { n: 0, s: 2, e: 2, w: 2, color: { r: 0, g: 0, b: 0, a: 192 } }
|
||||
contextmenu:
|
||||
color: { r: 50, g: 50, b: 51, a: 255 }
|
||||
cornerradii: { nw: 6, ne: 6, se: 6, sw: 6 }
|
||||
shadow: { n: 2, s: 2, e: 2, w: 2, color: { r: 0, g: 0, b: 0, a: 192 } }
|
||||
dialog:
|
||||
bg: { r: 0, g: 0, b: 0, a: 224 }
|
||||
title: { textsize: 16, color: *highemphasis, shadowcolor: *black }
|
||||
|
||||
@ -415,10 +415,11 @@ func (t *Tracker) openUrl(url string) {
|
||||
}
|
||||
|
||||
func (t *Tracker) Tags(curLevel int, yield TagYieldFunc) bool {
|
||||
ret := t.PatchPanel.Tags(curLevel+1, yield)
|
||||
curLevel++
|
||||
ret := t.SongPanel.Tags(curLevel, yield) && t.PatchPanel.Tags(curLevel, yield)
|
||||
if !t.Play().TrackerHidden().Value() {
|
||||
ret = ret && t.OrderEditor.Tags(curLevel+1, yield) &&
|
||||
t.TrackEditor.Tags(curLevel+1, yield)
|
||||
ret = ret && t.OrderEditor.Tags(curLevel, yield) &&
|
||||
t.TrackEditor.Tags(curLevel, yield)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@ package gomidi
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/vsariola/sointu/tracker"
|
||||
@ -18,27 +17,16 @@ import (
|
||||
|
||||
type (
|
||||
RTMIDIContext struct {
|
||||
driver *rtmididrv.Driver
|
||||
currentIn drivers.In
|
||||
broker *tracker.Broker
|
||||
driver *rtmididrv.Driver
|
||||
broker *tracker.Broker
|
||||
}
|
||||
|
||||
RTMIDIInputDevice struct {
|
||||
broker *tracker.Broker
|
||||
drivers.In
|
||||
}
|
||||
)
|
||||
|
||||
func (m *RTMIDIContext) InputDevices(yield func(string) bool) {
|
||||
if m.driver == nil {
|
||||
return
|
||||
}
|
||||
ins, err := m.driver.Ins()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, in := range ins {
|
||||
if !yield(in.String()) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Open the driver.
|
||||
func NewContext(broker *tracker.Broker) *RTMIDIContext {
|
||||
m := RTMIDIContext{broker: broker}
|
||||
@ -48,45 +36,49 @@ func NewContext(broker *tracker.Broker) *RTMIDIContext {
|
||||
return &m
|
||||
}
|
||||
|
||||
// Open an input device while closing the currently open if necessary.
|
||||
func (m *RTMIDIContext) Open(name string) error {
|
||||
if m.currentIn != nil && m.currentIn.String() == name {
|
||||
return nil
|
||||
}
|
||||
func (m *RTMIDIContext) Inputs(yield func(input tracker.MIDIInputDevice) bool) {
|
||||
if m.driver == nil {
|
||||
return errors.New("no driver available")
|
||||
return
|
||||
}
|
||||
if m.IsOpen() {
|
||||
m.currentIn.Close()
|
||||
}
|
||||
m.currentIn = nil
|
||||
ins, err := m.driver.Ins()
|
||||
if err != nil {
|
||||
return fmt.Errorf("retrieving MIDI inputs failed: %w", err)
|
||||
return
|
||||
}
|
||||
for _, in := range ins {
|
||||
if in.String() == name {
|
||||
m.currentIn = in
|
||||
r := RTMIDIInputDevice{In: in, broker: m.broker}
|
||||
if !yield(r) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if m.currentIn == nil {
|
||||
return fmt.Errorf("MIDI input device not found: %s", name)
|
||||
}
|
||||
|
||||
func (c *RTMIDIContext) Close() {
|
||||
if c.driver == nil {
|
||||
return
|
||||
}
|
||||
err = m.currentIn.Open()
|
||||
if err != nil {
|
||||
m.currentIn = nil
|
||||
c.driver.Close()
|
||||
}
|
||||
|
||||
func (c *RTMIDIContext) Support() tracker.MIDISupport {
|
||||
if c.driver == nil {
|
||||
return tracker.MIDISupportNoDriver
|
||||
}
|
||||
return tracker.MIDISupported
|
||||
}
|
||||
|
||||
// Open an input device and starting the listener.
|
||||
func (m RTMIDIInputDevice) Open() error {
|
||||
if err := m.In.Open(); err != nil {
|
||||
return fmt.Errorf("opening MIDI input failed: %w", err)
|
||||
}
|
||||
_, err = midi.ListenTo(m.currentIn, m.HandleMessage)
|
||||
if err != nil {
|
||||
m.currentIn.Close()
|
||||
m.currentIn = nil
|
||||
if _, err := midi.ListenTo(m.In, m.handleMessage); err != nil {
|
||||
m.In.Close()
|
||||
return fmt.Errorf("listening to MIDI input failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *RTMIDIContext) HandleMessage(msg midi.Message, timestampms int32) {
|
||||
func (m *RTMIDIInputDevice) handleMessage(msg midi.Message, timestampms int32) {
|
||||
var channel, key, velocity uint8
|
||||
if msg.GetNoteOn(&channel, &key, &velocity) {
|
||||
ev := tracker.NoteEvent{Timestamp: int64(timestampms) * 441 / 10, On: true, Channel: int(channel), Note: key, Source: m}
|
||||
@ -96,21 +88,3 @@ func (m *RTMIDIContext) HandleMessage(msg midi.Message, timestampms int32) {
|
||||
tracker.TrySend(m.broker.MIDIChannel(), any(ev))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RTMIDIContext) BPM() (bpm float64, ok bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (c *RTMIDIContext) Close() {
|
||||
if c.driver == nil {
|
||||
return
|
||||
}
|
||||
if c.currentIn != nil && c.currentIn.IsOpen() {
|
||||
c.currentIn.Close()
|
||||
}
|
||||
c.driver.Close()
|
||||
}
|
||||
|
||||
func (c *RTMIDIContext) IsOpen() bool {
|
||||
return c.currentIn != nil && c.currentIn.IsOpen()
|
||||
}
|
||||
|
||||
162
tracker/midi.go
162
tracker/midi.go
@ -2,27 +2,123 @@ package tracker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
MIDIModel Model
|
||||
MIDIContext interface {
|
||||
InputDevices(yield func(deviceName string) bool)
|
||||
Open(deviceName string) error
|
||||
Close()
|
||||
IsOpen() bool
|
||||
}
|
||||
)
|
||||
type MIDIModel Model
|
||||
|
||||
func (m *Model) MIDI() *MIDIModel { return (*MIDIModel)(m) }
|
||||
|
||||
type (
|
||||
midiState struct {
|
||||
currentInput MIDIInputDevice
|
||||
context MIDIContext
|
||||
inputs []MIDIInputDevice
|
||||
}
|
||||
|
||||
MIDIContext interface {
|
||||
Inputs(yield func(input MIDIInputDevice) bool)
|
||||
Close()
|
||||
Support() MIDISupport
|
||||
}
|
||||
|
||||
MIDIInputDevice interface {
|
||||
Open() error
|
||||
Close() error
|
||||
IsOpen() bool
|
||||
String() string
|
||||
}
|
||||
|
||||
MIDISupport int
|
||||
)
|
||||
|
||||
const (
|
||||
MIDISupportNotCompiled MIDISupport = iota
|
||||
MIDISupportNoDriver
|
||||
MIDISupported
|
||||
)
|
||||
|
||||
// Refresh
|
||||
func (m *MIDIModel) Refresh() Action { return MakeAction((*midiRefresh)(m)) }
|
||||
|
||||
type midiRefresh MIDIModel
|
||||
|
||||
func (m *midiRefresh) Do() {
|
||||
if m.midi.context == nil {
|
||||
return
|
||||
}
|
||||
m.midi.inputs = m.midi.inputs[:0]
|
||||
for i := range m.midi.context.Inputs {
|
||||
m.midi.inputs = append(m.midi.inputs, i)
|
||||
if m.midi.currentInput != nil && i.String() == m.midi.currentInput.String() {
|
||||
m.midi.currentInput.Close()
|
||||
m.midi.currentInput = nil
|
||||
if err := i.Open(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to reopen MIDI input port: %s", err.Error()), Error)
|
||||
continue
|
||||
}
|
||||
m.midi.currentInput = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InputDevices can be iterated to get string names of all the MIDI input
|
||||
// devices.
|
||||
func (m *MIDIModel) InputDevices(yield func(deviceName string) bool) { m.midi.InputDevices(yield) }
|
||||
func (m *MIDIModel) Input() Int { return MakeInt((*midiInputDevices)(m)) }
|
||||
|
||||
// IsOpen returns true if a midi device is currently open.
|
||||
func (m *MIDIModel) IsOpen() bool { return m.midi.IsOpen() }
|
||||
type midiInputDevices MIDIModel
|
||||
|
||||
func (m *midiInputDevices) Value() int {
|
||||
if m.midi.currentInput == nil {
|
||||
return 0
|
||||
}
|
||||
for i, d := range m.midi.inputs {
|
||||
if d == m.midi.currentInput {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
func (m *midiInputDevices) SetValue(val int) bool {
|
||||
if val < 0 || val > len(m.midi.inputs) {
|
||||
return false
|
||||
}
|
||||
if m.midi.currentInput != nil {
|
||||
if err := m.midi.currentInput.Close(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to close current MIDI input port: %s", err.Error()), Error)
|
||||
}
|
||||
m.midi.currentInput = nil
|
||||
}
|
||||
if val == 0 {
|
||||
return true
|
||||
}
|
||||
newInput := m.midi.inputs[val-1]
|
||||
if err := newInput.Open(); err != nil {
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Failed to open MIDI input port: %s", err.Error()), Error)
|
||||
return false
|
||||
}
|
||||
m.midi.currentInput = newInput
|
||||
(*Model)(m).Alerts().Add(fmt.Sprintf("Opened MIDI input port: %s", newInput.String()), Info)
|
||||
return true
|
||||
}
|
||||
func (m *midiInputDevices) Range() RangeInclusive {
|
||||
return RangeInclusive{Min: 0, Max: len(m.midi.inputs)}
|
||||
}
|
||||
func (m *midiInputDevices) StringOf(value int) string {
|
||||
if value < 0 || value > len(m.midi.inputs) {
|
||||
return ""
|
||||
}
|
||||
if value == 0 {
|
||||
switch m.midi.context.Support() {
|
||||
case MIDISupportNotCompiled:
|
||||
return "Not compiled"
|
||||
case MIDISupportNoDriver:
|
||||
return "No driver"
|
||||
default:
|
||||
return "Closed"
|
||||
}
|
||||
}
|
||||
return m.midi.inputs[value-1].String()
|
||||
}
|
||||
|
||||
// InputtingNotes returns a Bool controlling whether the MIDI events are used
|
||||
// just to trigger instruments, or if the note events are used to input notes to
|
||||
@ -34,44 +130,10 @@ type midiInputtingNotes Model
|
||||
func (m *midiInputtingNotes) Value() bool { return m.broker.mIDIEventsToGUI.Load() }
|
||||
func (m *midiInputtingNotes) SetValue(val bool) { m.broker.mIDIEventsToGUI.Store(val) }
|
||||
|
||||
// Open returns an Action to open the MIDI input device with a given name.
|
||||
func (m *MIDIModel) Open(deviceName string) Action {
|
||||
return MakeAction(openMIDI{Item: deviceName, Model: (*Model)(m)})
|
||||
}
|
||||
|
||||
type openMIDI struct {
|
||||
Item string
|
||||
*Model
|
||||
}
|
||||
|
||||
func (s openMIDI) Do() {
|
||||
m := s.Model
|
||||
if err := s.Model.midi.Open(s.Item); err == nil {
|
||||
message := fmt.Sprintf("Opened MIDI device: %s", s.Item)
|
||||
m.Alerts().Add(message, Info)
|
||||
} else {
|
||||
message := fmt.Sprintf("Could not open MIDI device: %s", s.Item)
|
||||
m.Alerts().Add(message, Error)
|
||||
}
|
||||
}
|
||||
|
||||
// FindMIDIDeviceByPrefix finds the MIDI input device whose name starts with the given
|
||||
// prefix. It returns the full device name and true if found, or an empty string
|
||||
// and false if not found.
|
||||
func FindMIDIDeviceByPrefix(c MIDIContext, prefix string) (deviceName string, ok bool) {
|
||||
for input := range c.InputDevices {
|
||||
if strings.HasPrefix(input, prefix) {
|
||||
return input, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// NullMIDIContext is a mockup MIDIContext if you don't want to create a real
|
||||
// one.
|
||||
type NullMIDIContext struct{}
|
||||
|
||||
func (m NullMIDIContext) InputDevices(yield func(string) bool) {}
|
||||
func (m NullMIDIContext) Open(deviceName string) error { return nil }
|
||||
func (m NullMIDIContext) Close() {}
|
||||
func (m NullMIDIContext) IsOpen() bool { return false }
|
||||
func (m NullMIDIContext) Inputs(yield func(input MIDIInputDevice) bool) {}
|
||||
func (m NullMIDIContext) Close() {}
|
||||
func (m NullMIDIContext) Support() MIDISupport { return MIDISupportNotCompiled }
|
||||
|
||||
@ -87,7 +87,7 @@ type (
|
||||
|
||||
broker *Broker
|
||||
|
||||
midi MIDIContext
|
||||
midi midiState
|
||||
|
||||
presetData presetData
|
||||
}
|
||||
@ -173,7 +173,7 @@ func (m *Model) Quitted() bool { return m.quitted }
|
||||
func NewModel(broker *Broker, synthers []sointu.Synther, midiContext MIDIContext, recoveryFilePath string) *Model {
|
||||
m := new(Model)
|
||||
m.synthers = synthers
|
||||
m.midi = midiContext
|
||||
m.midi = midiState{context: midiContext}
|
||||
m.broker = broker
|
||||
m.d.Octave = 4
|
||||
m.linkInstrTrack = true
|
||||
@ -196,6 +196,7 @@ func NewModel(broker *Broker, synthers []sointu.Synther, midiContext MIDIContext
|
||||
m.Preset().updateCache()
|
||||
m.derived.searchResults = make([]string, 0, len(sointu.UnitNames))
|
||||
m.Unit().updateDerivedUnitSearch()
|
||||
m.MIDI().Refresh().Do()
|
||||
go runDetector(broker)
|
||||
go runSpecAnalyzer(broker)
|
||||
return m
|
||||
|
||||
@ -182,6 +182,12 @@ func (v *playSyntherIndex) SetValue(value int) bool {
|
||||
TrySend(v.broker.ToPlayer, any(v.synthers[value]))
|
||||
return true
|
||||
}
|
||||
func (v *playSyntherIndex) StringOf(value int) string {
|
||||
if value < 0 || value >= len(v.synthers) {
|
||||
return ""
|
||||
}
|
||||
return v.synthers[value].Name()
|
||||
}
|
||||
|
||||
// SyntherName returns the name of the currently selected synther.
|
||||
func (v *Play) SyntherName() string { return v.synthers[v.syntherIndex].Name() }
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
package tracker
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/vsariola/sointu"
|
||||
"github.com/vsariola/sointu/vm"
|
||||
)
|
||||
|
||||
// Scope returns the ScopeModel view of the Model, used for oscilloscope
|
||||
@ -53,7 +54,16 @@ func (s *scopeTriggerChannel) SetValue(val int) bool {
|
||||
s.scopeData.triggerChannel = val
|
||||
return true
|
||||
}
|
||||
func (s *scopeTriggerChannel) Range() RangeInclusive { return RangeInclusive{0, vm.MAX_VOICES} }
|
||||
func (s *scopeTriggerChannel) Range() RangeInclusive { return RangeInclusive{0, len(s.d.Song.Patch)} }
|
||||
func (s *scopeTriggerChannel) StringOf(value int) string {
|
||||
if value == 0 {
|
||||
return "Disabled"
|
||||
}
|
||||
if value >= 1 && value <= len(s.d.Song.Patch) && s.d.Song.Patch[value-1].Name != "" {
|
||||
return s.d.Song.Patch[value-1].Name
|
||||
}
|
||||
return strconv.Itoa(value)
|
||||
}
|
||||
|
||||
// Waveform returns the oscilloscope waveform buffer.
|
||||
func (s *ScopeModel) Waveform() RingBuffer[[2]float32] { return s.scopeData.waveForm }
|
||||
|
||||
@ -71,6 +71,16 @@ func (v *spectrumChannels) SetValue(value int) bool {
|
||||
func (v *spectrumChannels) Range() RangeInclusive {
|
||||
return RangeInclusive{0, int(NumSpecChnModes) - 1}
|
||||
}
|
||||
func (v *spectrumChannels) StringOf(value int) string {
|
||||
switch SpecChnMode(value) {
|
||||
case SpecChnModeSum:
|
||||
return "Sum"
|
||||
case SpecChnModeSeparate:
|
||||
return "Separate"
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
type SpecChnMode int
|
||||
|
||||
|
||||
Reference in New Issue
Block a user