8-bit drawing tool
other author's drawing
http://www.mingchoi.twbbs.org/Flash/utli/8-bit_drawing_tool_result.html
/**
* Copyright Mingchoi ( http://wonderfl.net/user/Mingchoi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/lLNg
*/
/*
1.pick color tools
2.bmp import/export
3.more userfriendly color table
*/
package {
import flash.events.MouseEvent;
import flash.display.Sprite;
[SWF(width=465, height=465, backgroundColor=0xFFFFFF, frameRate=24)]
public class FlashTest extends Sprite {
public function FlashTest() {
this.graphics.beginFill(0xEEEEEE,1);
this.graphics.drawRect(0,0,465,465);
this.graphics.endFill();
inittrace(stage);
var testBitmap:pixelViewer=new pixelViewer(16,16);
testBitmap.x=335;
testBitmap.y=50
addChild(testBitmap);
var testBitmap2:pixelViewer=new pixelViewer(16,16);
testBitmap2.width*=2;
testBitmap2.height*=2;
testBitmap2.x=335;
testBitmap2.y=76;
addChild(testBitmap2);
var testBitmap3:pixelViewer=new pixelViewer(16,16);
testBitmap3.width*=4;
testBitmap3.height*=4;
testBitmap3.x=335;
testBitmap3.y=128;
addChild(testBitmap3);
var testBitmap4:pixelViewer=new pixelViewer(16,16);
testBitmap4.width*=8;
testBitmap4.height*=8;
testBitmap4.x=335;
testBitmap4.y=212;
addChild(testBitmap4);
var testingDrawPixelPanel:drawPixelPanel=new drawPixelPanel(16,16,20);
addChild(testingDrawPixelPanel);
testingDrawPixelPanel.x=10
testingDrawPixelPanel.y=35;
testingDrawPixelPanel.addRemoteViewer(testBitmap);
testingDrawPixelPanel.addRemoteViewer(testBitmap2);
testingDrawPixelPanel.addRemoteViewer(testBitmap3);
testingDrawPixelPanel.addRemoteViewer(testBitmap4);
/*
var testImgArray:Array=[[0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF],
[0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F],
[0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0],
[0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00],
[0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000],
[0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000],
[0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000],
[0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000]];
testBitmap4drawing.setFormArray(testImgArray);
testBitmap4drawing.setFormPixel(0,0,0xFF0060FF);
testBitmap4drawing.setFormPixel(4,4,0x00000000);
*/
//download function
new MingChoiSign(stage,"8-bit drawing tool","@2012/1/18");
}
}
}
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.utils.ByteArray;
import flash.geom.Matrix;
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import com.bit101.components.*;
import com.adobe.images.PNGEncoder;
/*
===DrawPixelPanel===
*/
class drawPixelPanel extends Sprite{
private var mainColumn:int;
private var mainRow:int;
private var mainBoxSize:int;
private var grid:Sprite;
private var colorBoxArray:Array=[];
private var nowDrawingColor:uint=0xFF000000;
private var alphaBG:pixelViewer;
private var drawingPaper:pixelViewer;
private var colorChooser:ColorChooser;
private var alphaSlider:HUISlider;
private var remoteViewerListLength:int=0
private var remoteViewerList:Array=[];
private var exportButton:PushButton;
private var exportScale:NumericStepper;
public function drawPixelPanel(bornColumn:int=16,bornRow:int=16,boxSize:int=10,remoteViewer:pixelViewer=null){
mainColumn=bornColumn;
mainRow=bornRow;
mainBoxSize=boxSize;
if(remoteViewer is pixelViewer){
addRemoteViewer(remoteViewer);
}
init();
}
private function init():void{
//fake alpha BG
alphaBG=new pixelViewer(mainColumn*2,mainRow*2);
alphaBG.width*=10;
alphaBG.height*=10;
addChild(alphaBG);
alphaBG.setFakeAlpha();
//real drawing
drawingPaper=new pixelViewer(mainColumn,mainRow);
drawingPaper.width*=20;
drawingPaper.height*=20;
addChild(drawingPaper);
//Grid
drawGrid();
//grid.visible=false;
//
drawingPaper.addEventListener(MouseEvent.MOUSE_DOWN,handleDrawOnPaper);
//
colorChooser = new ColorChooser(this, 0, -25, 0x000000, onChangeDrawColor);
colorChooser.usePopup = true;
addChild(colorChooser);
alphaSlider = new HUISlider(this,100,-25,'alpha', onChangeDrawColor);
alphaSlider.minimum = 0;
alphaSlider.maximum = 255;
alphaSlider.value = 255;
//download image button
exportButton = new PushButton(this,300,-25,"Download PNG *",exportPNG);
exportScale = new NumericStepper(this,400,-23);
exportScale.width = 50;
exportScale.value = 16;
}
private function drawGrid():void{
var i:int;
var j:int;
var fColumnWidth:int=mainBoxSize*mainColumn;
var fRowHeight:int=mainBoxSize*mainRow;
var temp:int;
grid=new Sprite();
grid.mouseEnabled=false;
grid.graphics.lineStyle(1,0,1);
for(i=0;i<=mainColumn;i++){
temp=mainBoxSize*i;
grid.graphics.moveTo(temp,0);
grid.graphics.lineTo(temp,fRowHeight);
}
for(j=0;j<=mainRow;j++){
temp=mainBoxSize*j;
grid.graphics.moveTo(0,temp);
grid.graphics.lineTo(fColumnWidth,temp);
}
grid.graphics.endFill();
addChild(grid);
}
private function handleDrawOnPaper(evt:MouseEvent=null):void{
handleDrawOnPaperMove(evt);
drawingPaper.addEventListener(MouseEvent.MOUSE_MOVE,handleDrawOnPaperMove);
drawingPaper.addEventListener(MouseEvent.MOUSE_UP,handleDrawOnPaperEnd);
drawingPaper.addEventListener(MouseEvent.MOUSE_OUT,handleDrawOnPaperOut);
}
private function handleDrawOnPaperMove(evt:MouseEvent=null):void{
var clickedPointX:int=Math.floor(evt.localX);
var clickedPointY:int=Math.floor(evt.localY);
drawingPaper.setFormPixel(clickedPointX,clickedPointY,nowDrawingColor);
//trace("clicked "+clickedPointX+","+clickedPointY+"("+nowDrawingColor+")");
if(remoteViewerListLength != 0){
var i:int;
for(i=0;i<remoteViewerListLength;i++){
remoteViewerList[i].setFormPixel(clickedPointX,clickedPointY,nowDrawingColor);
}
}
}
private function handleDrawOnPaperEnd(evt:MouseEvent=null):void{
drawingPaper.removeEventListener(MouseEvent.MOUSE_MOVE,handleDrawOnPaperMove);
drawingPaper.removeEventListener(MouseEvent.MOUSE_UP,handleDrawOnPaperEnd);
drawingPaper.removeEventListener(MouseEvent.MOUSE_OUT,handleDrawOnPaperEnd);
}
private function handleDrawOnPaperOut(evt:MouseEvent=null):void{
if(evt.localX>=mainColumn || evt.localY>=mainRow || evt.localX<=0 || evt.localY<=0){
drawingPaper.removeEventListener(MouseEvent.MOUSE_MOVE,handleDrawOnPaperMove);
drawingPaper.removeEventListener(MouseEvent.MOUSE_UP,handleDrawOnPaperEnd);
drawingPaper.removeEventListener(MouseEvent.MOUSE_OUT,handleDrawOnPaperEnd);
}
}
private function onChangeDrawColor(evt:Event=null):void{
if(alphaSlider.value==0){
nowDrawingColor=0x00000000;
}else{
nowDrawingColor=colorChooser.value+Math.floor(alphaSlider.value)*16777216;
}
}
public function addRemoteViewer(remoteViewer:pixelViewer):void{
remoteViewerListLength++;
remoteViewerList.push(remoteViewer);
}
private function exportPNG(evt:MouseEvent=null):void{
if(exportScale.value>64){
exportScale.value = 64;
}else if(exportScale.value<=0){
exportScale.value = 1;
}
var m_scale:Matrix=new Matrix();
m_scale.scale(exportScale.value,exportScale.value);
var exportBitmapData:BitmapData=new BitmapData(mainColumn*exportScale.value,mainRow*exportScale.value,true,0x00000000);
exportBitmapData.draw(drawingPaper.getBitmapData(),m_scale);
new PNGWriter(new Bitmap(exportBitmapData));
}
}
/*
===pixelViewer===
example:
var testImgArray:Array=[[0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF],
[0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F],
[0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0],
[0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00],
[0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000,0xFF00F000],
[0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000,0xFF9F0000],
[0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000,0xFFF00000],
[0xFFF00000,0xFF9F0000,0xFF00F000,0xFF009F00,0xFF0000F0,0xFF00009F,0xFFFFFFFF,0xFF000000]];
testBitmap.setFormArray(testImgArray);
testBitmap.setFormPixel(0,0,0xFF0060FF);
*P.S. 0xAARRGGBB
*/
class pixelViewer extends Sprite{
private var bitmapData:BitmapData;
private var mainBitmap:Bitmap;
private var mainWidth:int;
private var mainHeight:int;
public function pixelViewer(bornWidth:int=16,bornHeight:int=16){
mainWidth=bornWidth;
mainHeight=bornHeight;
bitmapData=new BitmapData(mainWidth,mainHeight,true,0x00FFFFFF);
mainBitmap=new Bitmap(bitmapData);
addChild(mainBitmap);
}
public function setFormArray(getArray:Array):void{
var i:int;
var j:int;
for(j=0;j<mainHeight;j++){
for (i=0;i<mainWidth;i++){
bitmapData.setPixel32(i,j,getArray[j][i]);
}
}
}
public function setFormPixel(sX:int,sY:int,sC:uint):void{
bitmapData.setPixel32(sX,sY,sC);
}
public function setFakeAlpha():void{
var i:int;
var j:int;
for(j=0;j<mainHeight;j++){
if(j%2==1){
i=1;
}else{
i=0;
}
for(i=i;i<mainWidth;i=i+2){
bitmapData.setPixel32(i,j,0xFFFFFFFF);
}
}
for(j=0;j<mainHeight;j++){
if(j%2==1){
i=0;
}else{
i=1;
}
for(i=i;i<mainWidth;i=i+2){
bitmapData.setPixel32(i,j,0xFFCCCCCC);
}
}
}
public function getBitmapData():BitmapData{
return bitmapData;
}
}
///
import com.adobe.images.PNGEncoder;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.utils.ByteArray;
class PNGWriter extends URLLoader {
public static const URL:String = "http://www.mingchoi.twbbs.org/Flash/utli/8-bit_drawing_tool.php";
public function PNGWriter(target:DisplayObject):void{
var source:BitmapData = new BitmapData(target.width, target.height, true, 0x00000000);
source.draw(target);
var stream:ByteArray = PNGEncoder.encode(source);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(PNGWriter.URL);
request.method = URLRequestMethod.POST;
request.data = stream;
request.requestHeaders.push(header);
navigateToURL(request,"_blank");
//load(request);
}
}
///// WONDERFL TRACE /////
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
function inittrace(s:Stage):void
{
WTrace.initTrace(s);
}
//global trace function
var trace:Function;
//wtreace class
class WTrace
{
private static var FONT:String = "Fixedsys";
private static var SIZE:Number = 12;
private static var TextFields:Array = [];
private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void
{
trace_stage = stg;
trace = wtrace;
}
private static function scrollup():void
{
// maximum number of lines: 100
if (TextFields.length > 100)
{
var removeme:TextField = TextFields.shift();
trace_stage.removeChild(removeme);
removeme = null;
}
for(var x:Number=0;x<TextFields.length;x++)
{
(TextFields[x] as TextField).y -= SIZE*1.2;
}
}
public static function wtrace(... args):void
{
var s:String="";
var tracefield:TextField;
for (var i:int;i < args.length;i++)
{
// imitating flash:
// putting a space between the parameters
if (i != 0) s+=" ";
s+=args[i].toString();
}
tracefield= new TextField();
tracefield.autoSize = "left";
tracefield.text = s;
tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE);
tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);
scrollup();
TextFields.push(tracefield);
}
}
// Sign! @2011/11/14 , Created by -MingChoi-
// Sign! @2012/1/18 , Created by -MingChoi-
import flash.text.StyleSheet;
import flash.display.Stage;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
class MingChoiSign extends Sprite{
private var trace_stage:Stage;
public function MingChoiSign(stg:Stage,workName:String="",dateString:String="",signName:String="Created by -MingChoi-",webSite:String="http://www.mingchoi.twbbs.org/",bornAlpha:Number=0.8,bornColor:String="#000000",bornColor2:String="#0090FF"){
trace_stage = stg;
trace_stage.addChild(WritingSign(workName,dateString,signName,webSite,bornAlpha,bornColor,bornColor2));
}
private function WritingSign(workName:String="",dateString:String="",signName:String="Created by -MingChoi-",webSite:String="http://www.mingchoi.twbbs.org/",bornAlpha:Number=0.8,bornColor:String="#000000",bornColor2:String="#0090FF"):TextField{
var myCSS:StyleSheet = new StyleSheet();
myCSS.setStyle("a:link", {color:bornColor,textDecoration:'none'});
myCSS.setStyle("a:hover", {color:bornColor2,textDecoration:'underline'});
myCSS.setStyle("onR", {textAlign:'right'});
var tf:TextField=new TextField();
tf.styleSheet=myCSS;
tf.selectable=false;
tf.autoSize="right";
tf.htmlText="<onR><a href='"+webSite+"' target='_blank'>"+workName+dateString+"\n"+signName+"</a></onR>";
tf.x=trace_stage.stageWidth-tf.width;
tf.y=trace_stage.stageHeight-tf.height;
tf.alpha=bornAlpha;
return tf;
}
}