/*=========================================================================== * * File: MorrowMapRecord.JAVA * Author: Dave Humphrey (uesp@m0use.net) * Created On: Monday, August 27, 2001 * * Defines one Tamriel map location record. * *=========================================================================*/ /* Import Packages */ import java.awt.*; import java.io.*; import MorrowMapLocTypes; /*=========================================================================== * * Begin Class MorrowMapRecord * * Class for handling a single Tamriel map record object. * *=========================================================================*/ public class MorrowMapRecord { /*---------- Class Constants -----------------------------------------*/ /* Point shapes */ static final int TMPT_POINT = 0; static final int TMPT_CIRCLE = 1; static final int TMPT_SQUARE = 2; static final int TMPT_STAR = 3; static final int TMPT_TRIANGLE = 4; static final int TMPT_CROSS = 5; static final int TMPT_X = 6; /* Read data return values */ static final int TM_READ_OK = 0; static final int TM_READ_ERROR = -1; static final int TM_READ_END = 1; static final int TM_READ_BADVAR = 2; /* Types of locations */ static final int TMLT_POINT = 0; static final int TMLT_LINE = 1; static final int TMLT_AREA = 2; static final int TMLT_INVIS = 3; /*---------- End of Class Constants --------------------------------*/ /*---------- Class Data Members ------------------------------------*/ String Name; /* The name of the location */ int LocationType; /* Type of location (point, region, etc... */ int LocSubType; /* Sub-type of location */ int DisplayLevel; /* Controls at what zoom level the location will be visible */ boolean IsLandmark; /* For special case of provinces, etc... */ boolean m_MatchSearch; /* Used when searching for locations */ Color PenColor; /* The color of the outline pen */ Color FillColor; /* The fill color */ int PointSize; /* Size of point for point locations (pixels) */ int PointType; /* Shape of point for point locations */ Polygon LocationPoly; /* Holds the point data for the object */ /*---------- End of Class Data Members -----------------------------------*/ /*=========================================================================== * * Class MorrowMapRecord Constructor * *=========================================================================*/ MorrowMapRecord() { Name = ""; LocationType = TMLT_POINT; LocSubType = 0; DisplayLevel = 0; IsLandmark = false; m_MatchSearch = false; PenColor = Color.white; FillColor = Color.blue; PointSize = 2; PointType = TMPT_CIRCLE; /* Create the empty polygon object */ LocationPoly = new Polygon(); } /*========================================================================= * End of Class MorrowMapRecord Constructor *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Destructor - finalize (); * *=========================================================================*/ protected void finalize() { } /*=========================================================================== * End of Function void finalize() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - void DestroyContents (); * *=========================================================================*/ public void DestroyContents () { Name = ""; LocationType = TMLT_POINT; LocSubType = 0; DisplayLevel = 0; PenColor = Color.white; FillColor = Color.blue; PointSize = 2; PointType = TMPT_CIRCLE; LocationPoly.npoints = 0; m_MatchSearch = false; IsLandmark = false; } /*=========================================================================== * End of Class MorrowMapRecord Method DestroyContents() *=========================================================================*/ /*=========================================================================== * * Function - public void DebugOutput (); * * Outputs debug information to the system.out stream. * *=========================================================================*/ public void DebugOutput () { int Index; System.out.println("Debug output..."); System.out.println("\t Name: " + Name); System.out.println("\tLocationType: " + LocationType); System.out.println("\t LocSubType: " + LocSubType); System.out.println("\tDisplayLevel: " + DisplayLevel); System.out.println("\t IsLandmark: " + IsLandmark); System.out.println("\t SearchMatch: " + m_MatchSearch); System.out.println("\t PenColor: " + PenColor); System.out.println("\t FillColor: " + FillColor); System.out.println("\t PointSize: " + PointSize); System.out.println("\t PointType: " + PointType); System.out.println("\t PolyCount: " + LocationPoly.npoints); for (Index = 0; Index < LocationPoly.npoints; Index++) { System.out.println("\t PolyPoints: " + LocationPoly.xpoints[Index] + ", " + LocationPoly.ypoints[Index]); } } /*=========================================================================== * End of Function void DebugOutput() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - DrawPoint (graph, MapOptions); * * Draws a point object. * *=========================================================================*/ public void DrawPoint (Graphics graph, MorrowMapOptions MapOptions) { String Buffer; int X, Y; int TextX, TextY; int HalfPointSize = PointSize / 2; /* Set the output color and coordinates */ graph.setColor(PenColor); X = MapOptions.MapToPixelX(LocationPoly.xpoints[0]); Y = MapOptions.MapToPixelY(LocationPoly.ypoints[0]); TextY = Y; /* Draw the point according to the point type member */ switch (PointType) { case TMPT_POINT: graph.drawOval(X, Y, 0, 0); TextX = X + 2; break; case TMPT_CIRCLE: graph.drawOval(X-HalfPointSize, Y-HalfPointSize, PointSize-1, PointSize-1); TextX = X + HalfPointSize + 2; break; case TMPT_SQUARE: graph.drawRect(X-HalfPointSize, Y-HalfPointSize, PointSize, PointSize); TextX = X + HalfPointSize + 2; break; case TMPT_TRIANGLE: graph.drawLine(X-HalfPointSize, Y+HalfPointSize, X+HalfPointSize, Y+HalfPointSize); graph.drawLine(X+HalfPointSize, Y+HalfPointSize, X, Y-HalfPointSize); graph.drawLine(X, Y-HalfPointSize, X-HalfPointSize, Y+HalfPointSize); TextX = X + HalfPointSize + 2; break; case TMPT_CROSS: graph.drawLine(X-HalfPointSize, Y, X+HalfPointSize, Y); graph.drawLine(X, Y-HalfPointSize, X, Y+HalfPointSize); TextX = X + HalfPointSize + 2; break; case TMPT_STAR: graph.drawLine(X-HalfPointSize, Y, X+HalfPointSize, Y); graph.drawLine(X, Y-HalfPointSize, X, Y+HalfPointSize); graph.drawLine(X+HalfPointSize, Y-HalfPointSize, X-HalfPointSize, Y+HalfPointSize); graph.drawLine(X-HalfPointSize, Y-HalfPointSize, X+HalfPointSize, Y+HalfPointSize); TextX = X + HalfPointSize + 2; break; case TMPT_X: graph.drawLine(X+HalfPointSize, Y-HalfPointSize, X-HalfPointSize, Y+HalfPointSize); graph.drawLine(X-HalfPointSize, Y-HalfPointSize, X+HalfPointSize, Y+HalfPointSize); TextX = X + HalfPointSize + 2; break; default: graph.drawOval(X-HalfPointSize, Y-HalfPointSize, PointSize-1, PointSize-1); TextX = X + HalfPointSize + 2; break; } /* Output the point text if required */ if (MapOptions.m_DrawText) { /* Choose the appropiate font for display */ if (DisplayLevel <= 1) graph.setFont(MapOptions.m_HeavyFont); else if (DisplayLevel <= 3) graph.setFont(MapOptions.m_DefaultFont); else graph.setFont(MapOptions.m_SmallFont); /* Output the point text */ graph.drawString(Name, TextX, TextY); if (MapOptions.m_ShowPosition) { graph.setFont(MapOptions.m_SmallFont); TextY += graph.getFontMetrics().getHeight() * 2 / 3; graph.drawString("( " + LocationPoly.xpoints[0] + ", " + LocationPoly.ypoints[0] + " )", TextX, TextY); } } } /*=========================================================================== * End of Class MorrowMapRecord Method DrawPoint() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - DrawPolygon (graph, MapOptions); * * Draws a polygon object. * *=========================================================================*/ public void DrawPolygon (Graphics graph, MorrowMapOptions MapOptions) { Polygon TempPoly = new Polygon(LocationPoly.xpoints, LocationPoly.ypoints, LocationPoly.npoints); Rectangle PolyRect; int Index; int TextX; int TextY; /* Convert all points in the polygon to a temporary polygon */ for (Index = 0; Index < LocationPoly.npoints; Index++) { TempPoly.xpoints[Index] = MapOptions.MapToPixelX(TempPoly.xpoints[Index]); TempPoly.ypoints[Index] = MapOptions.MapToPixelY(TempPoly.ypoints[Index]); //System.out.println("\tPoint = " + TempPoly.xpoints[Index] + ", " + TempPoly.ypoints[Index]); } /* Draw outlined/filled polygon or polyline as required */ if (LocationType == TMLT_LINE) { graph.setColor(PenColor); graph.drawPolyline(TempPoly.xpoints, TempPoly.ypoints, TempPoly.npoints); } else if (LocationType == TMLT_AREA) { graph.setColor(FillColor); graph.fillPolygon(TempPoly); graph.setColor(PenColor); graph.drawPolygon(TempPoly); } /* Draw the text for the object if required */ if ((DisplayLevel <= MapOptions.m_DisplayLevel || m_MatchSearch) && MapOptions.m_DrawText) { /* Choose the appropiate font for display */ if (DisplayLevel == 0) graph.setFont(MapOptions.m_HeavyFont); else if (DisplayLevel <= 2) graph.setFont(MapOptions.m_DefaultFont); else graph.setFont(MapOptions.m_SmallFont); PolyRect = TempPoly.getBounds(); TextX = PolyRect.x + (PolyRect.width - graph.getFontMetrics().stringWidth(Name))/2; TextY = PolyRect.y + (PolyRect.height - graph.getFontMetrics().getHeight())/2 + graph.getFontMetrics().getHeight(); graph.setColor(PenColor); graph.drawString(Name, TextX, TextY); } } /*=========================================================================== * End of Class MorrowMapRecord Method DrawPolygon() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - public boolean IsPoint(); * * Determines if the location is a point or polygon type. * *=========================================================================*/ public boolean IsPoint () { if (LocationType == TMPT_POINT && LocationPoly.npoints >= 1) return (true); return (false); } /*=========================================================================== * End of Class MorrowMapRecord Method IsPoint() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - boolean IsVisible(MapOptions); * * Returns TRUE if the current location is a valid visible location: * - Checks for an empty location polygon object * - Checks for invisible type objects * - Checks the map options for visibility settings * *=========================================================================*/ public boolean IsVisible (MorrowMapOptions MapOptions) { /* Ignore if no points in location */ if (LocationPoly == null) return (false); /* Check map bounds */ /* Check point or rectangle for intersection */ if (LocationPoly.npoints <= 1) { if (!MapOptions.m_MapBox.contains(LocationPoly.xpoints[0], LocationPoly.ypoints[0])) return (false); } else { if (!MapOptions.m_MapBox.intersects(LocationPoly.getBounds())) return (false); } /* Check for the show all option */ if (MapOptions.m_ShowAll) return (true); if (MapOptions.m_ForceDisplayLevel >= DisplayLevel) return (true); /* Check for search options */ if (MapOptions.m_DoSearch) { String TempString = Name.toLowerCase(); if (TempString.indexOf(MapOptions.m_SearchString) >= 0) { m_MatchSearch = true; return (true); } if (!IsLandmark) return (false); } /* Special case for landmarks */ m_MatchSearch = false; if (IsLandmark) return (true); /* Check the display level */ if (DisplayLevel > MapOptions.m_DisplayLevel) return (false); return (true); } /*=========================================================================== * End of Function IsVisible() *=========================================================================*/ /*=========================================================================== * * Method - public int OutputImage (graph, MapOptions); * * Outputs the location to the given graphics object using the given map * options. * *=========================================================================*/ public int OutputImage (Graphics graph, MorrowMapOptions MapOptions) { //System.out.println ("OutputImage(), " + Name); /* Ignore if the location is invalid or invisible */ if (!IsVisible(MapOptions)) return(0); //System.out.println (" IsVisible"); /* Draw a point or polygon object */ if (IsPoint()) DrawPoint(graph, MapOptions); else DrawPolygon(graph, MapOptions); return (0); } /*=========================================================================== * End of Method OutputImage() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - Color ParseColor (String Buffer); * * Parses the given string into a valid color which is returned. If the * string does not represent a valid color, a default color is returned. * *=========================================================================*/ Color ParseColor (String Buffer) { int ColorIndex; ColorIndex = (Integer.valueOf(Buffer)).intValue(); return MorrowMapLocTypes.GetColor(ColorIndex); } /*=========================================================================== * End of Class MorrowMapRecord Method ParseColor() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - int ParseLocationType (String Buffer); * * Parses the given string into a location type. Returns 0 on success or * a negative value on any error. * *=========================================================================*/ int ParseLocationType (String Buffer) { Buffer.toLowerCase(); if (Buffer.equals("invis")) LocationType = TMLT_INVIS; else if (Buffer.equals("point")) LocationType = TMLT_POINT; else if (Buffer.equals("line")) LocationType = TMLT_LINE; else if (Buffer.equals("area")) LocationType = TMLT_AREA; else /* No valid match */ return (-1); return (0); } /*=========================================================================== * End of Class MorrowMapRecord Method ParseLocationType() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - int ParsePointData (String Buffer); * * Parses the given string into a point data. Returns 0 on success or * a negative value on any error. Assumes the format: * X1,Y1,X2,Y2, etc... * *=========================================================================*/ int ParsePointData (String Buffer) { int X; int Y; int CharIndex; int StartIndex = 0; String TempString; /* Loop until finished parsing string */ //System.out.println ("ParsePointData(" + Buffer + ")"); do { /* Parse out the X-coordinate, if any */ CharIndex = Buffer.indexOf(',', StartIndex); if (CharIndex < 0) return (0); X = (Integer.valueOf((Buffer.substring(StartIndex, CharIndex)).trim())).intValue(); /* Parse out the Y-coordinate */ StartIndex = CharIndex + 1; CharIndex = Buffer.indexOf(',', StartIndex); if (CharIndex < 0) Y = (Integer.valueOf((Buffer.substring(StartIndex)).trim())).intValue(); else Y = (Integer.valueOf((Buffer.substring(StartIndex, CharIndex)).trim())).intValue(); /* Add point to polygon list */ LocationPoly.addPoint(X, Y); StartIndex = CharIndex + 1; } while (CharIndex > 0); return (0); } /*=========================================================================== * End of Class MorrowMapRecord Method ParsePointData() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - int ParsePointType (String Buffer); * * Parses the given string into a point type. Returns 0 on success or * a negative value on any error. * *=========================================================================*/ int ParsePointType (String Buffer) { Buffer.toLowerCase(); if (Buffer.equals("circle")) PointType = TMPT_CIRCLE; else if (Buffer.equals("point")) PointType = TMPT_POINT; else if (Buffer.equals("square")) PointType = TMPT_SQUARE; else if (Buffer.equals("star")) PointType = TMPT_STAR; else if (Buffer.equals("cross")) PointType = TMPT_CROSS; else if (Buffer.equals("triangle")) PointType = TMPT_TRIANGLE; else if (Buffer.equals("x")) PointType = TMPT_X; else /* No valid match */ return (-1); return (0); } /*=========================================================================== * End of Class MorrowMapRecord Method ParsePointType() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - int ParseTextLine (InputLine); * * Parses the given text line into its appropiate data value. Returns * TM_READ_OK - on success * TM_READ_ERROR - on any error * TM_READ_END - end of map location section * *=========================================================================*/ public int ParseTextLine (String InputLine) { String Variable; String Value; int CharIndex; /* Remove trailing/leading whitespace */ InputLine = InputLine.trim(); /* Check for end of map location */ if (InputLine.equalsIgnoreCase("end")) return (TM_READ_END); /* Check for a variable/value '=' character */ CharIndex = InputLine.indexOf('='); if (CharIndex < 0) return (TM_READ_OK); /* Parse out the variable/value pair */ Variable = InputLine.substring(0, CharIndex-1); Value = InputLine.substring(CharIndex+1); Variable = Variable.trim(); Value = Value.trim(); /* Set the appropiate class member */ return SetMember(Variable, Value); } /*=========================================================================== * End of Function int ParseTextLine() *=========================================================================*/ /*=========================================================================== * * Class MorrowMapRecord Method - int SetMember (Variable, Value); * * Sets the appropiate class member based on the variable string. Returns * 0 on success and a negative value on any error. * *=========================================================================*/ public int SetMember (String Variable, String Value) { Variable.toLowerCase(); /* Object name */ if (Variable.equals("nm")) { Name = Value; //System.out.println("ParsedName = '" + Value + "'"); } /* Comment */ else if (Variable.equals("cm")) { } /* Location type */ else if (Variable.equals("lt")) { ParseLocationType(Value); } /* Pen color */ else if (Variable.equals("pc")) { PenColor = ParseColor(Value); } /* Fill color */ else if (Variable.equals("fc")) { LocSubType = (Integer.valueOf(Value)).intValue(); FillColor = ParseColor(Value); } /* Number of points */ else if (Variable.equals("np")) { } /* Point type */ else if (Variable.equals("pt")) { ParsePointType(Value); } /* Point size */ else if (Variable.equals("ps")) { PointSize = (Integer.valueOf(Value)).intValue(); if (PointSize < 0) PointSize = 0; if (PointSize > 20) PointSize = 20; } /* Display Level */ else if (Variable.equals("dl")) { DisplayLevel = (Integer.valueOf(Value)).intValue(); if (DisplayLevel < 0) DisplayLevel = 0; if (DisplayLevel > 10) DisplayLevel = 10; } /* Point data */ else if (Variable.equals("pd")) { ParsePointData(Value); } /* No match */ else return (TM_READ_BADVAR); /* Return success */ return (TM_READ_OK); } /*=========================================================================== * End of Class MorrowMapRecord Method SetMember() *=========================================================================*/ } /*=========================================================================== * End of Class MorrowMapRecord *=========================================================================*/ /*=========================================================================== * End of File MorrowMapRecord.JAVA *=========================================================================*/