ShowTransform

From Director Online Wiki
Jump to: navigation, search
on ShowTransform(aTransform, aScope, aPrecision) ---------------------
  -- ACTION: Creates a string representation of a transform suitable
  --         for displaying in the Message window for debugging
  --         purposes
  -- INPUT: <aTransform> should be a transform or an node object
  --        <aScope> only has an effect if it is #world and aTransform
  --         is in fact a node object.  In this case, the
  --         worldTransform of the node is used.  If aScope is an
  --         integer, it may be used for aPrecision.
  --        <aPrecision> may be a positive integer indicating the
  --         number of figures to show after the decimal point.  5 is
  --         used by default
  -- OUTPUT: Returns a string where the transform matrix is displayed
  --         in neat columns, so long as a mono-spaced font is used.
  --         If aTransform is not a transform or a node object,
  --         the error symbol #invalidTransform is returned instead.
  --------------------------------------------------------------------
 
  vString = RETURN -- output value
 
  -- Parameter checking
  if not integerP(aPrecision) then
    if integerP(aScope) then
      aPrecision = aScope
    end if
  end if
 
  case ilk(aTransform) of
    #transform: -- continue
     
    #group, #model, #light, #camera:
      case aScope of
        #world:
          aTransform = aTransform.getWorldTransform()
         
        otherwise: -- #self
          aTransform = aTransform.transform
      end case
     
    otherwise:
      return #invalidTransform
  end case
 
  if integerP(aPrecision) then
    if aPrecision < 0 then
      aPrecision = 5
    end if
   
  else
    aPrecision = 5
  end if
  -- End of parameter checking
 
  -- Determine the maximum width of this column
  vlWidths  = [1, 1, 1, 1] -- number of spaces required by each column
  vlFigures = [] -- number of spaces required by each item
 
  -- Determine the number of spaces for each column
  repeat with i = 1 to 16
    vColumn    = ((i - 1) mod 4) + 1
    vColumnMax = vlWidths[vColumn]
   
    vItem      = aTransform[i]
    vNegative  = vItem < 0
    if vNegative then
      vFigures = 1 -- for the minus sign
    else
      vFigures = 0
    end if
   
    vItem = bitXor(vItem, 0) -- strips decimal part
    -- Thanks to Jonas Brink Wors?ɬ?e <jonas@savannah.dk> for the tip
   
    if not vItem then
      vFigures = vFigures + 1
    else
      repeat while vItem
        vFigures = vFigures + 1
        vItem = vItem / 10
      end repeat
    end if
   
    if vNegative then
      vlFigures[i] = -vFigures -- so -0.00000001 is shown correctly
     
    else
      vlFigures[i] = vFigures
    end if
   
    if vColumnMax < vFigures then
      vlWidths[vColumn] = vFigures
    end if
  end repeat
 
  -- Determine how the numbers should display
  vPrecision = the floatPrecision
  if integerP(aPrecision) then
    if aPrecision > 0 then
      the floatPrecision = aPrecision
    end if
  end if
 
  -- Create the display string
  repeat with i = 1 to 16
    vColumn = ((i - 1) mod 4) + 1
    vItem   = string(aTransform[i])
    vSpaces = vlFigures[i]
   
    -- Ensure that very small negative numbers don't lose their sign
    if vSpaces < 0 then
      if vItem.char[1] <> "-" then
        put "-" before vItem
      end if
      vSpaces = -vSpaces
    end if
   
    -- Pad this item with the right number of spaces
    vSpaceCount = vlWidths[vColumn] - vSpaces
    vSpaces = "  "
    repeat while vSpaceCount
      put " " after vSpaces
      vSpaceCount= vSpaceCount - 1
    end repeat
   
    put vSpaces&vItem after vString
    if vColumn = 4 then
      put RETURN after vString
    end if
  end repeat
 
  the floatPrecision = vPrecision
 
  return vString
end ShowTransform