Skip to content

Commit 7ab3c97

Browse files
committed
Initial commit
0 parents  commit 7ab3c97

File tree

360 files changed

+106675
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

360 files changed

+106675
-0
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.vscode/
2+
asconfig.json
3+
.code-workspace
4+
5+
.actionScriptProperties
6+
.flexProperties
7+
.flexLibProperties
8+
.metadata/
9+
.settings/
10+
.project
11+
12+
*.iml
13+
.idea/
14+
15+
.as3proj
16+
17+
.DS_Store
18+
Thumbs.db
19+
20+
.swf
21+
.swc
22+
.swz

ASCIIArt/AsciiArtApp.fla

931 KB
Binary file not shown.

ASCIIArt/AsciiArtApp.mxml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<mx:Application
3+
xmlns:mx="http://www.adobe.com/2006/mxml"
4+
xmlns="*"
5+
layout="vertical"
6+
paddingTop="0"
7+
paddingLeft="8"
8+
paddingRight="8"
9+
paddingBottom="0"
10+
creationComplete="initApp()">
11+
12+
<mx:Script>
13+
<![CDATA[
14+
import com.example.programmingas3.asciiArt.ImageInfo;
15+
import com.example.programmingas3.asciiArt.AsciiArtBuilder;
16+
17+
private var asciiArt:AsciiArtBuilder;
18+
19+
private function initApp():void
20+
{
21+
asciiArt = new AsciiArtBuilder();
22+
asciiArt.addEventListener("ready", imageReady);
23+
}
24+
25+
/**
26+
* Called when the AsciiArtBuilder has loaded the image data and is ready to
27+
* display an image.
28+
*/
29+
private function imageReady(event:Event):void
30+
{
31+
updatePreview();
32+
}
33+
34+
35+
/**
36+
* Called when the "next image" button is pressed.
37+
*/
38+
private function nextImage():void
39+
{
40+
// Advance to the next image
41+
asciiArt.next();
42+
// update the image preview
43+
updatePreview();
44+
}
45+
46+
47+
/**
48+
* Updates the image preview display, including title and image, using
49+
* the current image in the asciiArt object.
50+
*/
51+
private function updatePreview():void
52+
{
53+
var imageInfo:ImageInfo = asciiArt.currentImage.info;
54+
bmp.load(AsciiArtBuilder.IMAGE_PATH + imageInfo.fileName);
55+
sourceImage.title = imageInfo.title;
56+
asciiArtText.text = asciiArt.asciiArtText;
57+
}
58+
]]>
59+
</mx:Script>
60+
61+
<mx:Label id="title" text="ASCII Art Example" fontSize="24" fontStyle="bold" />
62+
<mx:Label id="subtitle" text="From Programming ActionScript 3.0, Chapter 6: Working with strings" fontSize="12" />
63+
64+
<mx:HBox width="100%" height="100%" horizontalAlign="center">
65+
66+
<mx:Panel id="sourceImage" backgroundColor="#b7babc" borderAlpha="1" borderColor="#666" borderStyle="solid" borderThickness="1">
67+
<mx:Image width="400" id="bmp" height="300"/>
68+
<mx:ControlBar>
69+
<mx:Spacer width="290" height="15"/>
70+
<mx:Button label="Next Image" click="nextImage()"/>
71+
</mx:ControlBar>
72+
</mx:Panel>
73+
74+
<mx:TextArea id="asciiArtText" width="510" height="450" fontFamily="_typewriter" fontSize="8"></mx:TextArea>
75+
76+
</mx:HBox>
77+
78+
</mx:Application>
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package com.example.programmingas3.asciiArt
2+
{
3+
import flash.events.Event;
4+
import flash.events.EventDispatcher;
5+
import flash.net.URLLoader;
6+
import flash.net.URLRequest;
7+
import com.example.programmingas3.asciiArt.BitmapToAsciiConverter;
8+
import com.example.programmingas3.asciiArt.Image;
9+
import com.example.programmingas3.asciiArt.ImageInfo;
10+
11+
/**
12+
* Provides appication-level functionality for the AsciiArt sample.
13+
*/
14+
public class AsciiArtBuilder extends EventDispatcher
15+
{
16+
// ------- Private vars -------
17+
18+
private const DATA_TARGET:String = "txt/ImageData.txt";
19+
private var _imageInfoLoader:URLLoader;
20+
private var _imageStack:Array;
21+
private var _currentImageIndex:uint;
22+
23+
24+
// ------- Constructor -------
25+
26+
public function AsciiArtBuilder()
27+
{
28+
_imageStack = new Array();
29+
var request:URLRequest = new URLRequest(DATA_TARGET);
30+
_imageInfoLoader = new URLLoader();
31+
_imageInfoLoader.addEventListener(Event.COMPLETE, imageInfoCompleteHandler);
32+
_imageInfoLoader.load(request);
33+
}
34+
35+
36+
// ------- Public Properties -------
37+
38+
public static const IMAGE_PATH:String = "image/";
39+
40+
public var asciiArtText:String = "";
41+
42+
public function get currentImage():Image
43+
{
44+
return _imageStack[_currentImageIndex];
45+
}
46+
47+
48+
// ------- Event Handlers -------
49+
50+
/**
51+
* Called when the image info text file is loaded.
52+
*/
53+
private function imageInfoCompleteHandler(event:Event):void
54+
{
55+
var allImageInfo:Array = parseImageInfo();
56+
57+
buildImageStack(allImageInfo);
58+
}
59+
60+
61+
/**
62+
* Called when the first image is loaded.
63+
*/
64+
private function imageCompleteHandler(event:Event):void
65+
{
66+
// move to the first image in the stack
67+
next();
68+
// notify any listeners that the application has finished its initial loading
69+
var readyEvent:Event = new Event("ready");
70+
dispatchEvent(readyEvent);
71+
}
72+
73+
74+
75+
// ------- Public Methods -------
76+
77+
/**
78+
* Advances the image stack to the next image, and populates the asciiArtText property
79+
* with that image's ASCII Art representation.
80+
*/
81+
public function next():void
82+
{
83+
// Advance the "current image" index (or set it back to 0 if we're on the last one)
84+
_currentImageIndex++;
85+
if (_currentImageIndex == _imageStack.length)
86+
{
87+
_currentImageIndex = 0;
88+
}
89+
// generate the ASCII version of the new "current" image
90+
var imageConverter:BitmapToAsciiConverter = new BitmapToAsciiConverter(this.currentImage);
91+
this.asciiArtText = imageConverter.parseBitmapData();
92+
}
93+
94+
95+
// ------- Private Methods -------
96+
97+
/**
98+
* Parses the contents of the loaded text file which contains information about the images
99+
* to load, and creates an Array of ImageInfo instances from that data.
100+
*
101+
* @return An Array of ImageInfo instances.
102+
*/
103+
private function parseImageInfo():Array
104+
{
105+
var result:Array = new Array();
106+
107+
/* Parse the contents of the text file, and put its contents into an Array of ImageInfo
108+
* instances.
109+
* Each line of text contains info about one image, separated by tab (\t) characters,
110+
* in this order:
111+
* - file name
112+
* - title
113+
* - white threshold
114+
* - black threshold
115+
*
116+
* Loop through the individual lines in the text file.
117+
* Note that we skip the first line, since it only contains column headers.
118+
*/
119+
var lines:Array = _imageInfoLoader.data.split("\n");
120+
var numLines:uint = lines.length;
121+
for (var i:uint = 1; i < numLines; i++)
122+
{
123+
var imageInfoRaw:String = lines[i];
124+
// trim leading or trailing white space from the current line
125+
imageInfoRaw = imageInfoRaw.replace(/^ *(.*) *$/, "$1");
126+
if (imageInfoRaw.length > 0)
127+
{
128+
// create a new image info record and add it to the array of image info
129+
var imageInfo:ImageInfo = new ImageInfo();
130+
// split the current line into values (separated by tab (\t) characters)
131+
// and extract the individual properties
132+
var imageProperties:Array = imageInfoRaw.split("\t");
133+
imageInfo.fileName = imageProperties[0];
134+
imageInfo.title = normalizeTitle(imageProperties[1]);
135+
imageInfo.whiteThreshold = parseInt(imageProperties[2], 16);
136+
imageInfo.blackThreshold = parseInt(imageProperties[3], 16);
137+
result.push(imageInfo);
138+
}
139+
}
140+
return result;
141+
}
142+
143+
144+
/**
145+
* Capitalizes the first letter of each word in a String, unless the word
146+
* is one of the English words which are not commonly capitalized
147+
*
148+
* @param str The String to "normalize"
149+
* @return The String with the words capitalized
150+
*/
151+
private function normalizeTitle(title:String):String
152+
{
153+
var words:Array = title.split(" ");
154+
var len:uint = words.length;
155+
156+
for(var i:uint; i < len; i++)
157+
{
158+
words[i] = capitalizeFirstLetter(words[i]);
159+
}
160+
161+
return words.join(" ");
162+
}
163+
164+
165+
166+
/**
167+
* Capitalizes the first letter of a single word, unless it's one of
168+
* a set of words which are normally not capitalized in English.
169+
*
170+
* @param word The word to capitalize
171+
* @return The capitalized word
172+
*/
173+
private function capitalizeFirstLetter(word:String):String
174+
{
175+
switch (word)
176+
{
177+
case "and":
178+
case "the":
179+
case "in":
180+
case "an":
181+
case "or":
182+
case "at":
183+
case "of":
184+
case "a":
185+
// don't do anything to these words
186+
break;
187+
default:
188+
// for any other word, capitalize the first character
189+
var firstLetter:String = word.substr(0, 1);
190+
firstLetter = firstLetter.toUpperCase();
191+
var otherLetters:String = word.substring(1);
192+
word = firstLetter + otherLetters;
193+
}
194+
195+
return word;
196+
}
197+
198+
199+
/**
200+
* Using an Array of ImageInfo instances, populates the image stack with Image instances.
201+
*
202+
* @param imageInfo An array of ImageInfo instances, containing the data about the
203+
* image files to be loaded into the image stack.
204+
*/
205+
private function buildImageStack(imageInfo:Array):void
206+
{
207+
var image:Image;
208+
var oneImageInfo:ImageInfo;
209+
var listenerAdded:Boolean = false;
210+
var numImages:uint = imageInfo.length;
211+
for (var i:uint = 0; i < numImages; i++)
212+
{
213+
_currentImageIndex = 0;
214+
oneImageInfo = imageInfo[i];
215+
image = new Image(oneImageInfo);
216+
_imageStack.push(image);
217+
if(!listenerAdded)
218+
{
219+
image.addEventListener(Event.COMPLETE, imageCompleteHandler);
220+
listenerAdded = true;
221+
}
222+
image.load();
223+
}
224+
}
225+
}
226+
}

0 commit comments

Comments
 (0)