summaryrefslogtreecommitdiff
path: root/info.py
blob: 22abc2fd79014dc67275dd497afcd27ba4a640c0 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def parse_indental(filename):
	"""Reads a geometry file into a tree of dictionaries."""
	with open(filename) as f:
		indents = [0]
		stack = [{}]
		key = ""
		for no, line in enumerate(f):
			# Compute indent
			indent = 0
			while indent < len(line) and line[indent] in " \t":
				indent += 1
			line = line.strip()

			# Skip comments and blank lines
			if not line or line[0] == "#" or line[0] == ";": continue

			try:
				# Find appropriate parent for the indent
				if indent > indents[-1]:
					indents.append(indent)
					entry = stack[-1][key] = {}
					stack.append(entry)
				else:
					while indent < indents[-1]:
						indents.pop()
						stack.pop()
					if indent != indents[-1]:
						raise SyntaxError("Improper indent, " + filename + ":" + str(no))

				# Parse & store line
				key, value = line.split(":") if ":" in line else (line, "")
				stack[-1][key.strip()] = value.strip()
			except:
				raise SyntaxError("Malformed line, " + filename + ":" + str(no))
		return stack[0]

def zone4point(geom, parent, x, y, width, height, depth=5):
	"""Figures out which zone from the rendered geometry has been clicked."""
	if geom is None: return None, None
	depth -= 1
	if not depth: return None, None

	# Maybe a parent exists?
	for i in range(len(parent)):
		key = parent[:len(parent)-i]
		if key in geom: return key, geom[key]

	# Try children...
	w, h = width/3.0, height/3.0
	if x < w:
		if y < h:
			return zone4point(geom, parent+"0", x, y, w, h, depth)
		elif y < h*2:
			return zone4point(geom, parent+"3", x, y - h, w, h, depth)
		elif y < h*3:
			return zone4point(geom, parent+"6", x, y - h*2, w, h, depth)
	elif x < w*2:
		if y < h:
			return zone4point(geom, parent+"1", x-w, y, w, h, depth)
		elif y < h*2:
			return zone4point(geom, parent+"4", x-w, y-h, w, h, depth)
		elif y < h*3:
			return zone4point(geom, parent+"7", x-w, y-h*2, w, h, depth)
	else:
		if y < h:
			return zone4point(geom, parent+"2", x-w*2, y, w, h, depth)
		elif y < h*2:
			return zone4point(geom, parent+"5", x-w*2, y-h, w, h, depth)
		elif y < h*3:
			return zone4point(geom, parent+"8", x-w*2, y-h*2, w, h, depth)
	return None, None