Improve multi-select list appearance

Optimize list item tap and render logic by passing index as parameter
This commit is contained in:
drdev
2014-06-03 22:18:32 +00:00
parent 0a51c45081
commit e92bdc665f
9 changed files with 53 additions and 30 deletions

View File

@@ -34,6 +34,7 @@ import forge.toolbox.FCheckBox;
import forge.toolbox.FEvent; import forge.toolbox.FEvent;
import forge.toolbox.FEvent.FEventHandler; import forge.toolbox.FEvent.FEventHandler;
import forge.toolbox.FList; import forge.toolbox.FList;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;
@@ -275,8 +276,7 @@ public final class ItemListView<T extends InventoryItem> extends ItemView<T> {
} }
@Override @Override
public boolean tap(Entry<T, Integer> value, float x, float y, int count) { public boolean tap(Integer index, Entry<T, Integer> value, float x, float y, int count) {
Integer index = list.getIndexOf(value);
if (allowMultipleSelections) { if (allowMultipleSelections) {
if (selectedIndices.contains(index)) { if (selectedIndices.contains(index)) {
selectedIndices.remove(index); selectedIndices.remove(index);
@@ -299,7 +299,18 @@ public final class ItemListView<T extends InventoryItem> extends ItemView<T> {
} }
@Override @Override
public void drawValue(Graphics g, Entry<T, Integer> value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, Entry<T, Integer> value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
if (allowMultipleSelections) {
if (pressed) { //if multi-select mode, draw SEL_COLOR when pressed
g.fillRect(SEL_COLOR, x - FList.PADDING, y - FList.PADDING, w + 2 * FList.PADDING, h + 2 * FList.PADDING);
}
//draw checkbox, with it checked based on whether item is selected
float checkBoxSize = h / 2;
float padding = checkBoxSize / 2;
w -= checkBoxSize + padding;
FCheckBox.drawCheckBox(g, selectedIndices.contains(index), x + w, y + padding, checkBoxSize, checkBoxSize);
w -= padding;
}
renderer.drawValue(g, value, font, foreColor, pressed, x + 1, y, w - 2, h); //x + 1 and w - 2 to account for left and right borders renderer.drawValue(g, value, font, foreColor, pressed, x + 1, y, w - 2, h); //x + 1 and w - 2 to account for left and right borders
} }
}); });
@@ -321,8 +332,8 @@ public final class ItemListView<T extends InventoryItem> extends ItemView<T> {
@Override @Override
protected FSkinColor getItemFillColor(int index) { protected FSkinColor getItemFillColor(int index) {
if (selectedIndices.contains(index)) { if (!allowMultipleSelections && selectedIndices.contains(index)) {
return SEL_COLOR; return SEL_COLOR; //don't show SEL_COLOR if in multi-select mode
} }
if (index % 2 == 1) { if (index % 2 == 1) {
return ALT_ROW_COLOR; return ALT_ROW_COLOR;

View File

@@ -1053,13 +1053,13 @@ public class ConstructedScreen extends LaunchScreen {
} }
@Override @Override
public boolean tap(Variant value, float x, float y, int count) { public boolean tap(Integer index, Variant value, float x, float y, int count) {
value.toggle(); value.toggle();
return true; return true;
} }
@Override @Override
public void drawValue(Graphics g, Variant value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, Variant value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) {
String text = value.gameType.toString(); String text = value.gameType.toString();
float totalHeight = h; float totalHeight = h;
h = font.getMultiLineBounds(text).height + 5; h = font.getMultiLineBounds(text).height + 5;

View File

@@ -81,13 +81,13 @@ public class FilesPage extends TabPage {
} }
@Override @Override
public boolean tap(FilesItem value, float x, float y, int count) { public boolean tap(Integer index, FilesItem value, float x, float y, int count) {
value.select(); value.select();
return true; return true;
} }
@Override @Override
public void drawValue(Graphics g, FilesItem value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, FilesItem value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) {
float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items
x += offset; x += offset;
y += offset; y += offset;

View File

@@ -268,7 +268,7 @@ public class SettingsPage extends TabPage {
lstOptions = add(new FList<String>(options)); lstOptions = add(new FList<String>(options));
lstOptions.setListItemRenderer(new FList.DefaultListItemRenderer<String>() { lstOptions.setListItemRenderer(new FList.DefaultListItemRenderer<String>() {
@Override @Override
public boolean tap(String value, float x, float y, int count) { public boolean tap(Integer index, String value, float x, float y, int count) {
if (!value.equals(currentValue)) { if (!value.equals(currentValue)) {
valueChanged(value); valueChanged(value);
} }
@@ -277,7 +277,7 @@ public class SettingsPage extends TabPage {
} }
@Override @Override
public void drawValue(Graphics g, String value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, String value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items
x += offset; x += offset;
y += offset; y += offset;
@@ -316,13 +316,13 @@ public class SettingsPage extends TabPage {
} }
@Override @Override
public boolean tap(Setting value, float x, float y, int count) { public boolean tap(Integer index, Setting value, float x, float y, int count) {
value.select(); value.select();
return true; return true;
} }
@Override @Override
public void drawValue(Graphics g, Setting value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, Setting value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) {
float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items float offset = w * SettingsScreen.INSETS_FACTOR - FList.PADDING; //increase padding for settings items
x += offset; x += offset;
y += offset; y += offset;

View File

@@ -365,8 +365,7 @@ public class DualListBox<T> extends FDialog {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
Integer index = ChoiceList.this.getIndexOf(value);
selectedIndices.clear(); selectedIndices.clear();
selectedIndices.add(index); selectedIndices.add(index);
if (renderer.tap(value, x, y, count)) { if (renderer.tap(value, x, y, count)) {
@@ -379,7 +378,7 @@ public class DualListBox<T> extends FDialog {
} }
@Override @Override
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
renderer.drawValue(g, value, font, foreColor, pressed, x, y, w, h); renderer.drawValue(g, value, font, foreColor, pressed, x, y, w, h);
} }
}); });

View File

@@ -36,7 +36,7 @@ public class FCheckBox extends FLabel {
@Override @Override
public void draw(Graphics g, float x, float y, float w, float h) { public void draw(Graphics g, float x, float y, float w, float h) {
drawCheckBox(g, BOX_COLOR, CHECK_COLOR, isSelected(), x, y, w, h); drawCheckBox(g, isSelected(), x, y, w, h);
} }
} }
@@ -45,6 +45,9 @@ public class FCheckBox extends FLabel {
drawContent(g, getWidth(), getHeight(), false); drawContent(g, getWidth(), getHeight(), false);
} }
public static void drawCheckBox(Graphics g, boolean isChecked, float x, float y, float w, float h) {
drawCheckBox(g, BOX_COLOR, CHECK_COLOR, isChecked, x, y, w, h);
}
public static void drawCheckBox(Graphics g, FSkinColor boxColor, FSkinColor checkColor, boolean isChecked, float x, float y, float w, float h) { public static void drawCheckBox(Graphics g, FSkinColor boxColor, FSkinColor checkColor, boolean isChecked, float x, float y, float w, float h) {
g.drawRect(Utils.scaleMin(1), boxColor, x, y, w, h); g.drawRect(Utils.scaleMin(1), boxColor, x, y, w, h);
if (isChecked) { if (isChecked) {

View File

@@ -243,7 +243,7 @@ public class FGroupList<E> extends FScrollPane {
} }
public boolean tap(float x, float y, int count) { public boolean tap(float x, float y, int count) {
return renderer.tap(value, x, y, count); return renderer.tap(-1, value, x, y, count);
} }
@Override @Override
@@ -256,7 +256,7 @@ public class FGroupList<E> extends FScrollPane {
g.fillRect(fillColor, 0, 0, w, h); g.fillRect(fillColor, 0, 0, w, h);
} }
renderer.drawValue(g, value, font, FList.FORE_COLOR, pressed, FList.PADDING, FList.PADDING, w - 2 * FList.PADDING, h - 2 * FList.PADDING); renderer.drawValue(g, -1, value, font, FList.FORE_COLOR, pressed, FList.PADDING, FList.PADDING, w - 2 * FList.PADDING, h - 2 * FList.PADDING);
if (drawLineSeparators()) { if (drawLineSeparators()) {
g.drawLine(1, FList.LINE_COLOR, 0, h, w, h); g.drawLine(1, FList.LINE_COLOR, 0, h, w, h);

View File

@@ -135,7 +135,7 @@ public class FList<E> extends FScrollPane implements Iterable<E> {
E item = getItemAt(index); E item = getItemAt(index);
if (item == null) { return false; } if (item == null) { return false; }
return renderer.tap(item, x, y - getItemTop(index), count); return renderer.tap(index, item, x, y - getItemTop(index), count);
} }
private float getItemTop(int index) { private float getItemTop(int index) {
@@ -187,7 +187,7 @@ public class FList<E> extends FScrollPane implements Iterable<E> {
g.fillRect(fillColor, 0, y, w, itemHeight); g.fillRect(fillColor, 0, y, w, itemHeight);
} }
renderer.drawValue(g, items.get(i), font, FORE_COLOR, pressedIndex == i, PADDING, y + PADDING, valueWidth, valueHeight); renderer.drawValue(g, i, items.get(i), font, FORE_COLOR, pressedIndex == i, PADDING, y + PADDING, valueWidth, valueHeight);
y += itemHeight; y += itemHeight;
@@ -214,8 +214,8 @@ public class FList<E> extends FScrollPane implements Iterable<E> {
public static abstract class ListItemRenderer<V> { public static abstract class ListItemRenderer<V> {
public abstract float getItemHeight(); public abstract float getItemHeight();
public abstract boolean tap(V value, float x, float y, int count); public abstract boolean tap(Integer index, V value, float x, float y, int count);
public abstract void drawValue(Graphics g, V value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h); public abstract void drawValue(Graphics g, Integer index, V value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h);
} }
public static class DefaultListItemRenderer<V> extends ListItemRenderer<V> { public static class DefaultListItemRenderer<V> extends ListItemRenderer<V> {
@@ -225,12 +225,12 @@ public class FList<E> extends FScrollPane implements Iterable<E> {
} }
@Override @Override
public boolean tap(V value, float x, float y, int count) { public boolean tap(Integer index, V value, float x, float y, int count) {
return false; return false;
} }
@Override @Override
public void drawValue(Graphics g, V value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, V value, FSkinFont font, FSkinColor color, boolean pressed, float x, float y, float w, float h) {
g.drawText(value.toString(), font, color, x, y, w, h, false, HAlignment.LEFT, true); g.drawText(value.toString(), font, color, x, y, w, h, false, HAlignment.LEFT, true);
} }
} }

View File

@@ -378,8 +378,7 @@ public class ListChooser<T> extends FContainer {
} }
@Override @Override
public boolean tap(T value, float x, float y, int count) { public boolean tap(Integer index, T value, float x, float y, int count) {
Integer index = ChoiceList.this.getIndexOf(value);
if (allowMultipleSelections) { if (allowMultipleSelections) {
if (selectedIndices.contains(index)) { if (selectedIndices.contains(index)) {
selectedIndices.remove(index); selectedIndices.remove(index);
@@ -403,7 +402,18 @@ public class ListChooser<T> extends FContainer {
} }
@Override @Override
public void drawValue(Graphics g, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) { public void drawValue(Graphics g, Integer index, T value, FSkinFont font, FSkinColor foreColor, boolean pressed, float x, float y, float w, float h) {
if (allowMultipleSelections) {
if (pressed) { //if multi-select mode, draw SEL_COLOR when pressed
g.fillRect(SEL_COLOR, x - FList.PADDING, y - FList.PADDING, w + 2 * FList.PADDING, h + 2 * FList.PADDING);
}
//draw checkbox, with it checked based on whether item is selected
float checkBoxSize = h / 2;
float padding = checkBoxSize / 2;
w -= checkBoxSize + padding;
FCheckBox.drawCheckBox(g, selectedIndices.contains(index), x + w, y + padding, checkBoxSize, checkBoxSize);
w -= padding;
}
renderer.drawValue(g, value, font, foreColor, pressed, x, y, w, h); renderer.drawValue(g, value, font, foreColor, pressed, x, y, w, h);
} }
}); });
@@ -422,8 +432,8 @@ public class ListChooser<T> extends FContainer {
@Override @Override
protected FSkinColor getItemFillColor(int index) { protected FSkinColor getItemFillColor(int index) {
if (selectedIndices.contains(index)) { if (!allowMultipleSelections && selectedIndices.contains(index)) {
return SEL_COLOR; return SEL_COLOR; //don't show SEL_COLOR if in multi-select mode
} }
if (index % 2 == 1) { if (index % 2 == 1) {
return ALT_ITEM_COLOR; return ALT_ITEM_COLOR;