<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://cegui.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newacct</id>
		<title>CEGUI Wiki - Crazy Eddie's GUI System (Open Source) - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://cegui.org/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newacct"/>
		<link rel="alternate" type="text/html" href="http://cegui.org/wiki/Special:Contributions/Newacct"/>
		<updated>2026-04-17T11:23:08Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.24.1</generator>

	<entry>
		<id>http://cegui.org/wiki/index.php?title=CELayoutUpgrader&amp;diff=4480</id>
		<title>CELayoutUpgrader</title>
		<link rel="alternate" type="text/html" href="http://cegui.org/wiki/index.php?title=CELayoutUpgrader&amp;diff=4480"/>
				<updated>2011-06-02T06:52:23Z</updated>
		
		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{VersionBadge|0.4}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
CELayoutUpgrader is a python script that can upgrade XML layout files to the Unified Coordinate System. Hopefully it will make life a little easier. See this [http://www.cegui.org.uk/phpBB2/viewtopic.php?t=1766 message board thread]for discussion.&lt;br /&gt;
&lt;br /&gt;
= Source =&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
&lt;br /&gt;
# CELayoutUpgrader: Upgrades CEGUI layout XML files to the Unified Coordinate System&lt;br /&gt;
# Copyright (C) 2006 Seth Yastrov &amp;lt;syastrov@gmail.com&amp;gt;&lt;br /&gt;
#&lt;br /&gt;
# This program is free software; you can redistribute it and/or&lt;br /&gt;
# modify it under the terms of the GNU General Public License&lt;br /&gt;
# as published by the Free Software Foundation; either version 2&lt;br /&gt;
# of the License, or (at your option) any later version.&lt;br /&gt;
#&lt;br /&gt;
# This program is distributed in the hope that it will be useful,&lt;br /&gt;
# but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;
# GNU General Public License for more details.&lt;br /&gt;
#&lt;br /&gt;
# You should have received a copy of the GNU General Public License&lt;br /&gt;
# along with this program; if not, write to the Free Software&lt;br /&gt;
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.&lt;br /&gt;
&lt;br /&gt;
import sys&lt;br /&gt;
import codecs&lt;br /&gt;
from glob import glob&lt;br /&gt;
&lt;br /&gt;
from xml.dom import minidom&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
  import xml.dom.ext&lt;br /&gt;
except ImportError:&lt;br /&gt;
  print 'Error importing PyXML: You need to install PyXML.'&lt;br /&gt;
  print 'The default XML pretty-printing implementation is pretty ugly.'&lt;br /&gt;
&lt;br /&gt;
class InvalidDocumentError(Exception): pass&lt;br /&gt;
&lt;br /&gt;
class UDim:&lt;br /&gt;
  def __init__(self, scale, offset):&lt;br /&gt;
    self.scale, self.offset = scale, offset&lt;br /&gt;
  def toString(self):&lt;br /&gt;
    return &amp;quot;{%f,%f}&amp;quot; % (self.scale, self.offset)&lt;br /&gt;
  def exists(self):&lt;br /&gt;
    return not (self.scale == self.offset == 0.0)&lt;br /&gt;
&lt;br /&gt;
class UVector2:&lt;br /&gt;
  def __init__(self, xs, xo, ys, yo):&lt;br /&gt;
    self.xs, self.xo, self.ys, self.yo = xs, xo, ys, yo&lt;br /&gt;
  def toString(self):&lt;br /&gt;
    return &amp;quot;{{%f,%f},{%f,%f}}&amp;quot; % (self.xs, self.xo, self.ys, self.yo)&lt;br /&gt;
  def exists(self):&lt;br /&gt;
    return not (self.xs == self.xo == self.ys == self.yo == 0.0)&lt;br /&gt;
  &lt;br /&gt;
class URect:&lt;br /&gt;
  def __init__(self, ls, lo, ts, to, rs, ro, bs, bo):&lt;br /&gt;
    self.ls, self.lo, self.ts, self.to, self.rs, self.ro, self.bs, self.bo =\&lt;br /&gt;
      ls, lo, ts, to, rs, ro, bs, bo&lt;br /&gt;
  def toString(self):&lt;br /&gt;
    return &amp;quot;{{%f,%f},{%f,%f},{%f,%f},{%f,%f}}&amp;quot; %\&lt;br /&gt;
      (self.ls, self.lo, self.ts, self.to, self.rs, self.ro, self.bs, self.bo)&lt;br /&gt;
  def exists(self):&lt;br /&gt;
    return not (self.ls == self.lo == self.ts == self.to == self.rs ==\&lt;br /&gt;
      self.ro == self.bs == self.bo == 0.0)&lt;br /&gt;
&lt;br /&gt;
def splitCoords(coords):&lt;br /&gt;
  result = {}&lt;br /&gt;
  for coord in coords.split(' '):&lt;br /&gt;
    name, value = coord.split(':',1)&lt;br /&gt;
    result[name] = value&lt;br /&gt;
  return result&lt;br /&gt;
&lt;br /&gt;
def upgrade(file):&lt;br /&gt;
  try:&lt;br /&gt;
    doc = minidom.parse(file);&lt;br /&gt;
  except Exception, detail:&lt;br /&gt;
    raise IOError, (&amp;quot;Failed to parse `%s': %s&amp;quot; % (file, detail))&lt;br /&gt;
  &lt;br /&gt;
  global upgradeCount&lt;br /&gt;
  upgradeCount = 0&lt;br /&gt;
  &lt;br /&gt;
  if doc.firstChild.nodeName != 'GUILayout':&lt;br /&gt;
    raise InvalidDocumentError&lt;br /&gt;
  &lt;br /&gt;
  for window in doc.getElementsByTagName('Window'):&lt;br /&gt;
    # Collect all the old properties for each window&lt;br /&gt;
    coordProperties = ['RelativeMinSize', 'AbsoluteMinSize',\&lt;br /&gt;
      'RelativeMaxSize', 'AbsoluteMaxSize',\&lt;br /&gt;
      'Rect', 'RelativeRect', 'AbsoluteRect',\&lt;br /&gt;
      'Position', 'AbsolutePosition', 'RelativePosition',\&lt;br /&gt;
      'Size', 'RelativeSize', 'AbsoluteSize']&lt;br /&gt;
      &lt;br /&gt;
    properties = {}&lt;br /&gt;
    for child in window.getElementsByTagName('Property'):&lt;br /&gt;
      if child in window.childNodes:&lt;br /&gt;
        properties[child.getAttribute('Name')] = child.getAttribute('Value')&lt;br /&gt;
      &lt;br /&gt;
    for prop in coordProperties:&lt;br /&gt;
      if prop in properties:&lt;br /&gt;
        properties[prop] = splitCoords(properties[prop])&lt;br /&gt;
    &lt;br /&gt;
    def getUDim(properties, name):&lt;br /&gt;
      scale, offset = 0.0, 0.0&lt;br /&gt;
      if name in properties:&lt;br /&gt;
        scale = float(properties[name])&lt;br /&gt;
      if 'Relative'+name in properties:&lt;br /&gt;
        scale = float(properties['Relative'+name])&lt;br /&gt;
      if 'Absolute'+name in properties:&lt;br /&gt;
        offset = float(properties[('Absolute'+name)])&lt;br /&gt;
      return UDim(scale, offset)&lt;br /&gt;
    &lt;br /&gt;
    def getUVector2(properties, name, c1, c2):&lt;br /&gt;
      xs, xo, ys, yo = 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
      if name in properties:&lt;br /&gt;
        xs, ys = float(properties[name][c1]), float(properties[name][c2])&lt;br /&gt;
      if 'Relative'+name in properties:&lt;br /&gt;
        xs, ys = float(properties['Relative'+name][c1]), float(properties['Relative'+name][c2])&lt;br /&gt;
      if 'Absolute'+name in properties:&lt;br /&gt;
        xo, yo = float(properties[('Absolute'+name)][c1]), float(properties[('Absolute'+name)][c2])&lt;br /&gt;
      return UVector2(xs, xo, ys, yo)&lt;br /&gt;
      &lt;br /&gt;
    def getURect(properties, name):&lt;br /&gt;
      ls, lo, ts, to, rs, ro, bs, bo = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
      if name in properties:&lt;br /&gt;
        ls, ts = float(properties[name]['l']), float(properties[name]['t'])&lt;br /&gt;
        rs, bs = float(properties[name]['r']), float(properties[name]['b'])&lt;br /&gt;
      if 'Relative'+name in properties:&lt;br /&gt;
        ls, ts = float(properties['Relative'+name]['l']), float(properties['Relative'+name]['t'])&lt;br /&gt;
        rs, bs = float(properties['Relative'+name]['r']), float(properties['Relative'+name]['b'])&lt;br /&gt;
      if 'Absolute'+name in properties:&lt;br /&gt;
        lo, to = float(properties['Absolute'+name]['l']), float(properties['Absolute'+name]['t'])&lt;br /&gt;
        ro, bo = float(properties['Absolute'+name]['r']), float(properties['Absolute'+name]['b'])&lt;br /&gt;
      return URect(ls, lo, ts, to, rs, ro, bs, bo)&lt;br /&gt;
    &lt;br /&gt;
    width = getUDim(properties, 'Width')&lt;br /&gt;
    height = getUDim(properties, 'Height')&lt;br /&gt;
    xposition = getUDim(properties, 'XPosition')&lt;br /&gt;
    yposition = getUDim(properties, 'YPosition')&lt;br /&gt;
    position = getUVector2(properties, 'Position', 'x', 'y')&lt;br /&gt;
    size = getUVector2(properties, 'Size', 'w', 'h')&lt;br /&gt;
    minSize = getUVector2(properties, 'MinSize', 'w', 'h')&lt;br /&gt;
    maxSize = getUVector2(properties, 'MaxSize', 'w', 'h')&lt;br /&gt;
    rect = getURect(properties, 'Rect')&lt;br /&gt;
    &lt;br /&gt;
    # Add new unified ones&lt;br /&gt;
    def addProperty(parent, name, value):&lt;br /&gt;
      global upgradeCount&lt;br /&gt;
      upgradeCount += 1&lt;br /&gt;
      &lt;br /&gt;
      node = doc.createElement('Property')&lt;br /&gt;
      node.setAttribute('Name', name)&lt;br /&gt;
      node.setAttribute('Value', value)&lt;br /&gt;
      window.insertBefore(node, parent.firstChild)&lt;br /&gt;
      return node&lt;br /&gt;
    &lt;br /&gt;
    if width.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedWidth', width.toString())&lt;br /&gt;
    if height.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedHeight', height.toString())&lt;br /&gt;
    if xposition.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedXPosition', xposition.toString())&lt;br /&gt;
    if yposition.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedYPosition', yposition.toString())&lt;br /&gt;
    &lt;br /&gt;
    if position.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedPosition', position.toString())&lt;br /&gt;
    if size.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedSize', size.toString())&lt;br /&gt;
    if minSize.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedMinSize', minSize.toString())  &lt;br /&gt;
    if maxSize.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedMaxSize', maxSize.toString())  &lt;br /&gt;
    &lt;br /&gt;
    if rect.exists():&lt;br /&gt;
      addProperty(window, 'UnifiedAreaRect', rect.toString())  &lt;br /&gt;
    &lt;br /&gt;
  # Remove old coord system nodes&lt;br /&gt;
  for child in doc.getElementsByTagName('Property'):&lt;br /&gt;
    if child.getAttribute('Name') in coordProperties:&lt;br /&gt;
      child.parentNode.removeChild(child)&lt;br /&gt;
      child.unlink()&lt;br /&gt;
  &lt;br /&gt;
  f = codecs.open(file, 'w', 'utf-8')&lt;br /&gt;
  #doc.writexml(f, &amp;quot;&amp;quot;, &amp;quot;    &amp;quot;)&lt;br /&gt;
  xml.dom.ext.PrettyPrint(doc, f, indent='\t')&lt;br /&gt;
  f.close()&lt;br /&gt;
&lt;br /&gt;
def usage(scriptname):&lt;br /&gt;
  print 'Usage: %s FILES' % scriptname&lt;br /&gt;
  print 'FILES can be a glob-style pattern or a list of files.'&lt;br /&gt;
&lt;br /&gt;
def main(argv):&lt;br /&gt;
  print 'CELayoutUpgrader: Upgrades CEGUI layout XML files to the Unified Coordinate System'&lt;br /&gt;
  print 'Copyright (C) 2006 Seth Yastrov &amp;lt;syastrov@gmail.com&amp;gt;\n'&lt;br /&gt;
  &lt;br /&gt;
  if len(argv) &amp;lt; 2:&lt;br /&gt;
    usage(argv[0])&lt;br /&gt;
    sys.exit(2)&lt;br /&gt;
  &lt;br /&gt;
  global upgradeCount&lt;br /&gt;
  &lt;br /&gt;
  for arg in argv[1:]:&lt;br /&gt;
    for file in glob(arg):&lt;br /&gt;
      try:&lt;br /&gt;
        upgrade(file)&lt;br /&gt;
      except InvalidDocumentError:&lt;br /&gt;
        print &amp;quot;'%s' =&amp;gt; Failed. Does not appear to be a valid GUILayout file.&amp;quot; % file&lt;br /&gt;
        sys.exit(2)&lt;br /&gt;
      except IOError, detail:&lt;br /&gt;
        print &amp;quot;'%s' =&amp;gt; %s&amp;quot; % (file, detail)&lt;br /&gt;
      else:&lt;br /&gt;
        print &amp;quot;'%s' =&amp;gt; Upgraded (%d properties)&amp;quot; % (file, upgradeCount)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
  main(sys.argv)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Dependencies =&lt;br /&gt;
* [http://www.python.org/ Python] (any relatively recent version will do)&lt;br /&gt;
* [http://pyxml.sourceforge.net/ PyXML] (0.8 and up should be fine)&lt;br /&gt;
&lt;br /&gt;
= Usage =&lt;br /&gt;
&lt;br /&gt;
python CELayoutUpgrader.py FILES&lt;br /&gt;
&lt;br /&gt;
FILES can be a glob-style pattern or a list of files.&lt;br /&gt;
&lt;br /&gt;
= Example =&lt;br /&gt;
&lt;br /&gt;
If you have a directory with a bunch of .layout files in it, stick the script in that directory and run it like this:&lt;br /&gt;
&lt;br /&gt;
 python CELayoutUpgrader.py *.layout&lt;br /&gt;
&lt;br /&gt;
= How it works =&lt;br /&gt;
&lt;br /&gt;
It will parse the layout files you specified searching for old-style properties.&lt;br /&gt;
It will convert each one to the Unified Coordinate System equivalent.&lt;br /&gt;
If the file already has some Unified Coordinate System properties, it will leave them in place.&lt;br /&gt;
After upgrading each file, it will report how many properties it upgraded.&lt;br /&gt;
&lt;br /&gt;
[[Category:HowTo]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>	</entry>

	</feed>