TComponent.java :  » Game » foogamelib » kku » cs » fgl » gui » Java Open Source

Java Open Source » Game » foogamelib 
foogamelib » kku » cs » fgl » gui » TComponent.java
package kku.cs.fgl.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.event.EventListenerList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Input;
import org.newdawn.slick.InputListener;
import org.newdawn.slick.opengl.renderer.Renderer;
import org.newdawn.slick.opengl.renderer.SGL;

import kku.cs.fgl.Actor;
import kku.cs.fgl.GameLoader;
import kku.cs.fgl.GamePane;

abstract public class TComponent extends Actor {

  /**
   *  Component  focus  1 
   */
  static private TComponent focusCompoent;
  static SGL GL = Renderer.get();

  public static TComponent getFocusCompoent() {
    return focusCompoent;
  }

  public static void initGUI(Input input) {
    class InputAdapter implements InputListener {

      public void controllerButtonPressed(int controller, int button) {
        if (focusCompoent != null)
          focusCompoent.controllerButtonPressed(controller, button);
      }

      public void controllerButtonReleased(int controller, int button) {
        if (focusCompoent != null)
          focusCompoent.controllerButtonReleased(controller, button);
      }

      public void controllerDownPressed(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerDownPressed(controller);
      }

      public void controllerDownReleased(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerDownReleased(controller);
      }

      public void controllerLeftPressed(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerLeftPressed(controller);
      }

      public void controllerLeftReleased(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerLeftReleased(controller);
      }

      public void controllerRightPressed(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerRightPressed(controller);
      }

      public void controllerRightReleased(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerRightReleased(controller);
      }

      public void controllerUpPressed(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerUpPressed(controller);
      }

      public void controllerUpReleased(int controller) {
        if (focusCompoent != null)
          focusCompoent.controllerUpReleased(controller);
      }

      public void inputEnded() {
        if (focusCompoent != null)
          focusCompoent.inputEnded();
      }

      public boolean isAcceptingInput() {
        if (focusCompoent != null)
          return focusCompoent.isAcceptingInput();
        return false;
      }

      public void keyPressed(int key, char c) {
        if (focusCompoent != null)
          focusCompoent.keyPressed(key, c);
      }

      public void keyReleased(int key, char c) {
        if (focusCompoent != null)
          focusCompoent.keyReleased(key, c);
      }

      public void mouseClicked(int button, int x, int y, int clickCount) {
        if (focusCompoent != null)
          focusCompoent.mouseClicked(button, x, y, clickCount);
      }

      public void mouseMoved(int oldx, int oldy, int newx, int newy) {
        if (focusCompoent != null)
          focusCompoent.mouseMoved(oldx, oldy, newx, newy);
      }

      public void mousePressed(int button, int x, int y) {
        if (focusCompoent != null)
          focusCompoent.mousePressed(button, x, y);
      }

      public void mouseReleased(int button, int x, int y) {
        if (focusCompoent != null)
          focusCompoent.mouseReleased(button, x, y);
      }

      public void mouseWheelMoved(int change) {
        if (focusCompoent != null)
          focusCompoent.mouseWheelMoved(change);
      }

      public void setInput(Input input) {
        if (focusCompoent != null)
          focusCompoent.input = input;

      }

    };
    
    input.addPrimaryListener(new InputAdapter());
    input.enableKeyRepeat(200,100);      
  }

  private TComponent owner;

  private Font font = GamePane.getFont(0);
  private Color textColor = Color.white;

  private Color bgColor   = Color.darkGray;
    private EventListenerList events = new EventListenerList();
  public int tag;
    
  private String action="action";

  public TComponent() {
  }

  public TComponent(float x, float y) {
    super(x, y);
  }

  public TComponent(float x, float y, int width, int height) {
    super(x, y, width, height);

  }

  public void addActionListener(ActionListener a) {
    events.add(ActionListener.class, a);
  }

  public void afterAdded() {

  }
  public boolean canFocus() {
    return true;
  }

  public void controllerButtonPressed(int controller, int button) {
    // TODO Auto-generated method stub

  }

  public void controllerButtonReleased(int controller, int button) {
    // TODO Auto-generated method stub

  }

  public void controllerDownPressed(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerDownReleased(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerLeftPressed(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerLeftReleased(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerRightPressed(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerRightReleased(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerUpPressed(int controller) {
    // TODO Auto-generated method stub

  }

  public void controllerUpReleased(int controller) {
    // TODO Auto-generated method stub

  }

  public void fireActionEvent(int id){
    ActionEvent evt = new ActionEvent(this,id,action);
    ActionListener[]T =events.getListeners(ActionListener.class);
    for (ActionListener listener : T) {
      listener.actionPerformed(evt);
    }
  }

  public void focus() {
    if (canFocus()) {
      focusCompoent = this;
    }
  }

  public String getAction() {
    return action;
  }

  public Color getBgColor() {
    return bgColor;
  }


  protected EventListenerList getEvents() {
    return events;
  }

  public Font getFont() {
    return font;
  }

  public TComponent getOwner() {
    return owner;
  }

  public Color getTextColor() {
    return textColor;
  }

  public void inputEnded() {
    // TODO Auto-generated method stub

  }

  public boolean isAcceptingInput() {
    return true;
  }

  public boolean isFocus() {
    return this == focusCompoent;
  }

  public void keyPressed(int key, char c) {

  }

  public void keyReleased(int key, char c) {

  }

  @Override
  public void mouseClicked(int button) {
    focus();
  }

  public void mouseClicked(int button, int x, int y, int clickCount) {
    // TODO Auto-generated method stub

  }

  public void mouseMoved(int oldx, int oldy, int newx, int newy) {
    // TODO Auto-generated method stub

  }

  public void mousePressed(int button, int x, int y) {
    // TODO Auto-generated method stub

  }

  public void mouseReleased(int button, int x, int y) {
    // TODO Auto-generated method stub

  }

  public void mouseWheelMoved(int change) {

  }

  public void removeActionListener(ActionListener a) {
    events.remove(ActionListener.class, a);
  }

  public void setAction(String action) {
    this.action = action;
  }

  public void setBgColor(Color bgColor) {
    this.bgColor = bgColor;
  }

  public void setFont(Font font) {
    this.font = font;
  }

  public void setTextColor(Color textColor) {
    this.textColor = textColor;
  }
  
}
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.