Objects (also called "Controls")

1. Objects/Controls in LiveCode - What are they?

Objects - In LiveCode, as well as in other object-oriented languages (Java), you have many types of objects:

     1. Buttons – to create events (e.g. Push button, on/off, )
     2. Fields – to show or enter data in (e.g. text entry - name, address, etc)
     3. Scrollbars – to set an amount (e.g. size of image)
     4. Graphics – basic objects or ones drawn by hand (e.g. box, circle, etc)
     5. Images – usually photos, etc (e.g. sunset.jpg)
     6. Players – for videos, sound, etc (e.g. clapping.wav, YouTube videos, etc.)

In LiveCode we also have

                Groups – a collection of objects together (e.g. keys of a keyboard, a set of buttons )
                Cards – essentially a window, screen or level in a game
                Stacks – many cards grouped together to make a program

All controls have names. LiveCode gives them a default name (like “Button”) and a unique ID.  We refer to them in the code by both – object type and their name:

                Graphic   “Rectangle”
                Field   “Text Entry Field”
                Label    “Label”
                Image   “Line”
                Button   “Button”

We can give them names that we like better. To change a name, click on the object then open up it’s ‘Property Inspector’. (Besides changing the name, you can also change the “Label”)

                Graphic   “myBox”
                Field   “First Name”
                Image   “Sunset”
                Button   “Push Me”

When we refer to them we have to include both the type and the name because you can have two different objects with the same name.

It is just like if you have a friend named “Sam” and your dog is also named “Sam” So if you are talking about Sam, you have to be clear which one you are talking about.  If you say “Sam eats food off the floor” you better not mean your friend “Sam”.  You need to specify if you mean your friend or your dog.  “My dog Sam is so funny” or “My friend Sam is really nice”.  

Likewise, in LiveCode we have to include both the object and its name

                set the top of  the  graphic  “mybox”   to the top of the  card  “mycard”
                put “Sally” into the  field  “Name”
                set the loc of the  button “myBox”  to 100,100
                move the image “ball” to 250,32
                hide the graphic “box”


            Controls

                But we can not call a group of objects - "objects" or stacks - "objects". So we use the term "Controls" which covers everything

                So from now on may will try to use the name Controls instead of Objects

2. Controls (Objects) - Identifying Them 

We know that objects are identified by their "Type" and "Name". 
e.g. 
    button "box"
    image "sunset"
    field "Name"
    button "b1"
    
But there are different forms for their name, depending on how much information that you need about it.
Suppose we have a button called "box"
It has a 
    name = button "box"
    short name = "box"
    long name = button "box" of card id 1002 of stack "Untitled 1"

You can see this for yourself by adding the following script to your card
    on mouseDown
       answer "the name is: " & the name of the target 
       answer "the short name is:  " & the short name of target  
       answer "the long name is: " & the long name of target  
    end mouseDown
and clicking on different objects on your card. You will see the differences.



Advanced Topics:


3. LiveCode Controls (Objects) and Their Properties

    1. Buttons - for performing actions
           A. Push Buttons - to perform an action
             Properties:

                  Styles = Push, Square, Rounded Rectangle, Transparent, Opaque and Shadow - to perform an action
                  Hilite = the property that it has been pressed and is true/false
                    if the hilite of button "Go" then             
                   
        (it has been pressed)
                    end if

                  Use: Usually, you have script on the button to detect when it has been pushed i.e. on mouseUp.
                      on mouseUp
                        (it has been pressed)
                      end mouseUp

                  But if you have many buttons on a card, you can use the "hilite" property in another handler

                       on mouseUp
                            
if the hilite of button "Go" then
                                    (it has been pressed)
                            end if
                            if the hilite of button "Go2" then
                                    (it has been pressed)
                            end if
                            ...
                       end mouseUp

               note: This is good because all your code and ligic is in one place (on the card) and not scattered across many buttons

          B - Menu Buttons - for giving menu choices
          C - Radio Buttons - for making 1 choice of many

              Properties:
                    Styles = Radio button and Check Box - to make choices (the radio buttons are usually used in groups)

                   But if you have many buttons on a card, you can use the "hilite" property in another handler

                       on mouseUp
                            
if the hilite of button "Go" then
                                    (it has been pressed)
                            end if
                            if the hilite of button "Go2" then
                                    (it has been pressed)
                            end if
                            ...
                       end mouseUp

            D - Checkbox Buttons - for checking on/off (true or false)

              Properties:
                   Styles = Radio button and Check Box - to make choices (the radio buttons are usually used in groups)

                 But if you have many buttons on a card, you can use the "hilite" property in another handler

                       on mouseUp
                            
if the hilite of button "Go" then
                                    (it has been pressed)
                            end if
                            if the hilite of button "Go2" then
                                    (it has been pressed)
                            end if
                            ...
                       end mouseUp


            E - Menu Buttons - for selecting one of many choices
               Properties:
                   Styles = menu
                   Types = option menu, pull Down menu, pop Up menu, Combo Box
                   menuMode = also the kind of menu
                   Text = the choices available
                   Label = currently chosen menu item
                   menuHistory = the line number of the chosen item     

                Use: getting the choice
                        on menuPick x
                            put x into theChoice
                        end menuPick

        2. Fields - to display or enter text 

                 Name = Field, Label Field, Scrolling, Scrolling List, Table text, list, table fields)
                            Table field - each column separated with a tab, each row by return
                            Scrolling List Fields - getting the choice , each choice is on a separate line 

                Use:
                        on mouseUp
                            put the selected text of me into theChoice
                        end mouseUp

        Data Grids - for displaying data in obth grid and form view as well as custom layouts with other LiveCode objects. Best for large data sets.


4. LiveCode Shortcuts - the "ID", the "short name" , the "name" and the "long name"

LiveCode has ways to identify objects: The "ID", the  "short name", and the "long name"

    1. ID - The ID is the number of that object. ID's are always assigned by LiveCode and are always unique. No 2 objects will have the same ID. (You use this when you skin buttons - you use the ID for the icon. 

        e.g.    1004

    2. short name - The "short name" is the name of the object. 

        e.g.   "box" or "enemy" 
        or     "square"

    3. name or abbrev name - The "name" or abbreviated name is the type and name of the object. 

        e.g.   button "box" 
        or     image "enemy" 
        or     graphic "square"

    4. long name - The long name is the complete description (or location) of the object (the card and stack that it is in)

        e.g.   button "box" of card "myCard" of stack "My Stack"   
         or     button "B" of card id 1375 of stack "/Users/admin/Downloads/school app.livecode" 


    For You To Try

    1. Make a new card, put a button on it and call the button "box. Then add the following code to the button:

            on mouseUp
                    answer the id of me
                    answer the short name of me
                    answer the long name of me
            end mouseUp

        When you click on it, you will see (in order)

                1004        (or whatever the ID # is of that object)
                Box         (the "short" name of this object)
                button "Box" of card "card id 1002 of stack "untitled"   

        In the long name, notice every object has a "type" and a "name" - button "box",  card "card id 1002", stack "untitled")

    2. Delete that code on the button and add the following code to the CARD script:

                on mouseUp
                        answer "The ID: is" && the id of the target
                        answer "Short Name: && the short name of the target
                        answer "Long Name:" && the long name of target
                end mouseUp

       now drag any objects from the "Tools Palette" to your card and click on them one by one. You will see what each is.

        You can refer to the object using any of them:

                hide button ID theID
                hide button "theShortName"
                hide button "theLongName"


5. When would we use this? - answer: when you have a large number of buttons, etc

Good question. Let's say you were making something with many buttons - a calculator, a keyboard, a memory game or anything else with many buttons. It would be tedious putting the same code on every button or object. Then if you would make a change, you would have to edit each one at a time.

Wouldn't it be easier just putting 1 copy of the code on the card script and have it see which button was pressed? 

Yes, for instance a calculator. You would have 10 buttons for the numbers (0 to 9) and normally would put code on each one. 

    On button "B1" - the button with the "1" on it

       on mouseUp
                put "1" into field "number"
       end mouseUp

    On button "B2" - the button with the "2" on it

       on mouseUp
                put "2" into field "number"
       end mouseUp
 

(note: we used "B1" for the name and "1" for the label. It is not a good idea to name any object with just a number. LiveCode does not know id you mean the ID or the name of the object. So instead we include a "B" before each number - as the name)

Instead, you can just do one script on the card 

    on the CARD Script

        on mouseUp
                
put char 2 of the short name of the target into the field "number"
        end mouseUp

A little more complicated (not really if you understand it) but a lot less work. You are done. You do not have to do any more scripts

        

        ...more to come

Comments