summaryrefslogtreecommitdiff
path: root/tools/genlayout.py
blob: 9a287db40852b0918511f80c0e6b47f86afaf23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from bs4 import BeautifulSoup

SCALE = 1.8

def readsvg(filename):
	with open(filename) as fp:
		soup = BeautifulSoup(fp, features="xml")

	result = []
	boxes = soup.find('g', id='Mission-Boxes')
	for box in boxes.find_all('g', recursive=False):
		area_name = box.attrs.get('serif:id', box.attrs['id'])
		for g in box.find_all('g'):
			xo = 0
			yo = 0
			transform = g.attrs.get('transform')
			if transform and transform.startswith("translate("):
				transform = transform.replace("translate(","").replace(")","").split(',')
				xo = float(transform[0])
				yo = float(transform[1])
			for rect in g.find_all('rect'):
				rect_id = rect.attrs.get('serif:id', rect.attrs['id'])
				x = float(rect.attrs['x']) + xo
				y = float(rect.attrs['y']) + yo
				w = float(rect.attrs['width'])
				h = float(rect.attrs['height'])
				xc = round((x+w/2.0)/SCALE)
				yc = round((y+h/2.0)/SCALE)
				name = area_name + '-' + rect_id
				result.append([name, xc, yc])

	return result

def print_list(data):
	print("const LAYOUT = {")
	for (name, x, y) in data:
		print(f'\t"{name}": [{x}, {y}],')
	print("}")

result = readsvg("tools/layout.svg")
print_list(result)