Skip to content

Commit

Permalink
Merge pull request #194 from goplus/main
Browse files Browse the repository at this point in the history
Release v1.0.0-rc5
  • Loading branch information
xushiwei authored Jan 5, 2022
2 parents 648ba1f + 4d26cb6 commit 88a40c6
Show file tree
Hide file tree
Showing 25 changed files with 362 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ _test
haiyang/
feijidazhan/
.idea
*.code-workspace
*.code-workspace
*.wasm

# Binaries for programs and plugins
*.exe
Expand Down
31 changes: 0 additions & 31 deletions dir.go

This file was deleted.

7 changes: 0 additions & 7 deletions dir_js.go

This file was deleted.

3 changes: 2 additions & 1 deletion game.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func (p *Game) initGame() {

// Gopt_Game_Main is required by Go+ compiler as the entry of a .gmx project.
func Gopt_Game_Main(game Gamer) {
setupWorkDir()
game.initGame()
game.(interface{ MainEntry() }).MainEntry()
}
Expand Down Expand Up @@ -1294,11 +1293,13 @@ type EffectKind int
const (
ColorEffect EffectKind = iota
BrightnessEffect
GhostEffect
)

var greffNames = []string{
ColorEffect: "Color",
BrightnessEffect: "Brightness",
GhostEffect: "Ghost",
}

func (kind EffectKind) String() string {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ajstarks/svgo v0.0.0-20210927141636-6d70534b1098
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/goplus/canvas v0.1.0
github.com/goplus/gop v1.0.37
github.com/goplus/gop v1.1.0-alpha2
github.com/hajimehoshi/ebiten/v2 v2.2.0
github.com/pkg/errors v0.9.1
github.com/qiniu/audio v0.2.1
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/goplus/canvas v0.1.0 h1:Vx3f2+U8UANvWf5/01YsQYKNbZDm1GZCjhlEBFrQkeU=
github.com/goplus/canvas v0.1.0/go.mod h1:Rhcvo5qkpD9WuXFnvnXtrBSY97l6h7sXQuofrmiLNdM=
github.com/goplus/gop v1.0.37 h1:U7/Asf3IyJBC38DXii56JF5hw94TayUs5Vg6XT5Wi14=
github.com/goplus/gop v1.0.37/go.mod h1:JMjZXjcfATZdT+1SIU4bXpaXJIvs6xTNVzjI8+XQhRM=
github.com/goplus/gox v1.8.7/go.mod h1:XWqV75HImeysI8+Q9gAdYyvwlP8jds24xsgYRwTO3JI=
github.com/goplus/gop v1.1.0-alpha2 h1:s3wMFJpprmFBmVRqUIoggQ2wJ3Kb3gX+7J0f8f2Oh8Q=
github.com/goplus/gop v1.1.0-alpha2/go.mod h1:FTu64eb9ZYCwnTfSze6asR8kbxXPs6mbI+kRmUikKf0=
github.com/goplus/gox v1.9.4/go.mod h1:PznHkzl2HARBf7+s2reqcwKm1Z1a6Wae6EntQEh0iJI=
github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.2.0 h1:2mP9HrLLqiH9X3MajElYZEjVZU/CGh22iFkjatxhT4w=
github.com/hajimehoshi/ebiten/v2 v2.2.0/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0=
Expand Down
2 changes: 1 addition & 1 deletion internal/audiorecord/audiorecord_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"syscall/js"
)

const scriptCode = `!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).GopAudioRecorder={})}(this,function(e){var t=new(window.AudioContext||window.webkitAudioContext)({latencyHint:"interactive"}),n=t.createAnalyser();n.fftSize=2048;var o=t.createGain();o.gain.value=0,n.connect(o),o.connect(t.destination);var i,r,a,c=new Uint8Array(n.frequencyBinCount);function s(){i&&(i.getTracks().forEach(function(e){return e.stop()}),i=void 0),r&&(r.disconnect(),r=void 0),clearInterval(a),t.suspend()}e.start=function(e){try{return s(),Promise.resolve(navigator.mediaDevices.getUserMedia({audio:!0})).then(function(o){(r=t.createMediaStreamSource(i=o)).connect(n),t.resume(),a=setInterval(function(){var o,i;n.getByteFrequencyData(c),e((o=0,i=c.length,c.forEach(function(e,n){var r=n*(t.sampleRate||44100)/i;if(r>22050)i-=1;else{var a,c,s=187374169.94399998*(c=(a=r)*a)*c/((c+424.36)*Math.sqrt((c+11599.29)*(c+544496.41))*(c+14884e4))*e/255;s<=0?i-=1:o+=s*s}}),0===i?0:Math.sqrt(o/i)))},100)})}catch(e){return Promise.reject(e)}},e.stop=function(){s()}});`
const scriptCode = `!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).GopAudioRecorder={})}(this,function(t){var e,i=/*#__PURE__*/function(){function t(t){this.audioContext=t,this.connectingToMic=!1,this.mic=null}var e=t.prototype;return e.getLoudness=function(){var t=this;if(this.mic||this.connectingToMic||(this.connectingToMic=!0,navigator.mediaDevices.getUserMedia({audio:!0}).then(function(e){t.audioStream=e,t.mic=t.audioContext.createMediaStreamSource(e),t.analyser=t.audioContext.createAnalyser(),t.mic.connect(t.analyser),t.micDataArray=new Float32Array(t.analyser.fftSize)}).catch(function(t){console.warn(t)})),this.mic&&this.audioStream&&this.audioStream.active){this.analyser.getFloatTimeDomainData(this.micDataArray);for(var e=0,i=0;i<this.micDataArray.length;i++)e+=Math.pow(this.micDataArray[i],2);var a=Math.sqrt(e/this.micDataArray.length);return this._lastValue&&(a=Math.max(a,.6*this._lastValue)),this._lastValue=a,a*=1.63,a=Math.sqrt(a),a=Math.round(100*a),(a=Math.min(a,100))/100}return 0},e.release=function(){this.connectingToMic=!1,this.mic&&(this.mic.disconnect(),this.mic=null),this.audioStream&&(this.audioStream.getTracks().forEach(function(t){return t.stop()}),this.audioStream=void 0),this.analyser&&(this.analyser.disconnect(),this.analyser=void 0)},t}(),a=new(window.AudioContext||window.webkitAudioContext)({latencyHint:"interactive"}),n=new i(a);t.start=function(t){try{return clearInterval(e),a.resume(),e=setInterval(function(){t(n.getLoudness())},100),Promise.resolve()}catch(t){return Promise.reject(t)}},t.stop=function(){clearInterval(e),n.release()}});`

var scriptInited bool

Expand Down
7 changes: 7 additions & 0 deletions internal/effect/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ShaderFrag = []byte(`package main
var (
Color float
Brightness float
Ghost float
)
func convertRGB2HSV(rgb vec3) vec3 {
Expand Down Expand Up @@ -109,6 +110,12 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
txtcolor = vec4(rgb,txtcolor.a)
}
//0 ~ 100
if Ghost > 0.0{
//1 - (Math.max(0, Math.min(x, 100)) / 100)
txtcolor *= 1.0 - (max(0.0, min(Ghost, 100.0)) / 100.0)
}
return txtcolor
}
`)
170 changes: 170 additions & 0 deletions quote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package spx

import (
"fmt"
"math"

"github.com/goplus/spx/internal/gdi"
xfont "github.com/goplus/spx/internal/gdi/font"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/image/colornames"
"golang.org/x/image/font"
)

const (
quotePadding = 5.0
quoteLineWidth = 8.0
quoteHeadLen = 16.0
quoteTextPadding = 3.0
quoteBorderRadis = 10.0
)

var (
quoteMsgFont gdi.Font
quoteDesFont gdi.Font
)

func init() {
const dpi = 72
quoteMsgFont = xfont.NewDefault(&xfont.Options{
Size: 35,
DPI: dpi,
Hinting: font.HintingFull,
})
quoteDesFont = xfont.NewDefault(&xfont.Options{
Size: 18,
DPI: dpi,
Hinting: font.HintingFull,
})
}

type quoter struct {
sprite *Sprite
message string
description string

cachedImg *ebiten.Image
}

func (p *Sprite) quote_(message, description string) {
old := p.quoteObj
if old == nil {
p.quoteObj = &quoter{sprite: p, message: message, description: description}
p.g.addShape(p.quoteObj)
} else {
old.message, old.description = message, description
old.cachedImg = nil
p.g.activateShape(old)
}
}

func (p *Sprite) waitStopQuote(secs float64) {
p.g.Wait(secs)
p.doStopQuote()
}

func (p *Sprite) doStopQuote() {
if p.quoteObj != nil {
p.g.removeShape(p.quoteObj)
p.quoteObj = nil
}
}

func (p *quoter) draw(dc drawContext) {
img := p.getImage()
if img == nil {
return
}
imgW, imgH := img.Size()
w, h := dc.Size()
op := new(ebiten.DrawImageOptions)
x := p.sprite.x + float64(w)/2 - float64(imgW)/2
y := -p.sprite.y - quotePadding - float64(imgH) + float64(h)/2 + float64(imgH)/2
op.GeoM.Translate(x, y)
dc.DrawImage(img, op)
}

func (p *quoter) getImage() *ebiten.Image {
if p.cachedImg != nil {
return p.cachedImg
}
bound := p.sprite.getRotatedRect()
w := math.Max(bound.Size.Height, bound.Size.Width)
w += quotePadding + quoteLineWidth
h := w * 1.15
quoteHeight := h
msgRender := gdi.NewTextRender(quoteMsgFont, 135, 2)
msgRender.AddText(p.message)
msgW, msgH := msgRender.Size()
h += float64(msgH / 2)
desRender := gdi.NewTextRender(quoteDesFont, 135, 2)
var desW, desH int
if p.description != "" {
desRender.AddText((p.description))
desW, desH = desRender.Size()
h += float64(desH + quoteTextPadding)
}

svg := gdi.NewSVG(int(w), int(h))
mainH := int(h) - msgH/2
dy := 0.0
if p.description != "" {
dy = float64(desH) + quoteTextPadding
mainH -= int(dy)
}
half := fmt.Sprintf("m 0 %f q 0 %f %f %f h %f q %f %f 0 %f h -%f v %f h %f q %f %f 0 %f h -%f q %f 0 %f %f z",
dy+quoteBorderRadis,

-quoteBorderRadis,
quoteBorderRadis,
-quoteBorderRadis,

quoteHeadLen,

quoteLineWidth/2,
quoteLineWidth/2,
quoteLineWidth,

quoteHeadLen+3,

float64(mainH)-2*quoteLineWidth,

quoteHeadLen+3,

quoteLineWidth/2,
quoteLineWidth/2,
quoteLineWidth,

quoteHeadLen,

-quoteBorderRadis,
-quoteBorderRadis,
-quoteBorderRadis,
)
svg.Def()
svg.Path(half, `id="quote"`)
svg.DefEnd()
// "["
style := "fill:rgb(144,169,55);stroke:black;"
svg.Use(0, 0, "#quote", style)
// "]"
svg.Gtransform(fmt.Sprintf("rotate(%.1f %f %f)", 180.0, w/2, quoteHeight/2+dy))
svg.Use(0, 0, "#quote", style)
svg.Gend()
svg.End()

img, err := svg.ToImage()
if err != nil {
panic(err)
}
p.cachedImg = ebiten.NewImageFromImage(img)
msgRender.Draw(p.cachedImg, (int(w)-msgW)/2, int(h)-msgH, colornames.White, 0)
if p.description != "" {
desRender.Draw(p.cachedImg, (int(w)-desW)/2, 0, colornames.White, 0)
}
return p.cachedImg
}

func (p *quoter) hit(hc hitContext) (hr hitResult, ok bool) {
return
}
37 changes: 36 additions & 1 deletion say.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spx

import (
"fmt"
"image/color"
"strconv"
"strings"
Expand Down Expand Up @@ -71,7 +72,6 @@ func (p *sayOrThinker) draw(dc drawContext) {
w, h := render.Size()
x, y := topx+2, topy-h-(trackCy+24)


pad := 9
w += (pad << 1)
h += (pad << 1)
Expand Down Expand Up @@ -163,3 +163,38 @@ func (p *sayOrThinker) hit(hc hitContext) (hr hitResult, ok bool) {
}

// -------------------------------------------------------------------------------------

func (p *Sprite) sayOrThink(msgv interface{}, style int) {
msg, ok := msgv.(string)
if !ok {
msg = fmt.Sprint(msgv)
}

if msg == "" {
p.doStopSay()
return
}

old := p.sayObj
if old == nil {
p.sayObj = &sayOrThinker{sp: p, msg: msg, style: style}
p.g.addShape(p.sayObj)
} else {
old.msg, old.style = msg, style
p.g.activateShape(old)
}
}

func (p *Sprite) waitStopSay(secs float64) {
p.g.Wait(secs)
p.doStopSay()
}

func (p *Sprite) doStopSay() {
if p.sayObj != nil {
p.g.removeShape(p.sayObj)
p.sayObj = nil
}
}

// -------------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions spgdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ func (p *Sprite) getRotatedRect() (rRect *math32.RotatedRect) {

func (p *Sprite) getTrackPos() (topx, topy int) {
rRect := p.getRotatedRect()
if rRect == nil {
return
}

pos := &math32.Vector2{
X: float64(rRect.Center.X),
Expand Down
Loading

0 comments on commit 88a40c6

Please sign in to comment.