From 3db38eb474c289ebee7397072bed9f15f110c279 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 19 Apr 2017 16:12:12 -0400 Subject: [PATCH] limit points to the limits of the monitor --- web/includes/functions.php | 19 ++++++++++++ web/skins/classic/views/zone.php | 48 ++++++++++++------------------- web/skins/classic/views/zones.php | 26 ++++++++++------- 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/web/includes/functions.php b/web/includes/functions.php index 7493b9649..ccf87e8e2 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -1755,6 +1755,25 @@ function coordsToPoints( $coords ) { return( $points ); } +function limitPoints( &$points, $min_x, $min_y, $max_x, $max_y ) { + foreach ( $points as &$point ) { + if ( $point['x'] < $min_x ) { + Debug('Limiting point x'.$point['x'].' to min_x ' . $min_x ); + $point['x'] = $min_x; + } else if ( $point['x'] > $max_x ) { + Debug('Limiting point x'.$point['x'].' to max_x ' . $max_x ); + $point['x'] = $max_x; + } + if ( $point['y'] < $min_y ) { + Debug('Limiting point y'.$point['y'].' to min_y ' . $min_y ); + $point['y'] = $min_y; + } else if ( $point['y'] > $max_y ) { + Debug('Limiting point y'.$point['y'].' to max_y ' . $max_y ); + $point['y'] = $max_y; + } + } // end foreach point +} // end function limitPoints( $points, $min_x, $min_y, $max_x, $max_y ) + function getLanguages() { $langs = array(); foreach ( glob("lang/*_*.php") as $file ) { diff --git a/web/skins/classic/views/zone.php b/web/skins/classic/views/zone.php index 1591564c7..ba0cb7328 100644 --- a/web/skins/classic/views/zone.php +++ b/web/skins/classic/views/zone.php @@ -18,9 +18,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // -if ( !canView( 'Monitors' ) ) -{ - $view = "error"; +if ( !canView( 'Monitors' ) ) { + $view = 'error'; return; } @@ -34,29 +33,25 @@ $hicolor = '0x00ff00'; // Green $presets = array(); $presetNames = array(); $presetNames[0] = translate('ChoosePreset'); -$sql = "select *, Units-1 as UnitsIndex, CheckMethod-1 as CheckMethodIndex from ZonePresets order by Id asc"; -foreach( dbFetchAll( $sql ) as $preset ) -{ - $presetNames[$preset['Id']] = $preset['Name']; - $presets[] = $preset; +$sql = 'SELECT *, Units-1 AS UnitsIndex, CheckMethod-1 AS CheckMethodIndex FROM ZonePresets ORDER BY Id ASC'; +foreach( dbFetchAll( $sql ) as $preset ) { + $presetNames[$preset['Id']] = $preset['Name']; + $presets[] = $preset; } $optTypes = array(); -foreach ( getEnumValues( 'Zones', 'Type' ) as $optType ) -{ - $optTypes[$optType] = $optType; +foreach ( getEnumValues( 'Zones', 'Type' ) as $optType ) { + $optTypes[$optType] = $optType; } $optUnits = array(); -foreach ( getEnumValues( 'Zones', 'Units' ) as $optUnit ) -{ - $optUnits[$optUnit] = $optUnit; +foreach ( getEnumValues( 'Zones', 'Units' ) as $optUnit ) { + $optUnits[$optUnit] = $optUnit; } $optCheckMethods = array(); -foreach ( getEnumValues( 'Zones', 'CheckMethod' ) as $optCheckMethod ) -{ - $optCheckMethods[$optCheckMethod] = $optCheckMethod; +foreach ( getEnumValues( 'Zones', 'CheckMethod' ) as $optCheckMethod ) { + $optCheckMethods[$optCheckMethod] = $optCheckMethod; } $monitor = new Monitor( $mid ); @@ -66,14 +61,10 @@ $maxX = $monitor->Width()-1; $minY = 0; $maxY = $monitor->Height()-1; -if ( !isset($newZone) ) -{ - if ( $zid > 0 ) - { +if ( !isset($newZone) ) { + if ( $zid > 0 ) { $zone = dbFetchOne( 'SELECT * FROM Zones WHERE MonitorId = ? AND Id=?', NULL, array( $monitor->Id(), $zid ) ); - } - else - { + } else { $zone = array( 'Id' => 0, 'Name' => translate('New'), @@ -104,17 +95,16 @@ if ( !isset($newZone) ) $zone['AreaCoords'] = preg_replace( '/\s+/', ',', $zone['Coords'] ); $newZone = $zone; -} +} # end if new Zone -//if ( !$points ) -//{ - //$points = $zone['Points']; -//} +# Ensure Zone fits within the limits of the Monitor +limitPoints( $newZone['Points'], $minX, $minY, $maxX, $maxY ); ksort( $newZone['Points'], SORT_NUMERIC ); $newZone['Coords'] = pointsToCoords( $newZone['Points'] ); $newZone['Area'] = getPolyArea( $newZone['Points'] ); +$newZone['AreaCoords'] = preg_replace( '/\s+/', ',', $newZone['Coords'] ); $selfIntersecting = isSelfIntersecting( $newZone['Points'] ); $focusWindow = true; diff --git a/web/skins/classic/views/zones.php b/web/skins/classic/views/zones.php index c00beba3f..c153d69c2 100644 --- a/web/skins/classic/views/zones.php +++ b/web/skins/classic/views/zones.php @@ -18,23 +18,27 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // -if ( !canView( 'Monitors' ) ) -{ - $view = "error"; - return; +if ( !canView( 'Monitors' ) ) { + $view = 'error'; + return; } $mid = validInt($_REQUEST['mid']); $monitor = new Monitor( $mid ); +# Width() and Height() are already rotated + $minX = 0; + $maxX = $monitor->Width()-1; + $minY = 0; + $maxY = $monitor->Height()-1; $zones = array(); -foreach( dbFetchAll( 'select * from Zones where MonitorId = ? order by Area desc', NULL, array($mid) ) as $row ) -{ - if ( $row['Points'] = coordsToPoints( $row['Coords'] ) ) - { - $row['AreaCoords'] = preg_replace( '/\s+/', ',', $row['Coords'] ); - $zones[] = $row; - } +foreach( dbFetchAll( 'SELECT * FROM Zones WHERE MonitorId=? ORDER BY Area DESC', NULL, array($mid) ) as $row ) { + $row['Points'] = coordsToPoints( $row['Coords'] ); + + limitPoints( $row['Points'], $minX, $minY, $maxX, $maxY ); + $row['Coords'] = pointsToCoords( $row['Points'] ); + $row['AreaCoords'] = preg_replace( '/\s+/', ',', $row['Coords'] ); + $zones[] = $row; } $connkey = generateConnKey();