Advertisement
Hendrix000007

Birds app

Oct 17th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.71 KB | None | 0 0
  1. --PLAYGAME START SCENE.
  2. display.setStatusBar(display.HiddenStatusBar)
  3.  
  4. require( "sqlite3" )
  5. local widget = require("widget")
  6. birds = require("Classes.ButtonsBirds")
  7. --LOCALS FWD
  8. local composer = require "composer"
  9. local scene = composer.newScene()
  10. local path = system.pathForFile( "Birds.sqlite", system.ResourceDirectory )
  11. local imagePath = "Images/Birds/"
  12. local bgImage = imagePath .. "bgTheme.jpg"
  13. local text, themeText, bg, t, count, bgItem, bg, colorConverter, tableView, leftRib, woodPanel, butNext, butBack
  14. local leftIndent = 50
  15.  
  16. local dbTable = {}
  17. local rowBg = {}
  18. local rowName = {}
  19. local rowLatin = {}
  20. local playButton = {}
  21. local onRowRender = {}
  22. local onRowTouch = {}
  23. removeGroup = {}
  24. updateSql = {}
  25. if not sql then sql = "SELECT * FROM  birds" print("sql is inited for the first time") end
  26.  
  27. local _W, _H         = display.contentWidth, display.contentHeight
  28. local group          = {}
  29. local containerGroup = display.newGroup( )
  30. local dashboardGroup = display.newGroup( )
  31.  
  32. --GLOBALS FWD
  33. local db                = sqlite3.open( path )
  34. _G.specPool   = {}
  35. _G.catText = "Trykk på en av knappene.."
  36.  
  37. -- FUNCTIONS
  38. local function onSystemEvent( event )
  39.     if ( event.type == "applicationExit" ) then
  40.         db:close()
  41.     end
  42. end
  43.  
  44. function removeRows()
  45.   if #dbTable >= 1 then
  46.        for i = 1, #dbTable do
  47.         tableView:removeSelf(i)
  48.       end
  49.   end
  50. end
  51.  
  52. local function colorConverter(r, g, b, a)
  53.    local colorR, colorG, colorB, alphA
  54.       colorR = r/256; colorG = g/256; colorB = b/256
  55.       alphA = a or nil
  56.    return colorR, colorG, colorB, alphA
  57. end
  58.  
  59. function updateCatText()
  60.     if _G.debug == 1 then print("** updateCatText: \n\n") end           -- DEBUG
  61.     print(_G.catText)
  62.     if themeText then display.remove( themeText) end
  63.     themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 34 )
  64.     themeText.anchorX, themeText.anchorY = 0, 0
  65.     themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 20
  66.     themeText:setFillColor( colorConverter(236, 210, 135, 1) )
  67.  
  68.     return true
  69. end
  70.  
  71. function updateSql(event)
  72.   sql = event
  73.   event = sql
  74. -- load the data into a table array
  75.     if _G.debug == 1 then print("** updateSql: \n\n") end           -- DEBUG
  76.   count = 0
  77. -- sql = sql or print("No SQL statement present")
  78.  
  79.     print("sql = ", event)
  80.       for row in db:nrows(sql) do
  81.          count = count +1
  82.          dbTable[count]={}
  83.          dbTable[count].NAME = row.NAME
  84.          dbTable[count].LATIN = row.LATIN
  85.          dbTable[count].INFO = row.INFO
  86.       end
  87.  
  88.  tableView:deleteAllRows()
  89.     for i = 1, #dbTable do
  90.         local isCategory = false
  91.         local rowHeight = 70
  92.         local rowColor = {
  93.             default = { colorConverter(114, 72, 30, 0) },
  94.         }
  95.         local lineColor = { colorConverter(20, 20, 0, 0) }
  96.  
  97.         -- Insert the row into the tableView
  98.         tableView:insertRow
  99.         {
  100.           rowHeight = rowHeight,
  101.           rowColor = rowColor,
  102.           lineColor = lineColor,
  103.         }
  104. --        tableView:reloadData()
  105.    
  106.  
  107.     end
  108.   return true
  109. end
  110.  
  111. function removeGroup()
  112.       for i = 1, #group do
  113.          print("Before removing from group: " .. #group)
  114.          group[i]:removeSelf()
  115.          group[i] = nil
  116.          print("Removed: " .. #group)
  117.       end
  118.    return true
  119. end
  120.  
  121. local function birdSpec(self, event)
  122.    if event.phase == "began" then
  123.    if _G.debug == 1 then print("** birdSpec: \n\n") end             -- DEBUG
  124.   --  composer.setVariable( "birdspec", {"name"=self.NAME} )
  125.       _G.specPool.name  = self.specName
  126.       _G.specPool.latin = self.specLatin
  127.       _G.specPool.info  = self.specInfo
  128.       print("playGame-birdSpec returns _G.specPool.name like: ", self.specName, " parsed to self.specName")
  129.       print("playGame-birdSpec returns _G.specPool.name like: ", _G.specPool.name)
  130.  
  131.  --     print(self.specIngress)
  132. --      print(self.specInfo)
  133.  --     updateSql()
  134.       print("going over til spec page now...")
  135.       composer.gotoScene( "playSpecScene" )
  136.    elseif event.phase == "cancelled" or event.phase == "ended" then
  137.    end
  138.    return true
  139. end
  140.  
  141. function onRowRender( event )
  142.    if _G.debug == 1 then print("** onRowRender: \n\n") end          -- DEBUG
  143.  
  144.     local phase = event.phase
  145.     local row = event.row
  146.     print("**************** Row rendered")
  147.     row.bg = display.newRoundedRect( 0, 0, display.contentWidth -48, 50, 12 )
  148.     row.bg.strokeWidth = 0
  149.     row.bg:setFillColor( colorConverter(236, 210, 135, 1) )
  150.     row.bg.anchorX = 1
  151.     row.bg.anchorY = 0
  152.     row.bg.x = display.contentWidth
  153.     row.bg.y = 10
  154.     row:insert( row.bg )
  155.  
  156.     row.title = display.newText( row, dbTable[row.index].NAME, 0, 0, _G.msgFont , 24 )
  157.     row.title.x = leftIndent + 10
  158.     row.title.anchorX = 0
  159.     row.title.y = row.contentHeight * 0.15+ row.bg.y
  160.     row.title:setFillColor( colorConverter(114, 72, 30, 1) )
  161.  
  162.     row.ingress = display.newText( row, dbTable[row.index].LATIN, 0, 0, _G.msgFont , 14 )
  163.     row.ingress.x = leftIndent + 10
  164.     row.ingress.anchorX = 0
  165.     row.ingress.y = row.title.y +20
  166.     row.ingress:setFillColor( colorConverter(114, 72, 30, 1) )
  167.  
  168.     --local mask = graphics.newMask( "imageMask.png" )
  169.  
  170.     row.image = display.newImageRect(row, tostring(imagePath .. dbTable[row.index].NAME) .. ".jpg", 45,45 )
  171.     row.image:addEventListener( "touch", row.image)
  172.     row.image.touch = birdSpec
  173.     row.image.anchorX = 1
  174.     row.image.anchorY = 0.25
  175.     row.image.x = display.viewableContentWidth - row.image.width/2
  176.     row.image.y = row.title.y
  177.  
  178.     -- PARSE VARIABLE TO THE GLOBAL SPECPOOL TABLE
  179.     row.image.specName = dbTable[row.index].NAME
  180.     row.image.specLatin = dbTable[row.index].LATIN
  181.     row.image.specInfo = dbTable[row.index].INFO
  182. print("**************** Row rendered")
  183.  
  184. end
  185.  
  186. -- Handle touches on the row
  187. function onRowTouch( event )
  188.     local phase = event.phase
  189.  
  190.     if "press" == phase then
  191.     native.showAlert(dbTable[event.target.index].NAME, "har det latinske navnet " .. dbTable[event.target.index].LATIN, "OKAY")
  192.         print( event.target.index )
  193.     end
  194. end
  195.  
  196.  
  197. -- "scene:create()"
  198. function scene:create( event )
  199.    local sceneGroup = self.view
  200.       if _G.debug == 1 then print("** playGame--scene:create: \n\n") end            -- DEBUG
  201.  
  202.    --Bird.group = Bird.group
  203.     bg = display.newImageRect( bgImage, _W, _H, 375, 667, true )
  204.     bg.anchorX, bg.anchorY = 0, 0
  205.     bg.alpha = 1
  206.     sceneGroup:insert(bg)
  207.  
  208.     leftRib = display.newImageRect( "leftRib.png", 48, 793, true )
  209.     leftRib.anchorX, leftRib.anchorY = 0, 0
  210.     leftRib.alpha = 1
  211.     sceneGroup:insert(leftRib)
  212.  
  213.     woodPanel = display.newImageRect( "woodPanel.png", 375, 131, true )
  214.     woodPanel.x, woodPanel.y = display.contentWidth/2, display.contentHeight
  215.     woodPanel.anchorX, woodPanel.anchorY = 0.5, 1
  216.     woodPanel.alpha = 1
  217.     sceneGroup:insert(woodPanel)
  218.  
  219.     themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 18 )
  220.     themeText.anchorX, themeText.anchorY = 0, 0
  221.     themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 30
  222.     themeText:setFillColor( colorConverter(236, 210, 135, 1) )
  223.     sceneGroup:insert(themeText)
  224. end
  225.  
  226. -- "scene:show()"
  227. function scene:show( event )
  228.    local sceneGroup = self.view
  229.    local phase = event.phase
  230.    if ( phase == "will" ) then
  231.    if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end            -- DEBUG
  232.    composer.removeHidden( )        
  233.    elseif ( phase == "did" ) then
  234.    if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end            -- DEBUG
  235. --audio.crossFadeBackground( "bgSound.mp3", 1 )
  236. --birds:initButtons()
  237.  
  238. -- load the data into a table array
  239.  if tableView and tableView:getNumRows()>1 then tableView:deleteAllRows() end
  240.     count = 0
  241.     print("Sql in playGame: scene show ", sql)
  242.     sql = sql or "SELECT * FROM Birds"
  243.       for row in db:nrows(sql) do
  244.          count = count +1
  245.          dbTable[count]={}
  246.          dbTable[count].NAME = row.NAME
  247.          dbTable[count].LATIN = row.LATIN
  248.          dbTable[count].INFO = row.INFO
  249.       end
  250.  
  251.     tableView = widget.newTableView
  252.     {
  253.         top = 0,
  254.         width = _W+1,
  255.         height = display.contentHeight,
  256.         hideBackground = true,
  257.         isBounceEnabled = true,
  258.         listener = tableViewListener,
  259.         onRowRender = onRowRender,
  260.     }
  261.  
  262.     for i = 1, #dbTable do
  263.         local isCategory = false
  264.         local rowHeight = 70
  265.         local rowColor = {
  266.             default = { colorConverter(114, 72, 30, 0) },
  267.         }
  268.         local lineColor = { colorConverter(20, 20, 0, 0) }
  269.  
  270.         -- Insert the row into the tableView
  271.         tableView:insertRow
  272.         {
  273.           --  isCategory = isCategory,
  274.           rowHeight = rowHeight,
  275.           rowColor = rowColor,
  276.           lineColor = lineColor,
  277.         }
  278.  
  279.     end
  280.     sceneGroup:insert(tableView)
  281.  
  282.   end
  283.  
  284.  
  285. end
  286.  
  287. -- "scene:hide()"
  288. function scene:hide( event )
  289.    local sceneGroup = self.view
  290.    local phase = event.phase
  291.    if ( phase == "will" ) then 
  292.    elseif ( phase == "did" ) then
  293.    if _G.debug == 1 then print("** playGame--scene:hide: \n\n") end             -- DEBUG
  294.  if tableView and tableView:getNumRows()>1 then tableView:deleteAllRows()
  295.   print("TableView iterated and deleted from scene hide in playGame")
  296. end
  297.  
  298.  
  299. _G.catText = ""
  300. updateCatText()
  301.  
  302.    end
  303. end
  304.  
  305. function scene:destroy( event )
  306.     local sceneGroup = self.view
  307.  
  308. end
  309.  
  310. -- Listener setup
  311. scene:addEventListener( "create", scene )
  312. scene:addEventListener( "show", scene )
  313. scene:addEventListener( "hide", scene )
  314. scene:addEventListener( "destroy", scene )
  315.  
  316. return scene
  317.  
  318. -- CLASS: buttonBirds recided in  Classes folder:
  319. Bird = {}
  320. Bird.pathForBehaviours = "Assets/Birds/Behaviour/"
  321. Bird.group =  display.newGroup( )
  322. Bird.group.x = 0
  323. Bird.group.anchorX = 0
  324. group = group
  325. sql = sql
  326. updateSql = updateSql
  327. themeText = themeText
  328. tableView = tableView
  329. local imgW, imgH = 48, 48
  330. Bird.group.y = (display.contentHeight - imgH)
  331. local behaviourCounter = 0
  332. --BIRDS TABLE
  333. Bird.buttons = {}
  334. --BEHAVIOURS TABLE
  335. Bird.buttons.behavior = {
  336. {
  337.       name                 =  "SELECT * FROM  birds WHERE PATTERN = 'Spettete'",
  338.       sound                =  "behaviour.mp3" or nil,
  339.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "behaviour.png", imgW, imgH ),
  340.       behavior             =  "Opførsel",
  341.       category             =  "Behavior",
  342. },
  343. {
  344.       name                 =  "SELECT * FROM  birds WHERE PATTERN = 'Stripete'",
  345.       sound                =  "birdHouse.mp3" or nil,
  346.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "birdHouse.png", imgW, imgH ),
  347.       behavior             =  "Farger",
  348.       category             =  "Behavior",
  349. },
  350. {
  351.       name                 =  "SELECT * FROM  birds WHERE PATTERN = 'Flekkete'",
  352.       sound                =  "catchingInsectsInFlight.mp3" or nil,
  353.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "catchingInsectsInFlight.png", imgW, imgH ),
  354.       behavior             =  "Form",
  355.       category             =  "Behavior",
  356. },
  357. {
  358.       name                 =  "SELECT * FROM  birds WHERE SIZE = 'S'",
  359.       sound                =  "climbingTree.mp3" or nil,
  360.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "climbingTree.png", imgW, imgH ),
  361.       behavior             =  "Familie",
  362.       category             =  "Behavior",
  363. },
  364. }
  365.  
  366. function Bird:engineResponse(event)
  367.     if ( event.phase == "began" ) then
  368.       if _G.debug == 1 then print("** ButtonBirds Class--engineResponse: \n\n") end      -- DEBUG
  369.  
  370.         display.getCurrentStage():setFocus( event.target )
  371.         print( "Touch started on: " .. self.id)
  372.  
  373.         sql = self.id
  374.  
  375.         updateSql(sql)
  376.  
  377.         print(self.catText)
  378.         _G.catText = self.catText
  379.         updateCatText()
  380.  
  381.       elseif event.phase == "ended" or event.phase == "cancelled" then
  382.         display.getCurrentStage():setFocus(nil)
  383.         print( "Touch ended" )
  384.     end
  385.   return true  
  386. end
  387.  
  388. function Bird:initButtons(event)
  389.   if _G.debug == 1 then print("** ButtonBirds Class--initButtons: \n\n") end      -- DEBUG
  390.    for k,v in ipairs (Bird.buttons.behavior) do
  391.       behaviourCounter = behaviourCounter + 1
  392.       local img
  393.       img = v['imageObj']
  394.       img.x = (imgW + 5) * behaviourCounter
  395.       img.anchorX = 0
  396.       img.anchorY = 0.20
  397.       img.id = v['name']
  398.  
  399.       img:addEventListener( "touch", img )
  400.       img.touch = Bird.engineResponse
  401.       Bird.group:insert(img)
  402.       img.catText = v['behavior']
  403.       img.sql = v['name']
  404.       Bird.category = v['category']
  405.  
  406.       print(Bird.category)
  407.    end
  408.    print( "Bird.group has "..Bird.group.numChildren .." children")
  409.    return true
  410. end
  411.  
  412.  
  413.  
  414. Bird:initButtons()
  415. return Bird
  416. ------------------------
  417. ------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement