Page 1 sur 1

Éviter les If en cascade

Posté : lun. 22 août 2016 19:52
par Nicoloquinte
Bonjour :mrgreen:

Je me heurte a un gros problème ,
Je souhaite effectuer une action quand j'appuie sur une touche de mon contrôleur Xbox 360 , Lancer une action , cela fonctionne , maintenant , si j'ai 14 boutons sur mon contrôleur Xbox et 14 choix pour chaque bouton , ça se complique et ça fini en
IF .. Then
     IF .. Then ... EndIf
     IF .. Then ... EndIf
     IF .. Then ... EndIf
EndIf
:( :(

Cela 14 lignes 14 fois , ça fait beaucoup :shock: , alors je voulais vous demander si ce n'était pas possible de compacter cela :?:

Voici le code pour detecter une touche Préssée (plus Xinput.au3)
#include <_XInput.au3>
$inputhwnd = _XInputInit()
$message = 0
SplashTextOn("Xbox Controller Text", $message, -1, -1, -1, -1, 4, "")
While 1
$btext = ""
$timer = 50
$input = _XInputGetInput($inputhwnd)
$buttons = _XInputButtons($input[2])
_XInputVibrate($inputhwnd,round(($input[3] / 255) * 100),round(($input[4] / 255) * 100))
_XInputVibrate($inputhwnd,round(32768),round(32768))
If $buttons[1] Then $btext = $btext & "Up" & @CRLF
If $buttons[2] Then $btext = $btext & "Down" & @CRLF
If $buttons[3] Then $btext = $btext & "Left" & @CRLF
If $buttons[4] Then $btext = $btext & "Right" & @CRLF
If $buttons[5] Then $btext = $btext & "Start" & @CRLF
If $buttons[6] Then $btext = $btext & "Back" & @CRLF
If $buttons[7] Then $btext = $btext & "LJoy" & @CRLF
If $buttons[8] Then $btext = $btext & "RJoy" & @CRLF
If $buttons[9] Then $btext = $btext & "LButton" & @CRLF
If $buttons[10] Then $btext = $btext & "RButton" & @CRLF
If $buttons[11] Then $btext = $btext & "A" & @CRLF
If $buttons[12] Then $btext = $btext & "B" & @CRLF
If $buttons[13] Then $btext = $btext & "X" & @CRLF
If $buttons[14] Then $btext = $btext & "Y" & @CRLF

$message = "-=Input=-" & @CRLF & "Packet Number: " & $input[1] & @CRLF & _
   "Buttons: " & $input[2] & @CRLF & "Left Trigger: " & $input[3] & @CRLF & _
   "Right Trigger: " & $input[4] & @CRLF & "Left Joy X: " & $input[5] & @CRLF & _
   "Left Joy Y: " & $input[6] & @CRLF & "Right Joy X: " & $input[7] & @CRLF & _
   "Right Joy Y: " & $input[8] & @CRLF & @CRLF & "-=Buttons=-" & @CRLF & $btext
ControlSetText("Xbox Controller Text", "", "Static1", $message)
Sleep($timer)
WEnd
Merci d'avance :mrgreen:

Re: Éviter les If en cascade

Posté : lun. 22 août 2016 20:18
par Faco
Salut,

Tu peux faire un switch au lieu que tous tes if mais ça fera pas moins de lignes.

++

Re: Éviter les If en cascade  

Posté : lun. 22 août 2016 21:10
par orax
Local $btext = ""
Local $buttons[15]
$buttons[1]  = 1 ; Up
$buttons[8]  = 1 ; RJoy
$buttons[14] = 1 ; Y

Local $a = [0, "Up", "Down", "Left", "Right", "Start", "Back", "LJoy", "RJoy", "LButton", "RButton", "A", "B", "X", "Y"]
$a[0] = UBound($a) - 1

For $i = 1 To $a[0]
   If $buttons[$i] Then $btext &= $a[$i] & @CRLF
Next

ConsoleWrite($btext)

Re: Éviter les If en cascade

Posté : lun. 22 août 2016 22:52
par Nicoloquinte
Merci pour ce code , mais je n'arrive pas a l'appliquer au fait que ce soit un fichier texte qui définisse la touche "remappé"

Re: Éviter les If en cascade

Posté : mar. 23 août 2016 20:42
par Nicoloquinte
En fait , je comprend pas trop ce code , tu utilise des tableaux multi dimmentionels ? :shock:

Re: Éviter les If en cascade

Posté : mar. 23 août 2016 21:28
par mikell
Non, c'est 2 tableaux 1D (de taille identique)

Code : Tout sélectionner

If $buttons[$i] Then $btext &= $a[$i] & @CRLF
ça veut dire : "si l'array $buttons n'est pas vide à l'index $i , alors rajoute à $btext la valeur qui figure dans l'array $a à l'index $i "

Edit
et un @CRLF :mrgreen:

Re: Éviter les If en cascade

Posté : mer. 24 août 2016 09:53
par Nicoloquinte
Haaa ok , ça supprime 196 lignes a mon code , c'est pas mal :mrgreen:
merci beaucoup :wink: