Fix so new cards added to a zone are scrolled into view automatically

This commit is contained in:
drdev
2014-03-29 23:29:00 +00:00
parent 82aefc0169
commit 5f30f26d6a
4 changed files with 50 additions and 22 deletions

View File

@@ -25,13 +25,22 @@ public abstract class VCardDisplayArea extends VDisplayArea {
protected void refreshCardPanels(List<Card> model) { protected void refreshCardPanels(List<Card> model) {
clear(); clear();
CardAreaPanel newCardPanel = null;
for (Card card : model) { for (Card card : model) {
addCard(card); CardAreaPanel panel = addCard(card);
if (newCardPanel == null && !orderedCards.contains(card)) {
newCardPanel = panel;
}
} }
revalidate(); revalidate();
if (newCardPanel != null) { //if new cards added, ensure first new card is scrolled into view
scrollIntoView(newCardPanel);
}
} }
public CardAreaPanel addCard(final Card card) { protected CardAreaPanel addCard(final Card card) {
CardAreaPanel cardPanel = add(new CardAreaPanel(card)); CardAreaPanel cardPanel = add(new CardAreaPanel(card));
cardPanels.add(cardPanel); cardPanels.add(cardPanel);
return cardPanel; return cardPanel;

View File

@@ -32,7 +32,7 @@ public class VField extends VZoneDisplay {
} }
@Override @Override
public CardAreaPanel addCard(final Card card) { protected CardAreaPanel addCard(final Card card) {
CardAreaPanel cardPanel = super.addCard(card); CardAreaPanel cardPanel = super.addCard(card);
//cardPanel.setVisible(false); //hide placeholder until card arrives //TODO: Uncomment when animation set up //cardPanel.setVisible(false); //hide placeholder until card arrives //TODO: Uncomment when animation set up
return cardPanel; return cardPanel;

View File

@@ -2,6 +2,8 @@ package forge.toolbox;
import java.util.ArrayList; import java.util.ArrayList;
import com.badlogic.gdx.math.Vector2;
import forge.Forge.Graphics; import forge.Forge.Graphics;
public abstract class FContainer extends FDisplayObject { public abstract class FContainer extends FDisplayObject {
@@ -74,4 +76,25 @@ public abstract class FContainer extends FDisplayObject {
listeners.add(this); listeners.add(this);
} }
} }
public Vector2 getChildRelativePosition(FDisplayObject child) {
return getChildRelativePosition(child, 0, 0);
}
private Vector2 getChildRelativePosition(FDisplayObject child, float offsetX, float offsetY) {
for (FDisplayObject c : children) { //check direct children first
if (child == c) {
return new Vector2(c.getLeft() + offsetX, c.getTop() + offsetY);
}
}
for (FDisplayObject c : children) { //check each child's children next if possible
if (c instanceof FContainer) {
Vector2 pos = ((FContainer)c).getChildRelativePosition(child, c.getLeft() + offsetX, c.getTop() + offsetY);
if (pos != null) {
return pos;
}
}
}
return null;
}
} }

View File

@@ -53,36 +53,32 @@ public abstract class FScrollPane extends FContainer {
} }
public void scrollIntoView(FDisplayObject child) { public void scrollIntoView(FDisplayObject child) {
Vector2 screenPos = getScreenPosition(); Vector2 childPos = getChildRelativePosition(child);
float screenLeft = screenPos.x; if (childPos == null) { return; } //do nothing if not a valid child
float screenRight = screenLeft + getWidth();
float screenTop = screenPos.y;
float screenBottom = screenTop + getHeight();
Vector2 childScreenPos = child.getScreenPosition(); float childLeft = childPos.x;
float childScreenLeft = childScreenPos.x; float childRight = childLeft + child.getWidth();
float childScreenRight = childScreenLeft + child.getWidth(); float childTop = childPos.y;
float childScreenTop = childScreenPos.y; float childBottom = childTop + child.getHeight();
float childScreenBottom = childScreenTop + child.getHeight();
float dx = 0; float dx = 0;
if (childScreenLeft < screenLeft) { if (childLeft < 0) {
dx = screenLeft - childScreenLeft; dx = childLeft;
} }
else if (childScreenRight > screenRight) { else if (childRight > getWidth()) {
dx = screenRight - childScreenRight; dx = childRight - getWidth();
} }
float dy = 0; float dy = 0;
if (childScreenTop < screenTop) { if (childTop < 0) {
dy = screenTop - childScreenTop; dy = childTop;
} }
else if (childScreenBottom > screenBottom) { else if (childBottom > getHeight()) {
dy = screenBottom - childScreenBottom; dy = childBottom - getHeight();
} }
if (dx == 0 && dy == 0) { return; } if (dx == 0 && dy == 0) { return; }
setScrollPositions(scrollLeft - dx, scrollTop - dy); setScrollPositions(scrollLeft + dx, scrollTop + dy);
} }
private void setScrollPositions(float scrollLeft0, float scrollTop0) { private void setScrollPositions(float scrollLeft0, float scrollTop0) {