Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Created May 27, 2016 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HorlogeSkynet/91d323ff56c607a69d8f4e70e5032df9 to your computer and use it in GitHub Desktop.
Save HorlogeSkynet/91d323ff56c607a69d8f4e70e5032df9 to your computer and use it in GitHub Desktop.
How to declare and use an enumeration-like type on Processing
// 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