Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2015 03:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/55c48784790b35375ef6 to your computer and use it in GitHub Desktop.
Save anonymous/55c48784790b35375ef6 to your computer and use it in GitHub Desktop.
Color Transition Matrix
--Created by Tanner Gower (triggdev)
local colormatrix = {}
local function getBackgroundColorMatrix(colors, pw, pt)
local matrix = {}
local t = 0
local d = 0
for i = 1, 3, 1 do
matrix[i] = {}
end
for c = 1, 3, 1 do
t = colors[1][c]
for s = 1, pt - 1, 1 do
for x = (s-1) * pw, pw * s, 1 do
t = t + ((colors[s+1][c] - colors[s][c]) / pw)
matrix[c][x] = t
end
end
end
return matrix
end
colormatrix.getBackgroundColorMatrix = getBackgroundColorMatrix
return colormatrix
display.setStatusBar( display.DefaultStatusBar )
local bg = display.newRect(display.contentWidth/2, display.contentHeight/2, display.contentWidth, display.contentHeight )
local widget = require "widget"
local composer = require "composer"
local springboard = require "springboard"
local page1 = require "page1"
local page2 = require "page2"
local page3 = require "page3"
local cm = require "colormatrix"
local group = display.newGroup()
local pageTotal = 3
local board
-- R G B
local backgroundColors = { {0, 204, 153}, --Page 1
{255, 134, 0}, --2
{51, 51, 204}} --3
local backgroundMatrix = cm.getBackgroundColorMatrix(backgroundColors, display.contentWidth, pageTotal)
--This is how you change the color, SET FPS TO 60 IN config.lua for smoothest results
local function updateBgColor(event)
local vx, vy = board.view:getContentPosition()
bg:setFillColor( backgroundMatrix[1][math.round(-vx)]/255,
backgroundMatrix[2][math.round(-vx)]/255,
backgroundMatrix[3][math.round(-vx)]/255, 1 )
end
Runtime:addEventListener( "enterFrame", updateBgColor )
-- create springboard
local boardGroup = display.newGroup();
group:insert(boardGroup)
board = springboard.new(boardGroup, display.contentWidth, display.contentHeight, pageTotal, nil, {backgroundColor={1,1,1, 0}, time=200})
boardGroup.x = 0
boardGroup.y = 0
board.view:insert(page1)
board.view:insert(page2)
board.view:insert(page3)
board:goToPage(2, 0) -- default page
local page1Group = display.newGroup()
local number = 0
-- create some text
local bg = display.newRect(display.contentWidth/2 + ((number) * display.contentWidth), display.contentHeight / 2, display.contentWidth, display.contentHeight )
bg:setFillColor(0,0,0,0)
local title = display.newText( "Page "..number, 0, 0, native.systemFont, 32 )
title:setFillColor( 1 ) -- black
title.x = display.contentWidth * 0.5 + (display.contentWidth * number)
title.y = 125
-- insert everything
page1Group:insert( bg )
page1Group:insert( title )
page1Group.anchorY = 0;
page1Group.anchorX = 0;
return page1Group
local page2Group = display.newGroup()
local number = 1
-- create some text
local bg = display.newRect(display.contentWidth/2 + ((number) * display.contentWidth), display.contentHeight / 2, display.contentWidth, display.contentHeight )
bg:setFillColor(0,0,0,0)
local title = display.newText( "Page "..number, 0, 0, native.systemFont, 32 )
title:setFillColor( 1 ) -- black
title.x = display.contentWidth * 0.5 + (display.contentWidth * number)
title.y = 125
-- insert everything
page2Group:insert( bg )
page2Group:insert( title )
page2Group.anchorY = 0;
page2Group.anchorX = 0;
return page2Group
local page3Group = display.newGroup()
local number = 2
-- create some text
local bg = display.newRect(display.contentWidth/2 + ((number) * display.contentWidth), display.contentHeight / 2, display.contentWidth, display.contentHeight )
bg:setFillColor(0,0,0,0)
local title = display.newText( "Page "..number, 0, 0, native.systemFont, 32 )
title:setFillColor( 1 ) -- black
title.x = display.contentWidth * 0.5 + (display.contentWidth * number)
title.y = 125
-- insert everything
page3Group:insert( bg )
page3Group:insert( title )
page3Group.anchorY = 0;
page3Group.anchorX = 0;
return page3Group
module(..., package.seeall)
local widget = require( "widget" )
local math_abs = math.abs
local math_floor = math.floor
local system_getTimer = system.getTimer
local springboard = { }
local springboard_mt = { __index = springboard }
function springboard.new(group, pageWidth, pageHeight, pages, onScrollComplete, params)
local touchTime, scrollView
local obj = {pageWidth=pageWidth, pageHeight=pageHeight, pages=pages, page=1, onScrollComplete=onScrollComplete}
obj.backgroundColor=(params and params.backgroundColor) or nil
obj.time=(params and params.time) or nil
local function scrollListener(event)
local phase = event.phase
if phase == "began" then
touchTime = event.time
elseif phase == "moved" and not touchTime then -- no began phase when focus is explicetly transferred to springboard with takeFocus
touchTime = event.time
elseif phase == "ended" or phase == "off" or phase == "cancelled" then
touchTime = touchTime or system_getTimer()
local motionTime = system_getTimer() - touchTime
local dist = event.xStart - event.x
local swipeDist = math_abs(dist)
local nextPage
-- calculate next page index (negative or 0)
if (motionTime <= 300 and swipeDist >= 30) then
-- fast page flip
nextPage = obj.page + dist/swipeDist
else
-- slow controlled drag
local vx, vy = scrollView:getContentPosition()
nextPage = math_floor(-vx/pageWidth) + ((-vx%pageWidth > 0.5*pageWidth and 1) or 0) + 1
end
-- move to page
if nextPage >= 1 and nextPage <= pages then
-- snap to next page
scrollView:scrollToPosition({x=-(nextPage-1)*pageWidth, onComplete=onScrollComplete, time=obj.time})
obj.page = nextPage
else
-- out of bounds snap quick because fuck out of bounds
scrollView:scrollToPosition({x=-(obj.page-1)*pageWidth, onComplete=onScrollComplete, time=50})
end
end
return true
end
scrollView = widget.newScrollView
{
top = 0,
left = 0,
width = pageWidth,
height = pageHeight,
scrollWidth = pageWidth * pages,
scrollHeight = pageHeight,
listener = scrollListener,
backgroundColor = obj.backgroundColor,
hideBackground = not obj.backgroundColor,
verticalScrollDisabled = true,
hideScrollBar = true,
}
group:insert(scrollView)
obj.view = scrollView
return setmetatable(obj, springboard_mt)
end
function springboard:goToPage(page, time)
local options = {x = -(page-1)*self.pageWidth, onComplete = self.onScrollComplete, time=(time or self.time)}
self.view:scrollToPosition(options)
self.page = page
end
return springboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment