Skip to content
Permalink
Browse files

Add OriginAtCenter property to Ball

  • Loading branch information
espertus authored and ewpatton committed Sep 29, 2019
1 parent 4e7df34 commit d2083ab52a54decf7b6ee1a3d3fad95ba827352d
@@ -1,6 +1,6 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

@@ -25,6 +25,7 @@

private static final String PROPERTY_NAME_RADIUS = "Radius";
private static final String PROPERTY_NAME_PAINTCOLOR = "PaintColor";
private static final String PROPERTY_NAME_ORIGIN_AT_CENTER = "OriginAtCenter";

private static final int DEFAULT_RADIUS = 5;

@@ -35,6 +36,10 @@
private int radius = DEFAULT_RADIUS;
private int diameter = 2 * radius;
private Color color = Color.BLACK;
private boolean originAtCenter = false;

int x;
int y;

/**
* Creates a new MockBall component.
@@ -64,9 +69,7 @@ private void fillCircle() {
canvas.clear();
canvas.setFillStyle(color);
canvas.beginPath();
int x = radius;
int y = radius;
canvas.arc(x, y, radius, 0, Math.PI * 2, true);
canvas.arc(radius, radius, radius, 0, Math.PI * 2, true);
canvas.fill();
}

@@ -79,6 +82,11 @@ private void setRadiusProperty(String text) {
canvas.setCoordSize(diameter, diameter);
canvas.setPixelSize(diameter, diameter);
fillCircle();
if (originAtCenter) {
// Force the position of the ballWidget to be adjusted relative
// to the parent canvas.
refreshCanvas();
}
} catch (NumberFormatException e) {
// Ignore this. If we throw an exception here, the project is unrecoverable.
}
@@ -92,30 +100,45 @@ private void setColorProperty(String text) {
fillCircle();
}

private void setZProperty(String text) {
private void refreshCanvas() {
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockBall on the palette
if (mockCanvas != null) {
mockCanvas.reorderComponents(this);
mockCanvas.reorderComponents(this); //refreshForm();
}
}

private void setXProperty(String text) {
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockBall on the palette
if (mockCanvas != null) {
mockCanvas.reorderComponents(this);
try {
x = (int) Math.round(Double.parseDouble(text));
} catch (NumberFormatException e) {
// Don't change value if unparseable (should not happen).
}
refreshCanvas();
}

private void setYProperty(String text) {
try {
y = (int) Math.round(Double.parseDouble(text));
} catch (NumberFormatException e) {
// Don't change value if unparseable (should not happen).
}
refreshCanvas();
}

private void setZProperty(String text) {
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockBall on the palette
if (mockCanvas != null) {
mockCanvas.reorderComponents(this);
}
}

private void setOriginAtCenterProperty(String text) {
originAtCenter = Boolean.valueOf(text);
refreshCanvas();
}

@Override
protected boolean isPropertyVisible(String propertyName) {
if (propertyName.equals(PROPERTY_NAME_WIDTH) ||
@@ -151,6 +174,28 @@ public void onPropertyChange(String propertyName, String newValue) {
setXProperty(newValue);
} else if (propertyName.equals(PROPERTY_NAME_Y)) {
setYProperty(newValue);
} else if (propertyName.equals(PROPERTY_NAME_ORIGIN_AT_CENTER)) {
setOriginAtCenterProperty(newValue);
}
}

@Override
public int getLeftX() {
return x - getXOffset();
}

@Override
public int getTopY() {
return y - getYOffset();
}

@Override
public int getXOffset() {
return originAtCenter ? radius : 0;
}

@Override
public int getYOffset() {
return originAtCenter ? radius : 0;
}
}
@@ -1,6 +1,6 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

@@ -23,8 +23,6 @@
* @author lizlooney@google.com (Liz Looney)
*/
final class MockCanvasLayout extends MockLayout {
private static final String PROPERTY_NAME_X = "X";
private static final String PROPERTY_NAME_Y = "Y";
private final Image image;
private String imageUrl;

@@ -108,21 +106,8 @@ void layoutChildren(LayoutInfo containerLayoutInfo) {
// Position the children.
for (MockComponent child : containerLayoutInfo.visibleChildren) {
LayoutInfo childLayoutInfo = containerLayoutInfo.layoutInfoMap.get(child);
int x;
try {
x = (int) Math.round(Double.parseDouble(child.getPropertyValue(PROPERTY_NAME_X)));
} catch (NumberFormatException e) {
// Ignore this. If we throw an exception here, the project is unrecoverable.
x = 0;
}
int y;
try {
y = (int) Math.round(Double.parseDouble(child.getPropertyValue(PROPERTY_NAME_Y)));
} catch (NumberFormatException e) {
// Ignore this. If we throw an exception here, the project is unrecoverable.
y = 0;
}
container.setChildSizeAndPosition(child, childLayoutInfo, x, y);
MockSprite sprite = (MockSprite) child;
container.setChildSizeAndPosition(child, childLayoutInfo, sprite.getLeftX(), sprite.getTopY());
}

// Update layoutWidth and layoutHeight.
@@ -139,9 +124,13 @@ void layoutChildren(LayoutInfo containerLayoutInfo) {

@Override
boolean onDrop(MockComponent source, int x, int y, int offsetX, int offsetY) {
MockSprite sprite = (MockSprite) source;

// Set position of component
source.changeProperty(PROPERTY_NAME_X, toIntegerString(x - offsetX));
source.changeProperty(PROPERTY_NAME_Y, toIntegerString(y - offsetY));
source.changeProperty(MockSprite.PROPERTY_NAME_X,
toIntegerString(x - offsetX + sprite.getXOffset()));
source.changeProperty(MockSprite.PROPERTY_NAME_Y,
toIntegerString(y - offsetY + sprite.getYOffset()));

// Perform drop
MockContainer srcContainer = source.getContainer();
@@ -151,7 +140,7 @@ boolean onDrop(MockComponent source, int x, int y, int offsetX, int offsetY) {
srcContainer.removeComponent(source, false);
}
container.addComponent(source);
((MockCanvas) container).reorderComponents((MockSprite) source);
((MockCanvas) container).reorderComponents(sprite);
return true;
}

@@ -1,6 +1,6 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

@@ -15,6 +15,8 @@
*
*/
public final class MockImageSprite extends MockImageBase implements MockSprite {
int x;
int y;

/**
* Component type name.
@@ -39,25 +41,33 @@ protected void addWidthHeightProperties() {
new YoungAndroidLengthPropertyEditor(false));
}

// Support for Z layers

private void setZProperty(String text) {
private void setXProperty(String text) {
try {
x = (int) Math.round(Double.parseDouble(text));
} catch (NumberFormatException e) {
// Don't change value if unparseable (should not happen).
}
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockImageSprite on the palette
if (mockCanvas != null) {
mockCanvas.reorderComponents(this);
}
}

private void setXProperty(String text) {
private void setYProperty(String text) {
try {
y = (int) Math.round(Double.parseDouble(text));
} catch (NumberFormatException e) {
// Don't change value if unparseable (should not happen).
}
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockImageSprite on the palette
if (mockCanvas != null) {
mockCanvas.reorderComponents(this);
}
}
private void setYProperty(String text) {

private void setZProperty(String text) {
MockCanvas mockCanvas = (MockCanvas) getContainer();
// mockCanvas will be null for the MockImageSprite on the palette
if (mockCanvas != null) {
@@ -76,4 +86,24 @@ public void onPropertyChange(String propertyName, String newValue) {
setYProperty(newValue);
}
}

@Override
public int getLeftX() {
return x;
}

@Override
public int getTopY() {
return y;
}

@Override
public int getXOffset() {
return 0;
}

@Override
public int getYOffset() {
return 0;
}
}
@@ -1,20 +1,48 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

package com.google.appinventor.client.editor.simple.components;

/**
* Marker interface for sprites
* Interface for sprites
*
*/
// spertus and lizlooney agreed this is the best place for the constants.
@SuppressWarnings("InterfaceIsType")
public interface MockSprite {
static final double DEFAULT_Z_LAYER = 1.0;
static final String PROPERTY_NAME_Z = "Z";
static final String PROPERTY_NAME_X = "X";
static final String PROPERTY_NAME_Y = "Y";
static final String PROPERTY_NAME_Z = "Z";
static final double DEFAULT_Z_LAYER = 1.0;

/**
* Gets the x-coordinate of the left edge of this sprite.
*
* @return the x-coordinate of the left edge of this sprite
*/
int getLeftX();

/**
* Gets the y-coordinate of the top edge of this sprite.
*
* @return the y-coordinate of the top edge of this sprite
*/
int getTopY();

/**
* Gets the difference between the X property and the x-coordinate of the left edge of this sprite.
*
* @return the difference between the X property and the x-coordinate of the left edge of this sprite
*/
int getXOffset();

/**
* Gets the difference between the Y property and the y-coordinate of the top of this sprite.
*
* @return the difference between the Y property and the y-coordinate of the top of this sprite
*/
int getYOffset();
}
@@ -490,11 +490,11 @@ private static int upgradeActivityStarterProperties(Map<String, JSONValue> compo
private static int upgradeBallProperties(Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 2) {
// The Heading property was changed from int to double
// The Heading property was changed from int to double.
srcCompVersion = 2;
}
if (srcCompVersion < 3) {
// The Z property was added
// The Z property was added.
srcCompVersion = 3;
}
if (srcCompVersion < 4) {
@@ -506,6 +506,10 @@ private static int upgradeBallProperties(Map<String, JSONValue> componentPropert
// The callback parameters speed and heading were added to Flung.
srcCompVersion = 5;
}
if (srcCompVersion < 6) {
// The OriginAtCenter property was added.
srcCompVersion = 6;
}
return srcCompVersion;
}
private static int upgradeBarcodeScannerProperties(Map<String, JSONValue> componentProperties,
@@ -1209,8 +1209,11 @@ Blockly.Versioning.AllUpgradeMaps =
for (Element block : getAllMatchingGenusBlocks("Ball-Flung")) {
markBlockBad(block, String.format(CHANGED_FLUNG_WARNING, "Flung"));
*/
5: "ai1CantDoUpgrade" // Just indicates we couldn't do upgrade even if we wanted to
5: "ai1CantDoUpgrade", // Just indicates we couldn't do upgrade even if we wanted to

// The CenterAtOrigin property was added.
// The default value of false is correct for upgraded apps.
6: "noUpgrade"
}, // End Ball upgraders

"BarcodeScanner": {

0 comments on commit d2083ab

Please sign in to comment.
You can’t perform that action at this time.