11 Replies Latest reply on Jul 16, 2006 11:08 AM by wgb14

    Creating a preferences window

    wgb14 Level 1
      Hi

      I wonder if there is any way to create in Director a preferences window (see example below).

      http://www.guide.lancs.ac.uk/tellguidemyname.html

      In particular what i would like to do is to allow my users to select the things they want to select from the preferences window and then when press save to get the appropriate movie.

      If the user selects the language of the system to be English then play movie English otherwise
      if the user selects the language of the system to be Greek then play Greek movie

      and

      If the user selects architecture + maritime related information to play movie A (that contains the relevant content)
      otherwise if the user selects History and architecture then play movie B (that contains the relevant content)

      I would also like to record the preferences selected in some kind of text file that would tell what the user selected. Is there any way to do that?

      All ideas will be appriciated
      Many thanks
        • 1. Re: Creating a preferences window
          Level 7
          Small text settings files can be set with setPref and getPref these
          calls wotk with both projectors and shockwave files run in browser,
          unlike the more complicated fileIO xtra which is only for projectors.

          One handy way to store a list based database in a text file is to
          convert To/From a string form using


          setPref "myPrefFile", string(myListDatabase)


          then retreive with:


          myListDatabase = value(getpref("myPrefFile")


          There's no tolerance for syntax slipups in lists stored in text form,
          it's best to have to pref file consist of a single list.
          • 2. Re: Creating a preferences window
            Applied CD Level 1
            If you are Windows only, Buddy API has baReadIni and baWriteIni which reads and writes settings to an ini file without all the hassles of fileio. If you choose this option I have two small scripts you might be interested in that code and decode the data going into the ini file to make the data “ini safe” … mostly they just replace the “=” sign, "[", "]", ";" and a few other characters that have special significance to ini files with “non-keyboard” characters when writing and restores them to the original characters when reading … this is only a problem if you are storing strings that the user has typed.
            • 3. Re: Creating a preferences window
              wgb14 Level 1
              Hi all,

              Thanks for your replies!!! AppliedCd can you please provide me with the scripts that you are saying you have? Also, I wonder if there is a specific example (.dir file) on how to create what i want!!

              Any ideas??/
              Thanks
              • 4. Re: Creating a preferences window
                Applied CD Level 1
                I don’t have a simple dir I can post but Buddy API’s help file has some code snippets that make good examples. If you have trouble getting started let me know. What I can post are some examples of how to use my scripts to code the ini data, keep in mind that you only need to code the data if you are allowing the users to type text strings to be stored in the ini file, if all of your data is generated by lingo you can make sure the data is ini safe yourself without having to code it first.

                First the scripts, copy and save in a movie script cast member:

                on codeIniData cMode, iniString
                case cMode of
                "read" :
                iniString = replaceField(iniString,"¬","=")
                iniString = replaceField(iniString,"¶",RETURN)
                iniString = replaceField(iniString,"µ",";")
                iniString = replaceField(iniString,"§","#")
                iniString = replaceField(iniString,"«","[")
                iniString = replaceField(iniString,"»","]")
                "write" :
                iniString = replaceField(iniString,"=","¬")
                iniString = replaceField(iniString,RETURN,"¶")
                iniString = replaceField(iniString,numToChar(10),"¶")
                iniString = replaceField(iniString,";","µ")
                iniString = replaceField(iniString,"#","§")
                iniString = replaceField(iniString,"[","«")
                iniString = replaceField(iniString,"]","»")
                otherwise : alert "Unknown mode in codeIniData."
                end case
                return iniString
                end

                on replaceField input, stringToFind, stringToInsert
                output = ""
                findLen = stringToFind.length - 1
                repeat while input contains stringToFind
                currOffset = offset(stringToFind, input)
                output = output & input.char [1..currOffset]
                delete the last char of output
                output = output & stringToInsert
                delete input.char [1.. (currOffset + findLen)]
                end repeat
                set output = output & input
                return output
                end

                OK, lets say you create the following ini file using notepad and save it with the name “foodPrefs.ini” in the same folder as your dir movie:

                [breakfast]
                sue=coffee
                bill=bagel
                tom=eggs

                [lunch]
                sue=Chicken McNuggets
                bill=Quarter Pounder with cheese
                tom=Fries

                If you want to read Bill’s lunch preference into Director simply use the following:

                myIniPath = _movie.path & " foodPrefs.ini"
                billsLunch = codeIniData("read",baReadIni("lunch","bill","Error",myIniPath))
                if billsLunch = "Error" then alert "Error reading settings file."

                If you have an editable text cast member with the name "suesBreakfast" and ask sue to type her breakfast order, to save that order do the following:

                myIniPath = _movie.path & " foodPrefs.ini"
                suesOrder = codeIniData("write",member("suesBreakfast").text)
                resultCode = baWriteIni("breakfast","sue",suesOrder,myIniPath)
                if not resultCode then alert "Error writing settings file."

                The most common reason for getting the alert when reading or writing to the ini file is either an error in your path or having the ini file open in notepad at the same time Buddy API is trying to access it. For best results quit notepad before testing.

                Storing the contents of an editable text cast member is an example of why you want to code the ini data. It is possible, although unlikely, that the user will type characters that have special meaning in an ini file, I simply temporarily replace those characters with others that are impossible to type (well, not really impossible, but very unlikely).

                If you are curious, numToChar(10) is the linefeed character which I replace with a standard RETURN. The most likely reason to encounter a LF is because the user copied and pasted the text from another source. Director doesn’t display LF’s in strings, so replacing it with a RETURN keeps the formatting looking like the original.
                • 5. Re: Creating a preferences window
                  wgb14 Level 1
                  Hi ApplieDCD

                  You script seems like a good place to start but i don't want my users to type anythign down, but rather only to select items from the preferences window. How can we adapt your code to do that?

                  Thanks
                  • 6. Re: Creating a preferences window
                    Level 7
                    Your basic tool is going to be BuddyAPI. If you haven't download it, do
                    so now. www.mods.com.au/budapi. It is free if you use up to 2
                    functions. In order to read and write to ini files, you'll use 2 so it
                    is free.

                    Set up your interface using radio buttons, checkboxes, or whatever you
                    want. When the user makes a selection, you write that to a file. Let's
                    say that you have a file named "prefs.ini" and it is in the same folder
                    as the Director movie.

                    For ease of demonstration, let's say there are 2 buttons. One will save
                    your preference for red shoes and the other will save your preference
                    for blue shoes.

                    --red
                    on mouseUp me
                    baWriteIni("prefs","shoes","red",the moviePath&"prefs.ini")
                    end

                    --blue
                    on mouseUp me
                    baWriteIni("prefs","shoes","blue",the moviePath&"prefs.ini")
                    end

                    Pushing that button will make or edit the prefs.ini file so it looks
                    like this:

                    [prefs]
                    shoes=red

                    (or blue)

                    When you want to read in the preference, use baReadIni. For example, in
                    a text box you could put this script (watch for line wrap):

                    on beginSprite me
                    shoeChoice=baReadIni("prefs","shoes","error",the moviePath&"prefs.ini")
                    sprite(me.spriteNum).member.text="You like"&&shoeChoice&&"shoes."
                    end

                    Consult the docs for BudAPI for more details on these (and other) functions.
                    • 7. Re: Creating a preferences window
                      wgb14 Level 1
                      Hi Mike

                      Your script is working very nice, but i would like to write both values under the same section (e.g., red and blue under prefs). Is there any way to do that?

                      Thanks
                      • 8. Re: Creating a preferences window
                        Level 7
                        on mouseUp me
                        baWriteIni("shoes","red","yes",the moviePath&"prefs.ini")
                        baWriteIni("shoes","blue","no",the moviePath&"prefs.ini")
                        end

                        This will make an ini file that looks like this:

                        [shoes]
                        red=yes
                        blue=no
                        • 9. Creating a preferences window
                          wgb14 Level 1
                          Hi,

                          Based on your feedback i finally created a script that works for my purposes. Here it is:

                          on mousedown

                          -----set variables

                          Topic1=baReadIni("Interested_In","Topic_1","error",the moviePath&"prefs.ini")
                          Topic2=baReadIni("Interested_In","Topic_2","error",the moviePath&"prefs.ini")
                          Topic3=baReadIni("Interested_In","Topic_3","error",the moviePath&"prefs.ini")

                          if Topic1 & Topic2 & Topic3 = "Architecture" & "History" & "Other" then
                          alert ("Play movie with everything")
                          else
                          If Topic1 & Topic2 = "Architecture" &"History" then
                          alert ("You play movie Architecture + History")
                          else
                          if Topic2 & Topic3 = "History" & "other" then
                          alert ("You play movie History and other")
                          else
                          if Topic1 & Topic3 = "Architecture" & "Other" then
                          alert ("Play movie architecture and other")
                          end if
                          end if
                          end if
                          end if
                          end

                          The mouse down is on a save preferences button. Now, although the aboe script works nicely, i can not do one rather simple thing..... the script works only with multiple selections (i.e., information from different topics). What about if my user selects just one topic (e.g., architecture). How do i write the code that will return him the appropriate movie. Also i would like to convert the above code to case statements.

                          Many thanks for your help so far to this task
                          • 10. Re: Creating a preferences window
                            Level 7
                            Here you go. Notice that the comparisons in the case statement begin
                            with the most specific case (all 3 variables) and go down to the most
                            general (just one). That is because the command will stop executing
                            when it comes across the 1st one that is true. If you had listed them
                            in reverse order, it would not work.

                            on mousedown

                            -----set variables

                            Topic1=baReadIni("Interested_In","Topic_1","error",the
                            moviePath&"prefs.ini")
                            Topic2=baReadIni("Interested_In","Topic_2","error",the
                            moviePath&"prefs.ini")
                            Topic3=baReadIni("Interested_In","Topic_3","error",the
                            moviePath&"prefs.ini")

                            -- if statements that play a movie according to what the user selects

                            If Language = "Greek" then
                            alert ("Languege is Greek")
                            else
                            If Language = "English" then
                            alert ("Language is English")
                            end if
                            end if

                            case true of
                            ((Topic1="Architecture") and (Topic2="History") and (Topic3="Other")):
                            alert "Play movie with everything"

                            ((Topic1="Architecture") and (Topic2="History")):
                            alert "You play movie Architecture + History"

                            ((Topic2="History") and (Topic3="Other")):
                            alert "You play movie History and other"

                            ((Topic1="Architecture") and (Topic3="Other")):
                            alert "Play movie architecture and other"

                            (Topic1="Architecture"):
                            alert "Play movie Architecture"

                            (Topic2="History"):
                            alert "Play movie History"

                            (Topic3="Other"):
                            alert "Play movie Other"

                            end case
                            end
                            end
                            • 11. Re: Creating a preferences window
                              wgb14 Level 1
                              Thank you it is working perfectly