Game Development Community

PickingToy / Mouse::onHover [Solved]

by Christian · in Torque 2D Beginner · 12/06/2013 (3:22 am) · 5 replies

Hoping someone can point me in the right direction.

Have been studying the PickingToy in my project, and trying to figure out why I haven't been able to get it to work.

When I use the PickingToy in the Sandbox project it all works fine, and I've compared the code from the 'main.cs' in the sandbox Picking Toy, it's the exact same as my pickray.cs code.

I don't see how the toy is ever initialized or created in the sandbox. Whenever I try to use any function of it in my project it says it doesn't exist.

Trying to use this for mouse hover events.

Where am I missing it's initialization line of code?

#1
12/06/2013 (5:20 am)
Can you post your code, please?
#2
12/06/2013 (3:19 pm)
Yeah, it's the exact same code that is in the PickingToy, posted below.

Have searched the entire sandbox project for anywhere that they initialize this or set it up outside of what's in this code, but found nothing.

function PickingToy::create( %this )
{
    echo("Picking Toy Created");
    // Configure the toy.
    PickingToy.PickType = Point; //Point
    PickingToy.PickMode = Any;
    PickingToy.NotPickedAlpha = 0.2;
    PickingToy.PickAreaSize = 10;
    PickingToy.RayStart = $activeSprite.getPosition();

    // Add the configuration options.
    addSelectionOption( "Point,Area,Circle,Ray", "Pick Type", 4, "setPickType", true, "Selects the picking type." );
    addSelectionOption( "Any,OOBB,AABB,Collision", "Pick Mode", 4, "setPickMode", false, "Selects the picking mode." );

    // Force-on debug options.
    setAABBOption(true); //true
    setOOBBOption(true); //true
    setCollisionOption(true); //true
    
    // Reset the toy.
    PickingToy.reset();
}


//-----------------------------------------------------------------------------

function PickingToy::destroy( %this )
{
    // Force-off debug options.
    setAABBOption( false );
    setOOBBOption( false );
    setCollisionOption( false );    
}

//-----------------------------------------------------------------------------

function PickingToy::reset( %this )
{
    echo("Picking Toy Reset");
    // Clear the scene.
    myScene.clear();
       
    // Create background.
    %this.createBackground();
       
    // Create target.
    %this.createTarget();   
    
    // Create pick cursor.
    %this.createPickCursor();
    
    // Create the ray-cast overlay.
    %this.createRaycastOverlay();    
}

//-----------------------------------------------------------------------------

function PickingToy::createBackground( %this )
{    
    echo("Picking Toy Background Created");
    // Create the sprite.
    %object = myScene.create( Sprite );
    %object.BodyType = static;
    %object.Position = "0 0";
    %object.Size = "100 75";
    %object.SceneLayer = 31;
    %object.Image = "ToyAssets:highlightBackground";
    %object.BlendColor = SlateGray;
    %object.PickingAllowed = false;
}

//-----------------------------------------------------------------------------

function PickingToy::createTarget( %this )
{    
    echo("Picking Toy Created Target");
    // Create the sprite.
    %object = myScene.create( Sprite );
    %object.Size = 40;
    %object.Angle = -30;
    %object.Image = "ToyAssets:Tiles";
    %object.Frame = 0;
    %object.setBlendAlpha( PickingToy.NotPickedAlpha );
    // Create some collision shapes.    
    %object.createCircleCollisionShape( 10, "-20 -20" );
    %object.createPolygonBoxCollisionShape( "20 20", "20 20" );
    
    // Set the target object.
    PickingToy.TargetObject = %object;
}

//-----------------------------------------------------------------------------

function PickingToy::createPickCursor( %this )
{
    echo("Picking Toy Created PickCursor");
    // Create the sprite.
    %object = myScene.create( Sprite );
    %object.Size = PickingToy.PickAreaSize;
    %object.BlendColor = Red;
    %object.PickingAllowed = false;
    
    if ( PickingToy.PickType $= "point" || PickingToy.PickType $= "ray" )
    {
        %object.Image = "ToyAssets:CrossHair1";
    }
    else if ( PickingToy.PickType $= "area" )
    {
        %object.Image = "ToyAssets:Blank";
    }
    else if ( PickingToy.PickType $= "circle" )
    {
        %object.Image = "ToyAssets:BlankCircle";
    }
    
    // Set the cursor.
    PickingToy.CursorObject = %object;
}
#3
12/06/2013 (3:51 pm)
Are you adding them to an existing scene, which is attached to a SceneWindow with mouse events enabled?
#4
12/06/2013 (7:33 pm)
I'm not sure. Looking at how the mouse is setup in the sandbox, I don't see it being attached to a scenewindow or where mouse events are enabled (outside of the rotatetoy, that seems to have a lot of mouse stuff setup to track the mouse).

My project has mouse events enabled for active inputs, but not passive hovering events.

#5
12/09/2013 (7:43 pm)
Solved:

Realized the only relevant thing form the PickingToy is it's:
"PickingToy::onTouchMoved". So I just gave that part to my project's InputManager. Very simple once I realized that was the only thing needed from the Toy to make this work.

Here's an example of it:
function InputManager::onTouchMoved(%this, %touchID, %worldPosition)
{
   %picked = myScene.pickPoint(%worldposition);

   for(%i=0; %i < %picked.count; %i++)
   {
      %myobj = getWord(%picked,%i);
      if (%myobj.selectable == 1)
      {
         %myobj.setBlendAlpha( 1.0 );
         return;
      }
   }
    // Target not picked so make it transparent.
    %myobj.setBlendAlpha( 0.25 );
}