Dynamic textbox adjustment using ImageLingo!?

Date: Wed Mar 14 05:14:23 MST 2001
From: Jeff Murray <jeff@psychicparrot.com>
Subject: Dynamic textbox adjustment using ImageLingo!?
The behaviour dynamically adjusts the size of a text box to fit the text ... there were behaviours to do this published and floating around the various director sites, but they had a problem. Basically, they used the LoctoChar function to work out the distance between the last character in the text and a point on screen. Then, the code would step the text box size down until it fitted correctly. I wanted to apply this to a drag and drop text object, but I found that if the text ran over the edge of the screen the adjustment behaviour would go strange and double the size of the text box. Hence, I wrote this one ... it uses Imaging Lingo to create a bitmap of the text, then scans it with getPixel to find the farthest right pixel. Then it rescales the textbox to suit. Confused? So was I at 4am trying to get the thing to work ... !

It runs a little slower than the other formatters out there, but it's accurate down to the pixel and there are no problems with drag n drop.

Apply this behaviour to the text sprite you want to adjust, then when you want to adjust it use the command Sendsprite(spritenum,#setwidth) where spritenum of course should be replaced with the number of the text sprite to be adjusted. (e.g. SendSprite(12,#setwidth) etc.)

If you have any funky behaviours, please share them with the rest of the world ... global development!


-- Dynamic bounding text box adjustment behaviour
--
-- By Jeff Murray March 14th 2001 - email jeff@psychicparrot.com
-- http://www.psychicparrot.com
--
-- This behaviour works by copying the text member into a bitmap then
-- using getpixel it scans to find the farthest right pixel filled with
-- the color of the original member. This gives us the width of the text
-- and using a simple division of the fontsize, we add a little space to
-- the end to avoid auto line feeding. That's it!
--
-- Works well between 8pt and 37pt text. May have trouble on larger
-- fonts, if so adjust the "space variable".
--
-- !!Will not work if the text color is the same as the background!!
-- !!Oh, and alignment is out of the question!!
--
--
-- copyPixels code adapted from Pat McClellan's article on resizing
-- text by converting fields to bitmaps published on Director-Online.
-- http://www.director-online.com
--

global maxx
property mycolor,sourceMem,spritenum
property sourceImage,imageWidth
property imageHeight,sourceColor

on setWidth me
  
  -- set up variable default values  
  
  maxx = 0
  sourceMem = sprite(spritenum).member
  TargetMem = 4 -- temporary member for processing the bitmap
  
  -- size up text box to allow for expanding as well as cropping
  
  sourceMem.width = 760
  
  -- will not work on anything other than left aligned text
  
  sourceMem.alignment = #left  
  
  -- get parameters for new member
  
  sourceImage = member(sourceMem).image
  imageWidth = sourceImage.width
  imageHeight = sourceImage.height 
  sourceRect = sourceImage.rect
  destRect = sourceImage.rect
  
  -- turn that damn wordwrap off!
  
  member(sourceMem).wordwrap= FALSE
  
  -- copyPixels bit to copy text member into a bitmap member   
  
  newImage = image(imageWidth,imageHeight,1,0)
  (newImage).copyPixels(sourceImage, destRect, sourceRect)
  member(targetMem).image = newImage
  member(targetMem).regPoint = point(0,0) 
  
  -- scan the bitmap and find the farthest point on the right which
  -- contains the text color of the original member
  
  repeat with x = 0 to imageWidth
    repeat with y = 0 to imageHeight
      mycolor = member(targetMem).image.getpixel (point(x,y))
      --put mycolor    
      if mycolor = paletteIndex( 1 ) and maxx < x then 
        maxx=x
      end if
      
    end repeat
  end repeat
  
  -- set the original text box to the new value discovered above.  
  
  sprite(spritenum).member.width = maxx + 4 
  
end