topical media & game development
actionscript-graph-high-CharacterHandler.ax
actionscript-graph-high-CharacterHandler.ax
[swf]
flex
package ConceptGraphHigh {
import ConceptGraphHigh.Character;
import flash.display.*;
import flash.filters.*;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Transform;
import flash.geom.Matrix;
import flash.text.TextFormat;
import flash.ui.Mouse;
public class @ax-actionscript-graph-high-CharacterHandler extends Sprite{
private var characterHandler:Array = new Array() //Array with the updated index of the elements in arrayCharacter
private var allCharacters:Array = new Array; //Array with all the character objects contained in the XML File (charData)
private var arrayCharacter:Array = new Array; //Array containing the character Object (arrayCharacter.object) and the quality of the relation (arrayCharacter.relation)
private var arrayRelations:Array = new Array; //Contains relations among persons: fromID, toID, quality
private var subRelation:Array = new Array;
private var container:Sprite = new Sprite();
private var originX:uint = 400; //Set the X center (where the clicked character is set)
private var originY:uint = 365; //Set the Y center (where the clicked character is set)
private var affineTransform:Matrix;
private var distanceBetweenTurns:uint = 70;
private var radiusCircle:uint = 100;
private var radius:uint = 50;
private var numCharactersFirstTurn:uint = 15; //Minimum 3
private var increaseCharactersFactor:uint = 3;
private var selectedLabel:TextField;
private var descriptionLabel:TextField;
private var ns:NetStream;
private var textObjectArray:Array = new Array();
private var textObjectDisplayed:int = 0; //Parameter of the application
private var textToolTip:TextField;
public function @ax-actionscript-graph-high-CharacterHandler (charData:Array, arrayRelations:Array, selectedLabel:TextField, descriptionLabel:TextField, ns:NetStream, textToolTip:TextField):void {
this.arrayRelations = arrayRelations;
this.selectedLabel=selectedLabel;
this.descriptionLabel=descriptionLabel;
this.ns = ns;
this.textToolTip= textToolTip;
for (var j:int=0; j<charData.length; j++) { //Add all the characters to arrayCharacter and allCharacters
var character:Character = new Character(radius, charData[j].id, charData[j].imageURL, charData[j].nameChar, charData[j].description, charData[j].videoURL);
trace(charData[j].nameChar);
trace(charData[j].description);
allCharacters.push(character);
arrayCharacter.push(character);
addFigure(arrayCharacter[j], originX, originY, j); //Add all the figures of arrayCharacter to the container
}
addEventListener(Event.ENTER_FRAME, onEnterFrame); //Activate the movement
click(arrayCharacter[0]); //We click on the first item
}
public function addFigure(character:Character, x:int, y:int, index:int):void {
character.addEventListener(MouseEvent.MOUSE_UP, onPress); //Handle event when click on one character
character.addEventListener(MouseEvent.MOUSE_OVER, overCharacter); //Handle event when the mouse is over one character
character.addEventListener(MouseEvent.MOUSE_OUT, outCharacter); //Handle event when the mouse is out one character (previously over)
character.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveCharacter); //Handle event when the mouse is moving over one character
character.setX(x); //Set the initial coordinate X of the character
character.setY(y); //Set the initial coordinate Y of the character
characterHandler.push(index); //The index of the actual character is added to the characterHandler array
container.addChild(character); //Add the character to the container
}
public function getContainer() :Sprite {
return container;
}
private function onEnterFrame(event:Event):void {
container.graphics.clear();
//Lines to connect the character in the center with the rest of the characters
container.graphics.lineStyle(3);
for (var i:int = 1; i<arrayCharacter.length; i++) {
trace("Arraychar id: "+ arrayCharacter[i].getId());
//Set the textField with the relation in the middle, between the center character and the character with the relation
textObjectArray[i-1].x=arrayCharacter[0].getX() - ((textObjectArray[i-1].width)/2) + ((arrayCharacter[i].getX()-arrayCharacter[0].getX())/2);
textObjectArray[i-1].y=arrayCharacter[0].getY() - ((textObjectArray[i-1].height)/2) + ((arrayCharacter[i].getY()-arrayCharacter[0].getY())/2);
//Sets the edges with lines
container.graphics.moveTo(arrayCharacter[0].getX(),arrayCharacter[0].getY());
container.graphics.lineTo(arrayCharacter[i].getX(),arrayCharacter[i].getY());
}
//Curves to indicate the subrelations among outer characters
container.graphics.lineStyle(1);
for (var j:int = 0; j<subRelation.length; j++) {
var relation:Relation = subRelation[j];
container.graphics.moveTo(arrayCharacter[Number(relation.getFromID())].getX(),arrayCharacter[Number(relation.getFromID())].getY())
container.graphics.curveTo((arrayCharacter[Number(relation.getToID())].getX()/2)+160, (arrayCharacter[Number(relation.getToID())].getY()/2)+160, arrayCharacter[Number(relation.getToID())].getX(),arrayCharacter[Number(relation.getToID())].getY());
}
}
//Check the characters in tempArray with the existing in arrayCharacters and update it
//The array with the updated characters is arrayCharacter
private function checkExistence(tempArray:Array):void {
var isInTheList:Boolean = false;
for (var i:int = 0; i<arrayCharacter.length;i++) {
for (var j:int = 0; j< tempArray.length; j++) {
//If the character is in tempArray and in arrayCharcter we remove it from tempArray
if (arrayCharacter[i] == tempArray[j]) {
arrayCharacter[i] = tempArray[j];
tempArray.splice(j, 1);
j--;
isInTheList = true;
}
}
//If the character is only in arrayCharacter, and not in tempArray, we remove it from arrayCharacter
if (isInTheList==false) {
container.removeChild(arrayCharacter[characterHandler[i]]);
trace("Removemos " + arrayCharacter[characterHandler[i]].getId());
arrayCharacter.splice(i, 1);
i--;
}
isInTheList = false;
}
//If there is some character in tempArray and not in arrayCharacter we add them to arrayCharacter from the center (originX originY)
for (var k:int =0; k<tempArray.length; k++) {
tempArray[k].setX(originX);
tempArray[k].setY(originY);
trace("AƱadimos " + tempArray[k].getId());
arrayCharacter.push(tempArray[k]);
container.addChild(tempArray[k]);
}
//If there are more characters than textField created, we need to create more textFields and add them to the container
while ((arrayCharacter.length-1)>textObjectArray.length) {
var textRelationDescription:TextField = new TextField();
textRelationDescription.background=true;
textRelationDescription.border=true;
textRelationDescription.autoSize=TextFieldAutoSize.CENTER;
textObjectArray.push(textRelationDescription);
container.addChild(textObjectArray[textObjectArray.length-1]);
textObjectDisplayed++;
}
//While there are more characters in the screen than textFields visible, we need to add them to the container
while ((arrayCharacter.length-1)>textObjectDisplayed) {
container.addChild(textObjectArray[textObjectDisplayed]);
textObjectDisplayed++;
}
//Set as visible only the textFields with content (not empty spaces)
for (var m:int=1; m<arrayCharacter.length; m++){
textObjectArray[m-1].text = arrayCharacter[m].getRelation();
if (textObjectArray[m-1].text=="") {
textObjectArray[m-1].visible=false;
} else {
textObjectArray[m-1].visible=true;
}
}
//If there are more textFields displayed than characters, we need to remove them from the container
while (textObjectDisplayed>(arrayCharacter.length-1)) {
container.removeChild(textObjectArray[textObjectDisplayed-1]);
textObjectDisplayed--;
}
subRelation.splice(0, subRelation.length);
}
private function characterGeometry():void {
var existentRelation:Boolean = false;
var angle:Number = 0;
var characterOne:Character;
var indexTurns:int = 1;
var increaseSize:Number = 0 //The size of the circle, the number of turs
var currentTurn:int = 1; //The current number of turns
var constantDivisionNum:int;
var value:int;
arrayCharacter[0].setXWanted(originX);
arrayCharacter[0].setYWanted(originY);
for (indexTurns; (increaseSize + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, indexTurns)) < arrayCharacter.length; indexTurns++) {
increaseSize += numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, indexTurns)
}
//Check for subrelations among characters in arrayCharacters (characterOne and characterTwo)
for (var i:int = 1; i<arrayCharacter.length; i++) {
characterOne = arrayCharacter[i];
for (var j:int=0; j< arrayRelations.length; j++) {
var relation:Relation=arrayRelations[j];
for (var k:int = 1; k<arrayCharacter.length; k++) {
var characterTwo:Object = arrayCharacter[k];
if (relation.getFromID()==characterOne.getId() && relation.getToID() == characterTwo.getId()) {
var subrelation:Relation;
for (var l:int = 0; l<subRelation.length; l++) {
subrelation = subRelation[l];
if (int(subrelation.getFromID()) == i && int(subrelation.getToID()) == k) {
existentRelation=true;
}
}
if (existentRelation==false) { //If the subrelation is not already included, we do it
var newSubrelation:Relation = new Relation(String(k), String(i)); //Subrelations are stored with the index of the arrayCharacter
subRelation.push(newSubrelation); //Store the subrelation
}
existentRelation=false;
}
}
}
for (currentTurn; (i-1)>(value + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)) || (i-1)==(value + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)); currentTurn++) {
value += numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)
}
if ((i-1) < increaseSize) { //If we are over increaseSize we divide the angle among the constantDivisionNum
constantDivisionNum = numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn);
angle += (360/(constantDivisionNum))*Math.PI/180;
} else { //Otherwise, we divide it amongst the rest of the characters
angle += 360/(arrayCharacter.length-increaseSize-1)*Math.PI/180;
}
arrayCharacter[i].setXWanted(originX + (((radiusCircle + (currentTurn * distanceBetweenTurns))*Math.cos(angle)))); //Final destination of the character
arrayCharacter[i].setYWanted(originY + (((radiusCircle + (currentTurn * distanceBetweenTurns))*Math.sin(angle)))); //Final destination of the character
}
}
public function click(characterClicked:Character): void {
var tempArray:Array = new Array();
selectedLabel.htmlText = "<b>You have selected: </b>" + characterClicked.getName();
descriptionLabel.htmlText = "<b>Description: </b>" + characterClicked.getDescription();
trace ("esto"+characterClicked.getVideoURL()+"es");
if ((characterClicked.getVideoURL()==null) || (characterClicked.getVideoURL()== "")) {
ns.pause();
} else {
ns.play(characterClicked.getVideoURL());
}
for (var i:int = 0; i<arrayCharacter.length; i++) { //Look for the clicked element and we eliminate it from its position
if (arrayCharacter[i].getId() == characterClicked.getId()) {
arrayCharacter.splice(i, 1);
i--;
}
}
characterClicked.setRelationExplanation("");
arrayCharacter.splice(0,0, characterClicked); //Store the clicked element in the first position of the array arrayCharacter
tempArray.push(characterClicked); //Store the clicked element in the tempArray
for (i = 0; i<arrayRelations.length; i++) { //Check the relations with the other characters
var relation:Relation = arrayRelations[i];
if (relation.getFromID() == characterClicked.getId()) { //If the relation is between the clicked character...
var possibleCharacter:Character;
for (var j:int = 0; j < allCharacters.length; j++) { //Search in allCharacters the toID character and push it in tempArray
possibleCharacter = allCharacters[j];
trace ("relationGetToId: " + relation.getToID() + " possibleCharacter.getId()" + possibleCharacter.getId());
if (relation.getToID() == possibleCharacter.getId()) {
trace ("Char: " + possibleCharacter.getId() + " relation: " + relation.getRelation());
possibleCharacter.setRelation(relation.getRelation()); //Sets the relation string inside the character
possibleCharacter.setRelationExplanation(relation.getRelationExplanation()); //Sets the string of the explanation of the relation inside the character
tempArray.push(possibleCharacter);
}
}
}
}
//arrayCharacter => All the characters when the mouse was clicked
//tempArray => All the characters that are related to the clicked character
checkExistence(tempArray); //Update arrayCharacter with tempArray
characterGeometry(); //Set the characters (math and geometry)
}
public function overCharacter(ev:MouseEvent):void {
ev.target.filters=[new BevelFilter( )]; //Bevel Filter when the mouse is over the character
ev.target.increaseSize(60); //Increase the size
ev.target.setIndexDisplay(container.getChildIndex(Character(ev.target)));
container.addChildAt(Character(ev.target), container.numChildren); //Display the character in the front
}
private function mouseMoveCharacter(ev:MouseEvent):void {
toolTipIn(ev); //When the mouse is over the character the tooltiptext is on
}
private function toolTipIn (ev:MouseEvent):void {
textToolTip.visible=true; //The tooltiptext is visible now
textToolTip.x=mouseX+40; //The tooltiptext follows the mouse X
textToolTip.y=mouseY+40; //The tooltiptext follows the mouse Y
textToolTip.htmlText = "<b>Name: </b>" + ev.target.getName() + "\n<b>Relation: </b>" + ev.target.getRelationExplanation();
}
public function outCharacter(ev:MouseEvent):void {
ev.target.decreaseSize(); //Decrease size
ev.target.filters=[]; //Eliminate Bevel Filter
container.addChildAt(Character(ev.target), 1); //Place the character where it was situated
toolTipOut(); //When the mouse is out of the character the tooltiptext disappear
}
private function toolTipOut():void {
textToolTip.visible=false; //The tooltiptext is not visible now
}
public function onPress(ev:MouseEvent):void {
click(Character(ev.target));
}
}
}
(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.