From 6abf8569d8a91a48049ed99c2efc9eb10af8d572 Mon Sep 17 00:00:00 2001 From: Arturs Sosins Date: Thu, 27 Sep 2012 23:14:44 +0300 Subject: [PATCH] Initial commit --- .gitattributes | 22 ++ .gitignore | 163 ++++++++++++++ GiderosInit.gproj | 7 + init.lua | 551 ++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 27 +++ 5 files changed, 770 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 GiderosInit.gproj create mode 100644 init.lua create mode 100644 main.lua diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ebd21a --- /dev/null +++ b/.gitignore @@ -0,0 +1,163 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store diff --git a/GiderosInit.gproj b/GiderosInit.gproj new file mode 100644 index 0000000..219ee57 --- /dev/null +++ b/GiderosInit.gproj @@ -0,0 +1,7 @@ + + + + + + + diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..d43907c --- /dev/null +++ b/init.lua @@ -0,0 +1,551 @@ +--[[ + HELPING FUNCTIONS +]]-- + +--function for adding methods +function addMethod(object, funcName, func) + object[funcName] = func +end + +--function for overriding methods +function overRideMethod(object, func, index, callback) + if object ~= nil and object[func] ~= nil and object["__"..func] == nil then + object["__"..func] = object[func] + object[func] = function(...) + if arg[index] then + arg[index] = callback(arg[index]) + end + return object["__"..func](unpack(arg)) + end + end +end + +local function print_r (t, indent, done) + done = done or {} + indent = indent or '' + local nextIndent -- Storage for next indentation value + for key, value in pairs (t) do + if type (value) == "table" and not done [value] then + nextIndent = nextIndent or + (indent .. string.rep(' ',string.len(tostring (key))+2)) + -- Shortcut conditional allocation + done [value] = true + print (indent .. "[" .. tostring (key) .. "] => Table {"); + print (nextIndent .. "{"); + print_r (value, nextIndent .. string.rep(' ',2), done) + print (nextIndent .. "}"); + else + print (indent .. "[" .. tostring (key) .. "] => " .. tostring (value).."") + end + end +end + +--[[ + SPRITE EXTENSIONS +]]-- + +--[[ z-axis manipulations ]]-- + +function Sprite:bringToFront() + local parent = self:getParent() + if parent then + parent:addChild(self) + end +end + +function Sprite:sendToBack() + local parent = self:getParent() + if parent then + parent:addChildAt(self, 0) + end +end + +function Sprite:setIndex(index) + local parent = self:getParent() + if parent then + if index y2+h2) or (x > x2+w2) or (x+w < x2)) +end + +--[[ messing up with set method ]]-- + +Sprite._set = Sprite.set + +function Sprite:set(param, value) + if Sprite.transform[param] then + local matrix = self:getMatrix() + matrix[param](matrix, value) + self:setMatrix(matrix) + else + local _, _, width, height = self:getBounds(stage) + if param == "x" and type(value) == "string" then + local xPosition = { + left = 0, + center = (application:getContentWidth() - width)/2, + right = application:getContentWidth() - width + } + if xPosition[value] then + value = xPosition[value] + end + end + if param == "y" and type(value) == "string" then + local yPosition = { + top = 0, + center = (application:getContentHeight() - height)/2, + bottom = application:getContentHeight() - height + } + if yPosition[value] then + value = yPosition[value] + end + end + Sprite._set(self, param, value) + end +end + +--[[ position wrappers ]]-- + +function Sprite:setX(x) + self:set("x", x) +end + +function Sprite:setY(y) + self:set("y", y) +end + +function Sprite:setPosition(x, y) + self:set("x", x) + self:set("y", y) +end + +--[[ skew transformation ]]-- + +Sprite.transform = { + "skew", + "skewX", + "skewY" +} +function Sprite:setSkew(xAng, yAng) + self:set("skewX", xAng) + self:set("skewY", yAng) +end + +function Sprite:setSkewX(xAng) + self:set("skewX", xAng) +end + +function Sprite:setSkewY(yAng) + self:set("skewY", yAng) +end + +--[[ flipping ]]-- + +function Sprite:flipHorizontal() + self:setScaleX(-1) +end + +function Sprite:flipVertical() + self:setScaleY(-1) +end + +--[[ hiding/showing visually and from touch/mouse events ]]-- + +function Sprite:hide() + if not self.isHidden then + self.xScale, self.yScale = self:getScale() + self:setScale(0) + self.isHidden = true + end +end + +function Sprite:isHidden() + return self.isHidden +end + +function Sprite:show() + if self.isHidden then + self:setScale(self.xScale, self.yScale) + end +end + +--[[ + SHAPE EXTENSIONS +]]-- + +--[[ draw a polygon from a list of vertices ]]-- + +function Shape:drawPoly(points) + self:beginPath() + for i,p in ipairs(points) do + self:lineTo(p[1], p[2]) + end + self:closePath() + self:endPath() +end + +--[[ draw rectangle ]]-- + +function Shape:drawRect(width, height) + self:drawPoly({ + {0, 0}, + {width, 0}, + {width, height}, + {0, height} + }) +end + +--[[ draw elipse from ndoss ]]-- + +function Shape:drawEllipse(x,y,xradius,yradius,startAngle,endAngle,anticlockwise) + local sides = (xradius + yradius) / 2 -- need a better default + local dist = 0 + + -- handle missing entries + if startAngle == nil then startAngle = 0 end + if endAngle == nil then endAngle = 2*math.pi end + + -- Find clockwise distance (convert negative distances to positive) + dist = endAngle - startAngle + if (dist < 0) then + dist = 2*math.pi - ((-dist) % (2*math.pi)) + end + + -- handle clockwise/anticlockwise + if anticlockwise == nil or anticlockwise == false then + -- CW + -- Handle special case where mod of the two angles is equal but + -- they're really not equal + if dist == 0 and startAngle ~= endAngle then + dist = 2*math.pi + end + else + -- CCW + dist = dist - 2*math.pi + + -- Handle special case where mod of the two angles is equal but + -- they're really not equal + if dist == 0 and startAngle ~= endAngle then + dist = -2*math.pi + end + + end + self:beginPath() + -- add the lines + for i=0,sides do + local angle = (i/sides) * dist + startAngle + self:lineTo(x + math.cos(angle) * xradius, + y + math.sin(angle) * yradius) + end + self:closePath() + self:endPath() + +end + +--[[ draw arc from ndoss ]]-- +function Shape:drawArc(centerX, centerY, radius, startAngle, endAngle, anticlockwise) + self:drawEllipse(centerX, centerY, radius, radius, startAngle ,endAngle, anticlockwise) +end + +--[[ draw circle from ndoss ]]-- + +function Shape:drawCircle(centerX, centerY, radius, anticlockwise) + self:drawEllipse(centerX, centerY, radius, radius, 0, 2*math.pi, anticlockwise) +end + +--[[ + SOUNDS EXTENSIONS +]]-- + +--[[ loop sounds ]]-- + +function Sound:loop() + return self:play(0, math.huge) +end + +--[[ + MATRIX EXTENSIONS +]]-- + +function Matrix:rotate(deg) + local rad = math.rad(deg) + self:multiply(Matrix.new(math.cos(rad), math.sin(rad), -math.sin(rad), math.cos(rad), 0, 0)) +end + +function Matrix:translate(x,y) + if not y then y = x end + self:multiply(Matrix.new(1, 0, 0, 1, x, y)) +end + +function Matrix:translateX(x) + self:translate(x, 0) +end + +function Matrix:translateY(y) + self:translate(0, y) +end + +function Matrix:scale(x,y) + if not y then y = x end + self:multiply(Matrix.new(x, 0, 0, y, 0, 0)) +end + +function Matrix:scaleX(x) + self:scale(x, 1) +end + +function Matrix:scaleY(y) + self:scale(1, y) +end + +function Matrix:skew(xAng,yAng) + if not yAng then yAng = xAng end + xAng = math.rad(xAng) + yAng = math.rad(yAng) + self:multiply(Matrix.new(1, math.tan(yAng), math.tan(xAng), 1, 0, 0)) +end + +function Matrix:skewX(xAng) + self:skew(xAng, 0) +end + +function Matrix:skewY(yAng) + self:skew(0, yAng) +end + +function Matrix:multiply(matrix) + local m11 = matrix:getM11()*self:getM11() + matrix:getM12()*self:getM21() + local m12 = matrix:getM11()*self:getM12() + matrix:getM12()*self:getM22() + local m21 = matrix:getM21()*self:getM11() + matrix:getM22()*self:getM21() + local m22 = matrix:getM21()*self:getM12() + matrix:getM22()*self:getM22() + local tx = self:getTx() + matrix:getTx() + local ty = self:getTy() + matrix:getTy() + self:setElements(m11, m12, m21, m22, tx, ty) +end + +function Matrix:copy() + return Transform.new(self:getElements()) +end + +function Matrix:apply(obj) + if obj.setMatrix then + obj:setMatrix(self) + end +end + +function Matrix:reset() + self:setElements(1, 0, 0, 1, 0, 0) +end + +--[[ + MOUSE/TOUCH EVENTS CROSSCOMPATABILITY +]]-- + +local os = application:getDeviceInfo() + +if os == "Windows" or os == "Mac OS" then + EventDispatcher.__addEventListener = EventDispatcher.addEventListener + + local function wrapper(t, e) + e.touch = {} + e.touch.id = 1 + e.touch.x = e.x + e.touch.y = e.y + + if t.data then + t.listener(t.data, e) + else + t.listener(e) + end + end + + function EventDispatcher:addEventListener(type, listener, data) + if type == Event.TOUCHES_BEGIN then + self:__addEventListener(Event.MOUSE_DOWN, wrapper, {listener = listener, data = data}) + elseif type == Event.TOUCHES_MOVE then + self:__addEventListener(Event.MOUSE_MOVE, wrapper, {listener = listener, data = data}) + elseif type == Event.TOUCHES_END then + self:__addEventListener(Event.MOUSE_UP, wrapper, {listener = listener, data = data}) + else + self:__addEventListener(type, listener, data) + end + end +end + +--[[ + NAMED COLORS +]]-- +local colors = { + aliceblue = 0xf0f8ff, + antiquewhite = 0xfaebd7, + aqua = 0x00ffff, + aquamarine = 0x7fffd4, + azure = 0xf0ffff, + beige = 0xf5f5dc, + bisque = 0xffe4c4, + black = 0x000000, + blanchedalmond = 0xffebcd, + blue = 0x0000ff, + blueviolet = 0x8a2be2, + brown = 0xa52a2a, + burlywood = 0xdeb887, + cadetblue = 0x5f9ea0, + chartreuse = 0x7fff00, + chocolate = 0xd2691e, + coral = 0xff7f50, + cornflowerblue = 0x6495ed, + cornsilk = 0xfff8dc, + crimson = 0xdc143c, + cyan = 0x00ffff, + darkblue = 0x00008b, + darkcyan = 0x008b8b, + darkgoldenrod = 0xb8860b, + darkgray = 0xa9a9a9, + darkgrey = 0xa9a9a9, + darkgreen = 0x006400, + darkkhaki = 0xbdb76b, + darkmagenta = 0x8b008b, + darkolivegreen = 0x556b2f, + darkorange = 0xff8c00, + darkorchid = 0x9932cc, + darkred = 0x8b0000, + darksalmon = 0xe9967a, + darkseagreen = 0x8fbc8f, + darkslateblue = 0x483d8b, + darkslategray = 0x2f4f4f, + darkslategrey = 0x2f4f4f, + darkturquoise = 0x00ced1, + darkviolet = 0x9400d3, + deeppink = 0xff1493, + deepskyblue = 0x00bfff, + dimgray = 0x696969, + dimgrey = 0x696969, + dodgerblue = 0x1e90ff, + firebrick = 0xb22222, + floralwhite = 0xfffaf0, + forestgreen = 0x228b22, + fuchsia = 0xff00ff, + gainsboro = 0xdcdcdc, + ghostwhite = 0xf8f8ff, + gold = 0xffd700, + goldenrod = 0xdaa520, + gray = 0x808080, + grey = 0x808080, + green = 0x008000, + greenyellow = 0xadff2f, + honeydew = 0xf0fff0, + hotpink = 0xff69b4, + indianred = 0xcd5c5c, + indigo = 0x4b0082, + ivory = 0xfffff0, + khaki = 0xf0e68c, + lavender = 0xe6e6fa, + lavenderblush = 0xfff0f5, + lawngreen = 0x7cfc00, + lemonchiffon = 0xfffacd, + lightblue = 0xadd8e6, + lightcoral = 0xf08080, + lightcyan = 0xe0ffff, + lightgoldenrodyellow = 0xfafad2, + lightgray = 0xd3d3d3, + lightgrey = 0xd3d3d3, + lightgreen = 0x90ee90, + lightpink = 0xffb6c1, + lightsalmon = 0xffa07a, + lightseagreen = 0x20b2aa, + lightskyblue = 0x87cefa, + lightslategray = 0x778899, + lightslategrey = 0x778899, + lightsteelblue = 0xb0c4de, + lightyellow = 0xffffe0, + lime = 0x00ff00, + limegreen = 0x32cd32, + linen = 0xfaf0e6, + magenta = 0xff00ff, + maroon = 0x800000, + mediumaquamarine = 0x66cdaa, + mediumblue = 0x0000cd, + mediumorchid = 0xba55d3, + mediumpurple = 0x9370d8, + mediumseagreen = 0x3cb371, + mediumslateblue = 0x7b68ee, + mediumspringgreen = 0x00fa9a, + mediumturquoise = 0x48d1cc, + mediumvioletred = 0xc71585, + midnightblue = 0x191970, + mintcream = 0xf5fffa, + mistyrose = 0xffe4e1, + moccasin = 0xffe4b5, + navajowhite = 0xffdead, + navy = 0x000080, + oldlace = 0xfdf5e6, + olive = 0x808000, + olivedrab = 0x6b8e23, + orange = 0xffa500, + orangered = 0xff4500, + orchid = 0xda70d6, + palegoldenrod = 0xeee8aa, + palegreen = 0x98fb98, + paleturquoise = 0xafeeee, + palevioletred = 0xd87093, + papayawhip = 0xffefd5, + peachpuff = 0xffdab9, + peru = 0xcd853f, + pink = 0xffc0cb, + plum = 0xdda0dd, + powderblue = 0xb0e0e6, + purple = 0x800080, + red = 0xff0000, + rosybrown = 0xbc8f8f, + royalblue = 0x4169e1, + saddlebrown = 0x8b4513, + salmon = 0xfa8072, + sandybrown = 0xf4a460, + seagreen = 0x2e8b57, + seashell = 0xfff5ee, + sienna = 0xa0522d, + silver = 0xc0c0c0, + skyblue = 0x87ceeb, + slateblue = 0x6a5acd, + slategray = 0x708090, + slategrey = 0x708090, + snow = 0xfffafa, + springgreen = 0x00ff7f, + steelblue = 0x4682b4, + tan = 0xd2b48c, + teal = 0x008080, + thistle = 0xd8bfd8, + tomato = 0xff6347, + turquoise = 0x40e0d0, + violet = 0xee82ee, + wheat = 0xf5deb3, + white = 0xffffff, + whitesmoke = 0xf5f5f5, + yellow = 0xffff00, + yellowgreen = 0x9acd32 +} + +local function colorCallback(color) + if type(color) == "string" then + color:lower() + if colors[color] then + color = colors[color] + end + end + return color +end + +overRideMethod(TextField, "setTextColor", 2, colorCallback) +overRideMethod(Application, "setBackgroundColor", 2, colorCallback) +overRideMethod(Shape, "setFillStyle", 3, colorCallback) +overRideMethod(Shape, "setLineStyle", 3, colorCallback) \ No newline at end of file diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..f16cd40 --- /dev/null +++ b/main.lua @@ -0,0 +1,27 @@ + +--testing named colors +application:setBackgroundColor("yellow") + +local text = TextField.new(nil, "Some text") +text:setTextColor("white") +--positioning +text:setPosition("center", "center") +stage:addChild(text) + +local shape = Shape.new() +shape:setFillStyle(Shape.SOLID, "red") +--drawing primitive shape +--shape:drawRect(100, 100) +shape:drawCircle(100, 100, 100) +--positioning +shape:setY("center") +shape:setX("center") +stage:addChild(shape) + +--controlling z-index +text:bringToFront() + +--touch works also on desktop players +shape:addEventListener(Event.TOUCHES_BEGIN, function(e) + print("touched at ", e.touch.x, e.touch.y) +end) \ No newline at end of file