Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.mit.appinventor;
- import java.util.List;
- import com.google.appinventor.components.annotations.DesignerComponent;
- import com.google.appinventor.components.annotations.DesignerProperty;
- import com.google.appinventor.components.annotations.PropertyCategory;
- import com.google.appinventor.components.annotations.SimpleFunction;
- import com.google.appinventor.components.annotations.SimpleObject;
- import com.google.appinventor.components.annotations.SimpleProperty;
- import com.google.appinventor.components.common.ComponentCategory;
- import com.google.appinventor.components.common.PropertyTypeConstants;
- import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
- import com.google.appinventor.components.runtime.Canvas;
- import com.google.appinventor.components.runtime.Component;
- import com.google.appinventor.components.runtime.ComponentContainer;
- import com.google.appinventor.components.runtime.ImageSprite;
- import com.google.appinventor.components.runtime.errors.YailRuntimeError;
- /**
- * This provides an interface for a canvas component, making it easy to create
- * grids of sprites. These grid tiles can be arranged in different sizes and
- * can be removed.
- *
- * @author Louis Van Steene
- */
- @DesignerComponent(version=1,
- description="Arranges sprites on a canvas into a grid.",
- category=ComponentCategory.EXTENSION,
- nonVisible=true,
- iconName="images/extension.png")
- @SimpleObject(external=true)
- // @UsesPermissions()
- public class GridAdapter extends AndroidNonvisibleComponent {
- private Canvas canvas;
- protected GridAdapter(ComponentContainer container) {
- super(container.$form());
- }
- // @SimpleFunction
- // public void AddGridToCanvas(Canvas canvas) {
- // this.canvas = canvas;
- // }
- @SimpleFunction(description="Turn sprites on the canvas into a grid.")
- public void GenerateGrid(int rows, int columns, List<ImageSprite> sprites) {
- if((rows * columns) < sprites.size()) {
- throw new YailRuntimeError("There aren't enough sprites in the canvas to do that.", "GridCanvas error");
- }
- try {
- for(int i = 0, row = 0, col = 0; i < sprites.size(); i++) {
- int width = canvas.Width();
- int height = canvas.Height();
- ImageSprite s = sprites.get(i);
- // set X and Y
- s.X((width / columns) * col);
- s.X((height / rows) * col);
- // set Width and Height
- s.Width(width / columns);
- s.Height(height / rows);
- // check if we have reached the end of this row
- if(col == columns - 1) {
- // if we have, move onto the next row
- if(row++ >= rows - 1) {
- break;
- }
- // and go back to the beginning of the column
- col = 0;
- }
- }
- } catch(Exception e) {
- throw new YailRuntimeError(e.getMessage(), "generate grid");
- }
- }
- /**
- * Returns the canvas object being concealed by a grid
- *
- * @return a {@link Canvas} in the editor
- */
- @SimpleProperty(
- category=PropertyCategory.BEHAVIOR,
- userVisible=true)
- public Component CanvasComponent() {
- return canvas;
- }
- /**
- * Specifies the canvas to be controlled by this class
- *
- * @param a {@link Canvas} in the editor
- *
- * @throws IllegalArgumentException if shape is not a legal value.
- */
- @SimpleProperty(description="The canvas to make into a grid.",
- userVisible=true)
- @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_COMPONENT)
- public void CanvasComponent(Component component) {
- if(component instanceof Canvas) {
- canvas = (Canvas) component;
- } else {
- throw new YailRuntimeError("The CanvasComponent must be a Canvas!", "Illegal Argument");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement