Syntax Highlighing:
comments, key words, predefined symbols, class members & methods, functions & classes
# NEzoom.sml
# View ToolScript that provides zoom in on left mouse click
# with variable zoom capability depending on current scale.
# At scale > 1,000,000 zooms to county polygon.
# At scale > 25,000 zooms in 4X at click-point.
# At scale < 25,000 zooms to maximum detail (1X) in orthoimage layer.
# Author: Randy Smith, MicroImages, Inc.
# January 2006.
# Revised 9 June 2006:
# After zooming to a county, if initally click outside that county,
# tool zooms to the new county. However, this rule is inactivated once
# left-click/zoom within the current county.
# Revised 13 June 2006:
# Added messages to View status line to identify current left-click action,
# with Spanish and Turkish translations.
# The following symbols are predefined
# class GRE_VIEW View {use to access the view the tool script is attached to}
# class GRE_GROUP Group {use to access the group being viewed if the script is run from a group view}
# class GRE_LAYOUT Layout {use to access the layout being viewed if the script is run from a layout view}
# numeric ToolIsActive Will be 0 if tool is inactive or 1 if tool is active
#
# The following values are also predefined and are valid when the various On...()
# functions are called which deal with pointer and keyboard events.
# numeric PointerX Pointer X coordinate within view in pixels
# numeric PointerY Pointer Y coordinate within view in pixels
# numeric ShiftPressed 1 if <shift> key being pressed or 0 if not
# numeric CtrlPressed 1 if <ctrl> key being pressed or 0 if not
# numeric LeftButtonPressed 1 if left pointer button pressed or 0 if not
# numeric RightButtonPressed 1 if right pointer button pressed or 0 if not
# numeric MiddleButtonPressed 1 if middle pointer button pressed or 0 if not
#
########################################
### Global variable declarations
########################################
# group/layer/object global variables
class GRE_LAYER_VECTOR cntyLayer; # county vector layer
class GRE_LAYER_VECTOR cntyLayer; # county vector layer
class VECTOR cntyVector; # county vector object
class GRE_VECTOR_POLYS cntyPolys; # handle for polygon elements in county vector object
class TRANSPARM layerTransparm; ## coordinate transformation parameters from screen to vector layer
class TRANSPARM viewTransparm; ## coordinate transformation parameters from screen to view (group map coordinates)
numeric lastCnty; # polygon number of last county clicked in
numeric zoomedBelowCnty; # flag to indicate if have zoomed in after zooming to county
numeric mapscale; ### current map scale of View
string language$;
string msg1$, msg2$, msg3$; # zoom prompt messages for View message field
# The following script functions will be called (if used in the script) when
# the appropriate action or event occurs as described in the comments before each.
# To use a function, uncomment the lines containing the 'func' definition
# and ending brace '}' by removing the leftmost '#' on the line and add the
# function code between the two lines.
# Called the first time the tool is activated.
# If the tool implements a dialog it should be created (but not displayed) here.
proc OnInitialize ()
{
Group = Layout.FirstGroup;
## iterate through groups to find Nebraska Data group
while (Group.Name != "Nebraska Data")
Group = Group.NextGroup;
# get Counties layer from the group and polygons from the layer
cntyLayer = Group.GetLayerByName("Counties");
cntyPolys = cntyLayer.Poly;
# get the vector object from the layer
DispGetVectorFromLayer(cntyVector, cntyLayer);
# set prompt message strings
language$ = _context.Locale;
if (language$ == "enu")
{
msg1$ = "Left-click in a Nebraska County to zoom to it.";
msg2$ = "Left-click to zoom in to that point.";
msg3$ = "Left-click to recenter the view on that point.";
}
else if (language$ == "esn")
{
msg1$ = "Clic izquierdo en un Condado de Nebraska para hacer zoom en él.";
msg2$ = "Clic izquierdo para hacer zoom en ese punto.";
msg3$ = "Clic izquierdo para recentrar la vista en ese punto.";
}
else if (language$ == "trk")
{
msg1$ = "Tıklayarak bir County görüntüleyin.";
msg2$ = "Bu noktaya büyütmek için tıklayın.";
msg3$ = "Görüntüyü bu noktada ortalamak için tıklayın.";
}
} # end of OnInitialize
# Called when tool is to be destroyed, will not be called if tool was never activated.
# If the tool implements a dialog it should be destroyed here.
# proc OnDestroy () {
# } # end of OnDestroy
# Called when tool is activated.
# If the tool implements a dialog it should be "managed" (displayed) here.
# proc OnActivate () {
### get current map scale
# mapscale = View.GetMapScale();
# if (mapscale > 1000000) then
# View.SetMessage(msg1$);
# } # end of OnActivate
# Called when tool is deactivated (usually when switching to another tool).
# If the tool implements a dialog it should be "unmanaged" (hidden) here.
# proc OnDeactivate () {
# } # end of OnDeactivate
# Called when tool is to be 'suspended' during a redraw operation.
# proc OnSuspend () {
# } # end of OnSuspend
# Called when tool is to be 'resumed' after a redraw operation.
# If the tool displays any graphics they should be updated by this function.
# proc OnResume () {
# } # end of OnResume
# Called when user presses 'left' pointer/mouse button.
proc OnLeftButtonPress ()
{
local class POINT2d clickPt, clickPtTrans; ### coordinates for mouse-click position
local numeric cntynum; ### element number of county polygon clicked in
### get cursor position in screen coordinates
clickPt.x = PointerX;
clickPt.y = PointerY;
### get current map scale
mapscale = View.GetMapScale();
# get TRANSPARM for coordinate transformation from screen to
# layer coordinates for the county vector layer and convert
# click point location to layer coordinates
layerTransparm = View.GetTransLayerToScreen(cntyLayer, 1);
clickPtTrans = layerTransparm.ConvertPoint2DFwd(clickPt);
# find polygon enclosing the click point
cntynum = FindClosestPoly(cntyVector, clickPtTrans.x, clickPtTrans.y, GetLastUsedGeorefObject(cntyVector) );
if (mapscale > 1000000) # zoom to extents of county polygon clicked in
{
if (cntynum > 0)
{
# highlight the polygon so can zoom to it, but don't redraw yet
cntyPolys.HighlightSingle(cntynum, "Replace", 0);
# zoom to the highlighted polygon, redraw when unhighlight all elements
cntyLayer.ZoomToActiveElement();
cntyLayer.UnhighlightAllElements(1);
lastCnty = cntynum;
zoomedBelowCnty = 0;
View.SetMessage(msg2$);
}
}
else
{
if (cntynum > 0)
{
if (cntynum <> lastCnty && zoomedBelowCnty == 0) # if not same as last county and haven't zoomed below
{
# highlight the polygon so can zoom to it, but don't redraw yet
cntyPolys.HighlightSingle(cntynum, "Replace", 0);
# zoom to the highlighted polygon, redraw when unhighlight all elements
cntyLayer.ZoomToActiveElement();
cntyLayer.UnhighlightAllElements(1);
lastCnty = cntynum;
zoomedBelowCnty = 0;
View.SetMessage(msg2$);
}
else # click point is in same county; do variable zoom in based on scale
{
# get TRANSPARM for coordinate transformation from screen to view
# coordinates and convert click point to view coordinates
viewTransparm = View.GetTransViewToScreen(1); # inverse to get screen to view
clickPt = viewTransparm.ConvertPoint2DFwd(clickPt);
# reset view center to click point
View.Center = clickPt;
if (mapscale > 25000) # zoom in by 4X centered on click point
{
View.ZoomIn(4, 1);
zoomedBelowCnty = 1;
View.SetMessage(msg2$);
}
else # zoom to 1X centered on click point
{
View.Zoom1X(1);
View.SetMessage(msg3$);
}
}
}
}
} # end of OnLeftButtonPress
# Called when user presses 'right' pointer/mouse button.
# proc OnRightButtonPress () {
# } # end of OnRightButtonPress
# Called when user presses 'middle' pointer/mouse button.
# proc OnMiddleButtonPress () {
# } # end of OnMiddleButtonPress
# Called when user releases 'left' pointer/mouse button.
# proc OnLeftButtonRelease () {
# } # end of OnLeftButtonRelease
# Called when user releases 'right' pointer/mouse button.
# proc OnRightButtonRelease () {
# } # end of OnRightButtonRelease
# Called when user releases 'middle' pointer/mouse button.
# proc OnMiddleButtonRelease () {
# } # end of OnMiddleButtonRelease
# Called when user moves cursor if no button being pressed
# proc OnPointerMoveNoButton () {
# } # end of OnPointerMoveNoButton
# Called when user moves cursor while holding down button
# proc OnPointerMoveWithButton () {
# } # end of OnPointerMoveWithButton
# Called when cursor enters window associated with view.
proc OnEnterWindow () {
### get current map scale
mapscale = View.GetMapScale();
if (mapscale > 1000000) then
View.SetMessage(msg1$);
else if (mapscale > 3648) then
View.SetMessage(msg2$);
else
View.SetMessage(msg3$);
} # end of OnEnterWindow
# Called when cursor leaves window associated with view.
# proc OnLeaveWindow () {
# } # end of OnLeaveWindow
# Called when user presses 'key' on keyboard.
# proc OnKeyPress (key) {
# } # end of OnKeyPress