Created
May 27, 2016 16:05
-
-
Save HorlogeSkynet/91d323ff56c607a69d8f4e70e5032df9 to your computer and use it in GitHub Desktop.
How to declare and use an enumeration-like type on Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Notre dictionnaire qui jouera le rôle d'énumération... | |
// Défini ici en tant que variable globale pour avoir la même "portée" qu'une énumération "classique" | |
IntDict monEnumeration; | |
void setup() | |
{ | |
// On crée une instance du dictionnaire ... | |
monEnumeration = new IntDict(); | |
//On ajoute nos entrées une par une (comme lors de la définition d'une énumération "classique") | |
monEnumeration.set("ROUGE", 0); | |
monEnumeration.set("BLEUE", 1); | |
monEnumeration.set("VERTE", 2); | |
} | |
void draw() | |
{ | |
/* ... */ | |
// * Traiter une variable à l'aide de conditions (SWITCH impossible car expression considérée non-constante) | |
// * Accéder une valeur grâce au nom de son entrée | |
if(maValeurATester == monEnumeration.get("ROUGE")) | |
{ | |
/* ... */ | |
} | |
else if(maValeurATester == monEnumeration.get("BLEUE")) | |
{ | |
/* ... */ | |
} | |
else if(maValeurATester == monEnumeration.get("VERTE")) | |
{ | |
/* ... */ | |
} | |
else | |
{ | |
/* ... */ | |
} | |
/* ... */ | |
// * Retrouver le nom de l'entrée qui contient une valeur | |
println(monEnumeration.keyArray()[0]); // --> Affiche "ROUGE" | |
println(monEnumeration.keyArray()[1]); // --> Affiche "BLEUE" | |
println(monEnumeration.keyArray()[2]); // --> Affiche "VERTE" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment