Page 1 of 2

Vertically Centering Cursor in Field *Solved*

Posted: Fri Jul 24, 2020 11:41 pm
by monki
Is there a way to vertically center the cursor to create a typewriter-like effect in a text field? So when the cursor moves down inside the text the field scrolls up, etc. Is this possible?

This solution by jacque and coded by SparkOut works well. I moved it to the first post to make it easier to find.

Thanks for everyone's help

Code: Select all

on textChanged
   centerCursor
end textChanged

on selectionChanged
   centerCursor
end selectionChanged

on centerCursor
   put the top of me into tTop
   put the height of me / 2 into tYoff
   put tTop + tYoff into tYmid
   put item 2 of the selectedLoc into tYLoc
   put tYmid - tYLoc into tScrollOffset
   put the vScroll of me into tVscroll
   put tVscroll - tScrollOffset into tNewVScroll
   set the vScroll of me to tNewVScroll
end centerCursor

Re: Vertically Centering Cursor in Field

Posted: Sat Jul 25, 2020 6:32 pm
by jacque
You could try using the selectedLoc to get the vertical position of the cursor, compare that to the vertical center of the field, and adjust the scroll accordingly.

Re: Vertically Centering Cursor in Field

Posted: Sat Jul 25, 2020 6:33 pm
by jiml
for a 'typewriter effect see TYPE in the dictionary

Re: Vertically Centering Cursor in Field

Posted: Sun Jul 26, 2020 3:17 am
by dunbarx
Hi.

Make a scrolling field with lots of lines, and a word or two in each of those lines. Put this into the field script:

Code: Select all

on returnInField
   put word 2 of the selectedLine into oldLine
   put the number of chars of line 1 to oldLine - 1 of me into tChars
   put word 2 of the selectedChunk - tChars - 2 into thisChar
   select after char thisChar of line oldLine + 1 of me
   set the scroll of me to the scroll of me + the textHeight of me
end returnInField
Set the cursor blinking somewhere and hit return. It is wordy for clarity. You have to remember to explicitly set the textHeight of your field. Unless you have a monoSpaced font, the cursor will not stay in the same horizontal position as you do.

Craig

Re: Vertically Centering Cursor in Field

Posted: Sun Jul 26, 2020 3:18 am
by dunbarx
for a 'typewriter effect see TYPE in the dictionary
Hi.

Did you mean just to be able to see the computer do your typing for you?

Craig

Re: Vertically Centering Cursor in Field

Posted: Sun Jul 26, 2020 4:08 am
by monki
No, I'm looking for a way to keep the line being typed into centered-ish in its field. The way a typewriter always keeps its current line in one location. Thanks for the ideas, guys. I'll dig into them tomorrow when I have more of a brain working.

Re: Vertically Centering Cursor in Field

Posted: Sun Jul 26, 2020 1:58 pm
by dunbarx
Is this what you want?

Code: Select all

on returnInField
   put word 2 of the selectedLine into oldLine   
   set the scroll of me to the scroll of me + the textHeight of me
   put return after me
   select before line oldLine + 1 of me
end returnInField
If you start typing into a field and hit return, the first few lines will set the cursor down to the next line. When you get about halfway, each new return will scroll the text, but leave the cursor in place vertically at the start of the next new line.

Craig

Re: Vertically Centering Cursor in Field

Posted: Sun Jul 26, 2020 11:45 pm
by monki
None of the solutions quite had the effect. I think part of the issue is that I'm dealing with long paragraphs of text rather than short lines. It's not that big of an issue, just thought it would be a nice thing to add. Thanks for the ideas tho.

Re: Vertically Centering Cursor in Field

Posted: Mon Jul 27, 2020 4:05 am
by dunbarx
Hi.

i have been fooling around with this, and deleted a couple of posts because after more fooling, I found them to be wanting.

When you type into a typewriter, as you approach the right side you manually return the carriage to the next line before it is too late. It is up to you not to leave yourself in the middle of a word. This is all done visually. It is only with text editors that you can type away blindfolded and the software will format properly as you go.

So here is another experiment, if anyone but me cares anymore:

Code: Select all

on textChanged
   if the right of me - item 1 of the selectedLoc < 40 then --40 is subject to tweaking
      set the currentText of me to the currentText of me & return & the last line of me
      put return after me
   end if
   
   if the number of lines of me > 5 then --5 is arbitrary. It depends on the field height
      delete line 1 of me
      select after me
   end if
end textChanged
This doesn't like actual returns, but if you just type away it works as I think you meant. It is a kludge that relies on the whole text being saved in a custom property ("the currentText" of the field) so you can get it back one day. It is unsatisfying as a kludge because of that. It is also unsatisfying in that the last line is not (yet) saved until it passes the right side limit (40 in the example)

If you still want to pursue this let me know. The action seems to do what you want, but it also has complications. I just like to play with LC.

Craig

Re: Vertically Centering Cursor in Field

Posted: Mon Jul 27, 2020 4:28 am
by monki
Thanks, Craig. I might dig back into it after I get some other stuff done.

Re: Vertically Centering Cursor in Field

Posted: Fri Jul 31, 2020 11:42 pm
by monki
dunbarx wrote:
Mon Jul 27, 2020 4:05 am
If you still want to pursue this let me know. The action seems to do what you want, but it also has complications. I just like to play with LC.

Craig

Code: Select all

on returnInField
   centerCursor(the selectedChunk)
   pass returnInField
end returnInField

on arrowKey aKey
   if aKey is "up" or aKey is "down" then centerCursor(the selectedChunk)
   pass arrowKey
end arrowKey

command centerCursor aSelectedChunk
   set the vScroll of me to PercentOfCharactersTo(word 4 of aSelectedChunk)
end centerCursor

function PercentOfCharactersTo aCharacterNumber
   local tScrollCompensation = 250
   get aCharacterNumber / (number of characters of me)
   return (((the formattedHeight of me) * it) - tScrollCompensation)
end PercentOfCharactersTo
Take a look at this--place in the field's script. Not quite centered, but does keep the text scrolling with the cursor. Any thoughts on how to improve it? Or problems I might not be seeing. Thanks.
* edited to add returnInField and if/then to only capture up/down arrows

Re: Vertically Centering Cursor in Field

Posted: Sat Aug 01, 2020 3:26 am
by monki
Added the "local tScrollCompensation = 250" var to cause the "scroll to" point to be a few characters above the cursor. That pushes the scroll point down the field. That seems to bring it more into place. I think I need a better compensation factor that will account for changing the distance between lines--leading.

Re: Vertically Centering Cursor in Field

Posted: Sat Aug 01, 2020 3:42 am
by dunbarx
Hi.

I pasted your handlers into the script of a field with many lines of text. I set the cursor somewhere in the middle, and simply got CR''s whenever I hit return.

Craig

Re: Vertically Centering Cursor in Field

Posted: Sat Aug 01, 2020 5:25 am
by monki
Hmm, my test field has 2166 words and I'm getting a scroll towards the middle effect, field width of 871 by a height of 950. It's not so pronounced if the cursor's already towards the middle.

Re: Vertically Centering Cursor in Field

Posted: Sat Aug 01, 2020 2:30 pm
by dunbarx
Hmm.

I see none of that. It doesn't matter where I set the cursor initially, all that happens is that returns are written into the field.

Have I misunderstood the original intent? In my last post with the textChanged handler, If you make a new field and just start typing, at the (arbitrary) 5th line, the insertion point will lock itself at that line, the text scrolling upward as you go. Returns are included automatically, as if you hit the typewriter's "carriage return" near the end of the line you are typing into.

The handler as it stands does not like actual typed returns, but this can be fixed by adding:

Code: Select all

on returnInField
end returnInField
So besides the fact that I cannot make your handler set work, what is missing from mine?

Craig