Refresh image memory in awesome WM - lua

Here's a function to create a widget (currently a wibox) based on screen #1. The floutage.sh script creates a blur image of a current screenshot of this screen.
When using this function, it's always the same blurred image showed: 1st blurred image processed, yet screen.png content changes.
function widget.createWidget(args)
local w = nil
local file = "/tmp/screen.png"
awful.spawn.easy_async_with_shell(
"sh /home/david/.config/awesome/widgets/floutage.sh " .. file,
function()
w = wibox({
x = 0,
y = 0,
width = 1200,
height = 1920,
border_width = 0,
screen = screen[1],
bgimage = file,
ontop = true,
visible = true
})
--
w:buttons(
gears.table.join(
awful.button({}, 1,
function()
w.visible = false
w = nil
end
)
)
)
--
end
)
--
return w
end
If I change file variable using a different name, the correct blurred image is shown:
local file = "/tmp/" .. os.date("%Y%m%d-%H%M%S") .. ".png"
How can I simply use "/tmp/screen.png"? (refresh image memory?)

bgimage = gears.surface.load_uncached(file),

Related

How do I set the visibility of a widget to false?

So, I have this function:
local function addMainMenu()
local widget = require( "widget" )
-- Function to handle button events
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
scene = "GAME"
end
end
-- Create the widget
local button1 = widget.newButton(
{
label = "button",
onEvent = handleButtonEvent,
emboss = false,
-- Properties for a rounded rectangle button
shape = "roundedRect",
width = 200,
height = 40,
cornerRadius = 2,
fillColor = { default={0.9,0.9,0.9,1}, over={1,0.1,0.7,0.4} },
strokeColor = { default={0,0,0,1}, over={0.8,0.8,1,1} },
strokeWidth = 5
}
)
-- Center the button
button1.x = display.contentCenterX
button1.y = display.contentCenterY
-- Change the button's label text
button1:setLabel( "Start Game" )
end
That adds the button to start the game, and then I have:
local function enterFrame()
local dt = getDeltaTime()
if (scene == "MAIN_MENU") then
addMainMenu()
elseif (scene == "GAME") then
if (running == false) then
startGame()
else
moveBg(dt)
moveEnemy(enemy)
updateScore()
end
elseif (scene == "GAME_OVER") then
local gameOverLabel = display.newText( "Game Over!", 50, 20, native.systemFont, 16)
gameOverLabel:setFillColor(1, 1, 1)
end
end
As you can see, once I click on the button to start, the scene changes to "GAME", and the button should go away. The thing is: It stays there. And reading the docs, I can't find a way to set its visibility to false. How can I cease displaying a widget?
To make it not visible use .isVisible:
button1.isVisible = false
To hide it use .alpha
button1.alpha = 0.00
-- or hide just a little by 50%
button1.alpha = 0.50
To remove it:
display.remove( button1)
button1= nil
A ButtoWidget inherits from GroupObject which inherits from DisplayObject which provides the property isVisible
Overview
Controls whether the object is visible on the screen. The
property is also readable. The default is true.
Example
local rect1 = display.newRect( 100, 100, 50, 50 )
rect1:setFillColor( 0.7 )
local rect2 = display.newRect( 150, 100, 50, 50 )
rect2:setFillColor( 1, 0, 0, 0.6 )
rect2.isVisible = false
button1.isVisible = false will hide the button. It will disappear with the next screen update.
In case you don't need the button anymore you can as well just remove it by calling button1:removeSelf() or by removing it from it's parent group

Unable to create a working sprite

I am unable to create a working animation sprite for this single area of my app.
I am using Corona SDK and have the following sprite:
This is names mainCharacter.png. I have a double sized version called mainCharacter#2x.png.
I have sheet options, 2 sequences, I'm building an image sheet and passing that to my sprite:
local playerSheetOptions =
{
width = 50,
height = 50,
numFrames = 17,
sheetContentWidth = 500,
sheetContentHeight = 100
}
local playerSequences = {
{
name = "idle",
start = 1,
count = 12,
time = 1200,
loopCount = 0,
loopDirection = "bounce"
},
{
name = "jump",
start = 13,
count = 5,
time = 600,
loopCount = 1
},
}
local playerSheet = graphics.newImageSheet( "resource/images/mainCharacter.png", playerSheetOptions )
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
I am getting the following error:
display.newSprite() requires argument #2 to a table containing sequence data
If I print the relevant data:
print(gameSheet)
print(playerSheet)
print(playerSequences)
I get:
14:27:05.703 userdata: 12445228
14:27:05.703 userdata: 0CF42600
14:27:05.703 table: 0CF41FD0
Where am I going wrong? I have tried simplifying the sequences a lot, but still get the same thing.
Use
local player = display.newSprite(playerSheet, playerSequences)
instead of
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
From Corona documentation
Once the image sheet(s) and sequences are set up, a new sprite object
can be created with the display.newSprite() API:
display.newSprite( [parent,] imageSheet, sequenceData )
For this API, the parent
parameter is optional and represents the display group in which to
insert the sprite. The imageSheet parameter defines the default image
sheet for the sprite, and sequenceData is the table that contains all
of the sequences for the sprite.
Read more about Sprite Animation.

ScrollView or NewText is not infinite

I am making a changelog for my app and I inserted a bunch of text into display.newText. Is there a limit to the amount of characters display.newText can take or is it a problem in ScrollView? My code:
local widget = require( "widget" )
scrollView = widget.newScrollView
{
hideBackground = true,
hideScrollBar = true,
left = 0,
top = 170,
width = display.contentWidth,
height = contH - 300,
topPadding = 150,
bottomPadding = 20,
horizontalScrollDisabled = true,
verticalScrollDisabled = false,
}
ChangeLogText = [[ ...a bunch of text here... ]]
ChangeLog = display.newText(ChangeLogText, contW / 2, 4730, contW - 100, 0, "Fonts/visitor1.ttf", 40)
scrollView:insert(ChangeLog)
I can see ~164 lines of text, but it just stops there... Is there a way to make it infinite?
I had the same problem just recently - too bad that such limitations are detailed nowhere in the API.
I started with this github repository, witch contains the full widget library of Corona SDK (it's no longer developed AFAIK), but managed to find nothing.
The workaround I eventually settled with, is to start with only the first part of the text inserted into the text display object, and then use scrollView scrollListener to reload text whenever top/bottom limit is reached.
Part of scrollListener (event.direction == "up" is basically the mirror image of this):
elseif ( event.direction == "down" ) then
--check if limit is reached or close to it, y is from scrollView:getContentPosition(), -800 is arbitrary
if ( event.limitReached ) or y > - 800 then print( "Reached top limit" )
--firstS stores number of first visible sign of a string
if chat.firstS > 1 then
--copy string
local shownText = stringText
--update display object
chat.text = shownText
local firstS, lastS
--do while display object is too high - I found that scrollView stops scrolling ~25000 height
while (chat.height) > 24000 do
--basically cut the text in half
firstS = chat.firstS - #shownText/4
if firstS < 1 then firstS = 1 end
lastS = firstS + #shownText/2
shownText = string.sub( stringText, firstS, lastS)
chat.text = shownText
end
chat.firstS = firstS
chat.lastS = lastS
--scroll to halfway position in no time for seamless transition
mainView:scrollToPosition({y = -(chat.height)/2, time = 0})
end
end
end
This is an early version and needs some refinement, but I hope it helps. If you find anything better, let me know. Also, I display text from bottom, so you might need to adjust it accordingly.

Lua - Corona SDK - Text not appearing inside a display Object

I am trying to move a display object that is hiding over the right side of the screen, into the scene. It works wonderfully with images (i.e. the background), but not with texts (the coords seem correct from debugging them with print(), but they never display, I already tried the obj:toFront).
I thought that they may work inside display objects, so I put everything in a display Object: Nothing. Just the text? Neither. Anyone knows why/how to override this?
function tscreen:init()
local textGroup = display.newGroup()
local menuBackground = self:getBtn("src/bgMenu.png")
menuBackground.isVisible = false
menuBackground.anchorX = 0.5
menuBackground.anchorY = 0.5
self.menuBackground = menuBackground
local optionsText = {
parent = textGroup,
text = "Hello World",
x = centerX,
y = centerY,
width = 128,
font = native.systemFontBold,
fontSize = 14,
align = "center"
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 0, 0 )
setPos(textGroup, W, 0)
--setPos() is a custom function that assigns x and y coords
textGroup.isVisible = false
self.textGroup = textGroup
end
function tscreen:show()
local menuBackground = self.menuBackground
local textGroup = self.textGroup
local inTime = 1200
setPos(menuBackground, 2*W + centerX, centerY)
menuBackground.isVisible = true
setPos(textGroup, W, 0)
textGroup.isVisible = true
self:cancelTween(menuBackground)
self:cancelTween(textGroup)
menuBackground.tween = transition.to(menuBackground, {time = inTime, transition = easing.outExpo, x = centerX,
onComplete = function()
tscreen:cancelTween(menuBackground)
end
})
textGroup.tween = transition.to(textGroup, {time = inTime, transition = easing.outExpo, x = 0,
onComplete = function()
tscreen:cancelTween(textGroup)
print(getPos(textGroup), textGroup.width, textGroup.height)
end
})
end
I have the starters edition of Corona, so I don't have the recently implemented Composer API.
Maybe this isn't the most appropriate site to post this query since there already is a Corona SDK forum, but I'm trying anyway.
I'm not seeing anything wrong, but a group should not be necessary. Verify that the text can be seen ever: in the init(), do
local optionsText = {
text = "Hello World",
x = 0,
y = 100,
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 1, 1 )
If you can't see the text then something else is going on, maybe your init() is not being called or such. Once you see it, change the parameters to what you want (fill color etc), and test. If still works, add a transition, right after, in the init():
local easeXto100 = {
time = 2000,
transition = easing.outExpo,
x = 100,
onComplete = function() print('did you see text move from x=0 to 100?') end
}
transition.to(workText, easeXto100)
If you see it move, then move the relevant parts of code to your show(), if now it disappears this will give you clue.

In Corona SDK how to add label(text) to images?

In corona SDK how to add label(text) to images
I have created my image as follow
local item = display.newImageRect('images/foo.png',70,70);
item.x = 50;
item.y = 50;
How to add a text to the image?
Many Thanks
To add text to the image, so it appears to "stick" to the image, you could create the object (as you have) and also the text object directly above it. Then, create a group and stick them both in it.
Here is an example of how that could be done:
local myGroup = display.newGroup()
local item = display.newImageRect('images/foo.png',70,70)
item.x = 50
item.y = 50
local text = display.newText( "My Text", 0, 0, "Helvetica", 18 )
text:setTextColor( 0, 0, 0, 255 )
-- insert items into group, in the order you want them displayed:
myGroup:insert( item )
myGroup:insert( text )
Then, to move it around, just modify the x/y of 'myGroup'.
Hope that helps!

Resources