-
-
Save dermotbalson/5352294 to your computer and use it in GitHub Desktop.
30. Text input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
-- Main | |
function setup() | |
setupParameters() | |
end | |
--this function shows how to include/exclude parameters that show on the left of screen | |
function setupParameters(showTextClose) | |
parameter.action("Show_keyboard",showkeyb) | |
if showTextClose then | |
parameter.action("Hide_keyboard",getText) | |
end | |
end | |
function showkeyb() --call for keyboard to show | |
inputText="" | |
--initialise keyboard class (see Input tab for explanation) | |
kb=Input(true,getText) --ask for input to complete on pressing Enter | |
kb:Open() | |
setupParameters(true) | |
end | |
function getText() --this is called by the Input class when we're done | |
inputText=kb.text --get final text | |
print("Text=",inputText) | |
kb=nil --close keyboard class | |
setupParameters(false) | |
end | |
function keyboard(key) --traps keyboard entries, this must be here | |
kb:keyboard(key) --pass through to Input class | |
end | |
function draw() | |
background(40, 40, 50) | |
if kb then | |
textMode(CORNER) | |
strokeWidth(2) | |
fontSize(24) | |
if kb.state~=nil then --still entering (if kb.state is not nil, that is) | |
kb:draw() --capture any new keystrokes | |
fill(255) | |
text("Text so far: "..kb.text,100,300) | |
end | |
end | |
end | |
--# Input | |
--[[ | |
This class provides a simple keyboard input feature | |
Usage: | |
The class is initialised like so | |
kb=Input(c,fn) | |
where c=true if Enter closes the kebroard | |
and fn is the function name to call when it's all over | |
if you want to show progressive input on the screen as it's typed, then you need to | |
* include the function keyboard(key) in main, and call the keyboard function in this class from there | |
(exactly as it's been done in this project) | |
* in the main draw function, call the draw function in here, to update the text before printing it on screen | |
assuming you've started by saying | |
kb=Input(c,fn) | |
nothing will happen until you run kb:Open(), which will open the keyboard | |
While the keyboard is open, the class provides the following ways to access its data | |
kb.state="Open" if input is continuing, else it is nil | |
kb.text = text so far | |
Closing the keyboard is done either by calling kb:Close(), or, if you have told the class | |
to close when Enter is pressed, when the user presses enter | |
The class then calls the callback function named in the initialisation function above. | |
--]] | |
Input = class() | |
function Input:init(c,fn) | |
self.closeOnEnter=c --if true, keyboard will close when enter pressed | |
self.callback=fn --the function to run, back in Main, when we're done | |
end | |
function Input:Open() | |
self.state="Open" --so Main knows we're entering text | |
self.text="" | |
showKeyboard() | |
end | |
function Input:Close() | |
self.text=keyboardBuffer() | |
hideKeyboard() | |
self.state=nil --tells Main we're done | |
self.callback() --call the function that is waiting to hear from us | |
end | |
function Input:draw() | |
if self.newKey==true then --update text if new chars have been typed | |
self.text=keyboardBuffer() | |
self.newKey=false | |
end | |
end | |
function Input:keyboard(key) | |
--close keyboard if Enter is pressed and if we have been to told to stop editing text if this happens | |
if self.closeOnEnter and string.byte(key)==10 then self:Close() end | |
self.newKey=true --to tell the draw function there is new text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment