diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BuffIndicator.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BuffIndicator.java index fbd27457d..0e40cede3 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BuffIndicator.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BuffIndicator.java @@ -34,7 +34,9 @@ import com.watabou.noosa.TextureFilm; import com.watabou.noosa.tweeners.AlphaTweener; import com.watabou.noosa.ui.Button; import com.watabou.noosa.ui.Component; -import com.watabou.utils.SparseArray; + +import java.util.ArrayList; +import java.util.LinkedHashMap; public class BuffIndicator extends Component { @@ -92,8 +94,8 @@ public class BuffIndicator extends Component { private SmartTexture texture; private TextureFilm film; - private SparseArray icons = new SparseArray(); - + private LinkedHashMap buffIcons = new LinkedHashMap<>(); + private boolean needsRefresh; private Char ch; public BuffIndicator( Char ch ) { @@ -120,24 +122,29 @@ public class BuffIndicator extends Component { film = new TextureFilm( texture, SIZE, SIZE ); } + @Override + public synchronized void update() { + super.update(); + if (needsRefresh){ + layout(); + needsRefresh = false; + } + } + @Override protected void layout() { - clear(); - - SparseArray newIcons = new SparseArray(); + ArrayList newBuffs = new ArrayList<>(); for (Buff buff : ch.buffs()) { if (buff.icon() != NONE) { - BuffIcon icon = new BuffIcon( buff ); - icon.setRect(x + members.size() * (SIZE + 2), y, 9, 12); - add(icon); - newIcons.put( buff.icon(), icon ); + newBuffs.add(buff); } } - for (Integer key : icons.keyArray()) { - if (newIcons.get( key ) == null) { - Image icon = icons.get( key ).icon; + //remove any icons no longer present + for (Buff buff : buffIcons.keySet().toArray(new Buff[0])){ + if (!newBuffs.contains(buff)){ + Image icon = buffIcons.get( buff ).icon; icon.origin.set( SIZE / 2 ); add( icon ); add( new AlphaTweener( icon, 0, 0.6f ) { @@ -145,19 +152,36 @@ public class BuffIndicator extends Component { protected void updateValues( float progress ) { super.updateValues( progress ); image.scale.set( 1 + 5 * progress ); - }; - + } + @Override protected void onComplete() { image.killAndErase(); } } ); - - icons.get( key ).destroy(); + + buffIcons.get( buff ).destroy(); + remove(buffIcons.get( buff )); + buffIcons.remove( buff ); } } - icons = newIcons; + //add new icons + for (Buff buff : newBuffs) { + if (!buffIcons.containsKey(buff)) { + BuffIcon icon = new BuffIcon( buff ); + add(icon); + buffIcons.put( buff, icon ); + } + } + + //layout + int pos = 0; + for (BuffIcon icon : buffIcons.values()){ + icon.updateIcon(); + icon.setRect(x + pos * (SIZE + 2), y, 9, 12); + pos++; + } } private class BuffIcon extends Button { @@ -174,6 +198,10 @@ public class BuffIndicator extends Component { icon.frame( film.get( buff.icon() ) ); add( icon ); } + + public void updateIcon(){ + icon.frame( film.get( buff.icon() ) ); + } @Override protected void layout() { @@ -191,7 +219,7 @@ public class BuffIndicator extends Component { public static void refreshHero() { if (heroInstance != null) { - heroInstance.layout(); + heroInstance.needsRefresh = true; } } }