Wow! It's starting to click now!

Ken Ray kray at sonsothunder.com
Sat Feb 18 21:49:18 EST 2006


On 2/18/06 7:05 PM, "Garrett Hylltun" <garrett at paraboliclogic.com> wrote:

> Greetings,
> 
> Well, it seems Rev is starting to click and stick for me now.  I
> actually wrote some code without referring to the docs or looking at
> any examples, or asking for a sample on the list here!
> 
> Garrett's pride and joy code from scratch:
> 
> local varTitleVisualStatus = 1
> local varDateVisualStatus = 1
> 
> on TitleVisualStatusSub
>    -- Title header is height of 24 open, 9 closed
>    if varTitleVisualStatus is 1 then
>      set the visible of field "FieldTitle" to false
>      set the visible of image "FieldTitleClose" to false
>      put 0 into varTitleVisualStatus
>    else
>      set the visible of field "FieldTitle" to true
>      set the visible of image "FieldTitleClose" to true
>      put 1 into varTitleVisualStatus
>    end if
> end TitleVisualStatusSub

Here's something else to learn - the way to write the same code but in less
space so you get more coding done in the same amount of time. :-)

Here's your handler, optimized a bit by providing abbreviations for
properties and object types ("vis" for "visible", "fld" for "field", and
"img" for "image":

on TitleVisualStatusSub
   -- Title header is height of 24 open, 9 closed
   if varTitleVisualStatus is 1 then
     set the vis of fld "FieldTitle" to false
     set the vis of img "FieldTitleClose" to false
     put 0 into varTitleVisualStatus
   else
     set the vis of fld "FieldTitle" to true
     set the vis of img "FieldTitleClose" to true
     put 1 into varTitleVisualStatus
   end if
end TitleVisualStatusSub

And here it is again, rethinking that varTitleVisualStatus can only be 0 or
1, which can also correspond to true (1) or false (0), and seeing that the
absolute value of (the current state - 1) will allow you to flip between 0
and 1:

on TitleVisualStatusSub
   -- Title header is height of 24 open, 9 closed
   set the vis of fld "FieldTitle" to (varTitleVisualStatus = 0)
   set the vis of img "FieldTitleClose" to (varTitleVisualStatus = 0)
   put abs(varTitleVisualStatus - 1) into varTitleVisualStatus
end TitleVisualStatusSub

So if the variable is 0, it sets it to abs(0-1) which is abs(-1) which is 1;
if the variable is 1, it sets it to abs(1-1) which is abs(0), which is 0.

This is the kind of stuff you'll learn to do over time, but I thought I'd
give you some insight on how it's done so you can start to look for these
things in your code as you develop your programming skills.

:-)

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com




More information about the use-livecode mailing list