package org.twodot.cognet; import danger.ui.Pen; import danger.ui.Font; import danger.ui.Bitmap; import danger.app.Application; import danger.app.ResourceDatabase; import danger.util.StringUtils; import org.twodot.cognet.Resources; public class SmileyEngine implements Resources { private String text; private static String emoticonsText[] = {"O:-)", ":-)", ":-(", ";-)", ":-P", "=-o", ":-*", ">:o", "8-)", ":-$", ":-!", ":-[", ":-\\", ":'(", ":-X", ":-D"}; private static int FIRST_ICON = 0xE000; private int iconWidth; private int iconCount; private Bitmap[] iconSet; Font font; public static final char ICON_INNOCENT = 0xE000; public static final char ICON_SMILING = 0xE001; public static final char ICON_FROWNING = 0xE002; public static final char ICON_WINKING = 0xE003; public static final char ICON_STICKING_OUT_TONGUE = 0xE004; public static final char ICON_SURPRISED = 0xE005; public static final char ICON_KISSING = 0xE006; public static final char ICON_YELLING = 0xE007; public static final char ICON_COOL = 0xE008; public static final char ICON_MONEY_MOUTH = 0xE009; public static final char ICON_FOOT_IN_MOUTH = 0xE00A; public static final char ICON_EMBARASSED = 0xE00B; public static final char ICON_INDIFFERENT = 0xE00C; public static final char ICON_CRYING = 0xE00D; public static final char ICON_LIPS_SEALED = 0xE00E; public static final char ICON_LAUGHING = 0xE00F; public static final char ICON_COG = 0xE010; public static final char ICON_IRC = 0xE011; public static final char ICON_FLAG = 0xE012; public static final char ICON_INFO = 0xE013; public static final char ICON_BULLET = 0xE014; public static final int LARGE_ICON_LIMIT = 11; public SmileyEngine(Font f, String _text) { this(f); this.text = replaceWithUnicode(_text); } public SmileyEngine(Font f) { font = f; ResourceDatabase rdb = Application.getCurrentApp().getResources(); iconSet=null; if (f.getAscent() + f.getDescent() >= LARGE_ICON_LIMIT) iconSet = rdb.getBitmapArray(k11x11Icons); else iconSet = rdb.getBitmapArray(k5x6Icons); iconWidth = iconSet.length; } public String getNext(int pixels) { if (text.length() <= 0) return null; int indexOfLastSpace = 0; int totalWidth = 0; String returnVal; int x=0; for (x=0; x= pixels) break; } // if indexOfLastSpace == 0, then we've got // one big 'ol word that needs to be broken up if (totalWidth < pixels && x == text.length()) { returnVal = text; text = ""; } else if (indexOfLastSpace == 0) { returnVal = text.substring(0,x); //if (x+1 < text.length()) text = text.substring(x); } else { // otherwise, break the line at indexOfLastSpace returnVal = text.substring(0,indexOfLastSpace); //if (indexOfLastSpace+1 < text.length()) text = text.substring(indexOfLastSpace+1); } return returnVal; } public static String replaceWithUnicode(String inText) { String output = inText; //replace all emoticons with corresponding character for (int x=0; x < emoticonsText.length; x++) { String thisUnicodeChar = "" + (char)(FIRST_ICON + x); // replate both 'nosed' and 'noseless' versions. output = replaceAll(output, emoticonsText[x], thisUnicodeChar); output = replaceAll(output, StringUtils.stripChar(emoticonsText[x], '-'), thisUnicodeChar); } return output; } public static String replaceAll(String in, String what, String with) { int pos = 0; int whatLen = what.length(); int withLen = with.length(); while ((pos = in.indexOf(what, pos)) >= 0) { in = in.substring(0,pos) + with + in.substring(pos+whatLen); pos = pos + withLen; } return in; } public void drawString(int x, int y, String s, Pen p) { int xpos = x; p.setFont(font); for (int c=0; c= FIRST_ICON && chr < FIRST_ICON + iconWidth); } public Bitmap getIconBitmap(char c) { int ndx = c - 0xE000; return iconSet[ndx]; } }