Commits

Chris Klimas committed 85575a6

Add debug shortcut functionality

Previously was just hotkeys, now these are visible as buttons in the console

Comments (0)

Files changed (4)

zoetrope/debug/debugger.lua

 		spacing = 10,
 		instruments = { narrow = Group:new(), wide = Group:new() },
 		widths = { wide = 0.7, narrow = 0.3 },
+		listeners = {},
 
 		_instrumentHeights = {},
 
 					end
 				end
 			end
+
+			for _, listener in pairs(self.listeners) do
+				listener()
+			end
 		end
 	}
 
 	the.app.meta:add(debugger.console)
 
 	debugger.addInstrument(DebugWatch:new())
+	debugger.addInstrument(DebugShortcuts:new())
 	debugger.addInstrument(DebugConsole:new())
 	debugger.addInstrument(DebugStepper:new())
 	debugger.addInstrument(DebugLocals:new())
 	debugger._resizeInstruments()
 end
 
+-- Function: debugger.addListener
+-- Adds a function that will be called on every frame,
+-- regardless of whether the console is visible.
+--
+-- Arguments:
+--		listener - function
+--
+-- Returns:
+--		nothing
+
+debugger.addListener = function (func)
+	table.insert(debugger.console.listeners, func)
+end
+
 -- Function: debugger.reload
 -- Resets the entire app and forces all code to be reloaded from 
 -- on disk. via https://love2d.org/forums/viewtopic.php?f=3&t=7965

zoetrope/debug/hotkeys.lua

--- Class: DebugHotkeys
---
--- This allows for debugging hotkeys -- e.g. you could set it so that
--- pressing Control-Alt-I toggles invincibility of the player sprite.
--- Out of the box:
---		- Control-Alt-F toggles fullscreen
---		- Control-Alt-Q quits the app.
---		- Control-Alt-P deactivates the view.
--- 		- Control-Alt-R reloads all app code from on disk.
---		- Control-Alt-S saves a screenshot to the app's directory --
---		  see https://love2d.org/wiki/love.filesystem for where this is.
-
-DebugHotkeys = Sprite:extend
-{
-	-- Property: modifiers
-	-- A table of modifier keys that must be held in order to activate
-	-- a debugging hotkey (set via <add()>). If you want hotkeys to
-	-- activate without having to hold any keys down, set this to nil.
-	modifiers = {'ctrl', 'alt'},
-
-	-- internal property: _hotkeys
-	-- A table with entries that have key (triggering key) and func
-	-- (function to run) properties.
-	_hotkeys = {},
-
-	visible = false,
-
-	new = function (self, obj)
-		obj = self:extend(obj or {})
-
-		obj:add('f', function() the.app:toggleFullscreen() end)
-		obj:add('p', function()
-			the.view.active = not the.view.active
-			if the.view.active then
-				the.view:tint()
-			else
-				the.view:tint(0, 0, 0, 200)
-			end
-		end)
-		obj:add('q', love.event.quit)
-		if debugger then obj:add('r', debugger.reload) end
-		obj:add('s', function() the.app:saveScreenshot('screenshot.png') end)
-
-		if obj.onNew then obj.onNew() end
-		return obj
-	end,
-
-	add = function (self, key, func)
-		table.insert(self._hotkeys, { key = key, func = func })
-	end,
-
-	update = function (self, elapsed)
-		local modifiers = (self.modifiers == nil)
-
-		if not modifiers then
-			modifiers = true
-
-			for _, key in pairs(self.modifiers) do
-				if not the.keys:pressed(key) then
-					modifiers = false
-					break
-				end
-			end
-		end
-
-		if modifiers then
-			for _, hotkey in pairs(self._hotkeys) do
-				if the.keys:justPressed(hotkey.key) then
-					hotkey.func(hotkey.key)
-				end
-			end
-		end
-	end
-}

zoetrope/debug/shortcuts.lua

+-- Class: DebugShortcuts
+--
+-- This allows for debugging shortcuts that perform tasks that aid 
+-- debugging -- e.g. skipping to a certain level or making a player sprite
+-- invincible. These can be triggered by a hotkey at any time, or appear
+-- in a list in the debug console.
+--
+-- Out of the box:
+--		- Control-Alt-F toggles fullscreen
+--		- Control-Alt-Q quits the app.
+--		- Control-Alt-P deactivates the view.
+-- 		- Control-Alt-R reloads all app code from on disk.
+--		- Control-Alt-S saves a screenshot to the app's directory --
+--		  see https://love2d.org/wiki/love.filesystem for where this is.
+
+DebugShortcuts = DebugInstrument:extend
+{
+	width = 'narrow',
+	visible = false,
+
+	-- Property: modifiers
+	-- A table of modifier keys that must be held in order to activate
+	-- a debugging hotkey (set via <add()>). If you want hotkeys to
+	-- activate without having to hold any keys down, set this to nil.
+	modifiers = {'ctrl', 'alt'},
+
+	-- internal property: _shortcuts
+	-- A table with entries that have desc (long description), key
+	-- (triggering key) and func (function to run) properties.
+	_shortcuts = {},
+
+	-- internal property: _buttons
+	-- An ordered table of shortcut buttons.
+	_buttons = {},
+
+	onNew = function (self)
+		self.title.text = 'Shortcuts'
+
+		self:addShortcut('Fullscreen', 'f', function() the.app:toggleFullscreen() end)
+
+		self:addShortcut('Pause', 'p', function()
+			the.view.active = not the.view.active
+			if the.view.active then
+				the.view:tint()
+			else
+				the.view:tint(0, 0, 0, 200)
+			end
+		end)
+
+		self:addShortcut('Quit', 'q', love.event.quit)
+
+		if self.reload then
+			self:addShortcut('Reload Code', 'r', self.reload)
+		end
+
+		self:addShortcut('Save Screenshot', 's', function()
+			if debugger.console.visible then
+				debugger.hideConsole()
+				the.view.timer:after(0.05, bind(the.app, 'saveScreenshot', 'screenshot.png'))
+				the.view.timer:after(0.1, debugger.showConsole)
+			else
+				the.app:saveScreenshot('screenshot.png')
+			end
+		end)
+
+		debugger.addListener(bind(self, 'listen'))
+		debugger.addShortcut = function (desc, key, func) self:addShortcut(desc, key, func) end
+		debugger.setShortcutModifiers = function (...) self.modifiers = {...} end
+	end,
+
+	addShortcut = function (self, desc, key, func)
+		table.insert(self._shortcuts, { desc = desc, key = key, func = func })
+
+		local label = desc
+
+		if key then
+			label = label .. ' (' .. key .. ')'
+		end
+
+		local button = DebugInstrumentButton:new
+		{
+			label = label,
+			onMouseUp = func
+		}
+
+		button.label.font = 11
+		button.label.y = 6
+		button.background.height = 24
+		
+		self:add(button)
+		table.insert(self._buttons, button)
+
+		self.contentHeight = #self._buttons * (DebugInstrumentButton.height + self.spacing) + 2 * self.spacing
+
+		self.visible = true
+	end,
+
+	listen = function (self)
+		local modifiers = (self.modifiers == nil)
+
+		if not modifiers then
+			modifiers = true
+
+			for _, key in pairs(self.modifiers) do
+				if not the.keys:pressed(key) then
+					modifiers = false
+					break
+				end
+			end
+		end
+
+		if modifiers then
+			for _, shortcut in pairs(self._shortcuts) do
+				if the.keys:justPressed(shortcut.key) then
+					shortcut.func(shortcut.key)
+				end
+			end
+		end
+	end,
+
+	onResize = function (self, x, y, width, height)
+		y = y + self.spacing 
+		x = x + self.spacing
+		width = width - self.spacing * 2
+
+		for _, spr in pairs(self._buttons) do
+			spr.x = x
+			spr.y = y
+			spr.background.width = width
+			spr.label.width = width
+		
+			y = y + DebugInstrumentButton.height + self.spacing
+		end
+
+	end
+}

zoetrope/init.lua

 if DEBUG then
 	require 'zoetrope.debug.instrument'
 	require 'zoetrope.debug.console'
-	require 'zoetrope.debug.hotkeys'
 	require 'zoetrope.debug.locals'
+	require 'zoetrope.debug.shortcuts'
 	require 'zoetrope.debug.stack'
 	require 'zoetrope.debug.stepper'
 	require 'zoetrope.debug.watch'