Skip to content
Permalink
Browse files

Logger to write touch events on disk and ability to load this data in…

… the editor.
  • Loading branch information...
valyard committed Jul 23, 2017
1 parent 9e60aad commit ef15e046e7071848bea510d34743032a5acf32d0

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -5,11 +5,11 @@ GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 4
serializedVersion: 5
m_Component:
- 4: {fileID: 400002}
- 114: {fileID: 11400000}
- 114: {fileID: 11478012}
- component: {fileID: 400002}
- component: {fileID: 11400000}
- component: {fileID: 11478012}
m_Layer: 0
m_Name: TouchManager
m_TagString: Untagged
@@ -26,10 +26,10 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 1
@@ -41,6 +41,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0dd4c394fe06f4ea49e03aaa5e7a8190, type: 3}
m_Name:
m_EditorClassIdentifier:
debugMode: 1
OnFrameStart:
m_PersistentCalls:
m_Calls: []
@@ -89,8 +90,7 @@ MonoBehaviour:
sendMessageEvents: 60
sendMessageTarget: {fileID: 0}
useUnityEvents: 0
layers:
- {fileID: 0}
layers: []
--- !u!114 &11478012
MonoBehaviour:
m_ObjectHideFlags: 1
@@ -0,0 +1,135 @@
/*
* @author Valentin Simonov / http://va.lent.in/
*/

using System;
using System.Collections.Generic;
using System.IO;
using TouchScript.Debugging.Filters;
using TouchScript.Pointers;
using UnityEngine;

#if TOUCHSCRIPT_DEBUG

namespace TouchScript.Debugging.Loggers
{
public class FileReaderLogger : IPointerLogger
{
public const int MIN_POINTER_LIST_SIZE = 1000;

private int pointerCount = 0;
private BinaryReader reader;

protected List<PointerData> data = new List<PointerData>(1);
protected List<List<PointerLog>> events = new List<List<PointerLog>>(1);

/// <inheritdoc />
public int PointerCount
{
get { return pointerCount; }
}

public FileReaderLogger(string path)
{
try
{
reader = new BinaryReader(new FileStream(path, FileMode.Open));
}
catch (IOException e)
{
Debug.LogFormat("Error opening file at '{0}'. {1}", path, e.Message);
}

try
{
while (true)
{
var type = (Pointer.PointerType) reader.ReadUInt32();
var log = new PointerLog()
{
Id = reader.ReadInt32(),
Tick = reader.ReadInt64(),
PointerId = reader.ReadInt32(),
Event = (PointerEvent) reader.ReadUInt32(),
State = new PointerState()
{
Buttons = (Pointer.PointerButtonState) reader.ReadUInt32(),
Position = new Vector2(reader.ReadSingle(), reader.ReadSingle()),
PreviousPosition = new Vector2(reader.ReadSingle(), reader.ReadSingle()),
Flags = reader.ReadUInt32(),
Target = null,
TargetPath = reader.ReadString(),
}
};

checkId(log.PointerId, type);
var list = getPointerList(log.PointerId);
list.Add(log);
}
}
catch (Exception e) {}
finally
{
reader.Close();
}
}

/// <inheritdoc />
public void Log(Pointer pointer, PointerEvent evt)
{
throw new NotImplementedException("FileReaderLogger doesn't support writing data.");
}

/// <inheritdoc />
public List<PointerData> GetFilteredPointerData(IPointerDataFilter filter = null)
{
//if (filter == null)
return new List<PointerData>(data);
}

/// <inheritdoc />
public List<PointerLog> GetFilteredLogsForPointer(int id, IPointerLogFilter filter = null)
{
if (id < 0 || id >= pointerCount)
return new List<PointerLog>();

List<PointerLog> list = events[id];
if (filter == null)
return new List<PointerLog>(list);

var count = list.Count;
List<PointerLog> filtered = new List<PointerLog>(count);
for (var i = 0; i < count; i++)
{
var item = list[i];
if (filter.Applies(ref item)) filtered.Add(item);
}
return filtered;
}

public void Dispose() {}

private IList<PointerLog> getPointerList(int id)
{
return events[id];
}

private void checkId(int id, Pointer.PointerType type)
{
if (id > pointerCount) throw new InvalidOperationException("Pointer id desync!");
else if (id == pointerCount)
{
var list = new List<PointerLog>(MIN_POINTER_LIST_SIZE);
events.Add(list);
data.Add(new PointerData()
{
Id = id,
Type = type,
});
pointerCount++;
}
}
}
}

#endif

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,81 @@
/*
* @author Valentin Simonov / http://va.lent.in/
*/

using System;
using System.Collections.Generic;
using System.IO;
using TouchScript.Debugging.Filters;
using TouchScript.Pointers;
using TouchScript.Utils;
using UnityEngine;

#if TOUCHSCRIPT_DEBUG

namespace TouchScript.Debugging.Loggers
{
public class FileWriterLogger : IPointerLogger
{
private int eventCount = 0;
private BinaryWriter writer;

/// <inheritdoc />
public int PointerCount
{
get { throw new NotImplementedException("FileWriterLogger doesn't support reading data."); }
}

public FileWriterLogger()
{
var path = Path.Combine(Application.dataPath, "../TouchEvents.bin");
try
{
writer = new BinaryWriter(new FileStream(path, FileMode.Create));
}
catch (IOException e)
{
Debug.LogFormat("Error creating file at '{0}'. {1}", path, e.Message);
}
}

/// <inheritdoc />
public void Log(Pointer pointer, PointerEvent evt)
{
var path = TransformUtils.GetHeirarchyPath(pointer.GetPressData().Target);

writer.Write((uint) pointer.Type);
writer.Write(eventCount);
writer.Write(DateTime.Now.Ticks);
writer.Write(pointer.Id);
writer.Write((uint) evt);
writer.Write((uint) pointer.Buttons);
writer.Write(pointer.Position.x);
writer.Write(pointer.Position.y);
writer.Write(pointer.PreviousPosition.x);
writer.Write(pointer.PreviousPosition.y);
writer.Write(pointer.Flags);
writer.Write(path ?? "");

eventCount++;
}

/// <inheritdoc />
public List<PointerData> GetFilteredPointerData(IPointerDataFilter filter = null)
{
throw new NotImplementedException("FileWriterLogger doesn't support reading data.");
}

/// <inheritdoc />
public List<PointerLog> GetFilteredLogsForPointer(int id, IPointerLogFilter filter = null)
{
throw new NotImplementedException("FileWriterLogger doesn't support reading data.");
}

public void Dispose()
{
if (writer != null) writer.Close();
}
}
}

#endif

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -30,22 +30,25 @@ public interface IPointerLogger
/// <param name="evt">The event.</param>
void Log(Pointer pointer, PointerEvent evt);


/// <summary>
/// Returns a list of pointers.
/// </summary>
/// <param name="filter">The filter to use.</param>
/// <returns>A list of <see cref="PointerData"/> objects.</returns>
List<PointerData> GetFilteredPointerData(IPointerDataFilter filter = null);


/// <summary>
/// Returns a lost of pointer events for a pointer.
/// </summary>
/// <param name="id">The pointer id.</param>
/// <param name="filter">The filter to use.</param>
/// <returns>A list of <see cref="PointerLog"/> entries.</returns>
List<PointerLog> GetFilteredLogsForPointer(int id, IPointerLogFilter filter = null);

/// <summary>
/// Releases resources.
/// </summary>
void Dispose();
}

/// <summary>
Oops, something went wrong.

0 comments on commit ef15e04

Please sign in to comment.
You can’t perform that action at this time.