The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.3KB

  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. public class %%plugin_class_name%%GUI : IAudioEffectPluginGUI
  7. {
  8. public override string Name { get { return "%%plugin_name%%"; } }
  9. public override string Description { get { return "%%plugin_description%%"; } }
  10. public override string Vendor { get { return "%%plugin_vendor%%"; } }
  11. //==============================================================================
  12. [DllImport("%%plugin_name%%")] static extern System.IntPtr getRenderCallback();
  13. [DllImport("%%plugin_name%%")] static extern void unityInitialiseTexture (int id, System.IntPtr texture, int width, int height);
  14. [DllImport("%%plugin_name%%")] static extern void unityMouseDown (int id, float x, float y, EventModifiers mods, int button);
  15. [DllImport("%%plugin_name%%")] static extern void unityMouseDrag (int id, float x, float y, EventModifiers mods, int button);
  16. [DllImport("%%plugin_name%%")] static extern void unityMouseUp (int id, float x, float y, EventModifiers mods);
  17. [DllImport("%%plugin_name%%")] static extern void unityKeyEvent (int id, KeyCode code, EventModifiers mods, string name);
  18. [DllImport("%%plugin_name%%")] static extern void unitySetScreenBounds (int id, float x, float y, float w, float h);
  19. //==============================================================================
  20. private class PluginGUIInstance
  21. {
  22. public PluginGUIInstance (ref IAudioEffectPlugin plugin, int id)
  23. {
  24. instanceID = id;
  25. float[] arr;
  26. plugin.GetFloatBuffer ("Editor", out arr, 1);
  27. hasEditor = (arr[0] > 0.0f);
  28. }
  29. public void repaint (Rect r)
  30. {
  31. Vector2 newScreenPosition = GUIUtility.GUIToScreenPoint (r.position);
  32. if (bounds != r
  33. || screenPosition != newScreenPosition)
  34. {
  35. screenPosition = newScreenPosition;
  36. bounds = r;
  37. unitySetScreenBounds (instanceID, screenPosition.x, screenPosition.y, bounds.width, bounds.height);
  38. setupTexture();
  39. }
  40. GL.IssuePluginEvent (getRenderCallback(), instanceID);
  41. texture.SetPixels32 (pixels);
  42. texture.Apply();
  43. EditorGUI.DrawPreviewTexture (bounds, texture);
  44. }
  45. public bool handleMouseEvent (EventType eventType)
  46. {
  47. Vector2 mousePos = Event.current.mousePosition;
  48. EventModifiers mods = Event.current.modifiers;
  49. if (! bounds.Contains (mousePos))
  50. return false;
  51. Vector2 relativePos = new Vector2 (mousePos.x - bounds.x, mousePos.y - bounds.y);
  52. if (eventType == EventType.MouseDown)
  53. {
  54. unityMouseDown (instanceID, relativePos.x, relativePos.y, mods, Event.current.button);
  55. GUIUtility.hotControl = GUIUtility.GetControlID (FocusType.Passive);
  56. }
  57. else if (eventType == EventType.MouseUp)
  58. {
  59. unityMouseUp (instanceID, relativePos.x, relativePos.y, mods);
  60. GUIUtility.hotControl = 0;
  61. }
  62. else if (eventType == EventType.MouseDrag)
  63. {
  64. unityMouseDrag (instanceID, relativePos.x, relativePos.y, mods, Event.current.button);
  65. }
  66. Event.current.Use();
  67. return true;
  68. }
  69. public void handleKeyEvent (EventType eventType)
  70. {
  71. if (eventType == EventType.KeyDown)
  72. {
  73. KeyCode code = Event.current.keyCode;
  74. if (code == KeyCode.None)
  75. return;
  76. EventModifiers mods = Event.current.modifiers;
  77. unityKeyEvent (instanceID, code, mods, code.ToString());
  78. }
  79. }
  80. private void setupTexture()
  81. {
  82. if (pixelHandle.IsAllocated)
  83. pixelHandle.Free();
  84. texture = new Texture2D ((int) bounds.width, (int) bounds.height, TextureFormat.ARGB32, false);
  85. pixels = texture.GetPixels32();
  86. pixelHandle = GCHandle.Alloc (pixels, GCHandleType.Pinned);
  87. unityInitialiseTexture (instanceID, pixelHandle.AddrOfPinnedObject(), texture.width, texture.height);
  88. }
  89. public int instanceID = -1;
  90. public bool hasEditor;
  91. private Vector2 screenPosition;
  92. private Rect bounds;
  93. private Texture2D texture;
  94. private Color32[] pixels;
  95. private GCHandle pixelHandle;
  96. }
  97. List<PluginGUIInstance> guis = new List<PluginGUIInstance>();
  98. private PluginGUIInstance getGUIInstanceForPlugin (ref IAudioEffectPlugin plugin)
  99. {
  100. float[] idArray;
  101. plugin.GetFloatBuffer ("ID", out idArray, 1);
  102. int id = (int) idArray[0];
  103. for (int i = 0; i < guis.Count; ++i)
  104. {
  105. if (guis[i].instanceID == id)
  106. return guis[i];
  107. }
  108. PluginGUIInstance newInstance = new PluginGUIInstance (ref plugin, id);
  109. guis.Add (newInstance);
  110. return guis[guis.Count - 1];
  111. }
  112. //==============================================================================
  113. public override bool OnGUI (IAudioEffectPlugin plugin)
  114. {
  115. PluginGUIInstance guiInstance = getGUIInstanceForPlugin (ref plugin);
  116. if (! guiInstance.hasEditor)
  117. return true;
  118. float[] arr;
  119. plugin.GetFloatBuffer ("Size", out arr, 6);
  120. Rect r = GUILayoutUtility.GetRect (arr[0], arr[1],
  121. new GUILayoutOption[] { GUILayout.MinWidth (arr[2]), GUILayout.MinHeight (arr[3]),
  122. GUILayout.MaxWidth (arr[4]), GUILayout.MaxHeight (arr[5]) });
  123. int controlID = GUIUtility.GetControlID (FocusType.Passive);
  124. Event currentEvent = Event.current;
  125. EventType currentEventType = currentEvent.GetTypeForControl (controlID);
  126. if (currentEventType == EventType.Repaint)
  127. guiInstance.repaint (r);
  128. else if (currentEvent.isMouse)
  129. guiInstance.handleMouseEvent (currentEventType);
  130. else if (currentEvent.isKey)
  131. guiInstance.handleKeyEvent (currentEventType);
  132. return false;
  133. }
  134. }
  135. #endif