Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from uurha/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
uurha committed Dec 9, 2022
1 parent 4597190 commit 0b2c99e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public override void OnGUI(Rect position, SerializedProperty serializedProperty,
CheckInteraction(position, serializedProperty, size);
}

private async void UpdateTextureLoop(CancellationToken cancellationToken)
private async void UpdateTextureLoop(EditorPopup editorPopup, CancellationToken cancellationToken)
{
await Task.Yield();
while (_isMouseDown && !cancellationToken.IsCancellationRequested)
{
UpdateTexture();
EditorPopup.UpdatePosition(_currentScreenMousePosition);
editorPopup.UpdatePosition(_currentScreenMousePosition);
await Task.Yield();
if (cancellationToken.IsCancellationRequested) break;
}
Expand All @@ -72,7 +72,8 @@ private void CheckInteraction(Rect position, SerializedProperty serializedProper
else
{
var isLeftDrag = DrawersHelper.IsMouseButton(EventType.MouseDrag, DrawersHelper.MouseButtonLeft);
if (DrawersHelper.IsLeftButtonUp() || isLeftDrag && !contains && _isMouseDown)
var isLeftUp = DrawersHelper.IsLeftButtonUp();
if (isLeftUp || isLeftDrag && !contains && _isMouseDown)
{
Deconstruct();
}
Expand All @@ -86,12 +87,12 @@ private void MouseDownCase(SerializedProperty serializedProperty, float size)
_previewScene.Construct();
var texture = GenerateTexture(serializedProperty.objectReferenceValue, size);
if (texture == null) return;
EditorPopup.Initialize(texture,
new Rect(_currentScreenMousePosition, Vector2.one * size));
var popup = EditorPopup.Initialize(texture,
new Rect(_currentScreenMousePosition, Vector2.one * size), true);
_isMouseDown = true;

_cancellation = new CancellationTokenSource();
UpdateTextureLoop(_cancellation.Token);
UpdateTextureLoop(popup, _cancellation.Token);
}
}
}
Expand Down
44 changes: 36 additions & 8 deletions Editor/EditorAddons/Helpers/EditorPopup.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
using UnityEditor;
using System;
using UnityEditor;
using UnityEngine;

namespace Better.Attributes.EditorAddons.Helpers
{
public class EditorPopup : EditorWindow
{
private Texture _texture;
private bool _needUpdate = true;
private bool _destroyTexture;
public event Action Closed;
public event Action FocusLost;

public static void Initialize(Texture texture, Rect position)
public static EditorPopup Initialize(Texture texture, Rect position, bool needUpdate, bool destroyTexture = false)
{
var window = HasOpenInstances<EditorPopup>() ? GetWindow<EditorPopup>() : CreateInstance<EditorPopup>();
window.position = position;
window._texture = texture;
window._needUpdate = needUpdate;
window._destroyTexture = destroyTexture;
window.ShowPopup();
return window;
}

public static EditorPopup InitializeAsWindow(Texture texture, Rect position, bool needUpdate, bool destroyTexture = false)
{
var window = HasOpenInstances<EditorPopup>() ? GetWindow<EditorPopup>() : CreateInstance<EditorPopup>();
window.position = position;
window._texture = texture;
window._needUpdate = needUpdate;
window._destroyTexture = destroyTexture;
window.ShowUtility();
return window;
}

private void Update()
{
Repaint();
if (_needUpdate)
Repaint();
}

private void OnGUI()
Expand All @@ -26,20 +46,28 @@ private void OnGUI()
GUI.DrawTexture(new Rect(0, 0, position.width, position.height), _texture, ScaleMode.ScaleToFit, true);
}

private void OnLostFocus()
{
FocusLost?.Invoke();
}

public static void CloseInstance()
{
if (!HasOpenInstances<EditorPopup>()) return;
var window = GetWindow<EditorPopup>();
window.Closed?.Invoke();
if (window._destroyTexture)
{
Destroy(window._texture);
}
window.Close();
}

public static void UpdatePosition(Vector2 newPosition)
public void UpdatePosition(Vector2 newPosition)
{
if (!HasOpenInstances<EditorPopup>()) return;
var window = GetWindow<EditorPopup>();
var rect = window.position;
var rect = position;
rect.position = newPosition;
window.position = rect;
position = rect;
}
}
}
2 changes: 1 addition & 1 deletion Samples~/TestSamples/New Prefab.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 48e1bdd289dc4c29bb25138e2231ffe7, type: 3}
m_Name:
m_EditorClassIdentifier:
sprite: {fileID: 10911, guid: 0000000000000000f000000000000000, type: 0}
sprite: {fileID: 21300000, guid: 78b772f3f305147419de3cc97ca72bd7, type: 3}
2 changes: 1 addition & 1 deletion Samples~/TestSamples/Scripts/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum MyFlagEnum

public class Test : MonoBehaviour
{
[IconHeader("78b772f3f305147419de3cc97ca72bd7")]
[IconHeader("78b772f3f305147419de3cc97ca72bd7")]
[SelectEnum] [SerializeField] private KeyCode keyCode;

[SelectEnum] [SerializeField] private MyFlagEnum myFlagEnumTest;
Expand Down
16 changes: 8 additions & 8 deletions Samples~/TestSamples/logo-social.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.uurha.betterattributes",
"displayName": "Better Attributes",
"version": "0.2.5",
"version": "0.2.6",
"unity": "2018.3",
"description": "Unity attributes, allows to serialize interfaces, draw handles for Vector3/Vector2/Quaternion/Bounds, create read only fields.",
"dependencies": {
Expand Down

0 comments on commit 0b2c99e

Please sign in to comment.