132 lines
3.3 KiB
Go
132 lines
3.3 KiB
Go
package menu
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"math"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/BeatGlow/display/draw"
|
|
"github.com/BeatGlow/display/pixel"
|
|
|
|
"github.com/BeatGlow/beatglow/app"
|
|
"github.com/BeatGlow/beatglow/app/font"
|
|
)
|
|
|
|
func (item *Item) Render(dst draw.Image, isActive, isEditing bool, style *app.TextStyle) {
|
|
var (
|
|
bounds = dst.Bounds()
|
|
edge = image.Rectangle{Min: bounds.Min, Max: bounds.Max.Sub(image.Pt(1, 1))}
|
|
)
|
|
logrus.WithFields(logrus.Fields{
|
|
"bounds": bounds,
|
|
"size": bounds.Size(),
|
|
}).Debug("render menu item")
|
|
|
|
if style == nil {
|
|
style = &app.StylesFor(dst.ColorModel()).Text
|
|
}
|
|
|
|
// Clear area.
|
|
app.ImageClear(dst)
|
|
|
|
if isEditing && isActive {
|
|
draw.Box(dst, bounds, pixel.Gray4{Y: 0x4})
|
|
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0xf})
|
|
} else if isActive {
|
|
draw.Box(dst, bounds, pixel.Gray4{Y: 0x2})
|
|
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0xb})
|
|
} else {
|
|
draw.Rectangle(dst, edge, pixel.Gray4{Y: 0x2})
|
|
}
|
|
|
|
// Render icon.
|
|
if item.Icon != "" {
|
|
font.Icon(dst, bounds.Min.Add(image.Pt(3, 2)), color.White, item.Icon)
|
|
}
|
|
|
|
// Render label.
|
|
style.Font.Draw(dst, bounds.Min.Add(image.Pt(16, 3)), style.Foreground(), item.Label)
|
|
|
|
// Render icon and widgets.
|
|
switch value := item.Value.(type) {
|
|
case *bool:
|
|
item.renderBool(dst, *value, isEditing)
|
|
case *uint8:
|
|
item.renderUint8(dst, *value, isEditing, style)
|
|
case *int:
|
|
item.renderInt(dst, *value, isEditing, style)
|
|
case *string:
|
|
item.renderString(dst, *value, isEditing, style)
|
|
case menuAction:
|
|
// no further renditions needed
|
|
case nil:
|
|
// sub menu item has no value
|
|
item.renderSubMenu(dst, style)
|
|
default:
|
|
logrus.WithFields(logrus.Fields{
|
|
"label": item.Label,
|
|
"type": fmt.Sprintf("%T", value),
|
|
}).Warn("no menu renderer for type")
|
|
}
|
|
}
|
|
|
|
func (item *Item) renderSubMenu(dst draw.Image, style *app.TextStyle) {
|
|
bounds := dst.Bounds()
|
|
font.Icon(dst, image.Pt(bounds.Max.X-14, bounds.Min.Y+3), color.White, "md_folder_open_outline")
|
|
}
|
|
|
|
func (item *Item) renderBool(dst draw.Image, value bool, isEditing bool) {
|
|
var (
|
|
bounds = dst.Bounds()
|
|
icon = "md_checkbox_blank_outline"
|
|
)
|
|
if value {
|
|
icon = "md_checkbox_marked_outline"
|
|
}
|
|
font.Icon(dst, image.Pt(bounds.Max.X-14, bounds.Min.Y+3), color.White, icon)
|
|
}
|
|
|
|
func (item *Item) renderUint8(dst draw.Image, value uint8, isEditing bool, style *app.TextStyle) {
|
|
var (
|
|
bounds = dst.Bounds()
|
|
text = fmt.Sprintf("%d", value)
|
|
width, _ = style.Font.MeasureString(text)
|
|
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
|
|
)
|
|
style.Font.Draw(dst, point, style.Foreground(), text)
|
|
}
|
|
|
|
func (item *Item) renderInt(dst draw.Image, value int, isEditing bool, style *app.TextStyle) {
|
|
var (
|
|
bounds = dst.Bounds()
|
|
text = fmt.Sprintf("%d", value)
|
|
width, _ = style.Font.MeasureString(text)
|
|
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
|
|
)
|
|
style.Font.Draw(dst, point, style.Foreground(), text)
|
|
}
|
|
|
|
func (item *Item) renderString(dst draw.Image, value string, isEditing bool, style *app.TextStyle) {
|
|
var (
|
|
bounds = dst.Bounds()
|
|
width, _ = style.Font.MeasureString(value)
|
|
point = image.Pt(bounds.Max.X-3-width, bounds.Min.Y+3)
|
|
)
|
|
style.Font.Draw(dst, point, style.Foreground(), value)
|
|
}
|
|
|
|
func numberOfDigits(n int) int {
|
|
if n == 0 {
|
|
return 1
|
|
}
|
|
sign := 0
|
|
if n < 0 {
|
|
sign++
|
|
n = -n
|
|
}
|
|
return int(math.Log10(float64(n))) + sign + 1
|
|
}
|