Package de.nameless.graphicEngine

Source Code of de.nameless.graphicEngine.InputAdapter

package de.nameless.graphicEngine;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import com.sun.net.ssl.internal.ssl.Debug;

import de.nameless.gameEngine.core.NEClient;
import de.nameless.gameEngine.gameObjects.NEField;
import de.nameless.gameEngine.gameObjects.NEGameObject;
import de.nameless.gameEngine.gameObjects.NEUnit;
import de.nameless.gameEngine.system.GameInputAdapter;
import de.nameless.gameEngine.system.transactions.NEMoveUnitTransaktion;
import de.nameless.gameEngine.util.StateMachine.Action;
import de.nameless.gameEngine.util.StateMachine.State;
import de.nameless.gameEngine.util.StateMachine.StateMachine;
import static de.nameless.gameEngine.util.Logger.debug;

/**
*
* @author Stefan Wittek Ein Adapter welche Die eingabe events eines GLDisplay
*         verarbeiten kann. Tastatur und Maus.
*/
public class InputAdapter implements KeyListener, MouseListener,
    MouseMotionListener, MouseWheelListener {
 
  private GLDisplay display;
  private NEGraphicServer GfxServer; 
 
  private float glMousePosX;
  private float glMousePosY;
 
  private boolean grabbed = false;
  private float grabStartCameraX;
  private float grabStartCameraY;
  private float grabStartMouseX; 
 
  private float grabStartMouseY;

  public float ScrallSpeed = 0.5f;
 
  GameInputAdapter gameInput;
 
   

  /**
   * @param gfxServer
   *            the gfxServer to set
   */
  public void setClient(NEClient client) {   
    gameInput = new GameInputAdapter(client);
  }

  /**
   * Tragt den adapter beim angegebenden GlDsyplay als listener ein.
   *
   * @param d :
   *            Der GLDisplay bei dem der Input Adapter eingeragen werden
   *            soll.
   */
  public void RegisterIn(NEGraphicServer g) {
    display = g.getDisplay();
    display.addKeyListener(this);
    display.addMouseListener(this);
    display.addMouseMotionListener(this);
    display.addMouseWheelListener(this);
    this.GfxServer = g;   
  }

  public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_ESCAPE:
      display.stop();
      break;
    case KeyEvent.VK_ENTER:     
      gameInput.endTurn();     
      break;
    case KeyEvent.VK_X:     
      break;     
    case KeyEvent.VK_G:
      NEField.showGrid.toggel();     
    break;
   
   
  }

  @Override
  public void keyTyped(KeyEvent e) {
 
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    this.updateGlMousePos(e);
  }

  @Override
  public void mouseDragged(MouseEvent e) {
    this.updateGlMousePos(e);   
   
    if (grabbed){ 
     
      this.GfxServer.setCameraPos(grabStartCameraX  
                   + (grabStartMouseX - (glMousePosX  - this.GfxServer.getCamera().x ) ),
                  
                     grabStartCameraY  
                    + (grabStartMouseY - (glMousePosY - this.GfxServer.getCamera().y ) ),
                   
                    GfxServer.getCameraZ());    }
 
  }

  @Override
  public void mouseEntered(MouseEvent e) {
    this.updateGlMousePos(e);
  }

  @Override
  public void keyPressed(KeyEvent e) {
 
  }

  @Override
  public void mouseExited(MouseEvent e) {
    this.updateGlMousePos(e);   
    switch (e.getButton()) {
    case MouseEvent.BUTTON1:
      System.out.println("B1");
      break;
    case MouseEvent.BUTTON2:
      System.out.println("B2");
      break;
    case MouseEvent.BUTTON3:
      grabbed = false;     
      break
    }   
  }

  @Override
  public void mouseMoved(MouseEvent e) { 
    display.setMousePos(e.getX(), e.getY());
    this.updateGlMousePos(e);   
   
  }

  @Override
  public void mousePressed(MouseEvent e) {   
    this.updateGlMousePos(e);   
    switch (e.getButton()) {
   
    case MouseEvent.BUTTON1:     
      NEabstractGraphicObject[] hits = display.getObjectsUnderMouse();
      if(hits.length>0){
        NEabstractGraphicObject hitGfx = hits[hits.length-1];
        if(hitGfx.getCausingGameObject()!=null) this.processLMBHitObject(hitGfx.getCausingGameObject());
      }     
      break;
     
    case MouseEvent.BUTTON2:
      System.out.println("B2");
      break;
     
    case MouseEvent.BUTTON3:
      if (!grabbed){
        grabbed = true;
        grabStartCameraX = GfxServer.getCameraX();
        grabStartCameraY = GfxServer.getCameraY();
        grabStartMouseX = display.glXPos(e,this.GfxServer.getCamera()) - this.GfxServer.getCameraX() ;
        grabStartMouseY = display.glYPos(e,this.GfxServer.getCamera()) - this.GfxServer.getCameraY();       
      }
      break
    }
  }

  @Override
  public void mouseWheelMoved(MouseWheelEvent arg0) { 
    this.GfxServer.getCamera().z += arg0.getWheelRotation();
   
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    this.updateGlMousePos(e);
   
    switch (e.getButton()) {
    case MouseEvent.BUTTON1:
      //System.out.println("B1");
      break;
    case MouseEvent.BUTTON2:
      //System.out.println("B2");
      break;
    case MouseEvent.BUTTON3:
      grabbed = false
      break
    }
  }

  public void updateGlMousePos(MouseEvent e){
    this.glMousePosX = display.glXPos(e,this.GfxServer.getCamera());
    this.glMousePosY = display.glYPos(e,this.GfxServer.getCamera())
  }

  public void processLMBHitObject(NEGameObject goj){
   
    if (goj instanceof NEField) {
      gameInput.fieldHit((NEField) goj);     
    }
   
    if (goj instanceof NEUnit) {
      NEUnit unit = (NEUnit) goj;
      if(unit.getSquare() != null) this.processLMBHitObject(unit.getSquare());
    }
   
  }


 

}
TOP

Related Classes of de.nameless.graphicEngine.InputAdapter

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.