Author Topic: Sliding over buttons/Sticky keys question  (Read 9811 times)

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Sliding over buttons/Sticky keys question
« on: September 03, 2014, 11:53:57 AM »
Hi,

We have a character movement interface that includes left/right,  jump and dive buttons.

Left and Right have the particularity, that you can slide your finger from one button to the other, and that one becomes 'pressed' which makes the character moved in the desired direction, it's not a slider per se, it's just that without lifting your finger, the button you're on becomes 'pressed'. If you go back to to the previous button now this one becomes 'pressed' again, and the previous one 'unpressed'.

The issue is that when the left/right buttons are pressed, if the Jump or Dive buttons are tapped, even though the movement still works, the buttons show their normal state, i.e. as if they weren't being pressed, but the character does move.

Here's the code:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class InputBtnLeft : MonoBehaviour {
  5.  
  6.         private GameObject btnRight;
  7.  
  8.         void Awake () {
  9.                 btnRight = GameObject.Find("btnRight");
  10.         }
  11.  
  12.         void OnPress (bool pressed) {
  13.                 if (pressed) {
  14.                         InputHandler.movingLeft = true;
  15.             TouchButtonController.touchBtnLeftPressed = true;
  16.                 } else {
  17.                         InputHandler.movingLeft = false;
  18.             TouchButtonController.touchBtnLeftPressed = false;
  19.                 }
  20.         }
  21.  
  22.         void OnDragOver(GameObject draggedObject) {
  23.                 btnRight.SendMessage("OnPress", false);
  24.                 gameObject.SendMessage("OnPress", true);
  25.         }
  26.  
  27.         void OnDragEnd() {
  28.                 btnRight.SendMessage("OnPress", false);
  29.         }
  30. }
  31.  

The Right button is pretty much the same.

The code inside OnDragEnd is what causes the button to show as unpressed when Jump or Dive are tapped, but I need it so that when the player slides to the other left or right button, it doesn't stay pressed (press here meaning that the character doesn't change direction but keeps moving in the previous direction) as I slide away from it.

We've tried out multiple things found lurking through the forum, but they don't seem to have the same requirements we have.

Is there a way to work around this? Hope you can help out. Is there something like UICamera.hoveredObject but for multitouch? Maybe that is what we need? IDK. Thanks for any help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #1 on: September 03, 2014, 12:00:59 PM »
Why are you doing any of this? You receive OnDragOver / OnDragOut notifications when you press on object A then move the touch to object B. Use them.

Also, the UIButton component has an option of what to do when you drag over it -- assuming a pressed state is one option.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #2 on: September 03, 2014, 02:34:57 PM »
Cool, I'll look into that.

On a similar note, we're having an issue on which if the device lags, the pushed button loses that it's being pressed (the character stops moving), and all buttons become unresponsive for a bit. Any clue?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #3 on: September 04, 2014, 10:33:12 AM »
Get rid of your custom events and stick to NGUI's, and it should work fine.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #4 on: September 04, 2014, 03:33:40 PM »
Hey Aren, thank you very much for your time.

We are not entirely sure if we understood your first message, but we tried this, is this what you mean about replacing the custom events we had? Thanks

  1.     void OnPress (bool pressed) {
  2.                 if (pressed) {
  3.                         InputHandler.movingLeft = true;
  4.             TouchButtonController.touchBtnLeftPressed = true;
  5.                 } else {
  6.                         InputHandler.movingLeft = false;
  7.                         TouchButtonController.touchBtnLeftPressed = false;
  8.                 }
  9.         }
  10.  
  11.         void OnDragOver() {
  12.                 InputHandler.movingLeft = true;
  13.                 TouchButtonController.touchBtnLeftPressed = true;
  14.         }
  15.  
  16.         void OnDragOut() {
  17.                 InputHandler.movingLeft = false;
  18.                 TouchButtonController.touchBtnLeftPressed = false;
  19.         }
  20.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #5 on: September 05, 2014, 09:33:02 AM »
Sure, this will work.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #6 on: September 05, 2014, 11:10:36 AM »
Hey,

This is giving us the exact same issue though. Pressing a second button (for instance jump), sets the state of the first one (move left for instance) to 'normal', instead of 'pressed'. The character still moves left, but the button isn't showing so. 'Allow multitouch' is enabled on UICamera also, which was what I thought could have been the issue originally, but that wasn't it.

Any ideas?

Thaks for your help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #7 on: September 05, 2014, 01:26:15 PM »
Assuming you use different code for each button instead of just re-using the same 'left', you may also want to check for whether the touch is still pressed in OnDragOut. Remember, you will get OnDragOut when you press on button A, then move the touch away from it. I suggest using an integer counter instead. +1 for touch, +1 for drag. Subtract when you unpress and/or drag out.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #8 on: September 05, 2014, 03:56:27 PM »
Hey Aren,

Thanks again for your response.

Do you mean something like this?

leftMovementButton (the right movement is just about the same):
  1.         void OnPress (bool pressed) {
  2.                 if (pressed) {
  3.                         InputHandler.movingLeft = true;
  4.             TouchButtonController.touchBtnLeftPressed = true;
  5.  
  6.                         leftButtonPressValue++;
  7.                 } else {
  8.                         InputHandler.movingLeft = false;
  9.                         TouchButtonController.touchBtnLeftPressed = false;
  10.  
  11.                         leftButtonPressValue--;
  12.                 }
  13.         }
  14.  
  15.         void OnDragOver() {
  16.                 InputHandler.movingLeft = true;
  17.                 TouchButtonController.touchBtnLeftPressed = true;
  18.  
  19.                 leftButtonPressValue = leftButtonPressValue + 2;
  20.         }
  21.  
  22.         void OnDragOut() {
  23.                 InputHandler.movingLeft = false;
  24.                 TouchButtonController.touchBtnLeftPressed = false;
  25.  
  26.                 leftButtonPressValue = leftButtonPressValue - 2;
  27.         }
  28.  

PressHandler:
  1.         void Update() {
  2.                 //RIGHT PRESSED
  3.                 if (rightButtonPressValue == 3) {
  4.                         btnRight.GetComponent<UIButton>().state = UIButton.State.Pressed;
  5.                 }
  6.  
  7.                 if (rightButtonPressValue == 1 && leftButtonPressValue == 0) {
  8.                         btnRight.GetComponent<UIButton>().state = UIButton.State.Pressed;
  9.                 }
  10.  
  11.                 //LEFT PRESSED
  12.                 if (leftButtonPressValue == 3) {
  13.                         btnLeft.GetComponent<UIButton>().state = UIButton.State.Pressed;
  14.                 }
  15.  
  16.                 if (leftButtonPressValue == 1 && rightButtonPressValue == 0) {
  17.                         btnLeft.GetComponent<UIButton>().state = UIButton.State.Pressed;
  18.                 }
  19.         }
  20.  

I guess I could optimize the handler, but I just want to make sure we're on the same page on what you are suggesting.

Thanks again.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #9 on: September 06, 2014, 01:59:54 AM »
Don't set something to 'false'. Do this instead:
  1. if (--yourCounter == 0) setSomeValue = false;

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #10 on: September 08, 2014, 02:49:23 PM »
Sorry, I didn't understand your reply.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #11 on: September 08, 2014, 10:47:05 PM »
You have two places where you can get the 'true' state set, and two places where you can have 'false' state set. However if you press on something, then start the drag operation, you will get OnDragOut, but the button is still pressed. In your code, this causes a 'false' state to be set, when in fact it's still 'true' because you don't consider that you're still in the pressed state.

This is why I am saying have an integer counter. +1 it in OnPress(true), +1 in OnDragOver. -1 in OnPress(false), -1 in OnDragOut. You're in a pressed state when the counter is above zero.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #12 on: September 09, 2014, 03:27:46 PM »
The thing about that is that if I do it on the 'right' button for instance:
- OnPress(true) +1 (counter = 1)
- OnDragOver +1 (counter = 2)
- OnDragOut -1 (counter = 1)

Then:
  1. if (rightButtonPressValue > 0) {gameObject.GetComponent<UIButton>().state = UIButton.State.Pressed;}

The resulting value on the counter will be 1, therefore showing as pressed even when I slide to the 'left' button. Which is what I don't want.

And unless there's a bug somehwere, for me OnPress(false) doesn't execute until I've lifted my finger, even if it's elsewhere on the screen. But I thought this was the actual behaviour.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #13 on: September 12, 2014, 04:03:33 AM »
Let me try to explain this again...

You presson a button:
  OnPress(true) +1 (current = 1)

You start dragging it:
  OnDragOver +1 (current = 2)

You drag away from it:
  OnDragOut -1 (current = 1)

You drag back onto the button:
  OnDragOver +1 (current = 2)

You release
  OnDragOut -1 (current = 1)
  OnPress(false) -1 (current = 0)

Do you see how it neatly adds up? I don't understand why you tried to use +2 and -2 in there.

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Sliding over buttons/Sticky keys question
« Reply #14 on: September 17, 2014, 10:37:19 AM »
Hi Aren,

Sorry if I haven't been able to communicate exactly what I want, because I don't think I have.

I already removed the "+2" on my code, as you suggested, the thing about having the buttons show as pressed when the value is > 0, is that it shows pressed even when I Drag Out of it, which is what I do not want, I want it to go to 'normal' state, when I drag out.

The issue is that while pressing it, if I don't drag out, but 'press' another button (with another finger), the button I'm still pressing goes to 'normal' state, but I have not released it. That is the issue.