Created
September 30, 2012 00:54
-
-
Save reefwing/3805522 to your computer and use it in GitHub Desktop.
Tutorial 18 - Saving and Loading Complicated Tables (Part 2)
This file contains hidden or 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
--# TextBox | |
TextBox = class() | |
-- TextBox | |
-- ver. 1.1 | |
-- a control for basic string editing | |
-- ================================== | |
-- | |
-- This class is courtesy of the Spritely example App | |
-- included with Codea. | |
-- | |
-- Modified by Reefwing Software: | |
-- | |
-- 1. hasFocus boolean added to allow handling of multiple textboxes | |
-- 2. Touch function shows keyboard if touch is on the textbox | |
-- and sets hasFocus variable. | |
-- 3. Added self.h for hit detection | |
function TextBox:init(x, y, w, s) | |
self.x = x | |
self.y = y | |
self.w = w | |
self.text = s | |
self.blink = ElapsedTime | |
self.blinkstate = true | |
self.hasFocus = false | |
self.h = 25 | |
end | |
function TextBox:draw() | |
local x, w, h | |
pushStyle() | |
pushMatrix() | |
font("Futura-Medium") | |
textMode(CENTER) | |
fontSize(18) | |
rectMode(CORNER) | |
strokeWidth(1) | |
stroke(0, 0, 0, 255) | |
fill(228, 228, 228, 255) | |
translate(self.x, self.y) | |
rect(0, 0, self.w, 24) | |
stroke(255, 255, 255, 255) | |
--noFill() | |
rect(2, 2, self.w - 4, 20) | |
fill(22, 22, 22, 255) | |
text(self.text, self.w / 2, 12) | |
w, h = textSize(self.text) | |
if self.blink < ElapsedTime - 0.3 then | |
self.blink = ElapsedTime | |
self.blinkstate = not self.blinkstate | |
end | |
if self.blinkstate and self.hasFocus then | |
strokeWidth(2) | |
stroke(45, 45, 45, 255) | |
x = self.w / 2 + w / 2 + 2 | |
line(x, 3, x, 21) | |
end | |
popMatrix() | |
popStyle() | |
end | |
function TextBox:touched(touch) | |
-- Show keyboard and accept text for this textbox. | |
if pointInRect(touch.x, touch.y, self.x, self.y, self.w, self.h) then | |
if touch.state == BEGAN then | |
showKeyboard() | |
self.hasFocus = true | |
end | |
else | |
self.hasFocus = false | |
--hideKeyboard() | |
end | |
end | |
function TextBox:acceptKey(k) | |
if k ~= nil then | |
if string.byte(k) == nil then | |
if string.len(self.text) > 0 then | |
self.text = string.sub(self.text, | |
1, string.len(self.text) - 1) | |
end | |
end | |
self.text = self.text..k | |
end | |
end |
This file contains hidden or 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
--# ToString | |
-- | |
-- Converts an arbitrary data type into a string. Will recursively convert | |
-- tables. | |
-- | |
-- () param data The data to convert. | |
-- () param indent (optional) The number of times to indent the line. Default is 0. | |
-- | |
-- () return A string representation of a data, will be one or more full lines. | |
-- | |
-- function from www.seclists.org/nmap-dev/2008/q4/550 | |
function to_string(data, indent) | |
local str = "" | |
if (indent == nil) then | |
indent = 0 | |
end | |
-- Check the type | |
if(type(data) == "string") then | |
str = str .. (" "):rep(indent) .. data .. "\n" | |
elseif(type(data) == "number") then | |
str = str .. (" "):rep(indent) .. data .. "\n" | |
elseif(type(data) == "boolean") then | |
if(data == true) then | |
str = str .. "true" | |
else | |
str = str .. "false" | |
end | |
elseif(type(data) == "table") then | |
local i, v | |
for i, v in pairs(data) do | |
-- Check for a table in a table | |
if(type(v) == "table") then | |
str = str .. (" "):rep(indent) .. i .. ":\n" | |
str = str .. to_string(v, indent + 2) | |
else | |
str = str .. (" "):rep(indent) .. i .. ": " .. to_string(v, 0) | |
end | |
end | |
else | |
print("Error: unknown data type: %s", type(data)) | |
end | |
return str | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment