Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Selecting buttons — Gideros Forum

Selecting buttons

jmrmbzjmrmbz Member
edited July 2013 in General questions
Was wondering if something like this is possible. If I have 6 buttons all running vertically in a scene. Is it possible that only one can be select-able at a time. So if button1 is already pressed and then press button2, button1 would become de-selected? Similar to the way radio buttons work.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    @jmrmbz basically you should store all buttons in the table, and on each click loop through the table and check if this is the button that was clicked, select it and deselect all other buttons.

    Something like that :)
  • jmrmbzjmrmbz Member
    Thanks @ar2rsawseen. Here's what I have so far. As you can see in the first image I have the first 6 buttons showing. As someone presses a button I'm removing that from the scene and replacing it with another "selected" button in the scene. But I only one one selected at a time. Right now I have the following for each button (i just listed the first 2 ):
    -- disabled 2x3 button
    local disabled2x3 = Button.new(Bitmap.new(Texture.new("images/disabled2x3.png")),
    						Bitmap.new(Texture.new("images/disabled2x3.png")))
    disabled2x3:setPosition(screenWidth()/2 - disabled2x3:getWidth()/2, (screenHeight()/15)+30)
    self:addChild(disabled2x3)
     
    -- enabled 2x3 button
    local enabled2x3 = Button.new(Bitmap.new(Texture.new("images/enabled2x3.png")),							             Bitmap.new(Texture.new("images/enabled2x3.png")))
    enabled2x3:setPosition(screenWidth()/2 - disabled2x3:getWidth()/2, screenHeight()/15)+30)
     
    -- disabled2x3 event listener
    disabled2x3:addEventListener("click", 
    	function()
    	-- adds enabled2x3 button, removes disabled2x3 button
    	-- adds nextenabled button, removes nextdisabled button
    	-- saves chosen gridsize to settings
    	print("2x3 grid size chosen")
    	self:addChild(enabled2x3)
    	self:removeChild(disabled2x3)
    	self:removeChild(nextdisabled)
    	self:addChild(nextenabled)
    	settings.gridsize = {2,3}
    	dataSaver.saveValue("settings", settings)
    	end
    )
     
    -- enabled2x3 event listener
    enabled2x3:addEventListener("click", 
    	function()
    	-- adds disabled2x3 button, removes enabled2x3 button
    	-- adds nextdisabled button, removes nextenabled button
    	-- saves gridsize to 0,0
    	print("2x3 grid size unchosen")
    	self:addChild(disabled2x3)
    	self:removeChild(enabled2x3)
    	self:removeChild(nextenabled)
    	self:addChild(nextdisabled)
    	settings.gridsize = {0,0}
    	dataSaver.saveValue("settings", settings)
    	end
    )

    So from what you're saying I place all 12 buttons in a table and make a loop. How would do what you suggest? I've got the following so far:
    local t = {
    disabled2x3,
    enabled2x3,
    disabled3x3,
    enabled3x3,
    disabled3x4,
    enabled3x4,
    disabled4x4,
    enabled3x4,
    disabled5x6,
    enabled5x6,
    disabled6x6,
    enabled6x6
    }
     
    for tcount = 1, #t do
          -- HELP!
    end
    I'm open to other suggestions if there's an easier way to create the buttons other than what I have so far. :-/
    buttons unselected.png
    347 x 544 - 32K
    button selected.png
    347 x 544 - 33K
  • edited July 2013 Accepted Answer
    @jmrmbz
    You can try my implement of "List value button" for this solution!
    (In my code, you can replace preference class by dataSaver)

    Happy coding!
    zip
    zip
    ListValueButton.zip
    48K
    Capture.PNG
    313 x 476 - 45K
    Coming soon
  • jmrmbzjmrmbz Member
    edited July 2013
    @vitalitymobile Looks good. Exactly what I was looking for. Does using it require the GiderosCodingEasy file?
  • ah, yep I use it for setAnchorPoint method
    Coming soon
  • jmrmbzjmrmbz Member
    Thanks again @vitalitymobile. Testing it out and it works great! :D
  • @jmrmbz you are welcome! :D
    Coming soon
Sign In or Register to comment.