[LIBRARY] tt.lua - Text triggers for lua!
Posted: Mon Oct 11, 2021 12:43 pm
I'm sure there is a more proper name for what this does, I just don't know what it is, Or how to look it up. So i'm just calling it text triggers. This library helps you call a certain function when a certain sequence of keys is pressed. Just like old video game cheat codes.
Proper documentation on github, Bad documentation below.
Load
Update buffer
tt:updateBuffer() can also go in textinput or mousepressed. Or wheverever you want, Just pass it something that makes sense.
Update internal timer
This is optional, But required if you want to take advantage of the 'buffer timeout' and 'wait for timeout' features.
Define some triggers
Now when you type "hello" or do the konami code, Something might happen if you've set it up right.
Demo: It's just a single file so i'll just drop it here
Proper documentation on github, Bad documentation below.
Load
Code: Select all
tt = require "tt"
Code: Select all
function love.keypressed(key)
tt:updateBuffer(key)
end
Update internal timer
Code: Select all
function love.update(dt)
tt:update(dt)
end
Define some triggers
Code: Select all
tt:new("escape", love.event.push, "quit")
tt:new("hello", print, "Hello world!")
tt:new("upupdowndownleftrightleftrightba", something_neat)
Demo: It's just a single file so i'll just drop it here
Code: Select all
function love.load()
-- Loading and configure tt
tt = require "tt"
tt:setWaitForTimeout(true)
tt:setBufferTimeout(0.5)
-- Defining some triggers
tt:new("escape", love.event.push, "quit")
tt:new("hello", print, "Hello world!")
tt:new("mouse1mouse1", print, "Double click")
end
function love.update(dt)
-- Updating the internal timer
tt:update(dt)
end
function love.draw()
-- Printing out the buffer
love.graphics.print("Current buffer: "..tt:getBuffer(), 12, 12)
end
function love.keypressed(key)
-- Updating the buffer
tt:updateBuffer(key)
end
function love.textinput(t)
-- In löve, You can also use 'textinput' to update the buffer.
-- That way you can have triggers with capital letters, but a trigger like "escape"
-- wont be triggered when the escape key is pressed. Only when you type it out.
-- tt:updateBuffer(t)
end
function love.mousepressed(x, y, b)
-- You could also update the buffer in mousepressed, For example to detect a double click
-- Here i send the buffer a string like "mouse1" instead of just passing it "b" directly
-- This is so the "double click" trigger doesnt also trigger if you just type "11"
local char = "mouse"..b
tt:updateBuffer(char)
end