Get device width & height as constants so that optimizer can constant-fold layout math

The API exposes System.getDeviceSettings(); to get the screen's width and height, which can be used to position graphical elements on-screen.

The resource compiler, however, does this statically at build time.  It converts XML into MonkeyC math expressions.  When compiling for a device that's 208 pixels wide, x="50%" compiles into the expression 208/2.  The optimizer's constant folding pre-computes it to 104.

When we write graphics code manually to avoid the overhead of xml layouts, is there a way to access device width and height as constants?  So the optimizer can do the same constant folding as it does for .mcgen from the resource compiler?

Hopefully something built-in. I know you can conditionally include per-device .mc source files, but I'd rather not generate a boatload of conditional <device_name>/constants.mc for each device.

  • I solved similar problem with creating DrawUtils class, which is initialized onStart(). then i pre precalculate all common variables for drawing and then access it directly from its instance like $.DrawUtils.height. Not built in solution, but does its job.

  • There are cases where you want to use the width and height of the dc, not the screen.

    for example, data fields and glance views.

    Also, on Edge touch devices, the on screen buttons take up part of the screen, reducing the size of the dc for full screen apps.

  • The fact is, the resource compiler is computing values statically, not at runtime.  The resource compiler emits code that doesn't refer to `dc` nor to `getDeviceSettings()`

    To achieve the same without the overhead of the resource compiler, I am using `const` today.  Not ideal and would get annoying if I had to build for other devices.  But since I'm creating something to be sideloaded for my own use, I won't have to create an `.mc` for each device.

  • On the devices that support Personality UI you can reference Rez.Styles.device_info.screenWidth and Rez.Styles.device_info.screenHeight to get the screen dimensions as constant values.