Android Open Source - DivisionByZero Label






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggstudios.divisionbyzero;
/*w  w w .ja va2 s .c  om*/
import com.ggstudios.utils.BitmapUtils;
import com.ggstudios.utils.DebugLog;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Label extends PictureBox {
  private static final String TAG = "Label";
  
  public static final int ALIGN_LEFT = 0, ALIGN_RIGHT = 1;
  
  private int alignment = ALIGN_LEFT;
  
  private Paint fontPaint;

  private String text;
  private String[] lines;

  private int maxWidth = -1;

  private Runnable rebuild = new Runnable() {

    @Override
    public void run() {
      generateTexture();
    }
    
  };
  
  protected Label() {
    super(0, 0);

    this.text = "";
    onTextChanged();
    refresh();
  }

  public Label(float x, float y, Paint painter, String text) {
    super(x, y);

    fontPaint = painter;

    this.text = text;
    onTextChanged();
    refresh();
  }
  
  public Label(float x, float y, Paint painter, String text, float maxWidth) {
    super(x, y);

    fontPaint = painter;

    this.text = text;
    setMaxWidth(maxWidth);
    onTextChanged();
    refresh();
  }

  public void setLocation(float x, float y){
    this.x = x;
    this.y = y;
  }

  public void setText(String text){
    if(this.text.equals(text)) return;

    this.text = text;
    onTextChanged();
    measure();
    Core.glView.queueEvent(rebuild);
  }
  
  private void onTextChanged() {
    if(text != null)
      lines = text.split("\n");
  }
  
  private void measure() {
    if(text == "" || text == null){
      w = 0;
      h = 0;
      return;
    }
    
    int numLines = lines.length;

    w = fontPaint.measureText(text);
    if(maxWidth > 0) { 
      for(String s : lines){
        int c = 0;
        char[] arr = s.toCharArray();
        while(c < arr.length) {
          int index = fontPaint.breakText(arr, c, arr.length - c, maxWidth, null);
          
          if(c != 0) {
            numLines++;
            w = maxWidth;
          }
          
          c += index;
        }
      }
    }
    
    Rect bounds = new Rect();
    float heightOfOneLine = fontPaint.descent() - fontPaint.ascent();
    fontPaint.getTextBounds(text, 0, text.length(), bounds);
    h = bounds.height() + (heightOfOneLine * (numLines - 1));
  }

  private void generateTexture(){
    if(text == "" || text == null){ 
      this.textureHandle = -1;
      return;
    }
    
    float heightOfOneLine = fontPaint.descent() - fontPaint.ascent();

    drawingW = Utils.findSmallestPot((int)w);
    drawingH = Utils.findSmallestPot((int)h);

    // now that we have our size information...
    generateBuffer();

    Bitmap bitmap = Bitmap.createBitmap((int)drawingW, (int)drawingH, Bitmap.Config.ARGB_4444);

    bitmap.eraseColor(Color.TRANSPARENT);
    Canvas canvas = new Canvas(bitmap);

    float hh = fontPaint.getTextSize() - fontPaint.descent();
    if(maxWidth > 0) { 
      for(String s : lines){
        int c = 0;
        char[] arr = s.toCharArray();
        while(c < arr.length) {
          int count = arr.length - c;
          int index = fontPaint.breakText(arr, c, count, maxWidth, null);
          int end = c + index;

          if(end < arr.length){
            while(arr[end] != ' ') {
              end--;
              index--;
            }
            end++;
            index++;
          }
          
          if(c == end) {
            // something is wrong... we are going into an infinite loop
            // to break out, let's just draw the rest of the text...
            DebugLog.e(TAG, "infinite loop with string: " + s);
            index = count;
            end = arr.length;
          }
          
          canvas.drawText(arr, c, index, 0, hh, fontPaint);
          c = end;
          hh += heightOfOneLine;
        }
      }
      
      //h = hh - heightOfOneLine;
    } else {
      for(String s : lines){
        canvas.drawText(s, 0, hh, fontPaint);
        hh += heightOfOneLine;
      }  
    }
    setTextureHandle(BitmapUtils.loadBitmap(bitmap, textureHandle));
  }
  
  public void setAlignment(int alignment){
    this.alignment = alignment;
  }
 
  @Override 
  public void draw(float offX, float offY) {
    switch(alignment) {
    case ALIGN_LEFT:
      super.draw(offX, offY);
      break;
    case ALIGN_RIGHT:
      super.draw(offX - w, offY);
      break;
    }
  }
  
  @Override
  public void refresh() {
    measure();
    generateTexture();
  }

  public String getText() {
    return text;
  }
  
  public void setMaxWidth(float maxWidth) {
    if(maxWidth <= 0) {
      DebugLog.e(TAG, "Max width must be > 0.", new Exception());
      return;
    }
    
    this.maxWidth = (int) maxWidth;
  }
}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java