Mercurial > hg > forks > multipaint-js
view events.pde @ 106:3abfab85e7bc
Change it so that each "machine type" uses separate local storage variable.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Jul 2018 06:22:10 +0300 |
parents | 6429d0b757cd |
children | a0d0994e9bac |
line wrap: on
line source
//collects mouse/key events and directly related command parsing boolean insidewindow(int x, int y) { // XXX is this correct? return (x > width - g_hedge || y > height - g_vedge); } void keyPressed() { g_dirty = true; if (key == ESC) { key = 0; } else if (key == CODED) { switch (keyCode) { case RIGHT: g_ofx++; break; case LEFT: g_ofx--; break; case UP: g_ofy--; break; case DOWN: g_ofy++; break; } refresh(); if (platform == MACOSX) { if (keyCode == COMMAND) g_control = true; } else { if (keyCode == CONTROL) g_control = true; } } else { // send to the indirect key handling // so commands can be invoked elsewhere too command(int(key)); } } void keyReleased() { if (key == CODED) { if (keyCode == CONTROL) g_control = false; if (keyCode == SHIFT) g_shift = false; if (platform == MACOSX) { if (keyCode == COMMAND) g_control = false; } else { if (keyCode == CONTROL) g_control = false; } } } void mouseMoved() { g_dirty = true; if (moicon(16 * 16 * g_uizoom, height - 32 * g_uizoom, 16 * g_uizoom, 24 * g_uizoom)) help(int('<')); if (moicon(16 * 16 * g_uizoom, height - 8 * g_uizoom, 16 * g_uizoom, 8 * g_uizoom)) help(int('>')); if (moicon(0, height - 32 * g_uizoom, 15 * 16 * g_uizoom, 32 * g_uizoom)) help(TAB); if (moicon(width - 32 * g_uizoom, 0, 32 * g_uizoom, 16 * g_uizoom)) help(int('.')); } void mouseDragged() { int msx, msy; mouseMoved(); g_iconx = mouseX; g_icony = mouseY; if (insidewindow(g_iconx, g_icony)) { if (g_phase == 0) { g_piconx = g_iconx; g_picony = g_icony; g_iconx = -1; g_icony = -1; g_repanel = -2; g_tooltrigger = 100; return; } else { g_iconx = -1; g_icony = -1; g_piconx = -1; g_picony = -1; } } else { if (g_data[int('m')] == 0 && g_data[int('M')] == 0) { msx = g_iconx - (g_windowx); msy = g_icony - (g_windowy); msx = g_msx - (g_windowx); msy = g_msy - (g_windowy); } else { msx = g_iconx; msy = g_icony; } g_iconx = -1; g_icony = -1; } g_iconx = -1; g_icony = -1; if (mouseButton == CENTER) return; if (g_tooltrigger == 0) g_tooltrigger = 1; } void mousePressed() { int msx, msy; g_dirty = true; g_iconx = mouseX; g_icony = mouseY; g_button = mouseButton; g_realbutton = g_button; if (insidewindow(g_iconx, g_icony)) { if (g_phase == 0) { g_repanel = -2; g_tooltrigger = 100; g_piconx = g_iconx; g_picony = g_icony; } else { g_iconx = -1; g_icony = -1; } return; } else { g_msx = g_iconx; g_msy = g_icony; if (g_data[int('m')] == 0 && g_data[int('M')] == 0) { msx = g_iconx - (g_windowx); msy = g_icony - (g_windowy); msx = g_msx - (g_windowx); msy = g_msy - (g_windowy); } else { msx = g_iconx; msy = g_icony; } if (g_button != CENTER) { if (tool() != 5 && tool() != 4) store_undo(); if (tool() == 4) { //cut brush results in undo if (g_button == RIGHT) store_undo(); } } else { g_button = 255; g_repanel = -2; if (g_shift) g_button = 254; } g_phase = 0; do_tool(msx, msy, g_button); g_phase = 1; } } void mouseReleased() { int swap; g_dirty = true; g_tooltrigger = 0; g_klikkeri = 0; if (tool() == 8) { if (g_phase == 1) { g_phase = 0; rectangle(g_rx, g_ry, g_rx2, g_ry2); } } if (tool() == 7) { //circle if (g_phase == 1) { g_phase = 0; docircle(g_rx, g_ry, g_rx2, g_ry2); } } if (tool() == 6) { if (g_phase == 1) { g_phase = 0; doline(g_rx, g_ry, g_rx2, g_ry2, 0); } } if (g_data[int('4')] == 1) { if (g_phase == 1) { g_phase = 0; g_rubbermode = 0; refresh(); g_data[int('4')] = 0; g_data[int('d')] = 0; g_data[int('r')] = 0; g_data[int('R')] = 0; g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('q')] = 0; g_data[int('p')] = 0; g_data[int('t')] = 0; g_data[int('n')] = 0; switcher(2); if (g_multic == 1 || g_hzoomer == 2) { g_rx = chop2(g_rx); g_rx2 = chop2(g_rx2); } g_bsourcex = g_rx; g_bsourcey = g_ry; g_bsourcex2 = g_rx2; g_bsourcey2 = g_ry2; if (g_bsourcex <= 0) g_bsourcex = 0; if (g_bsourcey <= 0) g_bsourcey = 0; if (g_bsourcex >= X) g_bsourcex = X - 1; if (g_bsourcey >= Y) g_bsourcex = Y - 1; if (g_bsourcex2 <= 0) g_bsourcex2 = 0; if (g_bsourcey2 <= 0) g_bsourcey2 = 0; if (g_bsourcex2 >= X) g_bsourcex2 = X - 1; if (g_bsourcey2 >= Y) g_bsourcey2 = Y - 1; set_tool(1); g_btype = 9; g_repanel = -2; if (g_button == RIGHT) { storeparameters(); g_data[int('f')] = 1; g_data[int('X')] = 0; g_data[int('Y')] = 0; doborx(g_rx, g_ry, g_rx2, g_ry2); restoreparameters(); } } consistency(); } if (g_phase == 1) { g_phase = 0; g_rubbermode = 0; refresh(); //needed otherwise the pen color is kept e.g. after right mousebutton } } void macro_command(int ckey) { //filter out load/save if (ckey == 's' || ckey == 'S' || ckey == 'l' || ckey == 'E' || ckey == 'w' || ckey == 'W' || ckey == 'A') return; command(ckey); } void command(int ckey) { int swap; g_dirty = true; //keypresses change g_data[] values directly as 1/0 switches //handling these presses "indirectly" adds complexity here //but is valuable elsewhere = corresponding icons have shortcuts automatically if (ckey == '-') ckey = 'h'; if (ckey == '+') ckey = 'H'; if (ckey == TAB) { if (g_shift == false) { if (g_britemode == 1) { if (g_realfront == 7) g_realfront = -1; if (g_realfront == 15) g_realfront = 7; } selectcolor(0, g_realfront + 1); g_repanel = -2; } else { if (g_britemode == 1) { if (g_realfront == 8) g_realfront = 16; if (g_realfront == 0) g_realfront = 8; } selectcolor(0, g_realfront - 1); g_repanel = -2; } } //println("ckey+"+ckey); if (g_control == true) { int UNDO = 26, REDO = 25, PASTE = 22, SAVE = 19, LOAD = 15, NEW = 14, COPY = 3, COPYALT = 24, ALL = 1; if (platform == MACOSX) { UNDO = 122; REDO = 25; PASTE = 22; SAVE = 115; LOAD = 111; NEW = 110; COPY = 99; COPYALT = 120; ALL = 97; } if (ckey == UNDO) { ckey = 'u'; if (g_shift) ckey = 'U'; } //undo if (ckey == REDO) ckey = 'U'; if (ckey == PASTE) ckey = '9'; if (ckey == SAVE) { ckey = 'S'; if (g_shift) ckey = 's'; } //save if (ckey == LOAD) ckey = 'l'; if (ckey == NEW) ckey = 'o'; if (ckey == COPY || ckey == COPYALT) ckey = '4'; if (ckey == ALL) { switcher(2); g_bsourcex = 0; g_bsourcey = 0; g_bsourcex2 = X - 1; g_bsourcey2 = Y - 1; ckey = '9'; g_data[int('p')] = 0; g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('q')] = 0; g_data[int('z')] = 0; g_phase = 0; g_rubbermode = 0; set_tool(1); } } if (ckey >= 128 && ckey <= 131) { g_bsize = ckey - 128; g_btype = 0; } if (ckey >= 132 && ckey <= 135) { g_bsize = ckey - 132; g_btype = 1; } if (ckey == ' ' || ckey == '.') { if (ckey == '.') { g_bsize = 0; g_btype = 0; g_data[int('n')] = 0; } ckey = '3'; if (ckey == ' ') {} } if (ckey >= 0 && ckey <= 255) { //g_depressed[ckey-48]=10; } if (ckey == ',') { g_repanel = -2; do_tool(mouseX, mouseY, 255); do_tool(g_mx, g_my, 255); return; } if (ckey == 'v') { g_repanel = -2; // do_tool(mouseX,mouseY,254); do_tool(g_mx, g_my, 254); return; } if (ckey == '[') { fixed_raster_command(int('x')); message("R.OFFSET " + str(g_raster_offset_x) + "." + str(g_raster_offset_y)); g_repanel = -2; return; } if (ckey == ']') { fixed_raster_command(int('y')); message("R.OFFSET " + str(g_raster_offset_x) + "." + str(g_raster_offset_y)); g_repanel = -2; return; } if (ckey == '(') { fixed_raster_command(int('p')); message("RASTER " + str(g_raster_no) + "/9|[=PREV|]=NEXT"); g_repanel = -2; return; } if (ckey == ')') { fixed_raster_command(int('n')); message("RASTER " + str(g_raster_no) + "/9|[=PREV|]=NEXT"); g_repanel = -2; return; } if (ckey == '>') { selectcolor(0, g_rgb[int(g_map[1])]); message("Current=Back"); g_repanel = -2; return; } if (ckey == '<') { g_repanel = -2; swap = g_farge; g_farge = g_backg; g_ofarge = g_farge; g_backg = swap; g_realfront = byte(g_farge); g_realback = byte(g_backg); } if (ckey == '0') { if (g_data[int('m')] == 0 && g_data[int('M')] == 0) { if (tool() != 0) { g_prevtool = tool(); } } else { ckey = 'm'; } } if (ckey == '9') { g_repanel = -2; g_btype = 9; return; } if (ckey >= '0' && ckey <= '8') { g_repanel = -2; set_tool(ckey - 48); g_phase = 0; g_rubbermode = 0; g_rx = -1; g_ry = -1; g_rx2 = -1; g_ry2 = -1; if (g_data[ckey] > 1) g_data[ckey] = 0; if (ckey == '4') { g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('h')] = 0; g_data[int('q')] = 0; g_data[int('t')] = 0; g_data[int('n')] = 0; } } if (ckey >= 'A' && ckey <= 'Z') { g_repanel = -2; g_data[ckey]++; if (g_data[ckey] > 1) g_data[ckey] = 0; if (ckey == 'Q') { g_repanel = -2; g_msgctr = 0; help(int('B')); g_msgctr = 50; } if (ckey == 'G') { g_data[int('G')] = 0; switch (g_gridx) { case 8: g_gridx = 16; g_gridy = 16; break; case 16: g_gridx = 4; g_gridy = 4; break; case 4: g_gridx = 8; g_gridy = 8; break; } g_repanel = -2; g_msgctr = 0; help(int('g')); g_msgctr = 50; } if (ckey == 'I') { // message("sprites shitted"); //poopsprites(); // poopspecialsprites(); } if (ckey == 'T') { // message("chars shitted"); // poopultimachars(); // poopsprites(); } if (ckey == 'G') { if (g_data[int('g')] == 0) g_data[int('g')] = 1; } if (g_data[int('M')] == 0) g_boxreconstruct = 2; if (g_data[int('M')] == 1) { if (ckey == 'M') { g_ofx = int(g_storedcoordx / 8) - int(magx() / 2); g_ofy = int(g_storedcoordy / 8) - int(magy() / 2); g_data[int('m')] = 0; message("Super|Magnify"); if (tool() == 0) { set_tool(g_prevtool); g_phase = 0; g_rubbermode = 0; } } } if (g_data[int('B')] == 1) { g_data[int('B')] = 0; if (machine != MSX) { store_undo(); g_map[0] = byte(zxcolor(g_farge)); sussborder(); message("Set border"); } if (machine == MSX) { g_data[int('C')] = 1; } } if (g_data[int('C')] == 1) { g_data[int('C')] = 0; if (int(g_map[1]) != 255) { store_undo(); if (machine == PLUS4M && g_farge == g_map[2]) { message("1st and 2nd|can't be|same"); refresh(); return; } g_map[1] = byte(g_farge); if (machine == MSX) { g_map[0] = byte(g_farge); sussborder(); } message("Set back|color"); } } if (g_data[int('V')] == 1) { g_data[int('V')] = 0; if (int(g_map[1]) != 255) { store_undo(); if (machine == PLUS4M && g_farge == g_map[1]) { message("1st and 2nd|can't be|same"); refresh(); return; } g_map[2] = byte(g_farge); message("Set 2nd|back color"); } } if (g_data[int('W')] == 1) { g_data[int('W')] = 0; if (g_formatext != "") { exportfmt = true; message("Format|export"); } else { message("None for|" + g_name); } } if (g_data[int('N')] == 1) { g_data[int('N')] = 0; if (++g_animspeed > 3) g_animspeed = 0; message("Playbrush|speed=" + g_animspeed); } refresh(); } if (ckey >= 'a' && ckey <= 'z') { g_repanel = -2; g_data[ckey]++; if (g_data[ckey] > 1) { if (ckey == 'm' || ckey == 'M') { g_boxreconstruct = 2; g_data[int('M')] = 0; g_data[int('m')] = 0; } g_data[ckey] = 0; } if (g_data[int('t')] == 1) { if (ckey == 't') message("Tile mode"); } if (ckey == 'n') { if (g_data[int('n')] == 1) { switcher(2); infersize(); if (g_data[int('n')] == 1) { command(int('1')); command(int('9')); g_data[int('p')] = 0; g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('q')] = 0; g_data[int('t')] = 0; } } } if (g_data[int('i')] == 1) { g_data[int('i')] = 0; selectcolor(0, nextluminance(g_farge, 1)); } if (g_data[int('k')] == 1) { g_data[int('k')] = 0; selectcolor(0, nextluminance(g_farge, -1)); } if (g_data[int('d')] == 0) { if (ckey == 'd') consistency(); } if (g_data[int('z')] == 1) { g_data[int('z')] = 0; int orient; orient = g_data[int('x')] * 100 + g_data[int('y')] * 10 + g_data[int('q')]; if (orient == 0) { g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('q')] = 1; } if (orient == 1) { g_data[int('x')] = 1; g_data[int('y')] = 1; g_data[int('q')] = 0; } if (orient == 110) { g_data[int('x')] = 1; g_data[int('y')] = 1; g_data[int('q')] = 1; } if (orient == 111) { g_data[int('x')] = 0; g_data[int('y')] = 0; g_data[int('q')] = 0; } if (orient == 100) { g_data[int('x')] = 0; g_data[int('y')] = 1; g_data[int('q')] = 1; } if (orient == 11) { g_data[int('x')] = 0; g_data[int('y')] = 1; g_data[int('q')] = 0; } if (orient == 10) { g_data[int('x')] = 1; g_data[int('y')] = 0; g_data[int('q')] = 1; } if (orient == 101) { g_data[int('x')] = 1; g_data[int('y')] = 0; g_data[int('q')] = 0; } message("Rotate|brush"); } if (ckey == 'm') { if (g_data[int('M')] == 1) { g_data[int('m')] = 0; ckey = 0; g_boxreconstruct = 2; } g_data[int('M')] = 0; } if (g_data[int('m')] == 1) { if (ckey == 'm') { g_data[int('M')] = 0; int moux, mouy; moux = int(g_mx / g_wzoom); mouy = int(g_my / g_wzoom); g_ofx = int(moux / 8) - int(magx() / 2); g_ofy = int(mouy / 8) - int(magy() / 2); message("Magnify"); if (tool() == 0) { set_tool(g_prevtool); g_phase = 0; g_rubbermode = 0; } } } refresh(); } if (g_data[int('J')] == 1) { g_data[int('J')] = 0; g_data[int('j')] = 0; spare(); store_undo(); message("Copied to|" + (g_spare ? "Spare" : "Front")); switcher(4); spare(); } if (g_data[int('j')] == 1) { g_data[int('j')] = 0; spare(); message((g_spare ? "Spare" : "Front") + " Page"); makecolor(259, g_r[g_map[0]], g_g[g_map[0]], g_b[g_map[0]]); g_boxreconstruct = 2; } if (g_data[int('E')] == 1) { g_data[int('E')] = 0; message("Source|export"); exportsrc = true; } if (g_data[int('A')] == 1) { g_data[int('A')] = 0; message("Executable|export"); exportemu = true; } if (g_data[int('w')] == 1) { g_data[int('w')] = 0; importfmt = true; } if (g_data[int('O')] == 1) { g_data[int('O')] = 0; store_undo(); for (int xx = 0; xx < X * Y; xx++) { g_map[1024 + xx] = 0; } for (int xx = 0; xx < X; xx++) { for (int yy = 0; yy < Y; yy++) { g_farge = g_backg; makepoint(xx, yy); g_farge = g_ofarge; } } } if (g_data[int('o')] == 1) { g_data[int('o')] = 0; message("Clear all"); store_undo(); storeparameters(); g_data[int('r')] = 0; g_data[int('R')] = 0; g_data[int('d')] = 0; g_data[int('t')] = 0; for (int xx = 0; xx < X * Y; xx++) { g_map[1024 + xx] = 0; } for (int xx = 0; xx < X; xx++) { for (int yy = 0; yy < Y; yy++) { g_farge = g_backg; makepoint(xx, yy); g_farge = g_ofarge; } } restoreparameters(); } if (g_data[int('e')] == 1) { g_data[int('e')] = 0; } if (g_data[int('h')] == 1) { g_data[int('h')] = 0; g_bsize--; if (g_bsize < 0) g_bsize = 0; g_msgctr = -2; help(int('h')); } if (g_data[int('H')] == 1) { g_data[int('H')] = 0; g_bsize++; if (g_bsize > 10) g_bsize = 10; g_msgctr = -2; help(int('h')); } if (g_data[int('u')] == 1) { g_data[int('u')] = 0; message("Undo"); restore_undo(); refresh(); g_boxreconstruct = 2; } if (g_data[int('U')] == 1) { g_data[int('U')] = 0; message("Redo"); redo_undo(); refresh(); } if (machine == SPECTRUM) { if (g_data[int('i')] == 1) { g_data[int('i')] = 0; if (g_iconmode == 0) { message("Icons|not loaded!"); } else { message("Store|icons"); mpSaveNativeImage("icons.bin"); } } if (g_data[int('I')] == 1) { g_data[int('I')] = 0; message("Restore|icons"); mpLoadNativeImage("icons.bin", true); refresh(); g_iconmode = 1; } } if (g_data[int('l')] == 1) { g_data[int('l')] = 0; fileselect = true; } if (g_data[int('s')] == 1) { g_data[int('s')] = 0; saveas = true; } if (g_data[int('S')] == 1) { g_data[int('S')] = 0; g_data[int('s')] = 0; if (mpHaveLocalStorage()) { var fdata = mpGetNativeImage(); if (fdata != null) { var fstr = JSON.stringify(fdata); localStorage.setItem("MPIMAGE_DATA"+ str(machine), fstr); message("Saved to lstorage"); } else message("ERROR"); } else message("Lstorage is not available"); } if (g_data[int('L')] == 1) { g_data[int('L')] = 0; if (mpHaveLocalStorage()) { var fstr = localStorage.getItem("MPIMAGE_DATA"+ str(machine)); if (fstr != null && typeof(fstr) == "string") { var fdata = JSON.parse(fstr); if (typeof(fdata) == "object" && Array.isArray(fdata)) { int res = mpSetNativeImage(fdata, false); if (res != 0) message("Wrong machine"); else message("Loaded lstorage"); } else message("Invalid data in lstorage"); } else message("No data in lstorage"); } else message("Lstorage is not available"); } }