Fixed rotating layouts; Added Shader layout
This commit is contained in:
13 files changed
+153
-39
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:
|
||||
|
||||
@@ -10,8 +10,6 @@ type base struct {
|
||||
name string
|
||||
widgets []app.Widget
|
||||
visible bool
|
||||
onButtonPress func(app.Context)
|
||||
onButtonLongPress func(app.Context)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,6 @@ 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)
|
||||
@@ -60,11 +59,9 @@ func (l *settings) HandleRotary(ctx app.Context, delta int) {
|
||||
}
|
||||
}
|
||||
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")
|
||||
@@ -88,11 +85,9 @@ func (l *settings) HandleButtonPress(ctx app.Context) {
|
||||
|
||||
// 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
|
||||
@@ -104,7 +99,6 @@ func (l *settings) HandleButtonLongPress(ctx app.Context) {
|
||||
|
||||
// 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.
|
||||
|
||||
+36
@@ -168,28 +168,64 @@ func (c *controllerContext) handleInput(event hardware.InputEvent) {
|
||||
switch event := event.(type) {
|
||||
case hardware.RotaryEvent:
|
||||
for _, layout := range c.layouts {
|
||||
if layout.IsVisible() {
|
||||
if handler, ok := layout.(app.RotaryInputHandler); ok {
|
||||
log.WithField("layout", layout.String()).Debug("dispatch rotary input")
|
||||
handler.HandleRotary(c, event.Delta)
|
||||
}
|
||||
}
|
||||
}
|
||||
case hardware.RotaryPressEvent:
|
||||
var handled bool
|
||||
for _, layout := range c.layouts {
|
||||
if layout.IsVisible() {
|
||||
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
||||
log.WithField("layout", layout.String()).Debug("dispatch button press")
|
||||
handler.HandleButtonPress(c)
|
||||
handled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !handled {
|
||||
c.handleRotaryPress()
|
||||
}
|
||||
case hardware.RotaryLongPressEvent:
|
||||
for _, layout := range c.layouts {
|
||||
if layout.IsVisible() {
|
||||
if handler, ok := layout.(app.ButtonInputHandler); ok {
|
||||
log.WithField("layout", layout.String()).Debug("dispatch button long press")
|
||||
handler.HandleButtonLongPress(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
log.Warn("unhandled input event")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *controllerContext) handleRotaryPress() {
|
||||
log := logrus.NewEntry(logrus.StandardLogger())
|
||||
log.Debug("no handler for rotary press event, switching layouts")
|
||||
|
||||
var activeIndex int
|
||||
for i, layout := range c.layouts {
|
||||
if layout == c.active {
|
||||
activeIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if activeIndex == 0 {
|
||||
// We're in settings, shouldn't be able to get here since settings handle rotary press events
|
||||
log.Debug("huh? how did we get here?")
|
||||
return
|
||||
}
|
||||
|
||||
nextIndex := activeIndex + 1
|
||||
if nextIndex >= len(c.layouts) {
|
||||
nextIndex = 1
|
||||
}
|
||||
|
||||
c.Show(c.layouts[nextIndex])
|
||||
}
|
||||
|
||||
var _ app.Context = (*controllerContext)(nil)
|
||||
@@ -107,6 +107,8 @@ func (c *Controller) configureInputs(log *logrus.Entry, settings *config.Input)
|
||||
func (c *Controller) configureApps(_ *logrus.Entry) (err error) {
|
||||
c.ctx.addSettings(layout.Settings(&c.ctx))
|
||||
c.ctx.addLayout(layout.Playing(&c.ctx))
|
||||
//c.ctx.addLayout(layout.Spectrum(&c.ctx))
|
||||
c.ctx.addLayout(layout.Shader(&c.ctx))
|
||||
c.ctx.Show(nil)
|
||||
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user