1
2
3
4
5
6
7 package org.sourceforge.jvb3d.Model;
8
9 import java.awt.Font;
10 import java.io.IOException;
11 import java.io.ObjectInput;
12 import java.io.ObjectOutput;
13
14 import javax.vecmath.*;
15 import javax.media.j3d.*;
16
17 /***
18 * @author spootnick
19 *
20 * Obiekt dynamiczny reprezentujący dynamiczny tekst (zwłaszcza wiadmości z chat-a)
21 */
22 public class HudObject extends DynamicObject implements IChatControl {
23 protected MyText2D[] lines;
24
25 protected MyText2D inputLine;
26 protected boolean[] newLines;
27
28 protected int fontSize = 18;
29
30 protected float scaleFactor = 1f / 2000;
31
32 protected int nLines = 10;
33
34 protected int currentLine = 0;
35
36 protected String textToSubmit = "";
37
38 protected String inputMark="";
39
40
41 public HudObject() {
42 super();
43
44 lines = new MyText2D[nLines];
45 newLines= new boolean[nLines];
46 inputLine = new MyText2D("", new Color3f(0.0f, 1.0f, 0.0f),
47 "Helvetica", fontSize, Font.PLAIN);
48 setCapabilities(inputLine);
49 this.addChild(inputLine);
50 Group group = this;
51 Transform3D t = new Transform3D();
52 t.set(new Vector3f(0, -scaleFactor * fontSize, 0));
53 for (int i = 0; i < lines.length; ++i) {
54 lines[i] = new MyText2D("", new Color3f(1.0f, 1.0f, 1.0f),
55 "Helvetica", fontSize, Font.PLAIN);
56 setCapabilities(lines[i]);
57 TransformGroup tg = new TransformGroup();
58 tg.setTransform(t);
59 tg.addChild(lines[i]);
60 group.addChild(tg);
61 group = tg;
62 newLines[i]=false;
63 }
64 }
65
66 /***
67 * Zwraca wysokość liter jaką będą miały w scenie
68 * @return wysokość liter na chacie
69 */
70 public float getCharacterHeight()
71 {
72 return fontSize*scaleFactor;
73 }
74
75 private void setCapabilities(MyText2D text) {
76 text.setRectangleScaleFactor(scaleFactor);
77 text.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
78 text.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
79 text.getAppearance().setCapability(Appearance.ALLOW_TEXTURE_READ);
80 text.getAppearance().setCapability(Appearance.ALLOW_TEXTURE_WRITE);
81 text.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
82 text.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
83 }
84
85 /***
86 * @see org.sourceforge.jvb3d.Model.IChatControl#submitLine(String)
87 */
88 public synchronized void submitLine(String source) {
89
90 textToSubmit = removeInputMark(inputLine.getString());
91 inputLine.setString("");
92 if(source!=null && !source.equals(""))
93 textToSubmit=source+":"+textToSubmit;
94 if (isServer) {
95 appendLine(textToSubmit);
96 }
97 inputMark="";
98 }
99
100 private void appendLine(String text){
101 if (currentLine == nLines) {
102 for (int i = 0; i < nLines - 1; ++i){
103 lines[i].setString(lines[i + 1].getString());
104 newLines[i]=newLines[i+1];
105 }
106 lines[nLines - 1].setString(text);
107 newLines[nLines - 1]=true;
108 } else {
109
110 lines[currentLine].setString(text);
111 newLines[currentLine++]=true;
112 }
113 inputMark="";
114 }
115
116 /***
117 * @see org.sourceforge.jvb3d.Model.IChatControl#append(String)
118 */
119 public synchronized void append(String s) {
120 String old = removeInputMark(inputLine.getString());
121 inputLine.setString(inputMark+old + s);
122 }
123
124 /***
125 * @see org.sourceforge.jvb3d.Model.IChatControl#delete(int)
126 */
127 public synchronized void delete(int nChars) {
128 String old = removeInputMark(inputLine.getString());
129 int dif;
130 if (nChars <= old.length())
131 dif = nChars;
132 else
133 dif = old.length();
134 inputLine.setString(inputMark+old.substring(0, old.length() - dif));
135 }
136
137 /***
138 * @see org.sourceforge.jvb3d.Model.IChatControl#beginEdit(String)
139 */
140 public synchronized void beginEdit(String inputMark){
141 this.inputMark=inputMark;
142 inputLine.setString(inputMark);
143 }
144
145 private String removeInputMark(String s){
146 return s.substring(inputMark.length());
147 }
148
149 /***
150 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
151 */
152 public synchronized void writeExternal(ObjectOutput arg0) throws IOException {
153
154 super.writeExternal(arg0);
155 if(isServer){
156 int nStrings=0;
157 for(int i=0;i<nLines;++i)
158 if(newLines[i])
159 ++nStrings;
160 arg0.writeInt(nStrings);
161 for(int i=0;i<nLines;++i)
162 if(newLines[i]){
163 newLines[i]=false;
164 arg0.writeObject(lines[i].getString());
165 }
166 }else{
167 arg0.writeInt(1);
168 arg0.writeObject(textToSubmit);
169 }
170
171 }
172
173 /***
174 * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
175 */
176 public synchronized void readExternal(ObjectInput arg0) throws IOException,
177 ClassNotFoundException {
178
179 super.readExternal(arg0);
180 int nStrings=arg0.readInt();
181 for(int i=0;i<nStrings;++i)
182 appendLine((String)arg0.readObject());
183 }
184 }