Thursday, November 24, 2011

An Image based Text Widget for TriplePlay

Starting to look into PlayN for a bit and whole bunch of utilities build for it like TriplePlay and React. I like the way TriplePlay struture it's widgets. You developed using TriplePlay widgets and it works across platforms without being dependent on the platform's native machinery. Following is a crude way to implement an Text display widget that source it's text from image. Hopefully, TriplePlay will incorporate more of such components into it's distribution. TriplePlay is still young so there're definitely plenty of room for enhancements.

A bit crude but it works.

public abstract class AbstractImageTextWidget<T extends AbstractImageTextWidget<T>> extends TextWidget<T> {
  public AbstractImageTextWidget(String text) {
    this(text, null);
  public AbstractImageTextWidget(String text, Image icon) {
    if (icon != null)
  protected void createTextLayer(LayoutData ldata, float tx, float ty, float twidth, float theight, float availWidth, float availHeight) {
    if (twidth > 0 && theight > 0) {
      tlayer = prepareCanvas(_tlayer, twidth, theight);
      char[] textArray = text.get().toCharArray();
      float eachTextWidth = twidth / textArray.length; 
      if (textArray.length > 0) {
        for (int a=0 ; a<textArray.length; a++) {
          Image i = getCharMap().get(Character.toLowerCase(textArray[a]));
          if (i != null) 
            _tlayer.canvas().drawImage(i, a*eachTextWidth, 0f, eachTextWidth, theight);
      tlayer.setTranslation(tx + ldata.halign.offset(twidth, availWidth), ty + ldata.valign.offset(theight, availHeight));
  protected abstract Map<Character, Image> getCharMap();

No comments:

Post a Comment