Highlighting Lines in Scrolling Text Field

Date:    Mon, 17 Apr 1995 11:36:49 -0500
From:    Paul Hertz <paul-hertz@NWU.EDU>
Subject: Re: Hilite(not) Contest (BIG PRIZES)
Here's some code for your ongoing saga. Don't know if it does anything near what you want. Anyroad, I have skipped using the hilite command altogether, and used changing forecolor of lines of text to simulate standard menu behaviour in a text field. This same thing should work with mouseWord instead of mouseLine.

This was written for a class I'm teaching, so it's loaded with comments and debugging code:

 
on mouseDown
  global gDebug
 
  -- When the user presses the mouse button, start responding
  -- by changing the color of the line of text under the cursor.
  -- First we puppet the text sprite and set up some variables:
 
  -- take control of channel 2
  puppetSprite 2, TRUE
  -- save the current color of text
  put the foreColor of cast "menu" into textColor
  -- set the hiliteColor to color #72
  put 72 into hiliteColor
  -- save the number of the line the cursor
  -- is over in currentLine and oldLine
  put the mouseLine into currentLine
  set oldLine = currentLine
  -- the cursor is within the menu, so set mouseOut to FALSE
  set mouseOut = FALSE
 
  -- Change the color of the text:
  set the foreColor of line currentLine of cast "menu" to hiliteColor
  -- We call updateStage to draw the changed color:
  updateStage
 
  -- Now we loop until the mouse button is released
  -- tracking the line the mouse is over
  -- and changing highlighting in the menu
 
  repeat while the mouseDown
 
    -- each time through the loop, get the mouseLine
    set theLine = the mouseLine
    if theLine < 1 then
 
      -- if theLine is less than 1, then we know
      -- the cursor isn't over a line in the menu any more
      -- so restore the original color of the whole menu
      -- if we've already restored it then mouseOut is TRUE,
      -- so just keep cycling
      if mouseOut = TRUE then
        next repeat
      else
        set mouseOut = TRUE
        set the foreColor of cast "menu" to textColor
        updateStage
        if gDebug = TRUE then
          put "mouse left, current line:" && currentLine && "old line:" && oldLine
        end if
      end if
 
    else
 
      -- theLine is greater than or equal to 1
      -- so the cursor is over a line in the menu
      put theLine into currentLine
      if mouseOut = TRUE then
        -- the cursor left the menu and just reentered it now
        set the foreColor of line currentLine of cast "menu" to hiliteColor
        updateStage
        set oldLine = currentLine
        -- indicate that the cursor is over the menu again
        set mouseOut = FALSE
        if gDebug = TRUE then
          put "mouse returned, current line:" && currentLine && "old line:" && oldLine
        end if
      else
        if oldLine <> currentLine then
          -- the cursor moved to a new line
          set the foreColor of line oldLine of cast "menu" to textColor
          set the foreColor of line currentLine of cast "menu" to hiliteColor
          if gDebug = TRUE then
            put "line changed, current line:" && currentLine && "old line:" && oldLine
          end if
          set oldLine = currentLine
          updateStage
        end if
      end if
     end if
  end repeat
 
  -- restore the original color of the current line
  set the foreColor of line currentLine of cast "menu" to textColor
  if rollover(2) then
    -- if a menu item was picked, flash it
    set the foreColor of line currentLine of cast "menu" to hiliteColor
    set the foreColor of line currentLine of cast "menu" to textColor
    set the foreColor of line currentLine of cast "menu" to hiliteColor
    set the foreColor of line currentLine of cast "menu" to textColor
    -- and respond to the selected line of the menu
    doIndex currentLine
  end if
 
  -- turn off the puppet sprite (important!)
  puppetSprite 2, FALSE
end
  
on doIndex theLine
  -- this handler responds to user's choice from our menu field (sprite 2)
  global gDebug
  if theLine = 1 then
    go "Intro"
  else if theLine = 2 then
    go "Simple"
  else if theLine = 3 then
    go "Voiceover"
  else if theLine = 4 then
    go "Multilayer"
  else if theLine = 5 then
    go "Crossfade"
  else if theLine = 6 then
    go "Outro"
  else
    --
  end if
  if gDebug then put theLine
end
 

paul-hertz@nwu.edu (Paul Hertz)                | (*,*) (+,+) (#,#) (=,=) |
Coordinator, ACNS Instructional Tech. Ctr.     | (#,=) (=,#) (*,+) (+,*) |
Northwestern University                        | (=,+) (#,*) (+,=) (*,#) |
(708) 467-2443, FAX (708) 491-3824             | (+,#) (*,=) (=,*) (#,+) |
        Home Page: <http://www.acns.nwu.edu/people/paul-hertz>


Date:    Thu, 2 Mar 1995 11:14:51 -0500
From:    Roger Van Scoy <rlvs@SEI.CMU.EDU>
Subject: re Scrolling Text Box Question
Here are a couple of scripts I use to deal with highlighting lines in a scrolling text field. The performance on these are pretty good. I've added a few comments to clarify some of the more obscure points (at least they were obscure to me when I was trying to get this to work. I hope this helps.

These handlers go in your text cast member:

on mouseDown
  getLine 4 -- 4 is the channel number of the text sprite
end
 on mouseUp
  global textCastNumber, lineNumber, lineText
  -- do what ever processing the selection calls for...
end mouseUp
This is the Movie script which handles highlighting lines while the user moves the mouse around. This handler uses globals rather than returning the line text, it can be done either way.
on getLine whichSprite global textCastNumber, lineNumber, lineText set textCastNumber = the mouseCast set lineText = "" repeat while the mouseDown set lineNumber = the mouseLine if rollOver(whichSprite) then if lineNumber = -1 then -- -- This allows the field to scroll when the mouse is over -- the scroll bar area of a text field. There are times when a -1 -- is returned, the mouse is still over the field, but not over -- the scroll bar; the pass shouldn't have any effect in this case. -- pass else -- -- Just highlight the current line -- hilite line lineNumber of field textCastNumber end if else -- -- When the mouse moves out of the field, but is still down, -- we clear the highlighting by trying to highlight a line that doesn't -- exist. Any value greater than the number of lines in the text field -- will work. Note that you can't use a number <= 0, since this will -- highlight the whole field. I just picked a number big enought to -- never cause a problem and avoid a function call. -- set lineNumber = 100 hilite line lineNumber of field textCastNumber end if end repeat -- -- We don't getting the actual text of the line the user selected -- until they mouse button is released. In the first case, the mouse -- was released outside the text area of the field. In the second case, -- the user actually made a selection! -- if lineNumber = -1 then set lineText = "" else set lineText = line lineNumber of field textCastNumber end if end