Issue 67 * March 13 2009

Playing with Behaviors
How to create an interactive map of Europe

by Viktoras Didziulis

Editors Note: This article requires you to have access to a pre-release version of Revolution 3.5. If you have an updated Enterprise license, you can get this by going to "Check for Updates" under the Help menu inside Revolution, and downloading the latest dp release. If you have a current Studio license you will be able to get access via the public beta, (see article 1 of this edition of revUp). If you have neither of these licenses types, and would like to find out your best route to upgrading, please email support@runrev.com and we'll be happy to help you.

 

If you have read revUp issue 65, you should already be aware about the new scripting feature called behaviors or parentscripts. This feature available in the upcoming 3.5 release of Revolution brings new potential to Revolution coding practices. Likely the most important thing to say about parentscripts is that it allows a programmer to reuse Revolution code easily without replicating it in each and every control where it is necessary. Using Revolution 3.5 it is as easy as setting the parentScript property of a control to point to a button where the reusable script resides.

This article should make you familiar and ready to go with this new feature. We will learn to create a simple interactive map application where the user can learn more about Europe by interacting with a vector map.

First let's create a stack with two cards on it. The top level card cMap contains all the necessary controls (vector polygon graphics) grouped into a group gCountries on it, and the second card called cResources holds all the relevant resources and keeps them hidden from the end user.

Next create an empty button called "bParent" on a hidden card cResources. This is will be the container of the parentscript. On the same card, create an empty field "fData". It will hold country data. For the sake of simplicity in this example, here I use a field to host data instead of a real database.

In fact what I did first was to choose a physical map of the world to use as a background image, then I digitized all the countries on it into polygons. This was probably the most complicated and time consuming part of the development. But you can skip it by reusing the stack I created. Type this in your message box:  

go url "http://ekoinf.net/revolution/ParentScripts.rev"

Its around 1mb. Feel free to use it in your other projects if it suits your needs. The result so far should look like the picture above - a map image with 90 polygons each representing regions on the map with each polygon name corresponding to a region code in the database of facts.

To make these polygons look more "eye candy" either set the relevant properties of the templateGraphic so that when creating a new polygon, it inherits these properties from the template, or alternatively write a handler with a repeat loop that changes the properties for all region polygons in the group "gCountries" (I put it in the script of an "initialize map" button on cResources card):


on mouseUp   
      repeat with i = 1 to the number of controls of group \
      "gCountries" of card "cMap"
            set the tooltip of control i of group "gCountries" \
      of card "cMap" to "click and listen..."
            set the backgroundcolor of control i of group \
      "gCountries" of card "cMap" to 255 , 255 , 0 #yellow
            set the bordercolor of control i of group \
      "gCountries" of card "cMap" to 255 , 128 , 0 #orange
            set the blendlevel of control i of group \
      "gCountries" of card "cMap" to 60
            set the parentScript of control i of group \
      "gCountries" of card "cMap" to the long id of \
      button "bParent" of card "cResources"
      end repeat    
end mouseUp   

I would like to draw your attention to the last sentence of the repeat loop which is all the magic necessary to set the parentscript property of each polygon. Note that the parentscript should point to a button containing the relevant script (bParent in our case) by addressing it via the longid property. Once you create or edit the script of bParent button, every control will be able to use it at once. There are no additional steps to get it working!

Now let's code the behavior. Open the script of the button "bParent" in the script editor and paste the following there:

-- we wish it to say facts about the region which is clicked, so
-- this handler calls searchItem() function to get the relevant 
-- info from the database and says it aloud. If speech engine 
-- is busy with talking, revStopSpeach will stop it, so end user 
-- won't be forced to wait until it stops speaking by itself.

on mouseUp 
      revStopSpeech 
      revSpeak searchItem( the short name of me )    
end mouseUp 

-- this changes appearance (makes it more prominent) of polygons 
--on mouse enter event. Note the use of “me” to address the
-- polygon as if the script resides in the polygon itself 

on mouseEnter 
      set the blendLevel of me to 20 
      set the foregroundcolor of me to 200 , 0 , 0 
end mouseEnter 

--this changes appearance of polygon back to the default on mouse
-- leave event. Again, note the “me” 

on mouseLeave 
      set the blendLevel of me to 60 
      set the foregroundcolor of me to 255 , 125 , 0 
end mouseLeave 

-- the following function extracts the relevant record from the
-- database of country facts. It looks for a record whose 
-- Code matches the country code representing polygon name. 
-- Then it forms a narration sentence for reading aloud and 
-- returns it back to the mouseUp hander that calls it.

function searchItem cCode    
      set the itemdelimiter to tab 
      repeat for each line myLine in fld "fData" of card \
      "cResources"
            if item 1 of myLine is cCode then 
                  return ( " " && item 2 of myLine & \
         ", its official name is" && \
         item 3 of myLine & "." && item 4 of myLine &&\ 
                  ". Its population is" && item 5 of myLine & \
         ", \
         it covers area of" && item 6 of myLine && \
         "square kilometers. Its capital is " &&\
                  item 7 of myLine & "." && item 8 of myLine) 
                  exit repeat 
            end if       
      end repeat 
end searchItem  

The only remaining task is to populate the database. Luckily, our training stack is already populated with facts about country or region names, official names, status, population size and area coverage in kilometers, also the name of the capital city and a few spicy notes. You can reuse it or add your own facts and narration templates. Enjoy!

About the Author

Viktoras Didziulis currently writes his doctoral dissertation in ecology at the University of Klaipeda, Lithuania. Enjoys involvement in the visual arts. Visit his website here.

 

Main Menu

What's New

Merchandise