Search in sources :

Example 1 with AbstractControl

use of com.jme3.scene.control.AbstractControl in project jmonkeyengine by jMonkeyEngine.

the class TestDepthStencil method simpleInitApp.

@Override
public void simpleInitApp() {
    int w = settings.getWidth();
    int h = settings.getHeight();
    //setup framebuffer
    fb = new FrameBuffer(w, h, 1);
    Texture2D fbTex = new Texture2D(w, h, Format.RGB8);
    fb.setDepthBuffer(Format.Depth24Stencil8);
    fb.setColorTexture(fbTex);
    // setup framebuffer's scene
    Sphere sphMesh = new Sphere(20, 20, 1);
    Material solidColor = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
    final Geometry sphere = new Geometry("sphere", sphMesh);
    sphere.setMaterial(solidColor);
    fbNode.attachChild(sphere);
    sphere.addControl(new AbstractControl() {

        @Override
        protected void controlUpdate(float tpf) {
            Material mat = sphere.getMaterial();
            mat.getAdditionalRenderState().setStencil(enableStencil, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.StencilOperation.Keep, RenderState.TestFunction.Never, RenderState.TestFunction.Never);
        }

        @Override
        protected void controlRender(RenderManager rm, ViewPort vp) {
        }
    });
    //setup main scene
    Picture p = new Picture("Picture");
    p.setPosition(0, 0);
    p.setWidth(w);
    p.setHeight(h);
    p.setTexture(assetManager, fbTex, false);
    rootNode.attachChild(p);
    inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
    ActionListener acl = new ActionListener() {

        public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("toggle") && keyPressed) {
                if (enableStencil) {
                    enableStencil = false;
                    System.out.println("Stencil Enabled (model should be hidden)");
                } else {
                    enableStencil = true;
                    System.out.println("Stencil Disabled (model should be visible)");
                }
            }
        }
    };
    inputManager.addListener(acl, "toggle");
    System.out.println("Press space to toggle stencil");
}
Also used : Texture2D(com.jme3.texture.Texture2D) KeyTrigger(com.jme3.input.controls.KeyTrigger) AbstractControl(com.jme3.scene.control.AbstractControl) Material(com.jme3.material.Material) FrameBuffer(com.jme3.texture.FrameBuffer) Sphere(com.jme3.scene.shape.Sphere) Geometry(com.jme3.scene.Geometry) ActionListener(com.jme3.input.controls.ActionListener) Picture(com.jme3.ui.Picture) ViewPort(com.jme3.renderer.ViewPort) RenderManager(com.jme3.renderer.RenderManager)

Example 2 with AbstractControl

use of com.jme3.scene.control.AbstractControl in project TeachingInSimulation by ScOrPiOzzy.

the class MHZ2_20DControlEx method controlUpdate.

@Override
protected void controlUpdate(float tpf) {
    Vector3f origin = start.getWorldTranslation();
    Vector3f goal = end.getWorldTranslation();
    Ray ray = new Ray(origin, goal.add(origin.negate()));
    CollisionResults results = new CollisionResults();
    rootNode.collideWith(ray, results);
    for (CollisionResult result : results) {
        Geometry geometry = result.getGeometry();
        if ("workpiece".equals(geometry.getUserData("type"))) {
            // 获得工件模型
            Spatial workpiece = geometry.getParent().getParent();
            if ("0".equals(geometry.getParent().getUserData("combined"))) {
                Spatial combined = workpiece.getParent();
                if (combined.getNumControls() != 0) {
                    combined.setLocalTranslation(Vector3f.ZERO);
                    ItemControl control = (ItemControl) combined.getControl(0);
                    control.setEnabled(false);
                    pieceLoc.attachChild(combined);
                }
            }
            break;
        } else if ("material".equals(geometry.getUserData("type"))) {
            // 获得工件模型
            Spatial material = geometry.getParent().getParent();
            if ("1".equals(geometry.getParent().getUserData("combined"))) {
                Spatial combined = material.getParent();
                if (combined.getNumControls() != 0) {
                    combined.setLocalTranslation(Vector3f.ZERO);
                    ItemControl control = (ItemControl) combined.getControl(0);
                    control.setEnabled(false);
                    pieceLoc.attachChild(combined);
                }
            }
            break;
        }
    }
    if (!clamp) {
        return;
    }
    float per = tpf * speed;
    if (b) {
        left.move(per, 0, 0);
        right.move(-per, 0, 0);
        total += per;
        if (total >= length) {
            left.move(length - total, 0, 0);
            right.move(total - length, 0, 0);
            total = length;
            clamp = false;
            magnet.setLocalTranslation(0, -0.015f, 0);
        }
    } else {
        left.move(-per, 0, 0);
        right.move(per, 0, 0);
        total -= per;
        if (total <= 0) {
            left.move(-total, 0, 0);
            right.move(total, 0, 0);
            total = 0;
            magnet.setLocalTranslation(0, 0.015f, 0);
            clamp = false;
            if (pieceLoc.getChildren().size() == 0) {
                return;
            }
            final Spatial combined = pieceLoc.getChild(0);
            if (combined != null && combined.getNumControls() != 0) {
                combined.setLocalTranslation(combined.getWorldTranslation());
                Dispatcher.getIns().getMainApp().enqueue(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            rootNode.attachChild(combined);
                            AbstractControl control = (AbstractControl) combined.getControl(0);
                            control.setEnabled(true);
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                        }
                    }
                });
            }
        }
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Geometry(com.jme3.scene.Geometry) CollisionResults(com.jme3.collision.CollisionResults) Spatial(com.jme3.scene.Spatial) Vector3f(com.jme3.math.Vector3f) AbstractControl(com.jme3.scene.control.AbstractControl) Ray(com.jme3.math.Ray)

Example 3 with AbstractControl

use of com.jme3.scene.control.AbstractControl in project TeachingInSimulation by ScOrPiOzzy.

the class LinkTargetHandler method select.

public void select() {
    MultimeterState_MF47 multimeterState = stateManager.getState(MultimeterState_MF47.class);
    if (multimeterState.isEnabled() && target instanceof Terminal) {
        multimeterState.putPenToTerminal((Terminal) target, target.getModel());
        return;
    }
    if (!handler.checkConnect(target)) {
        return;
    }
    LinkerMsg reqMsg = handler.offlineConnect(target);
    // 如果当前为多人联机模式则向服务器发送连线请求
    if (!handler.isAlone()) {
        reqMsg.setType(LinkerMsg.ADD_LINKER);
        ClientMgr.send(reqMsg);
    }
    if (!(target instanceof Terminal)) {
        return;
    }
    // 螺丝拧上拧下
    Node parent = (Node) target.getModel();
    // findCylinder(parent);
    final Spatial luosi = parent.getChild("screw");
    if (luosi == null) {
        throw new RuntimeException("螺丝的模型结构有问题, 没有指出螺杆节点");
    }
    if (luosi.getNumControls() > 0) {
        AbstractControl control = (AbstractControl) luosi.getControl(0);
        control.setEnabled(true);
    }
}
Also used : Spatial(com.jme3.scene.Spatial) MultimeterState_MF47(com.cas.circuit.state.MultimeterState_MF47) Node(com.jme3.scene.Node) AbstractControl(com.jme3.scene.control.AbstractControl) Terminal(com.cas.circuit.vo.Terminal) LinkerMsg(com.cas.circuit.msg.LinkerMsg)

Example 4 with AbstractControl

use of com.jme3.scene.control.AbstractControl in project TeachingInSimulation by ScOrPiOzzy.

the class SensorLogic method cloneForSpatial.

/**
 * Default implementation of cloneForSpatial() that simply clones the control and sets the spatial.
 *
 * <pre>
 * AbstractControl c = clone();
 * c.spatial = null;
 * c.setSpatial(spatial);
 * </pre>
 *
 * Controls that wish to be persisted must be Cloneable.
 */
@Override
public Control cloneForSpatial(Spatial spatial) {
    try {
        AbstractControl c = (AbstractControl) clone();
        // c.spatial = null; // to keep setSpatial() from throwing an exception
        c.setSpatial(spatial);
        return c;
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException("Can't clone control for spatial", e);
    }
}
Also used : AbstractControl(com.jme3.scene.control.AbstractControl)

Aggregations

AbstractControl (com.jme3.scene.control.AbstractControl)4 Geometry (com.jme3.scene.Geometry)2 Spatial (com.jme3.scene.Spatial)2 LinkerMsg (com.cas.circuit.msg.LinkerMsg)1 MultimeterState_MF47 (com.cas.circuit.state.MultimeterState_MF47)1 Terminal (com.cas.circuit.vo.Terminal)1 CollisionResult (com.jme3.collision.CollisionResult)1 CollisionResults (com.jme3.collision.CollisionResults)1 ActionListener (com.jme3.input.controls.ActionListener)1 KeyTrigger (com.jme3.input.controls.KeyTrigger)1 Material (com.jme3.material.Material)1 Ray (com.jme3.math.Ray)1 Vector3f (com.jme3.math.Vector3f)1 RenderManager (com.jme3.renderer.RenderManager)1 ViewPort (com.jme3.renderer.ViewPort)1 Node (com.jme3.scene.Node)1 Sphere (com.jme3.scene.shape.Sphere)1 FrameBuffer (com.jme3.texture.FrameBuffer)1 Texture2D (com.jme3.texture.Texture2D)1 Picture (com.jme3.ui.Picture)1