EditorViewPaintSelection.as 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. const int PAINT_STEP_UPDATE = 16;
  2. const int PAINT_SELECTION_KEY = KEY_C;
  3. bool EditorPaintSelectionShow = false;
  4. int EditorPaintSelectionUITimeToUpdate = 0;
  5. UIElement@ EditorPaintSelectionUIContainer = null;
  6. BorderImage@ paintSelectionImage = null;
  7. IntVector2 paintSelectionBrushDefaultSize(96,96);
  8. IntVector2 paintSelectionBrushCurrentSize = paintSelectionBrushDefaultSize;
  9. IntVector2 paintSelectionBrushMinSize(64,64);
  10. IntVector2 paintSelectionBrushMaxSize(512,512);
  11. IntVector2 paintSelectionBrushStepSizeChange(16,16);
  12. void CreatePaintSelectionContainer()
  13. {
  14. if (editorScene is null) return;
  15. EditorPaintSelectionUIContainer = UIElement();
  16. EditorPaintSelectionUIContainer.position = IntVector2(0,0);
  17. EditorPaintSelectionUIContainer.size = IntVector2(graphics.width,graphics.height);
  18. EditorPaintSelectionUIContainer.priority = -5;
  19. EditorPaintSelectionUIContainer.focusMode = FM_NOTFOCUSABLE;
  20. EditorPaintSelectionUIContainer.bringToBack = true;
  21. EditorPaintSelectionUIContainer.name ="DebugPaintSelectionContainer";
  22. EditorPaintSelectionUIContainer.temporary = true;
  23. ui.root.AddChild(EditorPaintSelectionUIContainer);
  24. }
  25. void CreatePaintSelectionTool()
  26. {
  27. paintSelectionImage = BorderImage("Icon");
  28. paintSelectionImage.temporary = true;
  29. paintSelectionImage.SetFixedSize(paintSelectionBrushDefaultSize.x,paintSelectionBrushDefaultSize.y);
  30. paintSelectionImage.texture = cache.GetResource("Texture2D", "Textures/Editor/SelectionCircle.png");
  31. paintSelectionImage.imageRect = IntRect(0,0,512,512);
  32. paintSelectionImage.priority = -5;
  33. paintSelectionImage.color = Color(1,1,1);
  34. paintSelectionImage.bringToBack = true;
  35. paintSelectionImage.enabled = false;
  36. paintSelectionImage.selected = false;
  37. paintSelectionImage.visible = true;
  38. EditorPaintSelectionUIContainer.AddChild(paintSelectionImage);
  39. }
  40. void UpdatePaintSelection()
  41. {
  42. PaintSelectionCheckKeyboard();
  43. // Early out if disabled
  44. if (!EditorPaintSelectionShow) return;
  45. if (editorScene is null || EditorPaintSelectionUITimeToUpdate > time.systemTime) return;
  46. EditorPaintSelectionUIContainer = ui.root.GetChild("DebugPaintSelectionContainer");
  47. if (EditorPaintSelectionUIContainer is null)
  48. {
  49. CreatePaintSelectionContainer();
  50. CreatePaintSelectionTool();
  51. }
  52. if (EditorPaintSelectionUIContainer !is null)
  53. {
  54. // Set visibility for all origins
  55. EditorPaintSelectionUIContainer.visible = EditorPaintSelectionShow;
  56. if (viewportMode!=VIEWPORT_SINGLE)
  57. EditorPaintSelectionUIContainer.visible = false;
  58. if (EditorPaintSelectionShow)
  59. {
  60. IntVector2 mp = input.mousePosition;
  61. paintSelectionImage.position = IntVector2(mp.x - (paintSelectionBrushCurrentSize.x * 0.5f), mp.y - (paintSelectionBrushCurrentSize.y * 0.5f));
  62. }
  63. }
  64. EditorPaintSelectionUITimeToUpdate = time.systemTime + PAINT_STEP_UPDATE;
  65. }
  66. void PaintSelectionCheckKeyboard()
  67. {
  68. bool key = input.keyPress[PAINT_SELECTION_KEY];
  69. if (key && ui.focusElement is null)
  70. {
  71. EditorPaintSelectionShow = !EditorPaintSelectionShow;
  72. if (EditorPaintSelectionUIContainer !is null)
  73. EditorPaintSelectionUIContainer.visible = EditorPaintSelectionShow;
  74. if (EditorPaintSelectionShow)
  75. {
  76. // When we start paint selection we change editmode to select
  77. editMode = EDIT_SELECT;
  78. //selectedNodes.Clear();
  79. // and also we show origins for proper origins update
  80. ShowOrigins(true);
  81. toolBarDirty = true;
  82. }
  83. }
  84. else if (EditorPaintSelectionShow && ui.focusElement is null)
  85. {
  86. if (editMode != EDIT_SELECT)
  87. {
  88. EditorPaintSelectionShow = false;
  89. EditorPaintSelectionUIContainer.visible = false;
  90. }
  91. }
  92. if (input.mouseButtonDown[MOUSEB_RIGHT])
  93. {
  94. EditorPaintSelectionShow = false;
  95. if (EditorPaintSelectionUIContainer !is null)
  96. EditorPaintSelectionUIContainer.visible = false;
  97. }
  98. }
  99. void SelectOriginsByPaintSelection(IntVector2 curPos, float brushRadius, bool isOperationAddToSelection = true)
  100. {
  101. if (!EditorPaintSelectionShow || EditorPaintSelectionUIContainer is null) return;
  102. for (int i=0; i < originsNodes.length; i++)
  103. {
  104. Vector3 v1(originsIcons[i].position.x, originsIcons[i].position.y, 0);
  105. Vector3 v2(curPos.x - ORIGINOFFSETICON.x, curPos.y - ORIGINOFFSETICON.y, 0);
  106. float distance = (v1 - v2).length;
  107. bool isThisOriginInCircle = distance < brushRadius ? true : false;
  108. int nodeID = originsIcons[i].vars[ORIGIN_NODEID_VAR].GetInt();
  109. if (isThisOriginInCircle)
  110. {
  111. WeakHandle handle = editorScene.GetNode(nodeID);
  112. if (handle.Get() !is null)
  113. {
  114. Node@ node = handle.Get();
  115. if (isOperationAddToSelection)
  116. {
  117. if (node !is null && isThisNodeOneOfSelected(node) == false)
  118. SelectNode(node, true);
  119. }
  120. else // Deselect origins operation
  121. {
  122. if (node !is null && isThisNodeOneOfSelected(node) == true)
  123. DeselectNode(node);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. void HandlePaintSelectionMouseMove(StringHash eventType, VariantMap& eventData)
  130. {
  131. if (!EditorPaintSelectionShow || EditorPaintSelectionUIContainer is null) return;
  132. int x = eventData["X"].GetInt();
  133. int y = eventData["Y"].GetInt();
  134. float r = (paintSelectionBrushCurrentSize.x * 0.5);
  135. IntVector2 mousePos(x,y);
  136. // Select by mouse
  137. if (input.mouseButtonDown[MOUSEB_LEFT] && input.qualifierDown[QUAL_CTRL] != true)
  138. {
  139. SelectOriginsByPaintSelection(mousePos, r, true);
  140. }
  141. // Deselect by mouse
  142. else if (input.mouseButtonDown[MOUSEB_LEFT] && input.qualifierDown[QUAL_CTRL] == true)
  143. {
  144. SelectOriginsByPaintSelection(mousePos, r, false);
  145. }
  146. }
  147. void HandlePaintSelectionWheel(StringHash eventType, VariantMap& eventData)
  148. {
  149. if (!EditorPaintSelectionShow || EditorPaintSelectionUIContainer is null) return;
  150. int wheelValue = eventData["Wheel"].GetInt();
  151. if (wheelValue != 0)
  152. {
  153. if (wheelValue > 0)
  154. {
  155. paintSelectionBrushCurrentSize = paintSelectionBrushCurrentSize - paintSelectionBrushStepSizeChange;
  156. paintSelectionBrushCurrentSize = IntVector2(Max(paintSelectionBrushCurrentSize.x, paintSelectionBrushMinSize.x), Max(paintSelectionBrushCurrentSize.y, paintSelectionBrushMinSize.y));
  157. }
  158. else if (wheelValue < 0)
  159. {
  160. paintSelectionBrushCurrentSize = paintSelectionBrushCurrentSize + paintSelectionBrushStepSizeChange;
  161. paintSelectionBrushCurrentSize = IntVector2(Min(paintSelectionBrushCurrentSize.x, paintSelectionBrushMaxSize.x), Min(paintSelectionBrushCurrentSize.y, paintSelectionBrushMaxSize.y));
  162. }
  163. paintSelectionImage.SetFixedSize(paintSelectionBrushCurrentSize.x, paintSelectionBrushCurrentSize.y);
  164. }
  165. }