void HandleInput() { Event guiEvent = Event.current; if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(guiEvent.mousePosition)) { mouseIsDownOverKey = true; selectedKeyIndex = i; needsRepaint = true; break; } } if (!mouseIsDownOverKey) { float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x); Color interpolatedColour = gradient.Evaluate(keyTime); Color randomColour = new Color(Random.value, Random.value, Random.value); selectedKeyIndex = gradient.AddKey((gradient.randomizeColour)?randomColour:interpolatedColour, keyTime); mouseIsDownOverKey = true; needsRepaint = true; } } if (guiEvent.type == EventType.MouseUp && guiEvent.button == 0) { mouseIsDownOverKey = false; } if (mouseIsDownOverKey && guiEvent.type == EventType.MouseDrag && guiEvent.button == 0) { float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x); selectedKeyIndex = gradient.UpdateKeyTime(selectedKeyIndex, keyTime); needsRepaint = true; } if (guiEvent.keyCode == KeyCode.Backspace && guiEvent.type == EventType.KeyDown) { gradient.RemoveKey(selectedKeyIndex); if (selectedKeyIndex >= gradient.NumKeys) { selectedKeyIndex--; } needsRepaint = true; } }
override protected void HandleInput() { Event guiEvent = Event.current; if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(guiEvent.mousePosition)) { mouseDownOverKey = true; selectedKeyIndex = i; NeedsRepaint(); } } if (gradientPreviewRect.Contains(guiEvent.mousePosition)) { Color colour = gradient.Evaluate(Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x)); float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x); selectedKeyIndex = gradient.AddKey(colour, keyTime); MarkSceneDirty(); NeedsRepaint(); } } if (guiEvent.type == EventType.MouseUp && guiEvent.button == 0) { mouseDownOverKey = false; } if (guiEvent.type == EventType.MouseDrag && guiEvent.button == 0 && mouseDownOverKey) { float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x); selectedKeyIndex = gradient.UpdateKeyTime(selectedKeyIndex, keyTime); MarkSceneDirty(); NeedsRepaint(); } if (guiEvent.type == EventType.KeyDown && (guiEvent.keyCode == KeyCode.Backspace || guiEvent.keyCode == KeyCode.Delete)) { gradient.RemoveKey(selectedKeyIndex); if (selectedKeyIndex >= gradient.KeyCount()) { selectedKeyIndex--; } MarkSceneDirty(); NeedsRepaint(); } }
private void HandleInput() { Event guiEvent = Event.current; if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { for (int i = 0; i < _keyRects.Length; i++) { if (_keyRects[i].Contains(guiEvent.mousePosition)) { // Hovering i-th keyRect _selectedKeyIndex = i; _mouseIsDownOverKey = true; _needsRepaint = true; break; } } if (!_mouseIsDownOverKey) { var keyTime = Mathf.InverseLerp(_gradientPreviewRect.x, _gradientPreviewRect.xMax, guiEvent.mousePosition.x); Color randomColor = new Color(Random.value, Random.value, Random.value); Color interpolatedColor = _gradient.Evaluate(keyTime); _selectedKeyIndex = _gradient.AddKey(_gradient.randomizeColor ? randomColor : interpolatedColor, keyTime); _mouseIsDownOverKey = true; } } if (guiEvent.type == EventType.MouseUp && guiEvent.button == 0) { _mouseIsDownOverKey = false; } if (_mouseIsDownOverKey && guiEvent.type == EventType.MouseDrag && guiEvent.button == 0) { var keyTime = Mathf.InverseLerp(_gradientPreviewRect.x, _gradientPreviewRect.xMax, guiEvent.mousePosition.x); _selectedKeyIndex = _gradient.UpdateKeyTime(_selectedKeyIndex, keyTime); _needsRepaint = true; } if (guiEvent.keyCode == KeyCode.Backspace && guiEvent.type == EventType.KeyDown) { _gradient.RemoveKey(_selectedKeyIndex); if (_selectedKeyIndex >= _gradient.NumKeys) { _selectedKeyIndex--; } _needsRepaint = true; } }
void HandleInput() { Event e = Event.current; if (e.type == EventType.MouseDown && e.button == 0) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(e.mousePosition)) { mouseIsDownOverKey = true; selectedKeyIndex = i; needRepaint = true; break; } } if (!mouseIsDownOverKey) { Color randomColor = new Color(Random.value, Random.value, Random.value); float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, e.mousePosition.x); selectedKeyIndex = gradient.AddKey(randomColor, keyTime); mouseIsDownOverKey = true; needRepaint = true; } } if (e.type == EventType.MouseUp && e.button == 0) { mouseIsDownOverKey = false; } if (mouseIsDownOverKey && e.type == EventType.MouseDrag && e.button == 0) { float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, e.mousePosition.x); selectedKeyIndex = gradient.UpdateKeyTime(selectedKeyIndex, keyTime); needRepaint = true; } if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Backspace) { gradient.RemoveKey(selectedKeyIndex); if (selectedKeyIndex >= gradient.KeyCount) { selectedKeyIndex--; } needRepaint = true; } }
public void OnGUI() { Event guiEvent = Event.current; Rect gradientPreviewRect = new Rect(borderSize, borderSize, position.width - borderSize * 2, 25); GUI.DrawTexture(gradientPreviewRect, gradient.GetTexture((int)gradientPreviewRect.width, (int)gradientPreviewRect.height)); for (int i = 0; i < gradient.NumKeys(); i++) { CustomGradient.ColourKey key = gradient.GetKey(i); Rect keyRect = new Rect(gradientPreviewRect.x + gradientPreviewRect.width * key.Time - keyWidth / 2f, gradientPreviewRect.yMax + borderSize, keyWidth, keyHeight); EditorGUI.DrawRect(keyRect, key.Colour); } if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { Color randomColor = new Color(Random.value, Random.value, Random.value); float keyTime = Mathf.InverseLerp(gradientPreviewRect.x, gradientPreviewRect.xMax, guiEvent.mousePosition.x); gradient.AddKey(randomColor, keyTime); Repaint(); } }
private void OnGUI() { Rect r = new Rect(borderSize, borderSize, position.width - borderSize * 2, 32); GUI.DrawTexture(r, gradient.GetTexture((int)r.width)); keyRects = new Rect[gradient.KeysCount]; for (int i = 0; i < gradient.KeysCount; i++) { var key = gradient.GetKey(i); float x = r.x + r.width * key.Time - keySize.x / 2f; float y = r.yMax + borderSize; Rect keyRect = new Rect(new Vector2(x, y), keySize); if (i == selectedKey) { var pos = new Vector2(x, y) - Vector2.one * 2; var size = keySize + Vector2.one * 4; var outline = new Rect(pos, size); EditorGUI.DrawRect(outline, Color.black); } EditorGUI.DrawRect(keyRect, key.Color); keyRects[i] = keyRect; } Event e = Event.current; if (e.button == 0) { if (e.type == EventType.MouseDown) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(e.mousePosition)) { mouseIsOverKey = true; selectedKey = i; needRepaint = true; break; } } if (mouseIsOverKey) { } else { var c = new Color(Random.value, Random.value, Random.value); float time = Mathf.InverseLerp(r.x, r.xMax, e.mousePosition.x); selectedKey = gradient.AddKey(c, time); mouseIsOverKey = true; needRepaint = true; } } else if (e.type == EventType.MouseUp) { mouseIsOverKey = false; } else if (e.type == EventType.MouseDrag) { if (mouseIsOverKey) { } } } if (needRepaint) { Repaint(); needRepaint = false; } }
private void HandleGradientInput(ControlState state) { Event guiEvent = Event.current; Rect temp = gradientPreviewRect; temp.x = 0; temp.y = 0; if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(guiEvent.mousePosition)) { mouseIsDownOverKey = true; selectedKeyIndex = i; break; } } if (!mouseIsDownOverKey && temp.Contains(guiEvent.mousePosition) && guiEvent.clickCount == 2) { float keyTime = (guiEvent.mousePosition.x - state.translation.x) / state.scale.x; Color interpolatedColour = gradient.Evaluate(keyTime); selectedKeyIndex = gradient.AddKey(interpolatedColour, keyTime); mouseIsDownOverKey = true; } } if (guiEvent.type == EventType.MouseUp && guiEvent.button == 0) { mouseIsDownOverKey = false; } if (mouseIsDownOverKey && guiEvent.type == EventType.MouseDrag && guiEvent.button == 0) { float keyTime = (guiEvent.mousePosition.x - state.translation.x) / state.scale.x; for (int i = 0; i < gradient.NumKeys; i++) { if (keyTime.Equals(gradient.GetKey(i).Time)) { keyTime += 0.1f; } } selectedKeyIndex = gradient.UpdateKeyTime(selectedKeyIndex, keyTime); } if (guiEvent.keyCode == KeyCode.Delete && guiEvent.type == EventType.KeyDown) { for (int i = 0; i < keyRects.Length; i++) { if (keyRects[i].Contains(guiEvent.mousePosition)) { gradient.RemoveKey(selectedKeyIndex); if (selectedKeyIndex >= gradient.NumKeys) { selectedKeyIndex--; } break; } } } }