Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.75 KB | None | 0 0
  1. do
  2.  
  3.     local camera2D = AK.Cam2D
  4.     camera2D.__index = camera2D
  5.    
  6.     local CSSCut = {
  7.         ["text"] = function(object,value)
  8.             if object.textRenderer ~= nil then
  9.                 object.textRenderer:SetText( value )
  10.             else
  11.                 local child = object:GetChildren()
  12.                 if #child == 1 then
  13.                     local child_object = child[1]
  14.                     if child_object.textRenderer ~= nil then
  15.                         child_object.textRenderer:SetText( value )
  16.                     end
  17.                 end
  18.             end
  19.         end,
  20.         ["font"] = function(object,value)
  21.             if object.textRenderer ~= nil then
  22.                 object.textRenderer:SetFont( CS.FindAsset( value ) )
  23.             else
  24.                 local child = object:GetChildren()
  25.                 if #child == 1 then
  26.                     local child_object = child[1]
  27.                     if child_object.textRenderer ~= nil then
  28.                         child_object.textRenderer:SetFont( CS.FindAsset( value ) )
  29.                     end
  30.                 end
  31.             end
  32.         end,
  33.         ["align"] = function(object,value)
  34.             local var
  35.             if value == "left" then
  36.                 var = TextRenderer.Alignment.Left
  37.             elseif value == "right" then
  38.                 var = TextRenderer.Alignment.Right
  39.             else
  40.                 var = TextRenderer.Alignment.Center
  41.             end
  42.             if object.textRenderer ~= nil then
  43.                 object.textRenderer:SetAlignment( var )
  44.             else
  45.                 local child = object:GetChildren()
  46.                 if #child == 1 then
  47.                     local child_object = child[1]
  48.                     if child_object.textRenderer ~= nil then
  49.                         child_object.textRenderer:SetAlignment( var )
  50.                     end
  51.                 end
  52.             end
  53.         end,
  54.         ["scale"] = function(object,value,primary)
  55.             primary = primary or false
  56.             if type(value) == "string" then
  57.                 local _,_,a,b,c = string.find(value, '(.*)[%s](.*)[%s](.*)')
  58.                 local scale = object.Scale
  59.                 a = tonumber(a) or scale.x
  60.                 b = tonumber(b) or scale.y
  61.                 c = tonumber(c) or scale.z
  62.                 if a > 1 then a = a / 100 elseif a < 0 then a = 0 end
  63.                 if b > 1 then b = b / 100 elseif b < 0 then b = 0 end
  64.                 if c > 1 then c = c / 100 elseif c < 0 then c = 0 end
  65.                 object.transform:SetLocalScale( Vector3:New( a , b , c ) )
  66.             elseif type(value) == "number" then
  67.                 if value > 1 then value = value / 100 elseif value < 0 then value = 0 end
  68.                 object.transform:SetLocalScale( Vector3:New( value ) )
  69.             elseif type(value) == "table" then
  70.                 value = value or {}
  71.                 value[1] = value[1] or 1
  72.                 value[2] = value[2] or 1
  73.                 value[3] = value[3] or 1
  74.                
  75.                 -- On vérifie les valeurs
  76.                 if value[1] > 1 then value[1] = value[1] / 100 elseif value[1] < 0 then value[1] = 0 end
  77.                 if value[2] > 1 then value[2] = value[1] / 100 elseif value[2] < 0 then value[2] = 0 end
  78.                 if value[3] > 1 then value[3] = value[1] / 100 elseif value[3] < 0 then value[3] = 0 end
  79.                
  80.                 object.transform:SetLocalScale( Vector3:New( unpack(value) ) )
  81.             end
  82.         end,
  83.         ["model"] = function(object,value,primary)
  84.             primary = primary or false
  85.             if object.modelRenderer ~= nil then
  86.                 object.modelRenderer:SetModel( CS.FindAsset( value , "Model" ) )
  87.             end
  88.         end,
  89.         ["opacity"] = function(object,value,primary)
  90.             primary = primary or false
  91.             if type(value) == "number" then
  92.                 local component
  93.                 if object.modelRenderer ~= nil then
  94.                     component = object.modelRenderer
  95.                 elseif object.mapRenderer ~= nil then
  96.                     component = object.mapRenderer
  97.                 elseif object.textRenderer ~= nil then
  98.                     component = object.textRenderer
  99.                 end
  100.                
  101.                 component:SetOpacity( value )
  102.             end
  103.         end,
  104.         ["zindex"] = function(object,value)
  105.             if type(value) == "number" then
  106.                 if value >= 0 and value <= 150 then
  107.                     AK.UI:Zindex(object.Name,value,true)
  108.                 end
  109.             elseif type(value) == "string" then
  110.                 if value == "inherit" then
  111.                     local parent = object:GetParent()
  112.                     if parent ~= nil then
  113.                         AK.UI:Zindex(object.Name,parent.Pos.z,true)
  114.                     end
  115.                 end
  116.             end
  117.         end,
  118.         ["show"] = function(object,value)
  119.             if type(value) == "number" then
  120.                 AK.UI:Show(object.Name,value)
  121.             end
  122.         end,
  123.         ["hide"] = function(object,value)
  124.             if type(value) == "number" then
  125.                 AK.UI:Hide(object.Name,value)
  126.             end
  127.         end,
  128.         ["button_reset"] = function(object,value)
  129.             if type(value) == "boolean" then
  130.                 local comp = object.behavior
  131.                 if value then
  132.                     comp.default_value = true
  133.                 elseif not value then
  134.                     comp.default_value = false
  135.                 end
  136.             end
  137.         end
  138.     }
  139.    
  140.     -- > Sub Action stylesheets
  141.     local CSSubAction = {
  142.         ["echo"] = function(object,value)
  143.             local echo = tostring(value)
  144.            
  145.             if echo == "pos" then
  146.                 echo = object.Pos
  147.             elseif echo == "scale" then
  148.                 echo = object.Scale
  149.             end
  150.            
  151.             print(echo)
  152.         end,
  153.         ["play"] = function(object,value)
  154.             if type(value) == "string" then
  155.                 if value == "this" then
  156.                     AK.UI:Anim(object.Name)
  157.                 else
  158.                     AK.UI:Anim(value)
  159.                 end
  160.             elseif type(value) == "table" then
  161.                
  162.                 for i=1,#value do
  163.                     if value[i] == "this" then
  164.                         AK.UI:Anim(object.Name)
  165.                     else
  166.                         AK.UI:Anim(value[i])
  167.                     end
  168.                 end
  169.             end
  170.         end,
  171.         ["switch"] = function(object,value)
  172.             if type(value) == "string" then
  173.                 local _,_,a,b = string.find(value, '(%a*)[%s]?(%a*)')
  174.                 local result = {a,b,c}
  175.                 if #result == 1 then
  176.                     AK.UI:Switch(value)
  177.                 elseif #result == 2 then
  178.                     if AK.UI.ModuleActive == a then
  179.                         AK.UI:Switch(c)
  180.                     elseif AK.UI.ModuleActive == c then
  181.                         AK.UI:Switch(a)
  182.                     else
  183.                         AK.UI:Switch(a)
  184.                         -- do nothing
  185.                        
  186.                     end
  187.                 end
  188.             end
  189.         end,
  190.         ["append"] = function(object,value)
  191.             if type(value) == "string" then
  192.                 local asset = CS.FindAsset( value , "Scene" )
  193.                 if asset ~= nil then
  194.                     CS.LoadScene( asset )
  195.                 end
  196.             end
  197.         end,
  198.         ["load"] = function(object,value)
  199.             if type(value) == "string" then
  200.                 local asset = CS.AppendScene( value , "Scene" )
  201.                 if asset ~= nil then
  202.                     CS.AppendScene( asset )
  203.                 end
  204.             end
  205.         end,
  206.         ["instantiate"] = function(object,value)
  207.             if type(value) == "table" then
  208.                 value = value or {}
  209.                 local asset = CS.FindAsset( value[2] , "Scene" )
  210.                 if asset ~= nil then
  211.                     AK.UI[value[1]] = CS.Instantiate( value[1].."_key" , asset )
  212.                 end
  213.             end
  214.         end,
  215.         ["destroy"] = function(object,value)
  216.             if type(value) == "string" then
  217.                 if AK.UI[value] ~= nil then
  218.                     CS.Destroy( AK.UI[value] )
  219.                     AK.UI[value] = nil
  220.                 end
  221.             end
  222.         end,
  223.         ["exit"] = function(object,value)
  224.             if type(value) == "boolean" then
  225.                 if value then
  226.                     CS.Exit()
  227.                 end
  228.             elseif type(value) == "table" then
  229.                 value = value or {}
  230.                 value[1] = value[1] or true
  231.                 value[2] = value[2] or Nil
  232.                 if value[1] then
  233.                     value[2]()
  234.                     CS.Exit()
  235.                 end
  236.             end
  237.         end,
  238.         ["atob"] = function(object,value)
  239.             if type(value) == "string" then
  240.                 local _,_,a,b = string.find(value, '(%a*)[%s]?(%a*)')
  241.                 local result = {a,b}
  242.                 if #result == 1 then
  243.                     AK.UI:AtoB(object.Name,a)
  244.                 elseif #result == 2 then
  245.                     AK.UI:AtoB(a,b)
  246.                 end
  247.             elseif type(value) == "table" then
  248.             end
  249.         end,
  250.         ["show"]        = CSSCut["show"],
  251.         ["hide"]        = CSSCut["hide"],
  252.         ["zindex"]      = CSSCut["zindex"],
  253.         ["scale"]       = CSSCut["scale"],
  254.         ["model"]       = CSSCut["model"],
  255.         ["opacity"]     = CSSCut["opacity"],
  256.         ["text"]        = CSSCut["text"],
  257.         ["font"]        = CSSCut["font"],
  258.         ["align"]       = CSSCut["align"],
  259.         ["button_resetset"]       = CSSCut["button_reset"]
  260.     }
  261.    
  262.     -- > Head action stylesheets
  263.     local CSSAction = {
  264.         -- model
  265.         ["model"]       = CSSCut["model"],
  266.         ["animation"] = function(object,value)
  267.             if type(value) == "string" and object.modelRenderer ~= nil then
  268.                 object.modelRenderer:SetAnimation( CS.FindAsset( value, "Animation" ) )
  269.             end
  270.         end,
  271.        
  272.         -- Map
  273.         ["map"] = function(object,value)
  274.             if value ~= nil and object.mapRenderer ~= nil then
  275.                 object.mapRenderer:SetMap( CS.FindAsset( value , "Map" ) )
  276.             end
  277.         end,
  278.         ["tile"] = function(object,value)
  279.             if value ~= nil and object.mapRenderer ~= nil then
  280.                 object.mapRenderer:SetTileSet( CS.FindAsset( value , "TileSet" ) )
  281.             end
  282.         end,
  283.        
  284.         -- Input action
  285.         ["onclick"] = function(object,value)
  286.             if type(value) == "table" then
  287.                 for k,v in pairs(CSSubAction) do    
  288.                     for a,b in pairs(value) do
  289.                         if a == k then
  290.                             object:OnClick( function()
  291.                                 v(object,b)
  292.                                
  293.                             end )
  294.                         end
  295.                     end
  296.                 end
  297.             elseif type(value) == "function" then
  298.                 object:OnClick( value )
  299.             end
  300.         end,
  301.         ["outclick"] = function(object,value)
  302.             if type(value) == "table" then
  303.                 for k,v in pairs(CSSubAction) do    
  304.                     for a,b in pairs(value) do
  305.                         if a == k then
  306.                             object:OutClick( function()
  307.                                 v(object,b)
  308.                                
  309.                             end )
  310.                         end
  311.                     end
  312.                 end
  313.             elseif type(value) == "function" then
  314.                 object:OutClick( value )
  315.             end
  316.         end,
  317.         ["onfocus"] = function(object,value)
  318.         end,
  319.         ["outfocus"] = function(object,value)
  320.         end,
  321.         ["onhover"] = function(object,value)
  322.             if type(value) == "table" then
  323.                 for k,v in pairs(CSSubAction) do    
  324.                     for a,b in pairs(value) do
  325.                         if a == k then
  326.                             object:OnHover( function()
  327.                                 v(object,b)
  328.                             end )
  329.                         end
  330.                     end
  331.                 end
  332.             elseif type(value) == "function" then
  333.                 object:OnHover( value )
  334.             end
  335.         end,
  336.         ["outhover"] = function(object,value)
  337.             if type(value) == "table" then
  338.                 for k,v in pairs(CSSubAction) do    
  339.                     for a,b in pairs(value) do
  340.                         if a == k then
  341.                             object:OutHover( function()
  342.                                 v(object,b)
  343.                             end )
  344.                         end
  345.                     end
  346.                 end
  347.             elseif type(value) == "function" then
  348.                 object:OutHover( value )
  349.             end
  350.         end,
  351.         ["next"] = function(object,value)
  352.             if type(value) == "table" then
  353.                 for k,v in pairs(CSSubAction) do    
  354.                     for a,b in pairs(value) do
  355.                         if a == k then
  356.                             object:OnNext( function()
  357.                                 v(object,b)
  358.                             end )
  359.                         end
  360.                     end
  361.                 end
  362.             elseif type(value) == "function" then
  363.                 object:OnNext( value )
  364.             end
  365.         end,
  366.        
  367.         -- > Input arguments
  368.         ["input_valide"] = function(object,value)
  369.         end,
  370.         ["input_err"] = function(object,value)
  371.         end,
  372.         ["input_warning"] = function(object,value)
  373.         end,
  374.        
  375.         -- Camera action
  376.         ["projection"] = function(object,value)
  377.             if type(value) == "string" then
  378.                 if object.camera ~= nil then
  379.                     if value == "perspective" then
  380.                         object.camera:SetProjectionMode( Camera.ProjectionMode.Perspective )
  381.                     else
  382.                         object.camera:SetProjectionMode( Camera.ProjectionMode.Orthographic )
  383.                     end
  384.                 end
  385.             end
  386.         end,
  387.         ["fov"] = function(object,value)
  388.             if type(value) == "number" then
  389.                 if object.camera ~= nil then
  390.                     object.camera:SetFOV( value )
  391.                 end
  392.             end
  393.         end,
  394.         ["orthoscale"] = function(object,value)
  395.             if type(value) == "number" then
  396.                 if object.camera ~= nil then
  397.                     object.camera:SetOrthographicScale( value )
  398.                 end
  399.             end
  400.         end,
  401.         ["radarsize"] = function(object,value)
  402.             if type(value) == "table" then
  403.                 if object.camera ~= nil then
  404.                     value = value or {}
  405.                     value[1] = value[1] or 1
  406.                     value[2] = value[2] or 1
  407.                     object.camera:SetRenderViewportSize( unpack(value) )
  408.                 end
  409.             end
  410.         end,
  411.         ["radarposition"] = function(object,value)
  412.             if type(value) == "table" then
  413.                 if object.camera ~= nil then
  414.                     value = value or {}
  415.                     value[1] = value[1] or 1
  416.                     value[2] = value[2] or 1
  417.                     object.camera:SetRenderViewportPosition( unpack(value) )
  418.                 end
  419.             end
  420.         end,
  421.        
  422.         -- Transform action
  423.         ["scale"]       = CSSCut["scale"],
  424.         ["margin"] = function(object,value)
  425.             if type(value) == "string" then                
  426.                 local _,_,a,b = string.find(value, '(%d*)[%s]?(%d*)')
  427.                 local result = {a,b}
  428.                 result[1] = result[1] or 0
  429.                 result[2] = result[2] or 0
  430.                 local pos = object.Pos
  431.                 if #result == 1 then
  432.                     object.transform:SetPosition( Vector3:New( pos.x + result[1], pos.y + result[1], pos.z ) )
  433.                
  434.                 elseif #result == 2 then
  435.                     object.transform:SetPosition( Vector3:New( pos.x + result[1], pos.y + result[2], pos.z ) )
  436.  
  437.                 else
  438.                     -- do nothing
  439.                    
  440.                 end
  441.             end
  442.         end,
  443.         ["position"] = function(object,value)
  444.             if type(value) == "string" then
  445.                 if value == "inherit" then
  446.                     local parent = object:GetParent()
  447.                     if parent ~= nil then
  448.                         object.transform:SetPosition( parent.Pos )
  449.                     end
  450.                 elseif value == "center" then
  451.                     object.transform:SetPosition( Vector3:New( 0 , 0 , object.Pos.z ) )
  452.                 else
  453.                     -- do nothing
  454.                    
  455.                 end
  456.             end
  457.         end,
  458.        
  459.         -- GameObject
  460.         ["parent"] = function(object,value)
  461.             if value ~= nil and type(value) == "string" then
  462.                 local parent = CS.Get(value)
  463.                 if parent ~= nil then
  464.                     object:SetParent( parent )
  465.                 end
  466.             end
  467.         end,
  468.         ["behavior"] = function(object,value)
  469.             if type(value) == "table" then
  470.                 local comp = object.behavior
  471.                 if comp ~= nil then
  472.                     for k,v in pairs(value) do
  473.                         if comp.k ~= nil then
  474.                             comp.k = v
  475.                         end
  476.                     end
  477.                 end
  478.             end
  479.         end,
  480.        
  481.         -- Animation
  482.         ["show"]        = CSSCut["show"],
  483.         ["hide"]        = CSSCut["hide"],
  484.        
  485.         -- Triple contexte
  486.         ["opacity"]     = CSSCut["opacity"],
  487.         ["zindex"]      = CSSCut["zindex"],
  488.        
  489.         -- TextRenderer CSS
  490.         ["text"]        = CSSCut["text"],
  491.         ["font"]        = CSSCut["font"],
  492.         ["align"]       = CSSCut["align"];
  493.         ["button_reset"]       = CSSCut["button_reset"]
  494.     }
  495.    
  496.     -- > UI build CSS function
  497.     function camera2D:BuildCSS(autoapply)
  498.         autoapply = autoapply or false
  499.         local x = AK.CSS
  500.        
  501.         for k,v in pairs(x) do
  502.             self.css[k] = v
  503.         end
  504.         if autoapply then
  505.             self:ApplyCSS()
  506.         end
  507.     end
  508.  
  509.     function camera2D:ApplyCSS()
  510.         local x = self.css
  511.        
  512.         for k,v in pairs(x) do
  513.        
  514.             local _,_,a,b,c,d,e = string.find(k, '(%a*)[%s]?(%a*)[%s]?(%a*)[%s]?(%a*)[%s]?(%a*)')
  515.             --print(a,b,c,d,e)
  516.             local result = {a,b,c,d,e}
  517.            
  518.             for _,values in pairs(result) do
  519.                 if values ~= nil and values ~= "" then
  520.                     local object = CS.Get(values)
  521.                     if object ~= nil then
  522.                    
  523.                         local pos = {{},{},nil}
  524.                         local posinit = false
  525.                         local fixedcontaint = {false,nil}
  526.                         local anim = {false,1,false,{0,0},{0,0}}
  527.                         for a,b in pairs(v) do
  528.                             if a == "script" then
  529.                                 b = b or "OnClick"
  530.                                 local comp = object:GetComponent("ScriptedBehavior")
  531.                                 if comp == nil then
  532.                                     self:AddScript(values,b)
  533.                                 end
  534.                             elseif a == "left" or a == "right" then
  535.                                 pos[1][1] = a
  536.                                 pos[1][2] = b
  537.                             elseif a == "top" or a == "bottom" then
  538.                                 pos[2][1] = a
  539.                                 pos[2][2] = b
  540.                             elseif a == "resize" then  
  541.                                 pos[3] = b or false
  542.                             elseif a == "position" then
  543.                                 posinit = true
  544.                             elseif a == "container" then
  545.                                 if type(b) == "string" then
  546.                                     --local new_parent = CS.Get(b)
  547.                                     --object:SetParent(new_parent,false)
  548.                                     fixedcontaint[1] = true
  549.                                     fixedcontaint[2] = b
  550.                                 end
  551.                             elseif a == "anim" then
  552.                                 if type(b) == "table" then
  553.                                     if #b >= 3 then
  554.                                         if not anim[1] then
  555.                                             anim[1] = true
  556.                                         end
  557.                                        
  558.                                         anim[2] = b[1] or 1
  559.                                         anim[3] = b[2] or false
  560.                                         anim[4] = b[3] or {0,0}
  561.                                         anim[5] = b[4] or {0,0}
  562.                                     end
  563.                                 end
  564.                             elseif a == "anim_pos" then
  565.                                 if type(b) == "table" then
  566.                                     if not anim[1] then
  567.                                         anim[1] = true
  568.                                     end
  569.                                     anim[4][1] = b[1] or 0
  570.                                     anim[4][2] = b[2] or 0
  571.                                 end
  572.                             elseif a == "anim_scale" then  
  573.                                 if type(b) == "table" then
  574.                                     if not anim[1] then
  575.                                         anim[1] = true
  576.                                     end
  577.                                     anim[5][1] = b[1] or 0
  578.                                     anim[5][2] = b[2] or 0
  579.                                 end
  580.                             elseif a == "anim_speed" then
  581.                                 anim[2] = b
  582.                             elseif a == "anim_resize" then
  583.                                 anim[3] = b
  584.                             end
  585.                             self:TrimCSS(values,a,b)
  586.                         end  
  587.                         if not posinit then
  588.                             if not fixedcontaint[1] then
  589.                                 pos[3] = pos[3] or false
  590.                                 self:Pos(values,pos[1],pos[2],pos[3])
  591.                             else
  592.                                 if fixedcontaint[2] ~= nil then
  593.                                     self:FixedPos(fixedcontaint[2],values,pos[1],pos[2])
  594.                                 end
  595.                             end
  596.                         end
  597.                         if anim[1] then
  598.                             self:NewAnim(values,anim[2],anim[3],anim[4],anim[5])
  599.                         end
  600.                     end
  601.                 end
  602.             end
  603.         end
  604.     end
  605.    
  606.     function camera2D:TrimCSS(object,K,V)    
  607.         local object = CS.Get(object)
  608.         if type(V) == "string" and string.lower(K) ~= "text" then
  609.             V = string.lower(V)
  610.         end
  611.         for a,b in pairs(CSSAction) do
  612.             local _,_,key = string.find(K,"key",1)
  613.             if key ~= nil then
  614.                 print(key)
  615.             end
  616.             if a == string.lower(K) then
  617.                 b(object,V,true)
  618.             end
  619.         end
  620.     end
  621.  
  622. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement