• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Code-Discussion] UICallbacks

conde2

Active Member
Joined
Jun 22, 2008
Messages
348
Reaction score
44
So I come here to show a simple code, if the community accepts the idea, I will be continuing the project.
This code will allow call some functions in the UI.
An example of a function that can be called is when you release a MiniWindow in LeftPanel / RightPanel, allowing editions


NOTE: This is only the concept of the project




In modules/corelib/ui/uiwidget thats the code necessary
Lua:
UICallbacks = {
  callbacks = {}
}

function UICallbacks:createCallback(classname, fromcall, callback, id)
 local id

  for functionName, _ in pairs(_G[classname]) do
      if fromcall== functionName then

          if not self.callbacks[classname] then
            self.callbacks[classname] = {}
            self.callbacks[classname][fromcall] = {}
          end
          id = id or #self.callbacks[classname][fromcall] + 1
          if not self.callbacks[classname][fromcall][id] then
            self.callbacks[classname][fromcall][id] = callback
          end
      end
  end
return true
end

function UICallbacks:executeCallbacks(classname, callname, ...)
    local delete = delete or false
    local id = id or false
    for callbackId, callbackFunction in pairs(self.callbacks[classname][callname]) do
        if not id then
          callbackFunction(...)
        elseif id and callbackId == id then
          callbackFunction(...)
          return true
        end
    end
end

An example of where we can use the code
In modules/corelib/ui/uiminiwindowcontainer thats the code necessary

Lua:
function UIMiniWindowContainer:onDrop(widget, mousePos)
  if widget:getClassName() == 'UIMiniWindow' then
    local oldParent = widget:getParent()
    if oldParent == self then
      return true
    end

    if oldParent then
      oldParent:removeChild(widget)
    end

    if widget.movedWidget then
      local index = self:getChildIndex(widget.movedWidget)
      self:insertChild(index + widget.movedIndex, widget)
    else
      self:addChild(widget)
    end

    UICallbacks:executeCallbacks(self:getClassName(), "onDrop", widget)
    self:fitAll(widget)
    return true
  end
end

And then to create calls:

Lua:
  UICallbacks:createCallback('UIMiniWindowContainer', 'onDrop', function(widget) print("TESTING") end, 1)


The structure will look like this:
Lua:
callbacks = {
  ['UIMiniWindow'] = {
    ['create'] = {functionX,},
    ['close'] = {functionX, functionT, functionP},
  },
  ['UIMiniWindowContainer'] = {
    ['onDrop'] = {functionX, functionT},
    ['create'] = {functionK, functionZ}
  },

 .
 .
 .

}
 
Last edited:
Back
Top