Crossfire JXClient, Trunk
Line.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.log;
24 
26 import java.awt.Graphics2D;
27 import java.awt.geom.RectangularShape;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
39 public class Line {
40 
44  private static final int MIN_LINE_HEIGHT = 8;
45 
50  @NotNull
51  private final List<TextSegment> segments = new ArrayList<>();
52 
56  private final int type;
57 
61  private final int subtype;
62 
66  private int height = -1;
67 
71  private int position;
72 
76  private int length;
77 
81  private boolean showFirstSegment;
82 
89  public Line(final int type, final int subtype, @NotNull final TextSegment segment) {
90  this.type = type;
91  this.subtype = subtype;
92  segments.add(segment);
93  length = segment.getText().length();
94  }
95 
100  public void addTextSegment(@NotNull final TextSegment segment) {
101  segments.add(segment);
102  length += segment.getText().length();
103  }
104 
109  @NotNull
110  public Iterable<TextSegment> segments() {
111  final List<TextSegment> tmp = Collections.unmodifiableList(segments);
112  return showFirstSegment ? tmp : tmp.subList(1, tmp.size());
113  }
114 
119  @Nullable
121  return segments.size() > 1 ? segments.get(segments.size()-1) : null;
122  }
123 
127  public void removeLastTextSegment() {
128  if (segments.size() <= 1) {
129  throw new IllegalStateException("line is empty");
130  }
131  final TextSegment segment = segments.remove(segments.size()-1);
132  length -= segment.getText().length();
133  }
134 
140  public int getHeight() {
141  return height;
142  }
143 
149  public int getStartPosition() {
150  return position;
151  }
152 
158  public int getEndPosition() {
159  return position+length;
160  }
161 
172  public int calculateHeight(final int linePosition, final int renderWidth, final boolean showSentCommands, final boolean showFirstSegment) {
173  position = linePosition;
174  this.showFirstSegment = showFirstSegment;
175 
176  if (!isVisible(showSentCommands)) {
177  int position = linePosition;
178  for (final TextSegment segment : segments) {
179  segment.setExtends(0, 0, position);
180  position += segment.getText().length();
181  }
182  height = 0;
183  return height;
184  }
185 
186  int height = 0;
187  int x = 0;
188  int minY = 0;
189  int maxY = 0;
190  int position = linePosition;
191  for (int i = 0; i < segments.size(); i++) {
192  final TextSegment segment = segments.get(i);
193  final RectangularShape rectangle = segment.getSize();
194  final int width = showFirstSegment || i > 0 ? (int)Math.round(rectangle.getWidth()) : 0;
195  if (x != 0 && x+width > renderWidth) {
196  height += maxY-minY;
197  x = 0;
198  minY = 0;
199  maxY = 0;
200  }
201 
202  segment.setExtends(x, height, position);
203 
204  x += width;
205  minY = (int)Math.min(minY, Math.round(rectangle.getY()));
206  maxY = (int)Math.max(maxY, Math.round(rectangle.getY()+rectangle.getHeight()));
207  position += segment.getText().length();
208  }
209 
210  height += maxY-minY;
211 
212  this.height = Math.max(MIN_LINE_HEIGHT, height);
213  return this.height;
214  }
215 
221  public boolean isVisible(final boolean showSentCommands) {
223  }
224 
229  public void updatePosition(final int positions) {
230  if (positions < 0 && position+positions < 0) {
231  throw new IllegalArgumentException("cannot shift line position "+position+" by "+positions);
232  }
233  position += positions;
234  for (final TextSegment segment : segments) {
235  segment.updatePosition(positions);
236  }
237  }
238 
246  public void drawLine(@NotNull final Graphics2D g, final int y, final int beginSelection, final int endSelection) {
247  for (TextSegment segment : segments()) {
248  segment.draw(g, y, beginSelection, endSelection);
249  }
250  }
251 
258  public void appendSelection(@NotNull final StringBuilder sb, final int beginSelection, final int endSelection) {
259  for (TextSegment segment : segments()) {
260  segment.appendSelection(sb, beginSelection, endSelection);
261  }
262  }
263 
264 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.gui.log.Line.length
int length
Definition: Line.java:76
com.realtime.crossfire.jxclient.gui.log.Line
Definition: Line.java:39
com.realtime.crossfire.jxclient.gui.log.Line.isVisible
boolean isVisible(final boolean showSentCommands)
Definition: Line.java:221
com.realtime.crossfire.jxclient.gui.log.TextSegment
Definition: TextSegment.java:40
com.realtime.crossfire.jxclient.gui.log.TextSegment.getText
String getText()
Definition: TextSegment.java:154
com.realtime.crossfire.jxclient.gui.log.TextSegment.setExtends
void setExtends(final int x, final int y, final int position)
Definition: TextSegment.java:172
com.realtime.crossfire.jxclient.gui.log.Line.position
int position
Definition: Line.java:71
com.realtime.crossfire.jxclient.gui.log.Line.appendSelection
void appendSelection(@NotNull final StringBuilder sb, final int beginSelection, final int endSelection)
Definition: Line.java:258
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.gui.log.Line.Line
Line(final int type, final int subtype, @NotNull final TextSegment segment)
Definition: Line.java:89
com.realtime.crossfire.jxclient.protocol.MessageType
Definition: MessageType.java:33
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_SUBTYPE_JXCLIENT_COMMAND
static final int MSG_SUBTYPE_JXCLIENT_COMMAND
Definition: MessageType.java:479
com.realtime.crossfire.jxclient.protocol.MessageType.MSG_TYPE_JXCLIENT
static final int MSG_TYPE_JXCLIENT
Definition: MessageType.java:136
com.realtime.crossfire.jxclient.gui.log.Line.height
int height
Definition: Line.java:66
com.realtime.crossfire.jxclient.gui.log.Line.getEndPosition
int getEndPosition()
Definition: Line.java:158
com.realtime.crossfire.jxclient.gui.log.Line.getHeight
int getHeight()
Definition: Line.java:140
com.realtime.crossfire.jxclient.gui.log.Line.subtype
final int subtype
Definition: Line.java:61
com.realtime.crossfire.jxclient.gui.log.Line.getLastTextSegment
TextSegment getLastTextSegment()
Definition: Line.java:120
com.realtime.crossfire.jxclient.gui.log.Line.getStartPosition
int getStartPosition()
Definition: Line.java:149
com.realtime.crossfire.jxclient.gui.log.Line.showFirstSegment
boolean showFirstSegment
Definition: Line.java:81
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.gui.log.Line.segments
final List< TextSegment > segments
Definition: Line.java:51
com.realtime.crossfire.jxclient.gui.log.Line.segments
Iterable< TextSegment > segments()
Definition: Line.java:110
com
com.realtime.crossfire.jxclient.gui.log.Line.calculateHeight
int calculateHeight(final int linePosition, final int renderWidth, final boolean showSentCommands, final boolean showFirstSegment)
Definition: Line.java:172
com.realtime.crossfire.jxclient.gui.log.TextSegment.getSize
RectangularShape getSize()
Definition: TextSegment.java:261
com.realtime.crossfire.jxclient.gui.log.Line.type
final int type
Definition: Line.java:56
com.realtime.crossfire.jxclient.gui.log.Line.updatePosition
void updatePosition(final int positions)
Definition: Line.java:229
com.realtime.crossfire.jxclient.gui.log.Line.removeLastTextSegment
void removeLastTextSegment()
Definition: Line.java:127
com.realtime.crossfire.jxclient.gui.log.Line.drawLine
void drawLine(@NotNull final Graphics2D g, final int y, final int beginSelection, final int endSelection)
Definition: Line.java:246
com.realtime.crossfire.jxclient.gui.log.Line.addTextSegment
void addTextSegment(@NotNull final TextSegment segment)
Definition: Line.java:100
com.realtime.crossfire.jxclient.gui.log.Line.MIN_LINE_HEIGHT
static final int MIN_LINE_HEIGHT
Definition: Line.java:44