Fixed rotating layouts; Added Shader layout
This commit is contained in:
13 files changed
+207
-93
No files matched your search
@@ -19,6 +19,9 @@ type Layout interface {
|
||||
// Show the layout.
|
||||
Show(Context)
|
||||
|
||||
// IsVisible checks if the layout is active.
|
||||
IsVisible() bool
|
||||
|
||||
// Widgets used by this screen.
|
||||
Widgets() []Widget
|
||||
}
|
||||
|
||||
+2
-2
@@ -16,8 +16,8 @@ import (
|
||||
)
|
||||
|
||||
// BufferFor creates a new image buffer for the provided display matching its color model.
|
||||
func BufferFor(ctx Context, size image.Point) draw.Image {
|
||||
switch ctx.Display().ColorModel() {
|
||||
func BufferFor(model color.Model, size image.Point) draw.Image {
|
||||
switch model {
|
||||
case pixel.MonoModel:
|
||||
return pixel.NewMonoImage(size.X, size.Y)
|
||||
case pixel.Gray2Model:
|
||||
|
||||
+3
-25
@@ -7,11 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type base struct {
|
||||
name string
|
||||
widgets []app.Widget
|
||||
visible bool
|
||||
onButtonPress func(app.Context)
|
||||
onButtonLongPress func(app.Context)
|
||||
name string
|
||||
widgets []app.Widget
|
||||
visible bool
|
||||
}
|
||||
|
||||
func (l *base) log() *logrus.Entry {
|
||||
@@ -50,23 +48,3 @@ func (l *base) Show(ctx app.Context) {
|
||||
}
|
||||
l.visible = true
|
||||
}
|
||||
|
||||
func (i *base) OnButtonPress(action func(app.Context)) {
|
||||
i.onButtonPress = action
|
||||
}
|
||||
|
||||
func (i *base) OnButtonLongPress(action func(app.Context)) {
|
||||
i.onButtonLongPress = action
|
||||
}
|
||||
|
||||
func (l *base) HandleButtonPress(ctx app.Context) {
|
||||
if l.onButtonPress != nil {
|
||||
l.onButtonPress(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *base) HandleButtonLongPress(ctx app.Context) {
|
||||
if l.onButtonLongPress != nil {
|
||||
l.onButtonLongPress(ctx)
|
||||
}
|
||||
}
|
||||
+42
-48
@@ -44,67 +44,61 @@ func Settings(ctx app.Context) app.Layout {
|
||||
}
|
||||
|
||||
func (l *settings) HandleRotary(ctx app.Context, delta int) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
log := l.log()
|
||||
log.WithField("item", l.index).Debug("menu item changed")
|
||||
l.menu.Change(delta)
|
||||
if l.menu.IsEditing() {
|
||||
// React on changed value, if it's an immediate change.
|
||||
switch item := l.menu.Items[l.menu.Position]; item.Label {
|
||||
case "Display contrast":
|
||||
if err := ctx.Display().SetContrast(*(item.Value.(*uint8))); err != nil {
|
||||
log.WithError(err).Error("error updating contrast")
|
||||
}
|
||||
default:
|
||||
log.WithField("label", item.Label).Debug("no immediate change")
|
||||
log := l.log()
|
||||
log.WithField("item", l.index).Debug("menu item changed")
|
||||
l.menu.Change(delta)
|
||||
if l.menu.IsEditing() {
|
||||
// React on changed value, if it's an immediate change.
|
||||
switch item := l.menu.Items[l.menu.Position]; item.Label {
|
||||
case "Display contrast":
|
||||
if err := ctx.Display().SetContrast(*(item.Value.(*uint8))); err != nil {
|
||||
log.WithError(err).Error("error updating contrast")
|
||||
}
|
||||
default:
|
||||
log.WithField("label", item.Label).Debug("no immediate change")
|
||||
}
|
||||
l.menuWidget.Redraw()
|
||||
}
|
||||
l.menuWidget.Redraw()
|
||||
}
|
||||
|
||||
func (l *settings) HandleButtonPress(ctx app.Context) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
log := l.log()
|
||||
if l.menu.IsEditing() {
|
||||
log.Debug("exit edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
} else if l.menu.IsSubMenu() {
|
||||
log.Debug("enter sub menu")
|
||||
l.menu = l.menu.EnterSubMenu()
|
||||
} else if item := l.menu.ActiveItem(); item.Value == menu.Back {
|
||||
log.Debug("exit sub menu")
|
||||
menu := l.menu.ExitSubMenu()
|
||||
if menu == nil {
|
||||
// Close settings layout, exiting root.
|
||||
ctx.Show(nil)
|
||||
return
|
||||
}
|
||||
l.menu = menu
|
||||
} else {
|
||||
log.Debug("enter edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
log := l.log()
|
||||
if l.menu.IsEditing() {
|
||||
log.Debug("exit edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
} else if l.menu.IsSubMenu() {
|
||||
log.Debug("enter sub menu")
|
||||
l.menu = l.menu.EnterSubMenu()
|
||||
} else if item := l.menu.ActiveItem(); item.Value == menu.Back {
|
||||
log.Debug("exit sub menu")
|
||||
menu := l.menu.ExitSubMenu()
|
||||
if menu == nil {
|
||||
// Close settings layout, exiting root.
|
||||
ctx.Show(nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Trigger re-render.
|
||||
l.menuWidget.Update(l.menu)
|
||||
l.menu = menu
|
||||
} else {
|
||||
log.Debug("enter edit mode")
|
||||
l.menu.ToggleEdit()
|
||||
}
|
||||
|
||||
// Trigger re-render.
|
||||
l.menuWidget.Update(l.menu)
|
||||
}
|
||||
|
||||
func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||
if l.menuWidget.IsVisible() {
|
||||
// Rewind to root menu
|
||||
for {
|
||||
parent := l.menu.Parent
|
||||
if parent == nil {
|
||||
break
|
||||
}
|
||||
l.menu = parent
|
||||
// Rewind to root menu
|
||||
for {
|
||||
parent := l.menu.Parent
|
||||
if parent == nil {
|
||||
break
|
||||
}
|
||||
|
||||
// Close settings layout.
|
||||
ctx.Show(nil)
|
||||
l.menu = parent
|
||||
}
|
||||
|
||||
// Close settings layout.
|
||||
ctx.Show(nil)
|
||||
}
|
||||
|
||||
func foreachStructField(v reflect.Value, f func(fv reflect.Value, sf reflect.StructField) (stop bool)) {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"sync"
|
||||
|
||||
"github.com/BeatGlow/audio"
|
||||
"github.com/BeatGlow/beatglow/app"
|
||||
"github.com/BeatGlow/beatglow/app/shader"
|
||||
"github.com/BeatGlow/beatglow/app/widget"
|
||||
)
|
||||
|
||||
type shaderLayout struct {
|
||||
base
|
||||
model color.Model
|
||||
effects []shader.Effect
|
||||
index int
|
||||
mu sync.RWMutex
|
||||
shader *shader.Shader
|
||||
widget *widget.Canvas
|
||||
}
|
||||
|
||||
func Shader(ctx app.Context) app.Layout {
|
||||
display := ctx.Display()
|
||||
layout := &shaderLayout{
|
||||
model: display.ColorModel(),
|
||||
shader: shader.New(display.ColorModel(), display.Bounds().Size()),
|
||||
effects: []shader.Effect{
|
||||
&shader.AudioIntensityBarsEffect{},
|
||||
&shader.AudioNoiseEffect{},
|
||||
&shader.AudioRipplesEffect{},
|
||||
&shader.ChannelSeparationEffect{},
|
||||
&shader.MoireRotatingZoomerEffect{},
|
||||
&shader.PulsatingCirclesEffect{},
|
||||
&shader.RadialAudioIntensityEffect{},
|
||||
&shader.StereoMirrorEffect{},
|
||||
&shader.StereoPulseEffect{},
|
||||
&shader.StereoRadialEffect{},
|
||||
&shader.StereoRippleEffect{},
|
||||
&shader.StereoSpectrumEffect{},
|
||||
&shader.StereoSwirlEffect{},
|
||||
&shader.StereoWaveformEffect{},
|
||||
},
|
||||
widget: widget.NewCanvas(display.Bounds().Size(), display.ColorModel()),
|
||||
}
|
||||
layout.shader.Effects = []shader.Effect{layout.effects[0]}
|
||||
layout.widgets = []app.Widget{layout.widget}
|
||||
return layout
|
||||
}
|
||||
|
||||
func (l *shaderLayout) ProcessBuffer(buffer audio.Buffer[float64]) {
|
||||
if l.visible {
|
||||
l.mu.Lock()
|
||||
l.shader.ProcessBuffer(buffer)
|
||||
app.ImageCopy(l.widget.Buffer(), l.shader.Render())
|
||||
//l.widget.Update(l.shader.Render(), l.model)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (l *shaderLayout) HandleRotary(_ app.Context, delta int) {
|
||||
l.index += delta
|
||||
for l.index < 0 {
|
||||
l.index = len(l.effects) + l.index
|
||||
}
|
||||
for l.index >= len(l.effects) {
|
||||
l.index = l.index - len(l.effects)
|
||||
}
|
||||
l.shader.Effects[0] = l.effects[l.index]
|
||||
}
|
||||
|
||||
var (
|
||||
_ app.Layout = (*shaderLayout)(nil)
|
||||
_ app.BufferProcessor = (*shaderLayout)(nil)
|
||||
_ app.RotaryInputHandler = (*shaderLayout)(nil)
|
||||
)
|
||||
@@ -43,6 +43,11 @@ func (s *Shader) ProcessBuffer(buffer audio.Buffer[float64]) {
|
||||
for channel, channels := 0, len(s.buffer); channel < channels; channel++ {
|
||||
copy(s.buffer[channel], buffer[channel])
|
||||
}
|
||||
|
||||
// Normalize channels
|
||||
for c := range s.buffer {
|
||||
audio.Normalize(s.buffer[c])
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Shader) Render() image.Image {
|
||||
|
||||
+1
-1
@@ -135,7 +135,7 @@ func (b *Base) Refresh(ctx app.Context) app.Widget {
|
||||
}
|
||||
|
||||
if b.buffer == nil {
|
||||
b.buffer = app.BufferFor(ctx, b.Bounds().Size())
|
||||
b.buffer = app.BufferFor(ctx.Display().ColorModel(), b.Bounds().Size())
|
||||
}
|
||||
|
||||
return b.super()
|
||||
|
||||
+25
-5
@@ -3,6 +3,7 @@ package widget
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
|
||||
"github.com/makeworld-the-better-one/dither/v2"
|
||||
"github.com/nfnt/resize"
|
||||
@@ -56,11 +57,6 @@ func (w *Image) Update(image image.Image, model color.Model) app.Widget {
|
||||
func (w *Image) Refresh(ctx app.Context) bool {
|
||||
log := logrus.WithField("widget", w.String())
|
||||
|
||||
if !w.IsVisible() {
|
||||
log.Trace("not refreshing; not visisble")
|
||||
return false
|
||||
}
|
||||
|
||||
if !w.changed.CompareAndSwap(true, false) {
|
||||
log.Trace("not refreshing; not changed")
|
||||
return false
|
||||
@@ -116,4 +112,28 @@ func convertImageFor(model color.Model, image image.Image) image.Image {
|
||||
return d.Dither(image)
|
||||
}
|
||||
|
||||
type Canvas struct {
|
||||
Base
|
||||
}
|
||||
|
||||
func NewCanvas(size image.Point, model color.Model) *Canvas {
|
||||
w := new(Canvas)
|
||||
w.buffer = app.BufferFor(model, size)
|
||||
w.Extend(w)
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Canvas) Buffer() draw.Image {
|
||||
return w.buffer
|
||||
}
|
||||
|
||||
func (w *Canvas) String() string {
|
||||
return "canvas:" + w.buffer.Bounds().Size().String()
|
||||
}
|
||||
|
||||
func (w *Canvas) Refresh(ctx app.Context) bool {
|
||||
app.ImageCopy(ctx.Display(), w.buffer)
|
||||
return true
|
||||
}
|
||||
|
||||
var _ app.Widget = (*Image)(nil)
|
||||
+1
-1
@@ -55,7 +55,7 @@ func (w *Menu) Refresh(ctx app.Context) bool {
|
||||
|
||||
if w.changed.CompareAndSwap(true, false) || w.buffer == nil || (w.curr != w.next && w.next != nil) {
|
||||
if w.buffer == nil {
|
||||
w.buffer = app.BufferFor(ctx, size)
|
||||
w.buffer = app.BufferFor(ctx.Display().ColorModel(), size)
|
||||
w.Root.SetPageSize(size.Y / 16)
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ func (w *Spectrum) Refresh(ctx app.Context) bool {
|
||||
bounds := w.bounds.Load()
|
||||
if w.changed.CompareAndSwap(true, false) {
|
||||
if w.buffer == nil {
|
||||
w.buffer = app.BufferFor(ctx, bounds.Size())
|
||||
w.buffer = app.BufferFor(ctx.Display().ColorModel(), bounds.Size())
|
||||
app.ImageCopy(w.buffer, w.styles.Window.Background())
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ func (w *Text) Refresh(ctx app.Context) bool {
|
||||
// Resize buffer if required.
|
||||
if w.buffer == nil || resize.X+resize.Y > 0 {
|
||||
log.WithField("size", resize).Debug("allocating new buffer")
|
||||
w.buffer = app.BufferFor(ctx, resize)
|
||||
w.buffer = app.BufferFor(ctx.Display().ColorModel(), resize)
|
||||
}
|
||||
|
||||
// Fill background.
|
||||
|
||||
Reference in new issue
Block a user