Skip to content

Commit

Permalink
add: score new style (#655)
Browse files Browse the repository at this point in the history
* newstyle

* make lint happy
  • Loading branch information
Jiang-Red authored Apr 4, 2023
1 parent 76dbf5a commit ed26d31
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 65 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Baidu-AIP/golang-sdk v1.1.1
github.com/FloatTech/AnimeAPI v1.6.1-0.20230331074616-e33f890df71e
github.com/FloatTech/floatbox v0.0.0-20230331064925-9af336a84944
github.com/FloatTech/gg v1.1.2
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08
github.com/FloatTech/imgfactory v0.2.2-0.20230322091809-b0ddbe44b94b
github.com/FloatTech/rendercard v0.0.10-0.20230223064326-45d29fa4ede9
github.com/FloatTech/sqlite v1.5.7
Expand All @@ -24,7 +24,6 @@ require (
github.com/fumiama/go-base16384 v1.6.4
github.com/fumiama/go-registry v0.2.6
github.com/fumiama/gotracemoe v0.0.3
github.com/fumiama/imgsz v0.0.2
github.com/fumiama/jieba v0.0.0-20221203025406-36c17a10b565
github.com/fumiama/unibase2n v0.0.0-20221020155353-02876e777430
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
Expand Down Expand Up @@ -52,6 +51,7 @@ require (
github.com/faiface/beep v1.1.0 // indirect
github.com/fumiama/go-simple-protobuf v0.1.0 // indirect
github.com/fumiama/gofastTEA v0.0.10 // indirect
github.com/fumiama/imgsz v0.0.2 // indirect
github.com/gabriel-vasile/mimetype v1.0.4 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/FloatTech/AnimeAPI v1.6.1-0.20230331074616-e33f890df71e h1:4V7UZyLZKm
github.com/FloatTech/AnimeAPI v1.6.1-0.20230331074616-e33f890df71e/go.mod h1:LSJN8VkJXBhyAdOolVNeUptJz6l1TZ+/CfXN1OafyEY=
github.com/FloatTech/floatbox v0.0.0-20230331064925-9af336a84944 h1:/eQoMa6Aj3coF5F7yhzZe1+SzX6SItul7MW8//pl18o=
github.com/FloatTech/floatbox v0.0.0-20230331064925-9af336a84944/go.mod h1:FwQm6wk+b4wuW54KCKn3zccMX47Q5apnHD/Yakzv0fI=
github.com/FloatTech/gg v1.1.2 h1:YolgOYg3uDHc1+g0bLtt6QuRA/pvLn+b9IBCIhOOX88=
github.com/FloatTech/gg v1.1.2/go.mod h1:uzPzAeT35egARdRuu+1oyjU3CmTwCceoq3Vvje7LpcI=
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08 h1:dPLeoiTVSBlgls+66EB/UJ2e38BaASmBN5nANaycSBU=
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08/go.mod h1:uzPzAeT35egARdRuu+1oyjU3CmTwCceoq3Vvje7LpcI=
github.com/FloatTech/imgfactory v0.2.2-0.20230322091809-b0ddbe44b94b h1:VMNci4SWBySdw/6poqF9Dn9zlT5ntTFSJOEEBjRnJ/4=
github.com/FloatTech/imgfactory v0.2.2-0.20230322091809-b0ddbe44b94b/go.mod h1:el5hGpj1C1bDRxcTXYRwEivDCr40zZeJpcrLrB1fajs=
github.com/FloatTech/rendercard v0.0.10-0.20230223064326-45d29fa4ede9 h1:hffajvmQFfP68U6wUwHemPuuwCUoss+SEFfoLYwbGwE=
Expand Down
219 changes: 219 additions & 0 deletions plugin/score/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package score

import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"strconv"
"sync"
"time"

"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/gg"
"github.com/FloatTech/imgfactory"
"github.com/FloatTech/rendercard"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/img/text"
"github.com/disintegration/imaging"
Expand Down Expand Up @@ -269,3 +272,219 @@ func drawScore17(a *scdata) (image.Image, error) {
canvas.SetRGB255(255, 255, 255)
return canvas.Image(), nil
}

func drawScore17b2(a *scdata) (img image.Image, err error) {
fontdata, err := file.GetLazyData(text.GlowSansFontFile, control.Md5File, false)
if err != nil {
return
}

getAvatar, err := initPic(a.picfile, a.uid)
if err != nil {
return
}

back, err := gg.LoadImage(a.picfile)
if err != nil {
return
}

bx, by := float64(back.Bounds().Dx()), float64(back.Bounds().Dy())

sc := 1280 / bx

colors := gg.TakeColor(back, 3)

canvas := gg.NewContext(1280, 1280*int(by)/int(bx))

cw, ch := float64(canvas.W()), float64(canvas.H())

sch := ch * 6 / 10

var blurback, scbackimg, backshadowimg, avatarimg, avatarbackimg, avatarshadowimg, whitetext, blacktext image.Image
var wg sync.WaitGroup
wg.Add(8)

go func() {
defer wg.Done()
scback := gg.NewContext(canvas.W(), canvas.H())
scback.ScaleAbout(sc, sc, cw/2, ch/2)
scback.DrawImageAnchored(back, canvas.W()/2, canvas.H()/2, 0.5, 0.5)
scback.Identity()

go func() {
defer wg.Done()
blurback = imaging.Blur(scback.Image(), 20)
}()

scbackimg = rendercard.Fillet(scback.Image(), 12)
}()

go func() {
defer wg.Done()
pureblack := gg.NewContext(canvas.W(), canvas.H())
pureblack.SetRGBA255(0, 0, 0, 255)
pureblack.Clear()

shadow := gg.NewContext(canvas.W(), canvas.H())
shadow.ScaleAbout(0.6, 0.6, cw-cw/3, ch/2)
shadow.DrawImageAnchored(pureblack.Image(), canvas.W()-canvas.W()/3, canvas.H()/2, 0.5, 0.5)
shadow.Identity()

backshadowimg = imaging.Blur(shadow.Image(), 8)
}()

aw, ah := (ch-sch)/2/2/2*3, (ch-sch)/2/2/2*3

go func() {
defer wg.Done()
avatar, _, err := image.Decode(bytes.NewReader(getAvatar))
if err != nil {
return
}

isc := (ch - sch) / 2 / 2 / 2 * 3 / float64(avatar.Bounds().Dy())

scavatar := gg.NewContext(int(aw), int(ah))

scavatar.ScaleAbout(isc, isc, aw/2, ah/2)
scavatar.DrawImageAnchored(avatar, scavatar.W()/2, scavatar.H()/2, 0.5, 0.5)
scavatar.Identity()

avatarimg = rendercard.Fillet(scavatar.Image(), 8)
}()

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/2/2)
if err != nil {
return
}
namew, _ := canvas.MeasureString(a.nickname)

go func() {
defer wg.Done()
avatarshadowimg = imaging.Blur(customrectangle(cw, ch, aw, ah, namew, color.Black), 8)
}()

go func() {
defer wg.Done()
avatarbackimg = customrectangle(cw, ch, aw, ah, namew, colors[0])
}()

go func() {
defer wg.Done()
whitetext, err = customtext(a, fontdata, cw, ch, aw, color.White)
if err != nil {
return
}
}()

go func() {
defer wg.Done()
blacktext, err = customtext(a, fontdata, cw, ch, aw, color.Black)
if err != nil {
return
}
}()

wg.Wait()
if scbackimg == nil || backshadowimg == nil || avatarimg == nil || avatarbackimg == nil || avatarshadowimg == nil || whitetext == nil || blacktext == nil {
err = errors.New("图片渲染失败")
return
}

canvas.DrawImageAnchored(blurback, canvas.W()/2, canvas.H()/2, 0.5, 0.5)

canvas.DrawImage(backshadowimg, 0, 0)

canvas.ScaleAbout(0.6, 0.6, cw-cw/3, ch/2)
canvas.DrawImageAnchored(scbackimg, canvas.W()-canvas.W()/3, canvas.H()/2, 0.5, 0.5)
canvas.Identity()

canvas.DrawImage(avatarshadowimg, 0, 0)
canvas.DrawImage(avatarbackimg, 0, 0)
canvas.DrawImageAnchored(avatarimg, int((ch-sch)/2/2), int((ch-sch)/2/2), 0.5, 0.5)

canvas.DrawImage(blacktext, 2, 2)
canvas.DrawImage(whitetext, 0, 0)

img = canvas.Image()
return
}

func customrectangle(cw, ch, aw, ah, namew float64, rtgcolor color.Color) (img image.Image) {
canvas := gg.NewContext(int(cw), int(ch))
sch := ch * 6 / 10
canvas.DrawRoundedRectangle((ch-sch)/2/2-aw/2-aw/40, (ch-sch)/2/2-aw/2-ah/40, aw+aw/40*2, ah+ah/40*2, 8)
canvas.SetColor(rtgcolor)
canvas.Fill()
canvas.DrawRoundedRectangle((ch-sch)/2/2, (ch-sch)/2/2-ah/4, aw/2+aw/40*5+namew, ah/2, 8)
canvas.Fill()

img = canvas.Image()
return
}

func customtext(a *scdata, fontdata []byte, cw, ch, aw float64, textcolor color.Color) (img image.Image, err error) {
canvas := gg.NewContext(int(cw), int(ch))
canvas.SetColor(textcolor)
scw, sch := cw*6/10, ch*6/10
err = canvas.ParseFontFace(fontdata, (ch-sch)/2/2/2)
if err != nil {
return
}
canvas.DrawStringAnchored(a.nickname, (ch-sch)/2/2+aw/2+aw/40*2, (ch-sch)/2/2, 0, 0.5)
err = canvas.ParseFontFace(fontdata, (ch-sch)/2/2/3*2)
if err != nil {
return
}
canvas.DrawStringAnchored(time.Now().Format("2006/01/02"), cw-cw/6, ch/2-sch/2-canvas.FontHeight(), 0.5, 0.5)

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/2/2)
if err != nil {
return
}
nextrankScore := 0
if a.rank < 10 {
nextrankScore = rankArray[a.rank+1]
} else {
nextrankScore = SCOREMAX
}
nextLevelStyle := strconv.Itoa(a.level) + "/" + strconv.Itoa(nextrankScore)

canvas.DrawStringAnchored("Level "+strconv.Itoa(a.rank), cw/3*2-scw/2, ch/2+sch/2+canvas.FontHeight(), 0, 0.5)
canvas.DrawStringAnchored(nextLevelStyle, cw/3*2+scw/2, ch/2+sch/2+canvas.FontHeight(), 1, 0.5)

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/2/3)
if err != nil {
return
}

canvas.DrawStringAnchored("Create By ZeroBot-Plugin "+banner.Version, 0+4, ch, 0, -0.5)

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/5*3)
if err != nil {
return
}

tempfh := canvas.FontHeight()

canvas.DrawStringAnchored(getHourWord(time.Now()), ((cw-scw)-(cw/3-scw/2))/8, (ch-sch)/2+sch/4, 0, 0.5)

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/5)
if err != nil {
return
}

canvas.DrawStringAnchored("ATRI币 + "+strconv.Itoa(a.inc), ((cw-scw)-(cw/3-scw/2))/8, (ch-sch)/2+sch/4+tempfh, 0, 0.5)
canvas.DrawStringAnchored("EXP + 1", ((cw-scw)-(cw/3-scw/2))/8, (ch-sch)/2+sch/4+tempfh+canvas.FontHeight(), 0, 1)

err = canvas.ParseFontFace(fontdata, (ch-sch)/2/4)
if err != nil {
return
}

canvas.DrawStringAnchored("你有 "+strconv.Itoa(a.score)+" 枚ATRI币", ((cw-scw)-(cw/3-scw/2))/8, (ch-sch)/2+sch/4*3, 0, 0.5)

img = canvas.Image()
return
}
Loading

0 comments on commit ed26d31

Please sign in to comment.