oddgames / UIToolkit

Single draw call UI solution for Unity with multi resolution support and more.
518 stars 153 forks source link

Inline Mouse Over effect #22

Closed namoricoo closed 12 years ago

namoricoo commented 13 years ago

/* I added a couple of functions for the mouse over effect. So if you have a 3 state button like, var playButton = UIButton.create( "playUp.png","playOver.png", "playDown.png", 0, 0 ); This will work work correctly without having to do the current hack that's in the Kitchen sink tutorial.

*/

using UnityEngine;

public class UIButton : UITouchableSprite { public delegate void UIButtonTouchUpInside( UIButton sender ); public event UIButtonTouchUpInside onTouchUpInside;

public delegate void UIButtonTouchDown( UIButton sender );  
public event UIButtonTouchDown onTouchDown; 

public UIUVRect highlightedUVframe;
public AudioClip touchDownSound;

#region Constructors/Destructor

#region Three State Button

public static UIButton create( string filename,string mouseOver,string highlightedFilename, int xPos, int yPos )
{
    return UIButton.create( UI.firstToolkit, filename,mouseOver,highlightedFilename, xPos, yPos );
}

public static UIButton create( UIToolkit manager, string filename,string mouseOver, string highlightedFilename, int xPos, int yPos )
{
    return UIButton.create(manager, filename,mouseOver,highlightedFilename, xPos, yPos, 1 );
}

public static UIButton create( UIToolkit manager, string filename,string mouseOver, string highlightedFilename, int xPos, int yPos, int depth )
{
    // grab the texture details for the normal state
    var normalTI = manager.textureInfoForFilename( filename );
    var frame = new Rect( xPos, yPos, normalTI.frame.width, normalTI.frame.height );

if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER

    var hoveredTI=manager.textureInfoForFilename(mouseOver);
    var highlightedTITwo = manager.textureInfoForFilename( highlightedFilename);

    return new UIButton(manager,frame,depth,normalTI.uvRect,hoveredTI.uvRect,highlightedTITwo.uvRect);

else //This code for for mobile platforms.

    var highlightedTI = manager.textureInfoForFilename( highlightedFilename);
    return new UIButton( manager, frame, depth, normalTI.uvRect, highlightedTI.uvRect);

endif

}

public UIButton( UIToolkit manager, Rect frame, int depth, UIUVRect uvFrame,UIUVRect hoveredUVframe,UIUVRect highlightedUVframe ):base( frame, depth, uvFrame )
{       
    // If a highlighted frame has not yet been set use the normalUVframe
    if( highlightedUVframe == UIUVRect.zero )
    {   
        highlightedUVframe = uvFrame;           
    }
    else if(hoveredUVframe == UIUVRect.zero )
    {
        hoveredUVframe=uvFrame;         
    }
    this.hoveredUVframe =hoveredUVframe;        
    this.highlightedUVframe = highlightedUVframe;   
    manager.addTouchableSprite( this );
}

#endregion

public static UIButton create( string filename, string highlightedFilename, int xPos, int yPos )
{
    return UIButton.create( UI.firstToolkit, filename, highlightedFilename, xPos, yPos );
}

public static UIButton create( UIToolkit manager, string filename, string highlightedFilename, int xPos, int yPos )
{
    return UIButton.create( manager, filename, highlightedFilename, xPos, yPos, 1 );
}

public static UIButton create( UIToolkit manager, string filename, string highlightedFilename, int xPos, int yPos, int depth )
{
    // grab the texture details for the normal state
    var normalTI = manager.textureInfoForFilename( filename );
    var frame = new Rect( xPos, yPos, normalTI.frame.width, normalTI.frame.height );

    // get the highlighted state
    var highlightedTI = manager.textureInfoForFilename( highlightedFilename );

    // create the button
    return new UIButton( manager, frame, depth, normalTI.uvRect, highlightedTI.uvRect );
}

public UIButton( UIToolkit manager, Rect frame, int depth, UIUVRect uvFrame, UIUVRect highlightedUVframe ):base( frame, depth, uvFrame )
{
    // If a highlighted frame has not yet been set use the normalUVframe
    if( highlightedUVframe == UIUVRect.zero )
        highlightedUVframe = uvFrame;

    this.highlightedUVframe = highlightedUVframe;

    manager.addTouchableSprite( this );
}

#endregion;

// Sets the uvFrame of the original UISprite and resets the _normalUVFrame for reference when highlighting
public override UIUVRect uvFrame
{
    get { return _uvFrame; }
    set
    {
        _uvFrame = value;
        manager.updateUV( this );
    }
}

public override bool highlighted
{
    set
    {
        // Only set if it is different than our current value
        if( _highlighted != value )
        {           
            _highlighted = value;

            if ( value )
                base.uvFrame = highlightedUVframe;
            else
                base.uvFrame = _tempUVframe;
        }
    }
}

// Touch handlers

if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER

public override void onTouchBegan( UIFakeTouch touch, Vector2 touchPos )

else

public override void onTouchBegan( Touch touch, Vector2 touchPos )

endif

{
    highlighted = true;
    /*
    initialTouchPosition = touch.position;
    */
    if( touchDownSound != null )
        UI.instance.playSound( touchDownSound );

    if( onTouchDown != null )
        onTouchDown( this );

}

if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER

public override void onTouchEnded( UIFakeTouch touch, Vector2 touchPos, bool touchWasInsideTouchFrame )

else

public override void onTouchEnded( Touch touch, Vector2 touchPos, bool touchWasInsideTouchFrame )

endif

{
    highlighted = false;

    // If the touch was inside our touchFrame and we have an action, call it
    if( touchWasInsideTouchFrame && onTouchUpInside != null )
        onTouchUpInside( this );
}

}

acron0 commented 12 years ago

Please submit this as a pull request. As you can see, GitHub has butchered it.