[转载]Use External Interface of Flash 8 in Python and C# Applications Open Source Flash

[转载]Use External Interface of Flash 8 in Python and C# Applications Open Source Flash.

A discussion about the External Interface of Flash8 has been post in this thread. And here we demonstrate how to use External Interface of Flash8 in Python and C# applications, we talk about how to serialize/deserialize the data passed through External Interface from/to Python and C#, and how to wrap these things up.

Data Structure in Xml

Data exchange through the External Interface is in a Xml-Rpc alike structure, so we need to write something serializer/deserializer to handle it, in this case, I use a separate class to handle this in Python and C#. For the detail, you should check the source code by yourself.

And the Wrapper

For easy use, We need a wrapper class around the Flash ax control, which help us registering callback functions for Flash side and calling Flash side functions. Check the source.

The Echo Sample

We supply a echo sample in both Python and C# files, which demonstrate the usage of the wrapper and the data conversion between Flash and the host.

JSCRIPT.NET:

/* 
* Jarrad Hope - May 2006 player.js 
* Enjoy! 
* jsc.exe /target:winexe player.js 
*/ 

import System; 
import System.Windows.Forms; 
import System.ComponentModel; 
import System.Drawing; 
import System.IO; 
import AxShockwaveFlashObjects; 

public class swfWindow extends Form {    
   private var mrSwf : AxShockwaveFlashObjects.AxShockwaveFlash; 

   function swfWindow() { 
   var startupSize = new System.Drawing.Size(800, 600); 
   // Swf Setup 
   this.mrSwf = new AxShockwaveFlash(); 
   this.mrSwf.BeginInit(); 
   this.mrSwf.Location = new Point(0,0); 
   this.mrSwf.Name = "mrSwf"; 
   this.mrSwf.TabIndex = 0; 
   this.Controls.Add(this.mrSwf); 
   this.mrSwf.Size = startupSize; 
   this.mrSwf.EndInit() 
   // Window Setup 
   this.mrSwf.Movie=Path.Combine(Directory.GetCurrentDirectory(),"test.swf"); 
   this.FormBorderStyle = 1; //Fixed Size 
   this.ClientSize = startupSize; 

   //ExternalInterface EXE->SWF 
   var recCall = this.mrSwf.CallFunction("<invoke name=\"callSwf\" returntype=\"xml\"><arguments><string>Ping!</string></arguments></invoke>"); 
   MessageBox.Show(recCall); 

   } 

} 

var window : swfWindow = new swfWindow(); 
window.ShowDialog();

Python:

class TestEIFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,
                          title='Test External Interface Between Python And Flash 8 - Echo',
                          size=(408,270))
        self.p=wx.Panel(self,-1)
        self.t=wx.TextCtrl(self.p,-1,pos=(200,0),size=(200,200),value='External Interface Echo',
                           style=wx.TE_MULTILINE)
        self.b=wx.Button(self.p,-1,pos=(200,205),size=(200,30),label='Press to Call Flash!')
        wx.EVT_BUTTON(self.b,self.b.GetId(),self.OnButton)
        self.asf=AxShockwaveFlashEx(self.p,-1,size=(200,240))
        self.asf.Scale='noScale'
        self.asf.Menu=False
        self.asf.RegisterCallback('echo',self.echo)
        self.asf.Movie=os.path.join(os.getcwd(),'eiecho.swf')
        return
    def echo(self,data):
        self.t.SetValue('Argument Received from Flash:\n\n')
        self.t.AppendText(str(data))
        return data
    def OnButton(self,evt):
        data={'a':705,
              'b':random.random(),
              'c':u'Hello World \u6587\u5b57 - Python',
              'd':[3,9,27],
              'e':None,
              'f':{'x':2.0,'y':4.5},
              'g':"",
              }
        self.t.SetValue('Argument Sended to Flash:\n\n')
        self.t.AppendText(str(data))
        data=self.asf.FlashSide.echo(data)
        self.t.AppendText('\n\nReturn Value from Flash:\n\n')
        self.t.AppendText(str(data))
        return

C#:

public class MainForm : System.Windows.Forms.Form
{
	private System.Windows.Forms.Button button1;
	private System.Windows.Forms.TextBox textBox1;
	private EIFlash.AxShockwaveFlashEx axShockwaveFlashEx1;
	public MainForm()
	{
		InitializeComponent();
		axShockwaveFlashEx1.Movie=System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(),"eiecho.swf");
		axShockwaveFlashEx1.RegisterCallback("echo",new DgEcho(Echo));
	}

	[STAThread]
	public static void Main(string[] args)
	{
		Application.Run(new MainForm());
	}

	#region Windows Forms Designer generated code
	private void InitializeComponent() {
		...
	}
	#endregion
	void InvokeFlashEcho(object sender, System.EventArgs e)
	{
		Hashtable echodata=new Hashtable();
		echodata.Add("a",100);
		System.Random r=new System.Random();
		echodata.Add("b",r.NextDouble());
		echodata.Add("c","Hello World 文字 - C#");
		echodata.Add("d",new int[]{2,4,8});
		echodata.Add("e",null);
		echodata.Add("f",false);
		Hashtable g=new Hashtable();
		g.Add("x",2.5);
		g.Add("y",7.525);
		echodata.Add("g",g);
		textBox1.Text="Argument Sended to Flash:\r\n\r\n";
		textBox1.Text+=Repr(echodata);
		object o=axShockwaveFlashEx1.CallFlashEx("echo",echodata);
		textBox1.Text+="\r\n\r\nReturn Value from Flash:\r\n\r\n";
		textBox1.Text+=Repr(o);
	}

	string Repr(object o)
	{
		if(o==null){
			return "<null>";
		}
		Type t=o.GetType();
		if(t==typeof(bool)){
			return "<bool:"+o.ToString()+">";
		}
		if(o is IList){
			IList a=(IList)o;
			string[] ss=new string[a.Count];
			for(int i=0;i<a.Count;i++){
				ss[i]=Repr(a[i]);
			}
			return "<"+t+"["+String.Join(", ",ss)+"]>";
		}
		if(o is IDictionary){
			IDictionary d=(IDictionary)o;
			string[] ss=new string[d.Count];
			int i=0;
			foreach(object k in d.Keys){
				ss[i++]=k.ToString()+": "+Repr(d[k]);
			}
			return "<"+t+"{"+String.Join(", ",ss)+"}>";
		}
		return o.ToString();
	}

	object Echo(object data)
	{
		textBox1.Text="Argument Received from Flash:\r\n\r\n"+Repr(data);
		return data;
	}
	delegate object DgEcho(object data);
}

Files Associated to this Tutorial

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏