tor wrote:Does any one in here have a clue if it is possible and if so maybe could point me in the right direction?
Yes, certainly possible, I use a technique a bit like that for the handles and lines in my GraphicsPath editor - though it's probably not a good learning example, as the methods are all a bit jumbled up!
There are essentially two ways you can do it...
1) Make a whole new class of object "MyButton", and create methods for it to define how a 'MyButton' object works. You could then create buttons using something like "@play_button = MyButton.new("play")", and make arrays of the new objects. But making new classes is a huge subject in its own right, and needs to be treated with extra-special care when using Ruby inside FS.
So, method two is probably simplest - and closer to the idea in your original question...
2) Define some methods that define how to do things for a single button - "drawMyButton", "toggleMyButton" etc. Then store data about the buttons inside an array - where they are on screen, current state etc. - easy in Ruby as you can put a mixture of any old objects into an Array, including other Arrays.
Then you need a few routines that scan the array and draw the buttons, or check for mouse clicks etc. for every button all in one go.
It does take quite a bit of setting up sometimes - but the big advantage is that you can change all of the buttons at once by just editing the "one button" chunks of code.
Here's a little example - an array of different sized round toggle buttons...
For folks coming new to Ruby from SM etc. there are probably a few strange concepts in there, so I'll cover a couple of points - not in much detail, but enough to know what to look for in online tutorials etc., or to ask about here.
Arrays - can mix all kinds of data together, and can include sub-arrays, sub-sub-arrays etc. No more SM restriction of all data having to be the same.
Hashes - a bit like an array, but instead of items being stored in numerical order by index, you give each one a unique 'key'. The keys and values can be just about any type of data that you like - but symbols (colon followed by a name, e.g ':key') are often used as they are quicker to look up than strings or numbers.
Enumerators - any of a whole range of methods that are used with "collections" of things like arrays. An enumerator automatically goes through all the items in the collection, doing something to each item in turn - no need to define loops, or clumsy index maths. The simlplest example is '.each' - which does just that, accesses 'each' item in turn.
Blocks - So what does something like ',each' actually do to each of the array items?
On it's own, '.each' doesn't do anything much at all, it just scans through the items - but, like many other Enumerators, you can tell it what to do by giving it a chunk of code to work with...
Code: Select all
myArray.each do |this_number| # All of this part must be on one line
this_number = this_number + 1
end
Everything between 'do' and 'end' is fed into the '.each' and will happen for every item in the input array. As each item is pulled from the array, it gets assigned to the variable between the || characters '|this_number|' - you then just do whatever with 'this_number' , and that will be what happens to everything in the array. And not a single array index in sight!
You can also use curly brackets instead of 'do' and 'end', which is great for little 'one-liners', e.g....
Code: Select all
myArray.each{|this_number| this_number+=1}
...which does exactly the same as the previous example - adds one to every number in the array.