ContextPalette.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. public sealed class ContextPalette : PaletteParent
  9. {
  10. private Vector3 m_position;
  11. private Vector2 m_startDropPosition;
  12. public ContextPalette( AmplifyShaderEditorWindow parentWindow ) : base( parentWindow, 0, 0, 250, 250, string.Empty, MenuAnchor.NONE, MenuAutoSize.NONE )
  13. {
  14. m_isActive = false;
  15. OnPaletteNodeCreateEvt += OnOptionSelected;
  16. m_searchFilterControl += "CONTEXTPALETTE";
  17. }
  18. public override void OnEnterPressed(int index = 0)
  19. {
  20. if ( m_searchFilter.Length > 0 && m_currentItems.Count > 0 )
  21. {
  22. FireNodeCreateEvent( m_currentItems[ index ].NodeType, m_currentItems[ index ].Name, m_currentItems[ index ].Function );
  23. }
  24. else
  25. {
  26. Disable();
  27. }
  28. }
  29. public override void OnEscapePressed()
  30. {
  31. Disable();
  32. if ( m_parentWindow.WireReferenceUtils.ValidReferences() )
  33. {
  34. m_parentWindow.WireReferenceUtils.InvalidateReferences();
  35. }
  36. }
  37. public override void Draw( Rect parentPosition, Vector2 mousePosition, int mouseButtonId, bool hasKeyboadFocus )
  38. {
  39. //if ( !_isActive )
  40. // return;
  41. if ( Event.current.type == EventType.MouseDown && !IsInside( Event.current.mousePosition ) )
  42. {
  43. Disable();
  44. return;
  45. }
  46. base.Draw( parentPosition, mousePosition, mouseButtonId, hasKeyboadFocus );
  47. }
  48. public void Show( Vector2 position, Rect cameraInfo )
  49. {
  50. m_startDropPosition = position;
  51. m_maximizedArea.x = ( position.x + m_maximizedArea.width ) > cameraInfo.width ? ( cameraInfo.width - 1.1f * m_maximizedArea.width ) : position.x;
  52. m_maximizedArea.y = ( position.y + m_maximizedArea.height ) > cameraInfo.height ? ( cameraInfo.height - 1.1f * m_maximizedArea.height ) : position.y;
  53. m_position = new Vector3( m_maximizedArea.x, m_maximizedArea.y, 0f );
  54. m_isActive = true;
  55. m_focusOnSearch = true;
  56. }
  57. // This override is removing focus from our window ... need to figure out a workaround before re-using it
  58. //public override bool CheckButton( GUIContent content, GUIStyle style, int buttonId )
  59. //{
  60. // if ( buttonId != m_validButtonId )
  61. // return false;
  62. // return GUILayout.Button( content, style );
  63. //}
  64. void OnOptionSelected( System.Type type, string name, AmplifyShaderFunction function )
  65. {
  66. Disable();
  67. }
  68. public void Disable()
  69. {
  70. m_isActive = false;
  71. }
  72. public Vector2 StartDropPosition
  73. {
  74. get { return m_startDropPosition; }
  75. }
  76. public Vector3 CurrentPosition
  77. {
  78. get { return m_position; }
  79. }
  80. public Vector2 CurrentPosition2D
  81. {
  82. get { return new Vector2( m_position.x, m_position.y ); }
  83. }
  84. }
  85. }