Notations/src/About.java

187 lines
5.7 KiB
Java
Raw Normal View History

2018-11-14 22:59:39 +02:00
/*
* aNNiMON 2010
* For more info visit http://annimon.com/
*/
/*
* На основе класса MultiLineText от А.С. Ледкова
* www.mobilab.ru
*/
import com.annimon.XID;
import com.annimon.ui.ESkin;
import com.annimon.ui.EWindow;
import java.util.Vector;
import javax.microedition.lcdui.*;
public class About extends EWindow {
private static final String PATH = "/about";
private Font def;
private int defHeight;
private String str; // текст
private int mh, y0; // Положение верхнего края текста
private int dy; // Шаг при прокрутке текста
private int textheight; // Общая высота текста
private Vector strLines;
public About(String about) {
super(about);
str = new XID().loadText(PATH);
def = Font.getDefaultFont();
dy = defHeight = def.getHeight()+2;
mh = super.hh - 1;
SetTextPar();
}
public void paint(Graphics g, int w, int h) {
g.setFont(def);
g.setColor(ESkin.textColor);
int y1 = y0;
for (int i = 0; i < strLines.size(); i++) {
if ((y1 + defHeight) > 0) {
g.drawString(strLines.elementAt(i).toString(), 1, 3 + y1, Graphics.LEFT | Graphics.TOP);
}
y1 += defHeight;
if (y1 > mh) break;
}
}
public void keyEvent(int key, byte state) {
if(state!=RELEASED) {
int ga = getGameAction(key);
if(ga==UP) MoveUp();
else if(ga==DOWN) MoveDown();
else if(ga==LEFT) PageUp();
else if(ga==RIGHT) PageDown();
else if(ga==FIRE || key==KEY_LSK || key==KEY_RSK)
Main.midlet.dsp.setCurrent(Main.midlet.main);
redraw();
}
}
protected void pointerPressed(int x, int y) {
int w3 = w/3;
int h3 = h/3;
if(y<h3 && (x>w3 && x<(w-w3))) keyPressed(UP);
else if(y>(h-h3) && (x>w3 && x<(w-w3))) keyPressed(DOWN);
if(x<w3 && (y>h3 && y<(h-h3))) keyPressed(LEFT);
else if(x>(w-w3) && (y>h3 && y<(h-h3))) keyPressed(RIGHT);
else if(x<w3 && y>(h-h3)) keyPressed(-6);
else if(x>(w-w3) && y>(h-h3)) keyPressed(-7);
else if( (x>w3 && x<(w-w3)) && (y>h3 && y<(h-h3)) ) keyPressed(FIRE);
}
private void SetTextPar() {
//Разбиваем строку на массив строк
strLines = null;
strLines = new Vector();
String[] arr = splitString(str, "\n");
for(int i=0; i<arr.length; i++) {
String substr = arr[i];
int i0 = 0, space = 0, in = 0, j = 0, jw = 0; //Смещение от начала строки
int imax = substr.length(); //Длина строки
boolean isexit = true;
y0 = 0;
while (isexit) {
space = substr.indexOf(" ", i0 + 1);
if (space <= i0) {
space = imax;
isexit = false;
}
j = def.stringWidth(substr.substring(i0, space));
if (jw + j < w) {//Слово умещается
jw = jw + j;
i0 = space;
} else {//Слово не умещается
strLines.addElement(substr.substring(in, i0));
in = i0 + 1;
jw = j;
if (j > w) {//Слово полностью не умещается в строке
space = i0;
while (jw > w) {
j = 0;
while (j < w) {
space++;
j = def.stringWidth(substr.substring(in, space));
}
space = space - 1;
j = def.stringWidth(substr.substring(in, space));
strLines.addElement(substr.substring(in, space));
jw = jw - j;
i0 = space;
in = space;
}
jw = 0;
} else {
i0 = space;
}
}
}
strLines.addElement(substr.substring(in, imax));
}
textheight = strLines.size() * defHeight;
}
private void MoveDown() {
if (textheight > mh) {
y0 = y0 - dy;
if (mh - y0 > textheight) {
y0 = mh - textheight;
}
}
}
private void MoveUp() {
if (textheight > mh) {
y0 = y0 + dy;
if (y0 > 0) {
y0 = 0;
}
}
}
private void PageUp() {
if (textheight > mh) {
y0 = y0 + mh;
if (y0 > 0) {
y0 = 0;
}
}
}
private void PageDown() {
if (textheight > mh) {
y0 = y0 - mh;
if (mh - y0 > textheight) {
y0 = mh - textheight;
}
}
}
private String[] splitString(String str, String delim) {
if ("".equals(str) || delim == null || delim.length() == 0) return new String[]{str};
Vector v = new Vector();
int pos = 0;
int newpos = str.indexOf(delim, 0);
while (newpos != -1) {
v.addElement(str.substring(pos, newpos));
pos = newpos + delim.length();
newpos = str.indexOf(delim, pos);
}
v.addElement(str.substring(pos));
String[] s = new String[v.size()];
v.copyInto(s);
return s;
}
}