Support compact tabs via setting

This commit is contained in:
drdev
2014-10-03 19:06:32 +00:00
parent fe33235f08
commit ce043302db
4 changed files with 93 additions and 26 deletions

View File

@@ -8,6 +8,8 @@ import forge.assets.FImage;
import forge.assets.FSkinColor; import forge.assets.FSkinColor;
import forge.assets.FSkinFont; import forge.assets.FSkinFont;
import forge.assets.FSkinColor.Colors; import forge.assets.FSkinColor.Colors;
import forge.model.FModel;
import forge.properties.ForgePreferences.FPref;
import forge.toolbox.FContainer; import forge.toolbox.FContainer;
import forge.toolbox.FDisplayObject; import forge.toolbox.FDisplayObject;
import forge.toolbox.FEvent; import forge.toolbox.FEvent;
@@ -17,6 +19,8 @@ import forge.toolbox.FScrollPane;
import forge.util.Utils; import forge.util.Utils;
public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen { public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
public static boolean COMPACT_TABS = FModel.getPreferences().getPrefBoolean(FPref.UI_COMPACT_TABS);
protected final TabPage<T>[] tabPages; protected final TabPage<T>[] tabPages;
private TabPage<T> selectedPage; private TabPage<T> selectedPage;
protected final TabHeader<T> tabHeader; protected final TabHeader<T> tabHeader;
@@ -88,7 +92,8 @@ public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
} }
private static class TabHeader<T extends TabPageScreen<T>> extends Header { private static class TabHeader<T extends TabPageScreen<T>> extends Header {
public static final float HEIGHT = Math.round(Utils.AVG_FINGER_HEIGHT * 1.4f); private static final float HEIGHT = Math.round(Utils.AVG_FINGER_HEIGHT * 1.4f);
private static final float COMPACT_HEIGHT = Math.round(Utils.AVG_FINGER_HEIGHT * 0.8f);
private static final float BACK_BUTTON_WIDTH = Math.round(HEIGHT / 2); private static final float BACK_BUTTON_WIDTH = Math.round(HEIGHT / 2);
private static final FSkinColor SEPARATOR_COLOR = BACK_COLOR.stepColor(-40); private static final FSkinColor SEPARATOR_COLOR = BACK_COLOR.stepColor(-40);
@@ -127,13 +132,12 @@ public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
public TabHeader(TabPage<T>[] tabPages, boolean showBackButton) { public TabHeader(TabPage<T>[] tabPages, boolean showBackButton) {
if (showBackButton) { if (showBackButton) {
btnBack = add(new FLabel.Builder().iconScaleAuto(false).icon(new BackIcon(BACK_BUTTON_WIDTH, BACK_BUTTON_WIDTH)).pressedColor(BTN_PRESSED_COLOR).align(HAlignment.CENTER).command(new FEventHandler() { btnBack = add(new FLabel.Builder().icon(new BackIcon(BACK_BUTTON_WIDTH, BACK_BUTTON_WIDTH)).pressedColor(BTN_PRESSED_COLOR).align(HAlignment.CENTER).command(new FEventHandler() {
@Override @Override
public void handleEvent(FEvent e) { public void handleEvent(FEvent e) {
Forge.back(); Forge.back();
} }
}).build()); }).build());
btnBack.setSize(BACK_BUTTON_WIDTH, HEIGHT);
} }
else { else {
btnBack = null; btnBack = null;
@@ -146,12 +150,12 @@ public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
@Override @Override
public float getPreferredHeight() { public float getPreferredHeight() {
return HEIGHT; return COMPACT_TABS ? COMPACT_HEIGHT : HEIGHT;
} }
@Override @Override
public void drawBackground(Graphics g) { public void drawBackground(Graphics g) {
g.fillRect(BACK_COLOR, 0, 0, getWidth(), HEIGHT); g.fillRect(BACK_COLOR, 0, 0, getWidth(), getHeight());
} }
@Override @Override
@@ -163,19 +167,23 @@ public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
} }
//draw bottom border for header //draw bottom border for header
float y = HEIGHT - LINE_THICKNESS / 2; float y = getHeight() - LINE_THICKNESS / 2;
g.drawLine(LINE_THICKNESS, LINE_COLOR, 0, y, getWidth(), y); g.drawLine(LINE_THICKNESS, LINE_COLOR, 0, y, getWidth(), y);
} }
@Override @Override
protected void doLayout(float width, float height) { protected void doLayout(float width, float height) {
float x = btnBack != null ? btnBack.getWidth() : 0; float x = 0;
if (btnBack != null) {
btnBack.setIconScaleAuto(COMPACT_TABS);
btnBack.setSize(BACK_BUTTON_WIDTH, height);
x += BACK_BUTTON_WIDTH;
}
scroller.setBounds(x, 0, width - x, height); scroller.setBounds(x, 0, width - x, height);
} }
} }
public static abstract class TabPage<T extends TabPageScreen<T>> extends FContainer { public static abstract class TabPage<T extends TabPageScreen<T>> extends FContainer {
private static final float TAB_PADDING = TabHeader.HEIGHT * 0.1f;
private static final FSkinColor SEL_TAB_COLOR = FSkinColor.get(Colors.CLR_ACTIVE); private static final FSkinColor SEL_TAB_COLOR = FSkinColor.get(Colors.CLR_ACTIVE);
private static final FSkinColor TAB_FORE_COLOR = FSkinColor.get(Colors.CLR_TEXT); private static final FSkinColor TAB_FORE_COLOR = FSkinColor.get(Colors.CLR_TEXT);
private static final FSkinFont TAB_FONT = FSkinFont.get(12); private static final FSkinFont TAB_FONT = FSkinFont.get(12);
@@ -245,30 +253,67 @@ public class TabPageScreen<T extends TabPageScreen<T>> extends FScreen {
public void draw(Graphics g) { public void draw(Graphics g) {
float w = getWidth(); float w = getWidth();
float h = getHeight(); float h = getHeight();
float padding = h * 0.1f;
if (parentScreen.getSelectedPage() == TabPage.this) { if (parentScreen.getSelectedPage() == TabPage.this) {
g.fillRect(SEL_TAB_COLOR, Header.LINE_THICKNESS / 2, 0, w - Header.LINE_THICKNESS, h); g.fillRect(SEL_TAB_COLOR, Header.LINE_THICKNESS / 2, 0, w - Header.LINE_THICKNESS, h);
} }
w -= 2 * padding;
//draw caption //draw caption and icon
float y = h - TAB_PADDING - TAB_FONT.getCapHeight(); if (COMPACT_TABS) {
g.drawText(caption, TAB_FONT, TAB_FORE_COLOR, TAB_PADDING, y - TAB_PADDING, w - 2 * TAB_PADDING, h - y + TAB_PADDING, false, HAlignment.CENTER, true); h -= 2 * padding;
if (icon == null) {
//draw icon if one g.drawText(caption, TAB_FONT, TAB_FORE_COLOR, padding, padding, w, h, false, HAlignment.CENTER, true);
if (icon != null) { }
float iconHeight = y - 2 * TAB_PADDING; else {
float iconWidth = iconHeight * icon.getWidth() / icon.getHeight(); //center combination of icon and text
float maxWidth = w - 2 * TAB_PADDING; float iconWidth = h * icon.getWidth() / icon.getHeight();
if (iconWidth > maxWidth) { float iconOffset = iconWidth + padding;
iconHeight *= maxWidth / iconWidth;
iconWidth = maxWidth; float x = padding;
float y = padding;
float dx;
FSkinFont font = TAB_FONT;
while (true) {
dx = (w - iconOffset - font.getMultiLineBounds(caption).width) / 2;
if (dx > 0) {
x += dx;
break;
}
if (!font.canShrink()) {
break;
}
font = font.shrink();
}
g.drawImage(icon, x, y, iconWidth, h);
x += iconOffset;
w -= iconOffset;
g.startClip(x, y, w, h);
g.drawText(caption, font, TAB_FORE_COLOR, x, y, w, h, false, HAlignment.LEFT, true);
g.endClip();
}
}
else {
float y = h - padding - TAB_FONT.getCapHeight();
g.drawText(caption, TAB_FONT, TAB_FORE_COLOR, padding, y - padding, w, h - y + padding, false, HAlignment.CENTER, true);
if (icon != null) {
float iconHeight = y - 2 * padding;
float iconWidth = iconHeight * icon.getWidth() / icon.getHeight();
if (iconWidth > w) {
iconHeight *= w / iconWidth;
iconWidth = w;
}
g.drawImage(icon, (w - iconWidth) / 2, (y - iconHeight) / 2, iconWidth, iconHeight);
} }
g.drawImage(icon, (w - iconWidth) / 2, (y - iconHeight) / 2, iconWidth, iconHeight);
} }
//draw right border if needed //draw right border if needed
if (parentScreen.tabHeader.finalVisibleTab != this) { if (parentScreen.tabHeader.finalVisibleTab != this) {
float x = getWidth() - Header.LINE_THICKNESS / 2; float x = getWidth() - Header.LINE_THICKNESS / 2;
g.drawLine(Header.LINE_THICKNESS, TabHeader.SEPARATOR_COLOR, x, 0, x, h); g.drawLine(Header.LINE_THICKNESS, TabHeader.SEPARATOR_COLOR, x, 0, x, getHeight());
} }
} }
} }

View File

@@ -17,6 +17,7 @@ import forge.model.FModel;
import forge.properties.ForgePreferences; import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.FPref; import forge.properties.ForgePreferences.FPref;
import forge.screens.FScreen; import forge.screens.FScreen;
import forge.screens.TabPageScreen;
import forge.screens.TabPageScreen.TabPage; import forge.screens.TabPageScreen.TabPage;
import forge.sound.SoundSystem; import forge.sound.SoundSystem;
import forge.toolbox.FCheckBox; import forge.toolbox.FCheckBox;
@@ -142,6 +143,17 @@ public class SettingsPage extends TabPage<SettingsScreen> {
"Randomize Card Art", "Randomize Card Art",
"Generates cards with random art in generated limited mode card pools."), "Generates cards with random art in generated limited mode card pools."),
4); 4);
lstSettings.addItem(new BooleanSetting(FPref.UI_COMPACT_TABS,
"Compact Tabs",
"Show smaller tabs on the top of tab page screens (such as this screen)") {
@Override
public void select() {
super.select();
//update layout of screen when this setting changes
TabPageScreen.COMPACT_TABS = FModel.getPreferences().getPrefBoolean(FPref.UI_COMPACT_TABS);
parentScreen.revalidate();
}
}, 4);
lstSettings.addItem(new BooleanSetting(FPref.UI_COMPACT_LIST_ITEMS, lstSettings.addItem(new BooleanSetting(FPref.UI_COMPACT_LIST_ITEMS,
"Compact List Items", "Compact List Items",
"Show only a single line of text for cards and decks on all list views by default."), "Show only a single line of text for cards and decks on all list views by default."),

View File

@@ -159,6 +159,13 @@ public class FLabel extends FDisplayObject implements IButton {
icon = icon0; icon = icon0;
} }
public boolean getIconScaleAuto() {
return iconScaleAuto;
}
public void setIconScaleAuto(boolean b0) {
iconScaleAuto = b0;
}
public Vector2 getInsets() { public Vector2 getInsets() {
return insets; return insets;
} }
@@ -306,6 +313,9 @@ public class FLabel extends FDisplayObject implements IButton {
iconHeight = h * iconScaleFactor; iconHeight = h * iconScaleFactor;
iconWidth = iconHeight * aspectRatio; iconWidth = iconHeight * aspectRatio;
} }
float iconOffset = iconWidth + insets.x + getExtraGapBetweenIconAndText();
if (iconInBackground || text.isEmpty()) { if (iconInBackground || text.isEmpty()) {
if (alignment == HAlignment.CENTER) { if (alignment == HAlignment.CENTER) {
x += (w - iconWidth) / 2; x += (w - iconWidth) / 2;
@@ -316,7 +326,7 @@ public class FLabel extends FDisplayObject implements IButton {
if (alignment == HAlignment.CENTER) { if (alignment == HAlignment.CENTER) {
float dx; float dx;
while (true) { while (true) {
dx = (w - iconWidth - font.getMultiLineBounds(text).width - insets.x) / 2; dx = (w - iconOffset - font.getMultiLineBounds(text).width) / 2;
if (dx > 0) { if (dx > 0) {
x += dx; x += dx;
break; break;
@@ -351,9 +361,8 @@ public class FLabel extends FDisplayObject implements IButton {
if (pressed) { if (pressed) {
y++; y++;
} }
float dx = iconWidth + insets.x + getExtraGapBetweenIconAndText(); x += iconOffset;
x += dx; w -= iconOffset;
w -= dx;
g.startClip(x, y, w, h); g.startClip(x, y, w, h);
g.drawText(text, font, textColor, x, y, w, h, false, HAlignment.LEFT, true); g.drawText(text, font, textColor, x, y, w, h, false, HAlignment.LEFT, true);
g.endClip(); g.endClip();

View File

@@ -53,6 +53,7 @@ public class ForgePreferences extends PreferencesStore<ForgePreferences.FPref> {
UI_LARGE_CARD_VIEWERS ("false"), UI_LARGE_CARD_VIEWERS ("false"),
UI_RANDOM_ART_IN_POOLS ("true"), UI_RANDOM_ART_IN_POOLS ("true"),
UI_COMPACT_PROMPT ("false"), UI_COMPACT_PROMPT ("false"),
UI_COMPACT_TABS ("false"),
UI_COMPACT_LIST_ITEMS ("false"), UI_COMPACT_LIST_ITEMS ("false"),
UI_CARD_SIZE ("small"), UI_CARD_SIZE ("small"),
UI_BUGZ_NAME (""), UI_BUGZ_NAME (""),