Difference between revisions of "Examples"

From GiderosMobile

m
m
Line 77: Line 77:
 
-- meter = CircularProgress.new(40, 5, 0x770000, nil, 2)
 
-- meter = CircularProgress.new(40, 5, 0x770000, nil, 2)
 
</source>
 
</source>
<br/>
 
  
  
Line 138: Line 137:
 
--print(csv:getField(7,"Time")
 
--print(csv:getField(7,"Time")
 
</source>
 
</source>
<br/>
 
  
  

Revision as of 20:04, 10 July 2019



Here are some examples from the Gideros forum. Enjoy!

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)


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")


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...