Welcome to Part 2 of the basics of DesktopX
by CerebroJD
Time for part two, assuming you read through and understood part one of the series.
Now we know basicly how to adjust some of an objects attributes. Width isnt the only thing that you can change though! Through various end attributes you can change position, color, transparency, rotation, etc. So far, what we've used is just a static value for the attribute value, though, and it doesnt have to be that way. We're gonna learn two things really quick here, timers and dynamic changes. First off, the timer. What we're going to do is take out that stuff we had on line 3 of the script, so that the first sub is empty again. Now, we must "initialize the timer". Sounds cool, but really isnt that big of a deal. Just gotta put in a bit saying what you're doing, then what the ID of the timer is, and how often it triggers. The math for how often it triggers is wierd... 1000 = 1 second. Therefor, intitializing a timer called 42 to trigger every second looks something like this:
Object.SetTimer 42, 1000
So now we've got this timer going...and it'll just keep firing away, with nothing actually happening, unless you put in a new sub with info on what to do when it triggers. In order to do that, we're going to add in a new sub. Its easy to get a sub to react to a timer, because instead of having it react to Object_OnScriptEnter, we just have it react to Object_OnTimer* with * being the number ID of your timer you made. For me, that code is this:
Sub Object_OnTimer42
End Sub
Working within this sub is just as easy as the other one, so we're gonna toss a tiny bit of dynamic content in there, just to make things interesting. Remember how we were setting the image width to a fixed number before? This time, we're going to set the width to equal its existing width, plus 10. If we put that into the the sub that is triggered by the timer, everytime it is fired, the width will increase by 10 pixels. Our final sub code for this will be this:
Sub Object_OnTimer42
DesktopX.Object("Test").Width = DesktopX.Object("Test").Width + 10
End Sub
Close your editor, and hit apply. Your object will begin growing slowly getting wider every second. Now, read this really fast, because it'll be too wide for your screen for very long. What we're gonna do now is take that line out again, otherwise it'll get too big! So take it out, (gonna do something different this time, so dont quit the editor yet!), hit File->Save and Apply. That way we dont have to be going in and out of the script editor so much! Immediate feedback is always handy.
To fully get the use of out of DX, you're not going to always be playing around with widths and heights... you're going to want to actually process data that's being moved around. We're gonna try some working with numbers next. Close out of the editor and look under the States tab, and change the radio button from Image to Text, then go back into the script editor via the General tab. We're going to start manipulating some of the numbers that we have. We've already done a bit of it, in the last example, but you can do all sorts of math using the VBScript that DX uses. Really, DX's power comes from two things: Its ablility to process and show data in a simple way, and its ability to display it using nearly any graphics format.
Really, everything you need from here on out is all in either the DesktopX documentation as well as in the section on VBScript over at www.w3schools.com, but I'm gonna run through a really neat little tutorial that will show you just a tiny bit of what dynamic DX scripting can do.
Goal of the object: To create a miniature representation of a monitor to graphically indicate mouse position. Gonna make this thing SUPER simple... 2 graphics... the rest is script. The images I used are one 6px by 6px blue box. Thats the "Screen" and will stetch and skew without any sort of distortion. The "Mouse" is a 1px by 1px white image.
Note: We're making this script within the "Screen" object, so work on it instead of "Mouse". Be sure you set the Screen object as the parent object of the Mouse one, with Mouse's properties window (Summary tab). First thing we're going to do is make a timer, naming it 42 (since thats the answer to Life, the Universe and Everything), and set it to go off every 1/10th of a second, so 100. (Note! Comment your work.... its very important if you wish others to learn from it)
'Called when the script is executed
Sub Object_OnScriptEnter
Object.SetTimer 42, 100 'Sets a timer that fires every 1/10th of a second
End Sub
Logical, makes sense, and leads to the next step, which is to grab some system info and do some math. First, we'll start our sub, then get the info.
'Goes to work when the timer triggers
Sub Object_OnTimer42
DesktopX.Object("Screen").Width = System.VScreenWidth/10 'Object's width equals width of screen divided by 10
DesktopX.Object("Screen").Height = System.VScreenHeight/10 'Object's height equals height of screen divided by 10
Now to position the "Mouse" on the "Screen" using a bit of data retrival and a bit of math, just like before.
DesktopX.Object("Mouse").Left = System.CursorX/10 'Self-explanatory
DesktopX.Object("Mouse").Top = System.CursorY/10 'Self-explanatory
End Sub
Finally, the last sub, the OnScriptExit one, that wraps it all together, and in this case, kills the timer.
'Called when the script is terminated
Sub Object_OnScriptExit
Object.KillTimer 42 'Does the killing
End Sub
Here's an interesting little bit of information for you, actually. This wont work correctly on my system just like this. My dual monitor setup hinders my use of the entire area, because although the VSscreenWidth will return a positive value of 3200 in this case, the stuff on the left monitor is measured in the negative pixel values. Therefore, to force this object to show the correct position for me, I have to add VScreen.Width/2 to the total to bump it over the right amount. Most of you WILL NOT have this problem, but its an easy fix in case you do have that.
My completed object's script in a screenshot: Click to view Image
-
Should I make more? What should I do them on?