public ComponentUIInput() : base(ETypeOfElement.Input) { InitializeComponent(); DataContext = new ElementInput(BaseElement.IdCounter) { CheckVisibility = false }; Label.DataContext = null; }
public static IElement CreateElementFromDescriptor(ElementDescriptor elementDescriptor) { IElement element; if (elementDescriptor is ElementDescriptorChild) { element = new ElementChild(); } else if (elementDescriptor is ElementDescriptorInput) { element = new ElementInput(); } else if (elementDescriptor is ElementDescriptorButton) { element = new ElementButton(); } else if (elementDescriptor is ElementDescriptorOutput) { element = new ElementOutput(); } else if (elementDescriptor is ElementDescriptorInfo) { element = new ElementInfo(); } else if (elementDescriptor is ElementDescriptorMap) { element = new ElementMap(); } else if (elementDescriptor is ElementDescriptorBrowser) { element = new ElementBrowser(); } else { throw new Exception("Invalid descriptor"); } SetSharedAttributes(element, elementDescriptor); element.ElementDescriptor = elementDescriptor; return(element); }
private void CreateInput(Point point, Canvas surface, int id = -1, string text = "") { ComponentUIInput componentUi; ElementInput element; ElementLabel elementLabel; var labelVM = new ViewModelLabel(); if (id < 0) { componentUi = new ComponentUIInput(point, BaseElement.IdCounter); element = new ElementInput(BaseElement.IdCounter); elementLabel = new ElementLabel(BaseElement.IdCounter); text = "Подпись"; } else { componentUi = new ComponentUIInput(point, id); element = new ElementInput(id); elementLabel = new ElementLabel(id); } BaseViewModelComponent baseViewModelComponent = new BaseViewModelComponent { element = element }; baseViewModelComponent.strength = text; componentUi.DataContext = element; surface.Children.Add(componentUi); componentUi.OnPinDropped += baseViewModelComponent.OnPinDrop; componentUi.OnDeleteElement += baseViewModelComponent.OnDelete; labelVM.element = elementLabel; componentUi.Label.DataContext = baseViewModelComponent; componentUi.Label.OnEdit += baseViewModelComponent.OnEdit; _elements.Add(baseViewModelComponent); _elements[_elements.Count - 1].OnPinElementDropped += OnElementPinDropped; _elements[_elements.Count - 1].OnElementDelete += Delete; }
/// <summary> /// Process inputs. /// </summary> /// <param name="playerId">Player identifier.</param> /// <param name="data">Input data as JSON formatted string.</param> public void ProcessInput(int playerId, string data) { int diffPlayerCount = playerId - VInput.Inputs.Count; // First input from this player? for (int i = 0; i <= diffPlayerCount; i++) { VInput.Inputs.Add(new Dictionary <string, ElementInput>()); } string currentElementName = null; bool stopReading = false; // Read input name identifier using (var sr = new StringReader(data)) using (var reader = new JsonTextReader(sr)) { // Use buffer reader.ArrayPool = JSONArrayPool.Instance; while (!stopReading && reader.Read()) { if ((reader.TokenType == JsonToken.PropertyName) && (reader.Path == "volplane.name")) { currentElementName = reader.ReadAsString(); stopReading = true; } } } // No volplane input if (currentElementName == null) { return; } // Get input object by name, or create new one if not specified yet if (!VInput.Inputs[playerId].TryGetValue(currentElementName, out tempInput)) { tempInput = new ElementInput(); VInput.Inputs[playerId].Add(currentElementName, tempInput); if (Config.DebugLog == (int)DebugState.All) { VDebug.LogFormat("[Volplane (Input Handling)] New input registered: '{0:G}'.", currentElementName); } } // Enqueue processing if the same input changed state in the same frame if (tempInput.Dirty) { updateQueue.Enqueue(delegate { ProcessInput(playerId, data); }); if (Config.DebugLog == (int)DebugState.All) { VDebug.Log("[Volplane (Input Handling)] Queueing multiple inputs from same element."); } return; } // Read and processing input data using (var sr = new StringReader(data)) using (var reader = new JsonTextReader(sr)) { // Use buffer reader.ArrayPool = JSONArrayPool.Instance; while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName) { switch (reader.Path) { // Input type case "volplane.type": reader.Read(); switch (reader.Value.ToString()) { case "dpad": tempInput.Type = ElementInput.InputType.DPad; break; case "joystick": tempInput.Type = ElementInput.InputType.Joystick; break; case "swipe": tempInput.Type = ElementInput.InputType.SwipeField; break; case "touch": tempInput.Type = ElementInput.InputType.TouchArea; break; case "motion": tempInput.Type = ElementInput.InputType.Motion; break; default: tempInput.Type = ElementInput.InputType.Button; break; } break; // Input state case "volplane.data.state": tempInput.State = reader.ReadAsBoolean() ?? false; break; // Input timestamp case "volplane.data.timeStamp": reader.Read(); tempInput.Delay = (int)(VolplaneController.AirConsole.GetServerTime() - Int64.Parse(reader.Value.ToString())); break; // Coordinate X case "volplane.data.x": tempInput.X = (float)(reader.ReadAsDouble() ?? 0d); break; // Coordinate X case "volplane.data.y": tempInput.Y = (float)(reader.ReadAsDouble() ?? 0d); break; // Trigger indicator case "volplane.data.hadDirections": tempInput.HadDirections = reader.ReadAsBoolean() ?? false; break; // Swipe distance case "volplane.data.distance": tempInput.Distance = (float)(reader.ReadAsDouble() ?? 0d); break; // Swipe angle in radians case "volplane.data.angle": tempInput.Angle = (float)(reader.ReadAsDouble() ?? 0d); break; // Swipe angle in degree case "volplane.data.degree": tempInput.Degree = (float)(reader.ReadAsDouble() ?? 0d); break; // Swipe speed case "volplane.data.speed": tempInput.Speed = (float)(reader.ReadAsDouble() ?? 0d); break; // Accelerometer X case "volplane.data.ax": tempInput.AX = (float)(reader.ReadAsDouble() ?? 0d); break; // Accelerometer Y case "volplane.data.ay": tempInput.AY = (float)(reader.ReadAsDouble() ?? 0d); break; // Accelerometer Z case "volplane.data.az": tempInput.AZ = (float)(reader.ReadAsDouble() ?? 0d); break; // Gyroscope alpha case "volplane.data.alpha": tempInput.Alpha = (float)(reader.ReadAsDouble() ?? 0d); break; // Gyroscope beta case "volplane.data.beta": tempInput.Beta = (float)(reader.ReadAsDouble() ?? 0d); break; // Gyroscope gamma case "volplane.data.gamma": tempInput.Gamma = (float)(reader.ReadAsDouble() ?? 0d); break; } } } } // Set dirty tempInput.Dirty = true; // Fire events switch (tempInput.Type) { case ElementInput.InputType.Button: // Event if (VInput.ButtonEvents && (OnButton != null)) { updateQueue.Enqueue(delegate { OnButton(playerId, currentElementName, tempInput.State); }); } break; case ElementInput.InputType.DPad: // Event if (VInput.DPadEvents && (OnDPad != null)) { updateQueue.Enqueue(delegate { OnDPad(playerId, currentElementName, new Vector2(tempInput.X, tempInput.Y)); }); } break; case ElementInput.InputType.Joystick: // Event if (VInput.JoystickEvents && (OnJoystick != null)) { updateQueue.Enqueue(delegate { OnJoystick(playerId, currentElementName, new Vector2(tempInput.X, tempInput.Y)); }); } break; case ElementInput.InputType.SwipeField: // Event if (VInput.SwipeEvents && (OnSwipe != null)) { updateQueue.Enqueue(delegate { OnSwipe(playerId, currentElementName, new Vector2(tempInput.X, tempInput.Y)); }); } break; case ElementInput.InputType.TouchArea: // Event if (VInput.TouchEvents && (OnTouch != null)) { updateQueue.Enqueue(delegate { OnTouch(playerId, currentElementName, new Vector2(tempInput.X, tempInput.Y)); }); } break; default: // Events if (VInput.MotionEvents && (OnAccelerometer != null)) { updateQueue.Enqueue(delegate { OnAccelerometer(playerId, new Vector3(tempInput.AX, tempInput.AY, tempInput.AZ)); }); } if (VInput.MotionEvents && (OnGyroscope != null)) { updateQueue.Enqueue(delegate { OnGyroscope(playerId, new Vector3(tempInput.Alpha, tempInput.Beta, tempInput.Gamma)); }); } break; } }