Recently I found this little jewel in some commercial UI plug-in for the Unity3D engine:
if (m_ctrlState == CONTROL_STATE.ACTIVE)
else if (ptr.evt == whenToInvoke)
Now don’t tell me you couldn’t do this in any other way, knowing that the Invoke jump label isn’t used anywhere else.
Fun fact: Visual Studio doesn’t even know how to deal with that – Find All References of Invoke returns a stumped “Search found no results.” 😉
Every programmer knows these moments. You come across those few lines of code that make you think: “Whoops…”. Or better: “Wat.” Most often this code is the result of crazy deadlines or massive refactoring of other parts of the application – or just simple stupidity.
Sometimes this code is your own.
In order to share these great moments with you, I decided to start a series I’d like to call Crappy Code. Should you ever run into any code that you think could fit this category, feel free to send me a mail to crappycode(at)npruehs(dot)de.
Let’s start right away with this little jewel: The first part of this series is about a piece of Python code that is ought to adapt the size of some buttons to the current display resolution.
if Globals.CurrentImageResolutionWidth == 1024:
g_game_button_frameScale = ( 0.352, 1, 0.429 * float(Globals.CurrentImageResolutionHeight) / float(Globals.CurrentImageResolutionWidth) )
g_game_button_scale = ( 0.1683, 1, 0.1705 * float(Globals.CurrentImageResolutionHeight) / float(Globals.CurrentImageResolutionWidth) )
g_game_button_frameScale = ( 0.352 * 0.95, 1, 0.429 * float(Globals.DefaultImageResolutionHeight) / float(Globals.DefaultImageResolutionWidth) )
g_game_button_scale = ( 0.1683, 1, 0.1705 * float(Globals.DefaultImageResolutionHeight) / float(Globals.DefaultImageResolutionWidth) )
This is the pure opposite of a resolution-independent UI. In fact this code has been taken from a game that was originally designed to be played with two resolutions, only: 1024×768 and 1280×800. Unfortunately, I got honored by the task to make the game look somehow good on laptops now, which – who’d have thought – includes other resolutions as well.
Second point: Where do all these strange numbers come from? What the frack is 0.352 * 0.95?
Ah. Yes. It’s 0.3344. I see 😉