Ooops
November 25, 2020, 06:12:38 AM

Author Topic: [bb] Dual Stick Config by Pongo [ 1+ years ago ]  (Read 549 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Dual Stick Config by Pongo [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Dual Stick Config
Author : Pongo
Posted : 1+ years ago

Description : Since every dual stick joypad seems to use a different axis combination, I needed a way to assign custom axis controls.

run the config program to generate a joypad.cfg command, then read that file into your own program.


Code :
Code: BlitzBasic
  1. Graphics 640,480,0,2
  2. SetBuffer BackBuffer()
  3. HidePointer
  4.  
  5. Global Controller_port = 0
  6. Global debug = 0
  7.  
  8. Global info$ = " "
  9.  
  10. Type joypad
  11.         Field x1id
  12.         Field y1id
  13.         Field x2id
  14.         Field y2id
  15.  
  16.         Field x1invert
  17.         Field y1invert
  18.         Field x2invert
  19.         Field y2invert
  20. End Type
  21.  
  22. Dim j.joypad(3)
  23.  
  24. For port = 0 To 3
  25.         j(port) = New joypad
  26.         j(port)x1id = 0
  27.         j(port)y1id = 0
  28.         j(port)x2id = 0
  29.         j(port)y2id = 0
  30.         j(port)x1invert = 1 ;toggles between 1 and -1
  31.         j(port)y1invert = 1
  32.         j(port)x2invert = 1
  33.         j(port)y2invert = 1
  34. Next
  35.  
  36.  
  37. Dim joy_label$(8)
  38. joy_label(0) = "null"
  39. joy_label(1) = "JoyX()"
  40. joy_label(2) = "JoyY()"
  41. joy_label(3) = "JoyZ()"
  42. joy_label(4) = "JoyPitch()"
  43. joy_label(5) = "JoyRoll()"
  44. joy_label(6) = "JoyYaw()"
  45. joy_label(7) = "JoyU()"
  46. joy_label(8) = "JoyV()"
  47.  
  48. While Not KeyHit(16) ; Q for Quit
  49.         Cls
  50.        
  51.         drawstuff()
  52.         checkinput()
  53.  
  54.         ; map the input values so the red dots can be drawn in position
  55.         x1 = FitValueToRange#( MapAxis(j(Controller_port)x1id,j(Controller_port)x1invert), -1, 1, 50, 150 )
  56.         y1=     FitValueToRange#( MapAxis(j(Controller_port)y1id,j(Controller_port)y1invert), -1,1, 275, 375 )
  57.         x2 = FitValueToRange#( MapAxis(j(Controller_port)x2id,j(Controller_port)x2invert), -1, 1, 250, 350 )
  58.         y2=     FitValueToRange#( MapAxis(j(Controller_port)y2id,j(Controller_port)y2invert), -1,1, 275, 375 )
  59.  
  60.         ;draw the control dots
  61.         Color 185,0,0
  62.         Oval  x1-5,y1-5,10,10
  63.         Oval  x2-5,y2-5,10,10
  64.  
  65.         ;debug and cursor
  66.         ;Text MouseX()+5,MouseY()-15,MouseX()+","+MouseY()
  67.         Line MouseX(),MouseY()-5,MouseX(),MouseY()+5
  68.         Line MouseX()-5,MouseY(),MouseX()+5,MouseY()
  69.  
  70.         Flip
  71.  
  72. Wend
  73.  
  74. End
  75.  
  76. Function FitValueToRange#( InValue#, RangeIn_Start#, RangeIn_End#, RangeOut_Start#, RangeOut_End# )
  77.        
  78.         OldRange# = RangeIn_End#-RangeIn_Start#
  79.         NewRange# = RangeOut_End# - RangeOut_Start#    
  80.        
  81.         OutValue# = ((InValue#-RangeIn_Start) / OldRange#) * NewRange#  + RangeOut_Start               
  82.  
  83.         Return OutValue#
  84.  
  85. End Function
  86.  
  87. Function drawstuff()
  88.         Color 255,255,255
  89.         Text 10,10,"Dual stick configuration utility"
  90.         Text 10,25,"Directions for use:"
  91.  
  92.         Color 35,65,115
  93.         Select controller_port ; draw current port selected
  94.                 Case 0
  95.                         Rect 10,45,45,25
  96.                 Case 1
  97.                         Rect 60,45,45,25
  98.                 Case 2
  99.                         Rect 110,45,45,25
  100.                 Case 3
  101.                         Rect 160,45,45,25
  102.         End Select
  103.  
  104.         Color 0,0,255
  105.         Rect 10,45,45,25,0 ; draw ports (outlines)
  106.         Rect 60,45,45,25,0
  107.         Rect 110,45,45,25,0
  108.         Rect 160,45,45,25,0
  109.  
  110.         Color 255,255,255      
  111.         Text 27,53,"0"
  112.         Text 77,53,"1"
  113.         Text 127,53,"2"
  114.         Text 177,53,"3"
  115.  
  116.         Text 220,45,"select controller port (0 for  single joypad)"
  117.         Text 220,60,"others are only needed for multiple joypads"
  118.  
  119.         Color 185,0,0
  120.         Rect 10,100,195,25
  121.  
  122.         Color 255,255,255
  123.         Rect 10,100,195,25,0
  124.         Text 25,105,"assign axis controls"
  125.  
  126.         Text 220,105,"click to assign the custom controls"
  127.  
  128.         Rect 10,135,600,100,0 ; large text info box outline
  129.         Color 128,128,128
  130.         Text 15,140,"After assigning the controls, test stick movement in the boxes below"
  131.         Text 15,155,"To invert an axis, right click on it's blue box"
  132.         Text 15,180,"when done, save the config file and exit, or repeat with the other ports"
  133.  
  134.         ;draw boxes for axis controls
  135.         Color 50,65,220
  136.         If j(Controller_port)x1invert =-1 Then Color 255,128,0
  137.         Rect 50,395,175,14,1
  138.  
  139.         Color 50,65,220
  140.         If j(Controller_port)y1invert =-1 Then Color 255,128,0
  141.         Rect 50,415,175,14,1
  142.  
  143.         Color 50,65,220
  144.         If j(Controller_port)x2invert =-1 Then Color 255,128,0
  145.         Rect 250,395,175,14,1
  146.  
  147.         Color 50,65,220
  148.         If j(Controller_port)y2invert =-1 Then Color 255,128,0
  149.         Rect 250,415,175,14,1
  150.  
  151.         Color 255,255,255
  152.         Text 50,380,"Stick 1" :         Text 250,380,"Stick 2"
  153.         Text 60,396,"X axis: " + joy_label(j(Controller_port)x1id) : Text 260,396,"X axis: " + joy_label(j(Controller_port)x2id)
  154.         Text 60,416,"Y axis: " + joy_label(j(Controller_port)y1id) : Text 260,416,"Y axis: " + joy_label(j(Controller_port)y2id)
  155.  
  156.         Rect 50,275, 100,100,0 ;draw boxes for controllers
  157.         Rect 250,275, 100,100,0
  158.  
  159.         Color 64,64,64
  160.         Rect 450,275,110,20
  161.         Rect 450,300,110,20
  162.         Rect 450,325,110,20
  163.         Rect 450,350,110,20
  164.  
  165.         Color 255,255,255
  166.         Text 453,277,"(Tab) Debug"
  167.         Text 453,302,"(S)ave Config"
  168.         Text 453,327,"(L)oad Config"
  169.         Text 453,352,"(Q)uit"
  170.  
  171. Text 220,250,info$
  172.  
  173.         If debug = 1
  174.                         Color 16,16,16
  175.                         Rect 0,25,640,217
  176.                         Color 48,48,48
  177.                         Rect 40,30,300,200
  178.                         Color 64,64,64 ; draw grey boxes
  179.                         For loop = 50 To 190 Step 20
  180.                                 Rect 180,loop,150,15
  181.                         Next
  182.                         Rect 40,30,300,200,0
  183.                         Rect 39,29,302,202,0
  184.        
  185.                         Color 128,128,128 ; draw boxes showing movement of axis
  186.                         Rect 180,50,FitValueToRange#( JoyX(Controller_port), -1, 1, 0, 150 ),15
  187.                         Rect 180,70,FitValueToRange#( JoyY(Controller_port), -1, 1, 0, 150 ),15
  188.                         Rect 180,90,FitValueToRange#( JoyZ(Controller_port), -1, 1, 0, 150 ),15
  189.                         Rect 180,110,FitValueToRange#( JoyPitch(Controller_port), -180, 180, 0, 150 ),15
  190.                         Rect 180,130,FitValueToRange#( JoyRoll(Controller_port), -180, 180, 0, 150 ),15
  191.                         Rect 180,150,FitValueToRange#( JoyYaw(Controller_port), -180, 180, 0, 150 ),15
  192.                         Rect 180,170,FitValueToRange#( JoyU(Controller_port), -1, 1, 0, 150 ),15
  193.                         Rect 180,190,FitValueToRange#( JoyV(Controller_port), -1, 1, 0, 150 ),15
  194.                
  195.                         Color 160,160,160 ; show values of axis
  196.                         Text 50,50, "1. JoyX()      : " + JoyX(Controller_port)
  197.                         Text 50,70, "2. JoyY()      : " + JoyY(Controller_port)
  198.                         Text 50,90, "3. JoyZ()      : " + JoyZ(Controller_port)
  199.                         Text 50,110,"4. JoyPitch()  : " + JoyPitch(Controller_port)
  200.                         Text 50,130,"5. JoyRoll()   : " + JoyRoll(Controller_port)
  201.                         Text 50,150,"6. JoyYaw()    : " + JoyYaw(Controller_port)
  202.                         Text 50,170,"7. JoyU()      : " + JoyU(Controller_port)
  203.                         Text 50,190,"8. JoyV()      : " + JoyV(Controller_port)
  204.         EndIf
  205. End Function
  206.  
  207. Function checkinput()
  208.         Color 255,0,0
  209.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,50,395,175,14) ; joy x 1
  210.                 Rect 49,394,177,16,0 ; draw a highlight rectangle
  211.                 j(Controller_port)x1id = j(Controller_port)x1id +MouseZSpeed ()
  212.                 If MouseHit (2) Then j(Controller_port)x1invert = - j(Controller_port)x1invert
  213.                 If j(Controller_port)x1id > 8 Then j(Controller_port)x1id =0
  214.                 If j(Controller_port)x1id < 0 Then j(Controller_port)x1id =8
  215.         EndIf
  216.  
  217.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,50,415,175,14) ; joy y 1
  218.                 Rect 49,414,177,16,0    ; draw a highlight rectangle
  219.                 j(Controller_port)y1id = j(Controller_port)y1id +MouseZSpeed ()
  220.                 If MouseHit (2) Then j(Controller_port)y1invert = - j(Controller_port)y1invert
  221.                 If j(Controller_port)y1id > 8 Then j(Controller_port)y1id =0
  222.                 If j(Controller_port)y1id < 0 Then j(Controller_port)y1id =8
  223.         EndIf
  224.  
  225.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,250,395,175,14) ; joy x 2
  226.                 Rect 249,394,177,16,0   ; draw a highlight rectangle
  227.                 j(Controller_port)x2id = j(Controller_port)x2id +MouseZSpeed ()
  228.                 If MouseHit (2) Then j(Controller_port)x2invert = - j(Controller_port)x2invert
  229.                 If j(Controller_port)x2id > 8 Then j(Controller_port)x2id =0
  230.                 If j(Controller_port)x2id < 0 Then j(Controller_port)x2id =8
  231.         EndIf
  232.  
  233.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,250,415,175,14) ; joy y 2
  234.                 Rect 249,414,177,16,0   ; draw a highlight rectangle
  235.                 j(Controller_port)y2id = j(Controller_port)y2id +MouseZSpeed ()
  236.                 If MouseHit (2) Then j(Controller_port)y2invert = - j(Controller_port)y2invert
  237.                 If j(Controller_port)y2id > 8 Then j(Controller_port)y2id =0
  238.                 If j(Controller_port)y2id < 0 Then j(Controller_port)y2id =8
  239.         EndIf
  240.  
  241.         ;joy port selection
  242.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,10,45,45,25)
  243.                 Rect 10,45,45,25,0
  244.                 If MouseHit (1) Then controller_port = 0
  245.         EndIf
  246.  
  247.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,60,45,45,25)
  248.                 Rect 60,45,45,25,0
  249.                 If MouseHit (1) Then controller_port = 1
  250.         EndIf
  251.  
  252.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,110,45,45,25)
  253.                 Rect 110,45,45,25,0
  254.                 If MouseHit (1) Then controller_port = 2
  255.         EndIf
  256.  
  257.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,160,45,45,25)
  258.                 Rect 160,45,45,25,0
  259.                 If MouseHit (1) Then controller_port = 3
  260.         EndIf
  261.  
  262.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,10,100,195,25) ; assign all axis button
  263.                 Rect 10,100,195,25,0
  264.                 If MouseHit (1) Then assign_all_joy_axis()
  265.         EndIf
  266.  
  267.         If KeyHit (59) Then Controller_port = 0 ;F1 key
  268.         If KeyHit (60) Then Controller_port = 1 ;F2 key
  269.         If KeyHit (61) Then Controller_port = 2 ;F3 key
  270.         If KeyHit (62) Then Controller_port = 3 ;F4 key
  271.  
  272.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,450,275,110,20) ; debug toggle
  273.                 Rect 450,275,110,20,0
  274.                 If MouseHit (1) Then debug = 1 - Debug
  275.         EndIf
  276.  
  277.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,450,300,110,20) ; save button
  278.                 Rect 450,300,110,20,0
  279.                 If MouseHit (1) Then writeconfig()
  280.         EndIf
  281.  
  282.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,450,325,110,20) ;load button
  283.                 Rect 450,325,110,20,0
  284.                 If MouseHit (1) Then readconfig()
  285.         EndIf
  286.  
  287.         If RectsOverlap  (MouseX()-2,MouseY()-2,4,4,450,350,110,20) ; quit button
  288.                 Rect 450,350,110,20,0
  289.                 If MouseHit (1) Then End
  290.         EndIf
  291.  
  292.         If KeyHit (15) Then debug = 1 - Debug ; tab key,... turns on debug
  293.  
  294.         If KeyHit (31) ; S key
  295.                 writeconfig()
  296.         EndIf
  297.  
  298.         If KeyHit (38) ; L key
  299.                 readconfig()
  300.         EndIf
  301.  
  302. End Function
  303.  
  304. Function getjoyaxis()
  305. ;why the double check?
  306. ; on some joysticks, when an axis is not present, the value is the max, so  I needed to get around that.
  307. Repeat
  308.         If Abs(JoyX(Controller_port)) > .5 And Abs(JoyX(Controller_port)) < .9 Return 1
  309.         If Abs(JoyY(Controller_port)) > .5 And Abs(JoyY(Controller_port)) < .9 Return 2
  310.         If Abs(JoyZ(Controller_port)) > .5 And Abs(JoyZ(Controller_port)) < .9 Return 3
  311.         If Abs(JoyPitch(Controller_port)/180) > .5 And Abs(JoyPitch(Controller_port)/180) < .9 Return 4
  312.         If Abs(JoyRoll(Controller_port)/180) > .5 And Abs(JoyRoll(Controller_port)/180) < .9 Return 5
  313.         If Abs(JoyYaw(Controller_port)/180) > .5 And Abs(JoyYaw(Controller_port)/180) < .9 Return 6
  314.         If Abs(JoyU(Controller_port)) > .5 And Abs(JoyU(Controller_port)) < .9 Return 7
  315.         If Abs(JoyV(Controller_port)) > .5 And Abs(JoyV(Controller_port)) < .9 Return 8
  316.         If KeyHit(1)    Return 0
  317. Forever
  318. End Function
  319.  
  320. Function MapAxis#( axis, invert )
  321.    Select axis
  322.       Case 1
  323.          joy#=JoyX(Controller_port)
  324.           Case 2
  325.          joy#=JoyY(Controller_port)
  326.       Case 3
  327.          joy#=JoyZ(Controller_port)
  328.       Case 4
  329.          joy#=JoyPitch(Controller_port)/180
  330.       Case 5
  331.          joy#=JoyRoll(Controller_port)/180
  332.       Case 6
  333.          joy#=JoyYaw(Controller_port)/180
  334.       Case 7
  335.          joy#=JoyU(Controller_port)
  336.       Case 8
  337.          joy#=JoyV(Controller_port)
  338.    End Select
  339.    Return (joy# * invert)
  340. End Function
  341.  
  342. Function Assign_All_Joy_Axis()
  343. SetBuffer FrontBuffer()
  344.  
  345. Color 0,0,0
  346. Rect 11,136,598,98 ;clear info box
  347.  
  348. Color 0,255,0
  349.         Text 15,140,"move stick 1 left or right"
  350.         j(Controller_port)x1id = getjoyaxis()
  351.         Text 250,140,"Stick 1 X axis assigned to " + joy_label(j(Controller_port)x1id)
  352.  
  353.         Delay (800)
  354.        
  355.         Text 15,155,"move stick 1 up or down"
  356.         j(Controller_port)y1id = getjoyaxis()
  357.         Text 250,155,"Stick 1 Y axis assigned to " + joy_label(j(Controller_port)y1id)
  358.  
  359.         Delay (800)
  360.  
  361.         Text 15,170,"move stick 2 left or right"
  362.          j(Controller_port)x2id = getjoyaxis()
  363.         Text 250,170,"Stick 2 X axis assigned to " + joy_label(j(Controller_port)x2id)
  364.  
  365.         Delay (800)
  366.  
  367.  
  368.         Text 15,185,"move stick 2 up or down"
  369.          j(Controller_port)y2id = getjoyaxis()
  370.         Text 250,185,"Stick 2 Y axis assigned to " + joy_label(j(Controller_port)y2id)
  371.  
  372.         Delay (500)
  373.  
  374. SetBuffer BackBuffer()
  375.  
  376. End Function
  377.  
  378. Function writeconfig()
  379. filename = WriteFile ("joypad.cfg")
  380. For port = 0 To 3
  381.         WriteLine (filename,"; Joypad port " + port)
  382.         WriteLine (filename,j(port)x1id)
  383.         WriteLine (filename,j(port)y1id)
  384.         WriteLine (filename,j(port)x2id)
  385.         WriteLine (filename,j(port)y2id)
  386.         WriteLine (filename,j(port)x1invert)
  387.         WriteLine (filename,j(port)y1invert)
  388.         WriteLine (filename,j(port)x2invert)
  389.         WriteLine (filename,j(port)y2invert)
  390. Next
  391. CloseFile (filename)
  392.  
  393. info$ = "config file written"
  394.  
  395. End Function
  396.  
  397. Function readconfig()
  398. filename = ReadFile ("joypad.cfg")
  399. If filename <> 0
  400.         For port = 0 To 3
  401.                 tmp = ReadLine (filename) ; skip this line in the file
  402.                 j(port)x1id = ReadLine (filename)
  403.                 j(port)y1id = ReadLine (filename)
  404.                 j(port)x2id  = ReadLine (filename)
  405.                 j(port)y2id = ReadLine (filename)
  406.                 j(port)x1invert = ReadLine (filename)
  407.                 j(port)y1invert = ReadLine (filename)
  408.                 j(port)x2invert = ReadLine (filename)
  409.                 j(port)y2invert = ReadLine (filename)
  410.         Next
  411.  
  412.         CloseFile (filename)
  413.  
  414.         info$ = "config file opened"
  415. Else
  416.         info$ = "load failed"
  417. EndIf
  418. End Function


Comments :


Pongo(Posted 1+ years ago)

 And here is a simple example that reads a joypad.cfg file and uses it
Code: [Select]

Graphics 640,480,0,2
SetBuffer BackBuffer()

Global Controller_port = 0
Global info$ = " "

Type joypad
Field x1id
Field y1id
Field x2id
Field y2id
Field x1invert
Field y1invert
Field x2invert
Field y2invert
End Type

Dim j.joypad(3)

For port = 0 To 3
j(port) = New joypad
j(port)x1id = 0
j(port)y1id = 0
j(port)x2id = 0
j(port)y2id = 0
j(port)x1invert = 1 ;toggles between 1 and -1
j(port)y1invert = 1
j(port)x2invert = 1
j(port)y2invert = 1
Next

readconfig()

While Not KeyHit(1) ; start main loop esc for Quit
Cls
Text 50,30,"Use the MapAxis Function to grab the inputs"
Text 50,50,info$

Text 50,70,"X1 axis :" + MapAxis(j(Controller_port)x1id,j(Controller_port)x1invert)
Text 50,90,"Y1 axis :" + MapAxis(j(Controller_port)y1id,j(Controller_port)y1invert)
Text 50,110,"X2 axis :" + MapAxis(j(Controller_port)x2id,j(Controller_port)x2invert)
Text 50,130,"Y2 axis :" + MapAxis(j(Controller_port)y2id,j(Controller_port)y2invert)

Flip
Wend  ; end main loop

End ; end program

Function MapAxis#( axis, invert )
   Select axis
      Case 1
         joy#=JoyX(Controller_port)
          Case 2
         joy#=JoyY(Controller_port)
      Case 3
         joy#=JoyZ(Controller_port)
      Case 4
         joy#=JoyPitch(Controller_port)/180
      Case 5
         joy#=JoyRoll(Controller_port)/180
      Case 6
         joy#=JoyYaw(Controller_port)/180
      Case 7
         joy#=JoyU(Controller_port)
      Case 8
         joy#=JoyV(Controller_port)
   End Select
   Return (joy# * invert)
End Function

Function readconfig()
filename = ReadFile ("joypad.cfg")
If filename <> 0
For port = 0 To 3
tmp = ReadLine (filename) ; skip this line in the file
j(port)x1id = ReadLine (filename)
j(port)y1id = ReadLine (filename)
j(port)x2id  = ReadLine (filename)
j(port)y2id = ReadLine (filename)
j(port)x1invert = ReadLine (filename)
j(port)y1invert = ReadLine (filename)
j(port)x2invert = ReadLine (filename)
j(port)y2invert = ReadLine (filename)
Next
CloseFile (filename)
info$ = "config file opened"
Else
info$ = "load failed, or no .cfg file present"
EndIf
End Function




RiK(Posted 1+ years ago)

 Very useful. I've got something similar I did a while back with one main difference that I also included a scaling factor as not all sticks return +/- 1. Some return +/- 128 or +/- 255.


xtremegamr(Posted 1+ years ago)

 Thanks! I've been looking for a way to make the right stick on my Xbox 360 Controller work!


Blitzplotter(Posted 1+ years ago)

 Thankyou very much, just what I was looking for (;-o)


superStruct(Posted 1+ years ago)

 Dude you are a genius. It took me a little while to get through the code and it is AMAZING. Nice work! [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal