Advertisement
Hendrix000007

BirdAppComposer

Nov 5th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.12 KB | None | 0 0
  1. display.setStatusBar(display.HiddenStatusBar)
  2.  
  3. require( "sqlite3" )
  4. local widget = require("widget")
  5. birds = require("Classes.ButtonsBirds") -- THIS HANDLES THE BUTTONS THAT WILL BUILD
  6.                                         -- THE SQL STATEMENT THAT AGAIN WILL NARROW THE SEARCH.
  7. --LOCALS FWD
  8. local composer       = require "composer"
  9. local scene          = composer.newScene()
  10. local path               = system.pathForFile( "Birds.sqlite", system.ResourceDirectory )
  11. local db             = sqlite3.open( path )
  12. local _W, _H         = display.contentWidth, display.contentHeight
  13. local containerGroup = display.newGroup( )
  14. local dashboardGroup = display.newGroup( )
  15. local imagePath        = "Images/Birds/"
  16. local bgImage        = imagePath .. "bgTheme.jpg"
  17. local text, themeText, bg, t, count, bgItem, bg, colorConverter, tableView, leftRib, woodPanel, butNext, butBack
  18. local leftIndent     = 50
  19. local dbTable        = {}
  20. local rowBg          = {}
  21. local rowName        = {}
  22. local rowLatin       = {}
  23. local playButton     = {}
  24. local onRowRender    = {}
  25. local onRowTouch     = {}
  26. local funtionSqlView = {}
  27. local onSystemEvent  = {}
  28. local colorConverter = {}
  29. local birdSpec       = {}
  30.  
  31. --GLOBALS FWD
  32. mySqlView            = {} -- I CHOOSE TO HAVE THIS GLOBAL FOR NOW
  33. _G.specPool          = {}
  34. _G.catText           = "Trykk på en av knappene.."
  35. updateSql            = {}
  36.  
  37. -- FUNCTIONS
  38. function functionSqlView() -- THIS IS THE SQL STATEMENT BUILDER (YOU CAN SEE IN THE TERMINAL)... LAME NAME, I KNOW HAHA
  39.   tableView:deleteAllRows()
  40.  
  41.   local counter = 1
  42.   local res     = {}
  43.   local sep     = '"'      -- FOR LAZYNESS
  44.  
  45.   table.insert(res, "SELECT * FROM birds WHERE ")
  46.   for _,v in ipairs(mySqlView) do
  47.     if counter < #mySqlView then
  48.       table.insert( res, (v ..  " AND ")) -- I WILL DEAL WITH 'OR' LATER ;D
  49.     else
  50.       table.insert( res, v )
  51.     end
  52.     counter = counter + 1
  53.   end
  54.   sql = sep .. table.concat( res ) .. sep -- CONCATENATES THE WHOLE STATEMEMT STRING
  55.   print("@ 3: functionSqlView builded the sql statememt like this: ", sql)
  56.  
  57.  
  58. --[[       count = 0
  59.     print("Sql in functionSqlView: ", sql)
  60.  
  61.     for row in db:nrows(sql) do
  62.          count = count +1
  63.          dbTable[count]={}
  64.          dbTable[count].NAME = row.NAME
  65.          dbTable[count].LATIN = row.LATIN
  66.          dbTable[count].INFO = row.INFO
  67.          print("*--inside forloop in functionSqlView--*")
  68.       end
  69. --]]  
  70.  
  71.     for i = 1, #dbTable do
  72.         local isCategory = false
  73.         local rowHeight = 70
  74.         local rowColor = {
  75.             default = { colorConverter(114, 72, 30, 0) },
  76.         }
  77.         local lineColor = { colorConverter(20, 20, 0, 0) }
  78.  
  79.         -- Insert the row into the tableView
  80.         tableView:insertRow
  81.         {
  82.           --  isCategory = isCategory,
  83.           rowHeight = rowHeight,
  84.           rowColor = rowColor,
  85.           lineColor = lineColor,
  86.         }
  87.     end
  88. --tableView:reloadData()
  89.     print("NUM ROWS ", tableView:getNumRows())
  90.     return sql
  91.  
  92. end
  93.  
  94. function onSystemEvent( event )
  95.     if ( event.type == "applicationExit" ) then
  96.         db:close()
  97.     end
  98. end
  99.  
  100.  
  101. function colorConverter(r, g, b, a)
  102.    local colorR, colorG, colorB, alphA
  103.       colorR = r/256; colorG = g/256; colorB = b/256
  104.       alphA = a or nil
  105.    return colorR, colorG, colorB, alphA
  106. end
  107.  
  108. function updateCatText()
  109.     if _G.debug == 1 then print("** updateCatText: \n\n") end           -- DEBUG
  110.     print(_G.catText)
  111.     if themeText then display.remove( themeText) end
  112.     themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 34 )
  113.     themeText.anchorX, themeText.anchorY = 0, 0
  114.     themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 20
  115.     themeText:setFillColor( colorConverter(236, 210, 135, 1) )
  116.  
  117.     return true
  118. end
  119. --[[
  120. function updateSql(event)
  121. tableView:deleteAllRows() -- FIRST I LIKE TO REMOVE THE ROWS AND THAT WORKS OF COURSE
  122.  
  123.     print("Sql in updateSql function is: ", sql)
  124.  
  125.  
  126.     for i = 1, #dbTable do
  127.         local isCategory = false
  128.         local rowHeight = 70
  129.         local rowColor = {
  130.             default = { colorConverter(114, 72, 30, 0) },
  131.         }
  132.         local lineColor = { colorConverter(20, 20, 0, 0) }
  133.  
  134.         -- Insert the row into the tableView
  135.         tableView:insertRow
  136.         {
  137.           --  isCategory = isCategory,
  138.           rowHeight = rowHeight,
  139.           rowColor = rowColor,
  140.           lineColor = lineColor,
  141.         }
  142.     end
  143.  
  144. end
  145. --]]
  146. function birdSpec(self, event)
  147.    if event.phase == "began" then
  148.    if _G.debug == 1 then print("** birdSpec: \n\n") end             -- DEBUG
  149.       _G.specPool.name  = self.specName
  150.       _G.specPool.latin = self.specLatin
  151.       _G.specPool.info  = self.specInfo
  152.       composer.gotoScene( "playSpecScene" )
  153.    elseif event.phase == "cancelled" or event.phase == "ended" then
  154.    end
  155.    return true
  156. end
  157.  
  158.  
  159.  
  160.  
  161. -- "scene:create()"
  162. function scene:create( event )
  163.    local sceneGroup = self.view
  164.       if _G.debug == 1 then print("** playGame--scene:create: \n\n") end            -- DEBUG
  165.  
  166.    --Bird.group = Bird.group
  167.     bg = display.newImageRect( bgImage, _W, _H, 375, 667, true )
  168.     bg.anchorX, bg.anchorY = 0, 0
  169.     bg.alpha = 1
  170.     sceneGroup:insert(bg)
  171.  
  172.     leftRib = display.newImageRect( "leftRib.png", 48, 793, true )
  173.     leftRib.anchorX, leftRib.anchorY = 0, 0
  174.     leftRib.alpha = 1
  175.     sceneGroup:insert(leftRib)
  176.  
  177.     woodPanel = display.newImageRect( "woodPanel.png", 375, 131, true )
  178.     woodPanel.x, woodPanel.y = display.contentWidth/2, display.contentHeight
  179.     woodPanel.anchorX, woodPanel.anchorY = 0.5, 1
  180.     woodPanel.alpha = 1
  181.     sceneGroup:insert(woodPanel)
  182.  
  183.     themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 18 )
  184.     themeText.anchorX, themeText.anchorY = 0, 0
  185.     themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 30
  186.     themeText:setFillColor( colorConverter(236, 210, 135, 1) )
  187.     sceneGroup:insert(themeText)
  188.  
  189.     function onRowRender( event )
  190.     local phase = event.phase
  191.     local row = event.row
  192.     if _G.debug == 1 then print("** onRowRender: \n\n") end      -- DEBUG
  193.     print("**************** Row rendered", row.id )
  194.  
  195.     row.bg = display.newRoundedRect( 0, 0, display.contentWidth -48, 50, 12 )
  196.     row.bg.strokeWidth = 0
  197.     row.bg:setFillColor( colorConverter(236, 210, 135, 1) )
  198.     row.bg.alpha = 1
  199.     row.bg.anchorX = 1
  200.     row.bg.anchorY = 0
  201.     row.bg.x = display.contentWidth
  202.     row.bg.y = 10
  203.     row:insert( row.bg )
  204.  
  205.     row.title = display.newText( row, dbTable[row.index].NAME, 0, 0, _G.msgFont , 24 )
  206.     row.title.x = leftIndent + 10
  207.     row.title.anchorX = 0
  208.     row.title.y = row.contentHeight * 0.15+ row.bg.y
  209.     row.title:setFillColor( colorConverter(114, 72, 30, 1) )
  210.  
  211.     row.ingress = display.newText( row, dbTable[row.index].LATIN, 0, 0, _G.msgFont , 14 )
  212.     row.ingress.x = leftIndent + 10
  213.     row.ingress.anchorX = 0
  214.     row.ingress.y = row.title.y +20
  215.     row.ingress:setFillColor( colorConverter(114, 72, 30, 1) )
  216.  
  217.     row.image = display.newImageRect(row, tostring(imagePath .. dbTable[row.index].NAME) .. ".jpg", 45,45 )
  218.     row.image:addEventListener( "touch", row.image)
  219.     row.image.touch = birdSpec
  220.     row.image.anchorX = 1
  221.     row.image.anchorY = 0.25
  222.     row.image.x = display.viewableContentWidth - row.image.width/2
  223.     row.image.y = row.title.y
  224.  
  225.     -- PARSE VARIABLE TO THE GLOBAL SPECPOOL TABLE
  226.     row.image.specName = dbTable[row.index].NAME
  227.     row.image.specLatin = dbTable[row.index].LATIN
  228.     row.image.specInfo = dbTable[row.index].INFO
  229.     print("**************** Row rendered")
  230.  
  231. end
  232. end
  233.  
  234. -- "scene:show()"
  235. function scene:show( event )
  236.    local sceneGroup = self.view
  237.    local phase = event.phase
  238.    if ( phase == "will" ) then
  239.    
  240.    if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end            -- DEBUG
  241.    composer.removeHidden( )        
  242.    elseif ( phase == "did" ) then
  243.    if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end            -- DEBUG
  244. --audio.crossFadeBackground( "bgSound.mp3", 1 )
  245.  
  246.  
  247. -- WHEN I FIRE IT UP THE FIRST TIME IT WILL SHOW ALL ENTRIES OF THE DB
  248. if sql == nil then sql = "SELECT * FROM birds" print("Initiated SQL *") end
  249.  
  250. --THE NEXT STATEMENT SIMULATES PUSHING THE BUTTON THAT SELECTS ALL BLACK BIRDS ('Sort' means black in Norwegian)
  251. --if sql == nil then sql = "SELECT * FROM birds WHERE COLOR1 = 'Sort'" print("Initiated SQL *") end
  252.  
  253.     count = 0
  254.     print("Sql in playGame: scene show ", sql)
  255.  
  256.       for row in db:nrows(sql) do
  257.          count = count +1
  258.          dbTable[count]={}
  259.          dbTable[count].NAME = row.NAME
  260.          dbTable[count].LATIN = row.LATIN
  261.          dbTable[count].INFO = row.INFO
  262.          print("*--inside forloop in show:Scene--*")
  263.       end
  264.  
  265.     tableView = widget.newTableView
  266.     {
  267.         top = 0,
  268.         width = _W+1,
  269.         height = display.contentHeight,
  270.         hideBackground = true,
  271.         isBounceEnabled = true,
  272.         listener = tableViewListener,
  273.         onRowRender = onRowRender,
  274.     }
  275.  
  276.     for i = 1, #dbTable do
  277.         local isCategory = false
  278.         local rowHeight = 70
  279.         local rowColor = {
  280.             default = { colorConverter(114, 72, 30, 0) },
  281.         }
  282.         local lineColor = { colorConverter(20, 20, 0, 0) }
  283.  
  284.         -- Insert the row into the tableView
  285.         tableView:insertRow
  286.         {
  287.           --  isCategory = isCategory,
  288.           rowHeight = rowHeight,
  289.           rowColor = rowColor,
  290.           lineColor = lineColor,
  291.         }
  292.  
  293.     end
  294.     sceneGroup:insert(tableView)
  295.  
  296.   end
  297.  
  298.  
  299. end
  300.  
  301. -- "scene:hide()"
  302. function scene:hide( event )
  303.    local sceneGroup = self.view
  304.    local phase = event.phase
  305.    if ( phase == "will" ) then 
  306.    elseif ( phase == "did" ) then
  307.  if tableView and tableView:getNumRows()>1 then tableView:deleteAllRows()
  308.   print("TableView iterated and deleted from scene hide in playGame")
  309. end
  310.  
  311.  
  312. _G.catText = ""
  313. updateCatText()
  314.  
  315.    end
  316. end
  317.  
  318. function scene:destroy( event )
  319.     local sceneGroup = self.view
  320.  
  321. end
  322.  
  323. -- Listener setup
  324. scene:addEventListener( "create", scene )
  325. scene:addEventListener( "show", scene )
  326. scene:addEventListener( "hide", scene )
  327. scene:addEventListener( "destroy", scene )
  328.  
  329. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement