Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Button Functionality (over, pressed and down)  (Read 1140 times)
0 Members and 1 Guest are viewing this topic.
Offline nellykvist

Senior Newbie





« Posted 2013-10-26 11:47:48 »

I'm creating a simple button class, with Slick2D library.
However, I have a problem, on how to create a buttonReleased function.
So problem is, I click the button, and it already executes, with out releasing a mouse click.
What I want to achieve, is, I click the button, but execute only if a mouse click is released inside a button area.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
    public boolean isActionPerformed(Input input, int mx, int my) throws SlickException {
        if (buttonContains(mx, my)) {
            if (buttonPressed(input)) {
                return true;
            }
            if (buttonDown(input)) {
            }
        }
        return false;
    }
   
    public boolean buttonContains(int mx, int my) throws SlickException {
        if ((mx > minX && mx < maxX) && (my > minY && my < maxY)) {
            buttonState = ButtonState.Over;
            return true;
        }
        buttonState = ButtonState.Normal;
        return false;
    }
   
    public boolean buttonPressed(Input input) throws SlickException {
        if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
            return true;
        }
        return false;
    }

    public boolean buttonDown(Input input) throws SlickException {
        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            buttonState = ButtonState.Down;
            return true;
        }
        return false;
    }
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2013-10-26 12:34:01 »

And? Whats the problem? No one is going to write your code for you. And if they do, thats not right because you aren't learning.

Offline nellykvist

Senior Newbie





« Reply #2 - Posted 2013-10-26 12:34:39 »

I don't need code, I need help, on how to create it.. like pseudo code or explain how it works?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-10-26 12:41:24 »

Well then, think about it logically. You have all the methods there to make it happen. You need to check if the mouse is pressed. Then check if the mouse press is within the bounds of a button. The, check if the mouse is still pressed, and when the user releases the mouse, perform your code.

Offline nellykvist

Senior Newbie





« Reply #4 - Posted 2013-10-26 13:08:49 »

Well, there is no isMouseReleased method inside Input class...
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-10-26 13:12:23 »

And? Seriously, just think about it. I know exactly how to do it with just those methods. If the mouse isn't pressed, its released. How can you check if the mouse is not pressed using your code? You have a boolean method called buttonDown. How can you use that to check if the button is up?

Offline nellykvist

Senior Newbie





« Reply #6 - Posted 2013-10-26 13:19:59 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
    boolean buttonDown;
    public boolean isActionPerformed(Input input, int mx, int my) throws SlickException {
        if (buttonContains(mx, my)) {
            if (buttonPressed(input)) {
                buttonDown = true;
            }
            if (buttonDown(input)) {
                buttonDown = false;
            }
        }
        return false;
    }


And.. ?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-10-26 13:22:47 »

No. Check if !buttonDown. That's how you know its released. You're just checking to see if the button was pressed, which doesn't allow you to check if it was down, and you're also just checking if its down.
1  
2  
3  
4  
5  
6  
7  
8  
9  
if(buttonContains(mx, my){
if(buttonDown(input){
if(!buttonDown(input){
//perform logic
buttonDown = false;
}
buttonDown = true;
}
}

Offline nellykvist

Senior Newbie





« Reply #8 - Posted 2013-10-26 13:32:37 »

Alright, but this is executing multiple times when mouse is down, what about mouse pressed?
I can't figure out where to place, since both pressed & down methods have the same action... In which case, pressed does only 1 click, and down does like 5-6 clicks??  That's what confuses me..
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #9 - Posted 2013-10-26 13:37:37 »

Right, that's always been a pain in the ass. You'll need to do something like this:
1  
2  
3  
4  
5  
public boolean keyDown(int keycode) {
      if ((keycode == Keys.E) && Level.editMode == false) Level.editMode = true;
      else if((keycode == Keys.E) && Level.editMode) Level.editMode = false;
      return false;
   }

(This is taken from one of my other games)
What I'm basically showing you here is a skeleton of how to handle keys that are pressed. I check if the button is down, and then set a boolean to true. Then, I check if the key is down again, and if the boolean is true. If so, perform the logic. This should help you get started, just use your buttonDown boolean.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #10 - Posted 2013-10-26 13:38:56 »

Right, that's always been a pain in the ass. You'll need to do something like this:
1  
2  
3  
4  
5  
public boolean keyDown(int keycode) {
      if ((keycode == Keys.E) && Level.editMode == false) Level.editMode = true;
      else if((keycode == Keys.E) && Level.editMode) Level.editMode = false;
      return false;
   }

(This is taken from one of my other games)
What I'm basically showing you here is a skeleton of how to handle keys that are pressed. I check if the button is down, and then set a boolean to true. Then, I check if the key is down again, and if the boolean is true. If so, perform the logic. This should help you get started, just use your buttonDown boolean.

Why dont you just make that:

1  
if (keycode == Keys.E) Level.editMode = !Level.editMode;


?

Offline Dxu1994
« Reply #11 - Posted 2013-10-26 13:40:59 »

@OP Try this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
boolean buttonDown;
    public boolean isActionPerformed(Input input, int mx, int my) throws SlickException {
        if (buttonContains(mx, my)) {
            if (buttonPressed(input)) {
                buttonDown = true;
            }
            else if (buttonReleased(input) && buttonDown) {
                 buttonDown = false;
                 runActionOrDoWhateverHere();
            }
        }
        return false;
    }

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #12 - Posted 2013-10-26 13:41:33 »

Well, you need to check if the editMode is true or false. The only reason the logic isn't fired off every frame when I'm holding down the edit key is because I check for the boolean. Without it, it would continually go off.

Offline Dxu1994
« Reply #13 - Posted 2013-10-26 13:43:28 »

Well, you need to check if the editMode is true or false. The only reason the logic isn't fired off every frame when I'm holding down the edit key is because I check for the boolean. Without it, it would continually go off.

I dont understand? my modification does the same thing yours does, it just uses a boolean negation to toggle instead, save a cycle of cmpne Smiley

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #14 - Posted 2013-10-26 13:43:42 »

@Dxu1994 He doesn't have a keyReleased method. Change that to !buttonDown. Besides, that makes no sense, no offense. If the key is released, and the key is down, well that's contradictory.
Edit: To your last post, no that's not true. You are only checking if the key is down, which can be true or false multiple times a frame. Then you set the boolean to be the opposite, which is basically going to spam toggle it multiple times a frame.

Offline nellykvist

Senior Newbie





« Reply #15 - Posted 2013-10-26 13:43:51 »

This is a function that I created few days ago, what it does is, if is pressed once, execute.. but after that press, if a button is not released, and is last for 1s, then execute down method, which spams.. Tbh, I don't like way of doing this.. So that's what I want now to short code into actionPerform

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
    public String getButtonExecutionInterval(GameContainer container, Input input, int delta, int mx, int my) throws SlickException {
        if (this.buttonContains(mx, my)) {
            if (this.buttonPressed(input)) {
                this.invoke = false;
                this.lastInvoke = container.getTime();

                return ONCE;
            }
            if (this.pressedTime < this.timeDuration && !this.buttonDown(input)) {
                this.pressedTime = this.timeDuration;
            }
            if (this.lastInvoke + 1000 < container.getTime()) {
                this.invoke = true;
            }
            if (this.buttonDown(input) && this.invoke) {
                this.pressedTime -= delta;
                if (this.pressedTime <= 0 && this.buttonDown(input)) {
                    this.pressedTime = this.timeDuration;

                    return REPEATEDLY;
                }
            }
        }
        return NEVER;
    }
Offline Dxu1994
« Reply #16 - Posted 2013-10-26 13:45:18 »

@Dxu1994 He doesn't have a keyReleased method. Change that to !buttonDown. Besides, that makes no sense, no offense. If the key is released, and the key is down, well that's contradictory.

It does make sense.

1. User clicks in button, buttonDown is set to true.
2. User releases mouse inside button rect, check if buttonDown is true, if so set it to false and fire event, if not, do nothing.

This prevents the user from clicking outside the button, dragging the mouse over the button and releasing to fire an event.

Offline nellykvist

Senior Newbie





« Reply #17 - Posted 2013-10-26 13:46:32 »

@OP Try this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
boolean buttonDown;
    public boolean isActionPerformed(Input input, int mx, int my) throws SlickException {
        if (buttonContains(mx, my)) {
            if (buttonPressed(input)) {
                buttonDown = true;
            }
            else if (buttonReleased(input) && buttonDown) {
                 buttonDown = false;
                 runActionOrDoWhateverHere();
            }
        }
        return false;
    }



I don't have buttonReleased method, what do you think it should contain ?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #18 - Posted 2013-10-26 13:47:10 »

Right, I understand that. But considering the OP doesn't have a released method, how would he check that? You can't call the same method twice in one if statement and hope for different results!
Sorry OP I like being competitive Smiley

Offline Dxu1994
« Reply #19 - Posted 2013-10-26 13:48:18 »

Im not sure how Slick works, but that's what I'd do if I had to implement a button. You need the released method, look up Slick docs or something.
The idea behind every button is the same.

Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #20 - Posted 2013-10-26 13:49:23 »

Slick doesn't have a native released method, he'll need to create it. I believe there are some topics about it on these forums that helped me.

Offline Dxu1994
« Reply #21 - Posted 2013-10-26 13:50:47 »

Here's a UIButton class from my game engine, it may help you:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
/**
 *
 * @author David
 */

public abstract class UIButton
        extends BaseWidget {

    protected ButtonState state = ButtonState.NORMAL;
    protected String text;

    protected UIButton() {
    }

    protected boolean isRolledOver() {
        return manager.rolledoverWidget() == this;
    }

    protected void checkState() {
        if (manager.rolledoverWidget() != this || manager.rolledoverManager() != manager) {
            state = ButtonState.NORMAL;
        }
    }
    protected boolean mouseInside = false;

    public void widgetUpdate(long elapsedMillis) {
    }

    public boolean onPointerPressed(PointerEvent evt) {
        if (inside(evt.getX(), evt.getY()) && evt.isLeftButton()) {
            mouseInside = true;
            state = ButtonState.PRESSED;
            manager.focusWidget(UIButton.this);
            return true;
        }
        manager.focusWidget(null);
        return false;
    }

    public boolean onPointerReleased(PointerEvent evt) {
        boolean wasMouseInside = mouseInside;
        mouseInside = false;
        if (inside(evt.getX(), evt.getY()) && wasMouseInside && evt.isLeftButton()) {
            state = ButtonState.ROLLOVER;
            manager.focusWidget(UIButton.this);
            dispatchAction();
            return true;
        }
        manager.focusWidget(null);
        return false;
    }

    public boolean onPointerMoved(PointerEvent evt) {
        if (inside(evt.getX(), evt.getY())) {
            manager.rolloverWidget(UIButton.this);
            state = ButtonState.ROLLOVER;
            return true;
        }
        state = ButtonState.NORMAL;
        return false;
    }

    public boolean onPointerDragged(PointerEvent evt) {
        if (inside(evt.getX(), evt.getY()) && mouseInside) {
            state = ButtonState.PRESSED;
            return true;
        }
        state = ButtonState.NORMAL;
        return false;
    }

    protected boolean inside(int x, int y) {
        return MathTool.inRect(x, y, getX(), getY(), width, height);
    }

    protected void dispatchAction() {
        if (listener != null) {
            listener.handleAction(null);
        }
    }

    @Override
    public void onKeyReleased(KeyEvent evt) {
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            dispatchAction();
        }
    }
}


Uses a different event dispatch system to Slick but the idea behind it should always be the same.

Offline nellykvist

Senior Newbie





« Reply #22 - Posted 2013-10-26 13:55:50 »

Alright, thank you guys for your time. I will see what I can do later with your code examples. Right now I'm not really focused into this.. and I have to go now do some other work.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (36 views)
2014-07-24 01:59:36

Riven (37 views)
2014-07-23 21:16:32

Riven (25 views)
2014-07-23 21:07:15

Riven (27 views)
2014-07-23 20:56:16

ctomni231 (57 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!