Controlling Groups
Beginner
Monday, January 22, 2007 by sViz | Discussion: DesktopX Tutorials
Toggling a group of objects on and off (show/hide) through object properties is not hard. You just need to group your objects and create another object as an object controller. Like what you see below.
In the properties of the group controller, change the object type to ‘object controller’. In the command type dialog set it to open/toggle, and then in the drop down choose the name of the group.
Folks, it’s that easy. Further configurations can be made in the group objects themselves under Properties > Relation > Popup as explained in the Developer’s Guide here—Link
I know you’re looking at the command options in that object controller and you’re thinking, can’t I control more than show/hide? Well, maybe you’re not but the answer is—YES! With simple scripts you can move the entire group, change the color of all the objects in the group, re-arrange the group, and yes, you can still show and hide the group, too!
Let’s get started.
Change the group controller’s object type back to ‘Layer’ because that’s where we’ll put the script.
When scripting groups, the For Each statement is extremely useful. This statement basically tells DX to do something for each object in the group. In this statement each object is called ‘elem’ and you can assign it most normal properties (e.g. object.text / elem.text, object.hue / elem.hue etc.) such as explained in the DevGuide here – Link
Here are some scripts for you to try (my group’s name is “testgroup”).
TOGGLE (SHOW/HIDE) SCRIPT:
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then
For Each elem In Desktopx.GroupObjects("testgroup")
If elem.visible = True Then
elem.visible = False
ElseIf elem.visible = False Then
elem.visible = True
End If
Next
End If
End Sub
MOVE GROUP SCRIPT:
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then
For Each elem In Desktopx.GroupObjects("testgroup")
elem.left = elem.left + 2
Next
End If
End Sub
CHANGE COLOR SCRIPT:
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then
For Each elem In Desktopx.GroupObjects("testgroup")
If elem.hue=> 255 Then
elem.hue = 0
Else
elem.hue = elem.hue + 15
End If
Next
End If
End Sub
Simple, no?
Now, what if you don’t want your objects in a group? After all you might want them to move independently. Ungroup the grouped objects and insert the script below into the group controller:
TOGGLE SEVERAL OBJECTS:
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then
‘Check if one of the objects are already hidden or showing
Select Case Desktopx.Object("1").visible
Case True
showhide= False
Case False
showhide= True
End Select
'Set objects visibility
Desktopx.Object("1").visible = showhide
Desktopx.Object("2").visible = showhide
Desktopx.Object("3").visible = showhide
End If
End Sub
MOVE SEVERAL OBJECTS:
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then
Desktopx.Object("1").top = Desktopx.Object("1").top + 2
Desktopx.Object("2").top = Desktopx.Object("2").top + 2
Desktopx.Object("3").top = Desktopx.Object("3").top + 2
End If
End Sub
Obviously, when you think about it, if you had A LOT of ungrouped objects you wanted to control, listing them would be laborious and tweaking one small aspect would be painstaking. Trust me, I know. Before I got the hang of scripting I used this method ad nauseam and every time I wanted to make a change I had to go through the entire list—several times! There is an easier way. This method uses the For Next statement and is facilitated by how you name the objects you want to control. I use this method a lot as I did in my Scrolling Text tutorial Link , here I’ll explain it some more.
First, rename your objects like this:
object1, object2, object3 OR item1, item2, item3 (not item01, 02, 03 etc.)
Whatever name you use make sure you name the other objects the same and in serial order. This is important. It is also important to define the number of objects you are controlling. We’ll use the variable numofobjs to do this. Insert the script below into the group controller.
Dim numofobjs
'Define number of objects to control
numofobjs = 3
Sub Object_OnLbuttonUp(x,y,dragged)
If Not dragged Then toggle
End Sub
Function toggle
'Check the visiblity of object
Select Case Desktopx.Object("object1").visible
Case True
showhide= False
Case False
showhide= True
End Select
'Reset visibility of objects
For x= 1 To numofobjs
desktopx.Object("object" & x).visible =showhide
Next
End Function
Now what does this mean:
For x= 1 To numofobjs
desktopx.Object("object" & x).visible =showhide
Next
For Next is basically a loop that will start at 1 and do whatever code is in between as many times as you define in numofobjs; that’s 3 times. So it goes from 1 to 3.
X is a variable that will change on each loop. On the 1st loop x=1, on the 2nd loop x=2, and on the 3rd loop x=3.
So when you add ‘x’ to the object name here: (“object” & x) you will get “object1” on the first loop, “object2” on the 2nd loop and “object3” on the 3rd loop. So, on each loop you set the visibility of a different object! It’s nice and streamlined and much easier than listing each object line after line.
The end! Thanks for reading.
Reply #2 Tuesday, January 23, 2007 7:56 AM
Please login to comment and/or vote for this skin.
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
- Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
- Access to a great community, with a massive database of many, many areas of interest.
- Access to contests & subscription offers like exclusive emails.
- It's simple, and FREE!
Reply #1 Monday, January 22, 2007 7:49 PM