1 |
/* |
/* |
2 |
OvalBgVob.java |
OvalBgVob.java |
3 |
* |
* |
4 |
* Copyright (c) 2003, Asko Soukka |
* Copyright (c) 2002, 2003 by Asko Soukka |
5 |
* |
* |
6 |
* This file is part of Gzz. |
* This file is part of Gzz. |
7 |
* |
* |
27 |
*/ |
*/ |
28 |
|
|
29 |
package gzz.vob.vobs; |
package gzz.vob.vobs; |
30 |
|
|
31 |
import gzz.vob.*; |
import gzz.vob.*; |
|
import gzz.*; |
|
|
import gzz.util.*; |
|
|
import gzz.errors.*; |
|
|
import gzz.vob.*; |
|
|
import gzz.client.*; |
|
32 |
import gzz.gfx.gl.*; |
import gzz.gfx.gl.*; |
33 |
import java.awt.*; |
|
34 |
import java.awt.event.*; |
import gzz.util.ColorUtil; |
35 |
import java.util.*; |
import java.awt.Color; |
36 |
import java.lang.Math; |
import java.awt.Graphics; |
37 |
|
import java.awt.Rectangle; |
38 |
import gzz.client.GraphicsAPI; |
import gzz.client.GraphicsAPI; |
39 |
|
import java.lang.Math; |
40 |
|
|
41 |
/** A vob which is a circular background and frame. |
/** A vob which is a circular background and frame. |
42 |
* Draws a filled (possibly with several colors) background circle, |
* Draws a filled (possibly with several colors) background circle, |
43 |
* surrounded by a circle of the current foreground color. |
* surrounded by a circle of the current foreground color. |
44 |
*/ |
*/ |
45 |
|
public class OvalBgVob extends Vob { |
|
public class OvalBgVob extends ColoredSectorVob { |
|
46 |
public static final String rcsid = "$Id$"; |
public static final String rcsid = "$Id$"; |
47 |
|
public static boolean dbg = false; |
48 |
|
private static final void p(String s) { if(dbg) System.out.println(s); } |
49 |
|
|
50 |
|
int NColors = 0; |
51 |
|
Color[] colors; |
52 |
|
|
53 |
|
/** Background color */ |
54 |
|
protected final Color bgColor; |
55 |
|
|
56 |
|
/** Border drawing flag */ |
57 |
|
protected final boolean drawBorder; |
58 |
|
|
59 |
|
static protected float [][] circleGL = new float[360][2]; |
60 |
|
static protected boolean circleGLReady = false; |
61 |
|
static protected void prepareCircleGL(float r) { |
62 |
|
if (!circleGLReady) { |
63 |
|
for (int i=0;i<360;i++) { |
64 |
|
float a = (float)((Math.PI / 180) * (360-i)); |
65 |
|
circleGL[i][0] = 0.5f + x(a, r); |
66 |
|
circleGL[i][1] = 0.5f + y(a, r); |
67 |
|
} |
68 |
|
circleGLReady = true; |
69 |
|
} |
70 |
|
} |
71 |
|
protected static float x(float angle, float radius) { |
72 |
|
return (float)(Math.cos((double)angle) * radius); |
73 |
|
} |
74 |
|
protected static float y(float angle, float radius) { |
75 |
|
return (float)(Math.sin((double)angle) * radius); |
76 |
|
} |
77 |
|
|
78 |
public OvalBgVob() { |
protected int start; |
79 |
super(90, 360, 10); |
protected int sector; |
80 |
start = 90; |
protected int step; |
81 |
sector = 360; |
|
82 |
step = 10; |
/** Constructor shorthands */ |
83 |
|
public OvalBgVob() { this(Color.white, true); } |
84 |
|
public OvalBgVob(Color bgColor) { this(bgColor, true); } |
85 |
|
public OvalBgVob(Color bgColor, boolean drawBorder) { |
86 |
|
this(bgColor, drawBorder, 90, 360, 10); |
87 |
|
} |
88 |
|
public OvalBgVob(int start, int sector, int step) { |
89 |
|
this(Color.white, true, start, sector, step); |
90 |
|
} |
91 |
|
|
92 |
|
/** Constructor for OvalBgVob. |
93 |
|
* @param bgColor Background color. |
94 |
|
* @param drawBorder Border drawing flag. |
95 |
|
* @param start Start angle of the sector. 0 degree is at clock three. |
96 |
|
* 90 degrees is at clock twelve. |
97 |
|
* @param sector The angle of the sector to be drawn. Max 360 degrees. |
98 |
|
* @param step Every step:th vertex is drawn. This affects only the |
99 |
|
* GL implementation. Currently the maximum |
100 |
|
* resolution is 360 vertexes. XXX this will be removed |
101 |
|
* after DiceableMesh renderable works. |
102 |
|
*/ |
103 |
|
public OvalBgVob(Color bgColor, boolean drawBorder, |
104 |
|
int start, int sector, int step) { |
105 |
|
super(); |
106 |
|
this.bgColor = bgColor; |
107 |
|
this.drawBorder = drawBorder; |
108 |
|
this.start = Math.abs(start); |
109 |
|
this.sector = Math.abs(sector); |
110 |
|
this.step = Math.abs(step); |
111 |
} |
} |
112 |
|
|
113 |
|
static Rectangle rect = new Rectangle(); |
114 |
|
|
115 |
public void render(Graphics g, |
public void render(Graphics g, |
116 |
boolean fast, |
boolean fast, |
117 |
Vob.RenderInfo info1, |
Vob.RenderInfo info1, |
122 |
|
|
123 |
Color oldfg = g.getColor(); |
Color oldfg = g.getColor(); |
124 |
|
|
125 |
/** Heavier border with bigger cells */ |
// Draw an oval in the background color, wiping out |
126 |
if(drawBorder && mh >= 20) { |
// the already drawn stuff a little wider than we will draw. |
127 |
g.setColor(info1.getMixedFgColor()); |
g.setColor(info1.getBgColor()); |
128 |
g.fillOval(mx-1, my-1, mw+2, mh+2); |
if(mh >= 14) g.fillOval(mx-2, my-2, mw+4, mh+4); |
129 |
} |
else g.fillOval(mx-1, my-1, mw+2, mh+2); |
130 |
|
|
131 |
if(nsolids == 0) { |
if(colors == null || NColors == 0) { |
132 |
g.setColor(bg); |
g.setColor(bgColor); |
133 |
g.fillOval(mx, my, mw, mh); |
g.fillOval(mx, my, mw, mh); |
134 |
} else { |
} else { |
135 |
/** Draws solids as colored stripes. Each stripe is composed of |
/** Draws colored stripes. Each stripe is composed of |
136 |
* on filled rectangle and two filled arcs. Drawing proceeds from |
* on filled rectangle and two filled arcs. Drawing proceeds from |
137 |
* the boundaries to the centre. Two stripes at time. If there is |
* the boundaries to the centre. Two stripes at time. If there is |
138 |
* odd number of stripes the centered stipes is drawn as two |
* odd number of stripes the centered stipes is drawn as two |
143 |
* \__ second arc |
* \__ second arc |
144 |
* |
* |
145 |
*/ |
*/ |
146 |
g.setColor(solids[0]); |
g.setColor(colors[0]); |
147 |
g.fillArc(mx, my, mw, mh, -90, 180); |
g.fillArc(mx, my, mw, mh, -90, 180); |
148 |
g.setColor(solids[nsolids-1]); |
g.setColor(colors[NColors-1]); |
149 |
g.fillArc(mx, my, mw, mh, 90, 180); |
g.fillArc(mx, my, mw, mh, 90, 180); |
150 |
int lastsolid = nsolids-1; |
int lastColor = NColors-1; |
151 |
int solidwidth = mw / nsolids; |
int colorWidth = mw / NColors; |
152 |
float a = mw/2; |
float a = mw/2; |
153 |
float b = mh/2; |
float b = mh/2; |
154 |
for(int i=1; i<lastsolid; i++) { |
for(int i=1; i<lastColor; i++) { |
155 |
float w = a-i*solidwidth; |
float w = a-i*colorWidth; |
156 |
float h = b * (float)Math.sqrt(1-(Math.pow(w,2)/Math.pow(a, 2))); |
float h = b * (float)Math.sqrt(1-(Math.pow(w,2)/Math.pow(a, 2))); |
157 |
int arc = 90 - (int)((180/Math.PI) * Math.atan2(h*(a/b),w)); |
int arc = 90 - (int)((180/Math.PI) * Math.atan2(h*(a/b),w)); |
158 |
|
|
159 |
// stripe on the left |
// stripe on the left |
160 |
g.setColor(solids[i]); |
g.setColor(colors[i]); |
161 |
g.fillRect((int)(mx+(i*solidwidth)), (int)(my+(b-h)), |
g.fillRect((int)(mx+(i*colorWidth)), (int)(my+(b-h)), |
162 |
(int)w, (int)(h*2)); |
(int)w, (int)(h*2)); |
163 |
g.fillArc(mx, my, mw, mh, 90, arc); |
g.fillArc(mx, my, mw, mh, 90, arc); |
164 |
g.fillArc(mx, my, mw, mh, -90-arc, arc); |
g.fillArc(mx, my, mw, mh, -90-arc, arc); |
165 |
|
|
166 |
// stripe on the right |
// stripe on the right |
167 |
g.setColor(solids[lastsolid-1]); |
g.setColor(colors[lastColor-1]); |
168 |
g.fillRect((int)(mx+a), (int)(my+(b-h)), (int)w, (int)(h*2)); |
g.fillRect((int)(mx+a), (int)(my+(b-h)), (int)w, (int)(h*2)); |
169 |
g.fillArc(mx, my, mw, mh, 90-arc, arc); |
g.fillArc(mx, my, mw, mh, 90-arc, arc); |
170 |
g.fillArc(mx, my, mw, mh, -90, arc); |
g.fillArc(mx, my, mw, mh, -90, arc); |
171 |
|
|
172 |
lastsolid--; |
lastColor--; |
173 |
} |
} |
174 |
} |
} |
175 |
|
|
176 |
if(drawBorder) { |
if(drawBorder) { |
177 |
g.setColor(info1.getMixedFgColor()); |
g.setColor(info1.getMixedFgColor()); |
178 |
g.drawOval(mx, my, mw, mh); |
g.drawOval(mx, my, mw, mh); |
179 |
|
if(mh >= 14) { |
180 |
|
/** Heavier border for greter Vobs */ |
181 |
|
g.drawOval(mx-1, my-1, mw+2, mh+2); |
182 |
|
} |
183 |
} |
} |
184 |
|
|
185 |
g.setColor(oldfg); |
g.setColor(oldfg); |
186 |
} |
} |
187 |
|
|
188 |
Vob glStencil, glBorder; |
Vob glStencil, glBorder, glList; |
189 |
|
|
190 |
public int putGL(final VobScene vs, final int coordsys1) { |
public int putGL(final VobScene vs, final int coordsys1) { |
191 |
if(dbg) pa("Addtolistgl ovalbg "+coordsys1); |
p("Addtolistgl ovalbg "+coordsys1); |
192 |
|
|
193 |
if(glList == null) { |
if(glList == null) { |
194 |
if (!circleGLReady) prepareCircleGL(0.5f); |
if (!circleGLReady) prepareCircleGL(0.5f); |
195 |
String bgcall = ""; |
String bgcall = ""; |
196 |
if(nsolids > 0) { |
if(colors != null && NColors > 0) { |
197 |
bgcall += glSolidsString(); |
bgcall += glColorsString(); |
198 |
} else bgcall += glBgString(); |
} else bgcall += glRectBgString(); |
199 |
|
|
200 |
glList = GLRen.createCallListBoxCoorded( |
glList = GLRen.createCallListBoxCoorded( |
201 |
"PushAttrib CURRENT_BIT ENABLE_BIT\n"+ |
"PushAttrib CURRENT_BIT ENABLE_BIT\n"+ |
209 |
glStencil = GLRen.createCallListBoxCoorded( |
glStencil = GLRen.createCallListBoxCoorded( |
210 |
"PushAttrib CURRENT_BIT ENABLE_BIT\n"+ |
"PushAttrib CURRENT_BIT ENABLE_BIT\n"+ |
211 |
"Disable TEXTURE_2D\n"+ |
"Disable TEXTURE_2D\n"+ |
212 |
super.glBgString() + |
glOvalBgString() + |
213 |
"PopAttrib\n" |
"PopAttrib\n" |
214 |
); |
); |
215 |
} |
} |
240 |
return 0; |
return 0; |
241 |
} |
} |
242 |
|
|
243 |
protected String glBgString() { |
protected String glBorderString() { |
244 |
String glString = ""; |
String glString = ""; |
245 |
glString += ( |
glString += ( |
246 |
"Color "+ColorUtil.colorGLString(bg)+" 1\n" + |
"Color 0 0 0 1\n" + |
247 |
|
"Begin LINE_LOOP\n"); |
248 |
|
if (sector < 360) glString += ("Vertex 0.5 0.5\n"); |
249 |
|
for (int i=0;i<sector;i+=step) { |
250 |
|
glString += ("Vertex " + circleGL[(i+start)%360][0] + |
251 |
|
" " + circleGL[(i+start)%360][1] + "\n"); |
252 |
|
} |
253 |
|
glString += ("Vertex " + circleGL[(sector-1+start)%360][0] + |
254 |
|
" " + circleGL[(sector-1+start)%360][1] + "\n" + |
255 |
|
"End\n"); |
256 |
|
return glString; |
257 |
|
} |
258 |
|
|
259 |
|
protected String glRectBgString() { |
260 |
|
String glString = ""; |
261 |
|
glString += ( |
262 |
|
"Color "+ColorUtil.colorGLString(bgColor)+" 1\n" + |
263 |
"Begin QUAD_STRIP\n"+ |
"Begin QUAD_STRIP\n"+ |
264 |
"Vertex 1 1\nVertex 1 0\n"+ |
"Vertex 1 1\nVertex 1 0\n"+ |
265 |
"Vertex 0 1\nVertex 0 0\n"+ |
"Vertex 0 1\nVertex 0 0\n"+ |
267 |
return glString; |
return glString; |
268 |
} |
} |
269 |
|
|
270 |
protected String glSolidsString() { |
protected String glOvalBgString() { |
|
double w = 1.0 / nsolids; |
|
271 |
String glString = ""; |
String glString = ""; |
272 |
for(int i=0; i<nsolids; i++) { |
glString = ( |
273 |
|
"Color "+ColorUtil.colorGLString(bgColor)+" 1\n" + |
274 |
|
"Begin POLYGON\n"); |
275 |
|
if (sector < 360) glString += ("Vertex 0.5 0.5\n"); |
276 |
|
for (int i=0;i<sector;i+=step) { |
277 |
|
glString += ("Vertex " + circleGL[(i+start)%360][0] + |
278 |
|
" " + circleGL[(i+start)%360][1] + "\n"); |
279 |
|
} |
280 |
|
glString += ("Vertex " + circleGL[(sector-1+start)%360][0] + |
281 |
|
" " + circleGL[(sector-1+start)%360][1] + "\n" + |
282 |
|
"End\n"); |
283 |
|
return glString; |
284 |
|
} |
285 |
|
|
286 |
|
protected String glColorsString() { |
287 |
|
double w = 1.0 / NColors; |
288 |
|
String glString = ""; |
289 |
|
for(int i=0; i<NColors; i++) { |
290 |
double x1 = i*w; |
double x1 = i*w; |
291 |
double x2 = x1 + w; |
double x2 = x1 + w; |
292 |
Color c = solids[i]; |
Color c = colors[i]; |
293 |
glString += ( |
glString += ( |
294 |
"Color "+ColorUtil.colorGLString(solids[i])+" 1\n"+ |
"Color "+ColorUtil.colorGLString(colors[i])+" 1\n"+ |
295 |
"Begin QUAD_STRIP\n"+ |
"Begin QUAD_STRIP\n"+ |
296 |
"TexCoord "+x1+" 1\n"+ |
"TexCoord "+x1+" 1\n"+ |
297 |
"Vertex "+x1+" 1\n"+ |
"Vertex "+x1+" 1\n"+ |
305 |
} |
} |
306 |
return glString; |
return glString; |
307 |
} |
} |
308 |
|
|
309 |
|
/** Adds one more solid color to be drawn inside |
310 |
|
* the cell. |
311 |
|
*/ |
312 |
|
public boolean addColor(Color c) { |
313 |
|
if(colors == null || NColors >= colors.length) { |
314 |
|
Color[] n= new Color[NColors + 10]; |
315 |
|
if(colors != null) System.arraycopy(colors, 0, n, 0, NColors); |
316 |
|
colors = n; |
317 |
|
} |
318 |
|
colors[NColors++] = c; |
319 |
|
return false; |
320 |
|
} |
321 |
|
/** The currently put solid colors. |
322 |
|
* null = none. There may be null references near the end |
323 |
|
* of the array. Mostly useful for checking for nullness. |
324 |
|
*/ |
325 |
|
public Color[] getSolidColors() { return colors; } |
326 |
} |
} |