Module:ProtoFlux: Difference between revisions

From Resonite Wiki
m try to paste code without explosion
Reapply revision 89923 by Paradox19 (talk)
Tag: Undo
 
(34 intermediate revisions by 4 users not shown)
Line 3: Line 3:
local p = {}
local p = {}


-- Type colors - RGB values to be included in the HTML output when a color is needed. This will be replaced with CSS at some point.
local ProtofluxColor = require("Module:ProtoFlux_Type_Color")
local typeColor =  
{
User = '255, 128, 255',
Impulse = '179, 255, 255',
bool = '115, 115, 115',
AsyncImpulse = '204, 179, 255',
String = '245, 31, 31',
Dummy = '255, 0, 255',
IFormatProvider = '168, 143, 214',
float = '0, 255, 255',
ColorProfile = '255, 196, 54',
colorX = '255, 89, 0',
Component = '112, 76, 85',
float3 = '0, 255, 255',
float2 = '0, 255, 255',
    ulong = '0, 255, 127',
    int = '0, 255, 0',
    Continuation = '255, 255, 178',
    Slot = '191, 255, 127',
WebsocketClient = '191, 242, 176',
IField = '171, 243, 133',
ICollider = '126, 84, 37',
IAssetProvider = '154, 66, 186',
Uri = '123, 49, 186',
IComponent = '93, 134, 70',
Variable = '175, 193, 56',
Animation = '188,255,254',
int2 = '0, 255, 0',
floatQ = '0, 127, 255'
}
 
local pack2 = {
"int2",
"float2",
"uint2",
"double2",
"long2",
"ulong2",
"bool2"
}


local pack3 = {
local css = {
"int3",
root = "color: rgb(224, 224, 224); background-color: rgb(18, 20, 28); box-shadow: 0 2px 4px 2px #0005; "
"float3",
.. "width: 256px; display: flex; flex-direction: column; align-items: stretch;",
"uint3",
node_title = "padding: 10px 0px 10px 0px; text-align: center; font-weight: bold; background-color: rgb(26, 41, 54); font-size: 18pt; flex-grow: 1;",
"double3",
node_footer = "text-align: center; padding: 10px; font-size: 18pt; color: rgb(64,64,64); font-weight: bold;",
"long3",
connector_row = "display: flex; min-height: 70px;",
"ulong3",
connector_row_labels = "flex-grow: 2; overflow: visible; display: flex; flex-direction: column; justify-content: space-between; order: 2;",
"bool3"
multi_button = "font-size: 1.75rem; width: 1em; height: 1em; background-color: #2c2f35; border-radius: 2em; "
}
.. "display:flex; align-items:center; justify-content: center;",
 
multi_bar = function(self, is_input, multi)
local pack4 = {
return string.format(
"int4",
"background-color: white; opacity: 0.5; width: 6px; height: %spx; position: absolute; bottom: 40px; %s: 13px;",
"float4",
70 * (multi - 2) + 50,
"uint4",
is_input and "left" or "right"
"double4",
)
"long4",
end,
"ulong4",
label = "text-overflow: ellipsis; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; ",
"bool4"
label_multi = "font-weight: bold; font-size: 1.4rem; line-height: 1;",
input_label = function(self, color, is_multi)
if is_multi then color = nil end
return string.format(
self.label
.."text-align: left; overflow: %s; padding-left: 4px; border-right: 20px solid #11151d; "
.."background-color: %s;%s",
color and "hidden" or "visible",
color and ColorToCss(MultA(color, 0.6)) or "transparent",
is_multi and " "..self.label_multi or ""
)
end,
output_label = function(self, color, is_multi)
if is_multi then color = nil end
return string.format(
self.label
.."text-align: right; overflow: %s; padding-right: 4px; border-left: 20px solid #11151d; "
.."background-color: %s;%s",
color and "hidden" or "visible",
color and ColorToCss(MultA(color, 0.6)) or "transparent",
is_multi and " "..self.label_multi or ""
)
end,
attachment_point = function(self, color, order)
return string.format(
"width: 30px; background-color: black; fill: %s; stroke: %s; order: %s;",
ColorToCss(MultA(color, 0.15)),
ColorToCss(color),
order
)
end,
global = function(self, color)
return string.format(
"display: flex; min-height: 70px; flex-direction: column; border-left: 10px solid %s;",
ColorToCss(color)
)
end,
global_input = "border-radius: 16px; text-align: center; display: flex; flex-direction: column; justify-content: center;"
}
}


Line 72: Line 69:
local outputs = mw.text.jsonDecode(frame.args.Outputs or '[]')
local outputs = mw.text.jsonDecode(frame.args.Outputs or '[]')
local globals = mw.text.jsonDecode(frame.args.Globals or '[]')
local globals = mw.text.jsonDecode(frame.args.Globals or '[]')
-- for custom nodes. if there is a variable for this, we need to force it to lower case so the innput: `Custom=True` is converted to `Custom=true`
local is_custom = mw.text.decode(frame.args.Custom or "false")
local lowered_is_custom = string.lower(tostring(is_custom))
-- Create an HTML div element to contain our node UI
-- Create an HTML div element to contain our node UI
Line 77: Line 78:
protofluxContainer
protofluxContainer
    :attr('class', frame.args.Inline and '' or 'floatright')
    :attr('class', frame.args.Inline and '' or 'floatright')
:cssText('color: rgb(224, 224, 224); background-color: rgb(18, 20, 28); width: 256px; display: flex; flex-direction: column; align-items: stretch;')
:cssText(css.root)
:tag('div') -- HTML div to contain the node title
:tag('div') -- HTML div to contain the node title
:cssText('padding: 10px 0px 10px 0px; text-align:center; font-weight: bold; background-color: rgb(26, 41, 54); font-size: 18pt; flex-grow: 1;')
:cssText(css.node_title)
:wikitext(frame.args.Name)
:wikitext(frame.args.Name)
:done() -- Close node title div
:done() -- Close node title div
Line 87: Line 88:
for i=1,#inputs do
for i=1,#inputs do
for j=1,(inputs and inputs[i].Multi or 1) do
local multi = tonumber(inputs[i] and inputs[i].Multi or 1)
for j=1,multi do
inputs[i].MultiIndex = j
inputs[i].MultiIndex = j
table.insert(processedInputs, { Name=inputs[i].Name, Type=inputs[i].Type, Multi=inputs[i].Multi, MultiIndex=j })
table.insert(processedInputs, { Name=inputs[i].Name, Type=inputs[i].Type, Multi=multi, MultiIndex=j })
end
end
end
end
for i=1,#outputs do
for i=1,#outputs do
for j=1,(outputs and outputs[i].Multi or 1) do
local multi = tonumber(outputs[i] and outputs[i].Multi or 1)
table.insert(processedOutputs, { Name=outputs[i].Name, Type=outputs[i].Type, Multi=outputs[i].Multi, MultiIndex=j })
for j=1,multi do
table.insert(processedOutputs, { Name=outputs[i].Name, Type=outputs[i].Type, Multi=multi, MultiIndex=j })
end
end
end
end
Line 118: Line 121:
protofluxContainer
protofluxContainer
:tag('div') -- HTML div to contain the node category footer
:tag('div') -- HTML div to contain the node category footer
:cssText('text-align: center; padding: 10px; font-size: 18pt; color: rgb(64,64,64); font-weight: bold;')
:cssText(css.node_footer)
:wikitext(frame.args.Category)
:wikitext(frame.args.Category)
:done() -- Close category footer div
:done() -- Close category footer div
-- Return the HTML generated above to the wiki page this script is invoked from.
-- Return the HTML generated above to the wiki page this script is invoked from.
return tostring(protofluxContainer) .. '[[Category:ProtoFlux:All]]'
return tostring(protofluxContainer) .. (lowered_is_custom == "true" and "" or '[[Category:ProtoFlux:All]]')
end
 
--- Creates a text which should be shown when hovering over a connector
-- @param Input Table containing a Name (optional) and Type for the input on this row.
function connector_hover_text(connector)
if connector.Name == nil then
return ("<%s>"):format(connector.Type)
end
return ("%s <%s>"):format(connector.Name, connector.Type)
end
end


--- Creates a new ProtoFlux connector row in the output
--- Creates a new ProtoFlux connector row in the output
-- @param Container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param Input Table containing a Name and Type for the input on this row. Can be nil if no input is to be placed on this row.
-- @param input Table containing a Name and Type for the input on this row. Can be nil if no input is to be placed on this row.
-- @param Output Table containing a Name and Type for the output on this row. Can be nil if no output is to be placed on this row.
-- @param output Table containing a Name and Type for the output on this row. Can be nil if no output is to be placed on this row.
function CreateConnectorRow(Container, Input, Output)
function CreateConnectorRow(container, input, output)
local connectorRow = Container
local connector_row = container
:tag('div') -- HTML div to contain the connector row. We specify a min-height of 70px for consistency.
:tag('div') -- HTML div to contain the connector row. We specify a min-height of 70px for consistency.
:cssText('display: flex; min-height: 70px;')
:cssText(css.connector_row)
-- Create the input (left) attachement point
CreateConnectorAttachmentPoint(connectorRow, Input, true)


local c = connectorRow
local connector_row_labels = connector_row
:tag('div') -- HTML div to contain the input and output labels in the connector row
:tag('div') -- HTML div to contain the input and output labels in the connector row
:cssText( 'flex-grow: 2; overflow: hidden; display: flex; flex-direction: column; justify-content: space-between;')
:cssText(css.connector_row_labels)
if Input and Input.Multi and Input.MultiIndex == 1 then
 
c
if input then
:tag('div') -- HTML div to contain the input label
local color, is_impulse = GetColor(input)
:attr('title', Input and (Input.Name .. ' <' .. Input.Type .. '>') or '') -- Add a basic mouseover description
local hover_text = connector_hover_text(input)
:cssText('text-align: left; overflow: hidden; text-overflow: ellipsis; padding-left: 4px;  border-right: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color: transparent;')
 
:wikitext(Input and Input.Name or '' )
if input.Multi and input.MultiIndex > 1 then
:done() -- Close input label div
local label = connector_row_labels
elseif Input and Input.Multi and Input.MultiIndex > 1 then
:tag('div') -- HTML div to contain the input label
local c2 = c
:attr('title', hover_text) -- Add a basic mouseover description
:tag('div') -- HTML div to contain the input label
:cssText(css:input_label())
:attr('title', Input and (Input.Name .. ' <' .. Input.Type .. '>') or '') -- Add a basic mouseover description
if input.Multi == input.MultiIndex then
:cssText('text-align: left; padding-left: 4px;  border-right: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color: transparent;')
label
if Input.Multi == Input.MultiIndex then
c2
:tag('div')
:cssText('position:relative; top:2rem; display:flex; gap: 4px;')
:tag('div')
:tag('div')
:cssText('font-size: 1.75rem; width: 1em; height: 1em; background-color:grey; border-radius: 2em; display:flex; align-items:center; justify-content:center;')
:cssText('position:relative; top:2rem; display: flex; gap: 4px;')
:wikitext('+')
:tag('div')
:done()
:cssText(css.multi_button)
:tag('div')
:wikitext('+')
:cssText('font-size: 1.75rem; width: 1em; height: 1em; background-color:grey; border-radius: 2em; display:flex; align-items:center; justify-content:center;')
:done()
:wikitext('-')
:tag('div')
:done()
:cssText(css.multi_button)
:wikitext('-')
:done()
:tag('div')
:cssText(css:multi_bar(true, input.Multi))
end
else
connector_row_labels
:tag('div') -- HTML div to contain the input label
:attr('title', hover_text) -- Add a basic mouseover description
:cssText(css:input_label(color, input.Multi > 1))
:wikitext(input.Name or '')
:done() -- Close input label div
end
end
else
 
c
-- Create the input (left) attachement point
:tag('div') -- HTML div to contain the input label
local segments = GetTypeSegments(input)
:attr('title', Input and (Input.Name .. ' <' .. Input.Type .. '>') or '') -- Add a basic mouseover description
CreateConnectorAttachmentPoint(connector_row, hover_text, color, segments, is_impulse, true)
:cssText('text-align: left; overflow: hidden; text-overflow: ellipsis; padding-left: 4px;  border-right: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color:' .. GetTypeColor(Input, 0.6) .. ';')
else -- if input
:wikitext(Input and Input.Name or '' )
connector_row:tag("div"):cssText("width: 30px; order: 1;")
:done() -- Close input label div
connector_row_labels:tag("div")
end
end -- if input
if Output and Output.Multi and Output.MultiIndex == 1 then
 
c
if output then
:tag('div') -- HTML div to contain the output label
local color, is_impulse = GetColor(output)
:attr('title', Output and (Output.Name .. ' <' .. Output.Type .. '>') or '') -- Add a basic mouseover description
local hover_text = connector_hover_text(output)
:cssText('text-align: right; overflow: hidden; text-overflow: ellipsis; padding-right: 4px; border-left: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color: transparent;')
 
:wikitext(Output and Output.Name or '')
if output.Multi and output.MultiIndex > 1 then
:done() -- Close output label div
local label = connector_row_labels
elseif Output and Output.Multi and Output.MultiIndex > 1 then
:tag('div') -- HTML div to contain the output label
local c2 = c
:attr('title', hover_text) -- Add a basic mouseover description
:tag('div') -- HTML div to contain the output label
:cssText(css:output_label())
:attr('title', Output and (Output.Name .. ' <' .. Output.Type .. '>') or '') -- Add a basic mouseover description
if output.Multi == output.MultiIndex then
:cssText('text-align: right; padding-right: 4px; border-left: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color: transparent;;')
label
if Output.Multi == Output.MultiIndex then
c2
:tag('div')
:cssText('display:flex; justify-content:flex-end; gap: 4px;')
:tag('div')
:tag('div')
:cssText('font-size: 1.75rem; width: 1em; height: 1em; background-color:grey; border-radius: 2em; display:flex; align-items:center; justify-content:center;')
:cssText('position:relative; display: flex; justify-content: flex-end; gap: 4px;')
:wikitext('+')
:tag('div')
:done()
:cssText(css.multi_button)
:tag('div')
:wikitext('+')
:cssText('font-size: 1.75rem; width: 1em; height: 1em; background-color:grey; border-radius: 2em; display:flex; align-items:center; justify-content:center;')
:done()
:wikitext('-')
:tag('div')
:done()
:cssText(css.multi_button)
:wikitext('-')
:done()
:tag('div')
:cssText(css:multi_bar(false, output.Multi))
end
else
connector_row_labels
:tag('div') -- HTML div to contain the output label
:attr('title', hover_text) -- Add a basic mouseover description
:cssText(css:output_label(color, output.Multi > 1))
:wikitext(output.Name or '')
:done() -- Close output label div
end
end
else
 
c
-- Create the output (right) attachment point
:tag('div') -- HTML div to contain the output label
local segments = GetTypeSegments(output)
:attr('title', Output and (Output.Name .. ' <' .. Output.Type .. '>') or '') -- Add a basic mouseover description
CreateConnectorAttachmentPoint(connector_row, hover_text, color, segments, is_impulse, false);
:cssText('text-align: right; overflow: hidden; text-overflow: ellipsis; padding-right: 4px; border-left: 20px solid #11151d; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; background-color: ' .. GetTypeColor(Output, 0.6) .. ';')
else -- if output
:wikitext(Output and Output.Name or '')
connector_row:tag("div"):cssText("width: 30px; order: 3;")
:done() -- Close output label div
end -- if output
end
-- Create the output (right) attachment point
CreateConnectorAttachmentPoint(connectorRow, Output, false);
end
end


--- Creates a new ProtoFlux global input field row in the output
--- Creates a new ProtoFlux global input field row in the output
-- @param Container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param Global Table containing a Name and Type for the global field on this row.
-- @param global Table containing a Name and Type for the global field on this row.
function CreateGlobalsRow(Container, Global)
function CreateGlobalsRow(container, global)
Container
local color = GetTypeColor(global)
local hover_text = connector_hover_text(global)
 
container
:tag( 'div' )
:tag( 'div' )
:attr('title', Global.Name .. ' <' .. Global.Type .. '>') -- Add a basic mouseover description
:attr('title', hover_text) -- Add a basic mouseover description
:cssText('display: flex; min-height: 70px; flex-direction: column; border-left: 10px solid ' .. GetTypeColor(Global, 1.0) .. ';')
:cssText(css:global(color))
:tag( 'div' )
:tag( 'div' )
:cssText('display: flex; flex-direction: row; align-items: center; flex-grow:1; overflow: hidden;')
:cssText('display: flex; flex-direction: row; align-items: center; flex-grow:1; overflow: hidden;')
:tag( 'span' )
:tag( 'span' )
:cssText('text-align: center; overflow: hidden; text-overflow: ellipsis; font-size: 14pt; font-weight: bold; flex-grow:1;')
:cssText('text-align: center; overflow: hidden; text-overflow: ellipsis; font-size: 14pt; font-weight: bold; flex-grow:1;')
:wikitext(Global.Name)
:wikitext(global.Name)
:done()
:done()
:done()
:done()
:tag( 'div' )
:tag( 'div' )
:attr('style', 'display:flex; gap: 10px; flex-grow: 1;')
:attr('style', 'display: flex; gap: 10px; flex-grow: 1;')
:tag('div')
:tag('div')
:cssText('border-radius: 16px; background-color: #777; font-style: italic; text-align:center; flex-grow: 3; display: flex; flex-direction: column; justify-content: center;')
:cssText('background-color: #777; font-style: italic; flex-grow: 3; '..css.global_input)
:tag( 'span' )
:tag( 'span' )
:wikitext('null')
:wikitext('null')
:done()
:done()
:done()
:done()
:tag( 'div')
:tag( 'div' )
:cssText('border-radius: 16px; background-color: #333; text-align:center; flex-grow: 1; display: flex; flex-direction: column; justify-content: center;')
:cssText('background-color: #333; flex-grow: 1; '..css.global_input)
:tag ( 'span' )
:tag ( 'span' )
:wikitext('∅')   
:wikitext('∅')   
Line 241: Line 266:


--- Creates a new input or output attachement point for a connector row.
--- Creates a new input or output attachement point for a connector row.
-- @param Container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param Connector Table containing a Name (Optional) and Type (Required) for the input/output attachment point.
--  @param hover_text Text to show when hovering, possibly nil
-- @param isInput True if this is an input side attachment point, false if it is an output side attachment point.
-- @param color The color of this attachment point (table with r, g, b, optionally a).
function CreateConnectorAttachmentPoint(Container, Connector, isInput)
-- @param segments The number of segments for the type, used for vectors or matrices.
if Connector and (Connector.Type == "Impulse" or Connector.Type == "AsyncImpulse" or Connector.Type == "Continuation")  then
-- @param is_impulse True if this is an impulse, false if it is a data type.
Container
-- @param is_input True if this is an input side attachment point, false if it is an output side attachment point.
:tag( 'div' )
function CreateConnectorAttachmentPoint(container, hover_text, color, segments, is_impulse, is_input)
:attr('title', Connector and (Connector.Name .. ' <' .. Connector.Type .. '>') or '') -- Add a basic mouseover description
local connector_type = nil
:cssText('width:30px; background-color: black; fill: ' .. GetTypeColor(Connector, 0.3) .. '; stroke: ' .. GetTypeColor(Connector, 1.0))
if is_impulse then connector_type = "InputArrow"
:wikitext(mw.getCurrentFrame():expandTemplate({title = 'ProtoFluxConnector', args = {Type="InputArrow"}}))
elseif is_input then connector_type = "InputBox"
 
else connector_type = "OutputBox"
elseif Connector then
Container
:tag( 'div' )
:attr('title', Connector and (Connector.Name .. ' <' .. Connector.Type .. '>') or '') -- Add a basic mouseover description
:cssText('width:30px; fill: ' .. GetTypeColor(Connector, 0.3) .. '; stroke: ' .. GetTypeColor(Connector, 1.0))
:wikitext(mw.getCurrentFrame():expandTemplate({title = 'ProtoFluxConnector', args = {Type=(isInput and "InputBox" or "OutputBox")}}))
end
end
end


--- Returns an RGBA value representing the type color within Resonite
container
-- @param Connector Table containing a Name (Optional) and Type (Required) for the input/output attachment point.
:tag( 'div' )
-- @param Alpha The alpha to use in the RGBA value.
:attr('title', hover_text or "") -- Add a basic mouseover description
function GetTypeColor(Connector, Alpha)
:cssText(css:attachment_point(color, is_input and 1 or 3))
return (Connector and 'rgba(' .. (typeColor[Connector.Type] or '0, 0, 0') .. ',' .. Alpha .. ')' or 'rgba(0, 0, 0, 0)')
:wikitext(mw.getCurrentFrame():expandTemplate({title = 'ProtoFluxConnector', args = {Type=connector_type, Segments=segments}}))
end
end


 
--- Returns a CSS color for the data type or impulse and whether the type is an impulse
 
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output.
--Copyright (c) 2007-2008 Neil Richardson (nrich@iinet.net.au)
function GetColor(connector)
--
if connector == nil then error("connector is nil") end
--Permission is hereby granted, free of charge, to any person obtaining a copy
if connector == nil or connector.Type == nil then error("Missing Type") end
--of this software and associated documentation files (the "Software"), to deal
local color = ProtofluxColor.get_impulse_color(connector.Type)
--in the Software without restriction, including without limitation the rights
if color ~= nil then
--to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
return color, true
--copies of the Software, and to permit persons to whom the Software is  
--furnished to do so, subject to the following conditions:
--
--The above copyright notice and this permission notice shall be included in all
--copies or substantial portions of the Software.
--
--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
--IN THE SOFTWARE.
 
module('CRC32', package.seeall)
 
local max = 2^32 -1
 
local CRC32 = {
    0,79764919,159529838,222504665,319059676,
    398814059,445009330,507990021,638119352,
    583659535,797628118,726387553,890018660,
    835552979,1015980042,944750013,1276238704,
    1221641927,1167319070,1095957929,1595256236,
    1540665371,1452775106,1381403509,1780037320,
    1859660671,1671105958,1733955601,2031960084,
    2111593891,1889500026,1952343757,2552477408,
    2632100695,2443283854,2506133561,2334638140,
    2414271883,2191915858,2254759653,3190512472,
    3135915759,3081330742,3009969537,2905550212,
    2850959411,2762807018,2691435357,3560074640,
    3505614887,3719321342,3648080713,3342211916,
    3287746299,3467911202,3396681109,4063920168,
    4143685023,4223187782,4286162673,3779000052,
    3858754371,3904687514,3967668269,881225847,
    809987520,1023691545,969234094,662832811,
    591600412,771767749,717299826,311336399,
    374308984,453813921,533576470,25881363,
    88864420,134795389,214552010,2023205639,
    2086057648,1897238633,1976864222,1804852699,
    1867694188,1645340341,1724971778,1587496639,
    1516133128,1461550545,1406951526,1302016099,
    1230646740,1142491917,1087903418,2896545431,
    2825181984,2770861561,2716262478,3215044683,
    3143675388,3055782693,3001194130,2326604591,
    2389456536,2200899649,2280525302,2578013683,
    2640855108,2418763421,2498394922,3769900519,
    3832873040,3912640137,3992402750,4088425275,
    4151408268,4197601365,4277358050,3334271071,
    3263032808,3476998961,3422541446,3585640067,
    3514407732,3694837229,3640369242,1762451694,
    1842216281,1619975040,1682949687,2047383090,
    2127137669,1938468188,2001449195,1325665622,
    1271206113,1183200824,1111960463,1543535498,
    1489069629,1434599652,1363369299,622672798,
    568075817,748617968,677256519,907627842,
    853037301,1067152940,995781531,51762726,
    131386257,177728840,240578815,269590778,
    349224269,429104020,491947555,4046411278,
    4126034873,4172115296,4234965207,3794477266,
    3874110821,3953728444,4016571915,3609705398,
    3555108353,3735388376,3664026991,3290680682,
    3236090077,3449943556,3378572211,3174993278,
    3120533705,3032266256,2961025959,2923101090,
    2868635157,2813903052,2742672763,2604032198,
    2683796849,2461293480,2524268063,2284983834,
    2364738477,2175806836,2238787779,1569362073,
    1498123566,1409854455,1355396672,1317987909,
    1246755826,1192025387,1137557660,2072149281,
    2135122070,1912620623,1992383480,1753615357,
    1816598090,1627664531,1707420964,295390185,
    358241886,404320391,483945776,43990325,
    106832002,186451547,266083308,932423249,
    861060070,1041341759,986742920,613929101,
    542559546,756411363,701822548,3316196985,
    3244833742,3425377559,3370778784,3601682597,
    3530312978,3744426955,3689838204,3819031489,
    3881883254,3928223919,4007849240,4037393693,
    4100235434,4180117107,4259748804,2310601993,
    2373574846,2151335527,2231098320,2596047829,
    2659030626,2470359227,2550115596,2947551409,
    2876312838,2788305887,2733848168,3165939309,
    3094707162,3040238851,2985771188,
}
 
local function xor(a, b)
    local calc = 0   
 
    for i = 32, 0, -1 do
local val = 2 ^ i
local aa = false
local bb = false
 
if a == 0 then
    calc = calc + b
    break
end
end
return GetTypeColor(connector), false
end


if b == 0 then
--- Returns a CSS color for the data type
    calc = calc + a
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output or global.
    break
function GetTypeColor(connector)
end
if connector == nil then error("connector is nil") end
 
if connector == nil or connector.Type == nil then error("Missing Type") end
if a >= val then
local color = ProtofluxColor.get_type_color(connector.Type)
    aa = true
return MultRGB(color, 1.5)
    a = a - val
end
end


if b >= val then
function MultRGB(color, fact)
    bb = true
return {
    b = b - val
r=color.r*fact,
end
g=color.g*fact,
 
b=color.b*fact,
if not (aa and bb) and (aa or bb) then
a=color.a,
    calc = calc + val
}
end
    end
 
    return calc
end
end


local function lshift(num, left)
function MultA(color, fact)
    local res = num * (2 ^ left)
return {
    return res % (2 ^ 32)
r=color.r,
g=color.g,
b=color.b,
a=(color.a or 1)*fact,
}
end
end


local function rshift(num, right)
function ColorToCss(color)
    local res = num / (2 ^ right)
return ("rgba(%s,%s,%s,%s)"):format(color.r*255, color.g*255, color.b*255, color.a or 1)
    return math.floor(res)
end
end


function Hash(str)
local type_segments = {
    local count = string.len(tostring(str))
enabled = {
    local crc = max
bool=true,
   
byte=true,
    local i = 1
sbyte=true,
    while count > 0 do
ushort=true,
local byte = string.byte(str, i)
short=true,
 
uint=true,
crc = xor(lshift(crc, 8), CRC32[xor(rshift(crc, 24), byte) + 1])
int=true,
ulong=true,
long=true,
float=true,
double=true,
decimal=true,
},
suffix = {
["2"]=2,
["3"]=3,
["4"]=4,
--["2x2"]=2,
--["3x3"]=3,
--["4x4"]=4,
},
}


i = i + 1
count = count - 1
    end


    return crc
--- Returns a CSS color for the data type
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output.
function GetTypeSegments(connector)
if connector == nil then error("connector is nil") end
if connector == nil or connector.Type == nil then error("Missing Type") end
local base = connector.Type:match("^%a+")
if not type_segments.enabled[base] then return 1 end
local suffix = connector.Type:sub(#base+1)
return type_segments.suffix[suffix] or 1
end
end


return p
return p

Latest revision as of 20:53, 22 August 2024

This is the Lua module for generating ProtoFlux UI elements on pages such as To String (ProtoFlux)

Please note, due to caching, updates made to this module will not immediately take effect on pages using it. If you need to see changes right away, use the editor view of a given page, which will update immediately.

Parameters

Name

Name of this ProtoFlux node. This should be the same as the name you see at the top of a given node in Resonite.

Category

Category for this ProtoFlux node. This is not the full category path, but only the direct parent - it should be the same as what appears at the bottom of a given node in Resonite.

Inline

Controls whether this node is displayed inline, or floats to the right. If this parameter is set, the node will draw inline, regardless of the value passed to it.

If you need content to flow below the element, use <div style="clear:right;"></div>, add the class .floatnone to an element, or use other elements with a similar CSS style tag.

Inputs

A string containing a JSON-encoded array of Name/Type information for each input on this node. Example input might be similar to the following, which adds two inputs of type String, one named Input1 and one named Input2

[{"Name":"Input1", "Type":"String"}, {"Name":"Input2", "Type":"String"}]

Outputs

A string containing a JSON-encoded array of Name/Type information for each output on this node. Example input might be similar to the following, which adds two outputs of type bool, one named Output1 and one named Output2

[{"Name":"Output1", "Type":"bool"}, {"Name":"Output2", "Type":"bool"}]

Globals

A string containing a JSON-encoded array of Name/Type information for each global on this node. Example input might be similar to the following, which adds two inputs of type User, one named Global1 and one named Global2

[{"Name":"Global1", "Type":"User"}, {"Name":"Global2", "Type":"User"}]

Custom

Either true or false (not case sensitive, defaults to false), if true the page is not added to ProtoFlux:All. This is mainly used for custom nodes.

Custom=false

Example Usage

If we combine all of the above examples together, we end up with this template invocation:

{{#Invoke:ProtoFlux|GenerateUI
|Name=Example Name
|Category=Example Category
|Inputs=
[
{"Name": "Input1", "Type": "Call"},
{"Name": "Input2", "Type": "String"}
]
|Outputs=
[
{"Name": "Output1", "Type": "bool"},
{"Name": "Output2", "Type": "bool"}
]
|Globals=
[
{"Name": "Global1", "Type": "User"},
{"Name": "Global2", "Type": "User"}
]
|}}

which results in the following:

Example Name
Input1
Output1
Input2
Output2
Global1
null
Global2
null
Example Category

-- Package definition to return to Scribunto - this allows us to define methods that can be called
-- when this module is targeted.
local p = {}

local ProtofluxColor = require("Module:ProtoFlux_Type_Color")

local css = {
	root = "color: rgb(224, 224, 224); background-color: rgb(18, 20, 28); box-shadow: 0 2px 4px 2px #0005; "
		.. "width: 256px; display: flex; flex-direction: column; align-items: stretch;",
	node_title = "padding: 10px 0px 10px 0px; text-align: center; font-weight: bold; background-color: rgb(26, 41, 54); font-size: 18pt; flex-grow: 1;",
	node_footer = "text-align: center; padding: 10px; font-size: 18pt; color: rgb(64,64,64); font-weight: bold;",
	connector_row = "display: flex; min-height: 70px;",
	connector_row_labels = "flex-grow: 2; overflow: visible; display: flex; flex-direction: column; justify-content: space-between; order: 2;",
	multi_button = "font-size: 1.75rem; width: 1em; height: 1em; background-color: #2c2f35; border-radius: 2em; "
		.. "display:flex; align-items:center; justify-content: center;",
	multi_bar = function(self, is_input, multi)
		return string.format(
			"background-color: white; opacity: 0.5; width: 6px; height: %spx; position: absolute; bottom: 40px; %s: 13px;",
			70 * (multi - 2) + 50,
			is_input and "left" or "right"
		)
	end,
	label = "text-overflow: ellipsis; border-bottom: 4px solid #11151d; border-top: 4px solid #11151d; ",
	label_multi = "font-weight: bold; font-size: 1.4rem; line-height: 1;",
	input_label = function(self, color, is_multi)
		if is_multi then color = nil end
		return string.format(
			self.label
			.."text-align: left; overflow: %s; padding-left: 4px; border-right: 20px solid #11151d; "
			.."background-color: %s;%s",
			color and "hidden" or "visible",
			color and ColorToCss(MultA(color, 0.6)) or "transparent",
			is_multi and " "..self.label_multi or ""
		)
	end,
	output_label = function(self, color, is_multi)
		if is_multi then color = nil end
		return string.format(
			self.label
			.."text-align: right; overflow: %s; padding-right: 4px; border-left: 20px solid #11151d; "
			.."background-color: %s;%s",
			color and "hidden" or "visible",
			color and ColorToCss(MultA(color, 0.6)) or "transparent",
			is_multi and " "..self.label_multi or ""
		)
	end,
	attachment_point = function(self, color, order)
		return string.format(
			"width: 30px; background-color: black; fill: %s; stroke: %s; order: %s;",
			ColorToCss(MultA(color, 0.15)),
			ColorToCss(color),
			order
		)
	end,
	global = function(self, color)
		return string.format(
			"display: flex; min-height: 70px; flex-direction: column; border-left: 10px solid %s;",
			ColorToCss(color)
		)
	end,
	global_input = "border-radius: 16px; text-align: center; display: flex; flex-direction: column; justify-content: center;"
}

--- Method that generates the HTML output
--	@param frame A Scribunto frame instance. frame.args contains the parameters passed into the module call
function p.GenerateUI( frame )
	-- Parse the JSON input, returning an empty array if no argument was passed
	local inputs = mw.text.jsonDecode(frame.args.Inputs or '[]')
	local outputs = mw.text.jsonDecode(frame.args.Outputs or '[]')
	local globals = mw.text.jsonDecode(frame.args.Globals or '[]')

	-- for custom nodes. if there is a variable for this, we need to force it to lower case so the innput: `Custom=True` is converted to `Custom=true`
	local is_custom = mw.text.decode(frame.args.Custom or "false")
	local lowered_is_custom = string.lower(tostring(is_custom))
	
	-- Create an HTML div element to contain our node UI
	local protofluxContainer = mw.html.create( 'div' )
	protofluxContainer
	    :attr('class', frame.args.Inline and '' or 'floatright')
		:cssText(css.root)
		:tag('div') -- HTML div to contain the node title
			:cssText(css.node_title)
			:wikitext(frame.args.Name)
			:done() -- Close node title div
	
	local processedInputs = {}
	local processedOutputs = {}
	
	for i=1,#inputs do
		local multi = tonumber(inputs[i] and inputs[i].Multi or 1)
		for j=1,multi do
			inputs[i].MultiIndex = j
			table.insert(processedInputs, { Name=inputs[i].Name, Type=inputs[i].Type, Multi=multi, MultiIndex=j })
		end
	end
	
	for i=1,#outputs do
		local multi = tonumber(outputs[i] and outputs[i].Multi or 1)
		for j=1,multi do
			table.insert(processedOutputs, { Name=outputs[i].Name, Type=outputs[i].Type, Multi=multi, MultiIndex=j })
		end
	end
	
	-- Calculate the larger number of rows required (either for inputs or outputs, if the node is asymmetric)
	local maxRows = math.max(#processedInputs, #processedOutputs)
	
	
	-- Iterate over each row, and populate it with the inputs/outputs. If the node is asymmetric,
	-- the value passed for either input or output might be nil.
	
	for i=1,maxRows do
		CreateConnectorRow(protofluxContainer, processedInputs[i], processedOutputs[i])
	end
	
	-- Iterate over each global value in the node, and create a row. These elements take up the entire width
	-- of the node, and so don't need to be balanced in any way.
	for i=1,#globals do
		CreateGlobalsRow(protofluxContainer, globals[i])
	end
	
	protofluxContainer
		:tag('div') -- HTML div to contain the node category footer
			:cssText(css.node_footer)
			:wikitext(frame.args.Category)
			:done() -- Close category footer div
	
	-- Return the HTML generated above to the wiki page this script is invoked from.
	return tostring(protofluxContainer) .. (lowered_is_custom == "true" and "" or '[[Category:ProtoFlux:All]]')
end

--- Creates a text which should be shown when hovering over a connector
-- @param Input Table containing a Name (optional) and Type for the input on this row.
function connector_hover_text(connector)
	if connector.Name == nil then
		return ("<%s>"):format(connector.Type)
	end
	return ("%s <%s>"):format(connector.Name, connector.Type)
end

--- Creates a new ProtoFlux connector row in the output
-- @param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
-- @param input Table containing a Name and Type for the input on this row. Can be nil if no input is to be placed on this row.
-- @param output Table containing a Name and Type for the output on this row. Can be nil if no output is to be placed on this row.
function CreateConnectorRow(container, input, output)
	local connector_row = container
		:tag('div') -- HTML div to contain the connector row. We specify a min-height of 70px for consistency.
			:cssText(css.connector_row)

	local connector_row_labels = connector_row
		:tag('div') -- HTML div to contain the input and output labels in the connector row
			:cssText(css.connector_row_labels)

	if input then
		local color, is_impulse = GetColor(input)
		local hover_text = connector_hover_text(input)

		if input.Multi and input.MultiIndex > 1 then
			local label = connector_row_labels
				:tag('div') -- HTML div to contain the input label
					:attr('title', hover_text) -- Add a basic mouseover description
					:cssText(css:input_label())
			if input.Multi == input.MultiIndex then
				label
					:tag('div')
						:cssText('position:relative; top:2rem; display: flex; gap: 4px;')
						:tag('div')
							:cssText(css.multi_button)
							:wikitext('+')
							:done()
						:tag('div')
							:cssText(css.multi_button)
							:wikitext('-')
							:done()
						:tag('div')
							:cssText(css:multi_bar(true, input.Multi))
			end
		else
			connector_row_labels
				:tag('div') -- HTML div to contain the input label
					:attr('title', hover_text) -- Add a basic mouseover description
					:cssText(css:input_label(color, input.Multi > 1))
					:wikitext(input.Name or '')
					:done() -- Close input label div
		end

		-- Create the input (left) attachement point
		local segments = GetTypeSegments(input)
		CreateConnectorAttachmentPoint(connector_row, hover_text, color, segments, is_impulse, true)
	else -- if input
		connector_row:tag("div"):cssText("width: 30px; order: 1;")
		connector_row_labels:tag("div")
	end -- if input

	if output then
		local color, is_impulse = GetColor(output)
		local hover_text = connector_hover_text(output)

		if output.Multi and output.MultiIndex > 1 then
			local label = connector_row_labels
				:tag('div') -- HTML div to contain the output label
					:attr('title', hover_text) -- Add a basic mouseover description
					:cssText(css:output_label())
			if output.Multi == output.MultiIndex then
				label
					:tag('div')
						:cssText('position:relative; display: flex; justify-content: flex-end; gap: 4px;')
						:tag('div')
							:cssText(css.multi_button)
							:wikitext('+')
							:done()
						:tag('div')
							:cssText(css.multi_button)
							:wikitext('-')
							:done()
						:tag('div')
							:cssText(css:multi_bar(false, output.Multi))
			end
		else
			connector_row_labels
				:tag('div') -- HTML div to contain the output label
					:attr('title', hover_text) -- Add a basic mouseover description
					:cssText(css:output_label(color, output.Multi > 1))
					:wikitext(output.Name or '')
					:done() -- Close output label div
		end

		-- Create the output (right) attachment point
		local segments = GetTypeSegments(output)
		CreateConnectorAttachmentPoint(connector_row, hover_text, color, segments, is_impulse, false);
	else -- if output
		connector_row:tag("div"):cssText("width: 30px; order: 3;")
	end -- if output
end

--- Creates a new ProtoFlux global input field row in the output
--	@param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
--	@param global Table containing a Name and Type for the global field on this row.
function CreateGlobalsRow(container, global)
	local color = GetTypeColor(global)
	local hover_text = connector_hover_text(global)

	container
		:tag( 'div' )
			:attr('title', hover_text) -- Add a basic mouseover description
			:cssText(css:global(color))
			:tag( 'div' )
				:cssText('display: flex; flex-direction: row; align-items: center; flex-grow:1; overflow: hidden;')
				:tag( 'span' )
					:cssText('text-align: center; overflow: hidden; text-overflow: ellipsis; font-size: 14pt; font-weight: bold; flex-grow:1;')
					:wikitext(global.Name)
					:done()
				:done()
			:tag( 'div' )
				:attr('style', 'display: flex; gap: 10px; flex-grow: 1;')
				:tag('div')
					:cssText('background-color: #777; font-style: italic; flex-grow: 3; '..css.global_input)
					:tag( 'span' )
						:wikitext('null')
						:done()
					:done()
				:tag( 'div' )
					:cssText('background-color: #333; flex-grow: 1; '..css.global_input)
					:tag ( 'span' )
						:wikitext('∅')   
end

--- Creates a new input or output attachement point for a connector row.
--	@param container The Scribunto HTML node we'll be creating this row inside of. Should be a container of some sort.
--  @param hover_text Text to show when hovering, possibly nil
--	@param color The color of this attachment point (table with r, g, b, optionally a).
--	@param segments The number of segments for the type, used for vectors or matrices.
--	@param is_impulse True if this is an impulse, false if it is a data type.
--	@param is_input True if this is an input side attachment point, false if it is an output side attachment point.
function CreateConnectorAttachmentPoint(container, hover_text, color, segments, is_impulse, is_input)
	local connector_type = nil
	if is_impulse then connector_type = "InputArrow"
	elseif is_input then connector_type = "InputBox"
	else connector_type = "OutputBox"
	end

	container
		:tag( 'div' )
			:attr('title', hover_text or "") -- Add a basic mouseover description
			:cssText(css:attachment_point(color, is_input and 1 or 3))
			:wikitext(mw.getCurrentFrame():expandTemplate({title = 'ProtoFluxConnector', args = {Type=connector_type, Segments=segments}}))
end

--- Returns a CSS color for the data type or impulse and whether the type is an impulse
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output.
function GetColor(connector)
	if connector == nil then error("connector is nil") end
	if connector == nil or connector.Type == nil then error("Missing Type") end
	local color = ProtofluxColor.get_impulse_color(connector.Type)
	if color ~= nil then
		return color, true
	end
	return GetTypeColor(connector), false
end

--- Returns a CSS color for the data type
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output or global.
function GetTypeColor(connector)
	if connector == nil then error("connector is nil") end
	if connector == nil or connector.Type == nil then error("Missing Type") end
	local color = ProtofluxColor.get_type_color(connector.Type)
	return MultRGB(color, 1.5)
end

function MultRGB(color, fact)
	return {
		r=color.r*fact,
		g=color.g*fact,
		b=color.b*fact,
		a=color.a,
	}
end

function MultA(color, fact)
	return {
		r=color.r,
		g=color.g,
		b=color.b,
		a=(color.a or 1)*fact,
	}
end

function ColorToCss(color)
	return ("rgba(%s,%s,%s,%s)"):format(color.r*255, color.g*255, color.b*255, color.a or 1)
end

local type_segments = {
	enabled = {
		bool=true,
		byte=true,
		sbyte=true,
		ushort=true,
		short=true,
		uint=true,
		int=true,
		ulong=true,
		long=true,
		float=true,
		double=true,
		decimal=true,
	},
	suffix = {
		["2"]=2,
		["3"]=3,
		["4"]=4,
		--["2x2"]=2,
		--["3x3"]=3,
		--["4x4"]=4,
	},
}


--- Returns a CSS color for the data type
-- @param connector Table containing a Name (Optional) and Type (Required) for the input/output.
function GetTypeSegments(connector)
	if connector == nil then error("connector is nil") end
	if connector == nil or connector.Type == nil then error("Missing Type") end
	local base = connector.Type:match("^%a+")
	if not type_segments.enabled[base] then return 1 end
	local suffix = connector.Type:sub(#base+1)
	return type_segments.suffix[suffix] or 1
end

return p