Package testing

Source Code of testing.GridLine

package testing;

import java.io.IOException;

import framework.collision.CollisionComponent;
import framework.collision.CollisionComponent.CollisionStartedEvent;
import framework.collision.CollisionComponent.CollisionStoppedEvent;
import framework.component.Component;
import framework.component.util.MouseFollowerComponent;
import framework.event.Event;
import framework.event.EventListener;
import framework.event.EventSystem;
import framework.event.SiblingFilter;
import framework.event.filter.EventSenderComponentFilter;
import framework.io.CustomInputStream;
import framework.rendering.SpriteComponent;

public class GridLine extends Component implements EventListener{

  private boolean hasBeenPlaced = false;
  private boolean mouseInside = false;
 
  private static final String emptyImage = "res/img/dot.png";
  private static final String mousedOverImage = "res/img/square.png";
  private static final String placedImage = "res/img/line.png";
 
  public GridLine() {
    super();
    init();
  }

  public GridLine(CustomInputStream in, int baseID, byte versionNum) throws IOException {
    super(in, baseID, versionNum);
    init();
  }

  private void init() {
    EventSystem.getInstance().registerEventListener(this, "MouseReleased", null);
    SiblingFilter filter = new SiblingFilter(this, CollisionComponent.class.getName());
    EventSystem.getInstance().registerEventListener(this, "CollisionStarted", new EventSenderComponentFilter(filter));
    EventSystem.getInstance().registerEventListener(this, "CollisionStopped", new EventSenderComponentFilter(filter));
  }

  @Override
  public boolean allowSameTypedSiblings() {
    return true;
  }
 
  @Override
  protected byte getDataFormatVersion() {
    return 0;
  }

  @Override
  public void onEvent(Event e) {
    if(e != null){
      if(e.getType().equals("MouseReleased")){
        if(mouseInside){
          if(!hasBeenPlaced){
            setHasBeenPlaced(true);
          }
        }
      }else if(e.getType().equals("CollisionStarted")){
        CollisionStartedEvent c = (CollisionStartedEvent) e;
        MouseFollowerComponent mouse = (MouseFollowerComponent) c.getCollidedWith().getSiblingByType(MouseFollowerComponent.class.getName());
        if(mouse != null){
          setMouseInside(true);
        }
      }else if(e.getType().equals("CollisionStopped")){
        CollisionStoppedEvent c = (CollisionStoppedEvent) e;
        MouseFollowerComponent mouse = (MouseFollowerComponent) c.getCollidedWith().getSiblingByType(MouseFollowerComponent.class.getName());
        if(mouse != null){
          setMouseInside(false);
        }
      }
    }
  }
 
  public boolean hasBeenPlaced() {
    return hasBeenPlaced;
  }

  public void setHasBeenPlaced(boolean hasBeenPlaced) {
    this.hasBeenPlaced = hasBeenPlaced;
    SpriteComponent s = (SpriteComponent) getSiblingByType(SpriteComponent.class.getName());
    if(s != null){
      s.setImageFilename(hasBeenPlaced ? placedImage : emptyImage);
    }
  }

  public boolean isMouseInside() {
    return mouseInside;
  }

  private void setMouseInside(boolean mouseInside) {
    this.mouseInside = mouseInside;
    if(!hasBeenPlaced){
      SpriteComponent s = (SpriteComponent) getSiblingByType(SpriteComponent.class.getName());
      if(s != null){
        s.setImageFilename(mouseInside ? mousedOverImage : emptyImage);
      }
    }
  }
 
  @Override
  public void onSiblingAdded(Component sibling) {
    super.onSiblingAdded(sibling);
    if(sibling != null && sibling.isType(SpriteComponent.class)){
      ((SpriteComponent) sibling).setImageFilename(emptyImage);
    }
  }
}
TOP

Related Classes of testing.GridLine

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.