Created
August 21, 2013 19:31
-
-
Save juaxix/6299080 to your computer and use it in GitHub Desktop.
Lua Harmony :: colaborative drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Main | |
supportedOrientations(LANDSCAPE_ANY) | |
myimage = image(WIDTH,HEIGHT) | |
setContext(myimage) | |
function setup() | |
--backingMode(RETAINED) | |
toolbox = ToolBox() | |
parameter.number("clean_canvas",0,1,0) | |
--background(255, 255, 255, 255) | |
--sprite("physicsnotebook:background4", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT) | |
--sprite("Documents:mydraw", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT) | |
noSmooth() | |
end | |
function draw() | |
toolbox:getCurrentBrush():reset() | |
background(255, 255, 255, 255) | |
sprite("physicsnotebook:background4", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT) | |
output.clear() | |
local b = toolbox:getCurrentBrush() | |
if b and #b.touches>0 then | |
b:draw() | |
local p = 0 | |
for _,t in ipairs(b.touches) do | |
p = p + #t.points + #t.decorations | |
end | |
print(p.. " points") | |
print(math.floor(1/DeltaTime).."FPS") | |
end | |
toolbox:draw() | |
end | |
function touched(touch) | |
if not toolbox:touched(touch) then | |
toolbox:getCurrentBrush():touched(touch) | |
end | |
--[[if touch.state == ENDED then | |
setContext() | |
saveImage("Documents:mydraw",myimage) | |
setContext(myimage) | |
end]]-- | |
end | |
-- ToolBox | |
ToolBox = class() | |
function ToolBox:init() | |
self:initBrushes() | |
self:initPalette() | |
parameter.number("thicknessFill", 2, 15, 2) | |
parameter.number("red", 0, 255) | |
parameter.number("green",0, 255) | |
parameter.number("blue",0, 255) | |
parameter.number("alpha", 0, 255, 255) | |
parameter.integer("resetBrushStartStroke",0,1,0) | |
parameter.watch ("_currentBrush") | |
parameter.integer("SelectedBrush",1,#self.brushList,1) | |
end | |
-- Brushes Management | |
function ToolBox:initBrushes() | |
self.brushList = { | |
-- Brush(), | |
Chrome(), | |
--Circles(), | |
Discs(), | |
Fur(), | |
LongFur(), | |
Shaded(), | |
Simple(), | |
Sketchy(), | |
Squares(), | |
Web() | |
} | |
self.brushListName = {} | |
for _,v in pairs(self.brushList) do | |
table.insert(self.brushListName,v:getName()) | |
end | |
self.currentBrushIdx = 1 | |
_currentBrush = self.brushListName[self.currentBrushIdx] | |
end | |
function ToolBox:updateBrush() | |
if self.currentBrushIdx ~= SelectedBrush then | |
self.brushList[self.currentBrushIdx]:reset() | |
self.currentBrushIdx = SelectedBrush | |
_currentBrush = self.brushListName[self.currentBrushIdx] | |
end | |
end | |
function ToolBox:getCurrentBrush() | |
return self.brushList[self.currentBrushIdx] | |
end | |
-- Palette Management | |
function ToolBox:initPalette() | |
self.palette = { | |
color(255, 0, 0, 255), | |
color(255, 127, 0, 255), | |
color(255, 255, 0, 255), | |
color(0, 255, 0, 255), | |
color(0, 255, 255, 255), | |
color(0, 0, 255, 255), | |
color(127, 0, 255, 255), | |
color(255, 0, 255, 255), | |
color(0, 0, 0, 255), | |
color(51, 51, 51, 255), | |
color(102, 102, 102, 255), | |
color(153, 153, 153, 255), | |
color(204, 204, 204, 255), | |
color(255, 255, 255, 255) | |
} | |
end | |
function ToolBox:getPaletteCol(index) | |
return self.palette[index] | |
end | |
function ToolBox:setPaletteCol(index,r,g,b,a) | |
local c = self.palette[index] | |
c.r = r | |
c.g = g | |
c.b = b | |
c.a = a | |
end | |
local selectedPaletteIdx = 0 | |
local paletteOffset = 5 | |
local paletteSize = 30 | |
local selPaletteSize = 25 | |
local menuBar = 50 | |
local shadowWidth = 6 | |
local baseCol = 200 | |
local startPos = HEIGHT/1.4 - 50 | |
function ToolBox:getPaletteId(y) | |
for pCount = 1, #self.palette do | |
local topPalSquare = (startPos - ((paletteSize + 5) * pCount)) + | |
(paletteSize/2) | |
local botPalSquare = topPalSquare - paletteSize | |
if y < topPalSquare and y > botPalSquare then | |
return pCount | |
end | |
end | |
return 0 | |
end | |
function ToolBox:drawPalette() | |
for pCount = 1, #self.palette do | |
if selectedPaletteIdx == pCount then | |
pushStyle() | |
c = self:getPaletteCol(pCount) | |
if handledColSelect then | |
red = c[1] | |
green = c[2] | |
blue = c[3] | |
alpha = c[4] | |
handledColSelect = false | |
end | |
fill(c) | |
strokeWidth(2) | |
stroke(255, 255, 255, 255) | |
rectMode(CENTER) | |
rect(menuBar/2, (startPos - (paletteSize + paletteOffset) * | |
pCount), selPaletteSize, selPaletteSize ) | |
popStyle() | |
else | |
c = self:getPaletteCol(pCount) | |
fill(c) | |
pushStyle() | |
rectMode(CENTER) | |
rect(menuBar/2, startPos - (paletteSize + paletteOffset) * | |
pCount, paletteSize, paletteSize ) | |
popStyle() | |
end | |
pushStyle() | |
fill(255) | |
popStyle() | |
end | |
end | |
function ToolBox:draw() | |
self:updateBrush() | |
-- bit of eyecandy drop shadow | |
for shadow = 1, shadowWidth do | |
local gradFactor = 255 - (baseCol/shadowWidth) * shadow | |
fill(gradFactor, gradFactor, gradFactor, 255) | |
rect(0, 0, menuBar + shadowWidth - shadow, HEIGHT) | |
end | |
fill(234, 234, 234, 255) | |
rect(0, 0, menuBar, HEIGHT) | |
--create the current brush style with white backing, | |
-- white impotant when using alpha < 255 | |
pushStyle() | |
stroke(255, 255, 255, 255) | |
strokeWidth(1) | |
fill(255) | |
ellipse(menuBar/2, HEIGHT - paletteSize, thicknessFill * 2) | |
fill(red,green,blue,alpha) | |
ellipse(menuBar/2, HEIGHT - paletteSize, thicknessFill * 2) | |
popStyle() | |
self:drawPalette() | |
if selectedPaletteIdx ~= 0 then | |
self:setPaletteCol(selectedPaletteIdx,red,green,blue,alpha) | |
end | |
end | |
function ToolBox:touched(touch) | |
if touch.state == BEGAN then | |
if touch.x < menuBar then | |
handledColSelect = true | |
local yColSelect = touch.y | |
local paletteIdx = self:getPaletteId(yColSelect) | |
if selectedPaletteIdx ~= paletteIdx then | |
selectedPaletteIdx = paletteIdx | |
else | |
selectedPaletteIdx = 0 | |
end | |
return true | |
end | |
end | |
return false | |
end | |
--# Brush | |
-- Brush | |
Brush = class() | |
function Brush:init(name) | |
assert(name) | |
self.name = name | |
self.touches = {} | |
end | |
function Brush:reset() | |
end | |
function Brush:getName() | |
return self.name | |
end | |
function Brush:strokeStart(x,y) | |
end | |
function Brush:stroke(x,y) | |
end | |
function Brush:strokeEnd() | |
end | |
function Brush:addDecoration(k,x,y) | |
end | |
function Brush:createTouch(t) | |
local touch = { | |
tid = t.id, | |
points = { | |
vec2(t.x,t.y) | |
}, | |
decorations = { | |
} | |
} | |
table.insert(self.touches, touch) | |
end | |
function Brush:findTouch(tid) | |
local t = self.touches | |
for i=1, #self.touches do | |
if t[i].tid == tid then | |
return i | |
end | |
end | |
return -1 | |
end | |
function Brush:touched(touch) | |
if touch.state == BEGAN then | |
self:createTouch(touch) | |
elseif touch.state == MOVING or touch.state == ENDED then | |
local k = self:findTouch(touch.id) | |
if k>-1 then | |
table.insert(self.touches[k].points, vec2(touch.x,touch.y)) | |
if #self.touches[k].points>1 then | |
self:addDecoration(k, touch.x, touch.y) | |
end | |
end | |
end | |
end | |
--# Chrome | |
Chrome = class(Brush) | |
function Chrome:init() | |
BrushEx.init(self,"Chrome") | |
end | |
function Chrome:addDecoration(k,x,y) | |
local t = self.touches[k] | |
local p = t.points | |
local s = t.decorations | |
local n = #p | |
local dx, dy, d | |
for i=1, n do | |
dx = p[i].x-p[n].x | |
dy = p[i].y-p[n].y | |
d = dx * dx + dy * dy | |
if (x~=p[i].x and y~=p[i].y) and d < 666 then | |
stroke(red,green,blue,alpha*0.2) | |
table.insert(s,vec2(p[n].x + dx * 0.2,p[n].y + dy * 0.2) ) | |
table.insert(s,vec2(p[i].x - dx * 0.2,p[i].y - dy * 0.2) ) | |
end | |
end | |
--[[ | |
for i = 1, #self.points do | |
dx = self.points[i].x - self.points[self.count].x | |
dy = self.points[i].y - self.points[self.count].y | |
d = dx * dx + dy * dy | |
if d < 1000 then | |
line(self.points[self.count].x + dx * 0.2, | |
self.points[self.count].y + dy * 0.2, | |
self.points[i].x - dx * 0.2, | |
self.points[i].y - dy * 0.2) | |
end | |
end | |
]]-- | |
end | |
function Chrome:drawPoints(t,alphadiffer) | |
local n = #t | |
for i = 1, n-1 do | |
if i>1 then | |
stroke(red,green,blue,alpha*alphadiffer) | |
line(t[i-1].x,t[i-1].y,t[i].x,t[i].y) | |
end | |
end | |
end | |
function Chrome:draw() | |
if #self.touches>0 then | |
--self:stroke(self.prevX,self.prevY,self.prevX,self.prevY) | |
pushStyle() | |
-- stroke(red,green,blue,alpha) | |
strokeWidth(thicknessFill) | |
local m = #self.touches | |
for j = 1, m do | |
local t = self.touches[j] | |
self:drawPoints( t.points, 1 ) | |
self:drawPoints( t.decorations, 0.2) | |
end | |
popStyle() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment