Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jme3.app.Application;
- import com.jme3.app.state.AbstractAppState;
- import com.jme3.app.state.AppStateManager;
- import com.jme3.input.InputManager;
- import com.jme3.input.KeyInput;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.Camera;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Node;
- import com.jme3.system.AppSettings;
- import de.b00n.daltura.client.scene.Daltura3DApplication;
- import de.b00n.daltura.client.scene.DalturaFreeWorldScene;
- import de.b00n.daltura.client.scene.appstates.gui.HudState;
- import java.util.concurrent.Callable;
- /**
- *
- * @author Sascha
- */
- public class MiniMapState extends AbstractAppState implements ActionListener {
- public static final String MINI_MAP = "miniMap", MINI_MAP_MOVE = "miniMapMove";
- public static final float MINIMAP_VIEWPORT_LEFT = .8f;
- public static final float MINIMAP_VIEWPORT_RIGHT = 1f;
- public static final float MINIMAP_VIEWPORT_BOTTOM = 0.f;
- public static final float MINIMAP_VIEWPORT_TOP = .2f;
- /*
- * 1/(Sqrt(2))
- */
- public static final double InverseSqrtOf2 = 0.70710678118654752440084436210485;
- /*
- * Distance of the minimap-camera.
- */
- public static final float MINI_MAP_CAMERA_DISTANCE = 2250f;
- private Camera miniMapCam;
- private ViewPort miniMapViewPort;
- private Application app;
- private InputManager inputManager;
- private float miniMapLeftScreen;
- private float miniMapRightScreen;
- private float miniMapTopScreen;
- private float miniMapBottomScreen;
- private float miniMapHeightScreenSpaceCenter;
- private float miniMapWidthScreenSpaceCenter;
- private Vector3f center;
- private RTSCam rtsCam;
- private Quaternion qRotationMiniMap;
- private Vector3f miniMapPosition;
- private double angle = -1;
- private float negSinY, cosY;
- private int screenWidth;
- private int screenHeight;
- @Override
- public void onAction(String name, boolean isPressed, float tpf) {
- switch (name) {
- case MINI_MAP:
- if (isPressed) {
- boolean b = !miniMapViewPort.isEnabled();
- HudState state = app.getStateManager().getState(HudState.class);
- if (state != null) {
- state.setMiniMapPanelEnabled(b);
- }
- setEnabled(b);
- }
- break;
- case MINI_MAP_MOVE:
- if (isPressed) {
- Vector2f cursorPosition = inputManager.getCursorPosition();
- if (cursorPosition.x >= miniMapLeftScreen && cursorPosition.x <= miniMapRightScreen
- && cursorPosition.y >= miniMapBottomScreen && cursorPosition.y <= miniMapTopScreen) {
- float xScale = (cursorPosition.x - miniMapRightScreen) / (miniMapWidthScreenSpaceCenter) + 1f;
- float x = DalturaFreeWorldScene.TerrainPatchScaleTimesPatchSize * xScale;
- float zScale = (cursorPosition.y - miniMapTopScreen) / (miniMapHeightScreenSpaceCenter) + 1f;
- float z = DalturaFreeWorldScene.TerrainPatchScaleTimesPatchSize * zScale;
- center.subtractLocal(qRotationMiniMap.mult(new Vector3f(x, -z, 0)));
- }
- }
- break;
- }
- }
- @Override
- public void setEnabled(boolean enabled) {
- super.setEnabled(enabled);
- miniMapViewPort.setEnabled(enabled);
- }
- public void attachScene(final Node scene) {
- app.enqueue(new Callable<Void>() {
- @Override
- public Void call() throws Exception {
- miniMapViewPort.attachScene(scene);
- return null;
- }
- });
- }
- public void detachScene(final Node scene) {
- app.enqueue(new Callable<Void>() {
- @Override
- public Void call() throws Exception {
- miniMapViewPort.detachScene(scene);
- return null;
- }
- });
- }
- @Override
- public void initialize(AppStateManager stateManager, final Application app) {
- super.initialize(stateManager, app);
- AppSettings settings = ((Daltura3DApplication) app).getSettings();
- screenWidth = settings.getWidth();
- screenHeight = settings.getHeight();
- miniMapLeftScreen = MINIMAP_VIEWPORT_LEFT * screenWidth;
- miniMapRightScreen = MINIMAP_VIEWPORT_RIGHT * screenWidth;
- miniMapTopScreen = MINIMAP_VIEWPORT_TOP * screenHeight;
- miniMapBottomScreen = MINIMAP_VIEWPORT_BOTTOM * screenHeight;
- miniMapHeightScreenSpaceCenter = (miniMapTopScreen - miniMapBottomScreen) * 0.5f;
- miniMapWidthScreenSpaceCenter = (miniMapRightScreen - miniMapLeftScreen) * 0.5f;
- rtsCam = stateManager.getState(RTSCam.class);
- center = rtsCam.getCenter();
- inputManager = app.getInputManager();
- this.app = app;
- inputManager.addMapping(MINI_MAP, new KeyTrigger(KeyInput.KEY_K));
- inputManager.addMapping(MINI_MAP_MOVE, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- inputManager.addListener(this, MINI_MAP, MINI_MAP_MOVE);
- Camera cam = app.getCamera();
- miniMapCam = cam.clone();
- miniMapCam.setViewPort(MINIMAP_VIEWPORT_LEFT, MINIMAP_VIEWPORT_RIGHT, MINIMAP_VIEWPORT_BOTTOM, MINIMAP_VIEWPORT_TOP);
- miniMapCam.setParallelProjection(true);
- miniMapCam.setFrustumPerspective(90f, cam.getWidth() / cam.getHeight(), 0.01f, MINI_MAP_CAMERA_DISTANCE + DalturaFreeWorldScene.heightScale);
- miniMapViewPort = app.getRenderManager().createPostView(MINI_MAP, miniMapCam);
- miniMapViewPort.setClearFlags(true, true, true);
- miniMapViewPort.setBackgroundColor(Daltura3DApplication.VIEWPORT_BACKGROUND_COLOR);
- qRotationMiniMap = miniMapCam.getRotation();
- miniMapPosition = miniMapCam.getLocation();
- miniMapPosition.y = MINI_MAP_CAMERA_DISTANCE;
- miniMapPosition.x = center.x;
- miniMapPosition.z = center.z;
- }
- public ViewPort getMiniMapView() {
- return miniMapViewPort;
- }
- @Override
- public void update(float tpf) {
- double rot = rtsCam.getRot() * 0.5;
- /**
- * Only refresh if position or rotation changed.
- */
- if (angle != rot || miniMapPosition.x != center.x || miniMapPosition.z != center.z) {
- miniMapPosition.x = center.x;
- miniMapPosition.z = center.z;
- negSinY = -(float) (StrictMath.sin(angle) * InverseSqrtOf2); //Doing double because Fastmath.sin is doing also double...
- cosY = (float) (StrictMath.cos(angle) * InverseSqrtOf2);//Doing double because Fastmath.cos is doing also double...
- qRotationMiniMap.set(negSinY, cosY, -cosY, negSinY);
- miniMapCam.onFrameChange();
- angle = rot;
- }
- }
- @Override
- public void cleanup() {
- super.cleanup();
- app.getRenderManager().removePostView(MINI_MAP);
- miniMapViewPort = null;
- }
- public void setMiniMapViewPort(float mLeft, float mRight, float mBottom, float mTop) {
- if (miniMapCam != null) {
- miniMapLeftScreen = mLeft * screenWidth;
- miniMapRightScreen = mRight * screenWidth;
- miniMapTopScreen = mTop * screenHeight;
- miniMapBottomScreen = mBottom * screenHeight;
- miniMapHeightScreenSpaceCenter = (miniMapTopScreen - miniMapBottomScreen) * 0.5f;
- miniMapWidthScreenSpaceCenter = (miniMapRightScreen - miniMapLeftScreen) * 0.5f;
- miniMapCam.setViewPort(mLeft, mRight, mBottom, mTop);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement