#!BPY

"""
Name: 'MDX (.mdx)'
Blender: 239
Group: 'Import'
Tooltip: 'Import from Kingpin file format (.mdx).'
"""

__author__ = 'FREDZ'
__version__ = '0.1'
__url__ = ["Kingpin.info, http://kingpin.info"]
__bpydoc__ = """\
This script imports a Kingpin file (MDX), textures, 
and animations into blender for editing.  Loader is based on MDX loader and Quark MDX importer.
"""

# ***** BEGIN GPL LICENSE BLOCK *****
#
# Script copyright (C) FREDZ
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------


import Blender
from Blender import Mesh, Object, sys
from Blender.BGL import *
from Blender.Draw import *
from Blender.Window import *
from Blender.Mathutils import Vector
import struct
from types import *


######################################################
# Main Body
######################################################

#returns the string from a null terminated string
def asciiz (s):
  n = 0
  while (ord(s[n]) != 0):
    n = n + 1
  return s[0:n]


######################################################
# MDX Model Constants
######################################################
MDX_MAX_TRIANGLES=4096
MDX_MAX_VERTICES=2048
#MDX_MAX_TEXCOORDS=2048
MDX_MAX_FRAMES=1024
MDX_MAX_SKINS=32
MDX_MAX_FRAMESIZE=(MDX_MAX_VERTICES * 4 + 128)

######################################################
# MDX data structures
######################################################
class mdx_alias_triangle(object):
	__slots__ = 'vertices', 'lightnormalindex'
	binary_format="<3BB" #little-endian (<), 3 Unsigned char
	
	def __init__(self):
		self.vertices=[0]*3
		self.lightnormalindex=0

	def load(self, file):
		temp_data = file.read(struct.calcsize(self.binary_format))
		data = struct.unpack(self.binary_format, temp_data)
		self.vertices[0]=data[0]
		self.vertices[1]=data[1]
		self.vertices[2]=data[2]
		self.lightnormalindex=data[3]
		return self

	def dump(self):
		print "MDX Alias_Triangle Structure"
		print "vertex: ", self.vertices[0]
		print "vertex: ", self.vertices[1]
		print "vertex: ", self.vertices[2]
		print "lightnormalindex: ",self.lightnormalindex
		print ""

class mdx_face(object):
	
	binary_format="<3h3h" #little-endian (<), 3 short, 3 short
	
	__slots__ = 'vertex_index', 'texture_index'
	
	def __init__(self):
		self.vertex_index = [ 0, 0, 0 ]
		self.texture_index = [ 0, 0, 0]

	def load (self, file):
		temp_data=file.read(struct.calcsize(self.binary_format))
		data=struct.unpack(self.binary_format, temp_data)
		self.vertex_index[0]=data[0]
		self.vertex_index[1]=data[1]
		self.vertex_index[2]=data[2]
		self.texture_index[0]=data[3]
		self.texture_index[1]=data[4]
		self.texture_index[2]=data[5]
		return self

	def dump (self):
		print "MDX Face Structure"
		print "vertex index: ", self.vertex_index[0]
		print "vertex index: ", self.vertex_index[1]
		print "vertex index: ", self.vertex_index[2]
		print "texture index: ", self.texture_index[0]
		print "texture index: ", self.texture_index[1]
		print "texture index: ", self.texture_index[2]
		print ""
		
class glGLCommands_t(object):
#	__slots__ = 'TrisTypeNum', 'SubObjectID'
    binary_format="<2i" #little-endian (<), 2 ints

    def __init__(self):
        self.TrisTypeNum=None
        self.SubObjectID=None

    def load (self, file):
        temp_data=file.read(struct.calcsize(self.binary_format))
        data=struct.unpack(self.binary_format, temp_data)
        self.TrisTypeNum=data[0]
        self.SubObjectID=data[1]
        return self

    def dump (self):
        print "MDX GL Command Structure"
        print "TrisTypeNum: ",self.TrisTypeNum
        print "SubObjectID: ",self.SubObjectID
        print ""

class glCommandVertex_t(object):
	__slots__ = 'u', 'v', 'vertexIndex'
	binary_format="<2fi" #little-endian (<), 2 floats + 1 int
	
	def __init__(self):
		self.u=0.0
		self.v=0.0
		self.vertexIndex=0

	def load (self, file):
		temp_data=file.read(struct.calcsize(self.binary_format))
		data=struct.unpack(self.binary_format, temp_data)
		self.u=data[0]
		self.v=data[1]
		self.vertexIndex=data[2]
		return self

	def dump (self):
		print "MDX GL Command Vertex"
		print "texture coordinate u: ",self.u
		print "texture coordinate v: ",self.v
		print "vertexIndex: ",self.vertexIndex
		print ""
		
class BBox_t(object):
#	__slots__ = 'min', 'max'
    binary_format="<6f" #little-endian (<), 6 floats

    def __init__(self):
        self.min=(0.,0.,0.)
        self.max=(0.,0.,0.)

    def load (self, file):
        temp_data=file.read(struct.calcsize(self.binary_format))
        data=struct.unpack(self.binary_format, temp_data)
        self.min=(data[0], data[1], data[2])
        self.max=(data[3], data[4], data[5])
        return self

    def dump (self):
        print "MDX BBox Frames Structure"
        print "min: ",self.min
        print "max: ",self.max
        print ""
		
class mdx_skin(object):
	__slots__ = 'name'
	binary_format="<64s" #little-endian (<), char[64]

	def __init__(self):
		self.name=""

	def load (self, file):
		temp_data=file.read(struct.calcsize(self.binary_format))
		data=struct.unpack(self.binary_format, temp_data)
		self.name=asciiz(data[0])
		return self

	def dump (self):
		print "MDX Skin"
		print "skin name: ",self.name
		print ""

class mdx_alias_frame(object):
	__slots__ = 'scale', 'translate', 'name', 'vertices'
	binary_format="<3f3f16s" #little-endian (<), 3 float, 3 float char[16]
	#did not add the "3bb" to the end of the binary format
	#because the alias_vertices will be read in through
	#thier own loader

	def __init__(self):
		self.scale=[0.0]*3
		self.translate=[0.0]*3
		self.name=""
		self.vertices=[]


	def load (self, file):
		temp_data=file.read(struct.calcsize(self.binary_format))
		data=struct.unpack(self.binary_format, temp_data)
		self.scale[0]=data[0]
		self.scale[1]=data[1]
		self.scale[2]=data[2]
		self.translate[0]=data[3]
		self.translate[1]=data[4]
		self.translate[2]=data[5]
		self.name=asciiz(data[6])
		return self

	def dump (self):
		print "MDX Alias Frame"
		print "scale x: ",self.scale[0]
		print "scale y: ",self.scale[1]
		print "scale z: ",self.scale[2]
		print "translate x: ",self.translate[0]
		print "translate y: ",self.translate[1]
		print "translate z: ",self.translate[2]
		print "name: ",self.name
		print ""

class mdx_obj(object):
	__slots__ =\
	'faces', 'frames',\
	'skins', 'ident', 'version',\
	'skin_width', 'skin_height',\
	'frame_size', 'num_skins', 'num_vertices',\
	'num_faces', 'num_GL_commands',\
	'num_SfxDefines', 'num_SfxEntries', 'num_SubObjects',\
	'num_frames', 'offset_skins',\
	'offset_faces', 'offset_frames', 'offset_GL_commands',\
	'offset_VertexInfo', 'offset_SfxDefines', 'offset_SfxEntries',\
	'offset_BBoxFrames', 'offset_DummyEnd'
	
	'''
	#Header Structure
    ident=0              #int  0   This is used to identify the file
    version=0            #int  1   The version number of the file (Must be 8)
    skin_width=0         #int  2   The skin width in pixels
    skin_height=0        #int  3   The skin height in pixels
    frame_size=0         #int  4   The size in bytes the frames are
    num_skins=0          #int  5   The number of skins associated with the model
    num_vertices=0       #int  6   The number of vertices (constant for each frame)
    num_faces=0          #int  7   The number of faces, triangles (polygons)
    num_GL_commands=0    #int  8   The number of gl commands
    num_frames=0         #int  9   The number of animation frames
    num_SfxDefines=0     #int 10   The number of sfx definitions
    num_SfxEntries=0     #int 11   The number of sfx entries
    num_SubObjects=0     #int 12   The number of subobjects in mdx file
    offset_skins=0       #int 13   The offset in the file for the skin data
    offset_faces=0       #int 14   The offset in the file for the face data
    offset_frames=0      #int 15   The offset in the file for the frames data
    offset_GL_commands=0 #int 16   The offset in the file for the gl commands data
    offset_VertexInfo=0  #int 17   The offset in the file for the vertex info data
    offset_SfxDefines=0  #int 18   The offset in the file for the sfx definitions data
    offset_SfxEntries=0  #int 19   The offset in the file for the sfx entries data
    offset_BBoxFrames=0  #int 20   The offset in the file for the bbox frames data
    offset_DummyEnd=0    #int 21   Same as offset_end below
    offset_end=0         #int 22   The end of the file offset
	'''
	binary_format="<23i"  #little-endian (<), 17 integers (17i)

	#mdx data objects

	def __init__ (self):
		self.faces=[]
		self.frames=[]
		self.skins=[]


	def load (self, file):
		temp_data = file.read(struct.calcsize(self.binary_format))
		data = struct.unpack(self.binary_format, temp_data)

		self.ident=data[0]
		self.version=data[1]

		if (self.ident!=1481655369 or self.version!=4):
			print "Not a valid MDX file"
			Exit()

		self.skin_width=data[2]
		self.skin_height=data[3]
		self.frame_size=data[4]

		#make the # of skin objects for model
		self.num_skins=data[5]
		for i in xrange(0,self.num_skins):
			self.skins.append(mdx_skin())

		self.num_vertices=data[6]

		#make the # of texture coordinates for model
#		self.num_tex_coords=data[7]
#		for i in xrange(0,self.num_tex_coords):
#			self.tex_coords.append(mdx_tex_coord())

		#make the # of triangle faces for model
		self.num_faces=data[7]
		for i in xrange(0,self.num_faces):
			self.faces.append(mdx_face())

		self.num_GL_commands=data[8]

		#make the # of frames for the model
		self.num_frames=data[9]
		for i in xrange(0,self.num_frames):
			self.frames.append(mdx_alias_frame())
			#make the # of vertices for each frame
			for j in xrange(0,self.num_vertices):
				self.frames[i].vertices.append(mdx_alias_triangle())

		self.num_SfxDefines=data[10]
		self.num_SfxEntries=data[11]
		self.num_SubObjects=data[12]
		
		self.offset_skins=data[13]
#		self.offset_tex_coords=data[12]
		self.offset_faces=data[14]
		self.offset_frames=data[15]
		self.offset_GL_commands=data[16]
		
		self.offset_VertexInfo=data[17]
		self.offset_SfxDefines=data[18]
		self.offset_SfxEntries=data[19]
		self.offset_BBoxFrames=data[20]
		self.offset_DummyEnd=data[21]
		
		#load the skin info
		file.seek(self.offset_skins,0)
		for i in xrange(0, self.num_skins):
			self.skins[i].load(file)
			#self.skins[i].dump()

		#load the texture coordinates
#		file.seek(self.offset_tex_coords,0)
#		for i in xrange(0, self.num_tex_coords):
#			self.tex_coords[i].load(file)
			#self.tex_coords[i].dump()

		#load the face info
		file.seek(self.offset_faces,0)
		for i in xrange(0, self.num_faces):
			self.faces[i].load(file)
			#self.faces[i].dump()

		#load the frames
		file.seek(self.offset_frames,0)
		for i in xrange(0, self.num_frames):
			self.frames[i].load(file)
			#self.frames[i].dump()
			for j in xrange(0,self.num_vertices):
				self.frames[i].vertices[j].load(file)
				#self.frames[i].vertices[j].dump()
		return self

	def dump (self):
		print "Header Information"
		print "ident: ", self.ident
		print "version: ", self.version
		print "skin width: ", self.skin_width
		print "skin height: ", self.skin_height
		print "frame size: ", self.frame_size
		print "number of skins: ", self.num_skins
		print "number of faces: ", self.num_faces
		print "number of GL commands: ", self.num_GL_commands
		print "number of frames: ", self.num_frames
		print "number of SfxDefines: ", self.num_SfxDefines
		print "number of SfxEntries: ", self.num_SfxEntries
		print "number of SubObjects: ", self.num_SubObjects
		print "number of vertices: ", self.num_vertices
		print "offset skins: ", self.offset_skins
		print "offset faces: ", self.offset_faces
		print "offset frames: ", self.offset_frames
		print "offset GL Commands: ", self.offset_GL_commands
		print "offset VertexInfo: ", self.offset_VertexInfo
		print "offset SfxDefines: ", self.offset_SfxDefines
		print "offset SfxEntries: ", self.offset_SfxEntries
		print "offset BBoxFrames: ", self.offset_BBoxFrames
		print "offset DummyEnd: ", self.offset_DummyEnd
		print ""

######################################################
# Import functions
######################################################
# Create the bboxes (hit boxes).
def MakePoly(compname, bbox):
    m = bbox[0]
    M = bbox[1]
    shortname = compname.split(":")[0]
    p = quarkx.newobj(shortname + ":p");
    p["assigned2"] = compname
    p['show'] = (1.0,)
    face = quarkx.newobj("north:f") # BACK FACE
    vtx0X, vtx0Y, vtx0Z = m[0],M[1],M[2]
    vtx1X, vtx1Y, vtx1Z = M[0],M[1],M[2]
    vtx2X, vtx2Y, vtx2Z = m[0],M[1],m[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)
    face = quarkx.newobj("east:f") # RIGHT FACE
    vtx0X, vtx0Y, vtx0Z = M[0],M[1],M[2]
    vtx1X, vtx1Y, vtx1Z = M[0],m[1],M[2]
    vtx2X, vtx2Y, vtx2Z = M[0],M[1],m[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)
    face = quarkx.newobj("south:f") # FRONT FACE
    vtx0X, vtx0Y, vtx0Z = M[0],m[1],M[2]
    vtx1X, vtx1Y, vtx1Z = m[0],m[1],M[2]
    vtx2X, vtx2Y, vtx2Z = M[0],m[1],m[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)
    face = quarkx.newobj("west:f") # LEFT FACE
    vtx0X, vtx0Y, vtx0Z = m[0],m[1],M[2]
    vtx1X, vtx1Y, vtx1Z = m[0],M[1],M[2]
    vtx2X, vtx2Y, vtx2Z = m[0],m[1],m[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)
    face = quarkx.newobj("up:f") # TOP FACE
    vtx0X, vtx0Y, vtx0Z = m[0],M[1],M[2]
    vtx1X, vtx1Y, vtx1Z = m[0],m[1],M[2]
    vtx2X, vtx2Y, vtx2Z = M[0],M[1],M[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)
    face = quarkx.newobj("down:f") # BOTTOM FACE
    vtx0X, vtx0Y, vtx0Z = m[0],M[1],m[2]
    vtx1X, vtx1Y, vtx1Z = M[0],M[1],m[2]
    vtx2X, vtx2Y, vtx2Z = m[0],m[1],m[2]
    face["v"] = (vtx0X, vtx0Y, vtx0Z, vtx1X, vtx1Y, vtx1Z, vtx2X, vtx2Y, vtx2Z)
    face["tex"] = None
    p.appenditem(face)

    return p

def load_textures(mdx, texture_filename):
	#did the user specify a texture they wanted to use?
	if texture_filename:
		if (Blender.sys.exists(texture_filename)):
			try:	return Blender.Image.Load(texture_filename)
			except:	return -1 # could not load?
			
	#does the model have textures specified with it?
	if int(mdx.num_skins) > 0:
		for i in xrange(0,mdx.num_skins):
			#mdx.skins[i].dump()
			if (Blender.sys.exists(mdx.skins[i].name)):
				try:	return Blender.Image.Load(mdx.skins[i].name)
				except:	return -1
	

def animate_mdx(mdx, mesh):
	######### Animate the verts through keyframe animation
	
	# Fast access to the meshes vertex coords
	verts = [v.co for v in mesh.verts] 
	scale = g_scale.val
	
	for i in xrange(1, mdx.num_frames):
		frame = mdx.frames[i]
		#update the vertices
		for j in xrange(mdx.num_vertices):
			x=(frame.scale[0] * frame.vertices[j].vertices[0] + frame.translate[0]) * scale
			y=(frame.scale[1] * frame.vertices[j].vertices[1] + frame.translate[1]) * scale
			z=(frame.scale[2] * frame.vertices[j].vertices[2] + frame.translate[2]) * scale
			
			#put the vertex in the right spot
			verts[j][:] = y,-x,z
			
		mesh.insertKey(i,"absolute")
		# mesh.insertKey(i)
		
		#not really necissary, but I like playing with the frame counter
		Blender.Set("curframe", i)
	
	
	# Make the keys animate in the 3d view.
	key = mesh.key
	key.relative = False
	
	# Add an IPO to teh Key
	ipo = Blender.Ipo.New('Key', 'mdx')
	key.ipo = ipo
	# Add a curve to the IPO
	curve = ipo.addCurve('Basis')
	
	# Add 2 points to cycle through the frames.
	curve.append((1, 0))
	curve.append((mdx.num_frames, (mdx.num_frames-1)/10.0))
	curve.interpolation = Blender.IpoCurve.InterpTypes.LINEAR
	


def load_mdx(mdx_filename, texture_filename):
	#read the file in
	file=open(mdx_filename,"rb")
	WaitCursor(1)
	DrawProgressBar(0.0, 'Loading MDX')
	mdx=mdx_obj()
	mdx.load(file)
	#mdx.dump()
	file.close()

	######### Creates a new mesh
	mesh = Mesh.New()

	uv_coord=[]
	#uv_list=[]
	verts_extend = []
	#load the textures to use later
	#-1 if there is no texture to load
	mesh_image=load_textures(mdx, texture_filename)
	if mesh_image == -1 and texture_filename:
		print 'MDX Import, Warning, texture "%s" could not load'

	######### Make the verts
	DrawProgressBar(0.25,"Loading Vertex Data")
	frame = mdx.frames[0]
	scale = g_scale.val
	
	def tmp_get_vertex(i):
		#use the first frame for the mesh vertices
		x=(frame.scale[0]*frame.vertices[i].vertices[0]+frame.translate[0])*scale
		y=(frame.scale[1]*frame.vertices[i].vertices[1]+frame.translate[1])*scale
		z=(frame.scale[2]*frame.vertices[i].vertices[2]+frame.translate[2])*scale
		return y,-x,z
	
	mesh.verts.extend( [tmp_get_vertex(i) for i in xrange(0,mdx.num_vertices)] )
	del tmp_get_vertex
	
	######## Make the UV list
	DrawProgressBar(0.50,"Loading UV Data")
	
	w = float(mdx.skin_width)
	h = float(mdx.skin_height)
	if w <= 0.0: w = 1.0
	if h <= 0.0: h = 1.0
	#for some reason quake2 texture maps are upside down, flip that
#	uv_list = [Vector(co.u/w, 1-(co.v/h)) for co in mdx.tex_coords]
	del w, h
	
	######### Make the faces
	DrawProgressBar(0.75,"Loading Face Data")
	faces = []
	face_uvs = []
	for mdx_face in mdx.faces:
		f = mdx_face.vertex_index[0], mdx_face.vertex_index[2], mdx_face.vertex_index[1]
#		uv = uv_list[mdx_face.texture_index[0]], uv_list[mdx_face.texture_index[2]], uv_list[mdx_face.texture_index[1]]
		
		if f[2] == 0:
			# EEKADOODLE :/
			f= f[1], f[2], f[0]
#			uv= uv[1], uv[2], uv[0]
		
		#ditto in reverse order with the texture verts
		faces.append(f)
#		face_uvs.append(uv)
	
	
	face_mapping = mesh.faces.extend(faces, indexList=True)
	print len(faces)
	print len(mesh.faces)
	mesh.faceUV= True  #turn on face UV coordinates for this mesh
	mesh_faces = mesh.faces
	for i, uv in enumerate(face_uvs):
		if face_mapping[i] != None:
			f = mesh_faces[face_mapping[i]]
			f.uv = uv
			if (mesh_image!=-1):
				f.image=mesh_image
	
	scn= Blender.Scene.GetCurrent()
	mesh_obj= scn.objects.new(mesh)
	animate_mdx(mdx, mesh)
	DrawProgressBar(0.98,"Loading Animation Data")
	
	#locate the Object containing the mesh at the cursor location
	cursor_pos=Blender.Window.GetCursorPos()
	mesh_obj.setLocation(float(cursor_pos[0]),float(cursor_pos[1]),float(cursor_pos[2]))
	DrawProgressBar (1.0, "") 
	WaitCursor(0)

#***********************************************
# MAIN
#***********************************************

# Import globals
g_mdx_filename=Create("*.mdx")
#g_mdx_filename=Create("/d/warvet/tris.mdx")
g_texture_filename=Create('')
# g_texture_filename=Create("/d/warvet/warvet.jpg")

g_filename_search=Create("*.mdx")
g_texture_search=Create('')
# g_texture_search=Create("/d/warvet/warvet.jpg")

#Globals
g_scale=Create(1.0)

# Events
EVENT_NOEVENT=1
EVENT_LOAD_MDX=2
EVENT_CHOOSE_FILENAME=3
EVENT_CHOOSE_TEXTURE=4
EVENT_SAVE_MDX=5
EVENT_EXIT=100

######################################################
# Callbacks for Window functions
######################################################
def filename_callback(input_filename):
	global g_mdx_filename
	g_mdx_filename.val=input_filename

def texture_callback(input_texture):
	global g_texture_filename
	g_texture_filename.val=input_texture

######################################################
# GUI Loader
######################################################


def draw_gui():
	global g_scale
	global g_mdx_filename
	global g_texture_filename
	global EVENT_NOEVENT,EVENT_LOAD_MDX,EVENT_CHOOSE_FILENAME,EVENT_CHOOSE_TEXTURE,EVENT_EXIT

	########## Titles
	glClear(GL_COLOR_BUFFER_BIT)
	glRasterPos2d(8, 125)
	Text("MDX loader")

	######### Parameters GUI Buttons
	BeginAlign()
	g_mdx_filename = String("MDX file to load: ", EVENT_NOEVENT, 10, 55, 210, 18,
							g_mdx_filename.val, 255, "MDX file to load")
	########## MDX File Search Button
	Button("Browse",EVENT_CHOOSE_FILENAME,220,55,80,18)
	EndAlign()

	BeginAlign()
	g_texture_filename = String("Texture file to load: ", EVENT_NOEVENT, 10, 35, 210, 18,
								g_texture_filename.val, 255, "Texture file to load-overrides MDX file")
	########## Texture Search Button
	Button("Browse",EVENT_CHOOSE_TEXTURE,220,35,80,18)
	EndAlign()

	########## Scale slider-default is 1/8 which is a good scale for mdx->blender
	g_scale= Slider("Scale Factor: ", EVENT_NOEVENT, 10, 75, 210, 18,
					1.0, 0.001, 10.0, 1, "Scale factor for obj Model");

	######### Draw and Exit Buttons
	Button("Load",EVENT_LOAD_MDX , 10, 10, 80, 18)
	Button("Exit",EVENT_EXIT , 170, 10, 80, 18)

def event(evt, val):	
	if (evt == QKEY and not val):
		Blender.Draw.Exit()

def bevent(evt):
	global g_mdx_filename
	global g_texture_filename
	global EVENT_NOEVENT,EVENT_LOAD_MDX,EVENT_SAVE_MDX,EVENT_EXIT

	######### Manages GUI events
	if (evt==EVENT_EXIT):
		Blender.Draw.Exit()
	elif (evt==EVENT_CHOOSE_FILENAME):
		FileSelector(filename_callback, "MDX File Selection")
	elif (evt==EVENT_CHOOSE_TEXTURE):
		FileSelector(texture_callback, "Texture Selection")
	elif (evt==EVENT_LOAD_MDX):
		if not Blender.sys.exists(g_mdx_filename.val):
			PupMenu('Model file does not exist')
			return
		else:
			load_mdx(g_mdx_filename.val, g_texture_filename.val)
			Blender.Redraw()
			Blender.Draw.Exit()
			return

if __name__ == '__main__':
	Register(draw_gui, event, bevent)
