How to catch a ONE click of the button in libgdx? - button

The button counts down clicks several times, but i need only one click. How can i catch only ONE click.
I was trying InputListener, EventListener, ChangeListener and ClickListener. Or is that not the problem?
Please, help me.
Look.addCaptureListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
touched=true;
}
});
if(!Gdx.input.isTouched()) {
OpenActions.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("X:" + x + " Y:" + y);
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touchup");
}
});
}
As you see - i tried to catch only one click without the possibility to catch another one. But i failed.

I was added a listener to the render()(the cyclical method)... It was a mistake. I moved a listener to create(), then my problem was gone.

Related

Wait until any button is pressed?

I am writing a TicTacToe game in JavaFX. I've decided to make a board as 9 (3x3) buttons with changing text: "" (if empty) or "X" or "O". Everything is going ok beside one thing... I got stuck here:
public void game() {
while(keepPlaying) {
if(computerTurn) {;
computerMove();
}else {
while(waitForUser) {
//wait until any of 9 buttons is pushed!
}
}
if (checkResult()) {
keepPlaying = false;
}
computerTurn = !computerTurn;
}
}
How to wait for user pushing any of those 9 buttons and then continue with computer turn??
I need something like waiting for scanner input in console application, but this input must be one of 9 buttons...
I know that there are few "possible duplicates", but in fact those problems were solved using methods I can't use here, for example timer. Correct me if I am wrong.
Blocking the application thread in JavaFX should not be done since it freezes the UI. For this reason a loop like this is not well suited for a JavaFX application. Instead you should react to user input:
public void game() {
if (keepPlaying && computerTurn) {
computerMove();
if (checkResult()) {
keepPlaying = false;
}
computerTurn = false;
}
}
// button event handler
private void button(ActionEvent event) {
if (keepPlaying) {
Button source = (Button) event.getSource();
// probably the following 2 coordinates are computed from GridPane indices
int x = getX(source);
int y = getY(source);
// TODO: update state according to button pressed
if (checkResult()) {
keepPlaying = false;
} else {
computerMove();
if (checkResult()) {
keepPlaying = false;
}
}
}
}
Starting with javafx 9 there is a public API for "pausing" on the application thread however:
private static class GridCoordinates {
int x,y;
GridCoordinates (int x, int y) {
this.x = x;
this.y = y;
}
}
private final Object loopKey = new Object();
public void game() {
while(keepPlaying) {
if(computerTurn) {
computerMove();
} else {
// wait for call of Platform.exitNestedEventLoop​(loopKey, *)
GridCoordinates coord = (GridCoordinates) Platform.enterNestedEventLoop​(loopKey);
// TODO: update state
}
if (checkResult()) {
keepPlaying = false;
}
computerTurn = !computerTurn;
}
}
private void button(ActionEvent event) {
if (keepPlaying) {
Button source = (Button) event.getSource();
// probably the following 2 coordinates are computed from GridPane indices
int x = getX(source);
int y = getY(source);
Platform.exitNestedEventLoop​(loopKey, new GridCoordinates(x, y));
}
}

JavaFX Spinner change is slow with click and hold of mouse button

The speed of Spinner update is slow when I click and hold the up/down arrow buttons. Is there a way to increase the change speed?
When I click, click, click with the mouse, the spinner values change as fast as I click. It also changes fast if I use the up/down arrows on the keyboard for each key press or if I hold down the up/down arrow keys. I want the values to change that fast when I click and hold on the arrow buttons.
Anyone know a way to do that?
The SpinnerBehavior of the SpinnerSkin triggers updates every 750 ms. Unfortunately there is no way to simply set/modify this behavour without using reflection to access private members. Therefore the only way to do this without reflection is using event filters to trigger the updates at a faster rate:
private static final PseudoClass PRESSED = PseudoClass.getPseudoClass("pressed");
#Override
public void start(Stage primaryStage) {
Spinner<Integer> spinner = new Spinner(Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
class IncrementHandler implements EventHandler<MouseEvent> {
private Spinner spinner;
private boolean increment;
private long startTimestamp;
private static final long DELAY = 1000l * 1000L * 750L; // 0.75 sec
private Node button;
private final AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
if (now - startTimestamp >= DELAY) {
// trigger updates every frame once the initial delay is over
if (increment) {
spinner.increment();
} else {
spinner.decrement();
}
}
}
};
#Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
Spinner source = (Spinner) event.getSource();
Node node = event.getPickResult().getIntersectedNode();
Boolean increment = null;
// find which kind of button was pressed and if one was pressed
while (increment == null && node != source) {
if (node.getStyleClass().contains("increment-arrow-button")) {
increment = Boolean.TRUE;
} else if (node.getStyleClass().contains("decrement-arrow-button")) {
increment = Boolean.FALSE;
} else {
node = node.getParent();
}
}
if (increment != null) {
event.consume();
source.requestFocus();
spinner = source;
this.increment = increment;
// timestamp to calculate the delay
startTimestamp = System.nanoTime();
button = node;
// update for css styling
node.pseudoClassStateChanged(PRESSED, true);
// first value update
timer.handle(startTimestamp + DELAY);
// trigger timer for more updates later
timer.start();
}
}
}
public void stop() {
timer.stop();
button.pseudoClassStateChanged(PRESSED, false);
button = null;
spinner = null;
}
}
IncrementHandler handler = new IncrementHandler();
spinner.addEventFilter(MouseEvent.MOUSE_PRESSED, handler);
spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> {
if (evt.getButton() == MouseButton.PRIMARY) {
handler.stop();
}
});
Scene scene = new Scene(spinner);
primaryStage.setScene(scene);
primaryStage.show();
}
I modified the answer of fabian a little bit to decrease the speed of the spinner while holding mouse down:
private int currentFrame = 0;
private int previousFrame = 0;
#Override
public void handle(long now)
{
if (now - startTimestamp >= initialDelay)
{
// Single or holded mouse click
if (currentFrame == previousFrame || currentFrame % 10 == 0)
{
if (increment)
{
spinner.increment();
}
else
{
spinner.decrement();
}
}
}
++currentFrame;
}
And after stopping the timer we adjust previousFrame again:
public void stop()
{
previousFrame = currentFrame;
[...]
}
A small improvement to Fabian's answer. Making the following mod to the MOUSE_RELEASED addEventerFilter will stop a NullPointerException caused when clicking the textfield associated with the spinner. Cheers Fabian!
spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> {
Node node = evt.getPickResult().getIntersectedNode();
if (node.getStyleClass().contains("increment-arrow-button") ||
node.getStyleClass().contains("decrement-arrow-button")) {
if (evt.getButton() == MouseButton.PRIMARY) {
handler.stop();
}
}
});
An alternative to changing the update speed might in some cases be adjusting the amount by which the value increments/decrements per update.
SpinnerValueFactory.IntegerSpinnerValueFactory intFactory =
(SpinnerValueFactory.IntegerSpinnerValueFactory) spinner.getValueFactory();
intFactory.setAmountToStepBy(100);
Reference: http://news.kynosarges.org/2016/10/28/javafx-spinner-for-numbers/

How to use touchup and touchDown with buttons

I have 3 buttons and i am trying to do some stuff on touchup and touchdown,i read many stuff in stackoverflow but its confusing i tried
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(button1.getX()==screenX&&button1.getY()==screenY) {
Gdx.app.log("button", "touchdown");
}
return true;
}
i also tried hit condition too and don't know right way to use it.plz help me with any suggestion
I don't understand your question. You can add a listener to your button which can catch the touchdown / touchup / checked state of a button:
Button playButton = new Button();
playButton.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
Have a look at the InputListener API, to find an explanation what these states are. For example the touchdown method:
"Called when a mouse button or a finger touch goes down on the actor. If true is returned, this listener will receive all touchDragged and touchUp events, even those not over this actor, until touchUp is received. Also when true is returned, the event is handled}."
You can also set different Button images for these states: Link
Create one InputListener and attach it to each of the three buttons. When you receive for example a touchDown, you can use the event parameter to get the listenerActor and check which button was used:
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (event.getListenerActor().equals(playButton)) {
}
}
Haven't tested it, but it should work:
public class Example {
private Button button1;
private Button button2;
private Button button3;
public Example() {
MyListener listener = new MyListener();
// create the buttons...
button1.addListener(listener);
button2.addListener(listener);
button3.addListener(listener);
}
private class MyListener extends InputListener {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if (event.getListenerActor().equals(button1)) {
Gdx.app.log("Example", "button 1 touchdown");
} else if (event.getListenerActor().equals(button2)) {
Gdx.app.log("Example", "button 2 touchdown");
} else if (event.getListenerActor().equals(button3)) {
Gdx.app.log("Example", "button 3 touchdown");
}
return super.touchDown(event, x, y, pointer, button);
}
}
}
if your using input processor , you have to set the input processor to the class . this can be done by the follwing code.
//in your create method
Gdx.input.setInputProcessor(this);

JavaFX show ContextMenu below cursor

I have a TextField where the user can type. I would like to show a ContextMenu below cursor when the user hit Ctrl+Space key combination.
codeArea.setOnKeyPressed(event -> {
if( event.getCode().equals( KeyCode.SPACE ) && event.isControlDown() ) {
int cursorX = ?;
int cursorY = ?;
cm.show(codeArea, x, y);
} else {
cm.hide();
}
});
How I get the cursor current position? I must give it's (screen) XY coords to the show() function.
I'd like use it for auto completion.
Thanks.
Add a MouseListener and save the last position in a Variable :)
You might wanna use getScreenX() and getScreenY() not sure of the difference at the moment.
codeArea.setOnMouseMoved(new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
this.cursorX = event.getX();
this.cursorY = event.getY();
}
});

processing: button press switch state

So I have a ON-OFF button that draws a circle. The trouble I am encountering is that the ON OFF states are random depending on how long I press the button. I guess this is due to the draw() function which also loops my button function in time with framerate. What I want is for the button to turn on when pressed once and turn off when pressed again irrespective of how long the button is pressed. Here is the code.
else if (circle4.pressed()) {
println("button 4 is pressed");
if(drawCirclesPrimary){
drawCirclesPrimary = false;
}
else{
drawCirclesPrimary = true;
}
println("drawCirclesPrimary"+drawCirclesPrimary);
}
I would suggest looking at the Buttons tutorial on processing.org. The following code is a subset of what is contained in the tutorial (you will need to review all the code in the tutorial, however). Comments are mine.
void setup() {
// Create instances of your button(s)
}
void draw() {
// Draw buttons, update cursor position, check if buttons have been clicked.
}
// Provides the overRect() method (among others).
class Button
{
// If the cursor is placed within the footprint of the button, return true.
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
}
class RectButton extends Button
{
// Create a rectangle button with these size/color attributes.
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
// Determines whether the cursor is over the button.
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
// Draws the rectangle button into your sketch.
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
This thread has some example code for drawing an object only while a key is pressed. That's pretty similar to what you want.
Instead of keyPressed and keyReleased, you can use mouseClicked. which is called once after a mouse button has been pressed and then released. Use a boolean variable to store the on/off state. Inside of mouseClicked, toggle the value of that boolean variable.

Resources