Skip to content

Commit

Permalink
Fixing bugs in left/right arrow keys with search selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dbirman committed Feb 12, 2024
1 parent 1bc884e commit b174942
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 47 deletions.
105 changes: 101 additions & 4 deletions Unity/Assets/Scripts/NPUltraRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BrainAtlas;
using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
Expand All @@ -8,6 +9,7 @@
using UnityEngine.AI;
using Urchin.Behaviors;
using Urchin.Managers;
using Random = UnityEngine.Random;
#if UNITY_WEBGL && !UNITY_EDITOR
using System.Runtime.InteropServices;
#endif
Expand All @@ -17,6 +19,9 @@ public class NPUltraRuntime : MonoBehaviour
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void FinishedLoading();

[DllImport("__Internal")]
private static extern void UpdateSelection(int idx);
#endif

private const float UNIT_SIZE = 0.075f;
Expand Down Expand Up @@ -94,6 +99,9 @@ public void LoadData()
_loaded = true;
}

private bool _searchActive;
private int[] _searched;

public void Search(string state)
{
if (!_loaded) return;
Expand All @@ -107,9 +115,9 @@ public void Search(string state)
// Set neurons to active/inactive state
if (activeArea || disabledDur || disabledWave)
{
int[] searched = new int[_data.Areas.Length];
_searched = new int[_data.Areas.Length];

for (int i = 0; i < searched.Length; i++)
for (int i = 0; i < _searched.Length; i++)
{
// For each unit, check if:
// we are searching for areas AND this is in that area, or we aren't searching for areas
Expand All @@ -125,14 +133,16 @@ public void Search(string state)

bool active = inArea && (isShort || isLong) && (isSmall || isLarge);

searched[i] = active ? 1 : -1;
_searched[i] = active ? 1 : -1;
}

_meshManager.SetSearched(_data.Names, searched);
_meshManager.SetSearched(_data.Names, _searched);
_searchActive = true;
}
else
{
_meshManager.SetSearched(_data.Names, 0);
_searchActive = false;
}

// Handle showing and hiding 3D areas
Expand Down Expand Up @@ -185,10 +195,97 @@ public void Select(string selectedName)
unMesh.Select(false);
}

// Select the new neuron
_selected = selectedName;

MeshBehavior meshBehavior = _meshManager.GetMesh(_selected);
meshBehavior.Select(true);

// If the selected neuron is outside the area we are in, re-select
CheckSelection();
}

private int SelectedIndex()
{
return Array.IndexOf(_data.Names, _selected);
}

private int _selDirection;
public void SelectDirection(int direction)
{
_selDirection = direction;

int selIdx = SelectedIndex(); // -1 to switch to 0 indexing
selIdx += direction;

if (selIdx >= _data.Names.Length)
selIdx = 0;
if (selIdx < 0)
selIdx = _data.Names.Length - 1;

// tell the javascript page to make this the new selection
#if UNITY_WEBGL && !UNITY_EDITOR
UpdateSelection(int.Parse(_data.Names[selIdx], System.Globalization.NumberStyles.Any));
#endif
}

public void CheckSelection()
{
if (_searchActive)
{
int selIdx = SelectedIndex(); // -1 to switch to 0 indexing

// Check if this is an inactive unit
if (_searched[selIdx] == -1)
{
// Unit is inactive
// We need to re-select the next neuron, loop around until we find it
int newIdx = 0;
int count = 0;

if (_selDirection > 0)
{
// We incremented, search up, then loop around if needed
newIdx = selIdx;
while (_searched[newIdx] == -1)
{
newIdx++;
if (newIdx >= _searched.Length)
newIdx = 0;

count++;
if (count > _searched.Length)
{
Debug.LogWarning("No neurons available");
break;
}
}
}
else if (_selDirection < 0)
{
// We decremented, search down, then loop around if needed
newIdx = selIdx;
while (_searched[newIdx] == -1)
{
newIdx--;
if (newIdx <= 0)
newIdx = _searched.Length - 1;

count++;
if (count > _searched.Length)
{
Debug.LogWarning("No neurons available");
break;
}
}
}

// tell the javascript page to make this the new selection
#if UNITY_WEBGL && !UNITY_EDITOR
UpdateSelection(int.Parse(_data.Names[newIdx], System.Globalization.NumberStyles.Any));
#endif
}
}
}
}

Expand Down
Binary file modified build/Build/build.data.unityweb
Binary file not shown.
Binary file modified build/Build/build.framework.js.unityweb
Binary file not shown.
Binary file modified build/Build/build.wasm.unityweb
Binary file not shown.
44 changes: 2 additions & 42 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,12 @@
// Check if left arrow key was pressed
if (event.keyCode === 39) {
// Decrement the selected value
state.selected++;
checkSelectedBounds(true);
update();
myGameInstance.SendMessage('main', 'SelectDirection', 1);
}
// Check if right arrow key was pressed
else if (event.keyCode === 37) {
// Increment the selected value
state.selected--;
checkSelectedBounds(false);
update();
myGameInstance.SendMessage('main', 'SelectDirection', -1);
}
});

Expand Down Expand Up @@ -488,42 +484,6 @@
// SELECTION / SEARCH //////////
////////////////////////////////

function checkSelectedBounds(inc = true) {
checkSelectedBounds
// if (state.search == '') {
// checkSelectedLimits();
// }
// else {
// // we are going to cycle inside of this area
// if (csvData[state.selected-1].area != state.search) {
// if (inc) {
// // count up from zero, find the first area that matches
// state.selected = 1;
// while (csvData[state.selected-1].area != state.search) {
// state.selected++;
// }
// }
// else {
// // count down
// state.selected = N;
// while (csvData[state.selected-1].area != state.search) {
// state.selected--;
// }
// }
// checkSelectedLimits();
// }
// }
}

function checkSelectedLimits() {
if (state.selected <= 0) {
state.selected += N;
}
if (state.selected > N) {
state.selected -= N;
}
}

function searchOnEnter(event) {
if (event.key == 'Enter') {
search(area);
Expand Down
2 changes: 1 addition & 1 deletion metadata_stripped.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5692,4 +5692,4 @@ ID,Amplitude (µV),Spike count,Area,Peak (µV),Trough (µV),P/T ratio,Duration (
5691,122,439,EW,42.2,-79.9,0.5,0.33,53
5692,174,1813,EW,54,-119.7,0.5,0.2,42
5693,174,409,EW,60.3,-113.4,0.5,0.2,40
5694,159,354,EW,42.6,-116.2,0.4,0.23,47
5694,159,354,EW,42.6,-116.2,0.4,0.23,47

0 comments on commit b174942

Please sign in to comment.