dna: Add groundwork for new XML-driven parser.

This commit is contained in:
Sam Edwards 2014-04-04 05:11:17 -06:00
parent 8aeefae192
commit 547e0dfe16
32 changed files with 657 additions and 0 deletions

View file

@ -0,0 +1,24 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNABaseline(DNASceneElement):
TAG = 'baseline'
PARENTS = ['sign']
def __init__(self, code=None, flags='', wiggle='0', stumble='0', stomp='0', kern='0', width='0', height='0'):
DNASceneElement.__init__(self)
self.code = code
self.flags = flags
self.wiggle = float(wiggle)
self.stumble = float(stumble)
self.stomp = float(stomp)
self.kern = float(kern)
self.width = float(width)
self.height = float(height)
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNABaseline)

16
toontown/dna/DNAColor.py Normal file
View file

@ -0,0 +1,16 @@
from DNAPropertyElement import DNAPropertyElement
from DNAParser import *
from panda3d.core import *
class DNAColor(DNAPropertyElement):
TAG = 'color'
def __init__(self, r="1", g="1", b="1", a="1"):
DNAPropertyElement.__init__(self)
self.color = (float(r), float(g), float(b), float(a))
def _apply(self, parent):
parent.setColor(self.color)
registerElement(DNAColor)

View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNACornice(DNASceneElement):
TAG = 'cornice'
PARENTS = ['wall']
def __init__(self, code):
DNASceneElement.__init__(self)
self.code = code
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNACornice)

17
toontown/dna/DNADoor.py Normal file
View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNADoor(DNASceneElement):
TAG = 'door'
PARENTS = ['landmark_building']
def __init__(self, code):
DNASceneElement.__init__(self)
self.code = code
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNADoor)

View file

@ -0,0 +1,19 @@
class DNAElement:
TAG = '*'
PARENTS = []
def __init__(self):
self._parent = None
self._children = []
def reparentTo(self, parent):
if self._parent:
self._parent._children.remove(self)
self._parent = parent
if parent:
self._parent._children.append(self)
def handleText(self, chars):
pass

View file

@ -0,0 +1,18 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAFlatBuilding(DNASceneElement):
TAG = 'flat_building'
PARENTS = ['group', 'node', 'visgroup']
def __init__(self, id, width="0"):
DNASceneElement.__init__(self)
self.id = id
self.width = float(width)
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNAFlatBuilding)

View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAGraphic(DNASceneElement):
TAG = 'graphic'
PARENTS = ['baseline']
def __init__(self, code):
DNASceneElement.__init__(self)
self.code = code
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNAGraphic)

17
toontown/dna/DNAGroup.py Normal file
View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAGroup(DNASceneElement):
TAG = 'group'
PARENTS = ['scene', 'visgroup']
def __init__(self, name):
DNASceneElement.__init__(self)
self.name = name
def _makeNode(self, storage, parent):
return parent.attachNewNode(self.name)
registerElement(DNAGroup)

16
toontown/dna/DNAHpr.py Normal file
View file

@ -0,0 +1,16 @@
from DNAPropertyElement import DNAPropertyElement
from DNAParser import *
from panda3d.core import *
class DNAHpr(DNAPropertyElement):
TAG = 'hpr'
def __init__(self, h="0", p="0", r="0"):
DNAPropertyElement.__init__(self)
self.hpr = (float(h), float(p), float(r))
def _apply(self, parent):
parent.setHpr(self.hpr)
registerElement(DNAHpr)

View file

@ -0,0 +1,19 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNALandmarkBuilding(DNASceneElement):
TAG = 'landmark_building'
PARENTS = ['group', 'node', 'visgroup']
def __init__(self, id, code, type=None):
DNASceneElement.__init__(self)
self.id = id
self.code = code
self.type = type
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNALandmarkBuilding)

22
toontown/dna/DNAModel.py Normal file
View file

@ -0,0 +1,22 @@
from DNAStorageElement import DNAStorageElement
from DNAParser import *
class DNAModel(DNAStorageElement):
TAG = 'model'
PARENTS = ['storage']
def __init__(self, path, scope=None):
DNAStorageElement.__init__(self)
self.path = path
self.scope = scope
self.model = None
def getModel(self):
if not self.model:
self.model = loader.loadModel(self.path)
return self.model
registerElement(DNAModel)

16
toontown/dna/DNANhpr.py Normal file
View file

@ -0,0 +1,16 @@
from DNAPropertyElement import DNAPropertyElement
from DNAParser import *
from panda3d.core import *
class DNANhpr(DNAPropertyElement):
TAG = 'nhpr'
def __init__(self, h="0", p="0", r="0"):
DNAPropertyElement.__init__(self)
self.hpr = (float(h), float(p), float(r))
def _apply(self, parent):
parent.setHpr(self.hpr)
registerElement(DNANhpr)

17
toontown/dna/DNANode.py Normal file
View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNANode(DNASceneElement):
TAG = 'node'
PARENTS = ['group', 'node', 'visgroup']
def __init__(self, name):
DNASceneElement.__init__(self)
self.name = name
def _makeNode(self, storage, parent):
return parent.attachNewNode(self.name)
registerElement(DNANode)

50
toontown/dna/DNAParser.py Normal file
View file

@ -0,0 +1,50 @@
import xml.sax
class DNAParseError(Exception): pass
elementRegistry = {}
def registerElement(element):
elementRegistry[element.TAG] = element
class DNASaxHandler(xml.sax.ContentHandler):
def __init__(self):
xml.sax.ContentHandler.__init__(self)
self.stack = []
self.root = None
def startElement(self, tag, attrs):
if self.stack:
parent = self.stack[-1]
parentTag = parent.TAG
else:
parent = None
parentTag = None
element = elementRegistry.get(tag)
if not element:
raise DNAParseError('Unknown element type: ' + tag)
if parentTag not in element.PARENTS:
raise DNAParseError('Cannot put %s below %s element' % (tag, parentTag))
element = element(**attrs)
self.stack.append(element)
element.reparentTo(parent)
if not self.root:
self.root = element
def endElement(self, tag):
self.stack.pop(-1)
def characters(self, chars):
if not self.stack:
return
self.stack[-1].handleText(chars)
def parse(stream):
handler = DNASaxHandler()
xml.sax.parse(stream, handler)
return handler.root

16
toontown/dna/DNAPos.py Normal file
View file

@ -0,0 +1,16 @@
from DNAPropertyElement import DNAPropertyElement
from DNAParser import *
from panda3d.core import *
class DNAPos(DNAPropertyElement):
TAG = 'pos'
def __init__(self, x="0", y="0", z="0"):
DNAPropertyElement.__init__(self)
self.pos = (float(x), float(y), float(z))
def _apply(self, parent):
parent.setPos(self.pos)
registerElement(DNAPos)

21
toontown/dna/DNAProp.py Normal file
View file

@ -0,0 +1,21 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAProp(DNASceneElement):
TAG = 'prop'
PARENTS = ['group', 'node', 'visgroup', 'prop', 'landmark_building']
def __init__(self, name, code):
DNASceneElement.__init__(self)
self.name = name
self.code = code
def _makeNode(self, storage, parent):
node = storage.requestNode(self.code).copyTo(parent)
node.setName(self.name)
return node
registerElement(DNAProp)

View file

@ -0,0 +1,14 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAPropertyElement(DNASceneElement):
PARENTS = ['prop', 'node', 'flat_building', 'landmark_building', 'door',
'wall', 'windows', 'cornice', 'sign', 'baseline']
def _makeNode(self, storage, parent):
# We don't return a node, because this is a property.
self._apply(parent)
def _apply(self, parent):
pass # Overridden by subclass.

16
toontown/dna/DNAScale.py Normal file
View file

@ -0,0 +1,16 @@
from DNAPropertyElement import DNAPropertyElement
from DNAParser import *
from panda3d.core import *
class DNAScale(DNAPropertyElement):
TAG = 'scale'
def __init__(self, x="1", y="1", z="1"):
DNAPropertyElement.__init__(self)
self.scale = (float(x), float(y), float(z))
def _apply(self, parent):
parent.setScale(self.scale)
registerElement(DNAScale)

View file

@ -0,0 +1,20 @@
from DNAElement import DNAElement
from DNAParser import *
class DNASceneElement(DNAElement):
def __init__(self):
DNAElement.__init__(self)
self.code = None
def _generate(self, storage, parent):
node = self._makeNode(storage, parent)
node.setTag('DNARoot', self.TAG)
node.setTag('DNACode', self.code)
if node:
for child in self._children:
child._generate(storage, node)
def _makeNode(self, storage, parent):
pass # Overridable by subclass

View file

@ -0,0 +1,25 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNASceneRoot(DNASceneElement):
TAG = 'scene'
PARENTS = [None]
def __init__(self, zone=None):
DNASceneElement.__init__(self)
self.zone = zone
def generate(self, storage):
"""
Generate the scenegraph for this scene using the nodes, textures, and
fonts stored within the provided DNAStorage object.
"""
scene = NodePath('scene')
for child in self._children:
child._generate(storage, scene)
return scene
registerElement(DNASceneRoot)

17
toontown/dna/DNASign.py Normal file
View file

@ -0,0 +1,17 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNASign(DNASceneElement):
TAG = 'sign'
PARENTS = ['prop', 'landmark_building']
def __init__(self, code=None):
DNASceneElement.__init__(self)
self.code = code
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNASign)

View file

@ -0,0 +1,29 @@
from DNAElement import DNAElement
from DNAParser import *
class DNAStorageElement(DNAElement):
def __init__(self):
DNAElement.__init__(self)
self.scope = None
def getScope(self):
if self.scope is not None:
return self.scope
elif self._parent is not None:
return self._parent
else:
raise DNAParseError('No scope defined')
def store(self, storage):
"""
Stores everything into the specified DNAStorage object.
"""
self._store(storage)
for child in self._children:
child.store(storage)
def _store(self, storage):
pass # Overridable by subclass

View file

@ -0,0 +1,13 @@
from DNAStorageElement import DNAStorageElement
from DNAParser import *
class DNAStorageRoot(DNAStorageElement):
TAG = 'storage'
PARENTS = [None]
def __init__(self, scope='global'):
DNAStorageElement.__init__(self)
self.scope = scope
registerElement(DNAStorageRoot)

View file

@ -0,0 +1,19 @@
from DNAStorageElement import DNAStorageElement
from DNAParser import *
class DNAStoreFont(DNAStorageElement):
TAG = 'store_font'
PARENTS = ['storage']
def __init__(self, root, code, path):
DNAStorageElement.__init__(self)
self.root = root
self.code = code
self.path = path
def _store(self, storage):
font = loader.loadFont(self.path)
storage.storeFont(self.code, font, self.getScope())
registerElement(DNAStoreFont)

View file

@ -0,0 +1,28 @@
from DNAStorageElement import DNAStorageElement
from DNAParser import *
class DNAStoreNode(DNAStorageElement):
TAG = 'store_node'
PARENTS = ['model']
def __init__(self, root, code, node=None):
DNAStorageElement.__init__(self)
self.root = root
self.code = code
if node is None:
self.node = code
else:
self.node = node
def _store(self, storage):
model = self._parent.getModel()
if self.node == "":
node = model
else:
node = model.find('**/' + self.node)
storage.storeNode(node, self.code, self.getScope())
registerElement(DNAStoreNode)

View file

@ -0,0 +1,19 @@
from DNAStorageElement import DNAStorageElement
from DNAParser import *
class DNAStoreTexture(DNAStorageElement):
TAG = 'store_texture'
PARENTS = ['storage']
def __init__(self, root, code, path):
DNAStorageElement.__init__(self)
self.root = root
self.code = code
self.path = path
def _store(self, storage):
texture = loader.loadTexture(self.path)
storage.storeTexture(texture, self.code, self.getScope())
registerElement(DNAStoreTexture)

21
toontown/dna/DNAText.py Normal file
View file

@ -0,0 +1,21 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAText(DNASceneElement):
TAG = 'text'
PARENTS = ['baseline']
def __init__(self):
DNASceneElement.__init__(self)
self.text = ''
def handleText(self, text):
self.text = text
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNAText)

21
toontown/dna/DNATitle.py Normal file
View file

@ -0,0 +1,21 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNATitle(DNASceneElement):
TAG = 'title'
PARENTS = ['landmark_building']
def __init__(self):
DNASceneElement.__init__(self)
self.title = ''
def handleText(self, text):
self.title = text
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNATitle)

View file

@ -0,0 +1,18 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAVisGroup(DNASceneElement):
TAG = 'visgroup'
PARENTS = ['group']
def __init__(self, zone, vis=''):
DNASceneElement.__init__(self)
self.zone = zone
self.vis = vis.split()
def _makeNode(self, storage, parent):
return parent.attachNewNode(self.name)
registerElement(DNAVisGroup)

18
toontown/dna/DNAWall.py Normal file
View file

@ -0,0 +1,18 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAWall(DNASceneElement):
TAG = 'wall'
PARENTS = ['flat_building']
def __init__(self, code, height="0"):
DNASceneElement.__init__(self)
self.code = code
self.height = float(height)
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNAWall)

View file

@ -0,0 +1,18 @@
from DNASceneElement import DNASceneElement
from DNAParser import *
from panda3d.core import *
class DNAWindows(DNASceneElement):
TAG = 'windows'
PARENTS = ['wall']
def __init__(self, code, count="0"):
DNASceneElement.__init__(self)
self.code = code
self.count = int(count)
def _makeNode(self, storage, parent):
pass # TODO
registerElement(DNAWindows)

39
toontown/dna/__init__.py Normal file
View file

@ -0,0 +1,39 @@
import DNAParser
# ---List elements here, so they get imported---
# Storage elements:
import DNAStorageRoot
import DNAModel
import DNAStoreNode
import DNAStoreFont
import DNAStoreTexture
# Scene elements:
import DNASceneRoot
import DNAGroup
import DNANode
import DNAVisGroup
import DNAProp
import DNAFlatBuilding
import DNALandmarkBuilding
import DNAWall
import DNADoor
import DNAWindows
import DNACornice
import DNASign
import DNAGraphic
# Scene properties:
import DNAPos
import DNAHpr
import DNANhpr
import DNAColor
import DNAScale
# Specific properties:
import DNABaseline
import DNAText
import DNATitle