Ignore:
Timestamp:
12/12/13 16:43:55 (11 years ago)
Author:
jts21
Message:

Add autowrapping to Text items. Enabled for all items by setting the 'AutoWrap' tag in Settings, or for specific items by setting the 'AutoWrap' property on a Text item.

File:
1 edited

Legend:

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

    r594 r613  
    3737import org.expeditee.gui.FrameMouseActions;
    3838import org.expeditee.gui.FrameUtils;
     39import org.expeditee.gui.FreeItems;
    3940import org.expeditee.math.ExpediteeJEP;
     41import org.expeditee.settings.UserSettings;
    4042import org.nfunk.jep.Node;
    4143
     
    122124
    123125        private int _selectionEnd = -1;
     126       
     127        // whether autowrap is on/off for this item
     128        protected boolean _autoWrap = false;
    124129
    125130        // text is broken up into lines
     
    14081413                }
    14091414
    1410                 float width = Float.MAX_VALUE;
     1415                /* float width = Float.MAX_VALUE;
    14111416
    14121417                if (limitWidth) {
     
    14151420                        // width = Math.max(50, getMaxWidth() - getX()
    14161421                        // - Item.MARGIN_RIGHT);
    1417                 }
     1422                } */
    14181423
    14191424                _textLayouts.clear();
     
    14231428
    14241429                TextLayout layout;
     1430               
     1431                float width;
     1432                float lineHeight = Float.NaN;
     1433                List<Point[]> lines = null;
     1434               
     1435                if(_autoWrap || UserSettings.AutoWrap) {
     1436                lines = new LinkedList<Point[]>();
     1437                if(DisplayIO.getCurrentFrame() == null) {
     1438                        return;
     1439                }
     1440                for(Item item : DisplayIO.getCurrentFrame().getItems()) {
     1441                                if(item instanceof Line) {
     1442                                        lines.add(new Point[] { ((Line) item).getStartItem().getPosition(), ((Line) item).getEndItem().getPosition() });
     1443                                }
     1444                                if(item instanceof Picture) {
     1445                                        lines.add(new Point[] { item.getPosition(), new Point(item.getX(), item.getY() + item.getHeight()) });
     1446                                }
     1447                }
     1448                for(Item item : FreeItems.getInstance()) {
     1449                                if(item instanceof Line) {
     1450                                        lines.add(new Point[] { ((Line) item).getStartItem().getPosition(), ((Line) item).getEndItem().getPosition() });
     1451                                }
     1452                                if(item instanceof Picture) {
     1453                                        lines.add(new Point[] { item.getPosition(), new Point(item.getX(), item.getY() + item.getHeight()) });
     1454                                }
     1455                }
     1456                width = getLineWidth(getX(), getY(), lines);
     1457               
     1458                if(width < 100) {
     1459                        width = 100;
     1460                }
     1461                } else {
     1462                        width = Float.MAX_VALUE;
     1463                if (limitWidth) {
     1464                        width = getAbsoluteWidth();
     1465                        // else if (getMaxWidth() > 0)
     1466                        // width = Math.max(50, getMaxWidth() - getX()
     1467                        // - Item.MARGIN_RIGHT);
     1468                }
     1469                }
     1470               
    14251471                _lineBreaker.setPosition(0);
    14261472
    14271473                // --- Get the output of the LineBreakMeasurer and store it in a
    14281474                while ((layout = _lineBreaker.nextLayout(width)) != null) {
     1475                       
    14291476                        // lineBreaker does not break on newline
    14301477                        // characters so they have to be check manually
    14311478                        int start = _lineOffsets.get(_lineOffsets.size() - 1);
    1432 
     1479                       
     1480                        // int y = getY() + (getLineDrop(layout) * (_lineOffsets.size() - 1)
     1481                       
    14331482                        // check through the current line for newline characters
    14341483                        for (int i = start + 1; i < _text.length(); i++) {
     
    14471496
    14481497                        _textLayouts.add(layout);
     1498                       
     1499                        if(_autoWrap || UserSettings.AutoWrap) {
     1500                        if(lineHeight != Float.NaN) {
     1501                                lineHeight = getLineDrop(layout);
     1502                        }
     1503                        width = getLineWidth(getX(), getY() + (lineHeight * (_lineOffsets.size() - 1)), lines);
     1504                        if(width < 100) {
     1505                                width = 100;
     1506                        }
     1507                        }
    14491508                }
    14501509
    14511510                updatePolygon();
    14521511
     1512        }
     1513       
     1514        private float getLineWidth(int x, float y, List<Point[]> lines) {
     1515                float width = FrameGraphics.getMaxFrameSize().width;
     1516                for(Point[] l : lines) {
     1517                        // check for lines that cross over our y
     1518                        if((l[0].y >= y && l[1].y <= y) || (l[0].y <= y && l[1].y >= y)) {
     1519                                float dX = l[0].x - l[1].x;
     1520                                float dY = l[0].y - l[1].y;
     1521                                float newWidth;
     1522                                if(dX == 0) {
     1523                                        newWidth = l[0].x;
     1524                                } else if(dY == 0) {
     1525                                        newWidth = Math.min(l[0].x, l[1].x);
     1526                                } else {
     1527                                        // System.out.print("gradient: " + (dY / dX));
     1528                                        newWidth = l[0].x + (y - l[0].y) * dX / dY;
     1529                                }
     1530                                // System.out.println("dY:" + dY + " dX:" + dX + " width:" + newWidth);
     1531                                if(newWidth < x) {
     1532                                        continue;
     1533                                }
     1534                                if(newWidth < width) {
     1535                                        width = newWidth;
     1536                                }
     1537                        }
     1538                }
     1539                return width - x;
    14531540        }
    14541541
     
    15561643                if (_text == null || _text.length() == 0)
    15571644                        return;
    1558 
    1559                 if (_textLayouts.size() < 1) {
     1645               
     1646                if(_autoWrap || UserSettings.AutoWrap) {
     1647                        invalidateAll();
     1648               
     1649                        rebuild(true);
     1650                } else if (_textLayouts.size() < 1) {
    15601651                        rebuild(true);
    15611652                        // System.out.println("Error: " + _text);
     
    20202111
    20212112        public void justify(boolean fixWidth) {
     2113               
     2114                // if autowrap is on, wrapping is done every time we draw
     2115                if(UserSettings.AutoWrap) {
     2116                        return;
     2117                }
     2118               
    20222119                Integer width = FrameGraphics.getMaxFrameSize().width;
    20232120
     
    23362433                return getText().split("\\s+")[0];
    23372434        }
     2435       
     2436        public boolean getAutoWrap() {
     2437                return _autoWrap;
     2438        }
     2439       
     2440        // workaround since true is the default value and would not be displayed normally
     2441        public String getAutoWrapToSave() {
     2442                if(!_autoWrap) {
     2443                        return null;
     2444                }
     2445                return "true";
     2446        }
     2447       
     2448        public void setAutoWrap(boolean autoWrap) {
     2449                _autoWrap = autoWrap;
     2450        }
    23382451}
Note: See TracChangeset for help on using the changeset viewer.