Skip to content

Instantly share code, notes, and snippets.

@reefwing
Created September 30, 2012 00:54
Show Gist options
  • Save reefwing/3805522 to your computer and use it in GitHub Desktop.
Save reefwing/3805522 to your computer and use it in GitHub Desktop.
Tutorial 18 - Saving and Loading Complicated Tables (Part 2)
--# MenuBar
-- MenuBar Class
--
-- Developed by @dave1707
-- 16th September 2012
--
-- Version 1.1
--
-- Modified by: Reefwing Software
--
-- Modifications:
--
-- 1. Menu constantly visible
-- 2. Functions triggered only once not repeatably
MenuBar=class()
local selected
function MenuBar:init(x,y,w,h,description,table)
self.x=x
self.y=y
self.w=w
self.h=h
self.d=description
self.pointer=table
self.error=MenuBar:error()
end
function MenuBar:on(t)
if t.y>HEIGHT-30 and table1==nil then
pointer1=b1tab
end
end
function MenuBar:check(t)
local x
selected=false
if pointer1~= nil then
for x=1,#pointer1 do
pointer1[x]:touched(t)
end
end
if pointer2 ~= nil then
pointer3=nil
for x=1,#pointer2 do
pointer2[x]:touched(t)
if pointer3~= nil then
return
end
end
end
if not selected then
pointer1=b1tab
pointer2=nil
end
end
function MenuBar:menu1()
local x
if type(pointer1)== "table" then
for x=1,#pointer1 do
pointer1[x]:draw()
end
end
if type(pointer2)== "table" then
for x=1,#pointer2 do
pointer2[x]:draw()
end
end
if type(pointer3)== "function" then
--pointer3()
end
end
function MenuBar:draw()
pushStyle()
rectMode(CENTER)
fill(0,0,255)
rect(self.x,self.y,self.w,self.h)
fill(255)
text(self.d,self.x,self.y)
popStyle()
end
function MenuBar:error()
--fill(255)
--text("pointer to table or function not setup.",WIDTH/2,HEIGHT/2)
end
function MenuBar:touched(t)
if t.state==ENDED then
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 then
if t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
selected=true
if type(self.pointer)=="table" then
pointer2=self.pointer
elseif type(self.pointer)=="function" then
pointer3=self.pointer
pointer3()
pointer1=b1tab
pointer2=nil
else
pointer3=self.error
end
end
end
end
end
--# 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
--# 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