Skip to content

Commit

Permalink
functionnal sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Astendas committed Mar 7, 2023
1 parent 98a8865 commit e3b4900
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 88 deletions.
69 changes: 39 additions & 30 deletions CFlicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public CFlicker(string n,int x,int y,int width,int height,CScreen screen, Color
Alpha2 = alph2;
Phase = phase;
TypeFrequence = typeFreq;
isActive= true;
//if (seq.Length > 0) { nextTime = 0; if (nextTime != 0) { isActive = false; } Console.WriteLine("seq lenght: {0}\n nextTime: {1}",seq.Length,nextTime); }
isActive= false;
double frameRate = GetFrameRate();
while (Frequency > frameRate)
{
Expand All @@ -116,10 +115,10 @@ public CFlicker(string n,int x,int y,int width,int height,CScreen screen, Color

//CreateAtlas();
}
public CFlicker(string n, int x, int y, int width, int height, CScreen screen, Color col1, double freq, int alph1, int alph2, double phase, int typeFreq, sequenceValue seq)
{
new CFlicker(n,x,y,width,height,screen,col1,freq,alph1,alph2,phase,typeFreq);
public CFlicker(string n, int x, int y, int width, int height, CScreen screen, Color col1, double freq, int alph1, int alph2, double phase, int typeFreq, sequenceValue seq) : this(n, x, y, width, height, screen, col1, freq, alph1, alph2, phase, typeFreq)
{
this.seq = seq;
sequenceDict = new OrderedDictionary();
}

/// <summary>
Expand Down Expand Up @@ -403,46 +402,56 @@ public sequenceValue currentSeq()
public sequenceValue nextSeq(double time)
{
sequenceValue newSeq;
var current = currentSeq();
if (sequenceDict.Count== 0) { newSeq = seq; }
else

if (sequenceDict.Count == 0) { newSeq = seq; }
else
{
if (current.contained_sequence.Count > 0)
var current = currentSeq();
if (current.contained_sequence.Count > 0)
{
newSeq = current.contained_sequence[0];
}
else
{

var parent = (sequenceValue)getSeq(sequenceDict.Count - 2).Key;
var indexSeq = parent.contained_sequence.FindIndex(a => a.contained_sequence.Contains(current));
sequenceDict.Remove(current);
// start by checking if we are at the end of the list
if (indexSeq == parent.contained_sequence.Count - 1)
if (sequenceDict.Count > 1)
{
//check recursively if we are at the end of the parent list, parent parent list....
while (indexSeq == parent.contained_sequence.Count - 1 && indexSeq != -1 && sequenceDict.Count > 1)
var parent = (sequenceValue)getSeq(sequenceDict.Count - 2).Key;
var indexSeq = parent.contained_sequence.IndexOf(current);
sequenceDict.Remove(current);
// start by checking if we are at the end of the list
if (indexSeq == parent.contained_sequence.Count - 1)
{
current = parent;
if (current == seq)
//check recursively if we are at the end of the parent list, parent parent list....
while (indexSeq == parent.contained_sequence.Count - 1 && indexSeq != -1 && sequenceDict.Count > 1)
{
newSeq = seq;
break;
current = parent;
if (current == seq)
{
newSeq = seq;
break;
}
parent = (sequenceValue)getSeq(sequenceDict.Count - 2).Key;
indexSeq = parent.contained_sequence.IndexOf(current);
}
parent = (sequenceValue)getSeq(sequenceDict.Count - 2).Key;
indexSeq = parent.contained_sequence.FindIndex(a => a.contained_sequence.Contains(current));
newSeq = parent.contained_sequence[indexSeq + 1];
}
else
{
Console.WriteLine(indexSeq);
newSeq = parent.contained_sequence[indexSeq + 1];
}
newSeq = parent.contained_sequence[indexSeq + 1];
}
}
else
{
newSeq = parent.contained_sequence[indexSeq + 1];
sequenceDict.Remove(current);
newSeq = current;
}


}
}
//add to dictionnary
if (newSeq.cond == sequenceValue.CondType.Time)
//add to dictionnary
if (newSeq.cond == sequenceValue.CondType.Time)
{
sequenceDict.Add(newSeq, time);
}
Expand Down Expand Up @@ -502,8 +511,8 @@ public enum type
}
public enum CondType
{
KeyPress = 0,
Time = 0,
KeyPress = 2,
Time = 3,
Always = 1,
Never = 0,
None
Expand Down
161 changes: 108 additions & 53 deletions CPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public struct DEVMODE
public void Read_File(string filePath)
{
try {
sequenceValue loadSeq(XmlNode node)
{
double.TryParse(node.SelectSingleNode("value").InnerText, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out double valueSeq);
Enum.TryParse<sequenceValue.type>(node.SelectSingleNode("Type").InnerText, out sequenceValue.type Type);
Enum.TryParse<sequenceValue.CondType>(node.SelectSingleNode("cond").InnerText, out sequenceValue.CondType cond);
sequenceValue seq= new sequenceValue(Type,cond,valueSeq);
Console.WriteLine("value: {0} original:{1}",valueSeq, node.SelectSingleNode("value").InnerText);
foreach(XmlNode nodeSeq in node.SelectSingleNode("contained_sequence"))
{
seq.addSeq(loadSeq(nodeSeq));
}
return seq;
}
// Load the XML file into memory
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
Expand Down Expand Up @@ -138,10 +151,11 @@ public void Read_File(string filePath)
{
image = node.SelectSingleNode("image").InnerText;
}
//load the sequence
sequenceValue seq = null;
if (node.SelectSingleNode("sequence") != null)
{

seq=loadSeq(node.SelectSingleNode("sequence"));
}
//create a window and add the flickers to the list of flickers
if (seq != null)
Expand Down Expand Up @@ -288,6 +302,8 @@ public void Animate_Flicker()
int lost_frame = 0; //only used if computer is too slow to display all frames, we will jump over some frames
long frame_ticks =(long) ((1d / frameRate) * TICKSPERSECONDS);
SDL.SDL_Event evt = new SDL.SDL_Event();

// ANIMATION LOOP
while (!quit && SDL.SDL_GetTicks() < 1000000000)
{
frame += 1;
Expand All @@ -301,53 +317,79 @@ public void Animate_Flicker()
// TODO: fasten this process with use of sorting or not checking when we are active and before endTime of activity
if (c.seq!=null)
{
var currentSeq = c.getSeq(c.sequenceDict.Count - 1);
var key = (sequenceValue)currentSeq.Key;
var value = (double)currentSeq.Value;
sequenceValue newSeq;
if (key.cond == sequenceValue.CondType.Always || key.cond == sequenceValue.CondType.None)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
//Check Times
else
{
if (key.cond == sequenceValue.CondType.Time)
{
if (watch.Elapsed.TotalSeconds - value < 0)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
else
{
newSeq = key;
}
}
else
{
newSeq = key;
}
}
if (newSeq.cond == sequenceValue.CondType.KeyPress)
if(c.sequenceDict.Count> 0)
{
var keyboardKey = (SDL.SDL_Keycode)(int)newSeq.value;
var currentSeq = c.getSeq(c.sequenceDict.Count - 1);
var key = (sequenceValue)currentSeq.Key;
var timeStamp = (double)currentSeq.Value;
sequenceValue newSeq;
if (key.cond == sequenceValue.CondType.Always || key.cond == sequenceValue.CondType.None || key.Type == sequenceValue.type.Block || key.Type == sequenceValue.type.Loop)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
//Check Times
else
{
if (key.cond == sequenceValue.CondType.Time)
{
if (watch.Elapsed.TotalSeconds - timeStamp > key.value)
{
newSeq = c.nextSeq(watch.Elapsed.TotalSeconds);
}
else
{
newSeq = key;
}
}
else
{
newSeq = key;
}



if (SequenceKeyDict.Keys.Contains(keyboardKey))
{
SequenceKeyDict[keyboardKey].Add((c,newSeq));
}
else
{
SequenceKeyDict[keyboardKey]=new List<(CFlicker, sequenceValue)>() { (c,newSeq)};
}

}
if (newSeq.cond == sequenceValue.CondType.KeyPress)
{
var keyboardKey = (SDL.SDL_Keycode)(int)newSeq.value;

if (SequenceKeyDict.Keys.Contains(keyboardKey))
{
SequenceKeyDict[keyboardKey].Add((c, newSeq));
}
else
{
SequenceKeyDict[keyboardKey] = new List<(CFlicker, sequenceValue)>() { (c, newSeq) };
}

}
if (newSeq.Type == sequenceValue.type.Active)
{
c.isActive= true;
//TODO: add LSL signal
}
if (newSeq.Type == sequenceValue.type.Inactive)
{
c.isActive= false;
//TODO: add LSL signal
}

}
else
{
c.nextSeq(watch.Elapsed.TotalSeconds);
}


}
else //if no sequence for this flicker
if (c.isActive)
{
c.display();
c.index += 1 + lost_frame;
}
else
{
c.Screen.show(0);
}
}
);
Expand All @@ -364,21 +406,34 @@ public void Animate_Flicker()
else
{
quit = false;
// will check if any flicker is waiting for a key input
foreach(var key in SequenceKeyDict.Keys)
{
//there might be multiple flicker bound to the same key at the same time
while (SequenceKeyDict[key].Count> 0)
{
//can be replaced with a stack
var tuple = SequenceKeyDict[key][0];
tuple.Item1.skipTo(tuple.Item2,watch.Elapsed.TotalSeconds);
SequenceKeyDict[key].Remove(tuple);
//remove that flicker from the list
if (SequenceKeyDict[key].Count == 0)
// will check if any flicker is waiting for a key input
if(SequenceKeyDict.Count> 0)
{
for(int keyIndex =0;keyIndex<SequenceKeyDict.Keys.Count;keyIndex++)
{
var key = (SequenceKeyDict.Keys.ToList())[keyIndex];
//there might be multiple flicker bound to the same key at the same time
try
{
SequenceKeyDict.Remove(key);
var linkedSeqs = SequenceKeyDict[key];
while (linkedSeqs.Count > 0)
{
//can be replaced with a stack
var tuple = linkedSeqs[0];
tuple.Item1.skipTo(tuple.Item2, watch.Elapsed.TotalSeconds);
linkedSeqs.Remove(tuple);
//remove that flicker from the list
if (linkedSeqs.Count == 0)
{
SequenceKeyDict.Remove(key);
}
}
}
catch
{

}

}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Interface2App/Flicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public Flicker()
Frequency= 1;
Phase = 0;
sequence = new sequenceValue(sequenceValue.type.Block, sequenceValue.CondType.Never);
sequence.addSeq(new sequenceValue(sequenceValue.type.Active, sequenceValue.CondType.Never));
}
/// <summary>
/// custom flicker with parameters
Expand Down Expand Up @@ -110,7 +111,7 @@ public Flicker(int X, int Y, int Width, int Height, System.Windows.Media.Color c
/// copy a flicker
/// </summary>
/// <param name="f"></param>
public Flicker(Flicker f) : this(f.Name, f.X, f.Y, f.Width, f.Height, f.color1, f.Opacity_Min, f.Opacity_Max, f.Frequency, f.Phase, (int)f.Type, new sequenceValue(sequenceValue.type.Block, sequenceValue.CondType.Always)/*f.sequence*/)
public Flicker(Flicker f) : this(f.Name, f.X, f.Y, f.Width, f.Height, f.color1, f.Opacity_Min, f.Opacity_Max, f.Frequency, f.Phase, (int)f.Type,f.sequence)
{

}
Expand Down Expand Up @@ -172,25 +173,25 @@ public sequenceValue(type t, CondType c, double v)
public sequenceValue addSeq(int pos, sequenceValue seq)
{
contained_sequence.Insert(pos, seq);
return seq;
return this;
}
public sequenceValue addSeq(sequenceValue seq)
{
contained_sequence.Add(seq);
return seq;
return this;

}
public sequenceValue addSeq(type t, CondType c)
{
var seq = new sequenceValue(t, c);
addSeq(seq);
return seq;
return this;
}
public sequenceValue addSeq(int pos, type t, CondType c)
{
var seq = new sequenceValue(t, c);
addSeq(pos, seq);
return seq;
return this;
}
public sequenceValue addSeq(int pos, type t, CondType c, double v)
{
Expand Down

0 comments on commit e3b4900

Please sign in to comment.