Component.java :  » Game » minime » minime » core » Java Open Source

Java Open Source » Game » minime 
minime » minime » core » Component.java
/**
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.core;

import minime.Drawable;
import minime.Logger;

/**
 * An Drawable object that has focus and a event listener associated with it.
 * The contents displayed and their interaction with the user are defined by
 * subclasses.
 * 
 * @author yishu
 * 
 */
public abstract class Component extends Drawable implements EventListener {
  private boolean focused;

  private boolean initFocused;

  static final Logger LOG = Logger.getLogger("minime.Component");

  public Component() {
    focused = false;
  }

  // focused use/override selected attribute
  public boolean isFocused() {
    return focused;
  }

  public void setFocused(boolean focused) {
    this.focused = focused;
  }

  /**
   * Sets the component object focus to true or false based on boolean
   * parameter.
   * 
   * @param focus
   *            true to set the focus, false to remove the focus
   * 
   */
  public void setFocus(boolean focus) {
    if (isFocused()) {
      // if object already have the focus, return
      if (focus)
        return;

      FocusManager.getInstance().removeFocus(this);
      return;
    }

    // set the focus on the component
    if (focus)
      FocusManager.getInstance().setFocus(this);
  }

  public void setInitFocused(boolean initFocused) {
    this.initFocused = initFocused;
  }

  public boolean isInitFocused() {
    return initFocused;
  }

  public int getPriority() {
    return COMPONENT_PRIORITY; // average priority
  }

  /*
   * Activate the Component, add it to the event loop
   * 
   * @see minime.Drawable#activate()
   */
  public void activate() {
    EventDispatcher.getInstance().attach(this);
  }

  /*
   * Deactivate the component
   * 
   * When the component is de-activated, it is removed from the focused list,
   * and from the event list
   * 
   * @see minime.Drawable#deactivate()
   */
  public void deactivate() {
    // ... don't test if hasFocus because component could be hidden by a
    // popupComponent
    FocusManager.getInstance().removeFocus(this);
    EventDispatcher.getInstance().detach(this);
  }

  /**
   * Called when a key is pressed.
   * 
   * User can choose to process either paramA or paramB
   * paramA: key code
   * paramB: game action translated from key code
   *
   * @param evt
   */
  protected boolean onKeyPressed(Event evt) {
    // null implementation as default
    return false;
  }

  /**
   * Called when a key is keyReleased.
   * 
   * User can choose to process either paramA or paramB
   * paramA: key code
   * paramB: game action translated from key code
   *
   * @param evt
   * @return true if event is consumed
   */
  protected boolean onKeyReleased(Event evt) {
    // null implementation as default
    return false;
  }

  /**
   * Called when the pointer is pressed. 
   * 
   * @param x the horizontal location where the pointer was pressed
   * @param y the vertical location where the pointer was pressed
   * @return true if event is consumed
   */
  protected boolean onPointerPressed(int x, int y) {
    // null implementation as default
    return false;
  }

  /**
   * Called when the pointer is released. 
   * 
   * @param x the horizontal location where the pointer was released
   * @param y the vertical location where the pointer was released
   * @return true if event is consumed
   */
  protected boolean onPointerReleased(int x, int y) {
    // null implementation as default
    return false;
    
  }

  /**
   * Called when the pointer is dragged. 
   * 
   * @param x the horizontal location where the pointer was dragged
   * @param y the vertical location where the pointer was dragged
   * @return true if event is consumed
   */
  protected boolean onPointerDragged(int x, int y) {
    // null implementation as default
    return false;
  }

  /**
   * Default implementation handles key events, pointer events and softbar events
   * 
   */
  public boolean onEvent(Event evt) {
    LOG.debug("onEvent:" + evt);
    
    boolean handled = false;

    switch (Event.getEventType(evt.type)) {
    case Event.POINTER_EVENT: {
      handled = this.containsPoint(evt.paramA, evt.paramB) ? handlePointerEvent(evt)
          : false;
      break;
    }
    case Event.KEY_EVENT: {
      handled = isFocused() ? handleKeyEvent(evt) : false;
      break;
    }
    case Event.SB_EVENT: {
      handled = onSoftBarEvent(evt);
      break;
    }
    default: {
      // TODO: might need to forward other events
      handled = false;
      break;
    }
    }

    if (handled)
      Runtime.getInstance().repaint();
    
    return handled;
  }

  /**
   * Method that processes pointer events. The default implementation does
   * nothing. User should override it for process pointer events.
   * 
   * @param evt
   * @return true if it is a pointer event and can be consumed by this
   *         Component
   */
  private boolean handlePointerEvent(Event evt) {
    LOG.debug("onPointerEvent:" + evt);
    int x = evt.paramA;
    int y = evt.paramB;
    
    switch (evt.type) {
    case Event.POINTER_PRESSED:
      return onPointerPressed(x, y);
    case Event.POINTER_RELEASED:
      return onPointerReleased(x, y);
    case Event.POINTER_DRAGGED:
      return onPointerDragged(x, y);
    default:
      // wrong pointer event type happened
      return false;
    }
  }

  /**
   * Method that processes key events. The default implementation does
   * nothing. User should override it to process key events.
   * 
   * @param evt
   * @return true if it is a key event and can be consumed by this Component
   */
  private boolean handleKeyEvent(Event evt){
      LOG.debug("onKeyEvent:" + evt);
      boolean handled = false;
      
      if(evt.type == Event.KEY_DOWN_EVENT) {
        handled = onKeyPressed(evt);
      } else if(evt.type == Event.KEY_UP_EVENT) {
        handled = onKeyReleased(evt);
      } else {
        // wrong key event type happened
      }
      return handled;
    }

  protected boolean onSoftBarEvent(Event evt) {
    return false;
  }

}
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.