Exemple #1
0
  /**
   * You can use this if you have a lot of text parameters to set instead of the individual
   * properties.
   *
   * @param Font The name of the font face for the text display.
   * @param Size The size of the font (in pixels essentially).
   * @param Color The color of the text in traditional flash 0xRRGGBB format.
   * @param Alignment A string representing the desired alignment ("left,"right" or "center").
   * @param ShadowColor An int representing the desired text shadow color in flash 0xAARRGGBB
   *     format.
   * @param ShadowX The x-position of the shadow, default is 1.
   * @param ShadowY The y-position of the shadow, default is 1.
   * @return This FlxText instance (nice for chaining stuff together, if you're into that).
   */
  public FlxText setFormat(
      String Font,
      float Size,
      int Color,
      String Alignment,
      int ShadowColor,
      float ShadowX,
      float ShadowY) {
    if (Font == null) Font = _font;

    if (!Font.equals(_font) || Size != _size) {
      try {
        _textField = new BitmapFontCache(FlxG.loadFont(Font, FlxU.round(Size)));
      } catch (Exception e) {
        FlxG.log(e.getMessage());
        _textField = new BitmapFontCache(FlxG.loadFont("org/flixel/data/font/nokiafc.fnt", 22));
      }

      _font = Font;
      _size = FlxU.round(Size);
    }

    setColor(Color);
    if (Alignment != null) // GWT doesn't support Locale. May cause problems?
    _alignment = HAlignment.valueOf(Alignment.toUpperCase()); // Locale.ENGLISH));
    _shadow = ShadowColor;
    _shadowX = ShadowX;
    _shadowY = ShadowY;

    calcFrame();

    return this;
  }
Exemple #2
0
 /** The alignment of the font ("left", "right", or "center"). */
 public void setAlignment(String Alignment) {
   if (Alignment == null) return;
   _alignment = HAlignment.valueOf(Alignment.toUpperCase()); // Locale.ENGLISH));
   calcFrame();
 }
Exemple #3
0
 /** The alignment of the font ("left", "right", or "center"). */
 public String getAlignment() {
   return _alignment.toString().toLowerCase(); // Locale.ENGLISH);
 }
Exemple #4
0
/**
 * @author Nathan Sweet
 * @author Matthias Mann
 */
class GdxFont implements Font {
  private static final HAlignment[] gdxAlignment = HAlignment.values();

  final GdxRenderer renderer;
  final BitmapFont bitmapFont;
  private final FontState[] fontStates;
  private final float yOffset;

  public GdxFont(
      GdxRenderer renderer,
      BitmapFont bitmapFont,
      Map<String, String> params,
      Collection<FontParameter> condParams) {
    this.bitmapFont = bitmapFont;
    this.renderer = renderer;
    yOffset = -bitmapFont.getAscent();

    ArrayList<FontState> states = new ArrayList<FontState>();
    for (FontParameter p : condParams) {
      HashMap<String, String> effective = new HashMap<String, String>(params);
      effective.putAll(p.getParams());
      states.add(new FontState(p.getCondition(), effective));
    }
    states.add(new FontState(null, params));
    this.fontStates = states.toArray(new FontState[states.size()]);
  }

  public int drawText(AnimationState as, int x, int y, CharSequence str) {
    return drawText(as, x, y, str, 0, str.length());
  }

  public int drawText(AnimationState as, int x, int y, CharSequence str, int start, int end) {
    FontState fontState = evalFontState(as);
    x += fontState.offsetX;
    y += fontState.offsetY + yOffset;
    bitmapFont.setColor(renderer.getColor(fontState.color));
    return bitmapFont.draw(renderer.batch, str, x, y, start, end).width;
  }

  public int drawMultiLineText(
      AnimationState as,
      int x,
      int y,
      CharSequence str,
      int width,
      de.matthiasmann.twl.HAlignment align) {
    FontState fontState = evalFontState(as);
    x += fontState.offsetX;
    y += fontState.offsetY + yOffset;
    bitmapFont.setColor(renderer.getColor(fontState.color));
    return bitmapFont.drawMultiLine(renderer.batch, str, x, y, width, gdxAlignment[align.ordinal()])
        .width;
  }

  public FontCache cacheText(FontCache cache, CharSequence str) {
    return cacheText(cache, str, 0, str.length());
  }

  public FontCache cacheText(FontCache cache, CharSequence str, int start, int end) {
    if (cache == null) cache = new GdxFontCache();
    GdxFontCache bitmapCache = (GdxFontCache) cache;
    bitmapFont.setColor(com.badlogic.gdx.graphics.Color.WHITE);
    bitmapCache.setText(str, 0, yOffset, start, end);
    return cache;
  }

  public FontCache cacheMultiLineText(
      FontCache cache, CharSequence str, int width, de.matthiasmann.twl.HAlignment align) {
    if (cache == null) cache = new GdxFontCache();
    GdxFontCache bitmapCache = (GdxFontCache) cache;
    bitmapFont.setColor(com.badlogic.gdx.graphics.Color.WHITE);
    bitmapCache.setMultiLineText(str, 0, yOffset, width, gdxAlignment[align.ordinal()]);
    return cache;
  }

  public void destroy() {
    bitmapFont.dispose();
  }

  public int getBaseLine() {
    return (int) bitmapFont.getCapHeight();
  }

  public int getLineHeight() {
    return (int) bitmapFont.getLineHeight();
  }

  public int getSpaceWidth() {
    return (int) bitmapFont.getSpaceWidth();
  }

  public int getEM() {
    return (int) bitmapFont.getLineHeight();
  }

  public int getEX() {
    return (int) bitmapFont.getXHeight();
  }

  public int computeMultiLineTextWidth(CharSequence str) {
    return bitmapFont.getMultiLineBounds(str).width;
  }

  public int computeTextWidth(CharSequence str) {
    return bitmapFont.getBounds(str).width;
  }

  public int computeTextWidth(CharSequence str, int start, int end) {
    return bitmapFont.getBounds(str, start, end).width;
  }

  public int computeVisibleGlpyhs(CharSequence str, int start, int end, int width) {
    return bitmapFont.computeVisibleGlyphs(str, start, end, width);
  }

  FontState evalFontState(AnimationState animationState) {
    int i = 0;
    for (int n = fontStates.length - 1; i < n; i++)
      if (fontStates[i].condition.evaluate(animationState)) break;
    return fontStates[i];
  }

  private static class FontState {
    final StateExpression condition;
    final Color color;
    final int offsetX;
    final int offsetY;

    public FontState(StateExpression condition, Map<String, String> params) {
      this.condition = condition;
      String colorStr = params.get("color");
      if (colorStr == null) throw new IllegalArgumentException("Color must be defined.");
      color = Color.parserColor(colorStr);
      if (color == null) throw new IllegalArgumentException("Unknown color name: " + colorStr);
      String value = params.get("offsetX");
      offsetX = value == null ? 0 : Integer.parseInt(value);
      value = params.get("offsetY");
      offsetY = value == null ? 0 : Integer.parseInt(value);
    }
  }

  private class GdxFontCache extends BitmapFontCache implements FontCache {
    public GdxFontCache() {
      super(bitmapFont);
    }

    public void draw(AnimationState as, int x, int y) {
      GdxFont.FontState fontState = evalFontState(as);
      setColor(renderer.getColor(fontState.color));
      setPosition(x + fontState.offsetX, y + fontState.offsetY);
      draw(renderer.batch);
    }

    public int getWidth() {
      return getBounds().width;
    }

    public int getHeight() {
      return getBounds().height;
    }

    public void destroy() {}
  }
}