easiest way to make button to *.lua file CORONA SDK - button

What is the easiest way to make button to *.lua file?
I want to make something like : main.lua (tap button) --> scene1.lua (BACK tap button) --> main.lua
I tried something like this:
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
local logo tlo = display.newImage("bg.png", 360, 640)
local zamow = display.newImage("1zamow.png", 185, 340)
local gadgety= display.newImage("2gadgety.png", 535, 340)
local facebook = display.newImage("3facebook.png", 185, 700)
local oferta = display.newImage("4oferta.png", 535, 700)
local cennik = display.newImage("5cennik.png", 185, 1060)
local kontakt = display.newImage("6kontakt.png", 535, 1060)
function cennik:touch (event)
storyboard.gotoScene("cennik", "fade", 400)
end
cennik:addEventListener( "touch", cennik )
return scene'
after that I have runtime error:
attempt to concatenate global 'sceneName' (a nil value)
I'm new one in Corona so please be nice :)

Its better to use widget for creating button. If cennik is your button image and scene1.lua is the next page, then code be written as
local storyboard = require ("storyboard")
local widget=require "widget"
local scene=storyboard.newScene()
local logotlo = display.newImage("bg.png", 360, 640)
local zamow = display.newImage("1zamow.png", 185, 340)
local gadgety= display.newImage("2gadgety.png", 535, 340)
local facebook = display.newImage("3facebook.png", 185, 700)
local oferta = display.newImage("4oferta.png", 535, 700)
local kontakt = display.newImage("6kontakt.png", 535, 1060)
local cennikBtn
local function onStartButtonRelease()
storyboard:gotoScene("scene1")
end
cennikBtn = widget.newButton {
defaultFile="5cennik.png",
onRelease=onStartButtonRelease
}
cennikBtn.x,cennikBtn.y =185, 1060
function scene:createScene(event)
local group = self.view
group:insert(logotlo)
group:insert(zamow)
group:insert(gadgety)
group:insert(facebook)
group:insert(oferta)
group:insert(kontakt)
group:insert(cennikBtn)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
storyboard.removeScene("main")
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene

Your code looks fine except for the ' after return scene and your event handler needs to be like this:
-- For each time you tap the image, this event gets triggered twice. Make sure you only dispatch your gotoScene once!
function cennik:touch(event)
if event.phase == "began" then
-- This happens on finger touching
elseif event.phase == "ended" then
-- This happens when the finger is lifted
storyboard.gotoScene("cennik", "fade", 400)
end
-- if you want to prevent the touch event from propagating (aka triggering other images touch event) you must return true
return true
end

Related

How in GUI to show the text from the file which changes?

In the file 1.txt in the first line there is an inscription, and it changes over time. In the GUI, it should also be changed. How to make it not flicker?
Local $Form1 = GUICreate('Form1', 261, 200, 192, 124)
$10 = FileReadLine ( "1.txt", 1);
GUISetState()
Local $spic = $10, $Pic1
While 1
$Pic1 = GUICtrlCreateLabel($10, 10, 70, 235, 50)
Switch FileExists($spic)
Case 0
If $Pic1 Then
GUICtrlDelete($Pic1)
$Pic1 = 0
EndIf
Case 1
If Not $Pic1 Then $Pic1 = GUICtrlCreatePic($spic, 16, 24, 212, 124)
EndSwitch
Sleep(1)
WEnd
#include <GUIConstantsEx.au3>
; Create the Gui.
$Form1 = GUICreate('Form1', 261, 200, 192, 124)
$iLabel = GUICtrlCreateLabel('', 10, 10, 235, 50)
$iPic = GUICtrlCreatePic('', 16, 34, 212, 124)
GUISetState()
; Hide picture control if no file [True|False].
$bHideImage = FileExists('default.jpg') ? False : True
; Updates in the loop to recognize change.
$sSavedFilename = ''
; Set time to reset image etc.
$iTimeReset = 1000
$hTimeStamp = TimerInit()
While 1
; Get Gui messages.
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
GUIDelete()
Exit
EndSwitch
; Check if time needs reset.
If TimerDiff($hTimeStamp) > $iTimeReset Then
$hTimeStamp = TimerInit()
Else
ContinueLoop
EndIf
; Read 1st line of a file.
$sReadFilename = FileReadLine ('1.txt', 1)
; If the saved line is different to read line.
If $sSavedFilename <> $sReadFilename Then
GUICtrlSetData($iLabel, $sReadFilename)
Switch FileExists($sReadFilename)
Case 0
If $bHideImage Then
GUICtrlSetState($iPic, $GUI_HIDE)
Else
; Display a default (blank?) image.
GUICtrlSetImage($iPic, 'default.jpg')
EndIf
Case 1
If $bHideImage Then
GUICtrlSetState($iPic, $GUI_SHOW)
EndIf
; Display the new image.
GUICtrlSetImage($iPic, $sReadFilename)
EndSwitch
; Save the current filename.
$sSavedFilename = $sReadFilename
EndIf
WEnd
Sleep is accurate to approximately
10 milliseconds, which is of so little time to update a control, thus
you get the flicker.
Update of labels which do not occur on an event like a button click,
can be handled using a timer.
If you use a message loop Gui, then you get the Gui messages with using
GuiGetMsg. After the messages, you can check a time stamp to know if
the time difference is larger than the time reset value which is currently
set as 1000 milliseconds. If larger, timer is reset and the code below
is executed, else will continue the loop from the top.
The filename read from the text file is saved to $sSavedFilename.
Updating of controls is only done when the read filename is different. If
the filename read does not exist, then display a default (blank?) image.
I sometimes choose a default image else an empty filename instead can
cause control sizing issues with the next image change. The control could
be instead be hidden, if no image to show. $bHideImage current decides
to use the file default.jpg if exist, else to hides the control.
This code updates the created controls instead of deleting and recreating them.
Update GUICtrlCreateLabel text with GUICtrlSetData.
Update GUICtrlCreatePic image with GUICtrlSetImage.

Not working Send() and Mouse Click in window with Class:SunAwtFrame on third level

I try to automate procedures in my Oracle MiddleWare Environment and when I get to the bottom of it, where I should click "Run" button (in Cyrrilic), I can't do this with Send, or Control, or Mouse. However, it is third level of the submenu, all other levels work (I know the usual problems with Frames, but it works for other levels, than why?))
The summary from Info is below:
Window <<<<
Title: My Window
Class: SunAwtFrame
Position: 0, 0
Size: 820, 660
Style: 0x16CF0000
ExStyle: 0x00000100
Handle: 0x00171058
Control <<<<
Class:
Instance:
ClassnameNN:
Name:
Advanced (Class):
ID:
Text:
Position:
Size:
ControlClick Coords:
Style:
ExStyle:
Handle: 0x000910F4
Mouse <<<<
Position: 448, 427
Cursor ID: 0
Color: 0xC0FFFF
StatusBar <<<<
ToolsBar <<<<
Visible Text <<<<
Hidden Text <<<<
Local $sLogin = InputBox("Security Check", "Enter your login", "")
Local $sPasswd = InputBox("Security Check", "Enter your password.", "","-")
$oIE = _IECreate("https://******************",0,0,1,1)
$oLinks = _IETagNameGetCollection($oIE, "input")
For $oLink In $oLinks
If String($oLink.type) = "button" And String($oLink.value) = "RUN" Then
_IEAction($oLink, "click")
ExitLoop
EndIf
Next
_IELoadWait($oIE, 1000)
Sleep(15000)
_WinWaitActivate("Oracle Fusion Middleware Forms Services","")
Send($sLogin)
Send("{TAB}")
Send($sPasswd)
Send("{TAB}")
Send("{SHIFTDOWN}prod{SHIFTUP}9{ENTER}")
_WinWaitActivate("My Window","")
Send("{TAB}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}
{DOWN}{UP}{RIGHT}{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}{DOWN}{RIGHT}{DOWN}
{DOWN}{DOWN}{DOWN}{DOWN}{RIGHT}{DOWN}{DOWN}{DOWN}{DOWN}{ENTER}")
*//And here it stops working without any error.//
Send("03/01/2019")
Send("{TAB}{TAB}{TAB}")
MouseMove(268,363,25)
MouseClick("primary")
Please note that I cannot change anything at all in Oracle MiddleWare environment or on the server side.
To answer this for your class SunAwtFrame the question is too unclear. But your code has some issues.
_WinWaitActivate() with Sleep(15000) isn't necessary because you can use WinWaitActive($sTitle, $sText, $iTimeout) with a timeout of 15 seconds as third parameter.
Your problem for the stopping should be the line 21. You can not add a new line within a function parameter without using of & _ at the end of the line. Use a function like _sendKeystrokesSeveralTimes() to avoid such long parameter values.
You also could shorten your MouseMove() and then MouseClick() action by doing MouseClick('left', 268, 363) to the target mouse position.
Here a reworked version of your code:
#include-once
#include <IE.au3>
Global $sLogin = InputBox('Security Check', 'Enter your login', '')
Global $sPasswd = InputBox('Security Check', 'Enter your password.', '', '-')
Global $oIE = _IECreate('https://******************', 0, 0, 1, 1)
Global $oLinks = _IETagNameGetCollection($oIE, 'input')
Func _clickButtonRun()
For $oLink In $oLinks
If String($oLink.type) == 'button' And String($oLink.value) == 'RUN' Then
_IEAction($oLink, 'click')
ExitLoop
EndIf
Next
EndFunc
Func _sendKeystrokesSeveralTimes($sKey, $iHowOften = 1)
For $i = 1 To $iHowOften Step 1
Send($sKey)
Sleep(200) ; to increase the robustness wait a bit between each input/send
Next
EndFunc
_clickButtonRun()
_IELoadWait($oIE, 1000)
WinWaitActive('Oracle Fusion Middleware Forms Services', '', 15)
_sendKeystrokesSeveralTimes($sLogin)
_sendKeystrokesSeveralTimes('{TAB}')
_sendKeystrokesSeveralTimes($sPasswd)
_sendKeystrokesSeveralTimes('{TAB}')
_sendKeystrokesSeveralTimes('PROD9')
_sendKeystrokesSeveralTimes('{ENTER}')
WinWaitActive('My Window', '', 5)
_sendKeystrokesSeveralTimes('{TAB}')
_sendKeystrokesSeveralTimes('{DOWN}', 11)
_sendKeystrokesSeveralTimes('{UP}')
_sendKeystrokesSeveralTimes('{RIGHT}')
_sendKeystrokesSeveralTimes('{DOWN}', 4)
_sendKeystrokesSeveralTimes('{RIGHT}')
_sendKeystrokesSeveralTimes('{DOWN}', 2)
_sendKeystrokesSeveralTimes('{RIGHT}')
_sendKeystrokesSeveralTimes('{DOWN}', 5)
_sendKeystrokesSeveralTimes('{RIGHT}')
_sendKeystrokesSeveralTimes('{DOWN}', 4)
_sendKeystrokesSeveralTimes('{ENTER}')
_sendKeystrokesSeveralTimes('03/01/2019')
_sendKeystrokesSeveralTimes('{TAB}', 3)
MouseClick('left', 268, 363)

lua corona - how to disable touch events widget.newScrollView

I have a widget.newScrollView component and a widget.newButton in front of it. Unfortunately when i click my button it also calls my ScrollView "tap" handler. How do i stop my ScrollView from getting this event?
Here is some of the code I'm using:
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
print( "Button was pressed and released" )
end
return true; **I tried this - but it had no effect**
end
added
local button1 = widget.newButton(
{
label = "button",
onEvent = handleButtonEvent,
emboss = false,
shape = "roundedRect",
width = 400,
height = 100,
cornerRadius = 32,
fillColor = { default={1,0,0,1}, over={1,0.1,0.7,1} },
strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} },
strokeWidth = 4,
fontSize=100;
}
I've got an array (planets) of display.NewImages and my handler - like this:
local planets = {};
planets[1] = display.newImage( "planetHexs/001.png", _topLeft_x, _topLeft_y);
planets[2] = display.newImage( "planetHexs/002.png", _topLeft_x, _topLeft_y + _planet_height2 );
....
local scrollView = widget.newScrollView(
{
top = 0,
left = 0,
width = display.actualContentWidth,
height = display.actualContentHeight,
scrollWidth = 0,
scrollHeight = 0,
backgroundColor = { 0, 0, 0, 0.5},
verticalScrollDisabled=true;
}
for i = 1, #planets do
local k = planets[i];
scrollView:insert( k )
end
function PlanetTapped( num )
print( "You touched the object!"..num );
end
for i = 1, #planets do
local k = planets[i];
k:addEventListener( "tap", function() PlanetTapped(i) end )
end
I get this print log:
Button was pressed and released
You touched the object2
You must return true on your event functions to prevent propagation. This essentially tells Corona that the event was handled correctly and that no more event listeners for that event should be fired. You can read more about event propagation here in the docs.
"tap" and "touch" events are handled with different listeners, so if you wish to stop taps when you touch the button, you will have add a "tap" listener to your button as well, that essentially just returns true to prevent, or block tap events to anything behind it.
button1:addEventListener("tap", function() return true end)
Since the button does not have a tap event, tap events simply go through the button, to any objects behind it which do have a "tap" event.

Knowing button press in corona sdk

In my corona aplication, I've a widget button to move an image. I was able to find the onPress method, but failed to find a method to check whether the button is still pressed. So that user don't have to tap the same button over and over again for moving the image...
Code:
function move( event )
local phase = event.phase
if "began" == phase then
define_robo()
image.x=image.x+2;
end
end
local movebtn = widget.newButton
{
width = 50,
height = 50,
defaultFile = "left.png",
overFile = "left.png",
onPress = move,
}
Any help is appreciable...
If your question is that you would like to know when the user's finger is moved, or when he releases the button, you can add handlers for those events:
"moved" a finger moved on the screen.
"ended" a finger was lifted from the screen.
"began" only handles when he starts touching the screen.
So your move function would be like:
function move( event )
local phase = event.phase
if "began" == phase then
define_robo()
image.x=image.x+2;
elseif "moved" == phase then
-- your moved code
elseif "ended" == phase then
-- your ended code
end
end
-- Updated according to comment:
Use this, replacing nDelay by the delay between each move, and nTimes by the number of times you wanna do the move:
function move( event )
local phase = event.phase
if "began" == phase then
local nDelay = 1000
local nTimes = 3
define_robo()
timer.performWithDelay(nDelay, function() image.x=image.x+2 end, nTimes )
end
end
Try this:
local image = display.newRect(100,100,50,50) -- Your image
local timer_1 -- timer
local function move()
print("move...")
image.x=image.x+2;
timer_1 = timer.performWithDelay(10,move,1) -- you can set the time as per your need
end
local function stopMove()
print("stopMove...")
if(timer_1)then timer.cancel(timer_1) end
end
local movebtn = widget.newButton {
width = 50,
height = 50,
defaultFile = "obj1.png",
overFile = "obj1.png",
onPress = move, -- This will get called when you 'press' the button
onRelease = stopMove, -- This will get called when you 'release' the button
}
Keep coding................ :)

How can I remove onEvent from button widget in Corona?

I am trying to remove onEvent listener from button widget. I tried to assign nil to onEvent attribute but it didn't work and lastly I tried this:
buttonWidget : removeEventListener("touch", buttonWidget.onEvent)
I have several button like that and it just stopped all button's event listeners. What do you suggest? How can I remove the event listener for one button widget? Thanks.
Here is how I create my button widgets:
for i=0,2 do
for j=0,8 do
count=count+1
letterBtn[count] = widget.newButton{
id = alphabet[count],
left = 5+j*50,
top = H-160+i*50,
label = alphabet[count],
width = 45,
height = 45,
font = nil,
fontSize = 18,
labelColor = { default = {0,0,0}, over = {255,255,255}},
onEvent = btnOnEventHandler
};
end
end
Can you tell me how can I remove onEvent later?
Okey, I tried Button: setEnabled(false) but still it disables all buttons not just one. I already tried your second advice but it gives the same result. I am copying the rest of the code. Can you please look at it and tell me what I am missing?
local function checkLetter(e)
if(guessWord) then
for i=1, #guessWord do
local c = guessWord:sub(i,i)
if c==e.target.id then
letter[i].text = e.target.id
letterCount = letterCount +1
print("letterCount"..letterCount)
e.target:setEnabled(false)
end
end
if (letterCount == #guessWord and not hanged) then
timer.performWithDelay(500, function()
letterCount=0
rightWGuess = rightWGuess+1
for k,v in pairs(notGuessedWord) do
if v == guessWord then
notGuessedWord[k]=nil
end
end
enableButtons()
startGame() end ,1)
end
end
end
local function btnOnEventHandler(e)
if(e.phase == "began") then
checkLetter(e)
print(e.target.id)
end
return true
end
If you want to temporarily (or permanently) stop a button from responding to touch events, you can use Button:setEnabled(false).
The following worked for me for removing a listener from just 2 buttons. Button 1 and 3 stopped responding to events as expected while 2, 4, and 5 still did.
Update: To disable, you have to do it on the 'ended' phase or Corona gets confused.
widget = require 'widget'
local function btnOnEventHandler(event)
print('Event', event.target.id, event.phase)
if event.phase == 'ended' then
-- Disable the button so it can't be clicked again
-- Must disable in the end state or Corona gets
-- confused
event.target:setEnabled(false)
end
end
local buttons = {}
for i=1,5 do
buttons[i] = widget.newButton{
id = 'button' .. i,
left = display.contentCenterX - 50,
top = 60 * i,
label = 'Button ' .. i,
width = 100,
height = 50,
onEvent = btnOnEventHandler
}
end
buttons[1]:removeEventListener('touch', buttons[1].onEvent)
buttons[3]:removeEventListener('touch', buttons[3].onEvent)

Resources