Big bunch of questions

Moderators: LCMark, LCfraser

Post Reply
pink
Posts: 202
Joined: Wed Mar 12, 2014 6:18 pm

Big bunch of questions

Post by pink » Thu Aug 09, 2018 3:46 am

-can I / how do I wrap text in a widget?

-if I put text into a rect, and the text is to long to stay within the rect, how can I cut it off?

-can I (and how) can I align text (e.g. center or right-align)

-are there any functions built in for drawing SVGs within an widget?

-are there any easy methods for converting a list, point, rect, or paint into a string?
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

bogs
Posts: 1909
Joined: Sat Feb 25, 2017 10:45 pm

Re: Big bunch of questions

Post by bogs » Thu Aug 09, 2018 5:34 am

pink wrote:
Thu Aug 09, 2018 3:46 am
-can I / how do I wrap text in a widget?
-if I put text into a rect, and the text is to long to stay within the rect, how can I cut it off?
-can I (and how) can I align text (e.g. center or right-align)
-are there any functions built in for drawing SVGs within an widget?
-are there any easy methods for converting a list, point, rect, or paint into a string?
Hm.
  1. Hopefully, someone that works in the newer vers. of the IDE will pop in for this one.
  2. I assume by "cut it off" you mean to wrap it. Just make sure that the 'dontWrap' is unchecked (see picture below).
  3. The easiest way would be to go to the 'text formatting' properties of the field you want to set (see second picture below).
  4. Again, I'll leave this to people who use the newer IDEs to answer.
  5. Sure, if I understand what your asking (not sure what you mean by "point" and "paint", but see the code block below for what I think I did understand...
Selection_002.png
Selection_003.png
Selection_003.png (11.92 KiB) Viewed 189 times

Code: Select all

// converting a list into a string...
# I am guessing you are working from a file or text field, or even from a function. 
# if you open the message box and type in the single line field 
	put the colorNames
# you will see a list of the different color names available in Lc. to put this list into a string, 
# the code would look something like (create a default stack, one button, one field) ~
// in the button code ...
on mouseUp
   put "" into field "lstField" // put the name of your field or string variable here...
   repeat for each word x in the colorNames
      put "  " & x after field "lstField" // the quotes have 2 spaces for separation...
   end repeat
end mouseUp
The same kind of code, slightly modified, would do the same thing for rects. For example -

Code: Select all

# change the code in the button ...
on mouseUp
   put "" into field "lstField"
   put the rect of me into field "lstField"
// the rect of the button is now a string in the field...
end mouseUp
I'm guessing here, but by "point" I am thinking you mean a specific location on screen, such as the mouseLoc. Again, no more difficult than ~

Code: Select all

on mouseUp
   put "" into field "lstField"
   put the mouseLoc into field "lstField"
// you will now see the coordinates of the mouse position on screen in the field...
// if you move it to different parts of the button, you'll see the points change...
end mouseUp
Sorry, but I have no idea what you mean by "paint into a string". You can pull colors, color names, rgb values, or even the color under the mouse cursor similarly to what is posted above, but if you want something other than that, you'll probably have to clarify.
Image

Klaus
Posts: 10166
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Big bunch of questions

Post by Klaus » Thu Aug 09, 2018 9:10 am

Hi Bogs,

I think ALL of Gregs questions relate to LiveCode Builder!
Simply because this is the LiveCode Builder forum, and Greg is no newbie. 8)


Best

Klaus

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1693
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Big bunch of questions

Post by [-hh] » Thu Aug 09, 2018 10:12 am

pink wrote:1-can I / how do I wrap text in a widget?
2-if I put text into a rect, and the text is to long to stay within the rect, how can I cut it off?
3-can I (and how) can I align text (e.g. center or right-align)
4-are there any functions built in for drawing SVGs within an widget?
5-are there any easy methods for converting a list, point, rect, or paint into a string?
To 1 and 2
You have to do your own wordwrap algorithm. Helpers:
the image bounds of text tText on this canvas (respects the current font settings for the canvas),
the [layout] bounds of text tText on this canvas (respects the current font settings for the canvas),
the [layout] bounds of text tText with tFont.

To 3
Use "fill text mText at mAlignment of mRect on mCanvas". For centering compute linewise the bounds of the "text boxes" and center the "text boxes".

To 4
Not generally. Only for placing simple SVG pathes (like the iconPath of SVG Icon widget).
For that
use com.livecode.library.iconsvg
and you can for example "put path iconSVGPathFromName(tText) into tPath".

To 5
There are a few, as "formatNumberListAsItems(pList)". Else you have to write your own.
You can use for that "combine" or build the string with concatenation ("&").
shiftLock happens

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: Big bunch of questions

Post by trevordevore » Thu Aug 09, 2018 1:54 pm

Here are a couple of functions I've used.

Fit text within a certain width. Add ... if it doesn't fit.

Code: Select all

public handler fitStringToRect(inout pString as String, in pFont as Font, in pMaxWidth as Number) returns nothing
   	-- Now make sure that text will fit within the available width
   	variable tTextRect as Rectangle
   	variable tEllipsis as String
   	variable tCharCount as Integer

   	put "..." into tEllipsis
   	put the number of chars in tEllipsis into tCharCount

   	put the bounds of text pString with pFont into tTextRect
   	if the width of tTextRect > pMaxWidth then
   		put tEllipsis into char -tCharCount to -1 of pString
   		put the bounds of text pString with pFont into tTextRect
   		repeat forever
   			if the width of tTextRect <= pMaxWidth or pString is tEllipsis then
   				exit repeat
   			else
   				put "..." into char -(tCharCount+1) to -1 of pString
   				put the bounds of text pString with pFont into tTextRect
   			end if
   		end repeat
   	end if
   end handler
Convert a color to string

Code: Select all

public handler colorToString(in pColor as Color, in pIncludeAlpha as Boolean) returns String
   	variable tColor as String

   	if pColor is nothing then
   		return ""
   	end if

   	put FormatInt(the rounded of ((the red of pColor) * 255)) into tColor
   	put "," & FormatInt(the rounded of ((the green of pColor) * 255)) after tColor
   	put "," & FormatInt(the rounded of ((the blue of pColor) * 255)) after tColor

   	if pIncludeAlpha then
   		put "," & FormatInt(the rounded of ((the alpha of pColor) * 255)) after tColor
   	end if

   	return tColor
   end handler

   ----------
   -- this handler converts a String of numbers to an RGBA color
   public handler stringToColor(in pString as String) returns Color
   	if the number of chars in pString is 0 then
   	     return ""
   	end if

   	variable tRed as Number
   	variable tGreen as Number
   	variable tBlue as Number
   	variable tAlpha as Number

   	variable tComponentList as List
   	split pString by "," into tComponentList

   	variable tComponentCount
   	put the number of elements in tComponentList into tComponentCount
   	if tComponentCount is not 3 and tComponentCount is not 4 then
   		// Invalid number of components detected
   		throw "Invalid color"
   	end if

   	put (element 1 of tComponentList) parsed as number into tRed
   	put (element 2 of tComponentList) parsed as number into tGreen
   	put (element 3 of tComponentList) parsed as number into tBlue

   	if tComponentCount is 4 then
   	  if element 4 of tComponentList is "inf" then
   	    put 255 into tAlpha
   	  else
        put the minimum value of [255, (element 4 of tComponentList) parsed as number] into tAlpha
   	  end if   		
   	else
   		put 255 into tAlpha
   	end if

   	return color [ tRed/255, tGreen/255, tBlue/255, tAlpha/255 ]
   end handler
   
Convert a point to string

Code: Select all

public handler stringToPoint(in pString as String) returns Point
   	variable tList as List
   	variable tPoint as Point

   	put point [0,0] into tPoint

   	split pString by ","
   	put the result into tList
   	put tList parsed as list of number into tList
   	if the number of elements in tList is 2 then
   		put point [element 1 of tList, element 2 of tList] into tPoint
   	end if

   	return tPoint
   end handler


   public handler pointToString(in pPoint as Point) returns String
   	return FormatInt(the x of pPoint) & "," & FormatInt(the y of pPoint)
   end handler
   
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

bogs
Posts: 1909
Joined: Sat Feb 25, 2017 10:45 pm

Re: Big bunch of questions

Post by bogs » Thu Aug 09, 2018 5:45 pm

Klaus wrote:
Thu Aug 09, 2018 9:10 am
Hi Bogs,
I think ALL of Gregs questions relate to LiveCode Builder!
Simply because this is the LiveCode Builder forum, and Greg is no newbie. 8)
Best
Klaus
:oops: :oops: :oops:
My apologies, it was the end of a 20 hour day for me
:oops: :oops: :oops:
Image

pink
Posts: 202
Joined: Wed Mar 12, 2014 6:18 pm

Re: Big bunch of questions

Post by pink » Fri Aug 10, 2018 2:11 pm

Thanks guys, this helped out a lot
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 995
Joined: Thu Apr 11, 2013 11:27 am

Re: Big bunch of questions

Post by LCMark » Fri Aug 10, 2018 5:22 pm

@pink: Yes - you can render SVG inside widgets. In LiveCode 9 we added the 'drawing' (drw) format emitted by the drawingCompileSvg function as a known image format. This means such data can be set as the text of an image *and* used with the image from data / file constructors available in LCB.

Note: SVG text is not supported - it has to passed though drawingCompileSvg first (the drawing format is basically a binary form of SVG in some sense - its actually a metafile format which supports a good subset of SVG operations).

Post Reply

Return to “LiveCode Builder”