[AndEngineLimbo] Positioner

  ... the case you feel the need for a new feature or want to submit one.

[AndEngineLimbo] Positioner

Postby nazgee » Thu May 09, 2013 1:52 am

Hi all,

This post is about two things:
1. new AE extension
2. Positioner feature
Since number 2) is more spectacular at first glance, if you hate reading, I suggest scrolling down and see how Positioner can help you in your scene layouting. If you're interested in contributing to new AE extension, read 1)

1) AndEngineLimbo
AndEntineLimbo is an ongoing attempt to extend AE with GOOD features that some of us have implemented. Since Nicolas is *extremely* cautious about the code he pulls into AE core it is quite hard to share something with a broader audience in way different than posing on this forum. This is why I think we need limbo- single place where we can gather our features.

Here is the limbo:

Since I already maintain DebugDrawExtension and RubeLoaderExtension, I know that there is NO chance to maintain Limbo for ALL flavours of AE out there. I decided to use AC branch and I'd like to stick to it. There is nothing that stops you from converting it (if feasible) to other branches, but let's not fuel GLES1 vs GLES2 vs GLES2-AC flame once again here :)

What I would like Limbo to be:
- container for GENERIC, complete features
- place to hold code not perfect enough for AE core, bot good enough for limbo
- something that is aimed for intermediate users of AE - it does not have to be understandable for everyone
- something that will be fluid - I expect some features come and go as better alternatives apper. I don't expect it to have rock solid API
- something that will NOT becom a junkyard - let's at least try to deliver quality comparable to Nicolas's code
- something that you will help to create :)

I already took the liberty to include in limbo:
- few of my more-polished classes
- fatal's gradient font implementation (kudos!), since I find it pretty much closed and mature - if you would like me to put your full name in source files, or remove it from Limbo, let me know - I understand that you might not like this idea.

2) Positioner

This is first AndEngineLimbo feature I'd like to present. I challange you to code the following scene:
pos.png (5.37 KiB) Viewed 1817 times

This will be a bit of ...W-w+h/2... isn't it? Well... This is how positioner does it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. e.setScene(new Scene() {
  2. {
  3.         setSize(GameConstants.CAMERA_WIDTH, GameConstants.CAMERA_HEIGHT);
  4.         Rectangle rectWhite = new Rectangle(0, 0, 100, 200, vbom);
  5.         rectWhite.setColor(Color.WHITE);
  6.         attachChild(rectWhite);
  8.         Rectangle rectRed = new Rectangle(0, 0, 30, 60, vbom);
  9.         rectRed.setColor(Color.RED);
  10.         attachChild(rectRed);
  12.         Rectangle rectGreen = new Rectangle(0, 0, 60, 120, vbom);
  13.         rectGreen.setColor(Color.GREEN);
  14.         attachChild(rectGreen);
  16.         Rectangle rectBlue = new Rectangle(0, 0, 30, 60, vbom);
  17.         rectBlue.setColor(Color.BLUE);
  18.         attachChild(rectBlue);
  20.         Rectangle rectYellow = new Rectangle(0, 0, 30, 60, vbom);
  21.         rectYellow.setColor(Color.YELLOW);
  22.         attachChild(rectYellow);
  24.         /*
  25.          * This one is tricky!
  26.          * It is not only attached to another entity, but it is
  27.          * also rotated. As you can see- it is still handled correctly
  28.          * because positioner does all required local-to-scene translations.
  29.          */
  30.         Rectangle rectCyan = new Rectangle(0, 0, 30, 60, vbom);
  31.         rectCyan.setColor(Color.CYAN);
  32.         rectYellow.attachChild(rectCyan);
  33.         rectCyan.setRotation(90);
  35.         Rectangle rectPink = new Rectangle(0, 0, 30, 60, vbom);
  36.         rectPink.setColor(Color.PINK);
  37.         attachChild(rectPink);
  39.         Rectangle rectBlack = new Rectangle(0, 0, 30, 60, vbom);
  40.         rectBlack.setColor(Color.BLACK);
  41.         attachChild(rectBlack);
  43.         PositionerSceneRelative.getInstance().center(this, rectWhite);
  44.         PositionerSceneRelative.getInstance().center(rectWhite, rectRed);
  45.         PositionerSceneRelative.getInstance().placeLeftOfAndCenter(rectWhite, rectGreen);
  46.         PositionerSceneRelative.getInstance().alignTopEdgesAndCenter(rectGreen, rectYellow);
  47.         PositionerSceneRelative.getInstance().placeBelowOfRightsAligned(rectWhite, rectBlue);
  48.         PositionerSceneRelative.getInstance().placeRightOfAndCenter(rectWhite, rectCyan);
  49.         PositionerSceneRelative.getInstance().placeBelowOfRightsAligned(rectCyan, rectPink);
  50.         PositionerSceneRelative.getInstance().alignBottomLeftEdges(rectWhite, rectBlack, 5, 5);
  51. }
  52. });
Parsed in 0.014 seconds, using GeSHi

What it supports (or at least should, if there are no bugs...)
- positioning one Entity inside of another (most of common cases)
- positioning one Entity outside of another (most of common cases)
- positioning in one of common cases, and then applying a translation
- Entities can be rotated (movable, and immovable) but it works best for right angles (0,90,180,270)
- Entites do NOT need to have the same parent (can be attached to other Entites), but they NEED to share the same Scene

The syntax is simple. You need to provide two entities. First is used as a reference (it is not moved) and the other one is the one that gets translated (accordingly to the method you chose). Also, as you can se with black rectangle:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. PositionerSceneRelative.getInstance().alignBottomLeftEdges(rectWhite, rectBlack, 5, 5);
Parsed in 0.011 seconds, using GeSHi

you can first apply the alignment, and then translate the entity (in this case 5 pixels along X and Y).

This is all you need to used it, but I suggest cloning the whole repo:
https://github.com/nazgee/AndEngineLimb ... positioner
Last edited by nazgee on Mon May 13, 2013 8:39 pm, edited 4 times in total.
Dirt Rider Mayhem is PUBLISHED now!
User avatar
Posts: 527
Joined: Fri Oct 21, 2011 10:31 pm
Location: Poland, Wrocław

Re: [AndEngineLimbo] Positioner

Postby fatal » Thu May 09, 2013 2:10 am

Great idea. :) I have no problem with using my gradient font class in this lib. That's why I shared it on the forums, I'm happy that is useful to other people. :)
User avatar
Posts: 488
Joined: Sun Jan 08, 2012 2:20 am
Location: Budapest, Hungary

Re: [AndEngineLimbo] Positioner

Postby fechy » Thu May 09, 2013 5:00 pm

I know you say not to fuel the GLES2 vs GLES2-AC flame, but I have a question (hopefully it will not fuel anything but to add water :)). Would that smart positioner help on the migration from GLES2 to AC?

I see everywhere people (including me) asking the same question about what takes to do the migration. The answer is rather simple to say but not really to apply: anchor points.

However, in my personal experience, having a game almost finished, port it to AC is a completely mess. Positioning everything will take do the game all over again. Maybe I wasn't doing my job right or whatever, since the positions sometimes are related either to center of screen or cameraHeight - EntityHeight, and so forth.

I found limbo a great idea, and probably I'll be adding some stuff I found while developing my game, and I think is great if all of us do the same to gather all features and fixes there are around this forum.

I will no answer again why I want to do the migration, but it will be good if we can do something with limbo to support both versions with little or no change to the positioning.

Posts: 47
Joined: Fri Sep 14, 2012 3:38 pm

Re: [AndEngineLimbo] Positioner

Postby nazgee » Thu May 09, 2013 10:12 pm

nobody will read this post... I should probably get a life...

fechy wrote:Positioning everything will take do the game all over again

No, it's not. It just looks to be that scary :)

Would that smart positioner help on the migration from GLES2 to AC?

It was not intended to help with it but... maybe indeed it could be used for this purpose?

GLES2 => GLES2-AC switch with help of Positioner
DISCLAIMER: I never tested approach described below. It is just a mental experiment I made

Given that you can make Positioner* to work fine for GLES2 branch (I'll discuss it later in this post), here is what you can do, to switch from GLES2 to GLES2-AnchorCenter

- copy Postioner* from Limbo to your code
- do the magic to make it work for you on GLES2 branch
- try to refactor your GLES2 code to use Positioner* instead of getWidth/getHeight/setX/setY/setPosition/getX/getY/getPosition. So instead of:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. button.setPosition(hud.getWidth()/2 - button.getWidth()/2, button.getHeight()/2 + 10);
Parsed in 0.011 seconds, using GeSHi

you should have
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. PositionerSceneRelative.getInstance().alignTopEdgeAndCenter(hud, button, 0, 10);
Parsed in 0.011 seconds, using GeSHi

- make sure above is done for most of your code
- switch AE code from GLES2 to GLES2-AC branch
- fix all broken imports, start using IEntity instead of IAreaShape, etc.
- switch to using Positioner implementation from Limbo (which is compatible with your GLES2-AC branch of AE core)
- launch your game, and fine tune all the stuff that is still wrong (e.g. you are calling setScaleCenterX(getWidth()), because GLES2 defines scale/rotation centers in 0-width range, while GLES2-AC uses 0-1 range)

I might be missing something from the big picture, but you can't say I have not tried. As you can see- it will not happen for free- there is still a lot of refactoring involved, but it MIGHT be a LITTLE bit easier.

Making Positioner from Limbo GLES2 compatible

First thing you have to do is changing these enums:
https://github.com/nazgee/AndEngineLimb ... .java#L264

Since GLES2 has Y axis facing down, and GLES2-AC has it facing up, it'll have to look sth like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static enum eAnchorPoint {
  2.         L_TOP(0.0f,0.0f),C_TOP(0.5f,0.0f),R_TOP(1.0f,0.0f),
  3.         L_CNT(0.0f,0.5f),C_CNT(0.5f,0.5f),R_CNT(1.0f,0.5f),
  4.         L_BTM(0.0f,1.0f),C_BTM(0.5f,1.0f),R_BTM(1.0f,1.0f);
  6.         public final AnchorPoint anchor;
  8.         private eAnchorPoint(final float pX, final float pY) {
  9.                 anchor = new AnchorPoint(pX, pY);
  10.         }
  11. }
Parsed in 0.011 seconds, using GeSHi

See what's changed? E.g.:
- on GLES2 right-bottom is (1,1), and on GLES2-AC it is in (1,0)
- on GLES2 left-bottom is (0,1), and on GLES2-AC it is in (0,0)
- on GLES2 left-top is (0,0), and on GLES2-AC it is in (0,1)
- ...

I think, that after fixing the enums, Positioner* should be successfully used on GLES2, as long as you'll change IEntity to IAreaShape. If not... well. Make it work, please :) Yeah... I know this might be tricky. At the beginning, try playing around with https://github.com/nazgee/AndEngineLimb ... e.java#L59

When it comes to maintain BOTH versions of Limbo- I'm waiting for contributions :) I will not do it myself, but I can help other members to make it happen. I just think it won't work out. Let's first see if the idea of limbo will catch on- maybe AE community is counting more on Nicolas, than on collaboration, and this project will just vanish.
Dirt Rider Mayhem is PUBLISHED now!
User avatar
Posts: 527
Joined: Fri Oct 21, 2011 10:31 pm
Location: Poland, Wrocław

Return to Features

Who is online

Users browsing this forum: No registered users and 6 guests