v0.3.4: okay, I think I've finally got item action buttons in a good place now.

This commit is contained in:
Evan Debenham 2016-01-28 01:02:38 -05:00 committed by Evan Debenham
parent be490fa739
commit d9dfa084f6

View File

@ -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<RedButton> 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<RedButton> 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<RedButton> 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<RedButton> widthComparator = new Comparator<RedButton>() {
@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;
}
}
};
}