Page 1 of 1

portland, an orientation handler for Android

Posted: Thu Jan 25, 2018 5:54 pm
by pgimeno
This is a toy library I created just to see if I could work around the lack of built-in orientation in the Android port of Löve. Turns out it was easy, so I'm offering it here in case someone else finds it useful. The license is my own creation for brevity, but basically a very liberal license similar to that of Löve: just keep the copyright notice and the license text itself and you're fine to do as you please.

Usage:
  • In conf.lua, add c.accelerometerjoystick = true (or t. if you use t instead of c).
  • Require the library with: local portland = require 'portland'
  • At initialization, define the tweening function to use for the rotation and the duration of the rotation, using portland.setTween(easing_function, duration_in_seconds). If easing_function is false, the change is immediate (default). portland includes a few built-in easing functions, I recommend sigmoid5 or brake:
    • portland.ease.linear
    • portland.ease.accel
    • portland.ease.brake
    • portland.ease.sigmoid3
    • portland.ease.sigmoid5
  • On love.update, call portland.updateOrientation(dt).
  • At the top of love.draw, call portland.orient(). Note it fiddles with the Löve transform, so if you call love.graphics.origin(), call portland.orient() again afterwards before drawing to the screen.
  • In your program, use portland.width and portland.height to read the current width and height in real time, and use them to adjust the visualization as required. The included example program uses them to scale an image so that the aspect ratio is preserved. If you need the current orientation, the property portland.orientation tells you, but note that it's the target value of the tweening and is not updated during the tweening. If you want one that is updated during tweening, use portland.angle instead.
Other functions and properties are:
  • portland.physWidth, portland.physHeight: The physical width and height of the screen as returned by love.graphics.getWidth() and .getHeight() respectively. Useful to not be calling these functions repeatedly.
  • portland.stableTime: The time in seconds for the current orientation to be considered stable and change to it. Defaults to 0.4.
  • portland.forceOrient(n): Forces a specific orientation regardless of the accelerometer values. 0 is landscape, 90 is portrait, 180 is landscape inverted, 270 is portrait inverted, false means stop forcing an orientation.
  • portland.onOrientationChange: It's a callback you may define when you want to be notified about a change in orientation. The parameters are the target orientation, the target width and the target height.
I guess it shouldn't be difficult to adapt it to iOS, if that's necessary, but I don't have access to one.

Sorry, no github. This was a pet project and I didn't use source control.

Here it is. Enjoy!

Re: portland, an orientation handler for Android

Posted: Fri Jan 26, 2018 12:29 am
by yetneverdone
This is what ive been looking for! thanks

Re: portland, an orientation handler for Android

Posted: Sat Jan 27, 2018 3:36 am
by jojomickymack
This is amazing and I can see this solving a ton of problems for everyone with quirky mobile screenwidths. I löve portland, thank you so much!