Skip to content

Commit

Permalink
exclude animation costumes for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Jan 24, 2025
1 parent e740289 commit 590f880
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
14 changes: 7 additions & 7 deletions spx-gui/src/models/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export type AnimationInits = {

export type RawAnimationConfig = {
builder_id?: string
frameFrom?: number | string
frameTo?: number | string
frameFrom?: string
frameTo?: string
frameFps?: number
onStart?: ActionConfig

Expand Down Expand Up @@ -134,12 +134,12 @@ export class Animation extends Disposable {
costumes: Costume[],
{ sounds, includeId = true }: AnimationExportLoadOptions
): [animation: Animation, animationCostumeNames: string[]] {
frameFrom = frameFrom ?? from
frameTo = frameTo ?? to
const finalFrom = frameFrom ?? from
const finalTo = frameTo ?? to
frameFps = frameFps ?? fps
if (frameFrom == null || frameTo == null) throw new Error(`from and to expected for Animation ${name}`)
const fromIndex = getCostumeIndex(costumes, frameFrom)
const toIndex = getCostumeIndex(costumes, frameTo)
if (finalFrom == null || finalTo == null) throw new Error(`from and to expected for Animation ${name}`)
const fromIndex = getCostumeIndex(costumes, finalFrom)
const toIndex = getCostumeIndex(costumes, finalTo)
const animationCostumes = costumes.slice(fromIndex, toIndex + 1).map((c) => c.clone(true))
const duration = animationCostumes.length / (frameFps ?? defaultFps)
// drop spx `duration`, which is different from ours
Expand Down
4 changes: 2 additions & 2 deletions tools/spxls/internal/server/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ func (ctx *completionContext) collectTypeSpecific(typ types.Type) error {
expectedSpxSprite := ctx.getSpxSpriteResource()
for _, spxSprite := range ctx.result.spxResourceSet.sprites {
if expectedSpxSprite == nil || spxSprite == expectedSpxSprite {
spxResourceIds = slices.Grow(spxResourceIds, len(spxSprite.Costumes))
for _, spxSpriteCostume := range spxSprite.Costumes {
spxResourceIds = slices.Grow(spxResourceIds, len(spxSprite.NormalCostumes))
for _, spxSpriteCostume := range spxSprite.NormalCostumes {
spxResourceIds = append(spxResourceIds, SpxSpriteCostumeResourceID{spxSprite.Name, spxSpriteCostume.Name})
}
}
Expand Down
69 changes: 53 additions & 16 deletions tools/spxls/internal/server/spx_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,26 @@ func NewSpxResourceSet(rootFS fs.FS) (*SpxResourceSet, error) {

// Process animations.
sprite.Animations = make([]SpxSpriteAnimationResource, 0, len(sprite.FAnimations))
for animName := range sprite.FAnimations {
for animName, fAnim := range sprite.FAnimations {
sprite.Animations = append(sprite.Animations, SpxSpriteAnimationResource{
ID: SpxSpriteAnimationResourceID{SpriteName: spriteName, AnimationName: animName},
Name: animName,
ID: SpxSpriteAnimationResourceID{SpriteName: spriteName, AnimationName: animName},
Name: animName,
FromIndex: getCostumeIndex(fAnim.FrameFrom, sprite.Costumes),
ToIndex: getCostumeIndex(fAnim.FrameTo, sprite.Costumes),
})
}

// Process normal costumes.
sprite.NormalCostumes = make([]SpxSpriteCostumeResource, 0, len(sprite.Costumes))
for i, costume := range sprite.Costumes {
isAnimation := slices.ContainsFunc(sprite.Animations, func(anim SpxSpriteAnimationResource) bool {
return anim.includeCostume(i)
})
if !isAnimation {
sprite.NormalCostumes = append(sprite.NormalCostumes, costume)
}
}

set.sprites[spriteName] = &sprite
}

Expand Down Expand Up @@ -265,15 +278,22 @@ func (id SpxSoundResourceID) URI() SpxResourceURI {
return SpxResourceURI(fmt.Sprintf("spx://resources/sounds/%s", id.SoundName))
}

type spxSpriteFAnimation struct {
FrameFrom string `json:"frameFrom"`
FrameTo string `json:"frameTo"`
}

// SpxSpriteResource represents an spx sprite resource.
type SpxSpriteResource struct {
ID SpxSpriteResourceID `json:"-"`
Name string `json:"name"`
Costumes []SpxSpriteCostumeResource `json:"costumes"`
CostumeIndex int `json:"costumeIndex"`
FAnimations map[string]json.RawMessage `json:"fAnimations"`
Animations []SpxSpriteAnimationResource `json:"-"`
DefaultAnimation string `json:"defaultAnimation"`
ID SpxSpriteResourceID `json:"-"`
Name string `json:"name"`
Costumes []SpxSpriteCostumeResource `json:"costumes"`
// NormalCostumes includes all costumes except animation costumes.
NormalCostumes []SpxSpriteCostumeResource `json:"-"`
CostumeIndex int `json:"costumeIndex"`
FAnimations map[string]spxSpriteFAnimation `json:"fAnimations"`
Animations []SpxSpriteAnimationResource `json:"-"`
DefaultAnimation string `json:"defaultAnimation"`
}

// SpxSpriteResourceID is the ID of an spx sprite resource.
Expand Down Expand Up @@ -315,10 +335,9 @@ func (sprite *SpxSpriteResource) Animation(name string) *SpxSpriteAnimationResou

// SpxSpriteCostumeResource represents an spx sprite costume resource.
type SpxSpriteCostumeResource struct {
ID SpxSpriteCostumeResourceID `json:"-"`
Index int `json:"index"`
Name string `json:"name"`
Path string `json:"path"`
ID SpxSpriteCostumeResourceID `json:"-"`
Name string `json:"name"`
Path string `json:"path"`
}

// SpxSpriteCostumeResourceID is the ID of an spx sprite costume resource.
Expand All @@ -339,8 +358,17 @@ func (id SpxSpriteCostumeResourceID) URI() SpxResourceURI {

// SpxSpriteAnimationResource represents an spx sprite animation resource.
type SpxSpriteAnimationResource struct {
ID SpxSpriteAnimationResourceID `json:"-"`
Name string `json:"name"`
ID SpxSpriteAnimationResourceID `json:"-"`
Name string `json:"name"`
FromIndex *int `json:"-"`
ToIndex *int `json:"-"`
}

func (a *SpxSpriteAnimationResource) includeCostume(index int) bool {
if a.FromIndex == nil || a.ToIndex == nil {
return false
}
return *a.FromIndex <= index && index <= *a.ToIndex
}

// SpxSpriteAnimationResourceID is the ID of an spx sprite animation resource.
Expand Down Expand Up @@ -382,3 +410,12 @@ func (id SpxWidgetResourceID) Name() string {
func (id SpxWidgetResourceID) URI() SpxResourceURI {
return SpxResourceURI(fmt.Sprintf("spx://resources/widgets/%s", id.WidgetName))
}

func getCostumeIndex(name string, costumes []SpxSpriteCostumeResource) *int {
for i, costume := range costumes {
if costume.Name == name {
return &i
}
}
return nil
}

0 comments on commit 590f880

Please sign in to comment.