学习python的x-plane插件编程--PythonInterface的helloworld程序

支 持 本 站: 捐赠服务器等运维费用,需要您的支持!

我不是一个程序员,也不懂python开发语言,
纯粹出于兴趣看了看这方面的资料,写个简单的入门资料,
希望能够抛砖引玉,期待今后不断有更多优秀的python 插件出现。

资源:
1. 本家的开发说明,作为入门很好。本文根据这篇作成。
http://www.xpluginsdk.org/downloads/Using%20Python%20with%20the%20PythonInterface%20Plugin.pdf

2. 本家提供的样例程序,一切还都是看代码吧,这是最好的学习方法。http://www.xpluginsdk.org/downloads/PythonScripts.zip

----------------------------------------------------

首先,你应该安装了PythonInterface吧,如果不知道的话,
这个下载页面里有最新的版本,你一定要下载适合你机器的Python版本的PythonInterface.zip,
并安装到plugins目录下。PythonInterface对应的插件都放在PythonScripts目录下面。

源程序文件的扩展名应为.py,比如"helloworld.py",
文件放到/Resources/plugins/PythonScipts目录下面。


支 持 本 站: 捐赠服务器等运维费用,需要您的支持!

一个最基本的程序框架如下,


class PythonInterface:
def XPluginStart(self):
self.Name = "Template"
self.Sig = "SandyBarbour.Python.Template"
self.Desc = "A test script for the Python Interface."
return self.Name, self.Sig, self.Desc

def XPluginStop(self):
pass

def XPluginEnable(self):
return 1

def XPluginDisable(self):
pass

def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
pass


也就是要定义一个PythonInterface类,
由XPluginStart方法来启动该插件,由XPluginStop来关闭该插件,
由XPluginEnable和XPluginDisable来管理enable和disable该插件,
由XPluginReceiveMessage来接受系统发来的消息并处理。
pass的意思好像是什么都不做。

下面是可以在画面上显示窗口,显示框和文字所需要的函数,
from XPLMDisplay import *
XPLMCreateWindow() -- 生成窗口
XPLMDestroyWindow() -- 删除窗口
XPLMGetWindowGeometry() -- 得到窗口的位置信息

from XPLMGraphics import *
XPLMDrawTranslucentDarkBox() -- 显示一个半透明的灰色显示框
XPLMDrawString() -- 显示字符串


生成窗口的例子如下,一般放到XPluginStart中,
self.WindowId = XPLMCreateWindow(self, 50, 600, 300, 400, 1, self.DrawWindowCB, self.KeyCB, self.MouseClickCB, 0)
并在XPluginStop中删除这个窗口,self.WindowId 为窗口id,
XPLMDestroyWindow(self, self.WindowId)。
self.DrawWindowCB, self.KeyCB, self.MouseClickCB分别指向窗口,键盘和鼠标的回调函数。

描画窗口需要定义函数DrawWindowCallback,
def DrawWindowCallback(self, inWindowID, inRefcon):
pass
可以在这个函数中使用
XPLMGetWindowGeometry(inWindowID, lLeft, lTop, lRight, lBottom)
得到窗口的坐标;
使用
gResult = XPLMDrawTranslucentDarkBox(left, top, right, bottom)
在指定的位置上显示半透明显示框;
还可以使用指定的颜色,位置和字体
gResult = XPLMDrawString(colour, left + 5, top - 20, Desc, 0, xplmFont_Basic)
来显示文本。

接受鼠标操作使用需要定义MouseClickCallback回调函数,
def MouseClickCallback(self, inWindowID, x, y, inMouse, inRefcon):
pass
鼠标是否按下可以通过inMouse的值来判断,如下所示:
if ((inMouse == xplm_MouseDown) or (inMouse == xplm_MouseUp)):
self.Clicked = 1 - self.Clicked

类似的,键盘操作需要KeyCallback函数,
def KeyCallback(self, inWindowID, inKey, inFlags, inVirtualKey, inRefcon, losingFocus):
pass

最基本的函数就是这些了,让我们看一下这个helloworld的程序,
它在画面上显示一个框,当按下鼠标是显示"I'm a plugin 1"文字,
送开鼠标后会显示"Hello World1"文字。

----------------------------------------------------------------------

from XPLMDisplay import *
from XPLMGraphics import *

class PythonInterface:
defXPluginStart(self):
self.Name = "Template"
self.Sig = "SandyBarbour.Python.Template"
self.Desc = "A test script for the Python Interface."
self.Clicked = 0
self.DrawWindowCB = self.DrawWindowCallback
self.KeyCB = self.KeyCallback
self.MouseClickCB = self.MouseClickCallback
self.WindowId = XPLMCreateWindow(self, 50, 600, 300, 400, 1, self.DrawWindowCB, self.KeyCB, self.MouseClickCB, 0)
return self.Name, self.Sig, self.Desc

defXPluginStop(self):
XPLMDestroyWindow(self, self.WindowId)
pass

defXPluginEnable(self):
return1

def XPluginDisable(self):
pass

def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
pass

def DrawWindowCallback(self, inWindowID, inRefcon):
lLeft = []; lTop = []; lRight = []; lBottom = []

XPLMGetWindowGeometry(inWindowID, lLeft, lTop, lRight, lBottom)
left = int(lLeft[0]); top = int(lTop[0]); right = int(lRight[0]); bottom = int(lBottom[0])

gResult = XPLMDrawTranslucentDarkBox(left, top, right, bottom)
colour = 1.0, 1.0, 1.0

if self.Clicked:
Desc = "I'm a plugin 1"
else:
Desc = "Hello World1"

gResult = XPLMDrawString(colour, left + 5, top - 20, Desc, 0, xplmFont_Basic)
pass

def KeyCallback(self, inWindowID, inKey, inFlags, inVirtualKey, inRefcon, losingFocus):
pass

def MouseClickCallback(self, inWindowID, x, y, inMouse, inRefcon):
if ((inMouse == xplm_MouseDown) or (inMouse == xplm_MouseUp)):
self.Clicked = 1- self.Clicked
return1


----------------------------------------------------------------------

支 持 本 站: 捐赠服务器等运维费用,需要您的支持!