From d9dfa084f6c8f7b4be8a1d97468739f59c1a4648 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 28 Jan 2016 01:02:38 -0500 Subject: [PATCH] v0.3.4: okay, I think I've finally got item action buttons in a good place now. --- .../windows/WndItem.java | 76 +++++++++++++++++-- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/windows/WndItem.java b/src/com/shatteredpixel/shatteredpixeldungeon/windows/WndItem.java index 10eaad036..81a26516e 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/windows/WndItem.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/windows/WndItem.java @@ -30,9 +30,12 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.RedButton; import com.shatteredpixel.shatteredpixeldungeon.ui.Window; import com.watabou.noosa.RenderedTextMultiline; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + public class WndItem extends Window { - private static final float BUTTON_WIDTH = 36; private static final float BUTTON_HEIGHT = 16; private static final float GAP = 2; @@ -65,6 +68,7 @@ public class WndItem extends Window { float x = 0; if (Dungeon.hero.isAlive() && owner != null) { + ArrayList line = new ArrayList<>(); for (final String action:item.actions( Dungeon.hero )) { RedButton btn = new RedButton( Messages.get(item, "ac_" + action), 8 ) { @@ -75,22 +79,84 @@ public class WndItem extends Window { owner.hide(); }; }; - btn.setSize( Math.max( BUTTON_WIDTH, btn.reqWidth() ), BUTTON_HEIGHT ); - if (x + btn.width() > width) { + btn.setSize( btn.reqWidth(), BUTTON_HEIGHT ); + if (x + btn.width() > width || line.size() == 3) { + layoutButtons(line, width - x, y); x = 0; y += BUTTON_HEIGHT + 1; + line = new ArrayList<>(); } - btn.setPos( x, y ); + x++; add( btn ); + line.add( btn ); if (action.equals(item.defaultAction)) { btn.textColor( TITLE_COLOR ); } - x += btn.width() + 1; + x += btn.width(); } + layoutButtons(line, width - x, y); } resize( width, (int)(y + (x > 0 ? BUTTON_HEIGHT : 0)) ); } + + //this method assumes a max of 3 buttons per line + //FIXME: this is really messy for just trying to make buttons fill the window. Gotta be a cleaner way. + private static void layoutButtons(ArrayList line, float extraWidth, float y){ + if (line == null || line.size() == 0 || extraWidth == 0) return; + if (line.size() == 1){ + line.get(0).setSize(line.get(0).width()+extraWidth, BUTTON_HEIGHT); + return; + } + ArrayList lineByWidths = new ArrayList<>(line); + Collections.sort(lineByWidths, widthComparator); + RedButton smallest, middle, largest; + smallest = lineByWidths.get(0); + middle = lineByWidths.get(1); + largest = null; + if (lineByWidths.size() == 3) { + largest = lineByWidths.get(2); + } + + float btnDiff = middle.width() - smallest.width(); + smallest.setSize(smallest.width() + Math.min(btnDiff, extraWidth), BUTTON_HEIGHT); + extraWidth -= btnDiff; + if (extraWidth > 0) { + if (largest == null) { + smallest.setSize(smallest.width() + extraWidth / 2, BUTTON_HEIGHT); + middle.setSize(middle.width() + extraWidth / 2, BUTTON_HEIGHT); + } else { + btnDiff = largest.width() - smallest.width(); + smallest.setSize(smallest.width() + Math.min(btnDiff, extraWidth/2), BUTTON_HEIGHT); + middle.setSize(middle.width() + Math.min(btnDiff, extraWidth/2), BUTTON_HEIGHT); + extraWidth -= btnDiff*2; + if (extraWidth > 0){ + smallest.setSize(smallest.width() + extraWidth / 3, BUTTON_HEIGHT); + middle.setSize(middle.width() + extraWidth / 3, BUTTON_HEIGHT); + largest.setSize(largest.width() + extraWidth / 3, BUTTON_HEIGHT); + } + } + } + + float x = 0; + for (RedButton btn : line){ + btn.setPos( x , y ); + x += btn.width()+1; + } + } + + private static Comparator widthComparator = new Comparator() { + @Override + public int compare(RedButton lhs, RedButton rhs) { + if (lhs.width() < rhs.width()){ + return -1; + } else if (lhs.width() == rhs.width()){ + return 0; + } else { + return 1; + } + } + }; }