Skip to content

Instantly share code, notes, and snippets.

@canoad56
Created December 18, 2011 19:11
Show Gist options
  • Save canoad56/1494183 to your computer and use it in GitHub Desktop.
Save canoad56/1494183 to your computer and use it in GitHub Desktop.
edit info for player data
Hi Peach;
Here is the file I am working on. It is directly from the Widget demo but I have changed it quit a bit to accommodate my own stuff. It very very experimental at the moment.
Trying to display a photo from the library as a thumbnail with the following code:
local baseDir = system.DocumentsDirectory
local img = display.newImage("photo.jpg",baseDir)
thumbnail:insert( img, true )
It works but I really don't want to save any file just display the photo. I am using code that I found at Corona resources and it gives an error if I comment the first two statements. Of course, I am also commenting the same two statements in the listener "pic_photo" when I try it.
Secondly, the photo image is persistent and there are two, one at the left hand origin and the other where I want it as a numb nail; I can't seem to delete it from the display in any of the other two tab bars. I have tried adding in removeself() but its not working.
To run it, place it in the Widget demo folder, it uses all the assets stored there.
Thanks for your help in advance!
--[[
Players Program:
edit player data module
revised: 12.19.11
--]]
display.setStatusBar( display.DefaultStatusBar )
local widget = require "widget"
widget.setTheme( "theme_ios" )
-- create display groups
local demoGroup = display.newGroup()
local tabBarGroup = display.newGroup()
-- forward declarations for widgets (so they can be removed manually)
local list, scrollBox, backButton, doneButton, pickerButton, picker
-- private functions
local function clearGroup( g )
-- remove all widgets (they must be removed manually)
if list then list:removeSelf(); list = nil; end
if scrollBox then scrollBox:removeSelf(); scrollBox = nil; end
if backButton then backButton:removeSelf(); backButton = nil; end
if doneButton then doneButton:removeSelf(); doneButon = nil; end
if pickerButton then pickerButton:removeSelf(); pickerButton = nil; end
if picker then picker:removeSelf(); picker = nil; end
if thumbnail then thumbnail:removeSelf(); thumbnail = nil; end -- cannot remove photo !!
if img then img:removeSelf(); img = nil; end
-- clear the contents of a group, but don't delete the group
for i=g.numChildren,1,-1 do
display.remove( g[i] )
end
end
-- create a gradient for the top-half of the toolbar
local toolbarGradient = graphics.newGradient( {168, 181, 198, 255 }, {139, 157, 180, 255}, "down" )
-- create toolbar to go at the top of the screen
local titleBar = widget.newTabBar{
top = display.statusBarHeight,
topGradient = toolbarGradient,
bottomFill = { 117, 139, 168, 255 },
height = 44
}
-- position the demo group underneath the toolbar
demoGroup.y = display.statusBarHeight + titleBar.height + 1
-- create embossed text to go above toolbar
local titleText = display.newEmbossedText( "edit your info", 0, 0, native.systemFontBold, 20, { 255 } )
titleText:setReferencePoint( display.CenterReferencePoint )
titleText.x = display.contentWidth * 0.5
titleText.y = titleBar.y + titleBar.height * 0.5
--
-- initialize player info
--
plyr_name = system.getInfo( "name" )
plyr_status = "single"
plyr_age = 21
plyr_sex = "male"
plyr_opt = "none"
plyr_pic = "yes"
plyr_dat = {}
plyr_dat[1] = plyr_name
plyr_dat[2] = plyr_status
plyr_dat[3] = plyr_sex
plyr_dat[4] = plyr_age
plyr_dat[5] = plyr_opt
plyr_dat[6] = plyr_pic
cat = {}
cat = {"name: ","marital s:","sex: ","age: ","opt: ","show pic:"}
-- tab button listeners
local function edit_info( event )
clearGroup( demoGroup )
-- create tableView widget
list = widget.newTableView{
width = 320,
height = 366,
maskFile = "assets/mask-320x366.png"
}
-- insert widget into demoGroup
demoGroup:insert( list.view )
-- onEvent listener for the tableView
local function onRowTouch( event )
local row = event.target
local rowGroup = event.view
if event.phase == "press" then
if not row.isCategory and row.title then
row.title.text = plyr_dat[event.index]
row.title:setReferencePoint( display.CenterLeftReferencePoint )
row.title:setTextColor( 100,100,255 )
row.title.x = 150
end
elseif event.phase == "release" then
if not row.isCategory then
row.reRender = true
print( plyr_dat[event.index])
-- Prompt the user on the terminal
io.write( "Enter ",cat[event.index] )
-- User enters answer on the terminal
local answer = io.read()
-- Display the answer on the terminal
io.write( "You entered: ", answer, "\n" )
plyr_dat[event.index] = answer
end
end
return true
end
-- onRender listener for the tableView
local function onRowRender( event )
local row = event.target
local rowGroup = event.view
local textFunction = display.newRetinaText
if row.isCategory then textFunction = display.newEmbossedText; end
--
-- combine both catagory and player data
--
C = cat[event.index].." "..plyr_dat[event.index]
row.title = textFunction( C, 12, 0, native.systemFontBold, 16 )
row.title:setReferencePoint( display.CenterLeftReferencePoint )
row.title.y = row.height * 0.5
if not row.isCategory then
row.title.x = 15
row.title:setTextColor( 0 )
end
-- must insert everything into event.view:
rowGroup:insert( row.title )
end
-- Add 6 rows, and two categories to the tableView:
for i=1,6 do
local rowHeight, rowColor, lineColor, isCategory
-- insert the row into the tableView widget
list:insertRow{
onEvent=onRowTouch,
onRender=onRowRender,
height=rowHeight,
isCategory=isCategory,
rowColor=rowColor,
lineColor=lineColor
}
end
end
local function pic_photo ( event )
local img = event.target
local baseDir = system.DocumentsDirectory
display.save( img, "photo.jpg", baseDir )
end -- pic_photo end
local function newphoto ( event )
clearGroup( demoGroup )
-- choose photo library for picture
media.show( media.SavedPhotosAlbum, pic_photo )
-- Create thumbnail
local thumbnail = display.newGroup()
local baseDir = system.DocumentsDirectory
local img = display.newImage("photo.jpg",baseDir)
thumbnail:insert( img, true )
img:scale( 0.5, 0.5 )
local r = 5
local border = display.newRoundedRect(0,0, img.contentWidth + 2*r, img.contentHeight + 2*r, r )
border:setFillColor( 0,100,255,100 )
thumbnail:insert( 1, border, true )
thumbnail:translate( 0.5*display.contentWidth, 0.5*display.contentHeight )
end
local function edit_age( event )
clearGroup( demoGroup )
-- set starting indexes for picker columns
local monthIndex, ageIndex, yearIndex = 5, 5, 56
-- create grey background
local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight-demoGroup.y )
bg:setFillColor( 152, 152, 156, 255 )
demoGroup:insert( bg )
-- create a label to show selected date
local ageText = display.newRetinaText( "age", 0, 0, native.systemFontBold, 22 )
ageText:setTextColor( 0 )
ageText:setReferencePoint( display.CenterReferencePoint )
ageText.x = display.contentWidth * 0.5
ageText.y = 100
demoGroup:insert( ageText )
-- onRelease listener for pickerButton
local function onButtonRelease( event )
-- onRelease listener for back button
local function onBackRelease( event )
-- remove the picker, the done button, and the back button (event.target)
if picker then picker:removeSelf(); picker = nil; end
if doneButton then doneButton:removeSelf(); doneButton = nil; end
if backButton then backButton:removeSelf(); backButton = nil; end
return true
end
-- onRelease listener for the done button
local function onDoneRelease( event )
-- extract pickerwheel column values
local dateColumns = picker:getValues()
local age = dateColumns[1].value
print("you entered: ", age);
-- mark column index so we can start in same position next time
-- monthIndex = dateColumns[1].index
ageIndex = dateColumns[1].index
-- yearIndex = dateColumns[3].index
-- change the dateText's label
ageText.text = age
-- remove the picker, the done button, and the back button (event.target)
if picker then picker:removeSelf(); picker = nil; end
if doneButton then doneButton:removeSelf(); doneButton = nil; end
if backButton then backButton:removeSelf(); backButton = nil; end
return true
end
-- create 'back' button to be placed on toolbar
backButton = widget.newButton{
label = "Back",
left = 5, top = 28,
style = "backSmall",
onRelease = onBackRelease
}
-- create 'done' button to be placed on toolbar
doneButton = widget.newButton{
label = "Done",
left = 255, top = 28,
style = "blue2Small",
onRelease = onDoneRelease
}
-- set up the pickerWheel's columns
local columnData = {}
columnData[1] = {}
for i=1,55 do
columnData[1][i] = i
end
columnData[1].alignment = "center"
columnData[1].width = 60
columnData[1].startIndex = ageIndex
-- create pickerWheel widget
picker = widget.newPickerWheel{
id="pickerWheel",
top=480, --258,
font=native.systemFontBold,
columns=columnData,
}
-- slide the picker-wheel up
transition.to( picker, { time=250, y=258, transition=easing.outQuad } )
end
-- create button to show pickerWheel
pickerButton = widget.newButton{
label = "enter age",
left = 22, top = 285,
onRelease = onButtonRelease
}
-- insert picker button into demoGroup
demoGroup:insert( pickerButton.view )
end
-- create buttons table for the tab bar
local tabButtons = {
{
label="edit your data",
up="assets/tabIcon.png",
down="assets/tabIcon-down.png",
width=32, height=32,
onPress=edit_info,
selected=true
},
{
label="select picture",
up="assets/tabIcon.png",
down="assets/tabIcon-down.png",
width=32, height=32,
onPress=newphoto,
},
{
label="edit age",
up="assets/tabIcon.png",
down="assets/tabIcon-down.png",
width=32, height=32,
onPress=edit_age,
}
}
-- create a tab-bar and place it at the bottom of the screen
local demoTabs = widget.newTabBar{
top=display.contentHeight-50,
buttons=tabButtons
}
-- insert tab bar into display group
tabBarGroup:insert( demoTabs.view )
-- start off in the tableView tab
edit_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment