Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --PLAYGAME START SCENE.
- display.setStatusBar(display.HiddenStatusBar)
- require( "sqlite3" )
- local widget = require("widget")
- birds = require("Classes.ButtonsBirds")
- --LOCALS FWD
- local composer = require "composer"
- local scene = composer.newScene()
- local path = system.pathForFile( "Birds.sqlite", system.ResourceDirectory )
- local imagePath = "Images/Birds/"
- local bgImage = imagePath .. "bgTheme.jpg"
- local text, themeText, bg, t, count, bgItem, bg, colorConverter, tableView, leftRib, woodPanel, butNext, butBack
- local leftIndent = 50
- local dbTable = {}
- local rowBg = {}
- local rowName = {}
- local rowLatin = {}
- local playButton = {}
- local onRowRender = {}
- local onRowTouch = {}
- removeGroup = {}
- updateSql = {}
- if not sql then sql = "SELECT * FROM birds" print("sql is inited for the first time") end
- local _W, _H = display.contentWidth, display.contentHeight
- local group = {}
- local containerGroup = display.newGroup( )
- local dashboardGroup = display.newGroup( )
- --GLOBALS FWD
- local db = sqlite3.open( path )
- _G.specPool = {}
- _G.catText = "Trykk på en av knappene.."
- -- FUNCTIONS
- local function onSystemEvent( event )
- if ( event.type == "applicationExit" ) then
- db:close()
- end
- end
- function removeRows()
- if #dbTable >= 1 then
- for i = 1, #dbTable do
- tableView:removeSelf(i)
- end
- end
- end
- local function colorConverter(r, g, b, a)
- local colorR, colorG, colorB, alphA
- colorR = r/256; colorG = g/256; colorB = b/256
- alphA = a or nil
- return colorR, colorG, colorB, alphA
- end
- function updateCatText()
- if _G.debug == 1 then print("** updateCatText: \n\n") end -- DEBUG
- print(_G.catText)
- if themeText then display.remove( themeText) end
- themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 34 )
- themeText.anchorX, themeText.anchorY = 0, 0
- themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 20
- themeText:setFillColor( colorConverter(236, 210, 135, 1) )
- return true
- end
- function updateSql(event)
- sql = event
- event = sql
- -- load the data into a table array
- if _G.debug == 1 then print("** updateSql: \n\n") end -- DEBUG
- count = 0
- -- sql = sql or print("No SQL statement present")
- print("sql = ", event)
- for row in db:nrows(sql) do
- count = count +1
- dbTable[count]={}
- dbTable[count].NAME = row.NAME
- dbTable[count].LATIN = row.LATIN
- dbTable[count].INFO = row.INFO
- end
- tableView:deleteAllRows()
- for i = 1, #dbTable do
- local isCategory = false
- local rowHeight = 70
- local rowColor = {
- default = { colorConverter(114, 72, 30, 0) },
- }
- local lineColor = { colorConverter(20, 20, 0, 0) }
- -- Insert the row into the tableView
- tableView:insertRow
- {
- rowHeight = rowHeight,
- rowColor = rowColor,
- lineColor = lineColor,
- }
- -- tableView:reloadData()
- end
- return true
- end
- function removeGroup()
- for i = 1, #group do
- print("Before removing from group: " .. #group)
- group[i]:removeSelf()
- group[i] = nil
- print("Removed: " .. #group)
- end
- return true
- end
- local function birdSpec(self, event)
- if event.phase == "began" then
- if _G.debug == 1 then print("** birdSpec: \n\n") end -- DEBUG
- -- composer.setVariable( "birdspec", {"name"=self.NAME} )
- _G.specPool.name = self.specName
- _G.specPool.latin = self.specLatin
- _G.specPool.info = self.specInfo
- print("playGame-birdSpec returns _G.specPool.name like: ", self.specName, " parsed to self.specName")
- print("playGame-birdSpec returns _G.specPool.name like: ", _G.specPool.name)
- -- print(self.specIngress)
- -- print(self.specInfo)
- -- updateSql()
- print("going over til spec page now...")
- composer.gotoScene( "playSpecScene" )
- elseif event.phase == "cancelled" or event.phase == "ended" then
- end
- return true
- end
- function onRowRender( event )
- if _G.debug == 1 then print("** onRowRender: \n\n") end -- DEBUG
- local phase = event.phase
- local row = event.row
- print("**************** Row rendered")
- row.bg = display.newRoundedRect( 0, 0, display.contentWidth -48, 50, 12 )
- row.bg.strokeWidth = 0
- row.bg:setFillColor( colorConverter(236, 210, 135, 1) )
- row.bg.anchorX = 1
- row.bg.anchorY = 0
- row.bg.x = display.contentWidth
- row.bg.y = 10
- row:insert( row.bg )
- row.title = display.newText( row, dbTable[row.index].NAME, 0, 0, _G.msgFont , 24 )
- row.title.x = leftIndent + 10
- row.title.anchorX = 0
- row.title.y = row.contentHeight * 0.15+ row.bg.y
- row.title:setFillColor( colorConverter(114, 72, 30, 1) )
- row.ingress = display.newText( row, dbTable[row.index].LATIN, 0, 0, _G.msgFont , 14 )
- row.ingress.x = leftIndent + 10
- row.ingress.anchorX = 0
- row.ingress.y = row.title.y +20
- row.ingress:setFillColor( colorConverter(114, 72, 30, 1) )
- --local mask = graphics.newMask( "imageMask.png" )
- row.image = display.newImageRect(row, tostring(imagePath .. dbTable[row.index].NAME) .. ".jpg", 45,45 )
- row.image:addEventListener( "touch", row.image)
- row.image.touch = birdSpec
- row.image.anchorX = 1
- row.image.anchorY = 0.25
- row.image.x = display.viewableContentWidth - row.image.width/2
- row.image.y = row.title.y
- -- PARSE VARIABLE TO THE GLOBAL SPECPOOL TABLE
- row.image.specName = dbTable[row.index].NAME
- row.image.specLatin = dbTable[row.index].LATIN
- row.image.specInfo = dbTable[row.index].INFO
- print("**************** Row rendered")
- end
- -- Handle touches on the row
- function onRowTouch( event )
- local phase = event.phase
- if "press" == phase then
- native.showAlert(dbTable[event.target.index].NAME, "har det latinske navnet " .. dbTable[event.target.index].LATIN, "OKAY")
- print( event.target.index )
- end
- end
- -- "scene:create()"
- function scene:create( event )
- local sceneGroup = self.view
- if _G.debug == 1 then print("** playGame--scene:create: \n\n") end -- DEBUG
- --Bird.group = Bird.group
- bg = display.newImageRect( bgImage, _W, _H, 375, 667, true )
- bg.anchorX, bg.anchorY = 0, 0
- bg.alpha = 1
- sceneGroup:insert(bg)
- leftRib = display.newImageRect( "leftRib.png", 48, 793, true )
- leftRib.anchorX, leftRib.anchorY = 0, 0
- leftRib.alpha = 1
- sceneGroup:insert(leftRib)
- woodPanel = display.newImageRect( "woodPanel.png", 375, 131, true )
- woodPanel.x, woodPanel.y = display.contentWidth/2, display.contentHeight
- woodPanel.anchorX, woodPanel.anchorY = 0.5, 1
- woodPanel.alpha = 1
- sceneGroup:insert(woodPanel)
- themeText = display.newText( tostring(_G.catText), 0, 0, _G.msgFont , 18 )
- themeText.anchorX, themeText.anchorY = 0, 0
- themeText.x, themeText.y = leftIndent, (woodPanel.y - woodPanel.height) + 30
- themeText:setFillColor( colorConverter(236, 210, 135, 1) )
- sceneGroup:insert(themeText)
- end
- -- "scene:show()"
- function scene:show( event )
- local sceneGroup = self.view
- local phase = event.phase
- if ( phase == "will" ) then
- if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end -- DEBUG
- composer.removeHidden( )
- elseif ( phase == "did" ) then
- if _G.debug == 1 then print("** playGame--scene:show:will: \n\n") end -- DEBUG
- --audio.crossFadeBackground( "bgSound.mp3", 1 )
- --birds:initButtons()
- -- load the data into a table array
- if tableView and tableView:getNumRows()>1 then tableView:deleteAllRows() end
- count = 0
- print("Sql in playGame: scene show ", sql)
- sql = sql or "SELECT * FROM Birds"
- for row in db:nrows(sql) do
- count = count +1
- dbTable[count]={}
- dbTable[count].NAME = row.NAME
- dbTable[count].LATIN = row.LATIN
- dbTable[count].INFO = row.INFO
- end
- tableView = widget.newTableView
- {
- top = 0,
- width = _W+1,
- height = display.contentHeight,
- hideBackground = true,
- isBounceEnabled = true,
- listener = tableViewListener,
- onRowRender = onRowRender,
- }
- for i = 1, #dbTable do
- local isCategory = false
- local rowHeight = 70
- local rowColor = {
- default = { colorConverter(114, 72, 30, 0) },
- }
- local lineColor = { colorConverter(20, 20, 0, 0) }
- -- Insert the row into the tableView
- tableView:insertRow
- {
- -- isCategory = isCategory,
- rowHeight = rowHeight,
- rowColor = rowColor,
- lineColor = lineColor,
- }
- end
- sceneGroup:insert(tableView)
- end
- end
- -- "scene:hide()"
- function scene:hide( event )
- local sceneGroup = self.view
- local phase = event.phase
- if ( phase == "will" ) then
- elseif ( phase == "did" ) then
- if _G.debug == 1 then print("** playGame--scene:hide: \n\n") end -- DEBUG
- if tableView and tableView:getNumRows()>1 then tableView:deleteAllRows()
- print("TableView iterated and deleted from scene hide in playGame")
- end
- _G.catText = ""
- updateCatText()
- end
- end
- function scene:destroy( event )
- local sceneGroup = self.view
- end
- -- Listener setup
- scene:addEventListener( "create", scene )
- scene:addEventListener( "show", scene )
- scene:addEventListener( "hide", scene )
- scene:addEventListener( "destroy", scene )
- return scene
- -- CLASS: buttonBirds recided in Classes folder:
- Bird = {}
- Bird.pathForBehaviours = "Assets/Birds/Behaviour/"
- Bird.group = display.newGroup( )
- Bird.group.x = 0
- Bird.group.anchorX = 0
- group = group
- sql = sql
- updateSql = updateSql
- themeText = themeText
- tableView = tableView
- local imgW, imgH = 48, 48
- Bird.group.y = (display.contentHeight - imgH)
- local behaviourCounter = 0
- --BIRDS TABLE
- Bird.buttons = {}
- --BEHAVIOURS TABLE
- Bird.buttons.behavior = {
- {
- name = "SELECT * FROM birds WHERE PATTERN = 'Spettete'",
- sound = "behaviour.mp3" or nil,
- imageObj = display.newImageRect( Bird.pathForBehaviours .. "behaviour.png", imgW, imgH ),
- behavior = "Opførsel",
- category = "Behavior",
- },
- {
- name = "SELECT * FROM birds WHERE PATTERN = 'Stripete'",
- sound = "birdHouse.mp3" or nil,
- imageObj = display.newImageRect( Bird.pathForBehaviours .. "birdHouse.png", imgW, imgH ),
- behavior = "Farger",
- category = "Behavior",
- },
- {
- name = "SELECT * FROM birds WHERE PATTERN = 'Flekkete'",
- sound = "catchingInsectsInFlight.mp3" or nil,
- imageObj = display.newImageRect( Bird.pathForBehaviours .. "catchingInsectsInFlight.png", imgW, imgH ),
- behavior = "Form",
- category = "Behavior",
- },
- {
- name = "SELECT * FROM birds WHERE SIZE = 'S'",
- sound = "climbingTree.mp3" or nil,
- imageObj = display.newImageRect( Bird.pathForBehaviours .. "climbingTree.png", imgW, imgH ),
- behavior = "Familie",
- category = "Behavior",
- },
- }
- function Bird:engineResponse(event)
- if ( event.phase == "began" ) then
- if _G.debug == 1 then print("** ButtonBirds Class--engineResponse: \n\n") end -- DEBUG
- display.getCurrentStage():setFocus( event.target )
- print( "Touch started on: " .. self.id)
- sql = self.id
- updateSql(sql)
- print(self.catText)
- _G.catText = self.catText
- updateCatText()
- elseif event.phase == "ended" or event.phase == "cancelled" then
- display.getCurrentStage():setFocus(nil)
- print( "Touch ended" )
- end
- return true
- end
- function Bird:initButtons(event)
- if _G.debug == 1 then print("** ButtonBirds Class--initButtons: \n\n") end -- DEBUG
- for k,v in ipairs (Bird.buttons.behavior) do
- behaviourCounter = behaviourCounter + 1
- local img
- img = v['imageObj']
- img.x = (imgW + 5) * behaviourCounter
- img.anchorX = 0
- img.anchorY = 0.20
- img.id = v['name']
- img:addEventListener( "touch", img )
- img.touch = Bird.engineResponse
- Bird.group:insert(img)
- img.catText = v['behavior']
- img.sql = v['name']
- Bird.category = v['category']
- print(Bird.category)
- end
- print( "Bird.group has "..Bird.group.numChildren .." children")
- return true
- end
- Bird:initButtons()
- return Bird
- ------------------------
- ------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement