Package net.cis.client.login

Source Code of net.cis.client.login.CharacterViewUIController

/**
*
*/
package net.cis.client.login;

import net.cis.common.model.system.Character;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyEventSubscriber;
import de.lessvoid.nifty.controls.ListBox;
import de.lessvoid.nifty.controls.RadioButtonGroupStateChangedEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
* @author SchaeckerM
*
*/
public class CharacterViewUIController implements ScreenController {

  private SimpleApplication app;
  private Nifty nifty;
  private Screen screen;
  private Camera cam;
  private boolean zoomed = false;

  private ListBox<Character> characterListBox;

  public CharacterViewUIController(SimpleApplication app) {
    this.app = app;
    this.cam = app.getCamera();
  }

  @Override
  public void bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    this.screen = screen;
  }

  @Override
  public void onEndScreen() {

  }

  @SuppressWarnings("unchecked")
  @Override
  public void onStartScreen() {
    screen.findElementByName("panel.character.create").hideWithoutEffect();
    screen.findElementByName("panel.messagebox").hideWithoutEffect();
    screen.findElementByName("panel.character.center").hideWithoutEffect();
    characterListBox = screen.findNiftyControl("character.list", ListBox.class);
    debugFillListBox();
  }

  public void create(String command) {

    if (command.equalsIgnoreCase("new")) {
      screen.findElementByName("panel.character.create").show();
      screen.findElementByName("panel.character.center").show();
      loadCharacter("male");
    } else if (command.equalsIgnoreCase("ok")) {
      screen.findElementByName("panel.character.create").hide();
      screen.findElementByName("panel.character.center").hide();

      // TODO: Character speichern
    } else if (command.equalsIgnoreCase("cancel")) {
      screen.findElementByName("panel.character.create").hide();
      screen.findElementByName("panel.character.center").hide();
      app.getRootNode().detachChildNamed("character");
    }
  }

  public void delete(String command) {
    if (command.equalsIgnoreCase("ok")) {
      Character character2Delete = characterListBox.getFocusItem();
      characterListBox.removeItem(character2Delete);
      // TODO: und aus Datenbank entfernen
    }
    screen.findElementByName("panel.messagebox").hideWithoutEffect();
  }

  public void rotate() {
    app.getRootNode().getChild("character").rotate(0f, FastMath.PI / 8, 0f);
  }

  public void zoom() {
//    Quaternion rotate = new Quaternion();
//    rotate.fromAngleAxis(FastMath.PI / 90, new Vector3f(1, 0, 0));
//    cam.setRotation(rotate);
   
    float value = zoomed ? 200f : -200f;
    float h = cam.getFrustumTop();
    float w = cam.getFrustumRight();
    float aspect = w / h;

    float near = cam.getFrustumNear();

    float fovY = FastMath.atan(h / near) / (FastMath.DEG_TO_RAD * .5f);
    fovY += value * 0.1f;

    h = FastMath.tan(fovY * FastMath.DEG_TO_RAD * .5f) * near;
    w = h * aspect;

    cam.setFrustumTop(h);
    cam.setFrustumBottom(-h);
    cam.setFrustumLeft(-w);
    cam.setFrustumRight(w);
   
    Vector3f pos = cam.getLocation().clone();
    pos.multLocal(0f, zoomed ? 0.1f : -0.1f, 0f);
    cam.setLocation(pos);
    zoomed = !zoomed;
  }

  public void showElement(String element) {
    screen.findElementByName(element).showWithoutEffects();
  }

  @NiftyEventSubscriber(id = "RadioGroup-1")
  public void onRadioGroup1Changed(final String id, final RadioButtonGroupStateChangedEvent event) {
    app.getRootNode().detachChildNamed("character");
    loadCharacter(event.getSelectedId());
  }

  private void loadCharacter(String id) {
    Node characterNode = new Node("character");
    Spatial character = app.getAssetManager().loadModel("character/" + id + "/Mesh.mesh.xml");
    character.setLocalTranslation(0f, 0f, 5f);
    Material mat_character = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
    mat_character.setTexture("DiffuseMap", app.getAssetManager().loadTexture("character/male/male.png"));
    character.setMaterial(mat_character);
    Quaternion rotate = new Quaternion();
    rotate.fromAngleAxis(-FastMath.PI / 2, new Vector3f(1, 0, 0));
    character.setLocalRotation(rotate);
    characterNode.attachChild(character);
    app.getRootNode().attachChild(characterNode);

    System.out.println(cam.getDirection());
  }

  private void debugFillListBox() {
    Character testChar1 = new Character("MudCee1");
    testChar1.setSpecies("Bichromat1");
    testChar1.setCredits(10000);
    testChar1.setIcon(nifty.createImage("ui/icons/quest.png", true));

    Character testChar2 = new Character("MudCee2");
    testChar2.setSpecies("Bichromat2");
    testChar2.setCredits(20000);
    testChar2.setIcon(nifty.createImage("ui/icons/quest.png", true));

    Character testChar3 = new Character("MudCee3");
    testChar3.setSpecies("Bichromat3");
    testChar3.setCredits(30000);
    testChar3.setIcon(nifty.createImage("ui/icons/quest.png", true));

    characterListBox.addItem(testChar1);
    characterListBox.addItem(testChar2);
    characterListBox.addItem(testChar3);
  }

}
TOP

Related Classes of net.cis.client.login.CharacterViewUIController

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.