Skip to content
Published on

Devlog #57 - Dynamic Windows

One of the biggest challenges in porting a game to mobile from desktop is dealing with varying screen sizes that are relatively small and can rotate to a new screen size at any time. Since we've already designed and implemented a GUI (Graphical User Interface) targeting a minimum size of 1024x768 we're now going to have to take a step back and redesign the whole GUI for the screens in our pockets.

Let's start with our base GUIWindow class and look at what it currently looks like on mobile.

Fantasy Online 2 image 1 from Devlog #57 - Dynamic Windows Oh boy, that doesn't fit at all.

Fantasy Online 2 image 2 from Devlog #57 - Dynamic WindowsPortrait cuts off the width and landscape cuts off the height.

The default width and height for a GUIWindow in FO2 is currently 544x520. Hard coding dimensions just doesn't work on mobile screens so we need to develop a new GUIWindow that can adapt to changing screen sizes gracefully. Let's call it GUIWindowDynamic.

So then what does GUIWindowDynamic need? It needs to take everything that works in GUIWindow and add in auto-scaling. It needs the properties minWidth, maxWidth, minHeight, maxHeight and it needs to use those to adapt.

Fantasy Online 2 image 3 from Devlog #57 - Dynamic Windows Ahh, that's so much better. The window now forces itself to stay within the bounds of the screen. Don't worry, I'll put the exit button back in.

Fantasy Online 2 image 4 from Devlog #57 - Dynamic WindowsAnd there we go, it works perfectly. We now have a GUIWindow that dynamically changes based on screen size and even updates if the screen size changes.

This means all the windows will just work on smart phones now right? No. We now have to go through all the windows in the game and convert the windows' contents to resize dynamically. Not all window contents are equal and some are much more difficult to port than others. I started with the mail window as it's contents are relatively simple and the layout was already designed to be dynamic. The only major change is that item names are no longer spelled out in text as it would overflow on small screens. As a bonus, the mail window now uses our newer GUIScrollList, which makes scrolling big mail boxes much faster.

Fantasy Online 2 image 5 from Devlog #57 - Dynamic Windows There we go. Welcome back window exit button!

Uh oh. I'm now realizing the BabylonJS onPointerEnterObservable and onPointerOutObservable are really buggy. They treat a mouse and a finger as the same thing, but fingers can't hover over a GUI control! So in order to not have the exit button highlight when we touch it we'll need to turn off all BabylonJS pointer enter and out events when we're on a touch only screen. How annoying. And with a few lines of code they are now turned off when then need to be turned off.

On to the next window! Let's see how the shop looks on an iPhone 12.

Fantasy Online 2 image 6 from Devlog #57 - Dynamic WindowsOh boy, looks like we're going to need to implement a grid that adapts. BabylonJS has a grid class but it's garbage and slower than a snail so we'll definitely need to implement our own.

Fantasy Online 2 image 7 from Devlog #57 - Dynamic WindowsSo close, yet so far. It looks like we'll need to also put the shop contents into a scroll view possibly. This window is going to be quite challenging but it will force us to create some classes that will be used by other windows in this conversion, so let's get to it.

First we comment out all the old shop window content code and change it to a GUIWindowDynamic.

Fantasy Online 2 image 8 from Devlog #57 - Dynamic WindowsAnd now we're off to the races. For the content we need to now layout the item for sale containers based on the size of the adjusted window.

Fantasy Online 2 image 9 from Devlog #57 - Dynamic WindowsFantasy Online 2 image 10 from Devlog #57 - Dynamic WindowsLook at that, our dynamic grid adjustment code is working great for the content. Now we just need to adjust the window width/height after the grid layout in order to better fit the content. We also need to figure out where the drag/drop to sell container is going to go.

Fantasy Online 2 image 11 from Devlog #57 - Dynamic WindowsFantasy Online 2 image 12 from Devlog #57 - Dynamic WindowsThere we go, everything is working nicely except for the window title overflowing. And also, where are we going to put the drag/drop to sell container? This adaptive GUI stuff is not easy.

Fantasy Online 2 image 13 from Devlog #57 - Dynamic WindowsAfter adding a measureText function to our GUIFont and a setTextWithMaxWidth to our TextGlyphGUIContainer we now have window titles that get truncated based on window width. As for the drag/drop to sell I think we're going to move selling somewhere else in the GUI for mobile. For now I will only add it if the screen height can handle it.

Fantasy Online 2 image 14 from Devlog #57 - Dynamic WindowsLooking great in mobile Safari on my iPhone.

Fantasy Online 2 image 15 from Devlog #57 - Dynamic WindowsOh my, portrait mode was long enough for the drag/drop. Too bad there's no room on screen for bags to actually drag and drop from. Figuring out how to have bags and windows open at the same time and actually being able to use them easily is going to be a tough nut to crack.

Fantasy Online 2 image 16 from Devlog #57 - Dynamic WindowsAnd look at that, back on the laptop/desktop front you would have never guessed the code underneath changed. That's the magic of GUIWindowDynamic!

Fantasy Online 2 image 17 from Devlog #57 - Dynamic WindowsThe mail window is insanely fast now and yet just like the shop you wouldn't know any code changed. Pure magic! Both the new shop window and mail window are now live. If you'd like to see the dynamic window in action be sure to login to your account on your phone, walk to a shop, and open it up. As always, please post any and all bugs you find.

FO2 has a lot of windows so this dynamic window conversion will happen over the rest of the year most likely. Now to figure out how do deal with bags and where to put the bag slots...

Until next time...

Have Fun & Keep Gaming!

See you next patch notes!