
Originally Posted by
Galuhad
Now is there a way I can have two different functions used on mouse events? The tooltip code will obviously need mouse events, but I don't want it to conflict with any other mouse events coded elsewhere?
Consider the code:
Code:
Card=class(Turbine.UI.Lotro.Window)
function Card:Constructor()
Turbine.UI.Lotro.Window.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(200,200)
self:SetText("Card")
end
function Card:MouseEnter(sender,args)
-- do some stuff on mouse enter
Turbine.Shell.WriteLine("Card Mouse Enter")
end
BigCard=class(Card)
function BigCard:Constructor()
Card.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(300,300)
self:SetText("Big Card")
self:SetPosition(200,0)
end
function BigCard:MouseEnter(sender,args)
-- call the base class MouseEnter
Card:MouseEnter(sender,args)
-- now do some other stuff
Turbine.Shell.WriteLine("Big Card Mouse Enter")
end
end
abc=Card()
abc:SetVisible(true)
def=BigCard()
def:SetVisible(true)
This creates two classes, Card and BigCard which both implement the MouseEnter event handler but BigCard is derived from Card and when it overrides the Card:MouseEnter handler, it explicitly calls the Card:MouseEnter handler to preserve that functionality.
Another, more Lua style approach is to assign a table to MouseEnter by changing Big Card:
Code:
Card=class(Turbine.UI.Lotro.Window)
function Card:Constructor()
Turbine.UI.Lotro.Window.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(200,200)
self:SetText("Card")
end
function Card:MouseEnter(sender,args)
-- do some stuff on mouse enter
Turbine.Shell.WriteLine("Card Mouse Enter")
end
BigCard=class(Card)
function BigCard:Constructor()
Card.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(300,300)
self:SetText("Big Card")
self:SetPosition(200,0)
self.MouseEnter={self.MouseEnter,function(sender,args)
-- now do some other stuff
Turbine.Shell.WriteLine("Big Card Mouse Enter sender:"..tostring(sender))
end}
end
abc=Card()
abc:SetVisible(true)
def=BigCard()
def:SetVisible(true)
Of course, both methods require that you preserve the class implementation of MouseEnter if you override it in the instance, for instance in the second case, if you define def.MouseEnter you would have to add the new functionality to the table. That leads to the third approach which is just an extension of how we implement event handlers for instances of Turbine shared objects. If you define and use the AddCallback functionality in your plugins, you can use the AddCallback mechanism to add handlers:
AddCallback(def,"MouseEnter",f unction(sender,args) -- do some more stuff here end)
Code:
function AddCallback(object, event, callback)
if (object[event] == nil) then
object[event] = callback;
else
if (type(object[event]) == "table") then
local exists=false;
local k,v;
for k,v in ipairs(object[event]) do
if v==callback then
exists=true;
break;
end
end
if not exists then
table.insert(object[event], callback);
end
else
if object[event]~=callback then
object[event] = {object[event], callback};
end
end
end
return callback;
end
Card=class(Turbine.UI.Lotro.Window)
function Card:Constructor()
Turbine.UI.Lotro.Window.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(200,200)
self:SetText("Card")
local tmpFunction=function (sender,args)
-- do some stuff on mouse enter
Turbine.Shell.WriteLine("Card Mouse Enter sender:"..tostring(sender))
end
AddCallback(self,"MouseEnter",tmpFunction)
end
BigCard=class(Card)
function BigCard:Constructor()
Card.Constructor(self); -- this is where our class calls the base class constructor
self:SetSize(300,300)
self:SetText("Big Card")
self:SetPosition(200,0)
AddCallback(self,"MouseEnter",function(sender,args)
-- now do some other stuff
Turbine.Shell.WriteLine("Big Card Mouse Enter sender:"..tostring(sender))
end)
end
abc=Card()
abc:SetVisible(true)
def=BigCard()
def:SetVisible(true)
Note that I declared the function as a local, tmpFunction, in Card which makes it a little easier to use indentation but in BigCard I defined the function right in the call to AddCallback. Either method is acceptable. I just happen to like the indentation of the first method a little better personally.