| 1 | /* |
| 2 | * This file is part of lanterna (http://code.google.com/p/lanterna/). |
| 3 | * |
| 4 | * lanterna is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright (C) 2010-2015 Martin |
| 18 | */ |
| 19 | package com.googlecode.lanterna.graphics; |
| 20 | |
| 21 | import com.googlecode.lanterna.TextCharacter; |
| 22 | import com.googlecode.lanterna.TerminalPosition; |
| 23 | import com.googlecode.lanterna.TerminalSize; |
| 24 | |
| 25 | /** |
| 26 | * This implementation of TextGraphics will take a 'proper' object and composite a view on top of it, by using a |
| 27 | * top-left position and a size. Any attempts to put text outside of this area will be dropped. |
| 28 | * @author Martin |
| 29 | */ |
| 30 | class SubTextGraphics extends AbstractTextGraphics { |
| 31 | private final TextGraphics underlyingTextGraphics; |
| 32 | private final TerminalPosition topLeft; |
| 33 | private final TerminalSize writableAreaSize; |
| 34 | |
| 35 | SubTextGraphics(TextGraphics underlyingTextGraphics, TerminalPosition topLeft, TerminalSize writableAreaSize) { |
| 36 | this.underlyingTextGraphics = underlyingTextGraphics; |
| 37 | this.topLeft = topLeft; |
| 38 | this.writableAreaSize = writableAreaSize; |
| 39 | } |
| 40 | |
| 41 | private TerminalPosition project(int column, int row) { |
| 42 | return topLeft.withRelative(column, row); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public TextGraphics setCharacter(int columnIndex, int rowIndex, TextCharacter textCharacter) { |
| 47 | TerminalSize writableArea = getSize(); |
| 48 | if(columnIndex < 0 || columnIndex >= writableArea.getColumns() || |
| 49 | rowIndex < 0 || rowIndex >= writableArea.getRows()) { |
| 50 | return this; |
| 51 | } |
| 52 | TerminalPosition projectedPosition = project(columnIndex, rowIndex); |
| 53 | underlyingTextGraphics.setCharacter(projectedPosition, textCharacter); |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public TerminalSize getSize() { |
| 59 | return writableAreaSize; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public TextCharacter getCharacter(int column, int row) { |
| 64 | TerminalPosition projectedPosition = project(column, row); |
| 65 | return underlyingTextGraphics.getCharacter(projectedPosition.getColumn(), projectedPosition.getRow()); |
| 66 | } |
| 67 | } |