Monday, 14 January 2013

Squashed the tie bug

A couple of other issues were uncovered and fixed while looking into the tie depth counter not working correctly when saving scores. Turns out on reading the xml in, I wasn't checking for more than one tie on a note, which confused me when I thought I'd corrected the code that saves scores!

So a Collections.sort with a custom comparator did the trick. In the comparator I make sure that we write tie elements that close a tie out before any that start a tie, which in turn keeps the depth counter accurate when we then work through the array.

Code is below, the eagle eyed will spot that I simply say equals when the note is the end of two or more ties, and similarly if it's the start of two ties. Put simply we don't support stacked ties until I fix that!

Collections.sort(tieList, new Comparator() { 
 public int compare(JTie arg0, JTie arg1) {
  // we want to return -1 if arg0 is before arg1
  // before is defined as meaning it's primarily a closure tie for JDrumNote dn
  // ie closes before opens
  if (arg0.isTail(dn)) {
   if (arg1.isTail(dn)) {
    // then this note has multiple ties terminating on it, the tie closest to it
    // is the one we want to process first in the following for loop
    //    so we decrement the group level therein appropriately
    // code here
   } else {
    // arg1 must be a start tie and therefore is greater in the sort order than arg0
    // so we tell sort arg0 < arg1
    return -1;
  } else {
   // it must be a start tie, so check if the other is a close
   if (arg1.isTail(dn)) {
    // then we want to process this before arg0 so we say arg0 > arg1
    return 1;
   } else {
    // both are start ties, we want to open the furthest tie first so it becomes last to close
    // code here to figure out which has the highest y,x co-ords
  // then if they are both closes, we return -1 if arg0 is before arg1 in y,x co-ord terms
  // there can be no arg0==arg1 in the sort unless there are two identical ties starting 
  // or ending on this note - user could do that if they really really wanted!
  return 0;


No comments:

Post a Comment