Package ru.vagrant_ai.questionmarkgame.util

Source Code of ru.vagrant_ai.questionmarkgame.util.Text

package ru.vagrant_ai.questionmarkgame.util;

import java.util.HashMap;

import org.newdawn.slick.Color;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.font.effects.OutlineEffect;
import org.newdawn.slick.font.effects.ShadowEffect;

import ru.vagrant_ai.questionmarkgame.main.Game;

public class Text {
 
  private static HashMap<String, UnicodeFont> font_index = new HashMap<String, UnicodeFont>();
 
  /**
   * Searches for specified font
   * @param size Specified size
   * @param shadow Includes or excludes shadow
   * @return null if font not found or reference to the font
   * @throws SlickException
   */
 
  static UnicodeFont extractFont(int size, int rule)
  {
    UnicodeFont f = searchFont(size, rule);
    if (f == null)
    {
      f = setFont(size, rule)
      return f;
    }
    else
    {
      return f;
    }
  }
   
  /**
   * Adds a new specified <thin_pixel-7.ttf> font to the collection
   * @param size Specified size
   * @param shadow Includes or excludes shadow
   * @return Font reference
   * @throws SlickException
   */
  @SuppressWarnings("unchecked") //due to the unfixable getEffects() warnings
    private static UnicodeFont setFont(int size, int rule)
    {
    UnicodeFont f = null;
    try
    {
      f = new UnicodeFont("res/thin_pixel-7.ttf", size, false, false);
      f.getEffects().add(new ColorEffect());
        if (rule == 2) f.getEffects().add(new OutlineEffect(size/50, java.awt.Color.black));
        else if (rule == 1) f.getEffects().add(new ShadowEffect(java.awt.Color.gray, size/90, size/90, 0.52f));
      f.addAsciiGlyphs();
      f.setPaddingTop(f.getAscent()/2);
      f.loadGlyphs();
    } catch (SlickException e) {}
    font_index.put(size+rule+"", f);
    return f;
  }

    /**
     * Searches for specified <thin_pixel-7> font
     * @param size Size of the font
     * @param shadow Includes or excludes shadow
     * @return
     */
   
  public static UnicodeFont searchFont(int size, int rule)
  {
      return font_index.get(size+rule+"");
    }
   
  /**
   * Draws string in the game with specified parameters
   * @param size Size of the drawn string
   * @param x X coordinate
   * @param y Y coordinate
   * @param str String, that will be drawn
   * @param col Color of the string
   * @param rule Include or exclude shadow
   * @throws SlickException
   */
 
    public static void drawString(int size, int x, int y, String str, Color col, int rule)
    {
      UnicodeFont f = extractFont(size, rule);
      f.drawString(x, y, str, col);   
    }
   
    public static void drawString(int size, int x, int y, String str, int rule)
    {
      drawString(size, x, y, str, Color.black, rule)
    }
   
    public static void drawString(int size, int x, int y, String str)
    {
      drawString(size, x, y, str, Color.black, 0)
    }
   
    public static void drawString(int size, int x, int y, String str, org.newdawn.slick.Color col)
    {
      drawString(size, x, y, str, col, 0)
    }
   
    public static void drawDebug(String str)
    {
      drawString(100, 100, 100, str, Color.red, 0);
    }
   
    /**
     * Extracts width of the specified string for centering needs
     * @param str Measured string
     * @param size Size of the font
     * @return Center coordinate of the string
     * @throws SlickException
     */
   
    public static int extractLength(String str, int size)
    {
      UnicodeFont f = Text.extractFont(size, 0);
      return (int) (Game.getAppX()/2-(f.getWidth(str)/2));
    }
   
    /**
     * Extracts width of the specified string
     * @param str Measured string
     * @param size Size of the font
     * @return Length of the string
     * @throws SlickException
     */
   
    public static int extractTrueLength(String str, int size)
    {
      UnicodeFont f = Text.extractFont(size, 0);
      return f.getWidth(str);
    }
}
TOP

Related Classes of ru.vagrant_ai.questionmarkgame.util.Text

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.