I need Help making an Intelligently Resizing TouchScreenButton

SosaseesSosasees Posts: 104Member
edited July 21 in GUI

In my Android App, I need to be able to press multiple Buttons at once.
But I can only press one button at a time, like in this YouTube Video:

I wanted to try TouchScreenButtons, but they can't be resized and stylized like Regular Buttons.
How could I make them Resize (and possible be stylized) like Regular Buttons?

I'm also open for Engine Modifications, but I prefer using an Official Build so that it's easier for others to modify my Games.


Tags :

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,831
    Accepted Answer

    Just found this: https://godotengine.org/qa/41604/how-to-enable-multi-touch-on-multiple-texturebutton

    Maybe you could use the _gui_input function and detect touches that way? Apparently it worked okay for detecting multitouches for the OP, based on the comments in that QA. The issue they seemed to get stuck on was emitting the pressed signal once a touch was detected, which should just be a matter of adding emit_signal("pressed") once a touch is detected (assuming it works as expected).

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin

    While not an answer to your question, I did think of something that may help: You may be able to convert the touchscreen events to mouse events via GDScript, and that may allow you to have multiple touch inputs with normal UI nodes with touchscreen devices. I don't know if this would work though, but it may be something to consider.

    The code for it could be something like this (untested):

    func _input(event):
        if (event is InputEventScreenTouch):
            var new_event = InputEventMouseButton.new()
            new_event.button_index = BUTTON_LEFT
            new_event.position = event.position
            new_event.pressed = event.pressed
    
            get_tree().set_input_as_handled()
            get_tree()._input_event(new_event)
    

    In theory, that should convert all screen taps to mouse button events. Again, I don't know if it would work, but it might be something to try.

  • SosaseesSosasees Posts: 104Member

    Where does this code belong?

    Should I put it on the Top-Most Parent of the GUI?
    Or even an Autoload Script if the entire game is GUI?

  • SosaseesSosasees Posts: 104Member
    edited July 23

    I created this script as a Standalone .gd file,
    and added it to Autoload.
    It didn't make any difference. I can still only touch One button at a time, like in the video.

    extends Node
    
    func _input(event):
        if event is InputEventScreenTouch:
            var new_event = InputEventMouseButton.new()
            new_event.button_index = BUTTON_LEFT
            new_event.position = event.position
            new_event.pressed = event.pressed
    
            get_tree().set_input_as_handled()
            get_tree()._input_event(new_event)
    
  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin
    edited July 23

    Hmm, so maybe that isn't a viable solution then. Just to be sure, have you tested multiple touch screen buttons and made sure they work with multiple touches? It might not hurt to check if you haven't already.

    As far as handling dynamically resizing TouchScreenButtons, I would see about using a normal Control node with anchors and the like, and have the TouchScreenButton node a child of that Control. Then, I would try writing a script that adjusts the size and position of the TouchScreenButton so it stays centered in the Control and scales when the Control's Rectsize changes. I'm sure its doable, but I have no idea how messy the code would be though. I'll keep thinking about it and see if I can think of a better solution.

    (Also: sorry about not replying sooner! Life's been busy as of late)

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin
    Accepted Answer

    Just found this: https://godotengine.org/qa/41604/how-to-enable-multi-touch-on-multiple-texturebutton

    Maybe you could use the _gui_input function and detect touches that way? Apparently it worked okay for detecting multitouches for the OP, based on the comments in that QA. The issue they seemed to get stuck on was emitting the pressed signal once a touch was detected, which should just be a matter of adding emit_signal("pressed") once a touch is detected (assuming it works as expected).

  • SosaseesSosasees Posts: 104Member
    edited July 24

    Thanks very much!
    I've replaced the code for the button:

    # res://scripts/playAudio.gd
    extends Button
    
    func _gui_input(event):
        if event is InputEventScreenTouch:
            pressed = !pressed
            $AudioStreamPlayer.play()
    

    Before, it was:

    # res://scripts/playAudio.gd
    extends Button
    
    func _pressed():
        $AudioStreamPlayer.play()
    

    Now I can press multiple buttons on my phone,
    although they don't anymore change their visual appearence when pressed:

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin

    Great! I'm glad that made it where you can use multi-touch with the buttons! I would have thought that changing the pressed variable would change the style though.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file