package Editor; import Main.Key; import Main.L; import Main.P; import Main.PPM; import Other.GetImage; import UI.UI; import com.annimon.ScreenShotCanvas; import com.annimon.resize.Resizer; import javax.microedition.lcdui.*; /** * Класс выбора цвета из палитры * @author aNNiMON */ public class PalColorSel extends ScreenShotCanvas implements Runnable { /** Размер курсора */ private static final byte CUR_SIZE = 4; /** Размер экрана */ private int w, h; /** Выполнение потока */ private boolean isRun; /** Нажатая клавиша */ private int key; /** Позиции курсора */ private int cx, cy; /** Размер шрифта */ private int fontHeight; /** Высота софтбара */ private int softBarHeight; /** Режим мультивыбора цвета (МЦ) */ private boolean multicolor; /** Идентификатор выбираемого цвета в МЦ режиме */ private byte multicolor_id; /** Картинка палитры цветов */ private Image pal; /** Предыдущий экран */ private Displayable previousScreen; /** * Конструктор */ public PalColorSel() { setFullScreenMode(true); w = getWidth(); h = getHeight(); previousScreen = PPM.midlet.ed; isRun = true; multicolor = false; key = 0; P.rc = P.gc = P.bc = 0; fontHeight = P.smBold.getHeight() + 2; cx = P.pcscx; cy = P.pcscy; if (cx == 0 && cy == 0) { P.pcscx = cx = w / 2; P.pcscy = cy = h / 2; } pal = new GetImage().loadImages(1); softBarHeight = UI.getSoftBarHeight(); Resizer resizer = new Resizer(w - 10, h - 4 * softBarHeight - fontHeight); resizer.setInterpolation(Resizer.BRESENHAM); resizer.setProcessingAlpha(false); pal = resizer.resizeImage(pal); new Thread(this).start(); } /** * Конструктор для МЦ режима * @param dspl экран, куда возвращаться * @param id идентификатор выбираемого цвета массива МЦ режима */ public PalColorSel(Displayable dspl, byte id) { this(); this.previousScreen = dspl; this.multicolor_id = id; multicolor = true; } protected void sizeChanged(int w, int h) { this.w = getWidth(); this.h = getHeight(); if (cx == 0 && cy == 0) { P.pcscx = cx = w / 2; P.pcscy = cy = h / 2; } pal = new GetImage().loadImages(1); Resizer resizer = new Resizer(w - 10, h - 4 * softBarHeight - fontHeight); resizer.setInterpolation(Resizer.BRESENHAM); resizer.setProcessingAlpha(false); pal = resizer.resizeImage(pal); super.sizeChanged(w, h); } public void paint(Graphics g) { g.setColor(P.backgrnd); g.fillRect(0, 0, w, h); UI.drawTitle(g, L.str[L.chooseColor] + ". " + L.str[L.palette] + "."); g.setFont(P.smBold); g.drawImage(pal, 5, softBarHeight + 2, 20); cursor(g); int rgb = Effects.get(pal, cx - 5, cy - softBarHeight - 2); P.rc = Effects.acolor(rgb, 'r'); P.gc = Effects.acolor(rgb, 'g'); P.bc = Effects.acolor(rgb, 'b'); int hh = fontHeight + softBarHeight / 2; int xh = h - fontHeight * 3; g.setColor(0xFFFFFF); g.fillRect(9, xh - 1, w - 16, hh + 2); g.setColor(0); g.fillRect(10, xh, w / 2 - 9, hh); Effects.drawRGBrect(g, P.ac, P.rc, P.gc, P.bc, 10, xh, w - 18, hh); g.setColor(P.obv); g.drawString(Integer.toHexString(rgb), 10, h - (fontHeight * 4), 20); UI.drawSoftBar(g, L.str[L.Ok], L.str[L.Back]); } private void cursor(Graphics g) { g.setColor(0x00); g.drawLine(cx + 1, cy, cx + CUR_SIZE, cy); g.drawLine(cx - CUR_SIZE, cy, cx - 1, cy); g.drawLine(cx, cy - CUR_SIZE, cx, cy - 1); g.drawLine(cx, cy + 1, cx, cy + CUR_SIZE); } public void run() { do { int ga = 0; if (key != 0) { ga = getGameAction(key); if (ga == UP) { cy--; if (cy < softBarHeight + 2) { cy = softBarHeight + 1 + pal.getHeight(); } } else if (ga == DOWN) { cy++; if (cy > softBarHeight + 1 + pal.getHeight()) { cy = softBarHeight + 2; } } else if (ga == LEFT) { cx--; if (cx < 5) { cx = w - 6; } } else if (ga == RIGHT) { cx++; if (cx > w - 6) { cx = 5; } } repaint(); } try { Thread.sleep(5L); } catch (Exception exception) { } } while (isRun); } public void keyPressed(int key) { super.keyPressed(key); int ga = getGameAction(key); if (key == Key.leftSoftKey) { leftSoft(); } else if (key == Key.rightSoftKey) { rightSoft(); } else if (ga == UP) { cy--; if (cy < softBarHeight + 2) { cy = softBarHeight + 1 + pal.getHeight(); } } else if (ga == DOWN) { cy++; if (cy > softBarHeight + 1 + pal.getHeight()) { cy = softBarHeight + 2; } } else if (ga == LEFT) { cx--; if (cx < 5) { cx = w - 6; } } else if (ga == RIGHT) { cx++; if (cx > w - 6) { cx = 5; } } else if (key == KEY_NUM0 && !multicolor) { int nrgb = Editor.rgb2; Editor.rgb2 = Effects.color(P.ac, P.rc, P.gc, P.bc); P.ac = Effects.acolor(nrgb, 'a'); P.rc = Effects.acolor(nrgb, 'r'); P.gc = Effects.acolor(nrgb, 'g'); P.bc = Effects.acolor(nrgb, 'b'); } repaint(); } protected void pointerDragged(int pix, int piy) { checkPointer(pix, piy); repaint(); } protected void pointerPressed(int pix, int piy) { int y = softBarHeight; if (pix > w - 2 * y && piy > h - y) { rightSoft(); } else if (pix < 2 * y && piy > h - y) { leftSoft(); } else { checkPointer(pix, piy); } repaint(); } protected void pointerReleased(int pix, int piy) { checkPointer(pix, piy); repaint(); } public void keyRepeated(int i) { key = i; } public void keyReleased(int i) { key = 0; } private void leftSoft() { if (multicolor) { P.multicolor[multicolor_id] = Effects.color(P.ac, P.rc, P.gc, P.bc); } else { Editor.rgb1 = Effects.color(P.ac, P.rc, P.gc, P.bc); Editor.G.setColor(Editor.rgb1); } rightSoft(); } private void rightSoft() { key = 0; P.pcscx = cx; P.pcscy = cy; isRun = false; PPM.dsp.setCurrent(previousScreen); } private void checkPointer(int pix, int piy) { cx = pix; cy = piy; if (cy < softBarHeight + 2) { cy = softBarHeight + 2; } else if (cy > softBarHeight + 1 + pal.getHeight()) { cy = softBarHeight + 1 + pal.getHeight(); } if (cx < 5) { cx = 5; } else if (cx > w - 6) { cx = w - 6; } } }