Inputbox with drop down list question
-
Hi,
I'm trying to get a drop down list to work. I'm able to creat an input box and even add a drop down list. But what I'm not able to figure out and can't seem to find the answer too is how to associate a value or have code run from the drop down list options.
As you might have guessed I'm new at this whole programming thing. Any help would be appreciated.
Thanks,
Frank -
@frankn said:
... how to associate a value or have code run from the drop down list options.
What your talking about, is called (in programming,) a "callback" method. Sorry to say, that the API does not implement callbacks for inputboxes. (You can do them, but you must write a extension in C/C++ and use the native system calls.)
In the Google Ruby API, all you can do is collect the values chosen in the the results array. And then use conditional statements depending on what they are.
If the user cancels the inputbox, results will be negative (either
false
ornil
,) and that's why it is common practice to follow the inputbox line with a conditional block that tests that results eval as true. (Remember in Ruby everything evalstrue
, exceptfalse
andnil
.):results = UI.inputbox( ...args... ) if results # process the user's choices end
Jim Foltz wrote an wrapper that makes using inputboxes more "Rubyish"
get it here, at his website:
http://sketchuptips.blogspot.com/2008/03/inputbox-class.html -
Also there is a way to put an inputbox inside a 'loop' so if an input error occurs, the box is redisplayed.
Look at TheDro's Voxelize plugin, I helped him set his inputbox up that way.
-
prompts=["Choose Color: ", "Enter Number: "]
values=["Red", 0] ### defaults are always 'Red' and 0
colors=["Red", "Green", "Blue"]
colors_joined=colors.join("|")which gives "Red|Green|Blue"
drops=[colors_joined, ""]
note how in 'drops' you list the dropdown[s] and a "" for non-dropdown[s] like 'number'
title="RGB?"
results=UI.inputbox(prompts, values, drops, title)
return nil if not resultsthis is to stop if the user clicks 'Cancel'...
color=results[0]
number=results[1]alternatively you can get them in one go
from the array thus:
color,number = results
IF you want to reuse the last entered values as the defaults for the next time the dialog opens in that session use
@color="Red" if not @color
@number=0 if not @numberthen
values=[@color, @number]
###and after you get good 'results'
@color,@number = results###You can then use tests on the values to decide what you do next...
if @color=="Red"do this...
elsif @color=="Green"
do that...
else ### it must be blue
do the other...
end
OR
case @color
when "Red"do this...
###etc
endIf you want to test 'Yes|No|Maybe' or 'True|False' or '0|1|2|3' remember that the result is s a 'string'.
So var1=true if @var1=="True" to make a boolean variable or use var2=@var2.to_i to covert input into a 'number' choice...As Dan says if you will only accept >=0 as a direct number input then make the dialog run from a nested method
def dialog()...
###then in tour main code have
return nil if not self.dialog()the def dialog() method contains these lines
after the dialog has opened etc...
...
return nil if not results
...
if results[1] < 0
UI.messagebox("The number must be >= 0 !")
self.dialog()retry - user either then cancels >> nil or enter a number >= 0 >> true
else ### number was OK
@number=results[1] ### reset @number only now...
return true
endat the end
-
Thank you guys that helped a ton in my understanding of how that works!
I do have a few of questions...
What is the the differnce in creating a list this way...
list = ["item1"|"item2"|"item3"]
and this...
list = ["Wall", "Wall - Corner", "Base", "Base - Corner"]
list_joined = list.join("|")
drops = [list_joined, ""]any advantages disadvantages?
Also in some examples that I found the don't use the UI in the inputbox command... again any advantage in doing either or?
And lastly I'm having trouble understanding/implementing this last part...
def dialog()...
###then in tour main code have
return nil if not self.dialog()the def dialog() method contains these lines
after the dialog has opened etc...
...
return nil if not results
...
if results[1] < 0
UI.messagebox("The number must be >= 0 !")
self.dialog()retry - user either then cancels >> nil or enter a number >= 0 >> true
else ### number was OK
@number=results[1] ### reset @number only now...
return true
endat the end
Frank
-
I'll mostly use
list.join("|")
when the list is auto generated or loaded from a file.
For instance user preferred fonts, in a cfg file. I'll never know what they might be, or how many the user would add to the file.
So I'll build the droplist with.join('|')
-
@frankn said:
Also in some examples that I found the don't use the UI in the inputbox command... again any advantage in doing either or?
OK.. there is a wrapper method in "Tools/sketchup.rb" that defines the global lowercase
inputbox()
Read that file, and you'll see, Scott commented it well.The examples that use the uppercase global
Inputbox
class, use Jim Foltz' class wrapper.(Altho.. I think both really need to be some Lib or Mixin namespace, rather than in the global namespace. Hence the confusion, for the newbie.)
I would suggest you do the wrapping in your own code the same way it's done in "sketchup.rb", rather than use that method in a global sense. Ie, teach yourself to control things, and wrap inside
begin
...retry
...rescue
..end
blocks, rather than rely on someone else's error handling. -
The
list = ["item1"|"item2"|"item3"]
will fail as the syntax is wrong as the elements need ',' to separate them outside on the ""...
BUT it's eaiser to make a list of strings an the join them into one string with the[].join("|")
method...
inputbox()
andUI.inputbox()
are synomymous. -
@tig said:
inputbox()
andUI.inputbox()
are synomymous.Not exactly.
sketchup.rb:inputbox()
wraps a call toUI.inputbox()
within abegin
..rescue
..retry
block, to trapTypeError
exceptions, and re-display the inputbox, so the user can correct the input error(s).It really would be better if this functionality were were just added to the native
UI.inputbox()
method. -
Dan, I stand corrected - clearly the skethcup.rb's 'inputbox' is to be preferred to the UI. version...
Advertisement