Source for file Object.php
Documentation is available at Object.php
* Object primitives used to draw the view
* SMap requires at least PHP version 5, but there are important bug fixes in
* more recent versions of PHP. Try to stay current.
* Copyright (c) 2006-2007, Seth Price <{@link mailto:seth@pricepages.org}
* seth@pricepages.org}> All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @author Seth Price <seth@pricepages.org>
* @copyright Seth Price, 2006-2007
* An Object that can appear in a layer
* The Object is the basic unit of drawing. {@link SMap_Layer Layers} generate
* these Objects and pass them back to the {@link SMap_Tile tile} when
* {@link SMap_Layer::getMapObjs()} or {@link SMap_Layer::getImgObjs()} is
* called. At this point each tile has an ordered array of Objects.
* The Objects are then organized by calling {@link SMap_Object::applyTile()} to
* pass a {@link SMap_Tile} to each Object. The intent is for each Object crop
* each and define the bounds which will be clipping the object. At this point,
* the Object has the option of returning false, and it will not be drawn.
* At this point, each Object should have all of the information required to be
* drawn. So {@link SMap_Object::draw()} is called on each Object, and a
* {@link SMap_Canvas canvas} is passed. The Canvas should be fairly efficent at
* rasterizing only when needed.
* Keep in mind that some of these steps are skipped when using cached images.
* For more information on the caching process, see
* {@link SMap_Tile::flatten()}.
* In order to work correctly, {@link SMap_Object_Area_Label} has some hooks in
* interesting places also. Only developers should need to worry about that,
* The layer that this object resides in
* The view that this object resides in
* The map that this object resides in
* Does this object link to anything?
* Are we labeling this object?
* The tile that has been applied to this object
* Give the object a context
* @param object The view that contains this object
* @param object The layer that produced this object
public function __construct(SMap_View $view, SMap_Layer $layer){
$this->map = $layer->getMap();
* Set the color for this object
* This is a generic color setting method, and means different things to
* different subclasses. If the first parameter is false, then the color is
* @param mixed Red channel or false (for no color)
* @param integer Green channel
* @param integer Blue channel
* @param integer Alpha channel (127 == clear)
function setColor($r, $g = 0, $b = 0, $a = null){
$this->color = array($r, $g, $b, $a);
* Set this to link to something
* @param string The URL's description
public function setLink($url, $alt){
* Get the rough inner rectangular bounds for this object.
* @return array A bounding the array
* Are we drawing a label for this object?
* @param object Apply a label to this object
abstract function label(SMap_Object_Area_Label $label);
* Apply a tile to this object
* Do whatever precalculation is needed to display this object in a given
* tile. Return if this object is visible.
* @return boolean Is visible?
abstract function applyTile(SMap_Tile $tile);
* Draw this object on a canvas
abstract function draw(SMap_Canvas $canvas);
* Return a map of this object
* If we are returning a client side image map, returned set of arrays
* represent the "area" of the image map. They have indexes at ['alt'],
* ['coords'], ['shape'], and ['href'].
* @return mixed An array of arrays of coords, or false.
* @see SMap_Tile::getImageMap()
* @see SMap_Layer::getTileMap()
protected $color = array(255,0,0);
* Set the location of the point
* @param object The view that this object is attached to
function __construct(SMap_View $view, SMap_Layer $layer, $x, $y){
* Return the bounds of the point
* Expands the point by a pixel in each direction
* @return array Bounding array
$scale = $this->view->scale;
//Expand by a bit so we don't overlap
return array( SMap::MAXX => $this->x + $scale[0],
SMap::MAXY => $this->y + $scale[1],
SMap::MINX => $this->x - $scale[0],
SMap::MINY => $this->y - $scale[1] );
public function label(SMap_Object_Area_Label $label){
* Apply a tile to this point
return ($B[SMap::MAXY] >= $this->y &&
$B[SMap::MAXX] > $this->x &&
$B[SMap::MINY] < $this->y &&
$B[SMap::MINX] <= $this->x );
* Set a pixel on the canvas
* Alone, it won't be noticable, but if you draw many, you might have
public function draw(SMap_Canvas $canvas){
$B = $this->tile->bounds;
$col = $canvas->allocColor($this->color);
$canvas->setPixel($ix, $iy, $col);
* A pixel gives nothing to work with
* So, an empty array is returned.
* A line between two points
* The line can include a grid line, part of a path, or a boundary line.
* Beginning and end of the line
* Lock this when we start retrieving data from it
protected $color = array(0,0,0,null);
* Construct with the beginning and end of the line
function __construct( SMap_View $view, SMap_Layer $layer,
$begX, $begY, $endX, $endY){
$this->points = array($begX, $begY, $endX, $endY);
* @param float Next X coord
* @param float Next Y coord
public function extend($x, $y){
* @return array A reasonable inner bounds
for($i = 2; isset ($this->points[$i+ 1]); $i += 2){
* Place the label on the line
public function label(SMap_Object_Area_Label $label){
//FIXME doesn't test visibilty
public function draw(SMap_Canvas $canvas){
//This function has been optimized. Blech.
$B = $this->tile->bounds;
$invScaleX = 1/ $this->view->scale[0];
$invScaleY = 1/ $this->view->scale[1];
$col = $canvas->allocColor($this->color);
//Find pt location, +0.5 because we are fake-rounding
$endX = ($pts[0] - $Bminx)* $invScaleX+ 0.5;
$endY = ($Bmaxy - $pts[1])* $invScaleY+ 0.5;
while(isset ($pts[++ $i])){
$endX = ($pts[ $i] - $Bminx)* $invScaleX+ 0.5;
$endY = ($Bmaxy - $pts[++ $i])* $invScaleY+ 0.5;
"\tColor: ". $this->color[0]. ' '. $this->color[1]. ' '. $this->color[2]. "<br />\n".
"\tEndpoints: (". $begX. ", ". $begY. ") (". $endX. ", ". $endY. ")<br />\n".
echo "\t</blockquote>\n";
$canvas->line($begX,$begY, $endX,$endY, $col);
* A path that connects a series of points
* It would be nice to label this with distance markers.
private $leftArea = null;
private $rightArea= null;
public function draw(SMap_Canvas $canvas){
* A boundary seperates two areas
private $leftArea = null;
private $rightArea= null;
* Sets the left and right areas that this boundary seperates
* Returns the area that is not the passed area
public function label(SMap_Object_Area_Label $label){
public function draw(SMap_Canvas $canvas){
* An area on the map encompassing one or more pixels
* An area is interesting because at this point it is linkable/clickable.
* Save the colors for this area
* Set the border color for this image
* If the first parameter is false, then there is no border. (default)
* @param mixed Red channel or false (for no color)
* @param integer Green channel
* @param integer Blue channel
* @param integer Alpha channel
* Sets the default click for the tile
|