View Single Post
07/23/14, 06:21 AM   #4
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I tried to write some untested code for illustration. Careful with dot where colon should be -- control:GetParent()

edit: or look at LAM controls, they too have custom data:
control.panel is the panel it's contained in
control.data is the optionData it was created from

Code:
<Control name="EventExplorerRow" virtual="true">
    <Dimensions y="45"/>
    <Controls>
        <Label name="$(parent)Id" font="ZoFontGame" horizontalAlignment="RIGHT">
            <Anchor point="RIGHT" relativePoint="LEFT" offsetX="50"/>
        </Label>
        <Label name="$(parent)Name" font="ZoFontGame">
            <!-- I don't know how, but it's definitely possible to make this clickable,
                 so that you can enable/disable events by cliking on the name (see "ZO_CheckButtonLabel") -->
            <Anchor point="LEFT" relativeTo="$(parent)Id" relativePoint="RIGHT" offsetX="20"/>
        </Label>
    </Controls>
</Control>
Lua Code:
  1. function EventExplorer:createRows()
  2.     local function onRowClicked(...)
  3.         self:onRowClicked(...)
  4.     end
  5.     self.rows = {}
  6.     for i = 1, NUM_PAGE_ROWS do
  7.         local row = CreateControlFromVirtual("$(parent)Row", self.pageScrollWindow, "EventExplorerRow", i)
  8.         row.data = {}
  9.         row:SetHandler("OnClicked", onRowClicked)
  10.         table.insert(self.rows, row)
  11.     end
  12. end
  13.  
  14. function EventExplorer:onRowClicked(control, button)
  15.     -- this handler should be usable for both labels and even the parent row control
  16.     -- (which I think will be useful so that cliking in the empty space between labels works)
  17.     local row
  18.     if control.data then
  19.         row = control
  20.     else
  21.         row = control:GetParent()
  22.     end
  23.     local status = self:toggleEventEnabled(row.data.eventId)
  24.     row:SetEnabled(status)
  25. end
Lua Code:
  1. -- this method will be used to fill the page with event ids and labels;
  2. -- you'd store all event ids returned from a filter in self.selectedEventIds;
  3. -- after the search you'd fill the 1st page with self:setPage(1);
  4. -- if there are more events than NUM_PAGE_ROWS, and you provide
  5. -- a control for switching to the next page, it can call self:setPage(self.currentPage);
  6. function EventExplorer:setPage(pageNumber)
  7.     local offset = math.min(0, pageNumber - 1) * NUM_PAGE_ROWS
  8.     for i, row in ipairs(self.rows) do
  9.         local eventId = self.selectedEventIds[i + offset]
  10.         local idLabel = row:GetNamedChild("Id")
  11.         local nameLabel = row:GetNamedChild("Name")
  12.         row.data.eventId = eventId
  13.         if eventId then
  14.             idLabel:SetText(tostring(eventId))
  15.             nameLabel:SetText(self:getEventName(eventId))
  16.             nameLabel:SetEnabled(self:isEventEnabled(eventId))
  17.         else
  18.             idLabel:SetText("")
  19.             nameLabel:SetText("")
  20.             nameLabel:SetEnabled(false)
  21.         end
  22.     end
  23. end
  24.  
  25. -- toggleEventEnabled, isEventEnabled will work with an internal table
  26. -- that will keep track of what events you have registered

Last edited by merlight : 07/23/14 at 06:30 AM.
  Reply With Quote