Centering a Control on a Card ?

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Centering a Control on a Card ?

Post by Traxgeek » Mon Jan 14, 2013 2:32 pm

Hi,

This SHOULD be really simple but can I find a way of doing it ? No !

Is there a real simple method of centering a single control on a card (horizontally specifically) ?
I can see how to align multiple controls relative to each other on a card.
I can see that I can get the width of my Card and then the width of my control and then subtract 1/2 the control width fro 1/2 my card width but...

I'm a simple guy with simple issues - Is there not a simple way ?

What have I missed ?

TIA
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Centering a Control on a Card ?

Post by Klaus » Mon Jan 14, 2013 2:37 pm

Hi Traxgeek,

...
the loc of this card
...
will always give you the exact loc of the "center" of your stack.
So you could:
...
set the loc of btn "to be centered" to the loc of this cd
...
## et voila, button centered.
If THAT is what you want.


Best

Klaus

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Centering a Control on a Card ?

Post by Traxgeek » Mon Jan 14, 2013 3:24 pm

Hi Klaus,

Thanks for that. Just wondered if there was a way of my doing this from the IDE, ie drop a label, click <Centre vertically> or <Centre Horizontally>

A litlle like the Alignment Palette that appears in the Property Inspector when more than one control is selected simultaneously EXCEPT this <Centre> would do so relative to the Card the control was on.

I seem to use <Centre> (or a derivative of it) numerous times each time I create the layout for a card...

Nevermind, I can do the math but it becomes boring with multiple controls and should I change a control's width because I've changed the text or the font size or... whatever...

It's not too important, it's just 'one of those things' I've mentally noted as 'missing' / must ask about.

Thanks a million anyways.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Centering a Control on a Card ?

Post by sturgis » Mon Jan 14, 2013 7:47 pm

Make a plugin to do it for you. Should be a very simple.

In fact, if you create a stack witha single button and put the following code into the button:

Code: Select all

on mouseUp
   put the selobjs into tObs
   if (the number of lines of tObs is 1) and not (the first word of tObs is "card")then
      get matchtext(tObs,"(card id \d*.*)",tCard)
      set the loc of the selobj to the loc of tCard
   end if
end mouseUp
Then save the stack to your plugins folder. At that point you can restart LC, then any time you need to center objects on the card, go to the development menu and select your newly made plugin. (set make sure it is set to start up in palette mode)

Select an object, click the button, voila' Its centered.

Thats one of the nice things about LC. It is SO easy to create your own quicky tools to do things.

EDIT: Of course you could always just select the object and execute "set the loc of the selobj to the loc of the current card" in the message box.
EDIT2: adjusted script to ignore the card being the selected object.

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Centering a Control on a Card ?

Post by Traxgeek » Tue Jan 15, 2013 12:02 pm

Hi Sturgis,

I had no idea... ! Fantastic idea... When I get a bit better I'll give that a serious looking at for various small 'idea's I've had to make my life easier...

In the meantime, I'll 'steal' your code and put it to work.

Many thanks.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Centering a Control on a Card ?

Post by sturgis » Tue Jan 15, 2013 3:56 pm

If you need to center only vertically or only horizontally you can adjust the code. "the loc" contains the center x,y coords of an object (including cards) so item 1 of the loc of a card will give you the left right center point, and item 2 will give you the up and down. Same for other objects of course so you'll just have to manipulate the loc of the object in question to get it to where you want it to be.

For example, to center an object right and left...

Code: Select all

-- to center left and right
   put the selobjs into tObs
   if (the number of lines of tObs is 1) and not (the first word of tObs is "card") then

-- this line grabs the name and stack of the card in question.  Basically everything from "card..." to the end of the line
      get matchtext(tObs,"(card id \d*.*)",tCard) 


      set the loc of the selobj to item 1 of the loc of tCard, item 2 of the loc of the selobj
   end if

-- to center up and down
   put the selobjs into tObs
   if (the number of lines of tObs is 1) and not (the first word of tObs is "card")then
      get matchtext(tObs,"(card id \d*.*)",tCard)
      set the loc of the selobj to item 1 of the loc of the selobj, item 2 of the loc of tCard
   end if

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

Re: Centering a Control on a Card ?

Post by Traxgeek » Thu Jan 17, 2013 6:49 pm

Hi again Sturgis,

Thank you for that. Make sense apart from :

Code: Select all

get matchtext(tObs,"(card id \d*.*)",tCard) 
'Matchtext' seems to be a very powerful function - I'm looking into its use right now !

Thanks again.

Regards.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Centering a Control on a Card ?

Post by sturgis » Thu Jan 17, 2013 7:04 pm

Matchtext uses regular expressions to do its matching.

tObjs is the name of the variable that contains the data to be searched.
The string "(card id \d*.*)" means basically, look for "card id " then a digit \d then any number of following digits (the * matches any number of chars matching the preceeding char in this case \d which means "a numerical digit") Then . means match any char (space, a char, punctuation, whatever) and the trailing * means any of the preceeding, so basically it will match any char to the end of the current line. I could have probably made it simpler by just doing "(card.*)" since I want everything from card to the end of the line.

The parens () designate what data will actually be pulled out. If you were matching "this is stuff to match" but you only wanted the word "match" to end up in the variable tCard you could do "this is stuff to (match)" Silly example I know. A better example would be.. well if you had this string "This is a string with 12345 in it" and wanted to grab the digits out, your regex string could look like this "with (\d*).*" So it would find the word "with " then place the first digit, and any number of following digits into a variable, because the digit portion is within the parens.
Traxgeek wrote:Hi again Sturgis,

Thank you for that. Make sense apart from :

Code: Select all

get matchtext(tObs,"(card id \d*.*)",tCard) 
'Matchtext' seems to be a very powerful function - I'm looking into its use right now !

Thanks again.

Regards.

Post Reply