A little help with text links?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Resident Eric
Posts: 3
Joined: Sat Sep 26, 2009 6:14 pm

A little help with text links?

Post by Resident Eric » Sat Sep 26, 2009 6:33 pm

Hi guys,

I'm trying to create something in Revolution that seemed to me very simple, but is begining to get a little complicated…

Basically, what I would like to do is work with text linking to cards in a Revolution stack.

I would like to write in a field something like : "Should I go to Card A or Card B? Let's choose Card A", with the two links "Card A" linked to the card "Card A" and the link "Card B" linked to the card "Card B". Then, I would like to add an if-then-else statement to the links (when you click the link "Card A", if x=0, go to card "Card A1", else, go to card "Card A2", for example). And as the text in the field isn't the definitive version, I would like to add words to the text, without creating any issues with the script.

Is there a way to do just that in Revolution?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sat Sep 26, 2009 9:31 pm

Hi Resident Eric,

welcome to the forum.

suppose you have a field that contains 2 lines of text. The first line says: Should I go to card A, the second says should I go to card B. This field is locked, traversalOn is false and the listbehaviour is true. You set all these in the property inspector.
You have a second field that you name theX, it hold 0 or 1 (just a placeholder for your future condition x)
Now you have a first card in a stack with these two fields on and you have 4 more cards: card "A1", "A2", "B1", "B2".
You set the script of the first field to the following script

Code: Select all

on mouseUp
   put field "theX" into x
   put the value of the selectedLine into tTheClickedText 
   if tTheClickedText contains "Card A" then
      if x = 0 then go to card "A1"
      if x = 1 then go to card "A2"
   end if
   if tTheClickedText contains "Card B" then
      if x = 0 then go to card "B1"
      if x = 1 then go to card "B2"
   end if
end mouseUp
This should do what you want.
By locking the text of the field you make it clickable and by setting the listbehavior to true the clicked line hilites. You may want to look up all these properties in the dictionary, also the selectedLine and the value.
If this is the most appropriate interface for you stack is up to you to decide, but it works.
regards
Bernd

Resident Eric
Posts: 3
Joined: Sat Sep 26, 2009 6:14 pm

Post by Resident Eric » Sun Sep 27, 2009 10:25 am

bn wrote:Hi Resident Eric,

welcome to the forum.

suppose you have a field that contains 2 lines of text. The first line says: Should I go to card A, the second says should I go to card B. This field is locked, traversalOn is false and the listbehaviour is true. You set all these in the property inspector.
You have a second field that you name theX, it hold 0 or 1 (just a placeholder for your future condition x)
Now you have a first card in a stack with these two fields on and you have 4 more cards: card "A1", "A2", "B1", "B2".
You set the script of the first field to the following script

Code: Select all

on mouseUp
   put field "theX" into x
   put the value of the selectedLine into tTheClickedText 
   if tTheClickedText contains "Card A" then
      if x = 0 then go to card "A1"
      if x = 1 then go to card "A2"
   end if
   if tTheClickedText contains "Card B" then
      if x = 0 then go to card "B1"
      if x = 1 then go to card "B2"
   end if
end mouseUp
This should do what you want.
By locking the text of the field you make it clickable and by setting the listbehavior to true the clicked line hilites. You may want to look up all these properties in the dictionary, also the selectedLine and the value.
If this is the most appropriate interface for you stack is up to you to decide, but it works.
regards
Bernd
Hi Bernd,

Thank you for your prompt answer! Actually, I don't need to create a field with a list, but a basic text field, with some words displayed as links like in a web page, and each link leading to different cards. Now, it could be done in another software like VoodooPad, but I also need if-then-else conditions for some links.

Now that I saw your script, I see better how the if-then-else condition could be done. I could define x into the stack script then create two buttons on a new card, leading to the main card holding the field (with put 0 into x, go to card MainCard, for the first button, and put 1 into x, go to card MainCard, for the second button, for example).

But I also need to be able to work with the text holding the links, meaning only some words of the text should be links, not whole lines, and I have to have a way to edit the text without creating issues with the links.

Do you know of a way to do just that?

Best regards,
Eric

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sun Sep 27, 2009 1:50 pm

Eric,
again you have your field 1 with the text that will have the links. Uncheck the listBehavior, leave the lockText. You still have the field "theX" for demo purposes.
Now you can have any text you want in the text field. Suppose you want the "Card A" and "Card B" in the text to be a link. Set the script of this field to

Code: Select all

on linkClicked tTheClickedText
   put field "theX" into x -- again to simulate your X
   if tTheClickedText contains "Card A" then
      if x = 0 then go to card "A1"
      if x = 1 then go to card "A2"
   end if
   if tTheClickedText contains "Card B" then
      if x = 0 then go to card "B1"
      if x = 1 then go to card "B2"
   end if
end linkClicked
Now you make a button and set the script of this button to

Code: Select all

on mouseUp
   find empty -- resets the find command
   put "Card A,Card B" into myLinks -- your text to link into this variable
   repeat with i = 1 to the number of items of myLinks
      put item i of myLinks into tUnderLineThis
      find empty -- resets the find command
      repeat
         find string tUnderLineThis in field 1
         put the foundChunk into tFound
         if tFound is empty then exit repeat
         put the textStyle of tFound into tStyle
         if not (tStyle contains "link") then
            if tStyle is not empty then
               put ",link" after tStyle
            else
               put "link" into tStyle
            end if
            set the textStyle of tFound to tStyle
         end if
      end repeat
   end repeat
   find empty -- resets the find command
end mouseUp
Note you have a item list myLinks in this script that you can fill with other things than "Card A" etc.
this button will set "Card A" and "Card B" to be a link in as many occurences as there are in the field. It looks a bit complicated because it tries not to change any other textStyles like bold or italic in the text.

If you want to take the underlines out of the field and just the underline then make a button and set its script to

Code: Select all

on mouseUp
   put the htmlText of field 1 into myHTML
   repeat until myOffset = 0
      put offset("<a>", myHtml) into myOffset
      if myOffset > 0 then delete char myOffset to myOffset + 2 of myHTML
   end repeat
   repeat until myOffset = 0
      put offset("</a>", myHtml) into myOffset
      if myOffset > 0 then delete char myOffset to myOffset + 3 of myHTML
   end repeat
   set the htmlText of field 1 to myHTML
end mouseUp
If you want to edit the text of the field 1 you can do that by changing the text as long as you dont write into "Card A" or "Card B" it is ok, the links will persist. If you happen to write into them above scripts should help you to take the links out of the field and make them anew.
MabBe there are easier solutions to this problem but this is one way of approaching it. What you basically want to dynamically have links controlled by scripts is just a bit involved. You might want to look up the different commands used in these scripts in the dictionary and the documentation. Youj might also want to look at the part: Coding-> Text in the revolution resource center(in the Help menu). Have a look at the video or the PDF.
regards
Bernd

Resident Eric
Posts: 3
Joined: Sat Sep 26, 2009 6:14 pm

Post by Resident Eric » Sun Sep 27, 2009 6:01 pm

It works! Thank you very much!! :D
Eric

Post Reply