Skip to content

Commit

Permalink
better handling of color codes, pack icons
Browse files Browse the repository at this point in the history
  • Loading branch information
olebeck committed Jul 9, 2024
1 parent 6e403ec commit c7732dc
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 71 deletions.
2 changes: 1 addition & 1 deletion gophertunnel
2 changes: 1 addition & 1 deletion handlers/worlds/map_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (m *MapUI) Start(ctx context.Context) {

m.ticker = time.NewTicker(33 * time.Millisecond)
go func() {
m.ChunkRenderer.ResolveColors(m.w.customBlocks, m.w.serverState.packs)
m.ChunkRenderer.ResolveColors(m.w.customBlocks, m.w.proxy.Server.ResourcePacks())
close(m.haveColors)
}()
go func() {
Expand Down
14 changes: 6 additions & 8 deletions handlers/worlds/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/bedrock-tool/bedrocktool/locale"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/flytam/filenamify"
"github.com/sandertv/gophertunnel/minecraft/resource"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -40,10 +41,6 @@ func (w *worldsHandler) AddPacks(fs utils.WriterFS) error {
packFolder := path.Join("behavior_packs", name)

for _, p := range w.proxy.Server.ResourcePacks() {
p, err := utils.PackFromBase(p)
if err != nil {
return err
}
w.bp.CheckAddLink(p)
}

Expand All @@ -67,15 +64,16 @@ func (w *worldsHandler) AddPacks(fs utils.WriterFS) error {
// add resource packs
if w.settings.WithPacks {
packNames := make(map[string][]string)
for _, pack := range w.serverState.packs {
packs := w.proxy.Server.ResourcePacks()
for _, pack := range packs {
packName := pack.Name()
packNames[packName] = append(packNames[packName], pack.UUID())
}

var rdeps []dep
for _, pack := range w.serverState.packs {
for _, pack := range packs {
log := w.log.WithField("pack", pack.Name())
if pack.Encrypted() && !pack.CanDecrypt() {
if pack.Encrypted() && !pack.CanRead() {
log.Warn("Cant add is encrypted")
continue
}
Expand Down Expand Up @@ -117,7 +115,7 @@ func (w *worldsHandler) AddPacks(fs utils.WriterFS) error {
return nil
}

func writePackToFs(pack utils.Pack, wfs utils.WriterFS, dir string) error {
func writePackToFs(pack resource.Pack, wfs utils.WriterFS, dir string) error {
return fs.WalkDir(pack, ".", func(fpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions handlers/worlds/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type serverState struct {

openItemContainers map[byte]*itemContainer
playerInventory []protocol.ItemInstance
packs []utils.Pack
dimensions map[int]protocol.DimensionDefinition
playerSkins map[uuid.UUID]*protocol.Skin

Expand Down Expand Up @@ -248,8 +247,6 @@ func NewWorldsHandler(settings WorldSettings) *proxy.Handler {
mapItemPacket.Content[0].StackNetworkID = 0xffff + rand.Int31n(0xfff)
}

w.serverState.packs = w.proxy.ResourcePacks

w.proxy.SendMessage(locale.Loc("use_setname", nil))
w.mapUI.Start(ctx)
return false
Expand Down
Binary file modified subcommands/resourcepack-d/resourcepack-d.go
Binary file not shown.
130 changes: 130 additions & 0 deletions ui/gui/mctext/mctext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package mctext

import (
"image/color"
"regexp"

"gioui.org/font"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget/material"
"gioui.org/x/styledtext"
)

var splitter = regexp.MustCompile("((?:§.)?(?:[^§]+)?)")

func Label(th *material.Theme, size unit.Sp, txt string) func(gtx layout.Context) layout.Dimensions {
split := splitter.FindAllString(txt, -1)
var Styles []styledtext.SpanStyle

var activeColor color.NRGBA = color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}
var bold bool
var italic bool
var obfuscated bool

for _, part := range split {
if len(part) == 0 {
continue
}
partR := []rune(part)
switch partR[1] {
case '0':
activeColor = color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}
case '1':
activeColor = color.NRGBA{R: 0x00, G: 0x00, B: 0xAA, A: 0xff}
case '2':
activeColor = color.NRGBA{R: 0x00, G: 0xAA, B: 0x00, A: 0xff}
case '3':
activeColor = color.NRGBA{R: 0x00, G: 0xAA, B: 0xAA, A: 0xff}
case '4':
activeColor = color.NRGBA{R: 0xAA, G: 0x00, B: 0x00, A: 0xff}
case '5':
activeColor = color.NRGBA{R: 0xAA, G: 0x00, B: 0xAA, A: 0xff}
case '6':
activeColor = color.NRGBA{R: 0xFF, G: 0xAA, B: 0x00, A: 0xff}
case '7':
activeColor = color.NRGBA{R: 0xc6, G: 0xc6, B: 0xc6, A: 0xff}
case '8':
activeColor = color.NRGBA{R: 0x55, G: 0x55, B: 0x55, A: 0xff}
case '9':
activeColor = color.NRGBA{R: 0x55, G: 0x55, B: 0xff, A: 0xff}
case 'a':
activeColor = color.NRGBA{R: 0x55, G: 0xff, B: 0x55, A: 0xff}
case 'b':
activeColor = color.NRGBA{R: 0x55, G: 0xff, B: 0xff, A: 0xff}
case 'c':
activeColor = color.NRGBA{R: 0xff, G: 0x55, B: 0x55, A: 0xff}
case 'd':
activeColor = color.NRGBA{R: 0xff, G: 0x55, B: 0xff, A: 0xff}
case 'e':
activeColor = color.NRGBA{R: 0xff, G: 0xff, B: 0x55, A: 0xff}
case 'f':
activeColor = color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
case 'g':
activeColor = color.NRGBA{R: 0xDD, G: 0xD6, B: 0x05, A: 0xff}
case 'h':
activeColor = color.NRGBA{R: 0xE3, G: 0xD4, B: 0xD1, A: 0xff}
case 'i':
activeColor = color.NRGBA{R: 0xCE, G: 0xCA, B: 0xCA, A: 0xff}
case 'j':
activeColor = color.NRGBA{R: 0x44, G: 0x3A, B: 0x3B, A: 0xff}
case 'm':
activeColor = color.NRGBA{R: 0x97, G: 0x16, B: 0x07, A: 0xff}
case 'n':
activeColor = color.NRGBA{R: 0xB4, G: 0x68, B: 0x4D, A: 0xff}
case 'p':
activeColor = color.NRGBA{R: 0xDE, G: 0xB1, B: 0x2D, A: 0xff}
case 'q':
activeColor = color.NRGBA{R: 0x97, G: 0xA0, B: 0x36, A: 0xff}
case 's':
activeColor = color.NRGBA{R: 0x2C, G: 0xBA, B: 0xA8, A: 0xff}
case 't':
activeColor = color.NRGBA{R: 0x21, G: 0x49, B: 0x7B, A: 0xff}
case 'u':
activeColor = color.NRGBA{R: 0x21, G: 0x49, B: 0x7B, A: 0xff}
case 'r':
activeColor = color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}
bold, italic, obfuscated = false, false, false
case 'l':
bold = true
case 'o':
italic = true
case 'k':
obfuscated = true
}
_ = obfuscated

partT := string(partR[2:])

if len(partT) == 0 {
continue
}

var fontStyle font.Style = font.Regular
if italic {
fontStyle = font.Italic
}
var fontWeight font.Weight = font.Normal
if bold {
fontWeight = font.Bold
}

Styles = append(Styles, styledtext.SpanStyle{
Font: font.Font{
Typeface: th.Face,
Style: fontStyle,
Weight: fontWeight,
},
Size: size,
Color: activeColor,
Content: partT,
})
}

return func(gtx layout.Context) layout.Dimensions {
return styledtext.TextStyle{
Styles: Styles,
Shaper: th.Shaper,
}.Layout(gtx, nil)
}
}
88 changes: 56 additions & 32 deletions ui/gui/pages/packs/packs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"image"
"image/color"
"image/png"
"io/fs"
"strings"
"sync"

"gioui.org/f32"
Expand All @@ -14,9 +17,11 @@ import (
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/component"
"github.com/bedrock-tool/bedrocktool/ui/gui/mctext"
"github.com/bedrock-tool/bedrocktool/ui/gui/pages"
"github.com/bedrock-tool/bedrocktool/ui/messages"
"github.com/bedrock-tool/bedrocktool/utils"
"github.com/sirupsen/logrus"
)

type (
Expand Down Expand Up @@ -44,11 +49,12 @@ type packEntry struct {
Icon paint.ImageOp
button widget.Clickable

Size uint64
Loaded uint64
Name string
Path string
Err error
Size uint64
Loaded uint64
Name string
Version string
Path string
Err error
}

func New() pages.Page {
Expand Down Expand Up @@ -122,21 +128,23 @@ func drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {

return layout.Inset{
Top: 5, Bottom: 5,
Left: 0, Right: 5,
Left: 5, Right: 5,
}.Layout(gtx, func(gtx C) D {
fn := func(gtx C) D {
return component.Surface(&material.Theme{
Palette: material.Palette{
Bg: component.WithAlpha(th.Fg, 10),
},
}).Layout(gtx, func(gtx C) D {
return component.Surface(&material.Theme{
Palette: material.Palette{
Bg: component.WithAlpha(th.Fg, 10),
},
}).Layout(gtx, func(gtx C) D {
return layout.UniformInset(5).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return drawPackIcon(gtx, pack.HasIcon, pack.Icon, image.Pt(50, 50))
iconSize := image.Pt(50, 50)
return drawPackIcon(gtx, pack.HasIcon, pack.Icon, iconSize)
}),
layout.Flexed(1, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(material.Label(th, th.TextSize, pack.Name).Layout),
layout.Rigid(mctext.Label(th, th.TextSize, pack.Name)),
layout.Rigid(material.Label(th, th.TextSize, pack.Version).Layout),
layout.Rigid(material.LabelStyle{
Text: size,
Color: colorSize,
Expand Down Expand Up @@ -178,16 +186,18 @@ func drawPackEntry(gtx C, th *material.Theme, pack *packEntry) D {
}),
)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if pack.Path != "" {
return layout.Flex{
Axis: layout.Vertical,
Alignment: layout.End,
}.Layout(gtx, layout.Rigid(material.Button(th, &pack.button, "Show").Layout))
}
return layout.Dimensions{}
}),
)
})
}

if pack.Path != "" {
return material.Clickable(gtx, &pack.button, fn)
} else {
return fn(gtx)
}

})
})
}

Expand Down Expand Up @@ -282,7 +292,8 @@ func (p *Page) HandleMessage(msg *messages.Message) *messages.Message {
p.Packs = append(p.Packs, &packEntry{
IsFinished: false,
UUID: dp.UUID,
Name: dp.SubPackName + " v" + dp.Version,
Name: strings.ReplaceAll(dp.SubPackName, "\n", " "),
Version: dp.Version,
Size: dp.Size,
})
}
Expand All @@ -299,18 +310,31 @@ func (p *Page) HandleMessage(msg *messages.Message) *messages.Message {
p.l.Unlock()

case messages.FinishedPack:
for _, pe := range p.Packs {
if pe.UUID == m.Pack.UUID() {
if m.Pack.Icon() != nil {
pe.Icon = paint.NewImageOp(m.Pack.Icon())
pe.Icon.Filter = paint.FilterNearest
pe.HasIcon = true
var icon image.Image
if fs, _ := fs.Sub(m.Pack, m.Pack.BaseDir()); fs != nil {
f, err := fs.Open("pack_icon.png")
if err == nil {
defer f.Close()
icon, err = png.Decode(f)
if err != nil {
logrus.Warnf("Failed to Parse pack_icon.png %s", m.Pack.Name())
}
pe.Name = m.Pack.Name() + " v" + m.Pack.Version()
pe.Loaded = pe.Size
break
}
}
for _, pe := range p.Packs {
if pe.UUID != m.Pack.UUID() {
continue
}
if icon != nil {
pe.Icon = paint.NewImageOp(icon)
pe.Icon.Filter = paint.FilterNearest
pe.HasIcon = true
}
pe.Name = strings.ReplaceAll(m.Pack.Name(), "\n", " ")
pe.Version = m.Pack.Version()
pe.Loaded = pe.Size
break
}

case messages.ProcessingPack:
p.l.Lock()
Expand Down
2 changes: 1 addition & 1 deletion utils/behaviourpack/bp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func fsFileExists(f fs.FS, name string) bool {
return true
}

func (bp *Pack) CheckAddLink(pack utils.Pack) {
func (bp *Pack) CheckAddLink(pack resource.Pack) {
hasBlocksJson := bp.HasBlocks() && fsFileExists(pack, "blocks.json")
hasEntitiesFolder := bp.HasEntities() && fsFileExists(pack, "entity")
hasItemsFolder := bp.HasItems() && fsFileExists(pack, "items")
Expand Down
3 changes: 2 additions & 1 deletion utils/chunk_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/df-mc/dragonfly/server/world"
"github.com/df-mc/dragonfly/server/world/chunk"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/resource"
)

func isBlockLightblocking(b world.Block) bool {
Expand Down Expand Up @@ -151,7 +152,7 @@ type ChunkRenderer struct {
customBlockColors map[string]color.RGBA
}

func (cr *ChunkRenderer) ResolveColors(entries []protocol.BlockEntry, packs []Pack) {
func (cr *ChunkRenderer) ResolveColors(entries []protocol.BlockEntry, packs []resource.Pack) {
colors := ResolveColors(entries, packs)
cr.customBlockColors = colors
}
Expand Down
2 changes: 1 addition & 1 deletion utils/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func calculateMeanAverageColour(img image.Image) (c color.RGBA) {
}
}

func ResolveColors(entries []protocol.BlockEntry, packs []Pack) map[string]color.RGBA {
func ResolveColors(entries []protocol.BlockEntry, packs []resource.Pack) map[string]color.RGBA {
log := logrus.WithField("func", "ResolveColors")
colors := make(map[string]color.RGBA)

Expand Down
Loading

0 comments on commit c7732dc

Please sign in to comment.