public int GetButtonState(InputFrame inputFrame, bool input, bool lastInput) { int buttonState; if (input) { if (lastInput) { buttonState = 3; //held } else { buttonState = 0; //pressed inputFrame.isSpecialFrame = true; } } else { if (lastInput) { buttonState = 1; //released inputFrame.isSpecialFrame = true; } else { buttonState = 4; //up } } return(buttonState); }
public InputViewModel(InputFrame input) { aChecked = input.a; bChecked = input.b; cChecked = input.c; dChecked = input.d; }
public void ConstructInputs(ObservableCollection <InputFrame>[] inputLists) { //This is the real magic //ba4 = new byte[] { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF }; for (int whichPlayer = 0; whichPlayer < inputLists.Count(); whichPlayer++) { //construct unique inputs list int durationCounter = 0; ObservableCollection <InputFrame> inputList = inputLists[whichPlayer]; ObservableCollection <InputFrame> uniqueInputs = new ObservableCollection <InputFrame>(); //initialize first input states InputFrame lastInput = new InputFrame(inputList[0].direction, inputList[0].a, inputList[0].b, inputList[0].c, inputList[0].d); lastInput.aState = GetButtonState(lastInput, lastInput.a, false); lastInput.bState = GetButtonState(lastInput, lastInput.b, false); lastInput.cState = GetButtonState(lastInput, lastInput.c, false); lastInput.dState = GetButtonState(lastInput, lastInput.d, false); for (int index = 0; index < inputList.Count; index++) { InputFrame input = inputList[index]; //if any raw input has changed, or if the last frame was a one-off special case bool inputChanged = (((input.direction != lastInput.direction) || (input.a != lastInput.a) || (input.b != lastInput.b) || (input.c != lastInput.c) || (input.d != lastInput.d) || lastInput.isSpecialFrame) && durationCounter < 255 && index != 0); if (inputChanged) { lastInput.counter = durationCounter; input.aState = GetButtonState(input, input.a, lastInput.a); input.bState = GetButtonState(input, input.b, lastInput.b); input.cState = GetButtonState(input, input.c, lastInput.c); input.dState = GetButtonState(input, input.d, lastInput.d); uniqueInputs.Add(lastInput); lastInput = input; durationCounter = 1; } else { durationCounter++; } if (index == (inputList.Count - 1)) { //Write the final input lastInput = uniqueInputs[uniqueInputs.Count - 1]; input.counter = durationCounter; input.aState = GetButtonState(input, input.a, lastInput.a); input.bState = GetButtonState(input, input.b, lastInput.b); input.cState = GetButtonState(input, input.c, lastInput.c); input.dState = GetButtonState(input, input.d, lastInput.d); uniqueInputs.Add(input); } } //convert unique inputs into bytes //hacks if (whichPlayer == 0) { ba4 = new byte[(uniqueInputs.Count * 6) + 1]; for (int index = 0; index < uniqueInputs.Count; index++) { int offset = index * 6; InputFrame input = uniqueInputs[index]; ba4[offset + 0] = Convert.ToByte(input.counter); ba4[offset + 1] = Convert.ToByte(input.direction); ba4[offset + 2] = Convert.ToByte(input.aState); ba4[offset + 3] = Convert.ToByte(input.bState); ba4[offset + 4] = Convert.ToByte(input.cState); ba4[offset + 5] = Convert.ToByte(input.dState); } //terminate the input data with 0x00 ba4[ba4.Length - 1] = 0x00; } else if (whichPlayer == 1) { ba4andHalf = new byte[(uniqueInputs.Count * 6) + 1]; for (int index = 0; index < uniqueInputs.Count; index++) { int offset = index * 6; InputFrame input = uniqueInputs[index]; ba4andHalf[offset + 0] = Convert.ToByte(input.counter); ba4andHalf[offset + 1] = Convert.ToByte(input.direction); ba4andHalf[offset + 2] = Convert.ToByte(input.aState); ba4andHalf[offset + 3] = Convert.ToByte(input.bState); ba4andHalf[offset + 4] = Convert.ToByte(input.cState); ba4andHalf[offset + 5] = Convert.ToByte(input.dState); } //terminate the input data with 0x00 ba4[ba4.Length - 1] = 0x00; } } byte[] ba4copy = ba4; ba4 = new byte[ba4.Length + ba4andHalf.Length]; System.Buffer.BlockCopy(ba4copy, 0, ba4, 0, ba4copy.Length); System.Buffer.BlockCopy(ba4andHalf, 0, ba4, ba4copy.Length, ba4andHalf.Length); }