LiveCode
  • Why LiveCode?
  • Customers
  • Resources
  • Education
  • Consulting
  • Pricing
  • Blog
  • Login
  • Try LiveCode
Back to
  • slides
  • tutorial
Resources

The OnCreate Handler

Topic Progress:
← Back to Lesson

The OnCreate handler

The OnCreate handler is sent to a widget when it is first created by LiveCode.

This handler can be used to initialise default data and, where applicable, reduce the burden for calculating constants etc in the OnPaint handler.

Add the OnCreate handler to the LCB file

  • Define a variable, tSectors, to hold the number of sectors
  • Call setValues to set the default values of the mValues variable.
  • Call setLabels to set the default values of the mLabels variable
  • Set the value of mShowLabels to true
  • Update tSectors with the number of elements in the list of values
  • Call colorList to intitialise the mColors variabler

Setting the property values in the OnCreate handle ensures the values of the properties are set when we test from the Extension Builder.

widget community.livecode.elanorb.piechart

...previous code

public handler OnCreate()
   variable tSectors

   setValues("1,2,3,5,8,13")
   setLabels("Jan,Feb,Mar,Apr,May,Jun")
   put true into mShowLabels

   put the number of elements in mValues into tSectors
   put colorList(tSectors) into mColors

end handler

end widget

LiveCode Builder Colors

LiveCode Builder colors are expressions which evaluates to a list of 3 or 4 numbers, the red, green, blue, and (optional) alpha components of the color.

The component value denotes the intensity of that component, expressed as a real number between 0 and 1. The alpha component represents the opacity of the color. If the alpha component is not specified then it is assumed to be 1 (fully opaque).

variable tColor
-- Set tColor to opaque red
put color [1.0, 0.0, 0.0] into tColor

-- Set tColor to partially transparent cyan
put color [0.0, 1.0, 1.0, 0.75] into tColor

The colorList handler

The colorList handler returns a list of unique colors for use when coloring the sectors of the chart. The number of colors to be returned is given by the pNumber parameter.

The handler loops calculating RGB values for colors.

  • Highest intensity primary colors
  • Highest intensity secondary colors
  • Reduce the color intensity by half

Each iteration adds 6 colors to the list.

  1. R,0,0,1
  2. 0,G,0,1
  3. 0,0,B,1
  4. R,G,0,1
  5. R,0,B,1
  6. 0,G,B,1

colors

Add the colorList handler to the source file.

  • Declare the variables that will be used.
  • Assign a value to tColorLevel: the color intensity to be used.
  • Calculate the number of repeats
    • Divide pNumber by 6, 6 colors are add to the color list on each iteration of the loop
    • Round the result
    • Add 1 to the result to ensure we always calculate enough colors
public handler colorList(in pNumber) returns List
   variable tColors as List   
   variable tColorLevel as Number
   variable tRepeats as Number

   put 1 into tColorLevel
   put pNumber / 6 into tRepeats
   round tRepeats
   add 1 to tRepeats

  … continues on next slide
end handler
  • Repeat tRepeats times
    • Push 6 colors to the tColors list
      • tColorLevel,0,0,1
      • 0,tColorLevel,0,1
      • 0,0,tColorLevel,1
      • tColorLevel,tColorLevel,0,1
      • tColorLevel,0,tColorLevel,1
      • 0,tColorLevel,tColorLevel,1
    • Divide tColorLevel by 2
  • Return the list of colors
public handler colorList(in pNumber) returns List

… previous code
   repeat tRepeats times
       push color [tColorLevel,0,0] onto tColors
       push color [0,tColorLevel,0] onto tColors
       push color [0,0,tColorLevel] onto tColors
       push color [tColorLevel,tColorLevel,0] onto tColors
       push color [tColorLevel,0,tColorLevel] onto tColors
       push color [0,tColorLevel,tColorLevel] onto tColors

       divide tColorLevel by 2
   end repeat
   return tColors
end handler

← Previous Topic

← Previous Topic Next Topic →
  •  
  •  
  • (0)
  •  

Expand All | Collapse All
Lessons Status
1 - Hello World Library
  • Introduction
  • Installing a Text Editor
  • Creating an LCB library
  • LCB library definition
  • LCB handler definitions
  • Compiling and testing a library in LiveCode
  • LCB metadata
  • Installing an LCB Library
  • Packaging and sharing an LCB library
  • Including an LCB library in a standalone
  • Conclusion
2 - Extended Hello World Library
  • Introduction
  • Explicit Types
  • LCB Lists and Arrays
  • Type Conversion between LCB and LiveCode Script
  • Using Explicit Typing in the Library
  • Passing Parameters to the Library
  • Using Lists in the Library
  • Documenting the Library
  • Browsing the Documentation
  • Conclusion
3 - Rotated Text Widget
  • Introduction
  • Defining a Widget
  • LCB Module Level Variables
  • Widget Properties
  • Widget Handlers
  • LCB Canvas Operations
  • Compiling and Testing the Widget
  • Installing the Widget and adding it to a Stack
  • Including the Widget in a Standalone
  • Conclusion
4 - Extended Rotated Text Widget
  • Introduction
  • Integrating Properties
  • Accessing LiveCode Control Properties
  • Handling Mouse Events
  • Saving and Loading Widgets
  • Read Only Properties
  • Documenting the Widget
  • Conclusion
5 - Pie Chart Widget
  • Introduction
  • Planning the Widget
  • Widget Properties
  • The OnCreate Handler
  • The OnPaint Handler
  • Responding to Mouse Events
  • Saving and Loading the Widget
  • Installing and Including the Widget in a Standalone
  • Documenting the Widget
  • Conclusion
6 - Modifying the Line Graph Widget
  • Introduction
  • Duplicating the Line Graph Widget
  • Additional Color Properties
  • Filling the area below the line
  • Adding graph marker mouse events
  • Conclusion
7 - Additional Resources
  • Extending LiveCode Guide
  • LiveCode Builder Language Reference
  • LiveCode Builder Style Guide
  • LiveCode Documentation Format
 

LCB Source

LCB Source
 

Login

LiveCode

  • Why LiveCode?
  • Pricing
  • Customer Stories
  • Extensions
  • LiveCode in Education
  • LiveCode in Business

Looking for LiveCode FileMaker?

LiveCode for FM

Resources

  • Docs
  • API (Language Dictionary)
  • Lessons
  • Sample Stacks
  • Forums
  • Stackoverflow
  • User Groups
  • Support
  • Directory
  • LiveCode Services

About

  • About
  • Meet The Team
  • Careers at LiveCode
  • Contact us

Recent Posts

  • LiveCode Classic 10.0.1 Stable and 10.0.2-rc-1 Released
  • Create Progress – the Three D’s
  • LiveCode Create: Closing out the year with big steps forward
  • Create dp-5: Unified Engines and Shared Extensions
  • 3 Great New LiveCode Releases
Sitemap
Terms Privacy Policy EULA
Brought to you by LiveCode Ltd, Registered in Scotland, No. SC200728
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok