Auto resize

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Auto resize

Post by dalkin » Tue Jan 19, 2021 5:50 am

Is it possible in a desktop app to set the size of the visible stack to a percentage of the available screen while preserving ratio? So many screens ... so many sizes ...

In a perfect world, the app would launch to fill 95% of the screen.
If we're treading on thin ice, well you might as well dance.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9294
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Auto resize

Post by richmond62 » Tue Jan 19, 2021 9:10 am

Yes, of course it is: but you have to do a spot of Mathematics to ensure that.
-
Screenshot 2021-01-19 at 10.23.23.png
Attachments
BR.livecode.zip
Here's the stack.
(8.82 KiB) Downloaded 146 times

dalkin
Posts: 176
Joined: Wed Jul 04, 2007 2:32 am
Location: Blackheath, Australia
Contact:

Re: Auto resize

Post by dalkin » Tue Jan 19, 2021 10:03 am

So many thanks. I suppose I would have got there in about 3,000 years.
If we're treading on thin ice, well you might as well dance.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9294
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Auto resize

Post by richmond62 » Tue Jan 19, 2021 10:41 am

However, that stack does NOT preserve its ratio.

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

Re: Auto resize

Post by dunbarx » Tue Jan 19, 2021 2:54 pm

Hi.

I did not see Richmond's stack. When you say "preserve ratio" do you mean including the controls within it or just the aspect ratio of the stack window itself?

Otherwise, setting the width and height of a stack to 95% of the screenrect should take about two lines, and one or two more to center it.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9805
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Auto resize

Post by FourthWorld » Tue Jan 19, 2021 4:13 pm

Though it's rare for an application to resize everything in its content region, if that's what you need I wonder if scaling the stack with the scaleFactor property may do what you need.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Auto resize

Post by Xero » Sat Feb 06, 2021 7:46 am

I ended up patching together a ratio-dependant stack resizer script for a project that I am still working on. It's a fair bit of maths...
If you have a stack that is a certain ratio, and you want to keep it that ratio regardless of the screen ratio (i.e. you always want your stack to be 1:1 on 3:4 or 5:3 screens), you will effectively need to do a little checking before resizing.
I work on a laptop with auto rotate that can be used portrait or landscape, and wanted a stack to open at a ratio equal to the image shown in the stack, and then resize to fit within the screen, THEN... be able to be resized using a button but still keep the ratio of the image shown in the stack.
It's all straight forwards until you start to work through different scenarios- portrait or landscape. When you're landscape, you can assume the height < width, so a 1:1 ratio stack can always be resized by using the height. Go portrait, and your stack is too wide. So you need to do a check of whether the height or width is smaller, then scale to that, then ratio the stack as necessary.
You'll need to do a stack ratio check;
A screen ratio check;
A scale to screen function for one (height or width);
A scale of the other (width or height) to match the checked stack ratio;
Then do a stack location on screen so it doesn't fly off the screen.
Beware though, anything that is dependant on bottom or right for it's location will need to be carefully scripted to not disappear when you rescale...
If you'd like I could dig up the resizer script and post, but it has a stack of extra functionality to be able to manually resize the stack.
XdM

mrcoollion
Posts: 719
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Auto resize

Post by mrcoollion » Sat Feb 06, 2021 10:04 am

@ Xero
I am interested in such a script to see how that works.
Could you post it?

Regards,
Paul

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9294
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Auto resize

Post by richmond62 » Sat Feb 06, 2021 12:41 pm

Then, & then, & then . . . indeed: but nothing, ultimately, more than the
Mathematics I learnt when I was 11 (and some of the folk round these parts
probably learnt when they were 9), and an ability to visualise things inside
one's head (not a skill gifted to everyone).

The clue to the whole jingbang lies in the "then, & then, & then" insofar
as one can do these operations sequentially rather than simultaneously.

When push-comes-to-shove good programming starts (and often finishes) inside one's head, and I get
very worried when children try to tell me that computers can think and visualise stuff without there being a head
somewhere down the line.
-
Holofernes.jpg

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: Auto resize

Post by Xero » Sun Feb 07, 2021 5:51 am

Here's the code to the open that may need to be adapted:

Code: Select all

on OpenStack
--if you replace the references to image with the working screenrect (particularly item 3= width, and item 4= height), then scale to 95% (*0.95) you should be able to patch together your sizes--
   put the width of img "Image" of stack "Image" & comma & the height of image "Image" of stack "Image" into pOriginalSize
   --check if image is too large to display onscreen, if so, shrink--
   if the height of image "image" of stack "Image"> (item 4 of the working screenrect)-200 then
      --shrink height--
      set the height of image "Image" of stack "image" to ((item 4 of the working screenrect) - 100)
      --set width proportionately to original size--
      set the width of image "Image" of stack "Image" to (item 1 of pOriginalSize / item 2 of pOriginalSize) * (the height of image "Image" of stack "Image")
      --resize stack to suit
      stackToSuitImage
   else
      --change the height and width of image stack--
      stackToSuitImage
   end if
   set the left of image "image" of stack "image" to 40
   set the top of image "Image" of stack "Image" to 40
   
   -- put top of window within the screen--
   set the top of stack "Image" to 30
   --put resizer icon to bottom right corner--
   set the location of image "Resizer" to (the width of stack "Image" - 20) & comma & (the height of stack "Image" - 20)
end OpenStack
Here's the code to my resizer:
The code sits in a button (actually a graphic). I've edited out some useless bits and altered it to work off of the stack size, not an image that I was using, so please forgive me if bits and pieces have glitches! I have tried to put in notes where possible.
Place a graphic called "resizer", 20 x 20 and offset 10 from bottom and 10 from right. Place this code in it.

Code: Select all

local theXoffset, theYoffset, allowDrag, 
local tWidtho, tBPx, tBPy
Local tSWo, tSHo
local pRatio, pPRatio

on mouseDown
   lock screen
   lock messages
   --work out the current stack sizes so we can keep it as a ratio--
   put the width of this stack into tSWo
   put the height of this stack into tSHo
   --stop the mouse moving until we've gathered necessary information--
   put false into allowDrag
   --work out ratio of stack size
   put tSWo/tSHo into pRatio
   
   --This works out where the mouse is relative to graphic used to resize things which is relative to the bottom right of screen The 70 is what I used as an offset for the centre of the graphic, you can tweak as you go.--
   put right of me - mouseH() -70  into theXoffset
   put bottom of me - mouseV() -70 into theYoffset
   --Now allow the mouse to move once we have gathered all necessary information--
   put true into allowDrag
   unlock screen
   unlock messages
end mouseDown

on mouseMove X,Y
   --exit strategy if we're not allowing mouse to move--
   if not allowDrag then exit mouseMove
   lock screen
   lock messages
   --maths--
   put X + theXoffset into Xpos 
   put (Xpos/pRatio) into Ypos
   --again, the 70 is the offset of the graphic used to resize--
   set bottomRight of me to Xpos+70,Ypos+70
   put rect of this stack into theRect
   put globalLoc(the location of me) into BRLoc
   --this is the offset of the bottom right of the graphic--
   add 20 to item 1 of BRLoc
   add 20 to item 2 of BRLoc
   --factor in the move of the mouse and offset of the graphic, and put it into the temp stack size
   put BRLoc into item 3 to 4 of theRect
   set rect of this stack to theRect
   
   put the width of this stack into tWidthN
   put tWidthN / tSWo into tScaleRatio
   --factor in bottom right of graphic offset
   put (tWidthN+20) / (tSWo+20) into pPRatio
   
   unlock messages
   unlock screen
end mouseMove

on mouseup
put false into allowdrag
end mouseup
There's one little (known) glitch to this, if you move to fast or out of the ratio line and mouseup outside of the graphic, when you mouseenter the graphic, it starts the resizing again without clicking the mouse. I am sure there are ways to fix that... I may have deleted them with my excess code.
I have just tried it in my stack and it seems to work. Let me know if it works or if there is anything that needs to be altered.
XdM

Post Reply

Return to “Talking LiveCode”