From 97c74ec2b1fbe13478484b462ef2b144ceab365c Mon Sep 17 00:00:00 2001 From: drdev Date: Wed, 22 Jan 2014 03:14:23 +0000 Subject: [PATCH] Make scroll arrow buttons semi-transparent and more like FLabels --- .../main/java/forge/gui/toolbox/FLabel.java | 85 ++++++---- .../java/forge/gui/toolbox/FScrollPanel.java | 160 +++++------------- 2 files changed, 93 insertions(+), 152 deletions(-) diff --git a/forge-gui/src/main/java/forge/gui/toolbox/FLabel.java b/forge-gui/src/main/java/forge/gui/toolbox/FLabel.java index 87eee14683d..939cc0b81d4 100644 --- a/forge-gui/src/main/java/forge/gui/toolbox/FLabel.java +++ b/forge-gui/src/main/java/forge/gui/toolbox/FLabel.java @@ -115,12 +115,12 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { * @return {@link forge.gui.toolbox.Builder} */ public Builder selectable(final boolean b0) { this.bldSelectable = b0; return this; } public Builder selectable() { selectable(true); return this; } - + /**@param b0   boolean * @return {@link forge.gui.toolbox.Builder} */ public Builder selected(final boolean b0) { this.bldSelected = b0; return this; } public Builder selected() { selected(true); return this; } - + /**@param b0   boolean that controls when the label responds to mouse events * @return {@link forge.gui.toolbox.Builder} */ public Builder reactOnMouseDown(final boolean b0) { this.bldReactOnMouseDown = b0; return this; } @@ -129,7 +129,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { /**@param b0   boolean that controls whether the text uses skin colors * @return {@link forge.gui.toolbox.Builder} */ public Builder useSkinColors(final boolean b0) { bldUseSkinColors = b0; return this; } - + /**@param c0   {@link forge.Command} to execute if clicked * @return {@link forge.gui.toolbox.Builder} */ public Builder cmdClick(final Command c0) { this.bldCmd = c0; return this; } @@ -173,7 +173,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { * @return {@link forge.gui.toolbox.Builder} */ public Builder iconInsets(final Point i0) { this.bldIconInsets = i0; return this; } } - + // sets better defaults for button labels public static class ButtonBuilder extends Builder { public ButtonBuilder() { @@ -181,7 +181,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { bldOpaque = true; } } - + //========== Constructors // Call this using FLabel.Builder()... protected FLabel(final Builder b0) { @@ -207,27 +207,27 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { this.setFontAlign(b0.bldFontAlign); this.setToolTipText(b0.bldToolTip); this.setHoverable(b0.bldHoverable); - + // Call this last; to allow the properties which affect icons to already be in place. this.setIcon(b0.bldIcon); // If the label has button-like properties, interpret keypresses like a button if (b0.bldSelectable || b0.bldHoverable) { this.setFocusable(true); - + this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(final KeyEvent e) { if (e.getKeyChar() == ' ' || e.getKeyCode() == 10 || e.getKeyCode() == KeyEvent.VK_ENTER) { _doMouseAction(); } } }); - + this.addFocusListener(new FocusListener() { @Override public void focusLost(FocusEvent arg0) { repaintSelf(); } @Override public void focusGained(FocusEvent arg0) { repaintSelf(); } }); } - + if (b0.bldUseSkinColors) { // Non-custom display properties this.setForeground(clrText); @@ -254,7 +254,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { private final SkinColor l10 = clrMain.stepColor(10); private final SkinColor l20 = clrMain.stepColor(20); private final SkinColor l30 = clrMain.stepColor(30); - + // Custom properties, assigned either at realization (using builder) // or dynamically (using methods below). private double iconScaleFactor; @@ -307,7 +307,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { cmdClick.run(); } } - + private void _doRightClickAction() { if (cmdRightClick != null && isEnabled()) { cmdRightClick.run(); @@ -318,38 +318,34 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { private final FMouseAdapter madEvents = new FMouseAdapter() { @Override public void onMouseEnter(MouseEvent e) { - hovered = true; - repaintSelf(); + setHovered(true); } @Override public void onMouseExit(MouseEvent e) { - hovered = false; - repaintSelf(); + setHovered(false); } - + @Override public void onLeftMouseDown(MouseEvent e) { if (reactOnMouseDown) { _doMouseAction(); //for best responsiveness, do action before repainting for pressed state } - pressed = true; - repaintSelf(); + setPressed(true); } - + @Override public void onLeftMouseUp(MouseEvent e) { - pressed = false; - repaintSelf(); + setPressed(false); } - + @Override public void onLeftClick(MouseEvent e) { if (!reactOnMouseDown) { _doMouseAction(); } } - + @Override public void onRightClick(MouseEvent e) { _doRightClickAction(); @@ -376,6 +372,16 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { else { this.addMouseListener(madEvents); } } + protected void setHovered(boolean hovered0) { + this.hovered = hovered0; + repaintSelf(); + } + + protected void setPressed(boolean pressed0) { + this.pressed = pressed0; + repaintSelf(); + } + /** @param b0   boolean */ // Must be public. public void setSelected(final boolean b0) { @@ -386,7 +392,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { public boolean getSelected() { return this.selected; } - + /** Sets alpha if icon is in background. * @param f0   float */ // NOT public; must be set when label is built. @@ -540,13 +546,26 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { else if (selectable) { if (selected) { paintDown(g2d, w, h); - } else { + } + else { paintBorder(g2d, w, h); } } + paintContent(g2d, w, h, paintPressedState); + + if (hoverable) { + g2d.setComposite(oldComp); + } + + if (hasFocus() && isEnabled()) { + paintFocus(g2d, w, h); + } + } + + protected void paintContent(final Graphics2D g, int w, int h, final boolean paintPressedState) { if (paintPressedState) { //while pressed, translate graphics so icon and text appear shifted down and to the right - g2d.translate(1, 1); + g.translate(1, 1); } // Icon in background @@ -560,21 +579,13 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { int y = (int) (((h - sh) / 2) + iconInsets.getY()); - g2d.drawImage(img, x, y, sw + x, sh + y, 0, 0, iw, ih, null); + g.drawImage(img, x, y, sw + x, sh + y, 0, 0, iw, ih, null); } super.paintComponent(g); if (paintPressedState) { //reset translation after icon and text painted - g2d.translate(-1, -1); - } - - if (hoverable) { - g2d.setComposite(oldComp); - } - - if (hasFocus() && isEnabled()) { - paintFocus(g2d, w, h); + g.translate(-1, -1); } } @@ -584,7 +595,7 @@ public class FLabel extends SkinnedLabel implements ILocalRepaint { FSkin.setGraphicsColor(g, l30); g.drawRect(1, 1, w - 4, h - 4); } - + private void paintPressed(final Graphics2D g, int w, int h) { FSkin.setGraphicsGradientPaint(g, 0, h, d50, 0, 0, d10); g.fillRect(0, 0, w - 1, h - 1); diff --git a/forge-gui/src/main/java/forge/gui/toolbox/FScrollPanel.java b/forge-gui/src/main/java/forge/gui/toolbox/FScrollPanel.java index 00d1df7cfd3..308868cc494 100644 --- a/forge-gui/src/main/java/forge/gui/toolbox/FScrollPanel.java +++ b/forge-gui/src/main/java/forge/gui/toolbox/FScrollPanel.java @@ -17,10 +17,7 @@ */ package forge.gui.toolbox; -import java.awt.AlphaComposite; -import java.awt.Color; import java.awt.Component; -import java.awt.Composite; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; @@ -28,17 +25,13 @@ import java.awt.LayoutManager; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollBar; import javax.swing.ScrollPaneConstants; import javax.swing.Timer; import javax.swing.border.Border; -import forge.gui.framework.ILocalRepaint; import forge.gui.toolbox.FSkin.SkinColor; /** @@ -47,10 +40,11 @@ import forge.gui.toolbox.FSkin.SkinColor; */ @SuppressWarnings("serial") public class FScrollPanel extends FScrollPane { + private static final SkinColor arrowColor = FSkin.getColor(FSkin.Colors.CLR_TEXT); private final boolean useArrowButtons; private final ArrowButton[] arrowButtons = new ArrowButton[4]; private JPanel innerPanel; - + /** * An extension of JScrollPane that can be used as a panel and supports using arrow buttons to scroll instead of scrollbars * This constructor assumes no layout, assumes using scrollbars to scroll, and "as needed" for horizontal and vertical scroll policies. @@ -59,7 +53,7 @@ public class FScrollPanel extends FScrollPane { public FScrollPanel() { this(null); } - + /** * An extension of JScrollPane that can be used as a panel and supports using arrow buttons to scroll instead of scrollbars * This constructor assumes using scrollbars to scroll and "as needed" for horizontal and vertical scroll policies. @@ -69,7 +63,7 @@ public class FScrollPanel extends FScrollPane { public FScrollPanel(final LayoutManager layout) { this(layout, false); } - + /** * An extension of JScrollPane that can be used as a panel and supports using arrow buttons to scroll instead of scrollbars * This constructor assumes "as needed" for horizontal and vertical scroll policies. @@ -94,7 +88,7 @@ public class FScrollPanel extends FScrollPane { innerPanel = (JPanel)getViewport().getView(); innerPanel.setOpaque(false); } - + /** * An extension of JScrollPane that can be used as a panel and supports using arrow buttons to scroll instead of scrollbars * @@ -104,7 +98,7 @@ public class FScrollPanel extends FScrollPane { public FScrollPanel(final Component view, boolean useArrowButtons0) { this(view, useArrowButtons0, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); } - + /** * An extension of JScrollPane that can be used as a panel and supports using arrow buttons to scroll instead of scrollbars * @@ -124,7 +118,7 @@ public class FScrollPanel extends FScrollPane { getVerticalScrollBar().setPreferredSize(new Dimension(0, 0)); } } - + @Override public void paint(Graphics g) { super.paint(g); @@ -146,7 +140,7 @@ public class FScrollPanel extends FScrollPane { } } } - + private void updateArrowButton(int dir, boolean[] visible) { ArrowButton arrowButton = arrowButtons[dir]; if (!visible[dir]) { @@ -155,14 +149,14 @@ public class FScrollPanel extends FScrollPane { } return; } - + //determine bounds of button int x, y, w, h; final int panelWidth = getWidth(); final int panelHeight = getHeight(); final int arrowButtonSize = 18; final int cornerSize = arrowButtonSize - 1; //make borders line up - + if (dir < 2) { //if button for horizontal scrolling y = 0; h = panelHeight; @@ -189,7 +183,7 @@ public class FScrollPanel extends FScrollPane { y = (dir == 2 ? 0 : panelHeight - arrowButtonSize); h = arrowButtonSize; } - + if (arrowButton == null) { switch (dir) { case 0: @@ -211,66 +205,39 @@ public class FScrollPanel extends FScrollPane { arrowButton.setSize(w, h); FAbsolutePositioner.SINGLETON_INSTANCE.show(arrowButton, this, x, y); } - - private abstract class ArrowButton extends JLabel implements ILocalRepaint { - private final SkinColor clrFore = FSkin.getColor(FSkin.Colors.CLR_TEXT); - private final SkinColor clrBack = FSkin.getColor(FSkin.Colors.CLR_INACTIVE); - private final SkinColor d50 = clrBack.stepColor(-50); - private final SkinColor d10 = clrBack.stepColor(-10); - private final SkinColor l10 = clrBack.stepColor(10); - private final SkinColor l20 = clrBack.stepColor(20); - private final AlphaComposite alphaDefault = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f); - private final AlphaComposite alphaHovered = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f); + + private abstract class ArrowButton extends FLabel { protected final int arrowSize = 6; private final JScrollBar scrollBar; private final int incrementDirection; - private boolean hovered; protected ArrowButton(final JScrollBar scrollBar0, final int incrementDirection0) { - super(""); + super(new FLabel.ButtonBuilder()); scrollBar = scrollBar0; incrementDirection = incrementDirection0; timer.setInitialDelay(500); //wait half a second after mouse down before starting timer - addMouseListener(madEvents); } - + @Override - public void repaintSelf() { - final Dimension d = getSize(); - repaint(0, 0, d.width, d.height); + protected void setPressed(boolean pressed0) { + super.setPressed(pressed0); + if (pressed0) { + scrollBar.setValue(scrollBar.getValue() + scrollBar.getUnitIncrement() * incrementDirection); + timer.start(); + } + else { + timer.stop(); + } } - + @Override - public void paintComponent(final Graphics g) { - Graphics2D g2d = (Graphics2D)g; - - int w = getWidth(); - int h = getHeight(); - - g.setColor(Color.white); //draw white background before composite so not semi-transparent - g.fillRect(0, 0, w, h); - - Composite oldComp = g2d.getComposite(); - g2d.setComposite(hovered ? alphaHovered : alphaDefault); - - FSkin.setGraphicsGradientPaint(g2d, 0, h, d10, 0, 0, l20); - g.fillRect(0, 0, w, h); - - FSkin.setGraphicsColor(g, d50); - g.drawRect(0, 0, w - 1, h - 1); - FSkin.setGraphicsColor(g, l10); - g.drawRect(1, 1, w - 3, h - 3); - - FSkin.setGraphicsColor(g, clrFore); + protected void paintContent(final Graphics2D g, int w, int h, final boolean paintPressedState) { + FSkin.setGraphicsColor(g, arrowColor); drawArrow(g); - - super.paintComponent(g); - - g2d.setComposite(oldComp); } protected abstract void drawArrow(final Graphics g); - + //timer to continue scrollling while mouse remains down final Timer timer = new Timer(50, new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -282,53 +249,19 @@ public class FScrollPanel extends FScrollPane { scrollBar.setValue(scrollBar.getValue() + scrollBar.getUnitIncrement() * incrementDirection); } }); - - private final MouseAdapter madEvents = new MouseAdapter() { - @Override - public void mousePressed(final MouseEvent e) { - scrollBar.setValue(scrollBar.getValue() + scrollBar.getUnitIncrement() * incrementDirection); - timer.start(); - } - - @Override - public void mouseReleased(final MouseEvent e) { - timer.stop(); - } - - @Override - public void mouseEntered(MouseEvent e) { - hovered = true; - repaintSelf(); - } - - @Override - public void mouseExited(MouseEvent e) { - hovered = false; - repaintSelf(); - } - - @Override - public void mouseMoved(MouseEvent e) { - if (!hovered) { - hovered = true; - repaintSelf(); - } - } - }; } - + private class LeftArrowButton extends ArrowButton { public LeftArrowButton(final JScrollBar horzScrollbar) { super(horzScrollbar, -1); } - + @Override protected void drawArrow(final Graphics g) { int x = (getWidth() - arrowSize) / 2; int y2 = getHeight() / 2; int y1 = y2 - 1; - for (int i = 0; i < arrowSize; i++) - { + for (int i = 0; i < arrowSize; i++) { g.drawLine(x, y1, x, y2); x++; y1--; @@ -336,19 +269,18 @@ public class FScrollPanel extends FScrollPane { } } } - + private class RightArrowButton extends ArrowButton { public RightArrowButton(final JScrollBar horzScrollbar) { super(horzScrollbar, 1); } - + @Override protected void drawArrow(final Graphics g) { int x = (getWidth() + arrowSize) / 2; int y2 = getHeight() / 2; int y1 = y2 - 1; - for (int i = 0; i < arrowSize; i++) - { + for (int i = 0; i < arrowSize; i++) { g.drawLine(x, y1, x, y2); x--; y1--; @@ -356,19 +288,18 @@ public class FScrollPanel extends FScrollPane { } } } - + private class TopArrowButton extends ArrowButton { public TopArrowButton(final JScrollBar vertScrollbar) { super(vertScrollbar, -1); } - + @Override protected void drawArrow(final Graphics g) { int x2 = getWidth() / 2; int x1 = x2 - 1; int y = (getHeight() - arrowSize) / 2; - for (int i = 0; i < arrowSize; i++) - { + for (int i = 0; i < arrowSize; i++) { g.drawLine(x1, y, x2, y); x1--; x2++; @@ -376,19 +307,18 @@ public class FScrollPanel extends FScrollPane { } } } - + private class BottomArrowButton extends ArrowButton { public BottomArrowButton(final JScrollBar vertScrollbar) { super(vertScrollbar, 1); } - + @Override protected void drawArrow(final Graphics g) { int x2 = getWidth() / 2; int x1 = x2 - 1; int y = (getHeight() + arrowSize) / 2; - for (int i = 0; i < arrowSize; i++) - { + for (int i = 0; i < arrowSize; i++) { g.drawLine(x1, y, x2, y); x1--; x2++; @@ -405,7 +335,7 @@ public class FScrollPanel extends FScrollPane { } return super.add(comp); } - + @Override public void add(PopupMenu popup) { if (innerPanel != null) { @@ -414,7 +344,7 @@ public class FScrollPanel extends FScrollPane { } super.add(popup); } - + @Override public void add(Component comp, Object constraints) { if (innerPanel != null) { @@ -423,7 +353,7 @@ public class FScrollPanel extends FScrollPane { } super.add(comp, constraints); } - + @Override public Component add(Component comp, int index) { if (innerPanel != null) { @@ -431,7 +361,7 @@ public class FScrollPanel extends FScrollPane { } return super.add(comp, index); } - + @Override public void add(Component comp, Object constraints, int index) { if (innerPanel != null) { @@ -440,7 +370,7 @@ public class FScrollPanel extends FScrollPane { } super.add(comp, constraints, index); } - + @Override public Component add(String name, Component comp) { if (innerPanel != null) {