#TheGate editor coded with #HollywoodMAL
---
Today I've changed the structure of the event handler to avoid SWITCH/CASE statements.

A GUI event creates a message with several fields but what I need to handle them are:
.id (the gadget id)
.attribute (the type of event like 'pressed', 'selected', etc...)

Previously I handled them with something like this:
Switch msg.attribute
Case "Pressed"
Switch msg.id
Case "Load_Button"
handler_func()

๐Ÿงต โžก๏ธ

this lead to very long Switch/Case caused by the very complex GUI.

Today I've used the ability of Hoollywood (like in #Lua ) to assign functions to tables and using a 2 dimensional table I was able to reduce the infinite Switch/Case to a single line, even if I had to define this table with all the handler functions.

So I've defined a table with this structure:
app.callback[id][attribute]

And now I'm able to hadle events with:
app.callback[msg.id][msg.attribute](msg)

๐Ÿงต โžก๏ธ

The boring part is that I've to setup the table with all the handler functions, but this removes the Switch/Case completely and make the code faster avoiding all the comparisons.

Picture 1: The table loaded with all the handler functions

Picture 2: The code used to replace the Switch/Case. I only have to check that the table fields exist before trying to access it.

This is common in #Lua and maybe someone will find useful to know that it can be done in #HollywoodMAL too ๐Ÿ˜€