I thought GetTypeForControl(id) returns type (not Ignore value) only if GUIUtility.hotControl == id
.
But I see that GetTypeForControl(id) also returns type for controls with lower id.
For example:
let GUIUtility.hotControl = 100;
GetTypeForControl will return type for id 90 and id 100. But not for 101.
You can see it on this video: - YouTube
Code:
public static void Draw(Rect position, Texture texture, Material material, Transform trans) {
Graphics.DrawTexture( position, texture, material );
using(new GUI.ClipScope( position )) {
position.position = Vector2.zero;
Matrix4x4 coordSystem = TranslateScaleMatrix( new Vector2(position.x, position.height), new Vector2(position.width, -position.height) );
DoViewer( position, trans, coordSystem.inverse );
}
}
private static void DoViewer(Rect position, Transform trans, Matrix4x4 coordSystem) {
int id = GUIUtility.GetControlID( position.GetHashCode(), FocusType.Passive );
EventType type = Event.current.GetTypeForControl(id);
if(type.IsMouseDown(1)) {
GUIUtility.hotControl = id;
Event.current.Use();
}
if(type.IsMouseUp(1)) {
GUIUtility.hotControl = 0;
Event.current.Use();
}
if(type.IsMouseDrag(1)) {
Debug.Log( "hot: "+GUIUtility.hotControl+" id: "+id );
Vector2 delta = Event.current.delta;
delta = coordSystem.MultiplyVector(delta);
trans.position += (Vector3) delta;
GUI.changed = true;
Event.current.Use();
}
if(type.IsScrollWheel()) {
Vector2 mouse = Event.current.mousePosition;
mouse = coordSystem.MultiplyPoint(mouse);
Vector2 pos1 = trans.InverseTransformPoint( mouse );
if(Event.current.delta.y < 0) trans.localScale *= 1.15f;
if(Event.current.delta.y > 0) trans.localScale /= 1.15f;
Vector2 pos2 = trans.InverseTransformPoint( mouse );
var delta = trans.TransformVector(pos2 - pos1);
trans.position += (Vector3) delta;
GUI.changed = true;
Event.current.Use();
}
}
This is code of GetTypeForControl from Unity 3. In last version of Unity this code is moved to native.
public EventType GetTypeForControl(int controlID)
{
if (GUIUtility.hotControl == 0)
{
return this.type;
}
switch (this.m_Type)
{
case EventType.MouseDown:
case EventType.MouseUp:
case EventType.MouseMove:
case EventType.MouseDrag:
if (!GUI.enabled)
{
return EventType.Ignore;
}
if (GUIClip.enabled || GUIUtility.hotControl == controlID)
{
return this.m_Type;
}
return EventType.Ignore;
case EventType.KeyDown:
case EventType.KeyUp:
if (!GUI.enabled)
{
return EventType.Ignore;
}
if (GUIClip.enabled || GUIUtility.hotControl == controlID || GUIUtility.keyboardControl == controlID)
{
return this.m_Type;
}
return EventType.Ignore;
case EventType.ScrollWheel:
if (!GUI.enabled)
{
return EventType.Ignore;
}
if (GUIClip.enabled || GUIUtility.hotControl == controlID || GUIUtility.keyboardControl == controlID)
{
return this.m_Type;
}
return EventType.Ignore;
default:
return this.m_Type;
}
}