Ignore:
Timestamp:
06/20/08 16:48:33 (16 years ago)
Author:
ra33
Message:

Fixed rounding errors in Text class...
They caused a whole bunch of problems with small fonts and text items with several lines!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Text.java

    r106 r107  
    88import java.awt.Point;
    99import java.awt.Polygon;
    10 import java.awt.Rectangle;
    1110import java.awt.Stroke;
    1211import java.awt.event.KeyEvent;
     
    396395                float y = getLineDrop(last) * line;
    397396
    398                 int x = (Math.round(getX() + caret[0]) + getJustOffset(last));
     397                float x = getX() + caret[0] + getJustOffset(last);
    399398                x = Math
    400399                                .min(
    401400                                                x,
    402401                                                (getX() - Item.MARGIN_RIGHT - (2 * getGravity()) + getBoundsWidth()));
    403                 return new Point(x, Math.round(getY() + y + caret[1]));
     402                return new Point(Math.round(x), Math.round(getY() + y + caret[1]));
    404403        }
    405404
     
    617616                float y = getLineDrop(current) * line;
    618617
    619                 int x = Math.round(getX() + caret[0]) + getJustOffset(current);
     618                float x = getX() + caret[0] + getJustOffset(current);
    620619                x = Math
    621620                                .min(
    622621                                                x,
    623622                                                (getX() - Item.MARGIN_RIGHT - (2 * getGravity()) + getBoundsWidth()));
    624                 return new Point(x, Math.round(getY() + y + caret[1]));
     623                return new Point(Math.round(x), Math.round(getY() + y + caret[1]));
    625624        }
    626625
     
    701700                float y = getLineDrop(current) * line;
    702701
    703                 return new Point(Math.round(getX() + caret[0])
    704                                 + getJustOffset(current), Math.round(getY() + y + caret[1]));
     702                return new Point(
     703                                Math.round(getX() + caret[0]) + getJustOffset(current), Math
     704                                                .round(getY() + y + caret[1]));
    705705        }
    706706
     
    715715         * @return The position in the string of the character being pointed at.
    716716         */
    717         private TextHitInfo getCharPosition(int line, int mouseX) {
     717        public TextHitInfo getCharPosition(int line, int mouseX) {
    718718                if (line < 0 || line >= _textLayouts.size())
    719719                        return null;
     
    726726        }
    727727
    728         private int getLinePosition(int mouseY) {
     728        public int getLinePosition(int mouseY) {
    729729                mouseY += getOffset().y;
    730730
    731                 int y = getY();
     731                float y = getY();
    732732
    733733                for (TextLayout text : _textLayouts) {
    734734                        // calculate X to ensure it is in the shape
    735                         Rectangle bounds = text.getLogicalHighlightShape(0,
    736                                         text.getCharacterCount()).getBounds();
     735                        Rectangle2D bounds = text.getLogicalHighlightShape(0,
     736                                        text.getCharacterCount()).getBounds2D();
    737737
    738738                        if (bounds.getWidth() < 1)
    739                                 bounds = new Rectangle((int) bounds.getMinX(), (int) bounds
    740                                                 .getMinY(), 10, (int) bounds.getHeight());
     739                                bounds.setRect(bounds.getMinX(), bounds
     740                                                .getMinY(), 10, bounds.getHeight());
    741741
    742742                        double x = bounds.getCenterX();
    743743
    744                         // if(text.getLogicalHighlightShape(0,
    745                         // text.getCharacterCount()).contains(text.getBounds().getMinX()
    746                         // + 10, mouseY - getY() - (y - getY())))
    747744                        if (bounds.contains(x, mouseY - getY() - (y - getY())))
    748745                                return _textLayouts.indexOf(text);
     
    973970        public boolean intersects(Polygon p) {
    974971                if (super.intersects(p)) {
    975                         int textY = getY();
     972                        float textY = getY();
    976973
    977974                        for (TextLayout text : _textLayouts) {
    978975                                // check left and right of each box
    979                                 Rectangle textOutline = text.getLogicalHighlightShape(0,
    980                                                 text.getCharacterCount()).getBounds();
    981                                 textOutline.width += 2;
    982                                 textOutline.height += 2;
    983 
    984                                 textOutline.translate(getX() - 1, textY - 1);
     976                                Rectangle2D textOutline = text.getLogicalHighlightShape(0,
     977                                                text.getCharacterCount()).getBounds2D();
     978                                textOutline
     979                                                .setRect(textOutline.getX() + getX() - 1, textOutline
     980                                                                .getY()
     981                                                                + textY - 1, textOutline.getWidth() + 2,
     982                                                                textOutline.getHeight() + 2);
    985983                                if (p.intersects(textOutline))
    986984                                        return true;
     
    1000998                mouseY += getOffset().y;
    1001999
    1002                 int textY = getY();
    1003                 int textX = getX();
    1004 
    1005                 Rectangle outline = getPolygon().getBounds();
     1000                float textY = getY();
     1001                float textX = getX();
     1002
     1003                Rectangle2D outline = getPolygon().getBounds2D();
    10061004
    10071005                // Check if its outside the top and left and bottom bounds
    1008                 if (outline.x - mouseX > gravity || outline.y - mouseY > gravity
    1009                                 || mouseY - (outline.y + outline.height) > gravity
    1010                                 || mouseX - (outline.x + outline.width) > gravity) {
     1006                if (outline.getX() - mouseX > gravity
     1007                                || outline.getY() - mouseY > gravity
     1008                                || mouseY - (outline.getY() + outline.getHeight()) > gravity
     1009                                || mouseX - (outline.getX() + outline.getWidth()) > gravity) {
    10111010                        return false;
    10121011                }
     
    10141013                for (TextLayout text : _textLayouts) {
    10151014                        // check left and right of each box
    1016                         Rectangle textOutline = text.getLogicalHighlightShape(0,
    1017                                         text.getCharacterCount()).getBounds();
     1015                        Rectangle2D textOutline = text.getLogicalHighlightShape(0,
     1016                                        text.getCharacterCount()).getBounds2D();
    10181017
    10191018                        // check if the cursor is within the top, bottom and within the
    10201019                        // gravity of right
    1021                         if (mouseY - textY > textOutline.y
    1022                                         && mouseY - textY < textOutline.y + textOutline.height
    1023                                         && mouseX - textX < textOutline.width + gravity
     1020                        if (mouseY - textY > textOutline.getY()
     1021                                        && mouseY - textY < textOutline.getY()
     1022                                                        + textOutline.getHeight()
     1023                                        && mouseX - textX < textOutline.getWidth() + gravity
    10241024                                                        + Item.MARGIN_RIGHT)
    10251025                                return true;
     
    13531353                        g.setColor(getPaintHighlightColor());
    13541354                        Stroke _highlightStroke = new BasicStroke(
    1355                                         (float) _highlightThickness, BasicStroke.CAP_BUTT,
    1356                                         BasicStroke.JOIN_ROUND);
     1355                                        (float) _highlightThickness, CAP, JOIN);
    13571356                        g.setStroke(_highlightStroke);
    13581357                        g.drawPolygon(getPolygon());
     
    16211620                        // bottom of text lines... so the cursor must be between two text
    16221621                        // lines
    1623                         int textY = getY();
    1624                         int textX = getX();
     1622                        float textY = getY();
     1623                        float textX = getX();
    16251624
    16261625                        for (TextLayout text : _textLayouts) {
    16271626                                // check left and right of each box
    1628                                 Rectangle textOutline = text.getLogicalHighlightShape(0,
    1629                                                 text.getCharacterCount()).getBounds();
     1627                                Rectangle2D textOutline = text.getLogicalHighlightShape(0,
     1628                                                text.getCharacterCount()).getBounds2D();
    16301629
    16311630                                // check if the cursor is within the top, bottom and within the
    16321631                                // gravity of right
    1633                                 if (y - textY > textOutline.y - NEAR_DISTANCE
    1634                                                 && y - textY < textOutline.y + textOutline.height
    1635                                                                 + NEAR_DISTANCE
    1636                                                 && x - textX < textOutline.width + NEAR_DISTANCE)
     1632                                if (y - textY > textOutline.getY() - NEAR_DISTANCE
     1633                                                && y - textY < textOutline.getY()
     1634                                                                + textOutline.getHeight() + NEAR_DISTANCE
     1635                                                && x - textX < textOutline.getWidth() + NEAR_DISTANCE)
    16371636                                        return true;
    16381637                                textY += getLineDrop(text);
Note: See TracChangeset for help on using the changeset viewer.