Advertisement
Guest User

ai2 extension

a guest
Jan 9th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. package edu.mit.appinventor;
  2.  
  3. import java.util.List;
  4.  
  5. import com.google.appinventor.components.annotations.DesignerComponent;
  6. import com.google.appinventor.components.annotations.DesignerProperty;
  7. import com.google.appinventor.components.annotations.PropertyCategory;
  8. import com.google.appinventor.components.annotations.SimpleFunction;
  9. import com.google.appinventor.components.annotations.SimpleObject;
  10. import com.google.appinventor.components.annotations.SimpleProperty;
  11. import com.google.appinventor.components.common.ComponentCategory;
  12. import com.google.appinventor.components.common.PropertyTypeConstants;
  13. import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
  14. import com.google.appinventor.components.runtime.Canvas;
  15. import com.google.appinventor.components.runtime.Component;
  16. import com.google.appinventor.components.runtime.ComponentContainer;
  17. import com.google.appinventor.components.runtime.ImageSprite;
  18. import com.google.appinventor.components.runtime.errors.YailRuntimeError;
  19.  
  20. /**
  21.  * This provides an interface for a canvas component, making it easy to create
  22.  * grids of sprites. These grid tiles can be arranged in different sizes and
  23.  * can be removed.
  24.  *
  25.  * @author Louis Van Steene
  26.  */
  27. @DesignerComponent(version=1,
  28.         description="Arranges sprites on a canvas into a grid.",
  29.         category=ComponentCategory.EXTENSION,
  30.         nonVisible=true,
  31.         iconName="images/extension.png")
  32. @SimpleObject(external=true)
  33. // @UsesPermissions()
  34. public class GridAdapter extends AndroidNonvisibleComponent {
  35.    
  36.     private Canvas canvas;
  37.  
  38.     protected GridAdapter(ComponentContainer container) {
  39.         super(container.$form());
  40.     }
  41.    
  42. //  @SimpleFunction
  43. //  public void AddGridToCanvas(Canvas canvas) {
  44. //      this.canvas = canvas;
  45. //  }
  46.    
  47.     @SimpleFunction(description="Turn sprites on the canvas into a grid.")
  48.     public void GenerateGrid(int rows, int columns, List<ImageSprite> sprites) {
  49.         if((rows * columns) < sprites.size()) {
  50.             throw new YailRuntimeError("There aren't enough sprites in the canvas to do that.", "GridCanvas error");
  51.         }
  52.         try {
  53.             for(int i = 0, row = 0, col = 0; i < sprites.size(); i++) {
  54.                 int width = canvas.Width();
  55.                 int height = canvas.Height();
  56.                
  57.                 ImageSprite s = sprites.get(i);
  58.                
  59.                 // set X and Y
  60.                 s.X((width / columns) * col);
  61.                 s.X((height / rows) * col);
  62.                
  63.                 // set Width and Height
  64.                 s.Width(width / columns);
  65.                 s.Height(height / rows);
  66.                
  67.                 // check if we have reached the end of this row
  68.                 if(col == columns - 1) {
  69.                     // if we have, move onto the next row
  70.                     if(row++ >= rows - 1) {
  71.                         break;
  72.                     }
  73.                     // and go back to the beginning of the column
  74.                     col = 0;
  75.                 }
  76.             }
  77.         } catch(Exception e) {
  78.             throw new YailRuntimeError(e.getMessage(), "generate grid");
  79.         }
  80.     }
  81.    
  82.     /**
  83.      * Returns the canvas object being concealed by a grid
  84.      *
  85.      * @return a {@link Canvas} in the editor
  86.      */
  87.     @SimpleProperty(
  88.             category=PropertyCategory.BEHAVIOR,
  89.             userVisible=true)
  90.     public Component CanvasComponent() {
  91.         return canvas;
  92.     }
  93.    
  94.     /**
  95.      * Specifies the canvas to be controlled by this class
  96.      *
  97.      * @param a {@link Canvas} in the editor
  98.      *
  99.      * @throws IllegalArgumentException if shape is not a legal value.
  100.      */
  101.     @SimpleProperty(description="The canvas to make into a grid.",
  102.             userVisible=true)
  103.     @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_COMPONENT)
  104.     public void CanvasComponent(Component component) {
  105.         if(component instanceof Canvas) {
  106.             canvas = (Canvas) component;
  107.         } else {
  108.             throw new YailRuntimeError("The CanvasComponent must be a Canvas!", "Illegal Argument");
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement