Package framework.rendering

Source Code of framework.rendering.SpriteComponent

package framework.rendering;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import framework.component.Component;
import framework.component.filter.ComponentFilter;
import framework.component.filter.ComponentPathFilter;
import framework.component.path.RelativeComponentPath;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSystem;
import framework.event.filter.EventSenderComponentFilter;
import framework.spacial.OrientationComponent;
import framework.spacial.OrientationComponent.OrientationChangedEvent;

public class SpriteComponent extends RenderableComponent implements EventListener{

  private String imageFilename = null;
  private Image image = null;

  public SpriteComponent(String imageFilename, Vector2f offsetFromOrigin, int width, int height){
    super();
    setImageFilename(imageFilename);
    if(getImage() != null){
      if(width > 0 && height > 0){
        this.image = image.getScaledCopy(width, height);
      }
    }

    setOffsetFromOrigin(offsetFromOrigin);

    RelativeComponentPath orientPath = new RelativeComponentPath(OrientationComponent.class.getName(), this, 1, new int[]{0});
    ComponentFilter orientFilter = new ComponentPathFilter(orientPath);
    EventSystem.getInstance().registerEventListener(this, "OrientationChanged", new EventSenderComponentFilter(orientFilter));
  }

  public SpriteComponent(String imageFilename){
    this(imageFilename, new Vector2f(0, 0),0,0);
  }

  public SpriteComponent(String imageFilename, int width, int height){
    this(imageFilename, new Vector2f(0, 0),width,height);
  }

  public float getWidth() {
    return image != null ? image.getWidth() : 0;
  }

  public float getHeight() {
    return image != null ? image.getHeight() : 0;
  }

  public void setHeight(int height) {
    if(getImage() != null){
      setWidthAndHeight(getImage().getWidth(), height);
    }
  }

  public void setWidth(int width) {
    if(getImage() != null){
      setWidthAndHeight(width, getImage().getHeight());
    }
  }

  private void setWidthAndHeight(int width, int height) {
    Image img = getImage();
    if(img != null){
      this.image = img.getScaledCopy(width, height);
    }
  }

  public String getImageFilename() {
    return imageFilename;
  }

  public void setImageFilename(String imageFilename) {
    setImageFilename(imageFilename, false);
  }

  public void setImageFilename(String imageFilename, boolean maintainCurrentSize) {
    if(this.imageFilename == null  || !this.imageFilename.equals(imageFilename)){
      this.imageFilename = imageFilename;
      if(getImage() != null && maintainCurrentSize){
        int width = getImage().getWidth();
        int height = getImage().getHeight();
        reloadImage();
        setWidthAndHeight(width, height);
      }else{
        reloadImage();
      }
    }
  }

  public void scaleToMatchTexture(){
    if(imageFilename != null){
      Image i;
      try {
        i = new Image(imageFilename);
        if(i != null){
          setWidthAndHeight(i.getHeight(),i.getWidth());
        }
      } catch (SlickException e) {
        e.printStackTrace();
      }
    }
  }

  public void reloadImage(){
    if(imageFilename != null){
      try {
        this.image = new Image(imageFilename);
      } catch (SlickException e) {
        e.printStackTrace();
      } catch(RuntimeException e){
        e.printStackTrace();
      }
    }
  }

  @Override
  public boolean allowSameTypedSiblings() {
    return false;
  }

  @Override
  protected byte getDataFormatVersion() {
    return 0;
  }

  public Image getImage(){
    return image;
  }

  @Override
  public void onEvent(Event e) {
    if(e != null){
      if(e.getType().equals("OrientationChanged")){
        rotate(((OrientationChangedEvent) e).getDelta());
      }
    }
  }

  @Override
  public void onSiblingAdded(Component sibling) {
    super.onSiblingAdded(sibling);
    if(sibling != null && sibling.isType(OrientationComponent.class)){
      rotate(((OrientationComponent) sibling).getValue());
    }
  }

  private void rotate(float theta){
    double oldTheta = getOffsetFromOrigin().getTheta();
    Vector2f offset = getOffsetFromOrigin();
    offset.setTheta(oldTheta + theta);
    setOffsetFromOrigin(offset);
  }

  @Override
  public void draw(Graphics g, int x, int y) {
    if(getImage() != null){
      g.drawImage(image, x, y);
    }
  }


  public void setImage(Image image) {
    this.image = image;
  }
}
TOP

Related Classes of framework.rendering.SpriteComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.