Skip to content

Instantly share code, notes, and snippets.

@HoraceBury
HoraceBury / graphicslib.lua
Last active March 21, 2018 09:45
Graphics extension library.
-- graphics extension library
require("mathlib") -- https://gist.github.com/HoraceBury/9431861
require("displaylib") -- https://gist.github.com/HoraceBury/1e2ce033e3441823038eb88b551ad981
-- https://math.stackexchange.com/questions/2269589/calculate-angle-of-the-next-point-on-a-circle
--[[
Generates a table of points containing the outline of a circle with each point 'step' pixels from the previous.
Parameters:
@HoraceBury
HoraceBury / _shadowlib.lua
Last active May 8, 2018 05:52
Shadow library. Given a display group, this will ensure that non-physics-based shadows will be updated to track their parent, whether the parent is physics-based or not. The test sample demonstrates using physics to provide a shadow, as well.
local lib = {}
function lib.newShadowLayer( shadowgroup )
local function refresh()
if (shadowgroup and shadowgroup.numChildren) then
for i=1, shadowgroup.numChildren do
local shadow = shadowgroup[i]
local object = shadowgroup[i].shadowparent
if (not shadow.isBodyActive) then
@HoraceBury
HoraceBury / Cache.cs
Created August 21, 2017 12:14
Generic caching mechanism which makes sliding expiration easy.
using System;
using System.Collections.Generic;
using System.Runtime.Caching;
namespace CacheServices
{
public class Cache
{
protected MemoryCache _cache = new MemoryCache("general");
@HoraceBury
HoraceBury / dragitemscrollview.lua
Created December 30, 2016 14:48
Provides a scrollview widget which can have items dragged off it.
local widget = require("widget")
local function angleOf( ax, ay, bx, by, adjust, positive )
local angle = math.atan2( by-ay, bx-ax ) * 180/math.pi
if (adjust) then
if (type(adjust) ~= "number") then adjust = -90 end
angle = angle - adjust
if (angle < -180) then angle=angle+360 end
if (angle > 180) then angle=angle-360 end
@HoraceBury
HoraceBury / simple drag and rotate demo.lua
Last active October 7, 2016 08:20
This code was an answer to a forum question asking for a touch to drag an object but a tap-touch to rotate the object. I kept it simple and without reference to my mathlib.lua for the sake of brevity.
local function angleOnTarget( e )
local a = math.atan2( e.y-e.target.y, e.x-e.target.x ) * 180 / (4*math.atan(1))
if (a<0) then a=a+360 end
return a
end
local function tap(e)
e.target.touchMode = "drag"
print("drag mode")
e.target.dragtimer = timer.performWithDelay( 400, function()
@HoraceBury
HoraceBury / circlelib.lua
Last active October 26, 2017 00:13
Create a portion of a fill or unfilled circle as a polygon and move it by it's circle-centre. Requires image: http://content.screencast.com/users/HoraceBury/folders/Default/media/18ea951e-71a1-44f4-a148-f2109a1c9059/brush2.png Download full solution: https://www.dropbox.com/s/8472upw9w2cwkwi/CircleLib.2015-12-02.001.zip?dl=0
-- circle lib
require("mathlib")
display.setDefault( "isAnchorClamped", false )
local min, max = -10000000000000, 10000000000000
local cache = {}
local circlelib = {}
@HoraceBury
HoraceBury / shadow.lua
Created April 27, 2015 05:30
Create a drop shadow
local function shadow( width, height, size )
local g = display.newGroup()
g.x, g.y = display.contentCenterX, display.contentCenterY
display.newRect( g, 0, 0, width+size, height+size ).fill = {1,1,1,0}
display.newRect( g, 0, 0, width, height ).fill = {0,0,0}
local c = display.capture( g )
g = display.remove( g )
@HoraceBury
HoraceBury / flood.lua
Last active May 2, 2022 04:13
Flood fill a display object with a Gfx2.0 gradient fill. Can be used to provide the Android Material Design button interaction animation, eg: http://www.google.com/design/spec/components/buttons.html#buttons-flat-raised-buttons
-- ref: http://docs.coronalabs.com/daily/guide/graphics/effects.html#generator.radialgradient
local back, outer, inner = {0,1,0,1}, {0,.9,0,.05}, {0,1,0,.05}
-- backing colour rect
display.newRect( display.actualCenterX*.5, display.actualContentHeight*.25, 220, 220 ).fill = back
-- radial fill rect
local r = display.newRect( display.actualCenterX*.5, display.actualContentHeight*.25, 200, 200 )
local function getRadius( r, xOffset, yOffset )
@HoraceBury
HoraceBury / debugconsole.lua
Last active January 29, 2019 13:38
Debug Print Console
-- debug console
local widget = require("widget")
local lib = {}
local originalPrintFunc = print
local debugPrintFunc = nil
local showingDebugConsole = false
@HoraceBury
HoraceBury / backswipelib.lua
Last active April 18, 2019 12:20
BackSwipe Navigation for Composer
-- swipe library
--[[ Libraries ]]--
local composer = require("composer")
--[[ Fields ]]--
local mainstage = display.getCurrentStage()
local stage = composer.stage