Skip to content

Instantly share code, notes, and snippets.

@miura
Created November 16, 2012 15:43
Show Gist options
  • Select an option

  • Save miura/4088330 to your computer and use it in GitHub Desktop.

Select an option

Save miura/4088330 to your computer and use it in GitHub Desktop.
Image-Clicking based Calculator (demo for the course)
// calculater based on an Image.
// kota MIura
ww = 300;
hh = 600;
rows = 7;
newImage("Calc", "8-bit Black", ww, hh, 1);
setFont("Monospaced", 14, "bold");
setColor(255, 255, 255);
drawString("Exit: Right Click", 10, hh - 15);
// row number assignments
num0row = 0;
num1row = 1;
num2row = 2;
num3row = 3;
opsrow = 4;
ansrow = 5;
screenrow = 6;
vspacing = hh/rows;
nums0 = newArray("1", "2", "3");
numsspacing0 = drawkeys(ww, hh, rows, nums0, num0row);
nums1 = newArray("4", "5", "6");
numsspacing1 = drawkeys(ww, hh, rows, nums1, num1row);
nums2 = newArray("7", "8", "9");
numsspacing2 = drawkeys(ww, hh, rows, nums2, num2row);
nums3 = newArray("", "0", "");
numsspacing3 = drawkeys(ww, hh, rows, nums3, num3row);
operators = newArray("+", "-", "*", "/");
opsspacing = drawkeys(ww, hh, rows, operators, opsrow);
buttons = newArray("=", "C", "<");
ansspacing = drawkeys(ww, hh, rows, buttons, ansrow);
cols = newArray(numsspacing0, numsspacing1, numsspacing2, numsspacing3, opsspacing, ansspacing, hh);
shift=1;
ctrl=2;
rightButton=4;
alt=8;
leftButton=16;
insideROI = 32; // requires 1.42i or later
x2=-1; y2=-1; z2=-1; flags2=-1;
if (getVersion>="1.37r")
setOption("DisablePopupMenu", true);
loop = true;
key = "";
formula = "";
ans = "";
doneflag = false;
while (loop) {
getCursorLoc(x, y, z, flags);
if (x!=x2 || y!=y2 || z!=z2 || flags!=flags2) {
s = " ";
if (flags&leftButton!=0){
s = s + "<left>";
crow = floor(y / vspacing);
ccol = floor(x / cols[crow]);
//print(x+" "+y+" "+z+" "+flags + "" + s);
//print("row:", crow, "col", ccol);
if (crow == num0row) {
key = nums0[ccol];
} else if (crow == num1row) {
key = nums1[ccol];
} else if (crow == num2row) {
key = nums2[ccol];
} else if (crow == num3row) {
key = nums3[ccol];
} else if (crow == opsrow) {
key = operators[ccol];
} else if (crow == ansrow) {
key = buttons[ccol];
} else if (crow == screenrow) {
//output field
}
if (key == "=") {
ans = eval("return toString(" + formula + ");");
//print(formula, "=", ans);
updateScreen(ww, screenrow, vspacing, numsspacing0/2 , "" + formula + "=" + ans);
//wait(50);
formula = "";
doneflag = true;
} else {
if (doneflag)
clearScreen(ww, screenrow, vspacing);
if (key == "C") {
formula = "";
clearScreen(ww, screenrow, vspacing);
} else if (key == "<") {
if (lengthOf(formula)>0)
formula = substring(formula, 0, lengthOf(formula)-1);
} else if (crow < ansrow)
formula += key;
//print(formula);
updateScreen(ww, screenrow, vspacing, numsspacing0/2 , formula);
doneflag = false;
}
}
if (flags&rightButton!=0){
loop = false;
close();
}
}
x2=x; y2=y; z2=z; flags2=flags;
wait(20);
}
function updateScreen(ww, row, vspace, xpos, stdout){
setColor(0, 0, 0);
fillRect(0, row*vspace, ww, vspace);
setFont("Monospaced", 18, "bold");
setColor(255, 255, 255);
drawString(stdout, xpos, row*vspace + vspace/2);
updateDisplay();
}
function clearScreen(ww, row, vspace){
setColor(0, 0, 0);
fillRect(0, row*vspace, ww, vspace);
setColor(255, 255, 255);
updateDisplay();
}
function drawkeys(width, height, rows, itemA, row){
setFont("Monospaced", 42, "bold");
hspacing = width/itemA.length;
vspacing = height/rows;
for(i = 0; i < itemA.length; i++){
xpos = hspacing * i + hspacing/2 - 10;
ypos = vspacing * row + vspacing/2;
drawString(itemA[i], xpos, ypos);
}
return hspacing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment