Translate JMonkey Tutorial to JRuby
-
25-05-2021 - |
Question
I've got all the tutorials up to beginner #5 translated and working, but I don't know Java well enough to know how to port the lines:
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Pause") && !keyPressed) {
isRunning = !isRunning;
}
}
};
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
...
}
}
How might this work?
Solution 2
Ah, found the answer. Turns out, they were anonymous inner classes. In JRuby, you can just create a class that implements the Java interface like so:
class RBActionListener
# This is how you implement Java interfaces from JRuby
include com.jme3.input.controls.ActionListener
def initialize(&on_action)
@on_action = on_action
end
def onAction(*args)
@on_action.call(*args)
end
end
class HelloJME3
# blah blah blah code blah blah blah
def register_keys
# ...
ac = RBActionListener.new {|name, pressed, tpf @running = !@running if name == "Pause" and !pressed}
input_manager.add_listener(ac, "Pause")
end
end
OTHER TIPS
As described in Calling Java from JRuby, you can use closure conversion, where blocks can be used to define a Java interface behaviour. Something like the following should work:
l = lambda { |name, pressed, tpf| running = !running if name == 'Pause' && !pressed }
input_managers.add_listener(l, ['Left', 'Right', 'Rotate'])
I wrapped the action listener in a method that would return an object that includes ActionListener using JRuby's :impl method
def isRunningActionListener
return ActionListener.impl do
|command, name, keyPressed, tpf|
case command
when :onAction
if name.eql?("Pause") && !keyPressed
isRunning = !isRunning;
end
end
end
end
you could also create your own ActionListener class that includes ActionListener...
class YourActionListener
include ActionListener
def onAction command, name, keyPressed, tpf
#your code here
end
end
creating your own class might be the better option as its a lot less verbose and easier to read and understand.