MouseInputActions.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package CameraAndMouse.MouseFiniteStateMachine;
import com.jme3.math.Vector3f;
import com.simsilica.lemur.input.AnalogFunctionListener;
import com.simsilica.lemur.input.FunctionId;
import com.simsilica.lemur.input.InputState;
import com.simsilica.lemur.input.StateFunctionListener;
import java.util.HashMap;
import java.util.List;
/**
*鼠标的状态
*
* @author icyboxs
*/
public class MouseInputActions implements StateFunctionListener, AnalogFunctionListener{
InputState inputState;
@Override
public void valueChanged(FunctionId func, InputState value, double tpf) {
if(func == MouseInputMap.Left_MOUSE_CLICK && value == InputState.Positive){
System.out.println(value+",Message printed to console. tpf was: " + tpf);
inputState=InputState.Positive;
}else{
System.out.println(value+",Message printed to console. tpf was: " + tpf);
inputState=value;
}
}
@Override
public void valueActive(FunctionId func, double value, double tpf) {
System.err.println(func+","+value);
}
}
MouseInputMap.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package CameraAndMouse.MouseFiniteStateMachine;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.simsilica.lemur.input.Axis;
import com.simsilica.lemur.input.Button;
import com.simsilica.lemur.input.FunctionId;
import com.simsilica.lemur.input.InputMapper;
/**
*
* @author icyboxs
*/
public class MouseInputMap {
public static final String MOUSE_GROUP = "MOUSE_GROUP";
public static final FunctionId Left_MOUSE_CLICK = new FunctionId(MOUSE_GROUP, "LeftMouseClick");
public static final FunctionId Right_MOUSE_CLICK = new FunctionId(MOUSE_GROUP, "Right_MOUSE_CLICK");
public static final FunctionId MOVE_MOUSE_Y = new FunctionId(MOUSE_GROUP, "MOVE_MOUSE_Y");
public static final FunctionId MOVE_MOUSE_X = new FunctionId(MOUSE_GROUP, "MOVE_MOUSE_X");
public static void initializeDefaultMappings( InputMapper inputMapper ) {
// Default key mappings
inputMapper.map(Left_MOUSE_CLICK, Button.MOUSE_BUTTON1);
inputMapper.map(Right_MOUSE_CLICK, Button.MOUSE_BUTTON2);
inputMapper.map(MOVE_MOUSE_Y, Axis.MOUSE_X);
inputMapper.map(MOVE_MOUSE_X, Axis.MOUSE_Y);
}
}
This is a piece of pseudo-code that indicates that I have initialized 2 ways of listening to the mouse at the same time.
protected void initialize(Application aplctn) {
CursorEventControl.addListenersToSpatial(simpleApp.getRootNode().getChild("MapNode"), new DefaultCursorListener() {
@Override
protected void click(CursorButtonEvent event, Spatial target, Spatial capture) {
// System.out.println("我被点击了:" + target);
// System.out.println("我被点击了:" + capture);
System.out.println("我被点击了:" + event.isPressed());
// System.out.println(results);
// System.out.println(scenarioState.mapGrid3D.getCellsizePosition(results.getContactPoint()));
}
@Override
public void cursorButtonEvent(CursorButtonEvent event, Spatial target, Spatial capture) {
event.setConsumed();
if (event.isPressed()) {
xDown = event.getX();
yDown = event.getY();
System.err.println(results.getContactPoint());
//BOX(scenarioState.mapGrid3D.getPutPlaceBuildingCoordinates(results.getContactPoint(),new Vector2f(3,3)));//向网格内添加建筑
// 鼠标按下,记录起始位置
startClick.set(inputManager.getCursorPosition());
isSelecting = true;
} else {
float x = event.getX();
float y = event.getY();
//它检查鼠标在释放按钮时是否移动得很小(小于3个像素)。如果是,它调用 click 方法
if (Math.abs(x - xDown) < 3 && Math.abs(y - yDown) < 3) {
click(event, target, capture);
}
isSelecting = false;
startClick.zero();
endClick.zero();
updateSelectionBox();
}
}
@Override
public void cursorEntered(CursorMotionEvent event, Spatial target, Spatial capture) {
// System.err.println(event);//.getCollision().getContactPoint()
}
@Override
public void cursorExited(CursorMotionEvent event, Spatial target, Spatial capture) {
}
@Override
public void cursorMoved(CursorMotionEvent event, Spatial target, Spatial capture) {
results = event.getCollision();
if (isSelecting) {
endClick.set(inputManager.getCursorPosition());
// 更新选择框大小和位置
updateSelectionBox();
}
}
});
GuiGlobals.initialize(simpleApp);
inputMapper = GuiGlobals.getInstance().getInputMapper();
// inputMapper.map(F_X_ROTATE_MOUSE, Axis.MOUSE_X);
MouseInputMap.initializeDefaultMappings(inputMapper);
MouseInputActions mouseInputActions = new MouseInputActions();
inputMapper.addStateListener(mouseInputActions,MouseInputMap.Left_MOUSE_CLICK);
inputMapper.addAnalogListener(mouseInputActions, MouseInputMap.Left_MOUSE_CLICK);
inputMapper.activateGroup(MouseInputMap.MOUSE_GROUP);
}
Something happens to inputMapper.addAnalogListener when both types of listeners are present at the same time.
This video shows how lifting the mouse when I press the left mouse button and dragging into the terrain inputMapper.addAnalogListener seems to conflict with CursorEventControl.addListenersToSpatial even though my mouse state is off inputMapper. addAnalogListener is still called.
I have to click the left mouse button again outside of the map (i.e., where CursorEventControl.addListenersToSpatial can’t detect it) in order to undo the call to inputMapper.addAnalogListener
Is this how lemurs were originally designed? Or am I using this class incorrectly?