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) {
clear();
CardAreaPanel newCardPanel = null;
for (Card card : model) {
addCard(card);
CardAreaPanel panel = addCard(card);
if (newCardPanel == null && !orderedCards.contains(card)) {
newCardPanel = panel;
}
}
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));
cardPanels.add(cardPanel);
return cardPanel;

View File

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

View File

@@ -2,6 +2,8 @@ package forge.toolbox;
import java.util.ArrayList;
import com.badlogic.gdx.math.Vector2;
import forge.Forge.Graphics;
public abstract class FContainer extends FDisplayObject {
@@ -74,4 +76,25 @@ public abstract class FContainer extends FDisplayObject {
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) {
Vector2 screenPos = getScreenPosition();
float screenLeft = screenPos.x;
float screenRight = screenLeft + getWidth();
float screenTop = screenPos.y;
float screenBottom = screenTop + getHeight();
Vector2 childPos = getChildRelativePosition(child);
if (childPos == null) { return; } //do nothing if not a valid child
Vector2 childScreenPos = child.getScreenPosition();
float childScreenLeft = childScreenPos.x;
float childScreenRight = childScreenLeft + child.getWidth();
float childScreenTop = childScreenPos.y;
float childScreenBottom = childScreenTop + child.getHeight();
float childLeft = childPos.x;
float childRight = childLeft + child.getWidth();
float childTop = childPos.y;
float childBottom = childTop + child.getHeight();
float dx = 0;
if (childScreenLeft < screenLeft) {
dx = screenLeft - childScreenLeft;
if (childLeft < 0) {
dx = childLeft;
}
else if (childScreenRight > screenRight) {
dx = screenRight - childScreenRight;
else if (childRight > getWidth()) {
dx = childRight - getWidth();
}
float dy = 0;
if (childScreenTop < screenTop) {
dy = screenTop - childScreenTop;
if (childTop < 0) {
dy = childTop;
}
else if (childScreenBottom > screenBottom) {
dy = screenBottom - childScreenBottom;
else if (childBottom > getHeight()) {
dy = childBottom - getHeight();
}
if (dx == 0 && dy == 0) { return; }
setScrollPositions(scrollLeft - dx, scrollTop - dy);
setScrollPositions(scrollLeft + dx, scrollTop + dy);
}
private void setScrollPositions(float scrollLeft0, float scrollTop0) {