portland, an orientation handler for Android

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
pgimeno
Party member
Posts: 1366
Joined: Sun Oct 18, 2015 2:58 pm

portland, an orientation handler for Android

Post by pgimeno » Thu Jan 25, 2018 5:54 pm

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!
Attachments
portland-with-demo.love
(120.26 KiB) Downloaded 66 times
Last edited by pgimeno on Sat Jan 27, 2018 1:08 am, edited 1 time in total.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

User avatar
yetneverdone
Party member
Posts: 302
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: portland, an orientation handler for Android

Post by yetneverdone » Fri Jan 26, 2018 12:29 am

This is what ive been looking for! thanks

User avatar
jojomickymack
Prole
Posts: 45
Joined: Tue Dec 26, 2017 4:52 pm

Re: portland, an orientation handler for Android

Post by jojomickymack » Sat Jan 27, 2018 3:36 am

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!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest