Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.system.AWTFrameProcessor;
import com.jme3.system.AWTTaskExecutor;
import com.jme3.system.awt.AWTFrameProcessor;
import com.jme3.system.awt.AWTTaskExecutor;

/**
* An app state dedicated to the rendering of a JMonkey application within an AWT component.
Expand All @@ -53,6 +52,8 @@ public class AWTComponentAppState extends AbstractAppState {

private AWTFrameProcessor.TransferMode transferMode = AWTFrameProcessor.TransferMode.ON_CHANGES;

private boolean flipY = true;

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
Expand All @@ -61,6 +62,7 @@ public void initialize(AppStateManager stateManager, Application app) {
@Override
public void stateAttached(final AppStateManager stateManager) {
processor = new AWTFrameProcessor();
processor.setFlipY(flipY);
processor.setTransferMode(transferMode);

AWTTaskExecutor.getInstance().addToExecute(new Runnable() {
Expand Down Expand Up @@ -116,20 +118,44 @@ public void setComponent(Component component) {
}

/**
* Get the {@link com.jme3.system.AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @return the {@link com.jme3.system.AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @see #setTransferMode(com.jme3.system.AWTFrameProcessor.TransferMode)
* Get the {@link AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @return the {@link AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @see #setTransferMode(AWTFrameProcessor.TransferMode)
*/
public AWTFrameProcessor.TransferMode getTransferMode(){
return transferMode;
}

/**
* Set the {@link com.jme3.system.AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @param mode the {@link com.jme3.system.AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* Set the {@link AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @param mode the {@link AWTFrameProcessor.TransferMode transfer mode} that is used by the underlying frame processor.
* @see #getTransferMode()
*/
public void setTransferMode(AWTFrameProcessor.TransferMode mode) {
this.transferMode = mode;
}


/**
* Is the rendered image is Y flipped.
* @return <code>true</code> if the rendered image has to be Y flipped and <code>false</code> otherwise
* @see #setFlipY(boolean)
*/
public boolean isFlipY() {
return flipY;
}

/**
* Set if the rendered image has to be Y flipped.
* @param flip <code>true</code> if the rendered image has to be Y flipped and <code>false</code> otherwise
* @see #isFlipY()
*/
public void setFlipY(boolean flip) {

if (processor != null) {
processor.setFlipY(flip);
}

this.flipY = flip;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input;
package com.jme3.input.awt;

import java.awt.Component;
import java.util.Objects;

import com.jme3.app.Application;
import com.jme3.input.Input;
import com.jme3.input.RawInputListener;
import com.jme3.system.AWTContext;
import com.jme3.system.AWTTaskExecutor;
import com.jme3.system.JmeContext;
import com.jme3.system.awt.AWTTaskExecutor;

/**
* The implementation of the {@link Input} dedicated to AWT {@link Component component}.
Expand All @@ -55,7 +55,7 @@ public class AWTInput implements Input {
/**
* The context.
*/
protected final AWTContext context;
protected final JmeContext context;

/**
* The raw listener.
Expand All @@ -77,7 +77,11 @@ public class AWTInput implements Input {
*/
protected boolean initialized;

public AWTInput(final AWTContext context) {
public AWTInput() {
this.context = null;
}

public AWTInput(final JmeContext context) {
this.context = context;
}

Expand All @@ -102,7 +106,7 @@ protected void initializeImpl() {

@Override
public void update() {
if (!context.isRenderable()) return;
if ((context == null) || (!context.isRenderable())) return;
updateImpl();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input;
package com.jme3.input.awt;

import java.awt.Component;
import java.awt.event.KeyEvent;
Expand All @@ -40,7 +40,7 @@

import com.jme3.input.KeyInput;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.system.AWTContext;
import com.jme3.system.JmeContext;


/**
Expand All @@ -51,7 +51,7 @@
* @author Julien Seinturier - COMEX SA - <a href="http://www.seinturier.fr">http://www.seinturier.fr</a>
* @author Alexander Brui (JavaSaBr)
*/
public class AWTKeyInput extends AWTInput implements KeyInput, KeyListener{
public class AWTInputKeyboard extends AWTInput implements KeyInput, KeyListener{

private static final Map<Integer, Integer> KEY_CODE_TO_JME = new HashMap<>();

Expand Down Expand Up @@ -168,7 +168,12 @@ public class AWTKeyInput extends AWTInput implements KeyInput, KeyListener{

private final LinkedList<KeyInputEvent> keyInputEvents;

public AWTKeyInput(AWTContext context) {
public AWTInputKeyboard() {
super();
keyInputEvents = new LinkedList<KeyInputEvent>();
}

public AWTInputKeyboard(JmeContext context) {
super(context);
keyInputEvents = new LinkedList<KeyInputEvent>();
}
Expand Down Expand Up @@ -219,19 +224,16 @@ private int convertKeyCode(int keyCode) {

@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key typed "+e.getKeyChar());
//onKeyEvent(e, false);
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed "+e.getKeyChar());
onKeyEvent(e, true);
}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("Key released "+e.getKeyChar());
onKeyEvent(e, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.input;
package com.jme3.input.awt;

import java.awt.Component;
import java.awt.event.MouseEvent;
Expand All @@ -45,7 +45,8 @@
import com.jme3.input.MouseInput;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.system.AWTContext;
import com.jme3.system.JmeContext;
import com.jme3.system.awt.AWTContext;

/**
* The implementation of the {@link MouseInput} dedicated to AWT {@link Component component}.
Expand All @@ -55,20 +56,14 @@
* @author Julien Seinturier - COMEX SA - <a href="http://www.seinturier.fr">http://www.seinturier.fr</a>
* @author Alexander Brui (JavaSaBr)
*/
public class AWTMouseInput extends AWTInput implements MouseInput, MouseListener, MouseMotionListener, MouseWheelListener {

private static final Map<Integer, Integer> MOUSE_BUTTON_TO_JME = new HashMap<>();

static {
MOUSE_BUTTON_TO_JME.put(MouseEvent.BUTTON1, BUTTON_LEFT);
MOUSE_BUTTON_TO_JME.put(MouseEvent.BUTTON2, BUTTON_RIGHT);
MOUSE_BUTTON_TO_JME.put(MouseEvent.BUTTON3, BUTTON_MIDDLE);
}
public class AWTInputMouse extends AWTInput implements MouseInput, MouseListener, MouseMotionListener, MouseWheelListener {

private Map<Integer, Integer> buttonMapping;

/**
* The scale factor for scrolling.
*/
private static final int WHEEL_SCALE = 10;
private int wheelScale = 10;

private final LinkedList<MouseMotionEvent> mouseMotionEvents;

Expand All @@ -78,10 +73,46 @@ public class AWTMouseInput extends AWTInput implements MouseInput, MouseListener
private int mouseY;
private int mouseWheel;

public AWTMouseInput(AWTContext context) {
/**
* Create a new {@link MouseInput Mouse input} that is binded with AWT Mouse. By default, mouse button mapping is:
* <ul>
* <li>{@link MouseEvent.BUTTON1} is mapped with {@link BUTTON_LEFT}
* <li>{@link MouseEvent.BUTTON2} is mapped with {@link BUTTON_MIDDLE}
* <li>{@link MouseEvent.BUTTON3} is mapped with {@link BUTTON_RIGHT}
* </ul>
* The button mapping can be changed using {@link #setButtonMapping(Map) setButtonMapping() method}.
*/
public AWTInputMouse() {
super();
mouseMotionEvents = new LinkedList<MouseMotionEvent>();
mouseButtonEvents = new LinkedList<MouseButtonEvent>();

buttonMapping = new HashMap<>();
buttonMapping.put(MouseEvent.BUTTON1, BUTTON_LEFT);
buttonMapping.put(MouseEvent.BUTTON2, BUTTON_MIDDLE);
buttonMapping.put(MouseEvent.BUTTON3, BUTTON_RIGHT);
}

/**
* Create a new {@link MouseInput Mouse input} that is binded with AWT Mouse and the given context.
* By default, mouse button mapping is:
* <ul>
* <li>{@link MouseEvent.BUTTON1} is mapped with {@link BUTTON_LEFT}
* <li>{@link MouseEvent.BUTTON2} is mapped with {@link BUTTON_MIDDLE}
* <li>{@link MouseEvent.BUTTON3} is mapped with {@link BUTTON_RIGHT}
* </ul>
* The button mapping can be changed using {@link #setButtonMapping(Map) setButtonMapping() method}.
* @param context to context to use.
*/
public AWTInputMouse(JmeContext context) {
super(context);
mouseMotionEvents = new LinkedList<MouseMotionEvent>();
mouseButtonEvents = new LinkedList<MouseButtonEvent>();

buttonMapping = new HashMap<>();
buttonMapping.put(MouseEvent.BUTTON1, BUTTON_LEFT);
buttonMapping.put(MouseEvent.BUTTON2, BUTTON_MIDDLE);
buttonMapping.put(MouseEvent.BUTTON3, BUTTON_RIGHT);
}

@Override
Expand Down Expand Up @@ -112,6 +143,43 @@ protected void updateImpl() {
}
}

/**
* Get the scale to apply to Wheel motion.
* @return the scale to apply to Wheel motion.
* @see #getSetWheelScale(int)
*/
public int getWheelScale() {
return wheelScale;
}

/**
* Set the scale to apply to Wheel motion.
* @param scale the scale to apply to Wheel motion.
* @see #getWheelScale()
*/
public void getSetWheelScale(int scale) {
wheelScale = scale;
}

/**
* Get the button mapping between JME and AWT.
* Within the map, keys are the JME button and values are the AWT Mouse button that is affected.
* @return the button mapping between JME and AWT.
* @see #setButtonMapping(Map)
*/
public Map<Integer, Integer> getButtonMapping() {
return buttonMapping;
}

/**
* Set the button mapping between JME and AWT.
* @param mapping the button mapping between JME and AWT.
* @see #getButtonMapping()
*/
public void setButtonMapping(Map<Integer, Integer> mapping) {
this.buttonMapping = mapping;
}

private void onWheelScroll(final double xOffset, final double yOffset) {

mouseWheel += yOffset;
Expand All @@ -134,8 +202,14 @@ private void onCursorPos(double xpos, double ypos) {
int xDelta;
int yDelta;
int x = (int) Math.round(xpos);
int y = context.getHeight() - (int) Math.round(ypos);


int y = 0;
if ((context != null) && (context instanceof AWTContext)) {
y = ((AWTContext)context).getHeight() - (int) Math.round(ypos);
} else {
y = (int) Math.round(ypos);
}

if (mouseX == 0) mouseX = x;
if (mouseY == 0) mouseY = y;

Expand Down Expand Up @@ -176,7 +250,7 @@ public void run() {
}

private int convertButton(int i) {
final Integer result = MOUSE_BUTTON_TO_JME.get(i);
final Integer result = buttonMapping.get(i);
return result == null ? 0 : result;
}

Expand All @@ -195,8 +269,7 @@ public void setNativeCursor(JmeCursor cursor) {

@Override
public void mouseDragged(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub

onCursorPos(e.getX(), e.getY());
}

@Override
Expand Down Expand Up @@ -234,6 +307,6 @@ public void mouseExited(java.awt.event.MouseEvent e) {

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
onWheelScroll(e.getWheelRotation() * WHEEL_SCALE, e.getWheelRotation() * WHEEL_SCALE);
onWheelScroll(e.getWheelRotation() * wheelScale, e.getWheelRotation() * wheelScale);
}
}
Loading