mjessup
02-05-2009, 10:45 AM
In response to one of the help questions I have created a library for a simple (but I believe versatile) set of classes for creating image maps / adding hotspots to a view.
The attached zip contains the library plus two examples. The files are:
1. imagemap.lzx: The library classes
2. DrawView.lzx: A simple example that creates some hotspots on a view and draws them using drawview
3. DrawView.xml: The shape data used by DrawView.lzx
4. Map.lzx: An image map example, that creates hotspots on a loaded image resource.
5. Map.xml: The hotspot data for Map.lzx
6. northeast.jpg: The image resource used by Map.lzx
7. MapZoom.llzx: An Image example with a clickable hotspots that will display a subview. Illustrates suspend and resume.
I have created/tested the examples using LPS4.1.1 in both swf8 and dhtml, and have not encountered any problems.
About The Library
The library contains four classes. The first is "hotspot". This class is a general interface contract class for use by the library. It specifies any hotspot class must define three methods:
1. contains(x, y): true if the hotspot contains the given x, y coordinate, false otherwise
2. doActivate(): called when a hotspot is activated.
3. doDeactivate: called when a hotspot is deactivated.
The next two classes are implementations of Hotspot. The first "polygonhotspot" implements an N-sided polygon. It contains four additional methods in addition to those defined by hotspot:
1. addPoint(x, y): Adds a new point to the path that describes the polygon
2. clearPoints(): Clears all points in the polygon
3. getNumPoints(): Returns the number of points describing the polygon
4. getPoint(n): Gets an object with an 'x' and 'y' attribute for the Nth point in the polygon (or null if n is out of range)
This class is used by first constructing the object and then making consecutive calls to addPoint for each point in the polygon.
The second implementation of Hotspot is "circlehotspot". This describe a simple circle with three numeric attributes:
1. x: The x coordinate of the center of the circle
2. y: The y coordinate of the center of the circle
3. radius: The raidus of the circle
The final class is "imagemap". This is the image map itself. It will attach itself to the parent view in which it is created, and then activate or deactivate its hotspots as the mouse passes in or out of them. The public members of the class are:
1. exclusive: A boolean attribute. When true only one hotspot controlled by the map may be active at any given time. If false multiple hotspots may be active at once (if hotspots overlap).
2. addHotspot(spot): Adds the given spot (implementation of "Hotspot") to the list of those controlled by the map.
3. clearHotspots(): Removes all hotspots from the map.
4. suspend(): Suspend activity by the map. The activation state of the hotspots will not change until resume is called.
5. resume(): Resume activity by the map. Checking will resume and all spot states will be updated.
The Example Programs
The first example "DrawView.lzx" will load a set of hotspots and then draw them. This example illustrates what a hotspot looks like in terms of its bounds and can also be used to view how chnaging the exclusive attribute of ImageMap effects the behavior as some of the hotspots overlap.
The second example "Map.lzx" is an example of creating something along the lines of a traditional HTML image map. This uses a picture (of the NE U.S.) as a resource and then creates hot areas on it (one for each state). As the mouse moves over each state it will display the states name (note for the sake of time the hotspot boundaries are a little crude).
The final example "MapZoom.lzx" is an extension of the second. In addition to its behavior when you click on a hotspot a subview will appear and map detection will be suspended. When the subview is closed the map detection is resumed.
Both of these examples also have further subclasses of those defined in the library showing how doActivate and doDeactivate can be overridden. They also contain code illustrating loading hotspot data from a dataset.
Two final notes about using the library. First as can be see in the example code, the view with the hotspots must have clickable="true". In addition if you create a subview over the hostspot view with clickable regions it may interfere with the detection of mousein/mouseout events, therefore if you want something clickable to appear I suggest either you suspend the map until the subview is destroyed, or have the subview "stay out of the way" of the mouse.
I have tested the examples using LPS4.1.1 and LPS4.2.0. They all seem to work, except DrawView.lzx in LPS4.2.0 compiled as DHTML (though I think this may be a problem with drawview in DHTML), everything works in SWF8/9 and the other examples work in DHTML.
You are free to use this code for any purpose, though if you create a subclass of Hotspot for another shape it would be appreciated if you share the code. I would also be interested to hear any comments about this library (is it useful? have you found bugs?).
The attached zip contains the library plus two examples. The files are:
1. imagemap.lzx: The library classes
2. DrawView.lzx: A simple example that creates some hotspots on a view and draws them using drawview
3. DrawView.xml: The shape data used by DrawView.lzx
4. Map.lzx: An image map example, that creates hotspots on a loaded image resource.
5. Map.xml: The hotspot data for Map.lzx
6. northeast.jpg: The image resource used by Map.lzx
7. MapZoom.llzx: An Image example with a clickable hotspots that will display a subview. Illustrates suspend and resume.
I have created/tested the examples using LPS4.1.1 in both swf8 and dhtml, and have not encountered any problems.
About The Library
The library contains four classes. The first is "hotspot". This class is a general interface contract class for use by the library. It specifies any hotspot class must define three methods:
1. contains(x, y): true if the hotspot contains the given x, y coordinate, false otherwise
2. doActivate(): called when a hotspot is activated.
3. doDeactivate: called when a hotspot is deactivated.
The next two classes are implementations of Hotspot. The first "polygonhotspot" implements an N-sided polygon. It contains four additional methods in addition to those defined by hotspot:
1. addPoint(x, y): Adds a new point to the path that describes the polygon
2. clearPoints(): Clears all points in the polygon
3. getNumPoints(): Returns the number of points describing the polygon
4. getPoint(n): Gets an object with an 'x' and 'y' attribute for the Nth point in the polygon (or null if n is out of range)
This class is used by first constructing the object and then making consecutive calls to addPoint for each point in the polygon.
The second implementation of Hotspot is "circlehotspot". This describe a simple circle with three numeric attributes:
1. x: The x coordinate of the center of the circle
2. y: The y coordinate of the center of the circle
3. radius: The raidus of the circle
The final class is "imagemap". This is the image map itself. It will attach itself to the parent view in which it is created, and then activate or deactivate its hotspots as the mouse passes in or out of them. The public members of the class are:
1. exclusive: A boolean attribute. When true only one hotspot controlled by the map may be active at any given time. If false multiple hotspots may be active at once (if hotspots overlap).
2. addHotspot(spot): Adds the given spot (implementation of "Hotspot") to the list of those controlled by the map.
3. clearHotspots(): Removes all hotspots from the map.
4. suspend(): Suspend activity by the map. The activation state of the hotspots will not change until resume is called.
5. resume(): Resume activity by the map. Checking will resume and all spot states will be updated.
The Example Programs
The first example "DrawView.lzx" will load a set of hotspots and then draw them. This example illustrates what a hotspot looks like in terms of its bounds and can also be used to view how chnaging the exclusive attribute of ImageMap effects the behavior as some of the hotspots overlap.
The second example "Map.lzx" is an example of creating something along the lines of a traditional HTML image map. This uses a picture (of the NE U.S.) as a resource and then creates hot areas on it (one for each state). As the mouse moves over each state it will display the states name (note for the sake of time the hotspot boundaries are a little crude).
The final example "MapZoom.lzx" is an extension of the second. In addition to its behavior when you click on a hotspot a subview will appear and map detection will be suspended. When the subview is closed the map detection is resumed.
Both of these examples also have further subclasses of those defined in the library showing how doActivate and doDeactivate can be overridden. They also contain code illustrating loading hotspot data from a dataset.
Two final notes about using the library. First as can be see in the example code, the view with the hotspots must have clickable="true". In addition if you create a subview over the hostspot view with clickable regions it may interfere with the detection of mousein/mouseout events, therefore if you want something clickable to appear I suggest either you suspend the map until the subview is destroyed, or have the subview "stay out of the way" of the mouse.
I have tested the examples using LPS4.1.1 and LPS4.2.0. They all seem to work, except DrawView.lzx in LPS4.2.0 compiled as DHTML (though I think this may be a problem with drawview in DHTML), everything works in SWF8/9 and the other examples work in DHTML.
You are free to use this code for any purpose, though if you create a subclass of Hotspot for another shape it would be appreciated if you share the code. I would also be interested to hear any comments about this library (is it useful? have you found bugs?).