Skip to content

Commit

Permalink
Improved usability of custom editor window.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keijiro Takahashi authored and Keijiro Takahashi committed Jun 20, 2015
1 parent f04a951 commit f1a8fee
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
29 changes: 22 additions & 7 deletions Assets/MidiJack/Editor/MidiJackWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MidiJackWindow : EditorWindow
{
#region Custom Editor Window Code

[MenuItem ("Window/MIDI Jack")]
[MenuItem("Window/MIDI Jack")]
public static void ShowWindow()
{
EditorWindow.GetWindow<MidiJackWindow>("MIDI Jack");
Expand All @@ -41,7 +41,7 @@ void OnGUI()
var endpointCount = CountEndpoints();

// Endpoints
var temp = "Detected MIDI endpoints:";
var temp = "Detected MIDI devices:";
for (var i = 0; i < endpointCount; i++)
{
var id = GetEndpointIdAtIndex(i);
Expand All @@ -57,26 +57,41 @@ void OnGUI()
EditorGUILayout.HelpBox(temp, MessageType.None);
}

#endregion

#region Update And Repaint

const int _updateInterval = 15;
int _countToUpdate;
int _lastMessageCount;

void Update()
{
if (EditorApplication.isPlaying || MidiDriver.Instance.CheckUpdate())
if (--_countToUpdate > 0) return;

var mcount = MidiDriver.Instance.TotalMessageCount;
if (mcount != _lastMessageCount) {
Repaint();
_lastMessageCount = mcount;
}

_countToUpdate = _updateInterval;
}

#endregion

#region Native Plugin Interface

[DllImport("MidiJackPlugin", EntryPoint="MidiJackCountEndpoints")]
public static extern int CountEndpoints();
static extern int CountEndpoints();

[DllImport("MidiJackPlugin", EntryPoint="MidiJackGetEndpointIDAtIndex")]
public static extern uint GetEndpointIdAtIndex(int index);
static extern uint GetEndpointIdAtIndex(int index);

[DllImport("MidiJackPlugin")]
private static extern System.IntPtr MidiJackGetEndpointName(uint id);
static extern System.IntPtr MidiJackGetEndpointName(uint id);

public static string GetEndpointName(uint id) {
static string GetEndpointName(uint id) {
return Marshal.PtrToStringAnsi(MidiJackGetEndpointName(id));
}

Expand Down
71 changes: 49 additions & 22 deletions Assets/MidiJack/MidiDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ChannelState()
// Channel state array
ChannelState[] _channelArray;

// Last frame number
// Last update frame number
int _lastFrame;

#endregion
Expand All @@ -62,7 +62,7 @@ public ChannelState()

public float GetKey(MidiChannel channel, int noteNumber)
{
CheckUpdate();
UpdateIfNeeded();
var v = _channelArray[(int)channel]._noteArray[noteNumber];
if (v > 1) return v - 1;
if (v > 0) return v;
Expand All @@ -71,19 +71,19 @@ public float GetKey(MidiChannel channel, int noteNumber)

public bool GetKeyDown(MidiChannel channel, int noteNumber)
{
CheckUpdate();
UpdateIfNeeded();
return _channelArray[(int)channel]._noteArray[noteNumber] > 1;
}

public bool GetKeyUp(MidiChannel channel, int noteNumber)
{
CheckUpdate();
UpdateIfNeeded();
return _channelArray[(int)channel]._noteArray[noteNumber] < 0;
}

public int[] GetKnobNumbers(MidiChannel channel)
{
CheckUpdate();
UpdateIfNeeded();
var cs = _channelArray[(int)channel];
var numbers = new int[cs._knobMap.Count];
cs._knobMap.Keys.CopyTo(numbers, 0);
Expand All @@ -92,7 +92,7 @@ public int[] GetKnobNumbers(MidiChannel channel)

public float GetKnob(MidiChannel channel, int knobNumber, float defaultValue)
{
CheckUpdate();
UpdateIfNeeded();
var cs = _channelArray[(int)channel];
if (cs._knobMap.ContainsKey(knobNumber)) return cs._knobMap[knobNumber];
return defaultValue;
Expand All @@ -104,7 +104,31 @@ public float GetKnob(MidiChannel channel, int knobNumber, float defaultValue)

#if UNITY_EDITOR

// Incoming message history
// Update timer
const float _updateInterval = 1.0f / 30;
float _lastUpdateTime;

bool CheckUpdateInterval()
{
var current = Time.realtimeSinceStartup;
if (current - _lastUpdateTime > _updateInterval || current < _lastUpdateTime) {
_lastUpdateTime = current;
return true;
}
return false;
}

// Total message count
int _totalMessageCount;

public int TotalMessageCount {
get {
UpdateIfNeeded();
return _totalMessageCount;
}
}

// Message history
Queue<MidiMessage> _messageHistory;

public Queue<MidiMessage> History {
Expand All @@ -128,22 +152,29 @@ public Queue<MidiMessage> History {
#endif
}

public bool CheckUpdate()
#endregion

#region Private Methods

void UpdateIfNeeded()
{
if (Application.isPlaying)
{
var frame = Time.frameCount;
if (frame == _lastFrame) return false;
_lastFrame = frame;
if (frame != _lastFrame) {
Update();
_lastFrame = frame;
}
}
else
{
#if UNITY_EDITOR
if (CheckUpdateInterval()) Update();
#endif
}
return Update();
}

#endregion

#region Private Methods

bool Update()
void Update()
{
// Update the note state array.
foreach (var cs in _channelArray)
Expand All @@ -159,7 +190,6 @@ bool Update()
}

// Process the message queue.
var incoming = false;
while (true)
{
// Pop from the queue.
Expand Down Expand Up @@ -200,20 +230,17 @@ bool Update()
}

#if UNITY_EDITOR
// Record the message history.
// Record the message.
_totalMessageCount++;
_messageHistory.Enqueue(message);
#endif

incoming = true;
}

#if UNITY_EDITOR
// Truncate the history.
while (_messageHistory.Count > 8)
_messageHistory.Dequeue();
#endif

return incoming;
}

#endregion
Expand Down

0 comments on commit f1a8fee

Please sign in to comment.