Radio Buttons

From FGWiki

Jump to: navigation, search


Add to your common_templates.xml file:

<template name="radiobutton">
    <genericcontrol>
      <stateicons>
        <on>indicator_checkon</on>
        <off>indicator_checkoff</off>
      </stateicons>
      <script file="scripts/template_radiobutton.lua" />
    </genericcontrol>
  </template>

create in its own new file, in scripts/template_radiobutton.lua:

-- 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
 
-- All producers of work derived from this definition are adviced to
-- familiarize themselves with the above license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
 
-- Copyright 2008 SmiteWorks Ltd.
 
local buttonvalue = "";
local defaultvalue = false;
local sourcenode = nil;
local readonlyvalue = false;
 
function onInit()
  setIcon(stateicons[1].off[1]);
  if source and source[1] then
    sourcenode = window.getDatabaseNode().createChild(source[1],"string");
  end
  if value and value[1] then
    buttonvalue = value[1];
  end
  if default then
    defaultvalue = true;
  end
  if sourcenode then
    sourcenode.onUpdate = refresh;
    readonlyvalue = sourcenode.isStatic();
  end
  if readonly then
    readonlyvalue = true;
  end
  if defaultvalue and buttonvalue~="" and sourcenode and sourcenode.getValue()=="" then
    activate();
  end
  refresh();
end
 
function onClickDown(button, x, y)
  return (not readonlyvalue);
end
 
function onClickRelease()
  activate();
end
 
function activate()
  if not readonlyvalue and sourcenode and buttonvalue~="" and sourcenode.getValue()~=buttonvalue then
    sourcenode.setValue(buttonvalue);
  end
end
 
function refresh()
  if sourcenode and buttonvalue~="" and sourcenode.getValue()==buttonvalue then
    setIcon(stateicons[1].on[1]);
  else
    setIcon(stateicons[1].off[1]);
  end
end
 
function getResult()
  if sourcenode then
    return sourcenode.getValue();
  else
    return "";
  end
end
 
function getValue()
  return buttonvalue;
end
 
function setValue(value)
  buttonvalue = value;
  refresh();
end
 
function isReadOnly()
  return readonlyvalue;
end
 
function setReadOnly(state)
  if sourcenode and sourcenode.isStatic() then
    return;
  end
  if state then
    readonlyvalue = true;
  else
    readonlyvalue = false;
  end
end
 
function isDefault()
  return defaultvalue;
end

Phew! To use it, you must designate a database node which is common to all the radio buttons in a group, each button must have a unique 'value' associated with it (so you can check which one has been selected) and one of them can have the 'default' tag declared.

The code supports read-only radio buttons (in response to static database nodes or an explicit <readonly> tag), and some simple methods to get and set the readonly, default and value attributes. Note that you can either query the underlying database node to determine which radio button is selected, or you can call getResult() on any of the radio buttons themselves. Calling getValue() returns the value associated with that button, not the value of the overall result.


Hope that helps!


Stuart




This text taken from a post on the www.fantasygrounds.com boards with Stuart's kind permission.

- Obe

Personal tools