View Single Post

  #1 (permalink)  
Old 29-05-08, 06:06
Flep Flep is offline
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Creare un output HTML con Flash CS3

Avete mai provato ad ottenere un output HTML da Flash ?

La cosa è davvero interessante.


Ho giocato un po con i metodi setTextformat e getTextFormat di Actionscript 3.0 e sono riuscito a creare una pagina HTML da Flash usando il PHP.


In pratica ho un campo di testo di tipo input, ho aggiunto un bottone bold e un componente ColorPicker.

Selezionando parte del testo si può assegnare un formato bold e cambiargli colore.

Infine, chiamando uno script PHP e passandogli l' output html del campo di testo, genero una pagina html con lo stesso formato.


Chiaramente questo è solo un esempio di quello che si potrebbe fare, apre migliaia di porte.

Creare un text editor, un editor WYSIWYNG.

Per qualsiasi applicazione che necessita un output html da Flash, questo è il primo passo da cui partire.








Selezionare parte del testo, assegnare il bold e scegliere un colore.

Cliccare send !


Creo un FLA che salvo con nome main.fla .

Al suo interno ho un campo di testo dinamico di tipo input con nome istanza field_txt econ un testo a caso.

Ho anche una MovieClip con nome istanza bold_mc ed un componente ColorPicker con nome istanza picker_mc.

Poi ho un bottone con nome istanza send_btn.

Infine ho 2 campi di testo che mi servono per incorporare i caratteri della font che uso ( in questo caso Arial ), uno normal e l' altro bold.

Per chi non sapesse di cosa sto parlando, meglio che si legga questo tutorial:

come incorporare le fonts in un SWF.


Adesso creo la Document Class, un file AS che salvo con nome Main.as, implementata in questo modo:


Code:
package
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.net.*;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			stage.focus=field_txt;
			
			addBoldListener();
			addColorPickerListener();
			addSendListener();
		}
		
		private function addBoldListener():void
		{
			bold_mc.mouseChildren=false;
			bold_mc.buttonMode=true;
			bold_mc.addEventListener(MouseEvent.MOUSE_DOWN,setBoldDown);
		}
		
		private function addColorPickerListener():void
		{
			picker_mc.addEventListener(Event.CHANGE,setColorChange);
		}
		
		private function addSendListener():void
		{
			send_btn.addEventListener(MouseEvent.MOUSE_DOWN,setSendDown);
		}
		
		private function setBoldDown(evt:MouseEvent):void
		{
			var f:TextFormat=field_txt.getTextFormat(field_txt.selectionBeginIndex,field_txt.selectionEndIndex);
			field_txt.setTextFormat(getBoldFormat(f,true),field_txt.selectionBeginIndex,field_txt.selectionEndIndex);
		}
		
		private function setSendDown(evt:MouseEvent):void
		{
			var variables:URLVariables=new URLVariables();
			variables.content=field_txt.htmlText;
			
			var request:URLRequest=new URLRequest("http://www.flepstudio.org/swf/PlayingWithSetFormat/myFormat.php");
			request.method=URLRequestMethod.POST;
			request.data=variables;
			
			var caller:URLLoader=new URLLoader();
			caller.addEventListener(Event.COMPLETE,htmlSent);
			caller.dataFormat=URLLoaderDataFormat.TEXT;
			try 
			{
				caller.load(request);
			} 
			catch (error:Error) 
			{
				trace('Unable to load requested document.');
			}
		}
		
		private function htmlSent(evt:Event):void
		{
			var caller:URLLoader=URLLoader(evt.target);
			var vars:URLVariables=new URLVariables(caller.data);
			var request:URLRequest=new URLRequest();
			request.url="http://www.flepstudio.org/swf/PlayingWithSetFormat/"+vars.answer;
			navigateToURL(request,"_blank");
		}
		
		private function setColorChange(evt:Event):void
		{
			var f:TextFormat=field_txt.getTextFormat(field_txt.selectionBeginIndex,field_txt.selectionEndIndex);
			field_txt.setTextFormat(getColorFormat(f,evt.target.selectedColor),field_txt.selectionBeginIndex,field_txt.selectionEndIndex);
		}
		
		private function getBoldFormat(fo:TextFormat,b:Boolean):TextFormat
		{
			fo.bold=b;
			return fo;
		}
		
		private function getColorFormat(fo:TextFormat,b:uint):TextFormat
		{
			fo.color=b;
			return fo;
		}
	}
}
Creo un PHP in questo modo:
PHP Code:
<?php
    header
("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    
header("Cache-Control: no-store, no-cache, must-revalidate");
    
header("Cache-Control: post-check=0, pre-check=0"false);
    
header("Pragma: no-cache");
    
    
$content=stripslashes($_POST["content"]);
    
    
$file_directory "files/"//the directory you want to store the new file in
    
    
$file_name time();//the file's name, stripped of any dangerous tags
    
    
$file $file_directory.$file_name.".html"//this is the entire filename
    
    
$create_file fopen($file"w+"); //create the new file
    
    
$chmod chmod($file0755); //set the appropriate permissions.
    
    
$header='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>'
.$file_name.'</title>
</head>
<body>'
;
                
    
$footer='
</body>
</html>'
;
    
    
fwrite($create_file$header."\n".$content.$footer);
    
    
//attempt to write basic content to the file
    
    
fclose($create_file);
    
    
$answer=$file;
    echo 
"answer=".$answer;
?>

Creo una cartella con nome files ( permessi 777 ) nella quale il PHP salverà i files html che crea in base all' output di Flash.


Analizziamo il codice Actionscript 3.0


Importo le classi che necessito

import flash.display.*;

import flash.events.*;

import flash.text.*;

import flash.net.*;


Dichiaro la classe

public class Main extends MovieClip


Dichiaro la funzione costruttrice

public function Main()

nella quale assegno un listener in ascolto dell' evento Event.ADDED_TO_STAGE che chiama una funzione con nome init. In questo modo, se lo necessitassimo potremmo tranquillamente caricare questo SWF dentro ad un altro.


Metodi

init

removeEventListener(Event.ADDED_TO_STAGE,init);

rimuovo il listener in ascolto dell' evento ADDED_TO_STAGE, dato che è già stato dispacciato.

stage.focus=field_txt;

imposto il focus sul campo di testo e chiamo 3 metodi:

addBoldListener();

addColorPickerListener();

addSendListener();


addBoldListener

in questo metodo assegno un listener alla MovieClip bold_mc in ascolto dell' evento MouseEvent.MOUSE_DOWN che chiamerà un metodo con nome setBoldDown

bold_mc.mouseChildren=false;

bold_mc.buttonMode=true;

bold_mc.addEventListener(MouseEvent.MOUSE_DOWN,set BoldDown);


addColorPickerListener

in questo metodo assegno un listener al ColorPicker ( picker_mc ) in ascolto dell' evento Event.CHANGE che chiamerà un metodo con nome setColorChange al momento in cui verrà scelto un colore

picker_mc.addEventListener(Event.CHANGE,setColorCh ange);


addSendListener

in questo metodo assegno un listener a send_btn in ascolto dell' evento MouseEvent.MOUSE_DOWN che chiamerà un metodo con nome setSendDown

send_btn.addEventListener(MouseEvent.MOUSE_DOWN,se tSendDown);


setBoldDown

in questo metodo assegno il bold al testo selezionato.

var f:TextFormat=field_txt.getTextFormat(field_txt.sel ectionBeginIndex,field_txt.selectionEndIndex);

field_txt.setTextFormat(getBoldFormat(f,true),fiel d_txt.selectionBeginIndex,field_txt.selectionEndIn dex);


setColorChange

in questo metodo assegno il colore al testo selezionato

var f:TextFormat=field_txt.getTextFormat(field_txt.sel ectionBeginIndex,field_txt.selectionEndIndex);

field_txt.setTextFormat(getColorFormat(f,evt.targe t.selectedColor),field_txt.selectionBeginIndex,fie ld_txt.selectionEndIndex);


setSendDown

in questo metodo chiamo lo script PHP passandogli il valore della proprietà htmlText del campo field_txt

var variables:URLVariables=new URLVariables();

variables.content=field_txt.htmlText;

var request:URLRequest=new URLRequest("http://www.flepstudio.org/swf/PlayingWithSetFormat/myFormat.php");

request.method=URLRequestMethod.POST;

request.data=variables;

var caller:URLLoader=new URLLoader();

caller.addEventListener(Event.COMPLETE,htmlSent);

caller.dataFormat=URLLoaderDataFormat.TEXT;

try

{

caller.load(request);

}

catch (error:Error)

{

trace('Unable to load requested document.');

}


htmlSent

questo è il metodo che viene eseguito quando il PHP mi restituisce l' echo col nome del file che ha creato.

var caller:URLLoader=URLLoader(evt.target);

var vars:URLVariables=new URLVariables(caller.data);

var request:URLRequest=new URLRequest();

request.url="http://www.flepstudio.org/swf/PlayingWithSetFormat/"+vars.answer;

navigateToURL(request,"_blank");


getBoldFormat

questo metodo cambia il bold al TextFormat del testo selezionato

fo.bold=b;

return fo;


getColorFormat

questo metodo cambia il colore al TextFormat del testo selezionato

fo.color=b;

return fo;


Alla prossima !


 

Attached Files
File Type: zip PlayingWithTextFormat.zip (626.6 KB, 56 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Reply With Quote