Computer Science Canada

Clicking Module

Author:  Aange10 [ Sun Dec 25, 2011 6:54 pm ]
Post subject:  Clicking Module

I absolutely hate Turing's built in clicking detection. The buttonmoved function is terrible. Honestly, its results feels random at best. It is probably me using it wrong, but I don't care. It's too complicated, and the documentation sucks. So I made my own.

It's VERY simple to use. Firstly, you need to know the the Turing Walkthrough has a fabulous Module guide if you get stuck.

With that bit out of the way, here is how you use the module:

You need to copy the module (At the bottom) and save it to a file in the same folder as where you'll be using it. You have to name it "DetectClick.tu". Make sure it is .tu and not .t. (There are ways to import the module without doing this. However, I will not cover those ways as they are covered in the tutorial) Now, in the file you are using it in, at the top, on very FIRST line you need to type import DetectClick.

Whoo! You now have the module imported to your program! Now using it is the simple part. The module has two functions that output TRUE if a click has happened, and FALSE if a click has not happened. The syntax for using these functions are easy; here is an example:

Turing:

import DetectClick
loop
    var mouseX, mouseY, mouseB : int
    Mouse.Where (mouseX, mouseY, mouseB)
     if DetectClick.Click (mouseB, "left") = true then
         put "You've clicked!"
     else
         put "You have not clicked."
    end if
end loop


The module's two function's syntax's are as followed:

Click (mouseState : int, buttonToCheck : string)
QuickClick (mouseState : int, buttonToCheck : string)

The accepted terms for buttonToCheck are "left", "right", "middle", "null" and "nill". Using any other string (NOT case sensitive) will result in an error, telling you your problem.
The difference between Click and QuickClick is that a QuickClick will only detect a click if they press and release the mouse within 500ms, while Click does not have that restriction.

* If you have trouble feel free to ask for help, but REFER to the tutorial on Modules before doing so.
Remmember to call the functions of the module you must put "DetectClick." before it.


Here is the Module:

Turing:
unit

module DetectClick

    export QuickClick, Click
   
    var lTimer, mTimer, rTimer : int := 0
    var QlTimer, QmTimer, QrTimer : int := 0
   
   
    fcn QC (mb : int, state : int, var timer : int) : boolean
   
        if mb = state then

            % Detect if its the first click
            if timer = 0 then
                timer := Time.Elapsed ()
            end if

        else

            % Check mouse timer and see if < 500 ms has passed
            if Time.Elapsed () - timer <= 500 then
                % Click
                timer := 0
                result true
            else
                % Reset if they released after 500 ms
                timer := 0
            end if

        end if
        result false
       
    end QC
   
    fcn C (mb : int, state : int, var timer : int) : boolean
    if mb = state then

            % Detect if its the first click
            if timer = 0 then
                timer := Time.Elapsed ()
            end if

        else

            % They are ending a click
            if timer not= 0 then
                timer := 0
                result true
            end if

        end if

    end C
   
    fcn QuickClick ( mb : int, button : string) : boolean
        var state : int

        % See what we are looking for
        if Str.Lower (button) = "left" then
            state := 1
            result QC (mb, state, QlTimer)
        elsif Str.Lower (button) = "right" then
            state := 100
            result QC (mb, state, QrTimer)
        elsif Str.Lower (button) = "middle" then
            state := 10
            result QC (mb, state, QmTimer)
        elsif Str.Lower (button) = "nill" or Str.Lower (button) = "null" then
            result false
        else
            cls
            put "ERROR IN CLICK FUNCTION. Appropriate strings are \"left\", \"right\", \"middle\", \"null\", or \"nill\"."
            View.Update
            delay (1000)
            result false
        end if
    end QuickClick



    fcn Click ( mb : int, button : string) : boolean
        var state : int
        % See what we are looking for
        if Str.Lower (button) = "left" then
            state := 1
            result QC (mb, state, lTimer)
        elsif Str.Lower (button) = "right" then
            state := 100
            result QC (mb, state, rTimer)
        elsif Str.Lower (button) = "middle" then
            state := 10
            result QC (mb, state, mTimer)
        elsif Str.Lower (button) = "nill" or Str.Lower (button) = "null" then
            result false
        else
            cls
            put "ERROR IN CLICK FUNCTION. Appropriate strings are \"left\", \"right\", \"middle\", \"null\", or \"nill\"."
            View.Update
            delay (1000)
        end if

    end Click
end DetectClick



AT THE BOTTOM I have included a working example of the module.


: