2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
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.
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.
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/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.graphics
;
21 import com
.googlecode
.lanterna
.TextCharacter
;
22 import com
.googlecode
.lanterna
.TerminalPosition
;
23 import com
.googlecode
.lanterna
.TerminalSize
;
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.
30 class SubTextGraphics
extends AbstractTextGraphics
{
31 private final TextGraphics underlyingTextGraphics
;
32 private final TerminalPosition topLeft
;
33 private final TerminalSize writableAreaSize
;
35 SubTextGraphics(TextGraphics underlyingTextGraphics
, TerminalPosition topLeft
, TerminalSize writableAreaSize
) {
36 this.underlyingTextGraphics
= underlyingTextGraphics
;
37 this.topLeft
= topLeft
;
38 this.writableAreaSize
= writableAreaSize
;
41 private TerminalPosition
project(int column
, int row
) {
42 return topLeft
.withRelative(column
, row
);
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()) {
52 TerminalPosition projectedPosition
= project(columnIndex
, rowIndex
);
53 underlyingTextGraphics
.setCharacter(projectedPosition
, textCharacter
);
58 public TerminalSize
getSize() {
59 return writableAreaSize
;
63 public TextCharacter
getCharacter(int column
, int row
) {
64 TerminalPosition projectedPosition
= project(column
, row
);
65 return underlyingTextGraphics
.getCharacter(projectedPosition
.getColumn(), projectedPosition
.getRow());