MNode.java :  » Game » hey-apple » com » margenta » android » framework » node » Android Open Source

Android Open Source » Game » hey apple 
hey apple » com » margenta » android » framework » node » MNode.java
package com.margenta.android.framework.node;

import java.util.ArrayList;
import com.margenta.android.framework.Destroyable;

import android.graphics.Canvas;
import android.graphics.Paint;

public class MNode implements Destroyable{
  
  public float width = 0, height = 0;
  public float left = 0, top = 0;
  
  public float originX = .5f, originY = .5f;
  public float scaleX = 1f , scaleY = 1f;
  
  public float rotate = 0;
  
  public int alpha = 255;
  public boolean visible = true;
  
  public ArrayList<MNode> vcChild = new ArrayList<MNode>();
  
  public void onInit() {};

  public MNode() {
    onInit();
  }

  protected void onDraw(Canvas c, Paint p) {
    
  }
  
  public void copyTo(MNode ret){
    ret.width = this.width;
    ret.height = this.height;
    ret.left = this.left;
    ret.top = this.top;
    ret.rotate = this.rotate;
    ret.originX = this.originX;
    ret.originY = this.originY;
    ret.scaleX = this.scaleX;
    ret.scaleY = this.scaleY;
    ret.alpha = this.alpha;
    ret.visible = this.visible;
  }
  
  public MNode copyProperty(){
    MNode ret = null;
    try {
      ret = this.getClass().newInstance();
      copyTo(ret);
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return ret;
  }
  
  public MNode copyClone(){
    MNode ret = copyProperty();
    
    if(ret!=null){
      for(MNode e:this.vcChild){
        ret.addChild(e.copyClone());
      }
    }
    
    return ret;
  }
  
  public void setCenter(){
    left -= width/2;
    top -= height/2;
  }

  public void addChild(MNode e) {
    vcChild.add(e);
  }

  public void removeChild(MNode e) {
    vcChild.remove(e);
  }
  
  private boolean isOn(float targetX, float targetY) {
    if (targetX > 0 && targetX < width && targetY > 0 && targetY < height)
      return true;
    else
      return false;
  }

  public boolean isOnTarget(int targetX, int targetY) {
    return isOn(targetX - left, targetY - top);
  }
  
  public void draw(Canvas c, Paint p) {
    if (visible) {
      Paint paint = new Paint(p);
      c.save();
      c.translate(left, top);
      c.scale(scaleX, scaleY, width * originX, height * originY);
      c.rotate(rotate, width * originX, height * originY);      
      
      if (alpha < 255) {
        if (p.getAlpha() < 255)
          paint.setAlpha((int) (alpha * p.getAlpha() / 255f));
        else
          paint.setAlpha(alpha);
      }
      
      onDraw(c,paint);

      for (MNode node : vcChild)
        node.draw(c, paint);
            
      c.restore();
    }
  }
  
  @Override
  public void destroy(){
    for(MNode node : vcChild)
      node.destroy();
    vcChild.clear();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.