/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27
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
DebugWatch = DebugInstrument:extend
{
	_watches = {},
	visible = false,

	onNew = function (self)
		self.title.text = 'Watch'
		self.labels = self:add(Text:new{ font = self.font })
		self.values = self:add(Text:new{ font = self.font })
		self.lineHeight = self.labels._fontObj:getHeight()

		debugger.watch = function (exp, label) self:addExpression(exp, label) end
	end,

	-- Method: addExpression
	-- Adds an expression to be watched.
	--
	-- Arguments:
	--		expression - expression to evaluate as a string
	--		label - string label, defaults to expression

	addExpression = function (self, expression, label)
		self.visible = true
		table.insert(self._watches, { label = label or expression,
									  func = loadstring('return ' .. expression) })
	
		self.contentHeight = #self._watches * self.lineHeight + 2 * self.spacing
	end,

	onUpdate = function (self)
		self.labels.text = ''
		self.values.text = ''

		for _, watch in pairs(self._watches) do
			local ok, value = pcall(watch.func)
			if not ok then value = nil end
			self.labels.text = self.labels.text .. watch.label .. '\n'
			self.values.text = self.values.text .. tostring(value) .. '\n'
		end
	end,

	onResize = function (self, x, y, width, height)
		self.labels.y, self.values.y = y + self.spacing, y + self.spacing
		self.labels.height, self.values.height = height, height

		self.labels.x = x + self.spacing
		self.labels.width = width / 2 - self.spacing * 2
		
		self.values.x = self.labels.x + self.labels.width + self.spacing
		self.values.width = self.labels.width
	end
}