protected override void ResolveDependencies() { base.ResolveDependencies(); var leftJoystickEntity = this.EntityManager.Find(LeftJoystickEntityPath); if (leftJoystickEntity != null) { this.leftJoystick = leftJoystickEntity.FindComponent <JoystickComponent>(); } var fireButtonEntity = this.EntityManager.Find(FireButtonEntityPath); if (fireButtonEntity != null) { this.fireButton = this.EntityManager.Find(FireButtonEntityPath).FindComponent <JoystickComponent>(); } var gamePlayEntity = this.EntityManager.Find(GamePlayEntityPath); if (gamePlayEntity != null) { this.gamePlay = this.EntityManager.Find(GamePlayEntityPath).FindComponent <GameplayBehavior>(); this.gamePlay.Player = this.Owner; this.gamePlay.GameReset += this.GamePlayGameReset; } }
/** 绑定节点 */ public void setNode(UIContainer node) { _rootTransform = (RectTransform)node.gameObject.transform; _rootPos = _rootTransform.position; JoystickComponent joyComponent = node.gameObject.AddComponent <JoystickComponent>(); joyComponent.logic = this; _chassis = node.getChild <UIImage>("chassis").image; _chassisTransform = _chassis.rectTransform; _thumb = _chassisTransform.Find("thumb").GetComponent <Image>(); _thumbTransform = _thumb.rectTransform; Vector2 halfThumbSize = _thumbTransform.rect.size / 2; Rect rect; UIObject rectObj = node.getChild("rect"); if (rectObj != null) { rect = rectObj.transform.rect; Vector2 rectLocalPos = rectObj.transform.localPosition; rect.x += rectLocalPos.x; rect.y += rectLocalPos.y; } else { rect = _rootTransform.rect; rect.x = 0; rect.y = 0; } Vector2 chassisSize = _chassisTransform.rect.size; Vector2 halfChassisSize = chassisSize / 2; thumbRadius = Math.Max(halfChassisSize.x, halfChassisSize.y) - Math.Max(halfThumbSize.x, halfThumbSize.y); _chassisRange = new Rect(rect.x + halfChassisSize.x, rect.y + halfChassisSize.y, rect.width - chassisSize.x, rect.height - chassisSize.y); if (_chassisRange.width < 0 || _chassisRange.height < 0) { Ctrl.throwError("资源位置出错"); } _chassisOriginPos = _chassisTransform.localPosition; setStopRadius(_stopRadius); setAlpha(false); }
internal void SetControlJoyCustomData(Guid device, JoystickComponent component, int element, int dir) { if (isCustomisingControl && CustomControlIdx < Interface.CurrentControls.Length) { if (Program.Joysticks.AttachedJoysticks[device] is AbstractRailDriver) { Interface.CurrentControls[CustomControlIdx].Method = ControlMethod.RailDriver; } else { Interface.CurrentControls[CustomControlIdx].Method = ControlMethod.Joystick; } Interface.CurrentControls[CustomControlIdx].Device = device; Interface.CurrentControls[CustomControlIdx].Component = component; Interface.CurrentControls[CustomControlIdx].Element = element; Interface.CurrentControls[CustomControlIdx].Direction = dir; Interface.SaveControls(null, Interface.CurrentControls); PopMenu(); isCustomisingControl = false; } }
// attached joysticks private void pictureboxJoysticks_Paint(object sender, PaintEventArgs e) { this.DoubleBuffered = true; int device = -1; JoystickComponent component = JoystickComponent.Invalid; int element = -1; int direction = -1; Translations.CommandType type = Translations.CommandType.Digital; if (this.Tag == null & listviewControls.SelectedIndices.Count == 1) { int j = listviewControls.SelectedIndices[0]; if (Interface.CurrentControls[j].Method == ControlMethod.Joystick && JoystickManager.AttachedJoysticks.ContainsKey(Interface.CurrentControls[j].Device)) { device = JoystickManager.AttachedJoysticks[Interface.CurrentControls[j].Device].Handle; component = Interface.CurrentControls[j].Component; element = Interface.CurrentControls[j].Element; direction = Interface.CurrentControls[j].Direction; type = Interface.CurrentControls[j].InheritedType; } } System.Globalization.CultureInfo Culture = System.Globalization.CultureInfo.InvariantCulture; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; Font f = new Font(this.Font.Name, 0.875f * this.Font.Size); float x = 2.0f, y = 2.0f; float threshold = ((float)trackbarJoystickAxisThreshold.Value - (float)trackbarJoystickAxisThreshold.Minimum) / (float)(trackbarJoystickAxisThreshold.Maximum - trackbarJoystickAxisThreshold.Minimum); for (int i = 0; i < JoystickManager.AttachedJoysticks.Count; i++) { Guid guid = JoystickManager.AttachedJoysticks.ElementAt(i).Key; JoystickManager.AttachedJoysticks[guid].Poll(); float w, h; if (JoystickImage != null) { e.Graphics.DrawImage(JoystickImage, x, y); w = (float)JoystickImage.Width; h = (float)JoystickImage.Height; if (h < 64.0f) { h = 64.0f; } } else { w = 64.0f; h = 64.0f; e.Graphics.DrawRectangle(new Pen(labelControlsTitle.BackColor), x, y, w, h); } { // joystick number e.Graphics.FillEllipse(Brushes.Gold, x + w - 16.0f, y, 16.0f, 16.0f); e.Graphics.DrawEllipse(Pens.Black, x + w - 16.0f, y, 16.0f, 16.0f); string t = (i + 1).ToString(Culture); SizeF s = e.Graphics.MeasureString(t, f); e.Graphics.DrawString(t, f, Brushes.Black, x + w - 8.0f - 0.5f * s.Width, y + 8.0f - 0.5f * s.Height); } { // joystick name e.Graphics.DrawString(JoystickManager.AttachedJoysticks[guid].Name, this.Font, Brushes.Black, x + w + 8.0f, y); } if (OpenTK.Configuration.RunningOnSdl2) { //HACK: Control configuration doesn't work in-form on SDL2 string error = Translations.GetInterfaceString("errors_controls_ingame"); if (OpenTK.Configuration.RunningOnSdl2) { error = error.Replace("[platform]", "SDL2"); } e.Graphics.DrawString(error, this.Font, Brushes.Black, x + w + 8.0f, y + 30.0f); return; } float m; if (groupboxJoysticks.Enabled) { m = x; Pen p = new Pen(Color.DarkGoldenrod, 2.0f); Pen ps = new Pen(Color.Firebrick, 2.0f); { // first row float u = x + w + 8.0f; float v = y + 24.0f; float g = h - 24.0f; { // hats int n = JoystickManager.AttachedJoysticks[guid].HatCount(); for (int j = 0; j < n; j++) { if (device == i & component == JoystickComponent.Hat & element == j) { e.Graphics.DrawEllipse(ps, u, v, g, g); } else { e.Graphics.DrawEllipse(p, u, v, g, g); } string t = "H" + (j + 1).ToString(Culture); SizeF s = e.Graphics.MeasureString(t, f); e.Graphics.DrawString(t, f, Brushes.Black, u + 0.5f * (g - s.Width), v + 0.5f * (g - s.Height)); JoystickHatState aa = JoystickManager.AttachedJoysticks[guid].GetHat(j); HatPosition a = aa.Position; if (a != HatPosition.Centered) { double rx = 0.0; double ry = 0.0; switch (a) { case HatPosition.Up: rx = 0.0; ry = -1.0; break; case HatPosition.Down: rx = 0.0; ry = 1.0; break; case HatPosition.Left: rx = -1.0; ry = 0.0; break; case HatPosition.Right: rx = 1.0; ry = 0.0; break; case HatPosition.UpLeft: rx = -1.0; ry = -1.0; break; case HatPosition.UpRight: rx = 1.0; ry = -1.0; break; case HatPosition.DownLeft: rx = -1.0; ry = 1.0; break; case HatPosition.DownRight: rx = 1.0; ry = 1.0; break; } double rt = rx * rx + ry * ry; rt = 1.0 / Math.Sqrt(rt); rx *= rt; ry *= rt; float dx = (float)(0.5 * rx * (g - 8.0)); float dy = (float)(0.5 * ry * (g - 8.0)); e.Graphics.FillEllipse(Brushes.White, u + 0.5f * g + dx - 4.0f, v + 0.5f * g + dy - 4.0f, 8.0f, 8.0f); e.Graphics.DrawEllipse(new Pen(Color.Firebrick, 2.0f), u + 0.5f * g + dx - 4.0f, v + 0.5f * g + dy - 4.0f, 8.0f, 8.0f); } if (device == i & component == JoystickComponent.Hat & element == j) { double rx = ((HatPosition)direction & HatPosition.Left) != 0 ? -1.0 : ((HatPosition)direction & HatPosition.Right) != 0 ? 1.0 : 0.0; double ry = ((HatPosition)direction & HatPosition.Up) != 0 ? -1.0 : ((HatPosition)direction & HatPosition.Down) != 0 ? 1.0 : 0.0; double rt = rx * rx + ry * ry; rt = 1.0 / Math.Sqrt(rt); rx *= rt; ry *= rt; float dx = (float)(0.5 * rx * (g - 8.0)); float dy = (float)(0.5 * ry * (g - 8.0)); e.Graphics.FillEllipse(Brushes.Firebrick, u + 0.5f * g + dx - 2.0f, v + 0.5f * g + dy - 2.0f, 4.0f, 4.0f); } u += g + 8.0f; } } if (u > m) { m = u; } } { // second row float u = x; float v = y + h + 8.0f; { // axes int n = JoystickManager.AttachedJoysticks[guid].AxisCount(); float g = (float)pictureboxJoysticks.ClientRectangle.Height - v - 2.0f; for (int j = 0; j < n; j++) { float r = (float)JoystickManager.AttachedJoysticks[guid].GetAxis(j); float r0 = r < 0.0f ? r : 0.0f; float r1 = r > 0.0f ? r : 0.0f; if ((float)Math.Abs((double)r) < threshold) { e.Graphics.FillRectangle(Brushes.RosyBrown, u, v + 0.5f * g - 0.5f * r1 * g, 16.0f, 0.5f * g * (r1 - r0)); } else { e.Graphics.FillRectangle(Brushes.Firebrick, u, v + 0.5f * g - 0.5f * r1 * g, 16.0f, 0.5f * g * (r1 - r0)); } if (device == i & component == JoystickComponent.Axis & element == j) { if (direction == -1 & type != Translations.CommandType.AnalogFull) { e.Graphics.DrawRectangle(p, u, v, 16.0f, g); e.Graphics.DrawRectangle(ps, u, v + 0.5f * g, 16.0f, 0.5f * g); } else if (direction == 1 & type != Translations.CommandType.AnalogFull) { e.Graphics.DrawRectangle(p, u, v, 16.0f, g); e.Graphics.DrawRectangle(ps, u, v, 16.0f, 0.5f * g); } else { e.Graphics.DrawRectangle(ps, u, v, 16.0f, g); } } else { e.Graphics.DrawRectangle(p, u, v, 16.0f, g); } e.Graphics.DrawLine(p, u, v + (0.5f - 0.5f * threshold) * g, u + 16.0f, v + (0.5f - 0.5f * threshold) * g); e.Graphics.DrawLine(p, u, v + (0.5f + 0.5f * threshold) * g, u + 16.0f, v + (0.5f + 0.5f * threshold) * g); string t = "A" + (j + 1).ToString(Culture); SizeF s = e.Graphics.MeasureString(t, f); e.Graphics.DrawString(t, f, Brushes.Black, u + 0.5f * (16.0f - s.Width), v + g - s.Height - 2.0f); u += 24.0f; } } { // buttons int n = JoystickManager.AttachedJoysticks[guid].ButtonCount(); float g = (float)0.5f * (pictureboxJoysticks.ClientRectangle.Height - v - 10.0f); for (int j = 0; j < n; j++) { bool q = JoystickManager.AttachedJoysticks[guid].GetButton(j) != 0; float dv = (float)(j & 1) * (g + 8.0f); if (q) { e.Graphics.FillRectangle(Brushes.Firebrick, u, v + dv, g, g); } if (device == i & component == JoystickComponent.Button & element == j) { e.Graphics.DrawRectangle(ps, u, v + dv, g, g); } else { e.Graphics.DrawRectangle(p, u, v + dv, g, g); } string t = "B" + (j + 1).ToString(Culture); SizeF s = e.Graphics.MeasureString(t, f); e.Graphics.DrawString(t, f, Brushes.Black, u + 0.5f * (g - s.Width), v + dv + 0.5f * (g - s.Height)); if ((j & 1) != 0 | j == n - 1) { u += g + 8.0f; } } } if (u > m) { m = u; } } } else { m = x + w + 64.0f; } x = m + 8.0f; } }