<!-- This file is provided under the Open Game License version 1.0a
  -- For more information on OGL and related issues, see 
  --   http://www.wizards.com/d20
  --
  -- For information on the Fantasy Grounds d20 Ruleset licensing and
  -- the OGL license text, see the d20 ruleset license in the program
  -- options.
  --
  -- All producers of work derived from this definition are adviced to
  -- familiarize themselves with the above licenses, and to take special
  -- care in providing the definition of Product Identity (as specified
  -- by the OGL) in their products.
  --
  -- Copyright 2007 SmiteWorks Ltd.
  -->

<?xml version="1.0" encoding="iso-8859-1"?>
<root version="2.0">
	<template name="abilityscore">
		<numberfield>
			<anchored>
				<position>belowleft</position>
				<offset>0,7</offset>
				<size>
					<width>32</width>
					<height>20</height>
				</size>
			</anchored>
			<frame>
				<name>bonus</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<font>sheetnumber</font>
			<script>
				function onInit()
					if getValue() == 0 then
						setValue(10);
					end
				end
			</script>
		</numberfield>
	</template>
	
	<template name="abilitylabel">
		<stringcontrol>
			<anchored>
				<position>lefthigh</position>
				<offset>0,4</offset>
				<size>
					<width>90</width>
					<height>20</height>
				</size>
			</anchored>
			<static />
			<font>sheetlabel</font>
		</stringcontrol>
	</template>

	<template name="abilitydamage">
		<numberfield>
			<anchored>
				<position>right</position>
				<offset>51,0</offset>
				<size>
					<width>32</width>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<stateframe>
				<drophilight>
					<name>sheetfocus</name>
					<offset>5,5,5,5</offset>
				</drophilight>
			</stateframe>
			<droptypes>
				<type>number</type>
			</droptypes>
			<font>sheetnumber</font>
			<hideonvalue value="0" />
			<nodrag />
			<script>
				function onValueChanged()
					local v = getValue();
					
					if v &gt; window[target[1]].getValue() then
						setValue(window[target[1]].getValue());
					end
				end
			</script>
		</numberfield>
	</template>
	
	<template name="abilitybonus">
		<modifiernumber>
			<anchored>
				<position>right</position>
				<offset>9,0</offset>
				<size>
					<width>36</width>
				</size>
			</anchored>
			<frame>
				<name>bonus</name>
				<offset>3,5,3,5</offset>
			</frame>
			<font>sheetnumber</font>
			<hideonvalue value="0" />
			<displaysign />
			<script>
				function onSourceUpdate()
					setValue(math.floor((sources[scorefield[1]].getValue() - sources[damagefield[1]].getValue() - 10) / 2) + getModifier());
				end

				function onInit()
					addSource(scorefield[1]);
					addSource(damagefield[1]);

					super.onInit();
				end
			</script>
		</modifiernumber>
	</template>

	<template name="classstring">
		<stringfield>
			<anchored>
				<top>
					<parent>classanchor</parent>
					<anchor>bottom</anchor>
					<relation>current</relation>
					<offset>5</offset>
				</top>
				<left>
					<parent>classframe</parent>
					<anchor>left</anchor>
					<relation>absolute</relation>
					<offset>15</offset>
				</left>
				<right>
					<parent>classframe</parent>
					<anchor>right</anchor>
					<relation>absolute</relation>
					<offset>-53</offset>
				</right>
				<size>
					<height>20</height>
				</size>
			</anchored>
			<font>sheettext</font>
			<frame>
				<name>textline</name>
			</frame>
		</stringfield>
	</template>

	<template name="levelnumber">
		<numberfield>
			<anchored>
				<top>
					<parent>classanchor</parent>
					<anchor>bottom</anchor>
					<relation>relative</relation>
					<offset>3</offset>
				</top>
				<left>
					<parent>classframe</parent>
					<anchor>right</anchor>
					<offset>-46</offset>
				</left>
				<size>
					<width>32</width>
					<height>20</height>
				</size>
			</anchored>
			<font>sheetnumber</font>
			<frame>
				<name>modifier</name>
				<offset>5,4,5,4</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,4,5,4</offset>
			</keyeditframe>
			<noreset />
			<script>
				function onValueChanged()
					window.characterlevel.updateValue();
				end
			</script>
		</numberfield>
	</template>
	
	<template name="hitpointfield">
		<numberfield>
			<frame>
				<name>bonus</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<font>sheetnumber</font>
			<stateframe>
				<drophilight>
					<name>sheetfocusplus</name>
					<offset>5,5,5,5</offset>
				</drophilight>
			</stateframe>
			<droptypes>
				<type>number</type>
			</droptypes>
			<script>
				function onDrop(x, y, draginfo)
					if draginfo.isType("number") then
						setValue(getValue() + draginfo.getNumberData());
						return true;
					end
				end
			</script>
		</numberfield>
	</template>

	<template name="save">
		<linkednumber>
			<anchored>
				<position>belowleft</position>
				<offset>0,8</offset>
				<size>
					<width>32</width>
					<height>20</height>
				</size>
			</anchored>
			<frame>
				<name>bonus</name>
				<offset>5,5,5,5</offset>
			</frame>
			<font>sheetnumber</font>
			<displaysign />
			<readonly />
			<script>
				function onInit()
					addSourceWithOp("saves." .. root[1] .. ".base", "+");
					addSourceWithOp("saves." .. root[1] .. ".misc", "+");
					addSourceWithOp("saves." .. root[1] .. ".temporary", "+");

					addSourceWithOp("abilities." .. stat[1] .. ".bonus", "+");
				
					super.onInit();
				end
			</script>
		</linkednumber>
	</template>

	<template name="savelabel">
		<stringcontrol>
			<anchored>
				<position>lefthigh</position>
				<offset>0,4</offset>
				<size>
					<width>80</width>
					<height>20</height>
				</size>
			</anchored>
			<font>sheetlabel</font>
			<static />
		</stringcontrol>
	</template>

	<template name="savebase">				
		<linkednumber>
			<anchored>
				<position>right</position>
				<offset>8,0</offset>
				<size>
					<width>32</width>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<font>sheetnumber</font>
			<displaysign />
			<hideonvalue>0</hideonvalue>
			<nodrag />
			<noreset />
		</linkednumber>
	</template>

	<template name="savestatbonus">
		<numbercontrol>
			<anchored>
				<position>right</position>
				<offset>8,0</offset>
				<size>
					<width>32</width>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>5,5,5,5</offset>
			</frame>
			<font>sheetnumber</font>
			<displaysign />
			<hideonvalue>0</hideonvalue>
			<readonly />
			<nodrag />
			<script>
				statNode = nil;
			
				function update(source)
					setValue(statNode.getValue());
				end

				function onInit()
					local node = window.getDatabaseNode().getChild(bonusfield[1]);
					if node then
						statNode = node;
						node.onUpdate = update;
						update();
					end
				end
			</script>
		</numbercontrol>
	</template>

	<template name="savemisc">
		<linkednumber>
			<anchored>
				<position>right</position>
				<offset>8,0</offset>
				<size>
					<width>32</width>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<font>sheetnumber</font>
			<displaysign />
			<hideonvalue>0</hideonvalue>
			<nodrag />
			<noreset />
		</linkednumber>
	</template>

	<template name="savemodifier">
		<linkednumber>
			<anchored>
				<position>right</position>
				<offset>8,0</offset>
				<size>
					<width>32</width>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>5,5,5,5</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>5,5,5,5</offset>
			</keyeditframe>
			<font>sheetnumber</font>
			<displaysign />
			<hideonvalue>0</hideonvalue>
			<nodrag />
			<noreset />
		</linkednumber>
	</template>

	<template name="textlistitemvalue">
		<stringfield name="value">
			<font>sheettext</font>
			<multilinespacing>20</multilinespacing>
			<frame>
				<name>textline</name>
			</frame>
			<script>
				function onEnter()
					local new = window.windowlist.createWindow();
					new[getName()].setFocus();
				end
				
				function onNavigateDown()
					local next = window.windowlist.getNextWindow(window);
					if next then
						next[getName()].setFocus();
						next[getName()].setCursorPosition(1);
						next[getName()].setSelectionPosition(1);
					end
				end
				
				function onNavigateUp()
					local prev = window.windowlist.getPrevWindow(window);
					if prev then
						prev[getName()].setFocus();
						prev[getName()].setCursorPosition(#prev[getName()].getValue()+1);
						prev[getName()].setSelectionPosition(#prev[getName()].getValue()+1);
					end
				end
				
				function onNavigateRight()
					if tabtarget and tabtarget[1] and tabtarget[1].next and tabtarget[1].next[1] then
						local target = window[tabtarget[1].next[1]];

						if type(target) == "stringcontrol" then
							target.setFocus();
							target.setCursorPosition(1);
							target.setSelectionPosition(1);
						end
					end
				end
				
				function onNavigateLeft()
					if tabtarget and tabtarget[1] and tabtarget[1].prev and tabtarget[1].prev[1] then
						local target = window[tabtarget[1].prev[1]];

						if type(target) == "stringcontrol" then
							target.setFocus();
							target.setCursorPosition(#target.getValue()+1);
							target.setSelectionPosition(#target.getValue()+1);
						end
					end
				end
				
				function onDeleteUp()
					if getValue() == "" and not nodelete then
						local target = window.windowlist.getPrevWindow(window);
						if target then
							target[getName()].setFocus();
							target[getName()].setCursorPosition(#target[getName()].getValue()+1);
							target[getName()].setSelectionPosition(#target[getName()].getValue()+1);
						end
						
						window.getDatabaseNode().delete();
					end
				end
				
				function onDeleteDown()
					if getValue() == "" and not nodelete then
						local target = window.windowlist.getNextWindow(window);
						if target then
							target[getName()].setFocus();
							target[getName()].setCursorPosition(1);
							target[getName()].setSelectionPosition(1);
						end
						
						window.getDatabaseNode().delete();
					end
				end
				
				function onGainFocus()
					window.setFrame("rowshade");
				end
				
				function onLoseFocus()
					window.setFrame(nil);
				end
			</script>
		</stringfield>
	</template>

	<template name="skillsetcontrol">
		<genericcontrol>
			<stateicons>
				<on>indicator_checkon</on>
				<off>indicator_checkoff</off>
			</stateicons>
			<script>
				function disable()
					if activesetnode and activesetnode.getValue() == tonumber(setid[1]) then
						activesetnode.setValue(1);
					end

					setVisible(false);
				end
			
				function update(source)
					local state = false;
					if activesetnode and activesetnode.getValue() == tonumber(setid[1]) then
						state = true;
					end
					
					if not state then
						setIcon(stateicons[1].off[1]);
					else
						setIcon(stateicons[1].on[1]);
					end
				end
				
				function onClickDown(button, x, y)
					if activesetnode and setid[1] ~= 0 and activesetnode.getValue() ~= tonumber(setid[1]) then
						activesetnode.setValue(setid[1]);
					end
				end

				function onInit()
					activesetnode = window.getDatabaseNode().getChild("activeskillset");

					--[[ If the active skill set is not yet set, initialize it to the first set ]]
					if not activesetnode then
						activesetnode = window.getDatabaseNode().createChild("activeskillset", "number");
						activesetnode.setValue(1);
					end
				
					activesetnode.onUpdate = update;
					update(activesetnode);
				end
			</script>
		</genericcontrol>
	</template>
	
	<template name="skillsetlabel">
		<stringfield>
			<font>sheettextsmall</font>
			<static />
			<script>
				function onClickDown(button, x, y)
					window[target[1]].onClickDown(button, x, y);
				end
			
				function updateVisibility()
					if defaultset then
						return;
					end
				
					if getValue() == "" then
						setVisible(false);
						window[target[1]].disable();
					else
						setVisible(true);
						window[target[1]].setVisible(true);
					end
				end

				function onValueChanged()
					updateVisibility();
				end
			
				function onInit()
					updateVisibility();
				end
			</script>
		</stringfield>
	</template>
	
	<template name="psioniccounter">
		<genericcontrol>
			<stateicons>
				<on>indicator_checkon</on>
				<off>indicator_checkoff</off>
			</stateicons>
			<spacing>10</spacing>
			<script file="scripts/charsheet_psioniccounter.lua" />
		</genericcontrol>
	</template>

	<template name="spellcounter">
		<genericcontrol>
			<stateicons>
				<on>indicator_checkon</on>
				<off>indicator_checkoff</off>
			</stateicons>
			<spacing>10</spacing>
			<script file="scripts/charsheet_spellcounter.lua" />
		</genericcontrol>
	</template>
	
	<template name="psionicsavailable">
		<numberfield>
			<anchored>
				<top>
					<anchor>top</anchor>
					<offset>8</offset>
				</top>
				<size>
					<width>25</width>
					<height>22</height>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>4,1,3,1</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>4,1,3,1</offset>
			</keyeditframe>
			<font>sheetnumbersmall</font>
			<script>
				function updateVisibility()
					local state = modenode.getValue () ~= 0;
					
					if not state and not isVisible() then
						setVisible(true);
						if window[getName() .. "label"] then
							window[getName() .. "label"].setVisible(true);
						end
					elseif state and isVisible() then
						setVisible(false);
						if window[getName() .. "label"] then
							window[getName() .. "label"].setVisible(false);
						end
					end
				end
				
				function onValueChanged()
					window.psioniclevellist.applyFilter();
				end
			
				function onInit()
					modenode = getDatabaseNode().createChild("....castingmode", "number");
					modenode.onUpdate = updateVisibility;
					updateVisibility();
				end
			</script>
		</numberfield>
	</template>

	<template name="spellsavailable">
		<numberfield>
			<anchored>
				<top>
					<anchor>top</anchor>
					<offset>8</offset>
				</top>
				<size>
					<width>25</width>
					<height>22</height>
				</size>
			</anchored>
			<frame>
				<name>modifier</name>
				<offset>4,1,3,1</offset>
			</frame>
			<keyeditframe>
				<name>sheetfocus</name>
				<offset>4,1,3,1</offset>
			</keyeditframe>
			<font>sheetnumbersmall</font>
			<script>
				function updateVisibility()
					local state = modenode.getValue () ~= 0;
					
					if not state and not isVisible() then
						setVisible(true);
						if window[getName() .. "label"] then
							window[getName() .. "label"].setVisible(true);
						end
					elseif state and isVisible() then
						setVisible(false);
						if window[getName() .. "label"] then
							window[getName() .. "label"].setVisible(false);
						end
					end
				end
				
				function onValueChanged()
					window.spelllevellist.applyFilter();
				end
			
				function onInit()
					modenode = getDatabaseNode().createChild("....castingmode", "number");
					modenode.onUpdate = updateVisibility;
					updateVisibility();
				end
			</script>
		</numberfield>
	</template>
	
	<template name="weaponattack">
		<numbercontrol>
			<font>sheetnumber</font>
			<displaysign />
			<sourcefields>
				<bonus>bonus</bonus>
				<type>type</type>
				<melee>...attackbonus.melee</melee>
				<range>...attackbonus.ranged</range>
				<weaponname>name</weaponname>
			</sourcefields>
			<script file="scripts/charsheet_weaponattack.lua" />
		</numbercontrol>
	</template>
	
	<template name="specialabilitynumber">
		<numbercontrol>
			<invisible />
			<script>
				function enable(state)
					if state then
						source = window.getDatabaseNode().createChild("number", "number");
						source.onDelete = onDelete;
						source.onUpdate = onUpdate;
						setValue(source.getValue());
					else
						source.delete();
						source = nil;
					end

					setVisible(state);
				end
				
				function checkSource()
					source = window.getDatabaseNode().getChild("number");
					if source then
						enable(true);
					end
				end
				
				function setSourceValue(value)
					if source then
						source.setValue(value);
					end
				end
				
				function onDelete()
					source = nil;
					setVisible(false);
				end
				
				function onUpdate()
					if source then
						setValue(source.getValue());
					end
				end
			
				function onValueChanged()
					if source then
						source.setValue(getValue());
					end
				end
				
				function onInit()
					registerMenuItem("Delete number value", "delete", 4);
					
					--[[ Set up monitoring for the creation of the data node ]]
					window.getDatabaseNode().onChildAdded = checkSource;
					checkSource();
				end
				
				function onMenuSelection(selection)
					if selection == 4 then
						enable(false);
					end
				end
				
				function onDrag(button, x, y, draginfo)
					if source then
						draginfo.setType("number");
						draginfo.setDescription(window.value.getValue());
						draginfo.setNumberData(getValue());
						return true;
					end
				end
			</script>
		</numbercontrol>
	</template>

	<template name="specialabilitydice">
		<diecontrol>
			<invisible />
			<script>
				function enable(state)
					if state then
						source = window.getDatabaseNode().createChild("dice", "dice");
						source.onDelete = onDelete;
						source.onUpdate = onUpdate;
						setDice(source.getValue());
					else
						source.delete();
						source = nil;
					end

					setVisible(state);
				end
				
				function checkSource()
					source = window.getDatabaseNode().getChild("dice");
					if source then
						enable(true);
					end
				end
				
				function setSourceValue(value)
					local dietypetable = {};
					
					--[[ Allow both dragdata style and database style die lists ]]
					for k,v in ipairs(value) do
						if type(v) == "table" then
							table.insert(dietypetable, v.type);
						else
							table.insert(dietypetable, v);
						end
					end
				
					if source then
						source.setValue(dietypetable);
					end
				end
				
				function onDelete()
					source = nil;
					setVisible(false);
				end
				
				function onUpdate()
					if source then
						setDice(source.getValue());
					end
				end
			
				function onValueChanged()
					if source then
						source.setValue(getValue());
					end
				end
				
				function onInit()
					registerMenuItem("Delete dice", "delete", 4);
					
					--[[ Set up monitoring for the creation of the data node ]]
					window.getDatabaseNode().onChildAdded = checkSource;
					checkSource();
				end
				
				function onMenuSelection(selection)
					if selection == 4 then
						enable(false);
					end
				end
				
				function onDrag(button, x, y, draginfo)
					if source then
						draginfo.setType("dice");
						draginfo.setDescription(window.value.getValue());
						draginfo.setDieList(getDice());
						return true;
					end
				end
			</script>
		</diecontrol>
	</template>
</root>