playMaker

Author Topic: GUILayout Begin Area follow mouse  (Read 4188 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
GUILayout Begin Area follow mouse
« on: August 09, 2012, 04:39:07 PM »
Hi,

 following a thread, please find a beginArea action that follows the mouse.

http://hutonggames.com/playmakerforum/index.php?topic=2055.msg9186#msg9186

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GUILayout)]
[Tooltip("Begin a GUILayout area that follows the mouse. Useful for overlays (e.g., playerName). NOTE: Block must end with a corresponding GUILayoutEndArea.")]
public class GUILayoutBeginAreaFollowMouse : FsmStateAction
{
[RequiredField]
public FsmFloat offsetLeft;

[RequiredField]
public FsmFloat offsetTop;

[RequiredField]
public FsmFloat width;

[RequiredField]
public FsmFloat height;

[Tooltip("Use normalized screen coordinates (0-1).")]
public FsmBool normalized;

[Tooltip("Optional named style in the current GUISkin")]
public FsmString style;

public override void Reset()
{

offsetLeft = 0f;
offsetTop = 0f;
width = 1f;
height = 1f;
normalized = true;
style = "";
}

public override void OnGUI()
{

// get screen position

Vector2 screenPos = Input.mousePosition;

var left = screenPos.x + (normalized.Value ? offsetLeft.Value * Screen.width : offsetLeft.Value);
var top = screenPos.y + (normalized.Value ? offsetTop.Value * Screen.width : offsetTop.Value);

var rect = new Rect(left, top, width.Value, height.Value);

if (normalized.Value)
{
rect.width *= Screen.width;
rect.height *= Screen.height;
}

// convert screen coordinates
rect.y = Screen.height - rect.y;

GUILayout.BeginArea(rect, style.Value);
}

}
}

bye,

 Jean

Netjera

  • Playmaker Newbie
  • *
  • Posts: 41
Re: GUILayout Begin Area follow mouse
« Reply #1 on: August 30, 2012, 03:11:14 AM »
Thanks for this!  It works awesome, too!