Examples
From GiderosMobile
Revision as of 16:44, 31 July 2019 by MoKaLux (talk | contribs) (→MOVING A SPRITE IN AN ARC PATH @Disciple: forgot the event listener!)
Here are some examples from the Gideros forum.
All samples are ready to use. Enjoy!
STRING
A COLORED STRING @hgy29
local text = TextField.new(nil, "This is a \e[color=#f005]semi transparent red\e[color] text")
text:setPosition(32, 64)
stage:addChild(text)
GFX
DRAWING A CIRCLE @xxx
xCircle = Core.class(Sprite)
function xCircle:init(r, steps, color)
color = color or 0xff0000
local sin, cos, d2r = math.sin, math.cos, math.pi / 180
local c = Shape.new()
c:setFillStyle(Shape.SOLID, color)
c:setLineStyle(0)
c:beginPath()
c:moveTo(r * sin(0 * d2r), r * cos(0 * d2r))
for i = 0, 360, 360 / steps do
c:lineTo(r * sin(i * d2r), r * cos(i * d2r))
end
c:endPath()
self:addChild(c)
end
--- usage
--- xCircle(10, 5, 0xFF0000)
DRAWING AN ARC @hgvyas123
function drawArc(xc,yc,xradius,yradius,startAngle,endAngle,isFill)
if yradius == nil then
yradius = xradius
end
if startAngle == nil then
startAngle = 0
end
if endAngle == nil then
endAngle = 360
end
if isFill == nil then
isFill = true
end
local shape = Shape.new()
if isFill then
shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
else
shape:setLineStyle(3, 0x000000)
end
shape:beginPath()
for i=startAngle,endAngle do
if i == 1 then
shape:moveTo(math.sin(math.rad(i)) * xradius, math.cos(math.rad(i)) * yradius)
else
shape:lineTo(math.sin(math.rad(i)) * xradius, math.cos(math.rad(i)) * yradius)
end
end
if isFill then
shape:closePath()
end
shape:endPath()
shape:setPosition(xc, yc)
return shape
end
-- usage
--function drawArc(xc,yc,xradius,yradius,startAngle,endAngle,isFill)
local myarc = drawArc(128, 128, 64, 64, 0, 5 * 45, false)
stage:addChild(myarc)
CIRCULAR PROGRESS @hgy29
CircularProgress = Core.class(Sprite)
function CircularProgress:init(radius, width, color, font, fontscale)
self.radiusMin, self.radiusMax = radius - width / 2, radius + width / 2
self.color = color
self.font = font
self:addChild(Path2D.new())
local text = TextField.new(font, "-")
text:setTextColor(color)
text:setScale(fontscale or 1)
self:addChild(text)
-- self:setValue(0.4)
end
function CircularProgress:setValue(val, xcolor, xalpha)
local angs, ange = 0, math.pi * val * 1.999
local rl, rh = self.radiusMin, self.radiusMax
local ta = self:getChildAt(1)
local ra = math.floor(val * 2)
-- ta:setLineColor(self.color, xalpha)
-- ta:setFillColor(self.color, xalpha)
ta:setLineColor(xcolor, xalpha)
ta:setFillColor(xcolor, xalpha)
ta:setPath("MALAZ",
math.sin(angs) * rl, -math.cos(angs) * rl,
rl, rl, 0, ra, 1,
math.sin(ange) * rl, -math.cos(ange) * rl,
math.sin(ange) * rh, -math.cos(ange) * rh,
rh, rh, 0, ra, 0,
math.sin(angs) * rh, -math.cos(angs) * rh
)
ta:setLineThickness(4, 0.5)
ta = self:getChildAt(2)
ta:setText(("%d%%"):format(math.floor(val * 100)))
ta:setTextColor(xcolor)
ta:setAlpha(xalpha)
local tax, tay, taw, tah = ta:getBounds(ta)
ta:setAnchorPosition(tax + taw / 2, tay + tah / 2)
return ta
end
-- usage
-- meter = CircularProgress.new(40, 5, 0x770000, nil, 2)
SAVING IMAGE TO FILE @mokalux
Saving an image or part of an image to disk.
function xsaveJPGtoDoc(xoriginalimgfile, xposx, xposy, xsizew, xsizeh, xdestimgname)
local mytexture = Texture.new(xoriginalimgfile)
local mybmp = Bitmap.new(mytexture)
local rt = RenderTarget.new(mybmp:getWidth(), mybmp:getHeight())
rt:draw(mybmp)
rt:save("|D|tex_"..xdestimgname..".jpg", xposx, xposy, xsizew, xsizeh)
return "|D|tex_"..xdestimgname..".jpg"
end
-- usage
--local xsavedjpg = xsaveJPGtoDoc("gfx/bg/dark.jpg", 0, 0, 64, 64, "dark")
PREVENT FROM DRAGGING A SHAPE OVER OTHER SHAPE @hgvyas123
--[[
Drag the shapes around with your mouse or fingers
This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" target="_blank" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
(C) 2010 - 2011 Gideros Mobile
]]
local myShapes = {}
function Sprite:collidesWith(sprite2)
local x,y,w,h = self:getBounds(stage)
local x2,y2,w2,h2 = sprite2:getBounds(stage)
return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
end
local function onMouseDown(self, event)
if self:hitTestPoint(event.x, event.y) then
self.isFocus = true
self.x0 = event.x
self.y0 = event.y
event:stopPropagation()
end
end
local function onMouseMove(self, event)
if self.isFocus then
self.lastX,self.lastY = self:getPosition()
local dx = event.x - self.x0
local dy = event.y - self.y0
self:setX(self:getX() + dx)
self:setY(self:getY() + dy)
self.x0 = event.x
self.y0 = event.y
for i=1,#myShapes do
if myShapes[i] ~= self then
if self:collidesWith(myShapes[i]) then
self:setPosition(self.lastX,self.lastY)
end
end
end
event:stopPropagation()
end
end
local function onMouseUp(self, event)
if self.isFocus then
self.isFocus = false
event:stopPropagation()
end
end
for i=1,5 do
local shape = Shape.new()
shape:setLineStyle(3, 0x000000)
shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
shape:beginPath()
shape:moveTo(0, 0)
shape:lineTo(100, 0)
shape:lineTo(100, 50)
shape:lineTo(0, 50)
shape:closePath()
shape:endPath()
shape:setX(math.random(0, 320 - 100))
shape:setY(math.random(0, 480 - 50))
shape.isFocus = false
shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape)
shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape)
shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape)
stage:addChild(shape)
table.insert(myShapes, shape)
end
local info = TextField.new(nil, "drag the shapes around with your mouse or fingers")
info:setPosition(23, 50)
stage:addChild(info)
CHECK POINT IN POLYGON @atilim
function pointInPolygon(vertices, x, y)
local intersectionCount = 0
local x0 = vertices[#vertices].x - x
local y0 = vertices[#vertices].y - y
for i= 1, #vertices do
local x1 = vertices[i].x - x
local y1 = vertices[i].y - y
if y0 > 0 and y1 <= 0 and x1 * y0 > y1 * x0 then
intersectionCount = intersectionCount + 1
end
if y1 > 0 and y0 <= 0 and x0 * y1 > y0 * x1 then
intersectionCount = intersectionCount + 1
end
x0 = x1
y0 = y1
end
return (intersectionCount % 2) == 1
end
local vertices = {
{x = 0, y = 0},
{x = 0, y = 100},
{x = 100, y = 100},
{x = 0, y = 0},
}
local shape = Shape.new()
shape:setFillStyle(Shape.SOLID, 0xff0000)
shape:setLineStyle(5, 0x0000ff, 1)
shape:beginPath()
shape:moveTo(vertices[1].x,vertices[1].y)
shape:lineTo(vertices[2].x,vertices[2].y)
shape:lineTo(vertices[3].x,vertices[3].y)
shape:lineTo(vertices[4].x,vertices[4].y)
shape:endPath()
shape:setPosition(64, 64)
stage:addChild(shape)
local vertices_shape = {}
for i = 1, #vertices do
vertices_shape[i] = {x = vertices[i].x + shape:getX(), y = vertices[i].y + shape:getY()}
end
shape:addEventListener(Event.MOUSE_HOVER, function(e)
if pointInPolygon(vertices_shape, e.x, e.y) then
print("hovering the shape")
else
print("not hovering the shape")
end
end)
ANIMATION
MOVING A SPRITE IN AN ARC PATH @Disciple
local bone = Sprite.new()
--local boneImg = Bitmap.new(Texture.new("gfx/enemy01.png")) -- add your gfx!
-- or use this one (Pixel)
local boneImg = Pixel.new(0x0000ff, 0.75, 32, 48)
stage:addChild(bone)
bone:addChild(boneImg)
boneImg:setAnchorPoint(0.5, 0.5)
local grav = -15 / 2
local velx = -3 / 2
local rotvel = -3 / 2
local x = 300 / 2
local y = 400 / 2
local rotation = 0
function enterFrame ()
grav = grav + 0.3
x = x + velx
y = y + grav
rotation = rotation + rotvel
bone:setRotation(rotation)
bone:setPosition(x, y)
end
stage:addEventListener(Event.ENTER_FRAME, enterFrame)
MOVING A SPRITE IN AN HORIZONTAL SINE WAVE @Harrison
local blimp1 = Pixel.new(0x0000ff, 0.5, 32, 64)
blimp1:setAnchorPoint(0.5, 0.5)
blimp1:setPosition(0, 128)
blimp1.speedx = 1.2
stage:addChild(blimp1)
function onEnterFrame(event)
local posx = blimp1:getX() + blimp1.speedx
local posy = blimp1:getY() + (1.5 * math.sin(0.075 * posx))
if blimp1:getX() > application:getContentWidth() + blimp1:getWidth() / 2 then
blimp1:setPosition(-blimp1:getWidth() / 2, 128)
posx = blimp1:getX()
posy = blimp1:getY()
end
blimp1:setPosition(posx, posy)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
MOVING A SPRITE IN A VERTICAL COS WAVE @Harrison
local blimp2 = Pixel.new(0x00ff00, 0.5, 64, 32)
blimp2:setAnchorPoint(0.5, 0.5)
blimp2:setPosition(128, 0)
blimp2.speedy = 1.3
stage:addChild(blimp2)
function onEnterFrame(event)
local posy = blimp2:getY() + blimp2.speedy -- posy first
local posx = blimp2:getX() + 2.5 * math.cos(0.1 * posy) -- posy first
if blimp2:getY() > application:getContentHeight() + blimp2:getHeight() then
blimp2:setPosition(128, -blimp2:getHeight())
posx = blimp2:getX()
posy = blimp2:getY()
end
blimp2:setPosition(posx, posy)
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
FILES
CSV @hgy29
Load a CSV file.
function loadCSV(file)
local function csvsplit(line)
local t, w, l2 = {}, nil, nil
while #line > 0 do
w, l2 = line:match("\"([^\"]*)\"[,\r\n]?(.*)") -- Check for quoted string
if not w then w, line = line:match("([^,]*)[,\r\n]?(.*)") -- Non quoted
else line = l2 end
if not w then break end --Nothing or issue, break
t[#t + 1] = w
end
return t
end
local headers, csv = nil, {}
for line in io.lines(file) do
f = csvsplit(line)
if not headers then -- Assume first line is headers
headers = {} for n, v in ipairs(f) do headers[v] = n end
else
csv[#csv + 1] = f
end
end
csv.getField = function(self, row, field) return self[row][headers[field]] end
return csv
end
--- usage
--csv=loadCSV("135-bis-bt.csv")
--print(csv:getField(4,"Value"))
--print(csv:getField(7,"Time")
JSON @xxx
Loading and saving preferences.
require "json" -- you must add JSON plugin to your app
function saveData(filepath, value)
local contents = json.encode(value)
local file = io.open("|D|"..filepath, "w") -- create file
file:write(contents) -- save json string in file
io.close(file)
end
function getData(filepath)
local value
local file = io.open("|D|"..filepath, "r")
if file then
local contents = file:read("*a") -- read contents
value = json.decode(contents) -- decode json
io.close(file)
end
return value
end
-- globals
g_configfilepath = "myplayground.txt"
g_language = application:getLanguage()
g_theme = "dark"
g_isaudio = true
g_level = 1
-- init prefs
local mydata = getData(g_configfilepath) -- try to read information from file
-- if no prefs file, create it
if not mydata then
mydata = {g_language, g_theme, g_isaudio, g_level}
saveData(g_configfilepath, mydata) -- create file and save datas
else
g_language = mydata[1]
g_theme = mydata[2]
g_isaudio = mydata[3]
g_level = mydata[4]
end
-- save prefs
function mySavePrefs(xlanguage, xtheme, xisaudio, xlevel)
local mydata = {xlanguage or g_language, xtheme or g_theme, xisaudio or g_isaudio, xlevel or g_level}
saveData(g_configfilepath, mydata) -- save new datas
g_language = mydata[1]
g_theme = mydata[2]
g_isaudio = mydata[3]
g_level = mydata[4]
end
--- usage
--mySavePrefs("en", "dark", true, 1)
--print(g_language)
--print(g_theme)
More to come God's willing...