// attached joysticks private void pictureboxJoysticks_Paint(object sender, PaintEventArgs e) { int device = -1; Interface.JoystickComponent component = Interface.JoystickComponent.Invalid; int element = -1; int direction = -1; Interface.CommandType type = Interface.CommandType.Digital; if (this.Tag == null & listviewControls.SelectedIndices.Count == 1) { int j = listviewControls.SelectedIndices[0]; if (Interface.CurrentControls[j].Method == Interface.ControlMethod.Joystick) { device = Interface.CurrentControls[j].Device; component = Interface.CurrentControls[j].Component; element = Interface.CurrentControls[j].Element; direction = Interface.CurrentControls[j].Direction; type = Interface.CurrentControls[j].InheritedType; } } Sdl.SDL_JoystickUpdate(); 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 < Joysticks.AttachedJoysticks.Length; i++) { 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(Joysticks.AttachedJoysticks[i].Name, this.Font, Brushes.Black, x + w + 8.0f, y); } 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; { /// trackballs int n = Sdl.SDL_JoystickNumBalls(Joysticks.AttachedJoysticks[i].SdlHandle); for (int j = 0; j < n; j++) { e.Graphics.DrawEllipse(Pens.Gray, u, v, g, g); string t = "L" + (j + 1).ToString(Culture); SizeF s = e.Graphics.MeasureString(t, f); e.Graphics.DrawString(t, f, Brushes.Gray, u + 0.5f * (g - s.Width), v + 0.5f * (g - s.Height)); int dx, dy; Sdl.SDL_JoystickGetBall(Joysticks.AttachedJoysticks[i].SdlHandle, j, out dx, out dy); u += g + 8.0f; } } { /// hats int n = Sdl.SDL_JoystickNumHats(Joysticks.AttachedJoysticks[i].SdlHandle); for (int j = 0; j < n; j++) { if (device == i & component == Interface.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)); byte a = Sdl.SDL_JoystickGetHat(Joysticks.AttachedJoysticks[i].SdlHandle, j); if (a != Sdl.SDL_HAT_CENTERED) { double rx = (a & Sdl.SDL_HAT_LEFT) != 0 ? -1.0 : (a & Sdl.SDL_HAT_RIGHT) != 0 ? 1.0 : 0.0; double ry = (a & Sdl.SDL_HAT_UP) != 0 ? -1.0 : (a & Sdl.SDL_HAT_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.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 == Interface.JoystickComponent.Hat & element == j) { double rx = (direction & Sdl.SDL_HAT_LEFT) != 0 ? -1.0 : (direction & Sdl.SDL_HAT_RIGHT) != 0 ? 1.0 : 0.0; double ry = (direction & Sdl.SDL_HAT_UP) != 0 ? -1.0 : (direction & Sdl.SDL_HAT_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 = Sdl.SDL_JoystickNumAxes(Joysticks.AttachedJoysticks[i].SdlHandle); float g = (float)pictureboxJoysticks.ClientRectangle.Height - v - 2.0f; for (int j = 0; j < n; j++) { float r = (float)Sdl.SDL_JoystickGetAxis(Joysticks.AttachedJoysticks[i].SdlHandle, j) / 32768.0f; 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 == Interface.JoystickComponent.Axis & element == j) { if (direction == -1 & type != Interface.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 != Interface.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 = Sdl.SDL_JoystickNumButtons(Joysticks.AttachedJoysticks[i].SdlHandle); float g = (float)0.5f * (pictureboxJoysticks.ClientRectangle.Height - v - 10.0f); for (int j = 0; j < n; j++) { bool q = Sdl.SDL_JoystickGetButton(Joysticks.AttachedJoysticks[i].SdlHandle, 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 == Interface.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; } }
// attached joysticks private void pictureboxJoysticks_Paint(object sender, PaintEventArgs e) { this.DoubleBuffered = true; int device = -1; Interface.JoystickComponent component = Interface.JoystickComponent.Invalid; int element = -1; int direction = -1; Interface.CommandType type = Interface.CommandType.Digital; if (this.Tag == null & listviewControls.SelectedIndices.Count == 1) { int j = listviewControls.SelectedIndices[0]; if (Interface.CurrentControls[j].Method == Interface.ControlMethod.Joystick) { device = Interface.CurrentControls[j].Device; 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 < Joysticks.AttachedJoysticks.Length; i++) { //Get the current joystick state var capabilities = Joystick.GetCapabilities(Joysticks.AttachedJoysticks[i].OpenTKHandle); var state = Joystick.GetState(Joysticks.AttachedJoysticks[i].OpenTKHandle); 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(Joysticks.AttachedJoysticks[i].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 = Interface.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 = capabilities.HatCount; for (int j = 0; j < n; j++) { if (device == i & component == Interface.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 = state.GetHat((JoystickHat)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 == Interface.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 = capabilities.AxisCount; float g = (float)pictureboxJoysticks.ClientRectangle.Height - v - 2.0f; for (int j = 0; j < n; j++) { float r = state.GetAxis((JoystickAxis)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 == Interface.JoystickComponent.Axis & element == j) { if (direction == -1 & type != Interface.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 != Interface.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 = capabilities.ButtonCount; float g = (float)0.5f * (pictureboxJoysticks.ClientRectangle.Height - v - 10.0f); for (int j = 0; j < n; j++) { bool q = state.GetButton((JoystickButton)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 == Interface.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; } }