n-Patch Texture

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

n-Patch Texture

Postby Kernle 32DLL » Mon May 09, 2011 3:14 pm

Current Version: 1.41 - 23.12.2011
The class is licensed under Apache 2

Edit as of 25.12.11:
As anyone of you should know by now, Mario has build his own version of Android's 9-Patch System. Altough mine is superior in terms of technical possebilitys, I understand that he has no time incoperating my "monster" into LibGDX. Nevertheless, I will still work on the project whenever I have the time, or problems arise. Thanks you!


Hi there,

I started to work on this some days ago. To clarify one thing at the start: The term "n-patch" is made up by me, so don't hang on to that.

My n-Patch Texture is basically a system similar to the nine-patch system android uses for its View/Layout System. The goal is to be able to draw Androids Nine-Patch graphics, without relying on android's view system, which is not available on libgdx desktop. Also, drawing UI elements on top of a libgdx layout view in Android is pretty performance wasteful.

So, what is a n-patch? Hope you are all familiar with the term nine-patch. Thats when you devide your image into 9 sections (or patches), of which every section is scaled a different way, e.g. the corners are not scaled at all, while the center gets scaled into all directions. So what is a n-patch now? Basically the same, but taking the idea of a nine-patch to the next level (hence the n). The image can be devided into an arbitary amount of sections.

Additional resource to look up concerning Androids nine-patch system:
http://developer.android.com/guide/deve ... patch.html
http://developer.android.com/guide/topi ... nine-patch

Image
Image
Bonus image: My scribblings about n-patches

You can download most up-to-date version of the class from my dropbox: DOWNLOAD

The zip contains a Eclipse project with the actual class and a basic Main - ApplicationListener frame for some testing (screen 1). Also, I've included a small demo project to showcase a bit what can be done with n-patches (screen 2). The JavaDoc may contain serious spelling errors, as my English is not that good. Suit yourself to correct them, as well as any other coding error you may find. Also, I would be glad if this class could be integrated into libgdx upon final release of the class. In any news, feedback is appreciated.

So long,
Kernle

Version history:
Code: Select all
v.1.0 - 09.05.2011
 - first release

v.1.1 - 09.05.2011
 - Working internally with TextureRegions now
 - Fixed two bugs in calcing the final scaling factor for downscaled patches
 - Removed useles variables
 - Added support for non-pot (power-of-two) textures
 - Dropped contructor for texturedata

v.1.2 - 10.05.2011
 - New functions: refreshRangeArrays() and refreshPatchTextures()
 - New functions: getTextureRegion() and swapTexture();
 - New function: setFilter() (wrapper to apply filter directly to texture)
 - New constructor: NPatchTexture(TextureRegion textureregion)
 - Dropped contructors wich took a mimap boolean parameter
 - Added support for loading Android 9-Patch files (see NPatchTexture.loadAndroid9Patch())
 - Added support for displaying n-patches via negative width and/or height.
 - Renamed rebuildAreas() to rebuildPatches()
 - Improved loading of non-pot files
 - Minor javadoc adjustments
 - Added licence info (see top of file)

v.1.3 - 11.05.2011
 - Merged Android 9-Patch file loader into constructors
 - New construtor: NPatchTexture(Texture) (pretty much useless tough)
 - n-patches loaded via FileHandle are now managed (thanks a lot badlogic!)
 - Minor code clean up
 - Removed function setFilter()

v.1.4 - 14.05.2011
 - Fixed serious problem with TextureRegions (thanks andrew!)
 - Fixed some calculation bugs while drawing with width and height
 - Fixed some calculation bugs during patches rebuilding

v 1.41 - 23.12.2011
 - Updated to work with Libgdx 0.9.2 (broken TextureData)

v.1.5 - TO BE RELEASED
 - Custom XML format for scaling info, to compensate for missing possibility
   to load Android 9-Patches from a TextureAtlas
Last edited by Kernle 32DLL on Sun Dec 25, 2011 1:15 pm, edited 18 times in total.
Kernle 32DLL
 
Posts: 30
Joined: Sat May 07, 2011 12:10 am
Location: Germany

Re: n-Patch Texture

Postby andrew » Mon May 09, 2011 3:43 pm

Awesome! I'll definitely download it and try it out. Now we can use the android tool to build and use a libgdx implementation.

MORE! WE NEED MORE STUFF LIKE THIS!
andrew
 
Posts: 2
Joined: Mon May 09, 2011 3:41 pm

Re: n-Patch Texture

Postby mzechner » Mon May 09, 2011 9:23 pm

Good job :)
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: n-Patch Texture

Postby bach » Mon May 09, 2011 9:57 pm

This is neat! :)

I'll definitely check this out.

Thanks Kernle,
Bach
bach
 
Posts: 713
Joined: Mon Mar 07, 2011 1:50 am

Re: n-Patch Texture

Postby Kernle 32DLL » Mon May 09, 2011 11:55 pm

File updated, download stayed the same, version history in start post. Will upload something neat to showcase n-patch application tomorrow :D

So long,
Kernle
Kernle 32DLL
 
Posts: 30
Joined: Sat May 07, 2011 12:10 am
Location: Germany

Re: n-Patch Texture

Postby Kernle 32DLL » Tue May 10, 2011 9:36 pm

File updated, download stayed the same, version history in start post. Key highlights: Android Nine-Patch loader and smaller adjustments.
Kernle 32DLL
 
Posts: 30
Joined: Sat May 07, 2011 12:10 am
Location: Germany

Re: n-Patch Texture

Postby mzechner » Wed May 11, 2011 12:29 am

Sweet! Keep up the good work. I'll try to look into it asap, no promises :D
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: n-Patch Texture

Postby Kernle 32DLL » Wed May 11, 2011 11:23 pm

File updated, download stayed the same, version history in start post. Key highlights: Merged Nine-Patch loader intro constructors, and managed n-patches (Thanks a lot Mario!)
Kernle 32DLL
 
Posts: 30
Joined: Sat May 07, 2011 12:10 am
Location: Germany

Re: n-Patch Texture

Postby andrew » Fri May 13, 2011 5:19 am

Trying to create an n patch from a texture region so that I can store my n patch image on a sprite sheet.

if I draw the npatch without scaling, it works as expected.

if I try to scale, nothing renders at all, with no error.

Here is what I've done

Code: Select all
border = new NPatchTexture(atlas.findRegion("border"));
      
border.setHorizontalScalingRange(0, 67, false);
border.setHorizontalScalingRange(68, 92, true);
border.setHorizontalScalingRange(93, 159, false);
   
border.setVerticalScalingRange(0, 73, false);
border.setVerticalScalingRange(74, 79, true);
      
border.rebuildPatches();
border.refreshPatchTextures();
andrew
 
Posts: 2
Joined: Mon May 09, 2011 3:41 pm

Re: n-Patch Texture

Postby Kernle 32DLL » Fri May 13, 2011 6:28 am

Thats strange, are you using the most recent version (1.3)? Could you please provide the size of the Texture Region, as well your call to actually draw the patch? If possible, could you also provide the actual image per PM or something like that?

So long,
Kernle
Kernle 32DLL
 
Posts: 30
Joined: Sat May 07, 2011 12:10 am
Location: Germany

Next

Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest