1 |
/* |
/* |
2 |
ColoredSectorVob.java |
ColoredSectorVob.java |
3 |
* |
* |
4 |
* Copyright (c) 1999-2002, Ted Nelson and Tuomas Lukka |
* Copyright (c) 2002, 2003 by Asko Soukka |
5 |
* |
* |
6 |
* This file is part of Gzz. |
* This file is part of Gzz. |
7 |
* |
* |
23 |
* |
* |
24 |
*/ |
*/ |
25 |
/* |
/* |
26 |
* Written by Tuomas Lukka, Tero Mäyränen and Asko Soukka |
* Written by Asko Soukka |
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) sector, |
* Draws a filled (possibly with several colors) sector, |
43 |
* surrounded by a sector of the current foreground color. |
* surrounded by a sector of the current foreground color. |
44 |
*/ |
*/ |
45 |
|
public class ColoredSectorVob extends OvalBgVob { |
|
public class ColoredSectorVob extends Vob { |
|
46 |
public static final String rcsid = "$Id$"; |
public static final String rcsid = "$Id$"; |
47 |
public static boolean dbg = false; |
public static boolean dbg = false; |
48 |
static final void p(String s) { if(dbg) System.out.println(s); } |
private static final void p(String s) { if(dbg) System.out.println(s); } |
|
static final void pa(String s) { System.out.println(s); } |
|
|
|
|
|
static protected float [][] circleGL = new float[360][2]; |
|
|
static protected boolean circleGLReady = false; |
|
|
static protected void prepareCircleGL(float r) { |
|
|
if (!circleGLReady) { |
|
|
for (int i=0;i<360;i++) { |
|
|
float a = (float)((Math.PI / 180) * (360-i)); |
|
|
circleGL[i][0] = 0.5f + x(a, r); |
|
|
circleGL[i][1] = 0.5f + y(a, r); |
|
|
} |
|
|
circleGLReady = true; |
|
|
} |
|
|
} |
|
|
protected static float x(float angle, float radius) { |
|
|
return (float)(Math.cos((double)angle) * radius); |
|
|
} |
|
|
protected static float y(float angle, float radius) { |
|
|
return (float)(Math.sin((double)angle) * radius); |
|
|
} |
|
|
|
|
|
protected int start; |
|
|
protected int sector; |
|
|
protected int step; |
|
|
|
|
|
/** Creates ColoredSectorVob |
|
|
* @param start Start angle of the sector. 0 degree is at clock three. |
|
|
* 90 degrees is at clock twelve. |
|
|
* @param sector The angle of the sector to be drawn. Max 360 degrees. |
|
|
* @param step Every step:th vertex is drawn. This affects only the |
|
|
* GL implementation. Currently the maximum |
|
|
* resolution is 360 vertexes. XXX this will be removed |
|
|
* after DiceableMesh renderable works. |
|
|
*/ |
|
|
public ColoredSectorVob(int start, int sector, int step) { |
|
|
super(); |
|
|
this.start = Math.abs(start); |
|
|
this.sector = Math.abs(sector); |
|
|
this.step = Math.abs(step); |
|
|
} |
|
|
/* Default valus for drawing a small circle. */ |
|
|
public ColoredSectorVob(){ |
|
|
this(90, 360, 10); |
|
|
} |
|
49 |
|
|
50 |
static Rectangle rect = new Rectangle(); |
static Rectangle rect = new Rectangle(); |
|
int nsolids = 0; |
|
|
Color[] solids; |
|
|
protected Color bg = Color.white; |
|
|
public void setBg(Color c){ if( c != null ) bg = c; } |
|
|
public Color getBg() { return bg; } |
|
|
boolean drawBorder = true; |
|
|
|
|
51 |
public void render(Graphics g, |
public void render(Graphics g, |
52 |
boolean fast, |
boolean fast, |
53 |
Vob.RenderInfo info1, |
Vob.RenderInfo info1, |
58 |
|
|
59 |
Color oldfg = g.getColor(); |
Color oldfg = g.getColor(); |
60 |
|
|
61 |
/** Heavier border with bigger cells */ |
// Draw an oval in the background color, wiping out |
62 |
if(drawBorder && mh >= 20) { |
// the already drawn stuff a little wider than we will draw. |
63 |
g.setColor(info1.getMixedFgColor()); |
g.setColor(info1.getBgColor()); |
64 |
g.fillArc(mx-1, my-1, mw+2, mh+2, start, sector); |
if(mh >= 14) g.fillArc(mx-2, my-2, mw+4, mh+4, start, sector); |
65 |
} |
else g.fillArc(mx-1, my-1, mw+2, mh+2, start, sector); |
66 |
|
|
67 |
if(nsolids == 0) { |
if(colors == null || NColors == 0) { |
68 |
g.setColor(bg); |
g.setColor(bgColor); |
69 |
g.fillArc(mx, my, mw, mh, start, sector); |
g.fillArc(mx, my, mw, mh, start, sector); |
70 |
} else { |
} else { |
71 |
// Draws multiple background colours as sectors |
// Draws multiple background colours as sectors |
72 |
int arc = sector / nsolids; |
int arc = sector / NColors; |
73 |
for(int i=0; i<nsolids; i++) { |
for(int i=0; i<NColors; i++) { |
74 |
g.setColor(solids[i]); |
g.setColor((Color)colors[i]); |
75 |
g.fillArc(mx, my, mw, mh, start + arc * i, arc); |
g.fillArc(mx, my, mw, mh, start + arc * i, arc); |
76 |
} |
} |
77 |
} |
} |
89 |
mx+mw/2 + (int)x(a, mw/2), |
mx+mw/2 + (int)x(a, mw/2), |
90 |
my+mh/2 + (int)y(a, mh/2)); |
my+mh/2 + (int)y(a, mh/2)); |
91 |
} |
} |
92 |
|
if(mh >= 14) { |
93 |
|
g.drawArc(mx-1, my-1, mw+2, mh+2, start, sector); |
94 |
|
if (sector < 360) { |
95 |
|
float a = (float)((Math.PI / 180f) * (360f-(start))); |
96 |
|
g.drawLine(mx-1+mw/2, my-1+mh/2, |
97 |
|
mx-1+mw/2 + (int)x(a, (mw+2)/2), |
98 |
|
my-1+mh/2 + (int)y(a, (mh+2)/2)); |
99 |
|
a = (float)((Math.PI / 180f) * (360f-(sector+start))); |
100 |
|
g.drawLine(mx-1+(mw+2)/2, my-1+(mh+2)/2, |
101 |
|
mx-1+(mw+2)/2 + (int)x(a, (mw+2)/2), |
102 |
|
my-1+(mh+2)/2 + (int)y(a, (mh+2)/2)); |
103 |
|
} |
104 |
|
} |
105 |
} |
} |
106 |
|
|
107 |
g.setColor(oldfg); |
g.setColor(oldfg); |
108 |
} |
} |
109 |
|
|
|
Vob glList; |
|
|
|
|
110 |
public int putGL(VobScene vs, int coordsys1) { |
public int putGL(VobScene vs, int coordsys1) { |
111 |
if(dbg) pa("Addtolistgl coloredsectorvob "+coordsys1); |
p("Addtolistgl coloredsectorvob "+coordsys1); |
112 |
if(glList == null) { |
if(glList == null) { |
113 |
if (!circleGLReady) prepareCircleGL(0.5f); |
if (!circleGLReady) prepareCircleGL(0.5f); |
114 |
|
|
115 |
String bgcall = ""; |
String bgcall = ""; |
116 |
if(nsolids > 0) bgcall += glSolidsString(); |
if(colors != null && NColors > 0) |
117 |
else bgcall += glBgString(); |
bgcall += glColorsString(); |
118 |
|
else bgcall += glOvalBgString(); |
119 |
if(drawBorder) bgcall += glBorderString(); |
if(drawBorder) bgcall += glBorderString(); |
120 |
|
|
121 |
glList = GLRen.createCallListBoxCoorded( |
glList = GLRen.createCallListBoxCoorded( |
129 |
return 0; |
return 0; |
130 |
} |
} |
131 |
|
|
132 |
protected String glBorderString() { |
protected String glColorsString() { |
133 |
String glString = ""; |
int arc = sector / NColors; |
|
glString += ( |
|
|
"Color 0 0 0 1\n" + |
|
|
"Begin LINE_LOOP\n"); |
|
|
if (sector < 360) glString += ("Vertex 0.5 0.5\n"); |
|
|
for (int i=0;i<sector;i+=step) { |
|
|
glString += ("Vertex " + circleGL[(i+start)%360][0] + |
|
|
" " + circleGL[(i+start)%360][1] + "\n"); |
|
|
} |
|
|
glString += ("Vertex " + circleGL[(sector-1+start)%360][0] + |
|
|
" " + circleGL[(sector-1+start)%360][1] + "\n" + |
|
|
"End\n"); |
|
|
return glString; |
|
|
} |
|
|
|
|
|
protected String glBgString() { |
|
134 |
String glString = ""; |
String glString = ""; |
135 |
glString = ( |
for(int i=0; i<NColors; i++) { |
136 |
"Color "+ColorUtil.colorGLString(bg)+" 1\n" + |
Color c = (Color)colors[i]; |
|
"Begin POLYGON\n"); |
|
|
if (sector < 360) glString += ("Vertex 0.5 0.5\n"); |
|
|
for (int i=0;i<sector;i+=step) { |
|
|
glString += ("Vertex " + circleGL[(i+start)%360][0] + |
|
|
" " + circleGL[(i+start)%360][1] + "\n"); |
|
|
} |
|
|
glString += ("Vertex " + circleGL[(sector-1+start)%360][0] + |
|
|
" " + circleGL[(sector-1+start)%360][1] + "\n" + |
|
|
"End\n"); |
|
|
return glString; |
|
|
} |
|
|
|
|
|
protected String glSolidsString() { |
|
|
int arc = sector / nsolids; |
|
|
String glString = ""; |
|
|
for(int i=0; i<nsolids; i++) { |
|
|
Color c = solids[i]; |
|
137 |
glString += ( |
glString += ( |
138 |
"Color "+ColorUtil.colorGLString(solids[i])+" 1\n"+ |
"Color "+ColorUtil.colorGLString((Color)colors[i])+" 1\n"+ |
139 |
"Begin POLYGON\n"+ |
"Begin POLYGON\n"+ |
140 |
"Vertex 0.5 0.5\n"); |
"Vertex 0.5 0.5\n"); |
141 |
for (int j=i*arc;j<(i+1)*arc;j+=step) { |
for (int j=i*arc;j<(i+1)*arc;j+=step) { |
151 |
} |
} |
152 |
return glString; |
return glString; |
153 |
} |
} |
|
|
|
|
/** Adds one more solid color to be drawn inside |
|
|
* the cell. |
|
|
*/ |
|
|
public boolean addColor(Color c) { |
|
|
if(solids == null || nsolids >= solids.length) { |
|
|
Color[] n= new Color[nsolids + 10]; |
|
|
if(solids != null) System.arraycopy(solids, 0, n, 0, nsolids); |
|
|
solids = n; |
|
|
} |
|
|
solids[nsolids++] = c; |
|
|
return false; |
|
|
} |
|
|
/** The currently put solid colors. |
|
|
* null = none. There may be null references near the end |
|
|
* of the array. Mostly useful for checking for nullness. |
|
|
*/ |
|
|
public Color[] getSolidColors() { return solids; } |
|
154 |
} |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|