+1 vote

Hello There,

I am developing a game for a german company in which they use many option buttons.

Sadly they also have a fixed resolution for the game. In one of the optionButtons, the options should show all the letters of the alphabet, meaning that it has more options then space in the screen.

Since the game should run on Android devices, im wondering how can I make the options scroll with a touch input, because I'm able to scroll using the mouse wheel but no dragging function..

I tried making a scrollcontainer for the whole scene, but the client didn't like it.

Is there any way I can scroll or make them grow horizontaly or is there any other options I may use ?

in Engine by (16 points)

2 Answers

0 votes

When you press an OptionButton it actually shows a PopupMenu, which you can access with get_popup() and then do whatever you want with.

Here's a simple example; it lets you drag the popup with right click.

extends OptionButton

onready var popup = get_popup()

func _ready():
    popup.connect("gui_input", self, "_on_popup_gui_input")

func _on_popup_gui_input(event):
    if event is InputEventMouseMotion\
    && Input.is_mouse_button_pressed(2):
        popup.rect_position.y += event.relative.y
by (14 points)
0 votes

@Royerson's answer got me halfway there (Thanks!), but I ran into issues with the popup erroneously selecting items when I mean to be dragging. I use Godot C# Mono, but here is my code that is working OK for me by tracking the drag state.


using Godot;
using System;

public class DraggableOptionButton : OptionButton{
    private PopupMenu popup;
    private Boolean wasJustDragging = false;
    public Boolean PopupIsVisible { get { 
        return this.popup.Visible;}}

    public override void _Ready(){
        this.popup = this.GetPopup();
        this.popup.HideOnItemSelection = false;
        this.popup.HideOnCheckableItemSelection = false;
        this.popup.Connect("gui_input", this, nameof(this.OnPopupGuiInput));
    }

    public void  OnPopupGuiInput(InputEvent @event){
        this.popup.HideOnItemSelection = false;
        this.popup.HideOnCheckableItemSelection = false;
        if(@event is InputEventScreenDrag){
            var dragEvent = (InputEventScreenDrag)@event;
            this.popup.RectPosition += new Vector2(0, dragEvent.Relative.y);
            //If we're dragging, set this flag to not release popup until we're done
            this.wasJustDragging = true;}
        if(@event is InputEventMouseButton){
            if(!this.wasJustDragging){
                //If we weren't just dragging, release the popup for normal input
                this.popup.HideOnItemSelection = true;
                this.popup.HideOnCheckableItemSelection = true;}
            this.wasJustDragging = false;}
    }
}
by (18 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.