Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OptionButton fix remove and select #21837

Closed

Conversation

AlexHolly
Copy link
Contributor

@AlexHolly AlexHolly commented Sep 7, 2018

closes #6558

Added a few features since these are always needed.

  • Make sure there is always something selected
    • Always Select next on remove_item
    • Always Select first on add_item if current is -1
  • Make sure no duplicate names are allowed (it is optional and disabled by default)
  • Deselect all now works with select(-1)

Insert at id was not working, solved it by converting -1 to size() and added a sort by item.ID in PopupMenu. It updates item.ID on each sort. Adding an item at < current will increase current by one silently.

Example

func _ready():
	$OptionButton.add_item("0")
	$OptionButton.add_item("1")
	$OptionButton.add_item("2")
	$OptionButton.add_item("3")
	
	$OptionButton.add_item("0.0",0) # Moves 0 to index 1 and updates current from 0 to 1

Test

extends Control

var option_button
var label_index

func _ready():
		
	var hb_label = HBoxContainer.new()
	var label = Label.new()
	label.text = "Selected: "
	label_index = Label.new()
	
	hb_label.add_child(label)
	hb_label.add_child(label_index)
	
	var vb = VBoxContainer.new()
	var hb = HBoxContainer.new()
	hb.set("custom_constants/separation", 20)
	
	var add = Button.new()
	var add_custom = Button.new()
	var delete = Button.new()
	var clear = Button.new()
	var select_nothing = Button.new()
	option_button = OptionButton.new()
	#option_button.set_allow_duplicates(false)
	option_button.connect("item_selected",self,"_on_OptionButton_item_selected")
	add()
	add()
	add()
	add()
	add()
	
	add.text = "Add"
	add_custom.text = "Add Custom"
	delete.text = "Delete"
	clear.text = "Clear"
	select_nothing.text = "Select Nothing"
	add.connect("pressed", self, "add")
	add_custom.connect("pressed", self, "add_custom")
	delete.connect("pressed", self, "delete")
	clear.connect("pressed", self, "clear")
	select_nothing.connect("pressed", self, "select_nothing")
	
	hb.add_child(add)
	hb.add_child(add_custom)
	hb.add_child(delete)
	hb.add_child(clear)
	hb.add_child(select_nothing)
	
	vb.add_child(hb)
	vb.add_child(hb_label)
	vb.add_child(option_button)
	
	add_child(vb)


func add():
	if !option_button.add_item("Element "+str(option_button.get_item_count())):
		print("Already in use: " + str(option_button.get_item_count()))
	
func add_custom():
	if !option_button.add_item("Custom Element "+str(option_button.get_item_count()), 0): # Try 0,1,2
		print("Already in use: " + str(option_button.get_item_count()))

func delete():
	option_button.remove_item(option_button.get_selected())
	
func clear():
	option_button.clear()

func select_nothing():
	option_button.select(-1)

func _process(delta):
	label_index.text = str(option_button.get_selected())
	
func _on_OptionButton_item_selected(ID):
	print("Event: " + str(ID))

@AlexHolly AlexHolly force-pushed the fix-optionbutton-remove-select branch from 3410837 to dcb5c7b Compare September 7, 2018 18:13
@AlexHolly AlexHolly requested a review from reduz as a code owner September 7, 2018 18:13
@AlexHolly AlexHolly force-pushed the fix-optionbutton-remove-select branch 2 times, most recently from 882fc6c to a55eb0e Compare September 7, 2018 20:55
@Chaosus Chaosus added this to the 3.1 milestone Sep 8, 2018
@AlexHolly AlexHolly force-pushed the fix-optionbutton-remove-select branch from a55eb0e to f812327 Compare September 8, 2018 14:28
@@ -313,11 +380,13 @@ void OptionButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear"), &OptionButton::clear);
ClassDB::bind_method(D_METHOD("select", "idx"), &OptionButton::select);
ClassDB::bind_method(D_METHOD("get_selected"), &OptionButton::get_selected);
ClassDB::bind_method(D_METHOD("get_selected_id"), &OptionButton::get_selected_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks compat, can't be merged for 3.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can put it back in, makes no difference. I will add it to the tracker.

@akien-mga
Copy link
Member

Moving to 3.2 milestone as we're about to enter the beta stage and release freeze for Godot 3.1. Only bug fixes against issues of the 3.1 milestone (or enhancements to core features of the 3.1 roadmap) will be considered until 3.1-stable is released.

Note: If it's merged after 3.1-stable is released, this change might be considered for cherry-picking into a later 3.1.x release.

@akien-mga akien-mga modified the milestones: 3.1, 3.2 Dec 12, 2018
@akien-mga akien-mga modified the milestones: 3.2, 4.0 Oct 4, 2019
@AlexHolly AlexHolly force-pushed the fix-optionbutton-remove-select branch from 6c2bb55 to de29248 Compare November 11, 2019 14:02
@aaronfranke
Copy link
Member

@AlexHolly Is this still desired? If so, it needs to be rebased on the latest master branch.

If not, abandoned pull requests will be closed in the future as announced here.

@aaronfranke
Copy link
Member

This PR has not received any new commits for over half a year and is abandoned, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A bucket of issues with OptionButton (Dropdown)
4 participants