Articles Archive
Articles Search
Director Wiki
 

Editable custom scrolling fields

May 11, 1998
by Pat McClellan

Dear Multimedia Handyman,

What do you do to create an editable custom scrolling text field? I've written my own custom scrolling field behavior, but it doesn't automatically scroll when the user types past the last line in the field.

Thank you for any suggestions,
Jessica

Dear Jessica,

The key to this issue is the property of a field called "the scrollTop of member". Using this property, you can precisely set which area of a text or field member, though to be editable you will need to use a field. Set the field to "fixed" and "word wrap" in its options dialog box. From the Lingo Dictionary: "The scrollTop property determines the distance, in pixels, from the top of a field cast member to the top of the field that is currently visible in the scrolling box."

For a smoothly scrolling display, like rolling the credits on a movie, you can increment the scrolltop by 1 or 2 pixels per frame. However, to simulate typical text scrolling, you will want to increment the scrollTop by a value equal to the height of a single line of text. Note that this value is likely to change cross platform, so it is best not to hardcode the increment. For example, if you use a 12 point font on the Mac, your lineHeight is likely to be about 16 pixels. But when you play the movie on a Windows machine, it may substitute a 12 point PC font that only uses a lineHeight of 15. Your best option is not to specify the lineHeight, but simply to refer to it by its property.

We will keep track of the position value of the bottom line in a global variable called gBottomLine. When the field first appears, its bottom line will be equal to its pageHeight. As text is entered we will evaluate the locV of the last character. If that value is greater than gBottomLine, we'll reset the scrollTop of the field and gBottomLine, adding the lineHeight to each. In order to make the scroll occur automatically, we'll need to check the scroll position after each character is entered (using a keyUpScript). I set the keyUpScript to our autoScroll handler in the startMovie script. Here's the code:


on startMovie
  global gBottomLine
  set the keyUpScript to "autoScroll"
  set gBottomLine = the pageHeight of member "test"
  set the scrollTop of member "test" to 1
end
on autoScroll
  global gBottomLine
  set x = the number of chars in field "Test"
  set theTop = the scrollTop of member "test"
  set theLineH = the lineHeight of member "test"
  set charLocV = the locV of charPosToLoc ¬
    (member "test", x)
  if charLocV > gBottomLine then
    set the scrollTop of member "test" = theTop ¬
          + theLineH
    set gBottomLine = gBottomLine + theLineH
  else if charLocV < gBottomLine - theLineH then
    set the scrollTop of member "test" to max(theTop ¬
          - theLineH, 1)
    set gBottomLine = max(gBottomLine - theLineH, the ¬
          pageHeight of member "test")
  end if
end

I also added a line (the "else if" part) which checks to see if we need to scroll up -- in case the user hits the backspace key. I use the "max()" function to stop scrolling up when the full page is displayed.

To make this handler fully functional, you'll want to experiment with placing the cursor in the middle of existing text. Customize the scrolling handler to the exact behaviors you need to achieve.

Good luck with your project.

Patrick McClellan is Director Online's co-founder. Pat is Vice President, Managing Director for Jack Morton Worldwide, a global experiential marketing company. He is responsible for the San Francisco office, which helps major technology clients to develop marketing communications programs to reach enterprise and consumer audiences.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.