Support moving dialogs around by dragging titlebar

This commit is contained in:
drdev
2014-04-15 05:13:04 +00:00
parent 5471cd32a4
commit 00db8ef1dc
2 changed files with 58 additions and 7 deletions

View File

@@ -49,11 +49,13 @@ public abstract class FContainer extends FDisplayObject {
@Override @Override
public void setBounds(float x, float y, float width, float height) { public void setBounds(float x, float y, float width, float height) {
super.setBounds(x, y, width, height); super.setBounds(x, y, width, height);
doLayout(width, height); doLayout(width, height); //always re-do layout when setBounds used
} }
@Override @Override
public void setSize(float width, float height) { public void setSize(float width, float height) {
if (getWidth() == width && getHeight() == height) { return; }
super.setSize(width, height); super.setSize(width, height);
doLayout(width, height); doLayout(width, height);
} }

View File

@@ -16,11 +16,11 @@ public abstract class FDialog extends FOverlay {
private static final float TITLE_HEIGHT = Math.round(Utils.AVG_FINGER_HEIGHT * 0.6f); private static final float TITLE_HEIGHT = Math.round(Utils.AVG_FINGER_HEIGHT * 0.6f);
private static float INSETS = 10; private static float INSETS = 10;
private FLabel lblTitlebar; private Titlebar lblTitlebar;
private float totalHeight; private float totalHeight;
protected FDialog(String title) { protected FDialog(String title) {
lblTitlebar = add(new FLabel.Builder().text(title).icon(FSkinImage.FAVICON).fontSize(12).align(HAlignment.LEFT).build()); lblTitlebar = add(new Titlebar(title));
} }
@Override @Override
@@ -49,9 +49,9 @@ public abstract class FDialog extends FOverlay {
protected void drawBackground(Graphics g) { protected void drawBackground(Graphics g) {
super.drawBackground(g); super.drawBackground(g);
float x = INSETS; float x = lblTitlebar.getLeft();
float y = lblTitlebar.getTop(); float y = lblTitlebar.getTop();
float w = getWidth() - 2 * x; float w = lblTitlebar.getWidth();
float h = totalHeight; float h = totalHeight;
g.drawImage(FSkinTexture.BG_TEXTURE, x, y, w, h); g.drawImage(FSkinTexture.BG_TEXTURE, x, y, w, h);
g.fillRect(FScreen.TEXTURE_OVERLAY_COLOR, x, y, w, h); g.fillRect(FScreen.TEXTURE_OVERLAY_COLOR, x, y, w, h);
@@ -62,9 +62,9 @@ public abstract class FDialog extends FOverlay {
@Override @Override
protected void drawOverlay(Graphics g) { protected void drawOverlay(Graphics g) {
float x = INSETS; float x = lblTitlebar.getLeft();
float y = lblTitlebar.getTop(); float y = lblTitlebar.getTop();
float w = getWidth() - 2 * x; float w = lblTitlebar.getWidth();
float h = totalHeight; float h = totalHeight;
//draw border around dialog //draw border around dialog
@@ -74,4 +74,53 @@ public abstract class FDialog extends FOverlay {
y += TITLE_HEIGHT; y += TITLE_HEIGHT;
g.drawLine(1, BORDER_COLOR, x, y, x + w, y); g.drawLine(1, BORDER_COLOR, x, y, x + w, y);
} }
private class Titlebar extends FLabel {
private Titlebar(String title) {
super(new FLabel.Builder().text(title).icon(FSkinImage.FAVICON).fontSize(12).align(HAlignment.LEFT));
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
for (FDisplayObject child : FDialog.this.getChildren()) {
child.setLeft(child.getLeft() + deltaX);
child.setTop(child.getTop() + deltaY);
}
return true;
}
@Override
public boolean panStop(float x, float y) {
//ensure titlebar in view after stopping panning so it's accessible
float dx = 0;
float dy = 0;
float maxLeft = FDialog.this.getWidth() - lblTitlebar.getHeight();
if (lblTitlebar.getLeft() > maxLeft) {
dx = maxLeft - lblTitlebar.getLeft();
}
else {
float minRight = lblTitlebar.getHeight();
if (lblTitlebar.getRight() < minRight) {
dx = minRight - lblTitlebar.getRight();
}
}
float maxTop = FDialog.this.getHeight() - lblTitlebar.getHeight();
if (lblTitlebar.getTop() > maxTop) {
dy = maxTop - lblTitlebar.getTop();
}
else {
float minBottom = lblTitlebar.getHeight();
if (lblTitlebar.getBottom() < minBottom) {
dy = minBottom - lblTitlebar.getBottom();
}
}
if (dx != 0 || dy != 0) {
for (FDisplayObject child : FDialog.this.getChildren()) {
child.setLeft(child.getLeft() + dx);
child.setTop(child.getTop() + dy);
}
}
return true;
}
}
} }