Corona sdk composer wont got back to previous scene - button

I have used coronaSDK composer to have scenes in my application. i have a options button that is able to get to the options scene. however when i put a button on the option scene to go back to mainMenu scene it doesnt work
mainMenu.lua
local composer = require( "composer" )
local scene = composer.newScene()
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
local backgroundimg = display.newImage("image/mainBackground.png")
backgroundimg.x = display.contentWidth*0.5
backgroundimg.y = display.contentHeight*0.5
sceneGroup:insert( backgroundimg )
local function startListener( event )
composer.gotoScene( "scene.play" )
end
local start = display.newRect(65,99.5,161,161)
start.alpha = 0.01
start:addEventListener( "tap", startListener )
local function leaderListener( event )
composer.gotoScene( "scene.leaderboards" )
end
local leader = display.newRect(208,227.5,147,147)
leader.alpha = 0.01
leader:addEventListener( "tap", leaderListener )
local function optionsListener( event )
composer.gotoScene( "scene.options" )
end
local options = display.newRect(332,92.5,147,147)
options.alpha = 0.01
options:addEventListener( "tap", optionsListener )
local function quitListener( event )
os.exit()
end
local quit = display.newRect(432,240.5,120,120)
quit.alpha = 0.01
quit:addEventListener( "tap", quitListener )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
options.lua
local composer = require( "composer" )
local scene = composer.newScene()
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
local function startListener( event )
composer.gotoScene( "scene.mainMenu" )
end
local start = display.newRect(65,99.5,161,161)
start.alpha = 1
start:addEventListener( "tap", startListener )
end

Try using:
composer.gotoScene( "mainMenu" )
Also, you are not adding the objects to the scene group. So, when the scene changes, the objects will not be removed automatically.

Related

How to get the tasklist children widgets in button function?

How can I get all children of the tasklist.
I have a tasklist in a function and a tasklist_buttons:
local tasklist_buttons =
gears.table.join(
awful.button(
{},
1,
function(c)
-- HOW TO GET THIS ↓↓↓
-- tasklistChildren = TaskList.children
end
)
)
local TaskList = function(s)
return awful.widget.tasklist(
s,
awful.widget.tasklist.filter.currenttags,
tasklist_buttons,
{},
list_update,
wibox.layout.fixed.horizontal()
)
end
Can't think of a good approach.

Corona Button not passing focus to scrollView

I am having an issue with scrolling when buttons are on the scrollView. The problem is the issue of events not being handled properly. All attempts to pass focus to the scrollView have proven fruitless. An observation I have made is that in this following code, it is not printing back to me, this is even after I was scrolling correctly without the buttons in the way of it.
local function scrollListener( event )
local phase = event.phase
if ( phase == "began" ) then print( "Scroll view was touched" )
elseif ( phase == "moved" ) then print( "Scroll view was moved" )
elseif ( phase == "ended" ) then print( "Scroll view was released" )
end
-- In the event a scroll limit is reached...
if ( event.limitReached ) then
if ( event.direction == "up" ) then print( "Reached top limit" )
elseif ( event.direction == "down" ) then print( "Reached bottom limit" )
elseif ( event.direction == "left" ) then print( "Reached left limit" )
elseif ( event.direction == "right" ) then print( "Reached right limit" )
end
end
return true
end
Here is the actual code being used for the scene:
function scene:enterScene(event)
--local yell=audio.play(ahhh, {channel=1, loops=0, fadein=0})
--Creating the Background
local background=display.newImageRect("background.png", 320, 580)
background.x=160
background.y=240
--Creating the scroll view
local scrollView = widget.newScrollView
{
top = 10,
left = 10,
width = 300,
height = 500,
scrollWidth = 300,
scrollHeight = 500,
horizontalScrollDisabled = true,
listener = scrollListener
}
local button = {}
local yCount = 0
for i = 1 , 11 do
button[i] = widget.newButton{
label = "Button " .. i,
left = 0,
id = button[i],
top = yCount,
width = 300,
height = 100,
defaultFile = "menuButton.png",
onEvent = handleButtonEvent
}
yCount = yCount + 100
scrollView:insert(button[i])
end
--local background=display.newRect(160, 270, 320, 510)
--scrollView:insert( background )
local menu=display.newImageRect("menu2.png", 90, 50)
menu.x=50
menu.y=-15
function menu:touch(event)
if event.phase=="began" then
--local illBeBack=audio.play(terminator,{channel=1, loops=0, fadein=0})
display.remove(menu)
display.remove(taskBar)
display.remove(background)
taskBar = nil
background = nil
menu = nil
storyboard.gotoScene("menu")
end
return true
end
menu:addEventListener("touch", menu)
end
--Button Event Handler
local function handleButtonEvent( event )
local phase = event.phase
if ( phase == "moved" ) then
local dy = math.abs( ( event.y - event.yStart ) )
-- If the touch on the button has moved more than 10 pixels,
-- pass focus back to the scroll view so it can continue scrolling
if ( dy > 10 ) then
scrollView:takeFocus( event )
end
end
return true
end
Try passing the event.phase into scrollView like this:
scrollView:takeFocus(event.phase)
The problem was solved by removing 'local' from the 'handleButtonEvent' and 'scrollListener' functions, along with removing 'local' from the 'scrollView' widget
So it appears that in corona, or at least the program I am making, that globals must be used a lot more than what they should be, unless someone else has a solution.

How to update gtable at gWidget in R?

I'm struggling to update the table data in the following code for GUI. It works to add each new request to the previous, so that it doesn't fit the screen, while I need to replace old request with the new one. Thanks! A.
require ( gWidgets )
options ( guiToolkit="RGtk2" )
window <- gwindow ( "Pilot>>" , visible = FALSE, horizontal = FALSE )
paned <- gpanedgroup ( cont = window )
group <- ggroup ( cont = paned, horizontal = FALSE )
glabel ( "FPC:" , cont = group )
entry <- gedit ( "" , cont = group )
entry [ ] <- actfpc$CURFPC ##list of codes for auto-filling
search_button <- gbutton ( "Go!" , cont = group )
addSpring ( group )
frame <- gframe ( "Supply:" , cont = paned , horizontal = FALSE,use.scrollwindow=TRUE )
addHandlerChanged ( search_button , handler = function ( h , ... ) {
##Data subset for entered value
a<-skulist[skulist$FPC==svalue(entry),]
a<-a[c("X_id","FPC","STATUS","COMMENT","DCTR","DCSTOCK")]
tbl <- gtable ( a , cont = frame, expand=TRUE )
} )
size ( frame) <- c ( 950 , 250)
visible ( window ) <- TRUE

custom sorting in mvc (QSortFilterProxyModel) only works once

I'm trying to set up a custom sort behaviour for my model/view structure in PySide.
All items are dictionaries and I need to filter by their keys (I'm not after a table view of those dictionaries but rather need to present them as one entity each).
I inherited from QSortFilterProxyModel and re-implemented the lessThan method. It works fine the first time the sort widget is changed (which triggers the proxy's sort() method), but after that the lessThan method is no longer called. I have no idea why and an hoping to get some help here. I'm more than happy to consider any suggestions how to tackle this in a different way if that is where the solution lies.
This is my proxy:
class ProxyModel ( QSortFilterProxyModel ):
def __init__( self, parent=None ):
super( ProxyModel, self).__init__( parent )
self.setFilterCaseSensitivity( Qt.CaseInsensitive )
self.setSortCaseSensitivity( Qt.CaseInsensitive )
self.setDynamicSortFilter( True )
def sortBy( self, attr ):
print 'sorting by', attr
self.__sortBy = attr
self.sort( 0, Qt.AscendingOrder ) # THIS DOES NOT GET CALLED WHEN THE COMBO BOX CHANGES A SECOND TIME
def lessThan( self, left, right ):
'''Custom sorting behaviour'''
leftTool = ( self.sourceModel().itemFromIndex( left ) )
rightTool = ( self.sourceModel().itemFromIndex( right ) )
leftData = leftTool.data()[ self.__sortBy ]
rightData = rightTool.data()[ self.__sortBy ]
return leftData < rightData
And here is the complete test code:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class MainWidget( QWidget ) :
def __init__( self, parent=None ):
super( MainWidget, self ).__init__()
self.listView = MyListView()
model = MyModel()
# MODELS AND VIEWS
self.proxyModel = ProxyModel()
self.proxyModel.setSourceModel( model )
self.listView.setModel(self.proxyModel)
# LAYOUTS
verticalLayout = QVBoxLayout()
filterLayout = QHBoxLayout()
# SORTING WIDGET
sortLayout = QHBoxLayout()
sortLabel = QLabel( 'sort:' )
self.sortWidget = QComboBox()
self.sortWidget.addItems( ['title', 'author', 'downloads'] )
self.sortWidget.currentIndexChanged.connect( self.sortTools )
sortLayout.addWidget( sortLabel )
sortLayout.addWidget( self.sortWidget )
verticalLayout.addLayout( filterLayout )
verticalLayout.addLayout( sortLayout )
verticalLayout.insertWidget(0, self.listView)
self.setLayout( verticalLayout )
def sortTools( self ):
text = self.sortWidget.currentText()
self.proxyModel.sortBy( text )
class ProxyModel ( QSortFilterProxyModel ):
def __init__( self, parent=None ):
super( ProxyModel, self).__init__( parent )
self.setFilterCaseSensitivity( Qt.CaseInsensitive )
self.setSortCaseSensitivity( Qt.CaseInsensitive )
self.setDynamicSortFilter( True )
def sortBy( self, attr ):
print 'sorting by', attr
self.__sortBy = attr
self.sort( 0, Qt.AscendingOrder ) # THIS DOES NOT GET CALLED WHEN THE COMBO BOX CHANGES A SECOND TIME
def lessThan( self, left, right ):
'''Custom sorting behaviour'''
leftTool = ( self.sourceModel().itemFromIndex( left ) )
rightTool = ( self.sourceModel().itemFromIndex( right ) )
leftData = leftTool.data()[ self.__sortBy ]
rightData = rightTool.data()[ self.__sortBy ]
return leftData < rightData
class MyListView( QListView ):
def __init__( self, parent=None ):
super( MyListView, self).__init__( parent )
self.setEditTriggers( QListView.NoEditTriggers )
self.setViewMode( QListView.IconMode )
self.setMovement( QListView.Static )
self.setResizeMode( QListView.Adjust )
self.setDragEnabled( True )
class MyModel( QStandardItemModel ):
def __init__( self, parent=None ):
super( MyModel, self).__init__( parent )
self.init_data()
def init_data(self):
row = 0
toolData = [ {'title':'ToolA', 'author':'John Doe', 'downloads':123, 'category':'color'},
{'title':'ToolB', 'author':'me', 'downloads':13, 'category':'color'},
{'title':'ToolC', 'author':'you', 'downloads':321, 'category':'transform'},
{'title':'ToolD', 'author':'unknown', 'downloads':2, 'category':'transform'}]
for tool in toolData:
item = QStandardItem( '%(title)s by %(author)s (%(category)s) - %(downloads)s downloads' % tool )
item.setData( tool )
self.setItem( row, 0, item )
row += 1
if __name__ == '__main__':
app = QApplication( sys.argv )
mainWidget = MainWidget()
mainWidget.resize( 400, 400 )
mainWidget.show()
sys.exit( app.exec_() )
Basically Qt "thinks" your Proxy is already sorted. To be precise it checks in C++ whether:
(d->dynamic_sortfilter &&
d->proxy_sort_column == column &&
d->sort_order == order)
So, to solve your problem, you can either set dynamic_sortfilter to False (it will have side-effects) or, maybe better, invalidate your sorting:
def sortBy( self, attr ):
print 'sorting by', attr
self.__sortBy = attr
self.invalidate() #invalidate helps
self.sort( 0, Qt.AscendingOrder )

why does the Director class make my global variables not visible to external modules in corona?

I'm doing basic stuff to understand using external classes/modules with global variables, and now I'm trying to add director. But when I added the director class, my global variables such as applyThrust then became not visible in the external class.
It worked fine before refactoring for Director. Before using director, I only had 2 files, rocket.lua and the mainScreen.lua file below was simply main.lua. I used the same globals, applyThrust and thrustForceY, and it worked.
By the way, the director functionality of switching screens appears to work fine, other than this odd side effect of making the global variables not visible.
I asked this on the corona director forums and got no response so I thought I'd try reliable STO.
Here's the files I'm using with the director stuff :
main.lua
display.setStatusBar (display.HiddenStatusBar)
local director = require ("director")
local mainGroup = display.newGroup()
local function main()
mainGroup:insert(director.directorView)
director:changeScene("menu")
return true
end
main()
menu.lua
module(..., package.seeall)
function new()
local localGroup = display.newGroup()
------------------------------------------------------------------------------
local playText = display.newText("Play",160, 100, "Arial", 32)
playText:setTextColor(0, 0, 255)
localGroup:insert(playText)
local function pressPlay (event)
if event.phase == "ended" then
director:changeScene ("mainScreen")
end
end
playText:addEventListener ("touch", pressPlay)
------------------------------------------------------------------------------
return localGroup
end
mainScreen.lua
local Rocket = require( "rocket" )
local physics = require( "physics" )
module(..., package.seeall)
function new()
local localGroup = display.newGroup()
------------------------------------------------------------------
score = 100
physics.start()
local ground = display.newRect( 60, 170, 60, 60 )
physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.2 } )
rocket = Rocket.new(80, 110)
local upText = display.newText("Up",160, 300, "Arial", 32)
upText:setTextColor(0, 0, 255)
------------- my global variables -----------
thrustForceY = -100
applyThrust = false
local function pressUp (event)
local t = event.target
local phase = event.phase
if( "began" == phase ) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
if( not applyThrust ) then
rocket:addThrust()
end
rocket:applyForce(0, thrustForceY, rocket.x, rocket.y)
applyThrust = true
elseif "ended" == phase or "cancelled" == phase then
rocket:removeThrust()
display.getCurrentStage():setFocus( nil )
t.isFocus = false
applyThrust = false
end
return true
end
upText:addEventListener ("touch", pressUp)
----------------------------------------------
return localGroup
end
rocket.lua
module(..., package.seeall)
local physics = require( "physics" )
--constructor--------------------
function new(x, y)
rocket = display.newGroup()
local body = display.newRect( x, y, 25, 60 )
physics.addBody( body, { density=1.5, friction=0.5, bounce=0.2 } )
body.isFixedRotation = true
rocket:insert(body)
local thrust = {}
function rocket:addThrust()
thrust = display.newRect( body.x, body.y + (body.height / 2) , 10, 10 )
thrust.y = body.y + (body.height / 2) + (thrust.height / 2)
physics.addBody( thrust, { density=1.5, friction=0.5, bounce=0.2 } )
rocket:insert(thrust)
end
function rocket:removeThrust()
rocket:remove(thrust)
end
function rocket:applyForce( xForce, yForce, atPointX, atPointY )
body:applyForce( xForce, yForce, atPointX, atPointY )
end
----------------------------------------------------------------------
-- enterFrame listener for rocket
----------------------------------------------------------------------
function rocket:enterFrame (event)
if( applyThrust ) then
body:applyForce(0, thrustForceY, body.x, body.y)
thrust.y = body.y + (body.height / 2) + (thrust.height / 2)
thrust.x = body.x
else
print("applyThrust is nil")
end
end
Runtime:addEventListener("enterFrame", rocket)
return rocket
end
A global variable in Corona is as simple as using "_G." before your variable.
For example:
lives = 3
Would become:
_G.lives = 3
But Director already has functionality to pass parameters to another scene.... there is no need to use globals.
scene1.lua
local params = {
param1= "http://github.com/",
param2 = "bla-bla"
}
director:changeScene( params, "screen2", "moveFromRight" )
screen2.lua
new = function ( params )
local url = params.param1
local bla = params.param2
...
end
Good point.
Also, you can put all your variables in a file you created and access it from any module.

Resources