DesktopX: Scripting Parent Child Relations
From WinCustomize Wiki
|Parent Child Relations|
Scripting Parent/Child Relations
Parent/child relations are often key to a more streamlined interface and integrated scripts. It can allow you to have one script in the parent object that can detect user interaction with the children, thus eliminating the need to have scripts running in all of the child objects. It can also facilitate visual effects pertaining to the visibility of the child objects using what’s called a ‘mask’.
Creating a Parent/Child Relation
Create an object and call it “parentobj”. Apply the image below.
Create another object and call it “childobj1”.
Place childobj1 inside the parentobj. R-click on childobj1 and set the parent to “parentobj”.
You’ve just created a parent/child relation! Now, when you move the parent object the child object moves with it. You can still move the child object independently but you’ll notice that when you move it outside of the parent boundaries it is not visible.
Oftentimes, you’ll need to set the parent/child relations through properties as well as manually setting the top/left coordinates to inside the parent object dimensions. To do this, you'd set it in the properties of the child object. R-click on childobj1 and go to Properties > Summary.
Here you see that the top/left coordinates are in relation to the parent and not the screen dimensions. The Parent/Owner entry is set to “parentobj”, and the Child entry is set to ‘yes’.
Setting the child entry to ‘no’ will allow the child object to move freely outside the boundaries of the parent and will reset the top/left coordinates to the screen dimensions. However, moving the parent object will still move the child object as well.
Using a Mask
As mentioned earlier when you move a child object outside of the parent object it is no longer visible. Using this technique creates a masking effect and here’s a great tutorial by Master RomanDA which uses this effect to show and hide objects on mouse over/away.
This technique is used alot to create complex widgets (as you'll see in upcoming tuts) and I highly recommend learning how to do it.
Scripting for Child Objects
A lot of the functions you would use in the script of an object, like OnLButtonUp, OnMouseOver etc., can be also used to detect user interactions with the child objects, too! This is extremely useful in reducing the amount of scripts you have running.
Set the parentobj width 250. Make sure childobj1’s parent is set to 'parentobj’ and child is set to ‘yes’. Clone childobj1 twice so you have three child objects.
Now create a new script in the parentobj and insert the code below.
'Called when L-click on object or its children Function Object_OnLButtonUpEx(obj, x, y, dragged) If Not dragged Then Select Case obj.name Case "childobj1" msgbox "You clicked on child 1" Case "childobj2" msgbox "You clicked on child 2" Case "childobj3" msgbox "You clicked on child 3" End Select End If End Function
The OnLButtonUpEx function has an extra parameter: ‘obj’. This will detect which child object the user is interacting with. Combined with .name, obj.name, we can retrieve the name of the child object in Select Case so we can have different results depending on which object is clicked.
Just as you can turn OnLButtonUp into OnLButtonUpEx you can do the same for almost all of the other object functions.
That’s all for today. :)
None right now.