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
.screen
;
21 import com
.googlecode
.lanterna
.TerminalPosition
;
22 import com
.googlecode
.lanterna
.TextCharacter
;
23 import com
.googlecode
.lanterna
.TerminalSize
;
24 import com
.googlecode
.lanterna
.graphics
.BasicTextImage
;
25 import com
.googlecode
.lanterna
.graphics
.TextGraphics
;
26 import com
.googlecode
.lanterna
.graphics
.TextImage
;
29 * Defines a buffer used by AbstractScreen and its subclasses to keep its state of what's currently displayed and what
30 * the edit buffer looks like. A ScreenBuffer is essentially a two-dimensional array of TextCharacter with some utility
31 * methods to inspect and manipulate it in a safe way.
34 public class ScreenBuffer
implements TextImage
{
35 private final BasicTextImage backend
;
38 * Creates a new ScreenBuffer with a given size and a TextCharacter to initially fill it with
39 * @param size Size of the buffer
40 * @param filler What character to set as the initial content of the buffer
42 public ScreenBuffer(TerminalSize size
, TextCharacter filler
) {
43 this(new BasicTextImage(size
, filler
));
46 private ScreenBuffer(BasicTextImage backend
) {
47 this.backend
= backend
;
51 public ScreenBuffer
resize(TerminalSize newSize
, TextCharacter filler
) {
52 BasicTextImage resizedBackend
= backend
.resize(newSize
, filler
);
53 return new ScreenBuffer(resizedBackend
);
56 boolean isVeryDifferent(ScreenBuffer other
, int threshold
) {
57 if(!getSize().equals(other
.getSize())) {
58 throw new IllegalArgumentException("Can only call isVeryDifferent comparing two ScreenBuffers of the same size!"
59 + " This is probably a bug in Lanterna.");
62 for(int y
= 0; y
< getSize().getRows(); y
++) {
63 for(int x
= 0; x
< getSize().getColumns(); x
++) {
64 if(!getCharacterAt(x
, y
).equals(other
.getCharacterAt(x
, y
))) {
65 if(++differences
>= threshold
) {
74 ///////////////////////////////////////////////////////////////////////////////
75 // Delegate all TextImage calls (except resize) to the backend BasicTextImage
77 public TerminalSize
getSize() {
78 return backend
.getSize();
82 public TextCharacter
getCharacterAt(TerminalPosition position
) {
83 return backend
.getCharacterAt(position
);
87 public TextCharacter
getCharacterAt(int column
, int row
) {
88 return backend
.getCharacterAt(column
, row
);
92 public void setCharacterAt(TerminalPosition position
, TextCharacter character
) {
93 backend
.setCharacterAt(position
, character
);
97 public void setCharacterAt(int column
, int row
, TextCharacter character
) {
98 backend
.setCharacterAt(column
, row
, character
);
102 public void setAll(TextCharacter character
) {
103 backend
.setAll(character
);
107 public TextGraphics
newTextGraphics() {
108 return backend
.newTextGraphics();
112 public void copyTo(TextImage destination
) {
113 if(destination
instanceof ScreenBuffer
) {
114 //This will allow the BasicTextImage's copy method to use System.arraycopy (micro-optimization?)
115 destination
= ((ScreenBuffer
)destination
).backend
;
117 backend
.copyTo(destination
);
121 public void copyTo(TextImage destination
, int startRowIndex
, int rows
, int startColumnIndex
, int columns
, int destinationRowOffset
, int destinationColumnOffset
) {
122 if(destination
instanceof ScreenBuffer
) {
123 //This will allow the BasicTextImage's copy method to use System.arraycopy (micro-optimization?)
124 destination
= ((ScreenBuffer
)destination
).backend
;
126 backend
.copyTo(destination
, startRowIndex
, rows
, startColumnIndex
, columns
, destinationRowOffset
, destinationColumnOffset
);
130 * Copies the content from a TextImage into this buffer.
131 * @param source Source to copy content from
132 * @param startRowIndex Which row in the source image to start copying from
133 * @param rows How many rows to copy
134 * @param startColumnIndex Which column in the source image to start copying from
135 * @param columns How many columns to copy
136 * @param destinationRowOffset The row offset in this buffer of where to place the copied content
137 * @param destinationColumnOffset The column offset in this buffer of where to place the copied content
139 public void copyFrom(TextImage source
, int startRowIndex
, int rows
, int startColumnIndex
, int columns
, int destinationRowOffset
, int destinationColumnOffset
) {
140 source
.copyTo(backend
, startRowIndex
, rows
, startColumnIndex
, columns
, destinationRowOffset
, destinationColumnOffset
);
144 public void scrollLines(int firstLine
, int lastLine
, int distance
) {
145 backend
.scrollLines(firstLine
, lastLine
, distance
);
149 public String
toString() {
150 return backend
.toString();