topical media & game development

talk show tell print

flex-led-char.mx

flex-led-char.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="10" height="30">
      <mx:Script>
          <![CDATA[
              
              public static const DISPLAY_7_SEG:uint = 0;
              public static const DISPLAY_14_SEG:uint = 1;
              public static const DISPLAY_16_SEG:uint = 2;
              
              //standard A-G segments for 7seg display (see wikipedia)
              private var segA:Boolean;
              private var segB:Boolean;
              private var segC:Boolean;
              private var segD:Boolean; 
              private var segE:Boolean;
              private var segF:Boolean;
              private var segG:Boolean;
              
              //extra segments for 14 and 16 segment display
              //ie the middle verticals and diagonals
              private var segH:Boolean;
              private var segI:Boolean;
              private var segJ:Boolean;
              private var segK:Boolean; 
              private var segL:Boolean;
              private var segM:Boolean;
              
              //segments that are split into 2 in the 14 and/or 16 segment display
              //ie. A, D, and G. These segments represent the 2 halves of each segment
              private var segA1:Boolean;
              private var segA2:Boolean;
              private var segD1:Boolean;
              private var segD2:Boolean;
              private var segG1:Boolean;
              private var segG2:Boolean;
              
              private var segDP:Boolean; //decimal point
              private var segP1:Boolean; //points that make up the ':' symbol
              private var segP2:Boolean;
              
              public var displayType:uint = DISPLAY_16_SEG;
              public var thickness:int = 3;
              public var gap:int = 1;
              public var onColor:uint = 0xFF0000;
              public var offColor:uint = 0x330000;
              public var alwaysDrawDots:Boolean = false;
              
              private static var mappings:Array = [];
              mappings["0"] = 63;
              mappings["1"] = 6;
              mappings["2"] = 91;
              mappings["3"] = 79;
              mappings["4"] = 102;
              mappings["5"] = 109;
              mappings["6"] = 125;
              mappings["7"] = 7;
              mappings["8"] = 127;
              mappings["9"] = 111;
              
              //require 14 or 16 seg
              mappings["A"] = 119;
              mappings["B"] = 387343;
              mappings["C"] = 57;
              mappings["D"] = 125199;
              mappings["E"] = 254009;
              mappings["F"] = 155697;
              mappings["G"] = 385085;
              mappings["H"] = 118;
              mappings["I"] = 2313;
              mappings["J"] = 35073;
              mappings["K"] = 132656;
              mappings["K"] = 132656;
              mappings["L"] = 56;
              mappings["M"] = 694;
              mappings["N"] = 1206;
              mappings["O"] = 63;
              mappings["P"] = 115;
              mappings["Q"] = 1087;
              mappings["R"] = 1139;
              mappings["S"] = 109;
              mappings["T"] = 2305;
              mappings["U"] = 62;
              mappings["V"] = 4656;
              mappings["W"] = 5174;
              mappings["X"] = 5760;
              mappings["Y"] = 4736;
              mappings["Z"] = 4617;
              
              mappings[""] = 2413;
              mappings[":"] = 3145728;
              mappings["."] = 524288;
              mappings["#"] = 8128;
              mappings["%"] = 473956;
              mappings["&"] = 1151;
              mappings["!"] = 524544;
              
              public function set skew(angle:Number):void
              {
                  var skew:Matrix = new Matrix(1,Math.tan(0),Math.tan(-angle/180 * Math.PI),1,this.x,this.y);
                  this.transform.matrix = skew;
              }
              
              public function interpretChar(char:String):void
              {
                  if(char.length == 1 && mappings[char] != null)
                  {
                      segA = mappings[char] & 0x01;
                      segB = (mappings[char] & 0x02) >> 1;
                      segC = (mappings[char] & 0x04) >> 2;
                      segD = (mappings[char] & 0x08) >> 3;
                      segE = (mappings[char] & 0x10) >> 4;
                      segF = (mappings[char] & 0x20) >> 5;
                      segG = (mappings[char] & 0x40) >> 6;
                      
                      segH = (mappings[char] & 0x80) >> 7;
                      segI = (mappings[char] & 0x100) >> 8;
                      segJ = (mappings[char] & 0x200) >> 9;
                      segK = (mappings[char] & 0x400) >> 10;
                      segL = (mappings[char] & 0x800) >> 11;
                      segM = (mappings[char] & 0x1000) >> 12;
                      
                      segA1 = ((mappings[char] & 0x2000) >> 13) || segA;
                      segA2 = ((mappings[char] & 0x4000) >> 14) || segA;
                      segD1 = ((mappings[char] & 0x8000) >> 15) || segD;
                      segD2 = ((mappings[char] & 0x10000) >> 16) || segD;
                      segG1 = ((mappings[char] & 0x20000) >> 17) || segG;
                      segG2 = ((mappings[char] & 0x40000) >> 18) || segG;
                      
                      segDP = (mappings[char] & 0x80000) >> 19;
                      segP1 = (mappings[char] & 0x100000) >> 20;
                      segP2 = (mappings[char] & 0x200000) >> 21;
                      
                      invalidateDisplayList();
                  }
              }
              
              override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
              {
                  graphics.clear();
                  
                  //the dots
                  if(segP1 || segP2 || alwaysDrawDots)
                  {
                      drawDot(unscaledWidth/2 + thickness,unscaledHeight/4,thickness/2,segP1);
                      drawDot(unscaledWidth/2 + thickness,(unscaledHeight * 3)/4,thickness/2,segP2);
                  }
                  
                  if(segDP || alwaysDrawDots)
                      drawDot(unscaledWidth/2 + thickness,unscaledHeight - (thickness * 3)/2,thickness/2,segDP);
                  
                  //the segments for the 7seg
                  if(displayType != DISPLAY_16_SEG)
                      drawSegment(thickness/2 + gap,0,unscaledWidth - thickness - (2 * gap),thickness,segA,true,false);
                  
                  drawSegment(width - thickness,thickness/2 + gap,thickness,(unscaledHeight - thickness - 4*gap) / 2,segB);
  
                  drawSegment(width - thickness,unscaledHeight/2 + gap,thickness,(unscaledHeight - thickness - 4*gap) / 2,segC);
                  
                  if(displayType != DISPLAY_16_SEG)
                      drawSegment(thickness/2 + gap,unscaledHeight - thickness,unscaledWidth - thickness - (2 * gap),thickness,segD,true,false);
  
                  drawSegment(0,unscaledHeight/2 + gap,thickness,(unscaledHeight - thickness - 4*gap) / 2,segE);
                  
                  drawSegment(0,thickness/2 + gap,thickness,(unscaledHeight - thickness - 4*gap) / 2,segF);
                  
                  if(displayType == DISPLAY_7_SEG)
                      drawSegment(thickness/2 + gap,unscaledHeight / 2 - thickness/2,unscaledWidth - thickness - (2 * gap),thickness,segG,true,false);
                  
                  
                  if(displayType == DISPLAY_14_SEG || displayType == DISPLAY_16_SEG)
                  {
                      //the diagonals
                      //top left
                      drawSegment(thickness + gap,thickness + gap,(unscaledWidth - (thickness*3))/2 - (2 * gap),(unscaledHeight - (thickness*3)) /2 - (gap * 2),segH,false,false);
                      //top right
                      drawSegment(unscaledWidth/2 + thickness/2 + gap,thickness + gap,(unscaledWidth - (thickness*3))/2 - (2 * gap),(unscaledHeight - (thickness*3)) /2 - (gap * 2),segJ,false,true);
                      //bottom right
                      drawSegment(unscaledWidth/2 + thickness/2 + gap,unscaledHeight/2 + thickness/2 + gap,(unscaledWidth - (thickness*3))/2 - (2 * gap),(unscaledHeight - (thickness*3)) /2 - (gap * 2),segK,false,false);
                      //bottom left
                      drawSegment(thickness + gap,unscaledHeight/2 + thickness/2 + gap,(unscaledWidth - (thickness*3))/2 - (2 * gap),(unscaledHeight - (thickness*3)) /2 - (gap * 2),segM,false,true);
                      
                      //the 2 central vertical segments
                      drawSegment(unscaledWidth/2 - thickness/2,thickness/2 + gap,thickness,(unscaledHeight/2) - thickness/2 - 2*gap,segI,true,true,(displayType == DISPLAY_16_SEG),true);
                      
                      drawSegment(unscaledWidth/2 - thickness/2,unscaledHeight/2 + gap,thickness,(unscaledHeight/2) - thickness/2 - 2*gap,segL,true,true,true,(displayType == DISPLAY_16_SEG));
                      
                      //the 2 horiz segments across the center, in place of segG in 16seg display
                      drawSegment(thickness/2 + gap,unscaledHeight / 2 - thickness/2,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segG || segG1,true,false);
                      
                      drawSegment(unscaledWidth/2 + gap,unscaledHeight / 2 - thickness/2,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segG || segG2,true,false);
                  }
                  
                  if(displayType == DISPLAY_16_SEG)
                  {
                      //the 2 horiz segments at the top in place of segA in 16seg display
                      drawSegment(thickness/2 + gap,0,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segA || segA1,true,false);
                      
                      drawSegment(unscaledWidth/2 + gap,0,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segA || segA2,true,false);
                      
                      //the 2 horiz segments at the bottom in place of segD in 16seg display
                      drawSegment(thickness/2 + gap,unscaledHeight - thickness,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segD || segD1,true,false);
                      
                      drawSegment(unscaledWidth/2 + gap,unscaledHeight - thickness,unscaledWidth/2 - thickness/2 - (2 * gap),thickness,segD || segD2,true,false);            
                          
                  }
                  
              }
              
              private function drawDot(x:Number,y:Number,radius:Number,on:Boolean):void
              {
                  graphics.beginFill(on?onColor:offColor,1);
                  graphics.drawCircle(x,y,radius);
              }
              
              private function drawSegment(x:Number,y:Number,width:Number,height:Number,on:Boolean, straight:Boolean = true, type:Boolean = true, begintip:Boolean = true, endtip:Boolean = true):void
              {
                  graphics.beginFill(on?onColor:offColor,1);
                  var tipw:Number;
                  if(straight)
                  {
                      if(type) //vertical
                      {
                          tipw = Math.tan(Math.PI/4) * (width / 2);
                          graphics.moveTo(x + 0,y + tipw);
                          graphics.lineTo(x + width/2,y + (begintip?0:tipw));
                          graphics.lineTo(x + width,y + tipw);
                          graphics.lineTo(x + width,y + height - tipw);
                          graphics.lineTo(x + width/2,y + height - (endtip?0:tipw));
                          graphics.lineTo(x + 0,y + height - tipw);
                          graphics.lineTo(x + 0,y + tipw);
                      }
                      
                      else //horizontal
                      {
                          tipw = Math.tan(Math.PI/4) * (height / 2);
                          graphics.moveTo(x + tipw, y + 0);
                          graphics.lineTo(x + width - tipw,y + 0);
                          graphics.lineTo(x + width - (endtip?0:tipw),y + height/2);
                          graphics.lineTo(x + width - tipw,y + height);
                          graphics.lineTo(x + tipw, y + height);
                          graphics.lineTo(x + (begintip?0:tipw),y + height/2);
                          graphics.moveTo(x + tipw, y + 0);
                      }
                  }
                  else
                  {
                      var angle:Number = Math.abs(Math.atan2(height,width));
                      var tipHorizEdge:Number = Math.sin(angle) * thickness;
                      var tipVertEdge:Number = Math.cos(angle) + thickness;
                      if(type) //diagonal bl to tr
                      {    
                          graphics.moveTo(x + 0,y + height);
                          graphics.lineTo(x + 0, y + height - tipVertEdge);
                          graphics.lineTo(x + width - tipHorizEdge,y + 0);
                          graphics.lineTo(x + width,y + 0);
                          graphics.lineTo(x + width, y + tipVertEdge);
                          graphics.lineTo(x + tipHorizEdge,y + height);
                          graphics.lineTo(x + 0,y + height);
                      }
                      else //diagonal tl to br
                      {
                          graphics.moveTo(x + 0,y + 0);
                          graphics.lineTo(x + tipHorizEdge,y + 0);
                          graphics.lineTo(x + width,y + height - tipVertEdge);
                          graphics.lineTo(x + width,y + height);
                          graphics.lineTo(x + width - tipHorizEdge,y + height);
                          graphics.lineTo(x + 0,y + tipVertEdge);
                          graphics.lineTo(x + 0,y + 0);
                          
                      }
                  }
                  
              }
              
          ]]>
      </mx:Script>
  </mx:Canvas>
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.