Advertisement
rfmonk

caesar.js

Oct 24th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  
  3. Here is a tool to add to your codebreaking toolbox
  4.  
  5. 1. Type your secret message in the top box.
  6.  
  7. 2. Use the cipher disk to define your shift (from 1-26)
  8.  
  9. 3. Your encrypted message will appear below.
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. */
  18.  
  19. angleMode="radians";
  20.  
  21. //▲ ▼ ◀ △ ▷ ▽ ◁ ▴ ▾ ▸ ◂
  22.  
  23. var COLORS={
  24.  
  25.   //  Brit
  26.   BRIT_BLUE:   color(29,86,214,255),
  27.  
  28.   //  Applied Math
  29.   AM0:  color(48,167,222,255),   AM1: color(99,180,227,255),
  30.   AM2:  color(139,194,233,255),  AM3: color(41,115,149,255),
  31.   AM4:  color(37,104,135,255),   AM5: color(29,81,105,252),
  32.  
  33.   AM0_LT:  color(48,167,222,4),
  34.  
  35.   _WHITE: color(255,255,255,255), WHITE_LT:   color(255,255,255,128),
  36.   _BLACK: color(0,0,0,255),       BLACK_LT:   color(0,0,0,128),
  37.  
  38.   RED:    color(170,29,29,255),   RED_LT:     color(170,29,29,128),
  39.   BLUE:   color(29,86,170,255),   BLUE_LT:    color(29,86,170,128),
  40.   YELLOW: color(238,214,15,255),  YELLOW_LT:  color(238,214,15,128),
  41.  
  42.   ORANGE: color(238,136,15,255),  ORANGE_LT:  color(238,136,15,128),
  43.   GREEN:  color(158,182,58,255),  GREEN_LT:   color(158,182,58,128),
  44.   GRAY:   color(128,128,128,255), GRAY_LT:    color(128,128,128,128),
  45.  
  46.   BROWN:  color(155,145,135,255), BROWN_LT:   color(155,145,135,128),
  47.  
  48.   GRAY_16:  color(16,16,16,255),    GRAY_16LT: color(16,16,16,128),
  49.   GRAY_32:  color(32,32,32,255),    GRAY_32LT: color(32,32,32,128),
  50.   GRAY_64:  color(64,64,64,255),    GRAY_64LT: color(64,64,64,128),
  51.   GRAY_96:  color(96,96,96,255),    GRAY_96LT: color(96,96,96,128),
  52.   GRAY_128: color(128,128,128,255), GRAY_128LT: color(128,128,128,128),
  53.   GRAY_160: color(160,160,160,255), GRAY_160LT: color(160,160,160,128),
  54.   GRAY_192: color(192,192,192,255), GRAY_192LT: color(192,192,192,128),
  55.   GRAY_224: color(224,224,224,255), GRAY_224LT: color(224,224,224,128),
  56.   GRAY_240: color(240,240,240,255), GRAY_240LT: color(240,240,240,128),  
  57.   GRAY_255: color(255,255,255,255), GRAY_255LT: color(255,255,255,128)
  58. };
  59.  
  60. var app={ pressed:            false,
  61.           legend:             false,
  62.           instructions:       true,
  63.           animate:            true,
  64.           animateRate:        15,
  65.           legendX:            -360,
  66.  
  67.           title:              "Title",
  68.           toolTip:            "Welcome",
  69.  
  70.           currentNode:        -1,
  71.           nodeSize:           20,
  72.           focus:              1,
  73.  
  74.           background:         COLORS._BLACK,
  75.  
  76.           rotation:           90,
  77.           index:              0,
  78.  
  79.           text:               [],
  80.           key:                [],
  81.           encrypted:          [],
  82.           textFrequency:      [],
  83.           encryptedFrequency: []
  84. };
  85.  
  86. //  Control Types
  87. var CONTROLS={  TOGGLE:   0,
  88.                 INDEX:    1,
  89.                 BUTTON:   2,
  90.                 LABEL:    3,
  91.                 CLEAR:    4,
  92.                 DISK:     5
  93. };
  94.  
  95. var tooltips=["Display / Hide Legend (F1)",
  96.               "Click to type message (DEL to backspace)",
  97.               "Click to type shift word",
  98.               "Encrypted message",
  99.               "Clear the message (F5)",
  100.               "Click to lock/unlock",              
  101.               "Clear the message"
  102. ];
  103.  
  104. var CAPTIONS={DECRYPT:      "Decrypt",
  105.               ENCRYPT:      "Encrypt",
  106.               TITLE:        "Caesar Cipher",
  107.               DESCRIPTION:  "Type in a secret message.  Rotate the cipher disk
  108. and watch as each letter is shifted and the encrypted text appears.",
  109.               SHIFT:        "Encrypted message:",
  110.               MESSAGE:      "Type in a secret message:"};
  111.  
  112. //  Generic control properties
  113. var prop=function(i,x,y,w,h,t,v,f,c){
  114.  
  115.     this.i=i;     //  index
  116.     this.x=x;     //  left
  117.     this.y=y;     //  top
  118.     this.w=w;     //  width
  119.     this.h=h;     //  height
  120.     this.t=t;     //  control type    
  121.     this.f=f;     //  execute function
  122.     this.v=v;     //  value
  123.     this.c=c;     //  caption
  124.  
  125. };
  126.  
  127. var
  128. englishFrequency=[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0
  129. .772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.758,0.978,2.360,0.
  130. 150,1.974,0.074];
  131. var
  132. alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q",
  133. "r","s","t","u","v","w","x","y","z"];
  134.  
  135. //  Converts text from integer to alpha
  136. var convertText=function(param){
  137.  
  138.   var n=0;
  139.   var _s="";
  140.  
  141.   for(n=0; n<param.length; n++){
  142.  
  143.     if(param[n]===-2){ _s+=" ";}
  144.     else             { _s+=alphabet[param[n]]; }
  145.   }
  146.  
  147.   return _s;
  148.  
  149. };
  150.  
  151. var calculateRotation=function(){
  152.  
  153.   var _x=(mouseX-200);
  154.   var _y=-1*(mouseY-200);
  155.   var theta=degrees(atan2(_y,_x));
  156.  
  157.   //println(theta);
  158.  
  159.   var offset=360/52;
  160.  
  161.   if(theta>=0){ theta=theta-theta%(offset*2)+offset; }  
  162.   else       { theta=360+theta-theta%(offset*2)-offset; }
  163.  
  164.   var i=round((theta-90)/360*26);
  165.  
  166.   if(theta<90){ i+=26; }
  167.  
  168.   //println(i);
  169.  
  170.   app.rotation=theta;
  171.   app.key=26-i;
  172.  
  173. };
  174.  
  175. var index=function(prop){
  176.  
  177.   var _pressed=false;
  178.   var _locked=false;
  179.  
  180.   var _hit=function(x,y){
  181.     if(x>=prop.x && x<=prop.x+prop.w &&
  182.        y>=prop.y && y<=prop.y+prop.h){ return true; }
  183.   };
  184.   this.pressed=function(x,y)  { _pressed=true;  };
  185.   this.released=function(x,y) { _pressed=false; };
  186.  
  187.   var _draw=function(x,y){
  188.  
  189.     var disp=0;
  190.  
  191.     if(_hit(x,y)){  stroke(COLORS.GRAY_160);
  192.                     if(app.pressed){  disp=1; }
  193.                     else           {  disp=0; }
  194.     }
  195.     else{ stroke(COLORS.GRAY_128); disp=0; }
  196.  
  197.     strokeWeight(3);
  198.  
  199.     line(prop.x+disp, prop.y+disp,    prop.x+prop.w, prop.y+disp);
  200.     line(prop.x+disp, prop.y+5+disp,  prop.x+prop.w, prop.y+5+disp);
  201.     line(prop.x+disp, prop.y+10+disp, prop.x+prop.w, prop.y+10+disp);
  202.  
  203.   };
  204.   this.clicked=function(x,y)   { if(_hit(x,y)){ app.legend=!app.legend;       }
  205. };
  206.   this.refresh=function(x,y)   { _draw(x,y);                                    
  207. };
  208.   this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
  209. };
  210. };
  211.  
  212. var label=function(prop){  //  label definition
  213.  
  214.   var _hit=function(x,y){
  215.     if(x>=prop.x && x<=prop.x+prop.w &&
  216.        y>=prop.y && y<=prop.y+prop.h){ return true; }
  217.   };
  218.   var _focus=function(){ return app.focus===prop.i; };
  219.   var _draw=function(x,y){
  220.  
  221.     strokeWeight(1);
  222.     stroke(COLORS.GRAY);
  223.     textAlign(RIGHT,CENTER);
  224.     fill(COLORS.GRAY_224);
  225.  
  226.     if(_focus())   {      
  227.       if(prop.i!==3){ fill(COLORS._WHITE); stroke(COLORS.BRIT_BLUE);
  228. strokeWeight(1.5); }
  229.     }
  230.     if(_hit(x,y))  { if(prop.i!==3){ fill(COLORS._WHITE); } }
  231.  
  232.     rect(prop.x, prop.y, prop.w, prop.h, 2);
  233.  
  234.     fill(COLORS._BLACK);
  235.     textSize(12);
  236.     textAlign(RIGHT,CENTER);
  237.  
  238.     var s="";
  239.    
  240.     if      (prop.i===1){ s=convertText(app.text); }
  241.     else if(prop.i===2){ s=convertText(app.key); }
  242.     else if(prop.i===3){ s=convertText(app.encrypted); }
  243.  
  244.     text(s, prop.x-3 + prop.w, prop.y+prop.h/2+1);
  245.  
  246.   };
  247.   this.clicked=function(x,y)    { if(_hit(x,y)){ app.focus=1; } };
  248.   this.refresh=function(x,y)    { _draw(x,y); };
  249.   this.mouseMoved=function(x,y) { if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
  250. };
  251. };
  252.  
  253. var button=function(prop){  //  button definition
  254.  
  255.   //  private methods --------------------------------------------------
  256.   var _hit=function(x,y){
  257.     if(x>=prop.x && x<=prop.x+prop.w &&
  258.        y>=prop.y && y<=prop.y+prop.h){ return true; }
  259.   };
  260.  
  261.   var _draw=function(x,y){
  262.  
  263.     strokeWeight(1);
  264.  
  265.     var disp=0;
  266.     var clr=COLORS.RED;
  267.     var hit=_hit(x,y);
  268.  
  269.     if(hit){ fill(COLORS._WHITE);
  270.              if(app.pressed){  disp=1; }
  271.              else           {  disp=0; } }
  272.     else   { fill(COLORS.GRAY_192);
  273.               disp=0; }
  274.  
  275.     //  Shadow
  276.     noStroke();
  277.     fill(COLORS.GRAY_32);
  278.     rect(prop.x+2, prop.y+2, prop.w, prop.h, 2);
  279.  
  280.     //  Gradient
  281.     var incr=1;
  282.  
  283.     for(var n=0; n<prop.h; n+=incr){
  284.       if(hit) { stroke(lerpColor(color(192,192,192), color(224,224,224),
  285. n/prop.h));  }
  286.       else    { stroke(lerpColor(color(224,224,224), color(192,192,192),
  287. n/prop.h));  }
  288.       rect(prop.x+disp+1, prop.y+n+disp+1, prop.w-2, incr-2,1);
  289.     }
  290.  
  291.     //  Button
  292.     noFill();
  293.     rect(prop.x+disp, prop.y+disp, prop.w, prop.h, 2);
  294.  
  295.     //  Caption
  296.     fill(COLORS._BLACK);
  297.     textAlign(CENTER,CENTER);
  298.     textSize(10);
  299.     text(prop.c, prop.x + prop.w/2+disp, prop.y+prop.h/2+disp);
  300.  
  301.   };
  302.  
  303.   //  public methods --------------------------------------------------
  304.   this.pressed=function(x,y){  
  305.     if(_hit(x,y)){ }
  306.   };
  307.   this.released=function(x,y){
  308.     if(_hit(x,y)){}
  309.   };
  310.   this.clicked=function(x,y){
  311.     if(_hit(x,y)){ prop.f(prop.c); }
  312.   };
  313.   this.refresh=function(x,y){ _draw(x,y); };
  314.   this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
  315. };
  316. };
  317.  
  318. var clear=function(prop){  //  play/pause button definition
  319.  
  320.   //  private methods --------------------------------------------------
  321.   var _hit=function(x,y){
  322.     if(x>=prop.x-10 && x<=prop.x+10 &&
  323.        y>=prop.y-10 && y<=prop.y+10){ return true; }
  324.   };
  325.  
  326.   var _draw=function(x,y){
  327.  
  328.     strokeWeight(1);
  329.  
  330.     var disp=21;
  331.     var clr=COLORS.RED;
  332.     var hit=_hit(x,y);
  333.  
  334.   pushMatrix();
  335.  
  336.     translate(prop.x,prop.y);
  337.  
  338.     if(hit){ fill(COLORS.GRAY_160);
  339.              stroke(COLORS.GRAY_160);
  340.              if(app.pressed){  disp=0;
  341.                                rotate(45); }
  342.              else           {  disp=0; } }
  343.     else   { fill(COLORS.GRAY_128);
  344.               stroke(COLORS.GRAY_128);
  345.               disp=0; rotate(radians(0)); }
  346.  
  347.     //rect(prop.x+disp-prop.w/2, prop.y+disp-prop.h/2, prop.w, prop.h, 2);
  348.    
  349.     //fill(COLORS.GRAY_160);                  
  350.     textAlign(CENTER,CENTER);
  351.     textSize(24);
  352.     text("↻", 0+disp,0+disp);
  353.  
  354.   popMatrix();
  355.  
  356.   };
  357.  
  358.   //  public methods --------------------------------------------------
  359.   this.pressed=function(x,y){  
  360.     if(_hit(x,y)){ }
  361.   };
  362.   this.released=function(x,y){  
  363.     if(_hit(x,y)){}
  364.   };
  365.   this.clicked=function(x,y){
  366.     if(_hit(x,y)){
  367.       //app.focus=prop.i;
  368.       prop.f(prop.c);
  369.     }  
  370.   };  
  371.   this.refresh=function(x,y){ _draw(x,y); };
  372.   this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
  373. };
  374. };
  375.  
  376. var disk=function(prop){  //  disk definition
  377.  
  378.   var _cipherDisk=function(){
  379.  
  380.     fill(COLORS.BRIT_BLUE);
  381.     //text("Shift: " + app.key, 380,385);
  382.  
  383.     textAlign(CENTER,CENTER);
  384.     textSize(14);
  385.  
  386.     pushMatrix();
  387.  
  388.       translate(200,200);
  389.       scale(1,-1);
  390.  
  391.       strokeWeight(2);
  392.       stroke(COLORS.BLUE);
  393.       fill(COLORS._WHITE);
  394.       ellipse(0,0,230,230);
  395.      
  396.       stroke(COLORS.GRAY);
  397.       strokeWeight(1);
  398.      
  399.       fill(COLORS._WHITE);
  400.       ellipse(0,0,100,100);
  401.       rotate(radians(app.rotation+360/52));
  402.  
  403.       //  Interior disk
  404.       for(var n=0; n<26; n++){
  405.  
  406.         strokeWeight(0.5);
  407.         line(50,0,113,0);
  408.  
  409.         rotate(-radians(360/52));
  410.  
  411.         if(n===0){ fill(COLORS.BLUE);
  412.                     ellipse(101,0,20,20); }
  413.  
  414.         pushMatrix();
  415.  
  416.           scale(1,-1);
  417.           translate(102,0);
  418.           rotate(radians(90));
  419.  
  420.           textSize(14);
  421.           if(app.index===n){ fill(COLORS.YELLOW); }
  422.           else            { fill(COLORS.BRIT_BLUE); }
  423.           text(alphabet[n],0,0);
  424.  
  425.         popMatrix();
  426.  
  427.         rotate(-radians(360/52));
  428.  
  429.       }
  430.  
  431.     popMatrix();
  432.  
  433.   };
  434.  
  435.   var _textDisk=function(){
  436.  
  437.     var _w = 180;
  438.  
  439.     textAlign(CENTER,CENTER);
  440.     textSize(14);
  441.  
  442.     pushMatrix();
  443.  
  444.       translate(200,200);
  445.       scale(1,-1);
  446.  
  447.       stroke(COLORS.RED);
  448.       strokeWeight(2);
  449.       fill(COLORS.GRAY_16);
  450.       ellipse(0,0,_w,_w);
  451.      
  452.       stroke(COLORS.GRAY);
  453.       strokeWeight(1);
  454.      
  455.       fill(COLORS.GRAY_224);
  456.       ellipse(0,0,_w-40,_w-40);
  457.      
  458.       fill(COLORS._WHITE);
  459.       ellipse(0,0,_w-80,_w-80);
  460.      
  461.       rotate(radians(90+360/52));
  462.  
  463.       //  Interior disk
  464.       for(var n=0; n<26; n++){
  465.  
  466.         strokeWeight(0.5);
  467.         stroke(COLORS.RED_LT);
  468.         line(50,0,90,0);
  469.  
  470.         rotate(-radians(360/52));
  471.          
  472.         if(n===0){
  473.  
  474.             fill(COLORS.BLUE_LT);
  475.             pushMatrix();
  476.               rotate(radians(app.rotation-90));
  477.               if(prop.v){ stroke(COLORS.BRIT_BLUE); }
  478.               else      { stroke(COLORS.RED); }
  479.               strokeWeight(5);
  480.               line(47,0,0,0);
  481.               noStroke();
  482.               fill(COLORS.RED);
  483.               ellipse(80,0,20,20);
  484.              
  485.             popMatrix();
  486.  
  487.         }
  488.  
  489.         pushMatrix();
  490.  
  491.           scale(1,-1);
  492.           translate(102,0);
  493.           rotate(radians(90));
  494.  
  495.           textSize(12);
  496.           fill(COLORS._WHITE);
  497.           text(alphabet[n],0,22);
  498.  
  499.           textSize(8);
  500.           if(app.index===n){ fill(COLORS.RED); }
  501.           else            { fill(COLORS.GRAY_64); }
  502.          
  503.           text(n+1,0,42);
  504.          
  505.         popMatrix();
  506.  
  507.         rotate(-radians(360/52));
  508.  
  509.       }
  510.  
  511.     popMatrix();
  512.  
  513.     textSize(10);
  514.     textAlign(CENTER,CENTER);
  515.     if(!prop.v){ fill(COLORS.RED);
  516.                   text("Click to unlock",200,210); }
  517.     else{ fill(COLORS.BRIT_BLUE);
  518.            text("Click to lock",200,210); }
  519.  
  520.   };
  521.  
  522.   //  private methods --------------------------------------------------
  523.   var _hit=function(x,y){
  524.     if(dist(x,y,200,200)<prop.w/2){ return true; }
  525.   };
  526.  
  527.   var _draw=function(x,y){
  528.  
  529.     strokeWeight(1);
  530.  
  531.     var disp=0;
  532.     var clr=COLORS.RED;
  533.     var hit=_hit(x,y);
  534.  
  535.     if(hit){ fill(COLORS._WHITE);
  536.              if(app.pressed){  disp=1; }
  537.              else           {  disp=0; } }            
  538.     else   { fill(COLORS.GRAY_192);
  539.               disp=0; }
  540.  
  541.     if(prop.v){ calculateRotation(); }
  542.     else      {
  543.      
  544.       fill(COLORS.RED);
  545.       text("Click to unlock",200,200); }
  546.  
  547.     _cipherDisk();
  548.     _textDisk();
  549.  
  550.   };
  551.  
  552.   //  public methods --------------------------------------------------
  553.   this.pressed=function(x,y){  
  554.     if(_hit(x,y)){ }
  555.   };
  556.   this.released=function(x,y){
  557.     if(_hit(x,y)){}
  558.   };
  559.   this.clicked=function(x,y){
  560.     if(_hit(x,y)){ prop.f(prop.c); prop.v=!prop.v; }
  561.   };
  562.   this.refresh=function(x,y){ _draw(x,y); };
  563.   this.mouseMoved=function(x,y){ if(_hit(x,y)){ app.tooltip=tooltips[prop.i]; }
  564. };
  565. };
  566.  
  567. var encrypt=function(){
  568.  
  569.   var encrypt=[];
  570.  
  571.   var k=0;
  572.  
  573.   for(var j=0; j<app.text.length; j++){
  574.    
  575.     if(app.text[j]===-2){
  576.       app.encrypted[j]=-2;
  577.     }
  578.     else{
  579.       app.encrypted[j] = (app.text[j]+app.key)%alphabet.length;
  580.       k++;
  581.     }
  582.  
  583.   }
  584.  
  585. };
  586.  
  587. var processE=function(){
  588.  
  589.   var n=0;
  590.  
  591.   for(n=0; n<alphabet.length; n++){
  592.     app.encryptedFrequency[n]=0;
  593.   }
  594.  
  595.   encrypt();
  596.  
  597.   if(app.encrypted.length===0) { return; }
  598.  
  599.   for(n=0; n<app.encrypted.length; n++){
  600.     app.encryptedFrequency[app.encrypted[n]]++;
  601.   }
  602.  
  603. };
  604.  
  605. var reset=function(c){
  606.  
  607.   app.text=[];
  608. //  app.key=0;
  609.   app.encrypted=[];
  610.  
  611.   for(var n=0; n<alphabet.length;n++){
  612.     app.textFrequency[n]=0;
  613.     app.encryptedFrequency[n]=0;
  614.   }
  615.  
  616. };
  617.  
  618. var controls=function(){  //  controls collection definition
  619.  
  620.   var _controls=[];
  621.  
  622.   this.add=function(prop){      //  ADD
  623.  
  624.     switch (prop.t){
  625.  
  626.       case CONTROLS.BUTTON:   _controls.push(new button(prop));   break;
  627.       case CONTROLS.LABEL:    _controls.push(new label(prop));    break;
  628.  
  629.       case CONTROLS.INDEX:    _controls.push(new index(prop));    break;
  630.  
  631.       case CONTROLS.CLEAR:    _controls.push(new clear(prop));    break;
  632.  
  633.       case CONTROLS.DISK:    _controls.push(new disk(prop));      break;
  634.  
  635.     }
  636.  
  637.   };
  638.   this.refresh=function(x,y){   //  REFRESH
  639.     for(var ctl in _controls){ _controls[ctl].refresh(x,y); }
  640.   };
  641.   this.clicked=function(x,y){     //  CLICKED
  642.     for(var ctl in _controls){_controls[ctl].clicked(x,y);}
  643.   };
  644.   this.released=function() {    //  RELEASED
  645.     for(var ctl in _controls){_controls[ctl].released();}
  646.   };
  647.   this.mouseMoved=function(x,y) {  //  MOVED
  648.     for(var ctl in _controls){ _controls[ctl].mouseMoved(x,y);}
  649.   };
  650.   this.item=function(n)  { return _controls[n];       };
  651.   this.length=function(){ return _controls.length;  };
  652.  
  653. };
  654.  
  655. //  controls collection object
  656. var controls=new controls();
  657.  
  658. var legend=function(){
  659.  
  660.   if(app.legend){
  661.     if(app.legendX<40){ app.legendX+=20; }
  662.   }
  663.   else{
  664.     if(app.legendX>-320){ app.legendX-=20; }
  665.   }
  666.  
  667.   var _x=app.legendX;
  668.  
  669.   stroke(COLORS.AM5);
  670.   fill(COLORS._BLACK);
  671.   rect(_x,20,320,350);
  672.  
  673.   for(var n=20; n<370; n++){
  674.     stroke(29,81,105,275-n/2);
  675.     line(_x,n,_x+320,n);
  676.   }
  677.  
  678.   noFill();
  679.   stroke(COLORS.AM0);
  680.   strokeWeight(2);
  681.  
  682.   fill(COLORS.AM5);
  683.   //rect(50,20,300,360);
  684.  
  685.   var title=CAPTIONS.TITLE;
  686.   var sub1=CAPTIONS.DESCRIPTION;
  687.  
  688.   fill(COLORS._WHITE);
  689.  
  690.   textAlign(CENTER,TOP);
  691.   textSize(20);
  692.   text(title,_x+150,30);
  693.  
  694.   textAlign(LEFT,TOP);
  695.   textSize(12);
  696.   text(sub1,_x+10,60,300,400);
  697.  
  698. };
  699.  
  700. var blank=function(){
  701.  
  702. };
  703.  
  704. //  Add controls to the controls collection
  705. controls.add(new prop(0,5,5,15,15,CONTROLS.INDEX,false,legend,"index"));
  706.  
  707. controls.add(new prop(1,25,30,345,20,CONTROLS.LABEL,false,"","Message"));
  708. controls.add(new prop(3,25,350,345,20,CONTROLS.LABEL,false,"","Encrypted
  709. Text"));
  710.  
  711. controls.add(new prop(4,385,15,20,20,CONTROLS.CLEAR,false,reset, "clear"));
  712.  
  713. controls.add(new prop(5,100,150,230,230,CONTROLS.DISK,false,blank, "disk"));
  714.  
  715. var header=function(){
  716.  
  717.   fill(COLORS.GRAY_232);
  718.  
  719.   //  Message
  720.   textSize(16);
  721.   textAlign(LEFT,BOTTOM);
  722.  
  723.   fill(COLORS.BRIT_BLUE);
  724.   text(CAPTIONS.MESSAGE, 26,25);
  725.  
  726.   fill(COLORS.RED);
  727.   text(CAPTIONS.SHIFT, 26,345);
  728.  
  729. };
  730.  
  731. var telemetry=function(){
  732.  
  733.   textSize(10);
  734.   fill(COLORS.GRAY_232);
  735.   text(app.tooltip,10,395);
  736.  
  737. };
  738.  
  739. var grid=function(){
  740.  
  741.   var n=0;
  742.  
  743.   strokeWeight(1);    
  744.   stroke(COLORS.GRAY_16);
  745.  
  746.   for(n=0; n<=height-40; n+=20){
  747.     line(0,n,width,n);
  748.   }
  749.  
  750.   for(n=0; n<=width; n+=20){
  751.     line(n,0,n,height-40);
  752.   }
  753.  
  754. };
  755.  
  756.  
  757.  
  758. reset();
  759.  
  760. var draw=function(){
  761.  
  762.   // Order is important
  763.   background(app.background);
  764.  
  765.   grid();
  766.  
  767.   //frequency();
  768.  
  769.   controls.refresh(mouseX,mouseY);
  770.  
  771.   header();
  772.  
  773.   telemetry();
  774.  
  775.   //cipherDisk();
  776.   //textDisk();
  777.  
  778.   encrypt();
  779.  
  780.   //  Always last!
  781.   legend();
  782.  
  783. };
  784.  
  785. var mousePressed=function() { app.pressed=true; };
  786. var mouseReleased=function(){ app.pressed=false; };
  787.  
  788. var mouseClicked=function(){ controls.clicked(mouseX,mouseY);    };
  789. var mouseMoved=function()  { controls.mouseMoved(mouseX,mouseY); };
  790.  
  791. var keyPressed=function(){
  792.  
  793.  // println(keyCode);
  794.  
  795.   switch(keyCode) {
  796.  
  797.     case F1:  app.legend=!app.legend; break;
  798.     case F5:  reset();  break;
  799.  
  800.   }
  801.  
  802.   if(keyCode===37 || keyCode===127 || keyCode===110){
  803.  
  804.     if(app.focus===1){       app.text.pop(app.text.length);
  805.                              app.encrypted.pop(app.encrypted.length);
  806.                              return; }
  807.     else if(app.focus===2){ app.key.pop(app.key.length);
  808.                              app.encrypted.pop(app.encrypted.length);
  809.                              return; }
  810.   }
  811.  
  812.   var n=keyCode-65;
  813.   var _s=-3;
  814.  
  815.   if (n>-1 && n<alphabet.length){
  816.  
  817.     _s=n;
  818.  
  819.     if (app.focus===1){ app.textFrequency[n]++; }
  820.  
  821.   }    
  822.   else if(keyCode===32){ _s=-2; }
  823.  
  824.   if(_s!==-3){
  825.  
  826.     switch(app.focus){
  827.  
  828.       case 1: app.text.push(_s);  break;
  829.       case 2: if(_s!==-2){ app.key.push(_s); }   break;
  830.  
  831.       default:                    break;
  832.  
  833.     }
  834.  
  835.   }
  836.  
  837.   encrypt();
  838.  
  839. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement