View Full Version : Dynamically creating WindowClasses
phantomwhale
July 17th, 2011, 14:53
I'm trying to do something a little over the top to make my code super-extensible.
The plan is to take something like this:
<windowclass name="charsheet">
<sheetdata>
<subwindow name="main">
<bounds>0,0,-1,-1</bounds>
<class>charsheet_main</class>
</subwindow>
<subwindow name="skills">
<bounds>0,0,-1,-1</bounds>
<class>charsheet_skills</class>
</subwindow>
...
Can the subwindow instantiation be made dynamic, perhaps via windowinstance.createControl() or by changing the subwindows for a windowlist of some class.
The idea would be for classes to "register" with some manager script (similar to options in 4E), such that when the charsheet class tries to load, all it does it call out to a "populate me" function in the manager script, passing itself in as a parameter, and then the subcontrols can be created on the fly ?
This would allow extension to simply "register" a new subwindow at boot time, and that would add it to instances the character sheet class ?
It's doing this with subwindows, or windows of all different classes (as windowlists need just one kind of class in them) that has me about 25 miles out of my depth here, I think.
I've had a play with the code, but I've just made a mess so far !
Moon Wizard
July 17th, 2011, 22:10
I'm not sure if I'm following completely. Maybe some specific examples of what you are trying to do would help?
On a general level, there are functions to create controls on the fly; though I believe that you will have some challenges creating some of the more advanced controls on the fly (windowlist, subwindow, etc.) as it appears from the code that they were never really designed with that in mind. I do believe that you could easily create basic controls (number, string, dice, formattedtext) within a windowinstance on the fly without any issues.
On a potentially related note, I have been considering adding some sort of capability to windowlist controls to allow a field to determine which windowclass is used to instantiate the windowinstance for each record.
An example of this is the 4E actions for powers. Right now, each action record contains all the fields for attack, damage, healing and effect details and rolls; and the majority of the fields are hidden. I want to be able to have 4 different windowclasses from which I can instantiate each record based on the value of one of the fields. (i.e. type field, in this case)
Is this similar to where you are heading?
Regards,
JPG
Zeus
July 17th, 2011, 22:38
An example of this is the 4E actions for powers. Right now, each action record contains all the fields for attack, damage, healing and effect details and rolls; and the majority of the fields are hidden. I want to be able to have 4 different windowclasses from which I can instantiate each record based on the value of one of the fields. (i.e. type field, in this case)
This would be a cool addition to have JPG. Until now I have used multiple windowclass definitions sharing the same database node and subtree and LUA logic to manipulate which entries are shown within windowlists. Having this functionality would simplify the 4E Partysheet extension implementation some what.
phantomwhale
July 18th, 2011, 03:09
What a difference 8 hours sleep makes... I've done it :)
The problem:
For character sheets, I was using XML hardcoded subwindows for tabs, and a tabcontrol custom element to drive which subwindow was displayed. This meant I needed to copy and paste the file into an extension if I wanted to mess about the tabs. In the case of DL:R, it adds a new tab (powers) to the sheet.
So what if I want to add another tab to the character sheet in a new extension, and have it work with vanilla SWEX or any SWEX extension that has already modified the tabs page ? And I don't want to create a different version of the extension for each underlying extension !
Anyway, I fixed it. The results are here : https://sw4fg2.tumblr.com/post/7744443425/character-sheet-tabs-are-now-much-easier-to-add-in
Now, here comes the science part...
To do this I had to move the subwindows into templates (thanks Foen : https://www.fantasygrounds.com/forums/showpost.php?p=86920&postcount=4). This made it possible to create them in LUA code.
<template name="main_charsheet">
<subwindow>
<bounds>0,0,-1,-1>
<class>charsheet_main</class>
</subwindow>
</template>
<template name="skills_charsheet">
<subwindow>
<bounds>0,0,-1,-1>
<class>charsheet_skills</class>
</subwindow>
</template>
Next I fiddle with the tab control so it can allow people to add tabs with a simple method call - I'm leaving this bit out as it wasn't a big leap of discovery. This meant I could define a tiny method in my character sheet windowclass of:
function addSheet(name, windowclass, tabicon)
createControl(windowclass, name)
tabs.registerTab(name, tabicon)
end
function onInit()
CharsheetManager.populate(self)
end
Then I created a "character sheet manager" scripts (used in the onInit method above) - this is a named LUA script which creates the initial character sheet, but allows other scripts to register extra sheets on start up.
function onInit()
registerSheet("main", "main_charsheet", "tab_main")
registerSheet("skills", "skills_charsheet", "tab_skills")
...
end
function registerSheet(name, windowclass, tabicon, before)
-- some cleverness here to prevent duplicate name, and allow tabs to be inserted before others (snipped)
table.insert(sheets, {name = name, windowclass = windowclass, tabicon = tabicon})
end
function populate(win)
for _,sheet in pairs(sheets) do
win.addSheet(sheet.name, sheet.windowclass, sheet.tabicon)
end
end
So then, for other extensions, it's simply a case of making sure to call the CharsheetManager.registerSheet() method on start up, define a new mini-template for the subwindow class, and the tab is inserted. No conflicts between multiple sources attempting to add tabs.
Complete tangent:
But having windowlists that can hold multiple classes would be cool too ;) Currently I use the same tricks of hiding elements and LUA massaging to get such things working, such as the combat tracker listings.
Very happy :)
Ben (-PW-)
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.