source: trunk/hiptop/cognet++/org/twodot/cognet/SmileyEngine.java@ 408

Last change on this file since 408 was 210, checked in by Nicholas Riley, 19 years ago

cognet++: Brian Swetland et al.'s generic proxied hiptop chat client.
Requires cognetd to function.

File size: 5.3 KB
Line 
1package org.twodot.cognet;
2
3import danger.ui.Pen;
4import danger.ui.Font;
5import danger.ui.Bitmap;
6import danger.app.Application;
7import danger.app.ResourceDatabase;
8import danger.util.StringUtils;
9import org.twodot.cognet.Resources;
10
11public class SmileyEngine implements Resources {
12
13 private String text;
14
15 private static String emoticonsText[] = {"O:-)", ":-)", ":-(", ";-)", ":-P", "=-o", ":-*", ">:o", "8-)", ":-$", ":-!", ":-[", ":-\\", ":'(", ":-X", ":-D"};
16
17 private static int FIRST_ICON = 0xE000;
18 private int iconWidth;
19 private int iconCount;
20 private Bitmap[] iconSet;
21 Font font;
22
23 public static final char ICON_INNOCENT = 0xE000;
24 public static final char ICON_SMILING = 0xE001;
25 public static final char ICON_FROWNING = 0xE002;
26 public static final char ICON_WINKING = 0xE003;
27 public static final char ICON_STICKING_OUT_TONGUE = 0xE004;
28 public static final char ICON_SURPRISED = 0xE005;
29 public static final char ICON_KISSING = 0xE006;
30 public static final char ICON_YELLING = 0xE007;
31 public static final char ICON_COOL = 0xE008;
32 public static final char ICON_MONEY_MOUTH = 0xE009;
33 public static final char ICON_FOOT_IN_MOUTH = 0xE00A;
34 public static final char ICON_EMBARASSED = 0xE00B;
35 public static final char ICON_INDIFFERENT = 0xE00C;
36 public static final char ICON_CRYING = 0xE00D;
37 public static final char ICON_LIPS_SEALED = 0xE00E;
38 public static final char ICON_LAUGHING = 0xE00F;
39 public static final char ICON_COG = 0xE010;
40 public static final char ICON_IRC = 0xE011;
41 public static final char ICON_FLAG = 0xE012;
42 public static final char ICON_INFO = 0xE013;
43 public static final char ICON_BULLET = 0xE014;
44
45 public static final int LARGE_ICON_LIMIT = 11;
46
47 public SmileyEngine(Font f, String _text) {
48 this(f);
49 this.text = replaceWithUnicode(_text);
50 }
51
52 public SmileyEngine(Font f) {
53 font = f;
54 ResourceDatabase rdb = Application.getCurrentApp().getResources();
55 iconSet=null;
56 if (f.getAscent() + f.getDescent() >= LARGE_ICON_LIMIT)
57 iconSet = rdb.getBitmapArray(k11x11Icons);
58 else
59 iconSet = rdb.getBitmapArray(k5x6Icons);
60
61 iconWidth = iconSet.length;
62 }
63
64 public String getNext(int pixels) {
65 if (text.length() <= 0) return null;
66
67 int indexOfLastSpace = 0;
68 int totalWidth = 0;
69 String returnVal;
70
71 int x=0;
72 for (x=0; x<text.length(); x++) {
73 char c = text.charAt(x);
74 if (c == ' ') indexOfLastSpace = x;
75 totalWidth += charWidth(c);
76 if (totalWidth >= pixels) break;
77 }
78
79 // if indexOfLastSpace == 0, then we've got
80 // one big 'ol word that needs to be broken up
81 if (totalWidth < pixels && x == text.length()) {
82 returnVal = text;
83 text = "";
84 } else if (indexOfLastSpace == 0) {
85 returnVal = text.substring(0,x);
86 //if (x+1 < text.length())
87 text = text.substring(x);
88 } else {
89 // otherwise, break the line at indexOfLastSpace
90 returnVal = text.substring(0,indexOfLastSpace);
91 //if (indexOfLastSpace+1 < text.length())
92 text = text.substring(indexOfLastSpace+1);
93 }
94 return returnVal;
95 }
96
97 public static String replaceWithUnicode(String inText) {
98 String output = inText;
99
100 //replace all emoticons with corresponding character
101 for (int x=0; x < emoticonsText.length; x++) {
102 String thisUnicodeChar = "" + (char)(FIRST_ICON + x);
103
104 // replate both 'nosed' and 'noseless' versions.
105 output = replaceAll(output, emoticonsText[x], thisUnicodeChar);
106 output = replaceAll(output, StringUtils.stripChar(emoticonsText[x], '-'), thisUnicodeChar);
107 }
108 return output;
109 }
110
111 public static String replaceAll(String in, String what, String with) {
112 int pos = 0;
113 int whatLen = what.length();
114 int withLen = with.length();
115 while ((pos = in.indexOf(what, pos)) >= 0) {
116 in = in.substring(0,pos) + with + in.substring(pos+whatLen);
117 pos = pos + withLen;
118 }
119 return in;
120 }
121
122 public void drawString(int x, int y, String s, Pen p) {
123 int xpos = x;
124 p.setFont(font);
125 for (int c=0; c<s.length(); c++) {
126 xpos += drawChar(p, xpos, y, s.charAt(c));
127 }
128 }
129
130 public int charWidth(char c) {
131 if (isIcon(c)) {
132 return iconSet[0].getWidth();
133 } else {
134 return font.charWidth(c);
135 }
136 }
137
138 public int drawChar(Pen p, int xpos, int ypos, char chr) {
139 if (isIcon(chr)) {
140 p.drawBitmap(xpos, ypos-font.getAscent(), getIconBitmap(chr));
141 return charWidth(chr) + font.getGap();
142 } else {
143 p.drawChar(xpos, ypos, chr);
144 return font.charWidth(chr);
145 }
146 }
147
148 public boolean isIcon(char chr) {
149 return (chr >= FIRST_ICON && chr < FIRST_ICON + iconWidth);
150 }
151
152 public Bitmap getIconBitmap(char c) {
153 int ndx = c - 0xE000;
154 return iconSet[ndx];
155 }
156}
Note: See TracBrowser for help on using the repository browser.