View Single Post
11-08-20, 08:20 PM   #9
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
To create template in Lua, we should use classes, it's not a simple job to fulfill all requirements.

I can do that with my PLoop and Scorpio Lib, but normally only authors with full OOP background can follow that. So this is only an example for the first two template.

Lua Code:
  1. -- This line make sure you can write code in an isolated environment
  2. -- and use the features provided by the my Scorpio lib
  3. Scorpio "DerangementPetBattle" ""
  4.  
  5. -- define a template class based on the frame
  6. class "DeePetBattleAura" (function(_ENV)
  7.     inherit "Frame"
  8.  
  9.     local function OnEnter(self)
  10.         -- DeePetBattleAura_OnEnter
  11.         -- Use GetChild to access the child elements
  12.         self:GetChild("Duration"):Show()
  13.     end
  14.  
  15.     local function OnLeave(self)
  16.         -- DeePetBattleAura_OnLeave
  17.         -- Also can use the name to access the child if no conflict
  18.         self.Duration:Hide()
  19.     end
  20.  
  21.     -- The __Template__ is used to declare the child elements with type
  22.     __Template__{
  23.         Icon = Texture,
  24.         Duration = FontString,
  25.         DebuffBorder = Texture,
  26.     }
  27.     function __ctor(self)
  28.         -- The constructor will be used to do the init
  29.         -- Bind the event handlers
  30.         self.OnEnter = self.OnEnter + OnEnter
  31.         self.OnLeave = self.OnLeave + OnLeave
  32.     end
  33. end)
  34.  
  35. class "DeePetBattleAbilityButton" (function(_ENV)
  36.     inherit "Button"
  37.  
  38.     -- The __new is used to create the widget object
  39.     function __new(_, name, parent)
  40.         -- Since your template is fixed, should be done here
  41.         return CreateFrame("Button",  nil, parent, "PetBattleActionButtonTemplate")
  42.     end
  43.  
  44.     __Template__{
  45.         -- Skip
  46.     }
  47.     function __ctor(self)
  48.     end
  49. end)
  50.  
  51. -- The Style settings works like the xml
  52. -- but it's very simple to create a new skin
  53. -- and apply to all widget objects
  54. Style.UpdateSkin("Default", {
  55.     [DeePetBattleAura] = {
  56.         Icon = {
  57.             drawLayer = "ARTWORK",
  58.             size = Size(30, 30),
  59.             location = { Anchor("CENTER") }
  60.         },
  61.         Duration = {
  62.             drawLayer = "OVERLAY",
  63.             fontObject = GameFontHighlight,
  64.             justifyH = "CENTER",
  65.             justifyV = "TOP",
  66.             height = 16,
  67.             location = {
  68.                 Anchor("TOPLEFT", 0, 0, nil, "BOTTOMLEFT"),
  69.                 Anchor("TOPRIGHT", 0, 0, nil, "BOTTOMRIGHT"),
  70.             }
  71.         },
  72.         DebuffBorder = {
  73.             drawLayer = "OVERLAY",
  74.             file = [[Interface\Buttons\UI-Debuff-Overlays]],
  75.             size = Size(33, 32),
  76.             location = { Anchor("CENTER", 0, 0, "Icon") },
  77.             texCoords = RectType(0.296875, 0.5703125, 0, 0.515625),
  78.             color = Color(1, 0, 0),
  79.         }
  80.     }
  81. })
  82.  
  83. -- The usage is the same to object created by CreateFrame
  84. -- The diff is only the object is created by class
  85. local aura = DeePetBattleAura("Aura1", UIParent)

The doc can be found at github.com/kurapica/Scorpio, if you want give it a try.
  Reply With Quote