Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtynjala committed Aug 1, 2023
0 parents commit 7ab3c97
Show file tree
Hide file tree
Showing 360 changed files with 106,675 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.vscode/
asconfig.json
.code-workspace

.actionScriptProperties
.flexProperties
.flexLibProperties
.metadata/
.settings/
.project

*.iml
.idea/

.as3proj

.DS_Store
Thumbs.db

.swf
.swc
.swz
Binary file added ASCIIArt/AsciiArtApp.fla
Binary file not shown.
78 changes: 78 additions & 0 deletions ASCIIArt/AsciiArtApp.mxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns="*"
layout="vertical"
paddingTop="0"
paddingLeft="8"
paddingRight="8"
paddingBottom="0"
creationComplete="initApp()">

<mx:Script>
<![CDATA[
import com.example.programmingas3.asciiArt.ImageInfo;
import com.example.programmingas3.asciiArt.AsciiArtBuilder;
private var asciiArt:AsciiArtBuilder;
private function initApp():void
{
asciiArt = new AsciiArtBuilder();
asciiArt.addEventListener("ready", imageReady);
}
/**
* Called when the AsciiArtBuilder has loaded the image data and is ready to
* display an image.
*/
private function imageReady(event:Event):void
{
updatePreview();
}
/**
* Called when the "next image" button is pressed.
*/
private function nextImage():void
{
// Advance to the next image
asciiArt.next();
// update the image preview
updatePreview();
}
/**
* Updates the image preview display, including title and image, using
* the current image in the asciiArt object.
*/
private function updatePreview():void
{
var imageInfo:ImageInfo = asciiArt.currentImage.info;
bmp.load(AsciiArtBuilder.IMAGE_PATH + imageInfo.fileName);
sourceImage.title = imageInfo.title;
asciiArtText.text = asciiArt.asciiArtText;
}
]]>
</mx:Script>

<mx:Label id="title" text="ASCII Art Example" fontSize="24" fontStyle="bold" />
<mx:Label id="subtitle" text="From Programming ActionScript 3.0, Chapter 6: Working with strings" fontSize="12" />

<mx:HBox width="100%" height="100%" horizontalAlign="center">

<mx:Panel id="sourceImage" backgroundColor="#b7babc" borderAlpha="1" borderColor="#666" borderStyle="solid" borderThickness="1">
<mx:Image width="400" id="bmp" height="300"/>
<mx:ControlBar>
<mx:Spacer width="290" height="15"/>
<mx:Button label="Next Image" click="nextImage()"/>
</mx:ControlBar>
</mx:Panel>

<mx:TextArea id="asciiArtText" width="510" height="450" fontFamily="_typewriter" fontSize="8"></mx:TextArea>

</mx:HBox>

</mx:Application>
226 changes: 226 additions & 0 deletions ASCIIArt/com/example/programmingas3/asciiArt/AsciiArtBuilder.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package com.example.programmingas3.asciiArt
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLRequest;
import com.example.programmingas3.asciiArt.BitmapToAsciiConverter;
import com.example.programmingas3.asciiArt.Image;
import com.example.programmingas3.asciiArt.ImageInfo;

/**
* Provides appication-level functionality for the AsciiArt sample.
*/
public class AsciiArtBuilder extends EventDispatcher
{
// ------- Private vars -------

private const DATA_TARGET:String = "txt/ImageData.txt";
private var _imageInfoLoader:URLLoader;
private var _imageStack:Array;
private var _currentImageIndex:uint;


// ------- Constructor -------

public function AsciiArtBuilder()
{
_imageStack = new Array();
var request:URLRequest = new URLRequest(DATA_TARGET);
_imageInfoLoader = new URLLoader();
_imageInfoLoader.addEventListener(Event.COMPLETE, imageInfoCompleteHandler);
_imageInfoLoader.load(request);
}


// ------- Public Properties -------

public static const IMAGE_PATH:String = "image/";

public var asciiArtText:String = "";

public function get currentImage():Image
{
return _imageStack[_currentImageIndex];
}


// ------- Event Handlers -------

/**
* Called when the image info text file is loaded.
*/
private function imageInfoCompleteHandler(event:Event):void
{
var allImageInfo:Array = parseImageInfo();

buildImageStack(allImageInfo);
}


/**
* Called when the first image is loaded.
*/
private function imageCompleteHandler(event:Event):void
{
// move to the first image in the stack
next();
// notify any listeners that the application has finished its initial loading
var readyEvent:Event = new Event("ready");
dispatchEvent(readyEvent);
}



// ------- Public Methods -------

/**
* Advances the image stack to the next image, and populates the asciiArtText property
* with that image's ASCII Art representation.
*/
public function next():void
{
// Advance the "current image" index (or set it back to 0 if we're on the last one)
_currentImageIndex++;
if (_currentImageIndex == _imageStack.length)
{
_currentImageIndex = 0;
}
// generate the ASCII version of the new "current" image
var imageConverter:BitmapToAsciiConverter = new BitmapToAsciiConverter(this.currentImage);
this.asciiArtText = imageConverter.parseBitmapData();
}


// ------- Private Methods -------

/**
* Parses the contents of the loaded text file which contains information about the images
* to load, and creates an Array of ImageInfo instances from that data.
*
* @return An Array of ImageInfo instances.
*/
private function parseImageInfo():Array
{
var result:Array = new Array();

/* Parse the contents of the text file, and put its contents into an Array of ImageInfo
* instances.
* Each line of text contains info about one image, separated by tab (\t) characters,
* in this order:
* - file name
* - title
* - white threshold
* - black threshold
*
* Loop through the individual lines in the text file.
* Note that we skip the first line, since it only contains column headers.
*/
var lines:Array = _imageInfoLoader.data.split("\n");
var numLines:uint = lines.length;
for (var i:uint = 1; i < numLines; i++)
{
var imageInfoRaw:String = lines[i];
// trim leading or trailing white space from the current line
imageInfoRaw = imageInfoRaw.replace(/^ *(.*) *$/, "$1");
if (imageInfoRaw.length > 0)
{
// create a new image info record and add it to the array of image info
var imageInfo:ImageInfo = new ImageInfo();
// split the current line into values (separated by tab (\t) characters)
// and extract the individual properties
var imageProperties:Array = imageInfoRaw.split("\t");
imageInfo.fileName = imageProperties[0];
imageInfo.title = normalizeTitle(imageProperties[1]);
imageInfo.whiteThreshold = parseInt(imageProperties[2], 16);
imageInfo.blackThreshold = parseInt(imageProperties[3], 16);
result.push(imageInfo);
}
}
return result;
}


/**
* Capitalizes the first letter of each word in a String, unless the word
* is one of the English words which are not commonly capitalized
*
* @param str The String to "normalize"
* @return The String with the words capitalized
*/
private function normalizeTitle(title:String):String
{
var words:Array = title.split(" ");
var len:uint = words.length;

for(var i:uint; i < len; i++)
{
words[i] = capitalizeFirstLetter(words[i]);
}

return words.join(" ");
}



/**
* Capitalizes the first letter of a single word, unless it's one of
* a set of words which are normally not capitalized in English.
*
* @param word The word to capitalize
* @return The capitalized word
*/
private function capitalizeFirstLetter(word:String):String
{
switch (word)
{
case "and":
case "the":
case "in":
case "an":
case "or":
case "at":
case "of":
case "a":
// don't do anything to these words
break;
default:
// for any other word, capitalize the first character
var firstLetter:String = word.substr(0, 1);
firstLetter = firstLetter.toUpperCase();
var otherLetters:String = word.substring(1);
word = firstLetter + otherLetters;
}

return word;
}


/**
* Using an Array of ImageInfo instances, populates the image stack with Image instances.
*
* @param imageInfo An array of ImageInfo instances, containing the data about the
* image files to be loaded into the image stack.
*/
private function buildImageStack(imageInfo:Array):void
{
var image:Image;
var oneImageInfo:ImageInfo;
var listenerAdded:Boolean = false;
var numImages:uint = imageInfo.length;
for (var i:uint = 0; i < numImages; i++)
{
_currentImageIndex = 0;
oneImageInfo = imageInfo[i];
image = new Image(oneImageInfo);
_imageStack.push(image);
if(!listenerAdded)
{
image.addEventListener(Event.COMPLETE, imageCompleteHandler);
listenerAdded = true;
}
image.load();
}
}
}
}
Loading

0 comments on commit 7ab3c97

Please sign in to comment.