Vertically Centering Cursor in Field *Solved*

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

monki
Posts: 56
Joined: Tue Dec 13, 2011 10:56 pm

Re: Vertically Centering Cursor in Field

Post by monki » Sat Aug 01, 2020 3:21 pm

Maybe the typewriter metaphor was a bit confusing. What I'm trying to do is scroll lock the cursor to, more or less, the center of the field--for most of the doc at least. This should create the effect of the paragraphs scrolling up to meet the cursor position. And because it does this, edits, returns, etc., happen more central to the field.

My script does a version of this, At least in my testing. It's a bit herky-jerky but basically functional. But translating the character count to a vScroll percent is a vague measure. Ideally, I think, the scroll would be linked to a percentage of visual (soft wrapped) lines.

SparkOut
Posts: 2254
Joined: Sun Sep 23, 2007 4:58 pm

Re: Vertically Centering Cursor in Field

Post by SparkOut » Sat Aug 01, 2020 5:34 pm

What if you set a very large bottom margin on the field so that the last visible line of the text is spaced half-way up the height of the field, and then in the field script (without any other code)

Code: Select all

on textChanged
   set the vScroll of me to the formattedHeight of me
end textChanged
For me, that seems to do what you want, assuming I have understood what you want!

[edit] I think I have misunderstood, because this applies to having empty space at the end of the field so that a newly typed line is centred vertically. Works great, except that I think you want to be able to scroll up and down within pre-existing text and have the insertion point always at the middle vertically. Right?[/edit]

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6775
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Vertically Centering Cursor in Field

Post by dunbarx » Sat Aug 01, 2020 6:06 pm

Is Sparkout correct? I had thought you wanted a "typewriter", where the "current" line is always in the same place vertically, and whatever you have already typed lives above.

To hold the cursor at the same vLoc, regardless of how you edit, scroll, cut and paste or otherwise manipulate EXISTING text, including text below, is a different problem.

Craig

SparkOut
Posts: 2254
Joined: Sun Sep 23, 2007 4:58 pm

Re: Vertically Centering Cursor in Field

Post by SparkOut » Sat Aug 01, 2020 6:41 pm

Try this in the field script

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
I have "spelt out" the construction of the calculated offset step by step, you can obviously condense this.
Vertical Centre.zip
(745 Bytes) Downloaded 4 times

monki
Posts: 56
Joined: Tue Dec 13, 2011 10:56 pm

Re: Vertically Centering Cursor in Field

Post by monki » Sun Aug 02, 2020 12:36 am

SparkOut wrote:
Sat Aug 01, 2020 6:41 pm
Try this in the field script
Yeah, that nailed it. :D Handles changes to text size and leading too. Excellent.
Thanks for all your help guys.

SparkOut
Posts: 2254
Joined: Sun Sep 23, 2007 4:58 pm

Re: Vertically Centering Cursor in Field

Post by SparkOut » Sun Aug 02, 2020 4:25 am

Glad it worked. This is actually an implementation of what jacque suggested in the first reply to your question. :D

monki
Posts: 56
Joined: Tue Dec 13, 2011 10:56 pm

Re: Vertically Centering Cursor in Field

Post by monki » Sun Aug 02, 2020 5:36 am

I rarely use long text fields and mostly don't even use scrolling fields, so selectedLoc and vScroll I don't know much of anything about. So I couldn't make the association that she was trying to point me towards. A made a more complicated one that didn't work as well 8)
Last edited by monki on Sun Aug 02, 2020 4:00 pm, edited 1 time in total.

SparkOut
Posts: 2254
Joined: Sun Sep 23, 2007 4:58 pm

Re: Vertically Centering Cursor in Field

Post by SparkOut » Sun Aug 02, 2020 8:24 am

Yes, we all do stuff like that. I didn't mean you were at fault, I just didn't want to steal credit from jacque for her suggestion.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”