📄 geographictextrenderer.java
字号:
/* Copyright (C) 2001, 2006 United States Government as represented bythe Administrator of the National Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.render;import com.sun.opengl.util.j2d.TextRenderer;import gov.nasa.worldwind.exception.WWRuntimeException;import gov.nasa.worldwind.geom.*;import gov.nasa.worldwind.terrain.SectorGeometryList;import gov.nasa.worldwind.util.Logging;import javax.media.opengl.GL;import javax.media.opengl.glu.GLU;import java.awt.*;import java.awt.geom.*;import java.io.IOException;import java.util.*;/** * @author dcollins * @version $Id: GeographicTextRenderer.java 10925 2009-05-06 22:22:50Z dcollins $ */public class GeographicTextRenderer{ private TextRenderer lastTextRenderer = null; private final GLU glu = new GLU(); private static final Font DEFAULT_FONT = Font.decode("Arial-PLAIN-12"); private static final Color DEFAULT_COLOR = Color.white; private boolean cullText = false; private boolean hasJOGLv111Bug = false; public GeographicTextRenderer() { } public boolean isCullTextEnabled() { return cullText; } public void setCullTextEnabled(boolean cullText) { this.cullText = cullText; } public void render(DrawContext dc, Iterable<GeographicText> text) { this.drawMany(dc, text); } public void render(DrawContext dc, GeographicText text, Vec4 textPoint) { if (!isTextValid(text, false)) return; this.drawOne(dc, text, textPoint); } private void drawMany(DrawContext dc, Iterable<GeographicText> textIterable) { if (dc == null) { String msg = Logging.getMessage("nullValue.DrawContextIsNull"); Logging.logger().fine(msg); throw new IllegalArgumentException(msg); } if (textIterable == null) { String msg = Logging.getMessage("nullValue.IterableIsNull"); Logging.logger().fine(msg); throw new IllegalArgumentException(msg); } if (dc.getVisibleSector() == null) return; SectorGeometryList geos = dc.getSurfaceGeometry(); if (geos == null) return; Iterator<GeographicText> iterator = textIterable.iterator(); if (!iterator.hasNext()) return; Frustum frustumInModelCoords = dc.getView().getFrustumInModelCoordinates(); double horizon = dc.getView().computeHorizonDistance(); while (iterator.hasNext()) { GeographicText text = iterator.next(); if (!isTextValid(text, true)) continue; if (!text.isVisible()) continue; Angle lat = text.getPosition().getLatitude(); Angle lon = text.getPosition().getLongitude(); if (!dc.getVisibleSector().contains(lat, lon)) continue; Vec4 textPoint = geos.getSurfacePoint(lat, lon, text.getPosition().getElevation()); if (textPoint == null) continue; double eyeDistance = dc.getView().getEyePoint().distanceTo3(textPoint); if (eyeDistance > horizon) continue; if (!frustumInModelCoords.contains(textPoint)) continue; dc.addOrderedRenderable(new OrderedText(text, textPoint, eyeDistance)); } } private void drawOne(DrawContext dc, GeographicText text, Vec4 textPoint) { if (dc == null) { String msg = Logging.getMessage("nullValue.DrawContextIsNull"); Logging.logger().fine(msg); throw new IllegalArgumentException(msg); } if (dc.getView() == null) { String msg = Logging.getMessage("nullValue.ViewIsNull"); Logging.logger().fine(msg); throw new IllegalArgumentException(msg); } if (dc.getVisibleSector() == null) return; SectorGeometryList geos = dc.getSurfaceGeometry(); if (geos == null) return; if (!text.isVisible()) return; if (textPoint == null) { if (text.getPosition() == null) return; Angle lat = text.getPosition().getLatitude(); Angle lon = text.getPosition().getLongitude(); if (!dc.getVisibleSector().contains(lat, lon)) return; textPoint = geos.getSurfacePoint(lat, lon, text.getPosition().getElevation()); if (textPoint == null) return; } double horizon = dc.getView().computeHorizonDistance(); double eyeDistance = dc.getView().getEyePoint().distanceTo3(textPoint); if (eyeDistance > horizon) return; if (!dc.getView().getFrustumInModelCoordinates().contains(textPoint)) return; dc.addOrderedRenderable(new OrderedText(text, textPoint, eyeDistance)); } private static boolean isTextValid(GeographicText text, boolean checkPosition) { if (text == null || text.getText() == null) return false; //noinspection RedundantIfStatement if (checkPosition && text.getPosition() == null) return false; return true; } private class OrderedText implements OrderedRenderable, Comparable<OrderedText> { GeographicText text; Vec4 point; double eyeDistance; OrderedText(GeographicText text, Vec4 point, double eyeDistance) { this.text = text; this.point = point; this.eyeDistance = eyeDistance; } public int compareTo(OrderedText t) { if ( t.text.getPriority() - this.text.getPriority()== 0) { return (int)( this.eyeDistance - t.eyeDistance); }else return (int)(t.text.getPriority() - this.text.getPriority()); } public double getDistanceFromEye() { return this.eyeDistance; } private GeographicTextRenderer getRenderer() { return GeographicTextRenderer.this; } public void render(DrawContext dc) { GeographicTextRenderer.this.beginRendering(dc); try { if (cullText) { ArrayList<OrderedText> textList = new ArrayList<OrderedText>(); textList.add(this); // Draw as many as we can in a batch to save ogl state switching. Object nextItem = dc.getOrderedRenderables().peek(); while (nextItem != null && nextItem instanceof OrderedText) { OrderedText ot = (OrderedText) nextItem; if (ot.getRenderer() != GeographicTextRenderer.this) return; textList.add(ot); dc.getOrderedRenderables().poll(); // take it off the queue nextItem = dc.getOrderedRenderables().peek(); } Collections.sort(textList); //sort for rendering priority ArrayList<Rectangle2D> textBounds = new ArrayList<Rectangle2D>(); for (OrderedText ot:textList ) { Rectangle2D newBounds = GeographicTextRenderer.this.computeTextBounds(dc, ot); if (newBounds == null) continue; boolean overlap = false; for (Rectangle2D rect: textBounds) { if (rect.intersects(newBounds)) overlap=true; } if (!overlap) { textBounds.add(newBounds); GeographicTextRenderer.this.drawText(dc, ot); } } } else //just draw each label { GeographicTextRenderer.this.drawText(dc, this); // Draw as many as we can in a batch to save ogl state switching. Object nextItem = dc.getOrderedRenderables().peek(); while (nextItem != null && nextItem instanceof OrderedText)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -