I have a number of thumbnail images and I need to be able to browse through them by name and show the picture. I also need to allow the user to look for a specific image by name or part of a name. How can I set this up?
There are a number of ways to accomplish this, but since you are searching by name it isn't too difficult. Director has all the data we need, we just need to get it into an easily accessible format so that we can sort it quickly. Lets start with the list. We want to have quick and easy access to the data so a simple list is ideal, but to show our search results as well as the master list, we will need a text or field member. In this case I am going to use a field member, as Director can redraw them much faster than text members. The parameters we want will be stored and passed to this cast member and it will be our controller for the whole process.
With the base layout decided we can put all of the functionality into this single script, then use a sendSprite command to access the different handlers. In the beginSprite handler we can set up 2 lists. The first is a master list of all images from the casts we have specified. We can reference back to this list every time we want to generate the results from a search, etc. The list is the current list of images. At the beginning they are identical, but we have to remember to make it a duplicate of the first list and not set it equal. If one variable is set equal to a list and the 2nd changes its value, the first will change as well. Using the duplicate command we make a copy and avoid this problem. Once the list is generated we sort it to get an alphabetized version. Now we are ready to start browsing.
One of the properties we set at the beginning is the currentItem. This way we can track where we are in our currentList and easily browse with the forward and back button. For a search we simply run a repeat loop that goes through the imageList (master list) and adds any item that has the searched text within it. I have also added features to "hilite" the current line and keep it "hilited" if the new search contains the same item. Below is the code...
on getPropertyDescriptionList me
if getOne([#field, #text], sprite(the currentSpriteNum).member.type) = 0 and the currentSpriteNum > 0 then
alert "This is for text and field members only!"
exit
end if
p_list = [:]
p_list.addProp(#imageSprite, [#format : #integer, #comment : "Which sprite is the thumbnail image?", #default : 1])
p_list.addProp(#whichCasts, [#format : #string, #comment : "Which casts to search for thumbnails?(Separate with commas and no spaces.)", #default : "Internal"])
return p_list
end
on beginSprite me
imageList = []
--Get the list of casts
the itemDelimiter = ","
repeat with x = 1 to whichCasts.item.count
theCast = whichCasts.item[x]
--Get the list of graphics
repeat with y = 1 to the number of members of castlib theCast
if member(y, theCast).type = #bitmap then
imageList.add(member(y,theCast).name)
end if
end repeat
end repeat
imageList.sort()
--create the currentList
currentList = duplicate(imageList)
newText()
end
on mouseUp me
currentItem = the mouseLine
theImage = sprite(spriteNum).member.line[the mouseLine]
sprite(spriteNum).member.line[the mouseLine].hilite()
sprite(imageSprite).member = member(theImage)
end
on buildList me, whatString
currentList = []
repeat with x = 1 to imageList.count
if imageList[x] contains whatString then
currentList.add(imageList[x])
end if
end repeat
newText()
end
on newText me
theString = ""
repeat with x = 1 to currentList.count
theString = theString & currentList[x]
if x < currentList.count then theString = theString & return
end repeat
sprite(spriteNum).member.text = theString
currentItem = currentList.getOne(sprite(imageSprite).member.name)
if currentItem > 0 then
sprite(spriteNum).member.line[currentItem].hilite()
else if currentList.count = 0 then
sprite(imageSprite).member = 0
else
sprite(imageSprite).member = member(currentList[1])
sprite(spriteNum).member.line[1].hilite()
currentItem = 1
end if
end
on advance me, howFar
if currentList.count > 0 then
newItem = currentItem + howFar
if newItem < 1 then
repeat while newItem < 1
newItem = newItem + currentList.count
end repeat
end if
if newItem > currentList.count then
repeat while newItem > currentList.count
newItem = newItem - currentList.count
end repeat
end if
currentItem = newItem
sprite(imageSprite).member = member(currentList[currentItem])
sprite(spriteNum).member.line[currentItem].hilite()
end if
end
on getBehaviorDescription me
return "This behavior sets up a field or text member as a browser for images in a cast. Drop it on the field member and tell it which sprite contains the thumbnail and a list of all casts to use. To do a search send the sprite a buildList statement (sendSprite(x, #buildList, " & quote & "searchword" & quote & ")) and to auto advance use the advance command with the number of frames to move (sendSprite(x, #advance, -1))"
end
That's all there is to it. Here is an example of the code in action. (Exhibit 1)
So what now? You could add the ability to search keywords, or how about export or copying larger versions of the file when you click on it. Play with it, and happy coding.
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA