diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java index a1ec5513b..7a3c15dad 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Badges.java @@ -264,6 +264,11 @@ public class Badges { } } + public static int unlocked(boolean global){ + if (global) return Badges.global.size(); + else return Badges.local.size(); + } + public static void validateMonstersSlain() { Badge badge = null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/BadgesScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/BadgesScene.java index bd74d9741..99a302370 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/BadgesScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/BadgesScene.java @@ -24,23 +24,14 @@ package com.shatteredpixel.shatteredpixeldungeon.scenes; import com.shatteredpixel.shatteredpixeldungeon.Assets; import com.shatteredpixel.shatteredpixeldungeon.Badges; import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; -import com.shatteredpixel.shatteredpixeldungeon.effects.BadgeBanner; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.ui.Archs; +import com.shatteredpixel.shatteredpixeldungeon.ui.BadgesGrid; import com.shatteredpixel.shatteredpixeldungeon.ui.ExitButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; import com.shatteredpixel.shatteredpixeldungeon.ui.Window; -import com.shatteredpixel.shatteredpixeldungeon.windows.WndBadge; import com.watabou.noosa.Camera; -import com.watabou.noosa.Game; -import com.watabou.noosa.Image; import com.watabou.noosa.audio.Music; -import com.watabou.noosa.audio.Sample; -import com.watabou.noosa.ui.Button; -import com.watabou.utils.Random; - -import java.util.ArrayList; -import java.util.List; public class BadgesScene extends PixelScene { @@ -60,7 +51,7 @@ public class BadgesScene extends PixelScene { archs.setSize( w, h ); add( archs ); - float left = 5; + float margin = 5; float top = 20; RenderedTextBlock title = PixelScene.renderTextBlock( Messages.get(this, "title"), 9 ); @@ -73,41 +64,9 @@ public class BadgesScene extends PixelScene { add(title); Badges.loadGlobal(); - - ArrayList lockedBadges = new ArrayList<>(); - for (Badges.Badge badge : Badges.Badge.values()){ - if (badge.image != -1 && !Badges.isUnlocked(badge)){ - lockedBadges.add(badge); - } - } - Badges.filterHigherIncrementalBadges(lockedBadges); - - List badges = Badges.filterReplacedBadges( true ); - - int totalBadges = lockedBadges.size() + badges.size(); - - //4-5 columns in portrait, 6-8 in landscape - int nCols = landscape() ? 6 : 4; - if (!landscape() && totalBadges > 32) nCols++; - if (landscape() && totalBadges > 24) nCols++; - if (landscape() && totalBadges > 35) nCols++; - - int nRows = (int) Math.ceil(totalBadges/(float)nCols); - - float badgeWidth = (w - 2*left)/nCols; - float badgeHeight = (h - top - left)/nRows; - - for (int i = 0; i < totalBadges; i++){ - int row = i / nCols; - int col = i % nCols; - Badges.Badge b = i < badges.size() ? badges.get( i ) : lockedBadges.get( i - badges.size() ); - BadgeButton button = new BadgeButton( b, i < badges.size() ); - button.setPos( - left + col * badgeWidth + (badgeWidth - button.width()) / 2, - top + row * badgeHeight + (badgeHeight - button.height()) / 2); - align(button); - add( button ); - } + BadgesGrid grid = new BadgesGrid(true); + grid.setRect(margin, top, w-(2*margin), h-top-margin); + add(grid); ExitButton btnExit = new ExitButton(); btnExit.setPos( Camera.main.width - btnExit.width(), 0 ); @@ -129,49 +88,4 @@ public class BadgesScene extends PixelScene { ShatteredPixelDungeon.switchNoFade( TitleScene.class ); } - private static class BadgeButton extends Button { - - private Badges.Badge badge; - private boolean unlocked; - - private Image icon; - - public BadgeButton( Badges.Badge badge, boolean unlocked ) { - super(); - - this.badge = badge; - this.unlocked = unlocked; - - icon = BadgeBanner.image(badge.image); - if (!unlocked) { - icon.brightness(0.4f); - } - add(icon); - - setSize( icon.width(), icon.height() ); - } - - @Override - protected void layout() { - super.layout(); - - icon.x = x + (width - icon.width()) / 2; - icon.y = y + (height - icon.height()) / 2; - } - - @Override - public void update() { - super.update(); - - if (unlocked && Random.Float() < Game.elapsed * 0.1) { - BadgeBanner.highlight( icon, badge.image ); - } - } - - @Override - protected void onClick() { - Sample.INSTANCE.play( Assets.Sounds.CLICK, 0.7f, 0.7f, 1.2f ); - Game.scene().add( new WndBadge( badge, unlocked ) ); - } - } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BadgesGrid.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BadgesGrid.java new file mode 100644 index 000000000..6932b2791 --- /dev/null +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/BadgesGrid.java @@ -0,0 +1,157 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2020 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.shatteredpixel.shatteredpixeldungeon.ui; + +import com.shatteredpixel.shatteredpixeldungeon.Assets; +import com.shatteredpixel.shatteredpixeldungeon.Badges; +import com.shatteredpixel.shatteredpixeldungeon.effects.BadgeBanner; +import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; +import com.shatteredpixel.shatteredpixeldungeon.windows.WndBadge; +import com.watabou.noosa.Game; +import com.watabou.noosa.Image; +import com.watabou.noosa.audio.Sample; +import com.watabou.noosa.ui.Button; +import com.watabou.noosa.ui.Component; +import com.watabou.utils.Random; + +import java.util.ArrayList; + +public class BadgesGrid extends Component { + + ArrayList badgeButtons; + + public BadgesGrid( boolean global ){ + super(); + badgeButtons = new ArrayList<>(); + + for (Badges.Badge badge : Badges.filterReplacedBadges( global )) { + + if (badge.image == -1) { + continue; + } + + BadgeButton button = new BadgeButton( badge, true ); + add( button ); + badgeButtons.add(button); + } + + if (global) { + + ArrayList lockedBadges = new ArrayList<>(); + for (Badges.Badge badge : Badges.Badge.values()) { + if (badge.image != -1 && !Badges.isUnlocked(badge)) { + lockedBadges.add(badge); + } + } + Badges.filterHigherIncrementalBadges(lockedBadges); + + for (Badges.Badge badge : lockedBadges) { + BadgeButton button = new BadgeButton( badge, false ); + add(button); + badgeButtons.add(button); + } + + } + + } + + @Override + protected void layout() { + super.layout(); + + //2-5 columns in portrait, 5-8 in landscape + int nCols; + if (width() > height()){ + if (badgeButtons.size() > 35) nCols = 8; + else if (badgeButtons.size() > 24) nCols = 7; + else if (badgeButtons.size() > 15) nCols = 6; + else nCols = 5; + } else { + if (badgeButtons.size() > 32) nCols = 5; + else if (badgeButtons.size() > 21) nCols = 4; + else if (badgeButtons.size() > 10) nCols = 3; + else nCols = 2; + } + + int nRows = (int) Math.ceil(badgeButtons.size()/(float)nCols); + + float badgeWidth = width()/nCols; + float badgeHeight = height()/nRows; + + for (int i = 0; i < badgeButtons.size(); i++){ + int row = i / nCols; + int col = i % nCols; + BadgeButton button = badgeButtons.get(i); + button.setPos( + left() + col * badgeWidth + (badgeWidth - button.width()) / 2, + top() + row * badgeHeight + (badgeHeight - button.height()) / 2); + PixelScene.align(button); + } + } + + private static class BadgeButton extends Button { + + private Badges.Badge badge; + private boolean unlocked; + + private Image icon; + + public BadgeButton( Badges.Badge badge, boolean unlocked ) { + super(); + + this.badge = badge; + this.unlocked = unlocked; + + icon = BadgeBanner.image(badge.image); + if (!unlocked) { + icon.brightness(0.4f); + } + add(icon); + + setSize( icon.width(), icon.height() ); + } + + @Override + protected void layout() { + super.layout(); + + icon.x = x + (width - icon.width()) / 2; + icon.y = y + (height - icon.height()) / 2; + } + + @Override + public void update() { + super.update(); + + if (unlocked && Random.Float() < Game.elapsed * 0.1) { + BadgeBanner.highlight( icon, badge.image ); + } + } + + @Override + protected void onClick() { + Sample.INSTANCE.play( Assets.Sounds.CLICK, 0.7f, 0.7f, 1.2f ); + Game.scene().add( new WndBadge( badge, unlocked ) ); + } + } + +} \ No newline at end of file diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndRanking.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndRanking.java index e31282f24..17f192c4d 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndRanking.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndRanking.java @@ -31,12 +31,12 @@ import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; import com.shatteredpixel.shatteredpixeldungeon.sprites.HeroSprite; +import com.shatteredpixel.shatteredpixeldungeon.ui.BadgesGrid; import com.shatteredpixel.shatteredpixeldungeon.ui.BadgesList; import com.shatteredpixel.shatteredpixeldungeon.ui.Icons; import com.shatteredpixel.shatteredpixeldungeon.ui.ItemSlot; import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextBlock; -import com.shatteredpixel.shatteredpixeldungeon.ui.ScrollPane; import com.shatteredpixel.shatteredpixeldungeon.ui.TalentsPane; import com.shatteredpixel.shatteredpixeldungeon.ui.Window; import com.watabou.noosa.ColorBlock; @@ -45,6 +45,7 @@ import com.watabou.noosa.Group; import com.watabou.noosa.Image; import com.watabou.noosa.audio.Sample; import com.watabou.noosa.ui.Button; +import com.watabou.noosa.ui.Component; import java.util.Locale; @@ -302,11 +303,15 @@ public class WndRanking extends WndTabbed { super(); camera = WndRanking.this.camera; - - ScrollPane list = new BadgesList( false ); - add( list ); - - list.setSize( WIDTH, HEIGHT ); + + Component badges; + if (Badges.unlocked(false) <= 7){ + badges = new BadgesList(false); + } else { + badges = new BadgesGrid(false); + } + add(badges); + badges.setSize( WIDTH, HEIGHT ); } }