Script Lesson 15

download Script Lesson 15

of 4

Transcript of Script Lesson 15

  • 7/29/2019 Script Lesson 15

    1/4

    Home TI-Nspire Authoring TI-Nspire Scripting HQScripting Tutorial - Lesson 15

    Scripting Tutorial - Lesson 15: (Advanced) Using Mouse Controls withMultiple Classes

    Download supporting files for this tutorial

    Texas Instruments TI-Nspire Scripting Support Page

    In lesson 14 we saw how to use Keyboard controls to select and workwith multiple class objects. In this final lesson, we extend our mousecontrols in the same way. Most of what we need has already been putin place using the TrackedObject variable. All that is really needed is

    to generalize the mouseDown function to work with out Objectstable rather than the specific case of the Square previously defined.

    We need tostudy thevarious parts ofthemouseDownfunction closelyin order tounderstandwhat ishappening

    here.

    The functionbegins just astabKey did,with a loopthrough theobjects in theObjects table,allocating alocal variableobj to each inturn. As we

    might expectfrom previousmouse work, ifthe mousedown occurswithin aparticularobject (usingthe containsfunction) thenthat becomesour selectedobject to be

    Tracked.

    As before, firstrelease it if theobject was

    function on.mouseDown(x,y)

    for i = #Objects, 1, -1 do

    local obj = Objects[i]

  • 7/29/2019 Script Lesson 15

    2/4

    alreadyselected so weknow whatstate we are in.Then we defineTrackedObjectas obj, andindicate that itis selected.

    The next two

    lines define twonew globalvariables calledTrackOffsetxandTrackOffsety.You may havenoticed that ifwe click on anobjectsomewhereother than thecenter, the

    object isinclined tojump to placethe centerunder themouse. TheseTrackedOffsetvalues serve toavoid this littlejump. Theyshould initiallybe defined aszero in our

    on.resizefunction sothat they existwhen the pageis created andchanged.

    Back totrackingmultipleobjects. Thetable.removeand table.insert

    commandsserve to releaseany previousselected objectand replacewith thecurrent one -essentiallymoving it toprime positionin the table.This has theeffect desired -

    the objectbeing clickedon becomesthe selected(Tracked)

    if obj:contains(x, y) then

    if TrackedObject ~= nil then

    TrackedObject.selected= false

    end

    TrackedObject = obj

    obj.selected = true

    TrackOffsetx = TrackedObject.x- x

    TrackOffsety = TrackedObject.y -y

    table.remove(Objects, i)

    table.insert(Objects, obj)

    platform.window:invalidate()

    break

    end

    end

    end

    function on.mouseUp(x,y)

    if TrackedObject ~= nil then

    TrackedObject.selected = false

    end

    TrackedObject = nil

    end

    function on.mouseMove(x,y)

    if TrackedObject ~= nil then

    TrackedObject.x = x + TrackOffsetx

    TrackedObject.y = y + TrackOffsety

    platform.window:invalidate()

    end

    end

  • 7/29/2019 Script Lesson 15

    3/4

    object and all iswell.

    Only one otherchange is madeto mouseMoveand that issimply toinclude theTrackOffsetvalues referred

    to previously.

    Copy this codeintoscript_tut14.luaand you nowhave completedyour mission.Two objectsmay now beselected andcontrolled byeither mouse

    or keyboard!

    I would suggestthat you nowextend thisscript toinclude otherobjects. Simplydefine theseand then inserttheir definition

    into the Objectstable and youwill be able tocontrol themjust as easily asyou control thesquare and thecircle. Multipleimages willwork just aswell.

    Time then,

    perhaps, to goback to theShape Patternsdocumentfeatured inlesson 11 andthink abouthow you maynow create yourown rich andpowerfuldocumentswhich work

    equally wellwith bothhandheld andcomputer.

    Click to see what such a document might look like

  • 7/29/2019 Script Lesson 15

    4/4

    Congratulationson completingthis series oflessons!

    Back to Top

    Home TI-Nspire Authoring TI-Nspire Scripting HQScripting Tutorial - Lesson 15