Skip to content

Commit

Permalink
some cleanup of complex "update" function
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Sep 14, 2022
1 parent f9cb8a9 commit d7123a1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
100 changes: 56 additions & 44 deletions campaign/scripts/item_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@
-- Please see the LICENSE.md file included with this distribution for attribution and copyright information.
--

-- luacheck: globals update getItemType

function getItemType()
local bWeapon, bArmor, bShield, bWand, bStaff, bWondrous;
local sType = string.lower(type.getValue());
local sSubtype = string.lower(subtype.getValue());
-- luacheck: globals update getItemTypes

function getItemTypes()
local tTypes = {
['weapon'] = false,
['armor'] = false,
['wand'] = false,
['staff'] = false,
['wondrous item'] = false,
['shield'] = false,
};

for s, _ in pairs(tTypes) do
tTypes[s] = (type.getValue() .. subtype.getValue()):lower():match(s);
end

if sType:match('weapon') then bWeapon = true; end
if sType:match('armor') then bArmor = true; end
if sType:match('wand') then bWand = true; end
if sType:match('staff') then bStaff = true; end
if sType:match('wondrous item') then bWondrous = true; end
if sType:match('shield') or sSubtype:match('shield') then bShield = true; end
return tTypes
end

return bWeapon, bArmor, bShield, bWand, bStaff, bWondrous;
local function sectionVis(tSections)
for k, v in ipairs(tSections) do
local num, bool = k, nil;
if k > 2 then
repeat
num = num - 1;
bool = tSections[num] or bool;
until num == 1;

if self['divider' .. tostring(k - 1)] then self['divider' .. tostring(k - 1)].setVisible(v and bool); end
elseif k == 2 then
if self['divider'] then self['divider'].setVisible(v and tSections[k - 1]); end
end
end
end

function update(...)
Expand All @@ -26,54 +44,51 @@ function update(...)
local bReadOnly = WindowManager.getReadOnlyState(nodeRecord);
local bID = LibraryData.getIDState("item", nodeRecord);

local bWeapon, bArmor, bShield, bWand, bStaff, bWondrous = getItemType();
local tSections = {}

local bSection1 = false;
if Session.IsHost then
if self.updateControl("nonid_name", bReadOnly, true) then bSection1 = true; end;
if self.updateControl("nonid_name", bReadOnly, true) then tSections[1] = true; end;
else
self.updateControl("nonid_name", false);
end
if (Session.IsHost or not bID) then
if self.updateControl("nonidentified", bReadOnly, true) then bSection1 = true; end;
if self.updateControl("nonidentified", bReadOnly, true) then tSections[1] = true; end;
else
self.updateControl("nonidentified", false);
end

local bSection2 = false;
if self.updateControl("type", bReadOnly, bID) then bSection2 = true; end
if self.updateControl("subtype", bReadOnly, bID) then bSection2 = true; end
if self.updateControl("type", bReadOnly, bID) then tSections[2] = true; end
if self.updateControl("subtype", bReadOnly, bID) then tSections[2] = true; end

local bSection3 = false;
if Session.IsHost then
if self.updateControl("cost", bReadOnly, bID) then bSection3 = true; end
if self.updateControl("cost", bReadOnly, bID) then tSections[3] = true; end
else
if self.updateControl('cost', bReadOnly, bID and (cost_visibility.getValue() == 0)) then bSection3 = true; end
if self.updateControl('cost', bReadOnly, bID and (cost_visibility.getValue() == 0)) then tSections[3] = true; end
end
if self.updateControl("weight", bReadOnly, bID) then bSection3 = true; end
if self.updateControl('size', bReadOnly, bID) then bSection3 = true; end
if self.updateControl("weight", bReadOnly, bID) then tSections[3] = true; end
if self.updateControl('size', bReadOnly, bID) then tSections[3] = true; end

local bSection4 = true;
local tTypes = getItemTypes();
if Session.IsHost or bID then
if bShield then
if tTypes['shield'] then
type_stats.setValue("item_main_armor", nodeRecord);
type_stats2.setValue("item_main_weapon", nodeRecord);
elseif bWeapon then
elseif tTypes['weapon'] then
type_stats.setValue("item_main_weapon", nodeRecord);
elseif bArmor then
elseif tTypes['armor'] then
type_stats.setValue("item_main_armor", nodeRecord);
else
type_stats.setValue("", "");
bSection4 = false;
tSections[4] = false;
end
else
type_stats.setValue("", "");
bSection4 = false;
tSections[4] = false;
end
type_stats.update(bReadOnly, bID);
type_stats2.update(bReadOnly, bID);

if self.updateControl('charge', bReadOnly, bID and (bWand or bStaff)) then
if self.updateControl('charge', bReadOnly, bID and (tTypes['wand'] or tTypes['staff'])) then
maxcharges.setReadOnly(bReadOnly);
charge.setReadOnly(false);
current_label.setVisible(true);
Expand All @@ -85,15 +100,16 @@ function update(...)
maxcharges_label.setVisible(false);
end

if self.updateControl('equipslot', bReadOnly, bID and bWondrous) then bSection4 = true; end
if self.updateControl('equipslot', bReadOnly, bID and tTypes['wondrous']) then tSections[4] = true; end

local bSection5 = false;
if self.updateControl("aura", bReadOnly, bID) then bSection5 = true; end
if self.updateControl("cl", bReadOnly, bID) then bSection5 = true; end
if self.updateControl("prerequisites", bReadOnly, bID) then bSection5 = true; end
if self.updateControl("activation", bReadOnly, bID and (bWeapon or bArmor or bShield or bStaff or bWondrous)) then bSection5 = true; end
if self.updateControl("aura", bReadOnly, bID) then tSections[5] = true; end
if self.updateControl("cl", bReadOnly, bID) then tSections[5] = true; end
if self.updateControl("prerequisites", bReadOnly, bID) then tSections[5] = true; end
if self.updateControl("activation", bReadOnly, bID and (
tTypes['shield'] or tTypes['armor'] or tTypes['shield'] or tTypes['staff'] or tTypes['wondrous']
)) then tSections[5] = true; end

local bSection6 = bID;
tSections[6] = bID;
description.setVisible(bID);
description.setReadOnly(bReadOnly);
self.updateControl('sourcebook', bReadOnly, bID);
Expand All @@ -102,9 +118,5 @@ function update(...)
gmonly_label.setVisible(Session.IsHost);
end

divider.setVisible(bSection1 and bSection2);
divider2.setVisible((bSection1 or bSection2) and bSection3);
divider3.setVisible((bSection1 or bSection2 or bSection3) and bSection4);
divider4.setVisible((bSection1 or bSection2 or bSection3 or bSection4) and bSection5);
divider5.setVisible((bSection1 or bSection2 or bSection3 or bSection4 or bSection5) and bSection6);
sectionVis(tSections)
end
6 changes: 3 additions & 3 deletions extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<root version="3.1">
<properties>
<name>Feature: Enhanced Items</name>
<version>4.6</version>
<author>Llisandur</author>
<version>4.7</version>
<author>llisandur</author>
<description>Adds useful fields to various types of items</description>
<ruleset>
<name>3.5E</name>
Expand All @@ -20,7 +20,7 @@
<loadorder>50</loadorder>
</properties>

<announcement text="https://www.fantasygrounds.com/forums/showthread.php?40602-Extension-Enhanced-Items-v4\rEnhanced Items v4.6\rOriginal extension by sciencephile with updates by jwguy, llisandur, rmilmine, and bmos\rAdds useful fields to various types of items." font="emotefont" />
<announcement text="https://www.fantasygrounds.com/forums/showthread.php?40602-Extension-Enhanced-Items-v4\rEnhanced Items v4.7\rOriginal extension by sciencephile with updates by jwguy, llisandur, rmilmine, zarestia, and bmos\rAdds useful fields to various types of items." font="emotefont" />

<base>
<!-- Strings -->
Expand Down

0 comments on commit d7123a1

Please sign in to comment.