Matching Text to Screen Positions

by Gary Rosenzweig

As Lingo has evolved over the years, there have been many ways to determine where characters are position in fields and text members. For backwards compatibility, all of these are still around. However, the most recent functions for determining character positions are all that you need.

First, there was the mouseChar, the mouseWord and the mouseLine. These would return the number of the character, word, or line under the cursor. However, you couldn't use it to determine the character at an arbitrary location.

Then, we got functions like loctoCharPos which would tell us which character was under any point relative to the upper left corner of the member. Unfortunately, we had to take things into account like sprite position and scrolling.

The modern functions pointToChar, pointToWord, and pointToLine make the previous functions obsolete. Now, all you need to do is to feed one of these functions with the sprite number and screen location. They even account for scrolling.

The example movie below will show you which character, word and line the cursor is over. Here is the code that is used.

on exitFrame me
  output = ""

  n = pointToLine (sprite me.spriteNum, the mouseLoc)
  put "Line:" && n &RETURN after output

  n = pointToWord (sprite me.spriteNum, the mouseLoc)
  put "Word:" && n & RETURN after output

  n = pointToChar (sprite me.spriteNum, the mouseLoc)
  put "Char:" && n & RETURN after output
    
  member ("output").text = output
end

 

 

So what can you use this for? By combining these functions with other Lingo, you can create all sorts of things. For instance, you could show a different graphic image depending on which line of text the cursor is over. You could create an area that shows the line of text enlarged for those who have trouble reading small text. You could even recreate Director's hypertext functionality with your own code.

One thing I like to use pointToLine for is to create a functional text list. The user can scroll through a list of options and click on the one that they want. Using pointToLine is a lot easier than making every single line a piece of hypertext.

A sample Director 8 movie is available for download in Mac or Windows format.