package com.caini.iDictionary.BarChartDisplay;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.graphics.Paint;
import android.opengl.GLSurfaceView;
public class StudyBarChartRenderer implements GLSurfaceView.Renderer {
public static final int NUMOFDAYS = 7;
private LabelMaker title, weekdaysNumOfWords, weekdays;
private Paint titlePaint, numOfWordsPaint, weekdaysPaint;
private int labelTitlePart1, labelTitlePart2;
private int mondayNumOfWords, tuesdayNumOfWords, wednesdayNumOfWords, thursdayNumOfWords, fridayNumOfWords, saturdayNumOfWords, sundayNumOfWords;
private int mondayLabel, tuesdayLabel, wednesdayLabel, thursdayLabel, fridayLabel, saturdayLabel, sundayLabel;
private int width, height;
private int[] numOfWords;
private String[] dates;
private StudyVerticesArrayGenerator vag;
private float red = 0f;
private float green = 0f;
private float blue = 0f;
// a raw buffer to hold indices allowing a reuse of points.
private ShortBuffer indexBuffer;
// a raw buffer to hold the vertices
private FloatBuffer[] vertexBuffers;
private float[][] verticesArrays;
private short[] indicesArray = {0, 1, 2, 3, 4, 5, 6};
public StudyBarChartRenderer(int[] numOfWords, String[] dates) {
this.numOfWords = numOfWords;
this.dates = dates;
vertexBuffers = new FloatBuffer[NUMOFDAYS];
vag = new StudyVerticesArrayGenerator(numOfWords);
verticesArrays = vag.getVertexArrays();
titlePaint = new Paint();
titlePaint.setTextSize(18);
titlePaint.setAntiAlias(true);
titlePaint.setARGB(0xff, 0x00, 0x00, 0xff);
numOfWordsPaint = new Paint();
numOfWordsPaint.setTextSize(12);
numOfWordsPaint.setAntiAlias(true);
numOfWordsPaint.setARGB(0xff, 0x00, 0x00, 0x00);
weekdaysPaint = new Paint();
weekdaysPaint.setTextSize(11);
weekdaysPaint.setAntiAlias(true);
weekdaysPaint.setARGB(0xff, 0x00, 0x00, 0x00);
}
protected static FloatBuffer makeFloatBuffer(float[] arr)
{
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(arr);
fb.position(0);
return fb;
}
protected static ShortBuffer makeShortBuffer(short[] arr)
{
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 2);
bb.order(ByteOrder.nativeOrder());
ShortBuffer sb = bb.asShortBuffer();
sb.put(arr);
sb.position(0);
return sb;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// preparation
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
for (int i = 0; i < NUMOFDAYS; i++)
vertexBuffers[i] = makeFloatBuffer(verticesArrays[i]);
indexBuffer = makeShortBuffer(indicesArray);
title = new LabelMaker(true, 256, 64);
weekdaysNumOfWords = new LabelMaker(true, 256, 64);
weekdays = new LabelMaker(true, 256, 64);
title.initialize(gl);
title.beginAdding(gl);
labelTitlePart1 = title.add(gl, "Number of words", titlePaint);
labelTitlePart2 = title.add(gl, "remembered this week", titlePaint);
title.endAdding(gl);
weekdaysNumOfWords.initialize(gl);
weekdaysNumOfWords.beginAdding(gl);
mondayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[0], numOfWordsPaint);
tuesdayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[1], numOfWordsPaint);
wednesdayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[2], numOfWordsPaint);
thursdayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[3], numOfWordsPaint);
fridayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[4], numOfWordsPaint);
saturdayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[5], numOfWordsPaint);
sundayNumOfWords = weekdaysNumOfWords.add(gl, ""+numOfWords[6], numOfWordsPaint);
weekdaysNumOfWords.endAdding(gl);
weekdays.initialize(gl);
weekdays.beginAdding(gl);
mondayLabel = weekdays.add(gl, dates[0], weekdaysPaint);
tuesdayLabel = weekdays.add(gl, dates[1], weekdaysPaint);
wednesdayLabel = weekdays.add(gl, dates[2], weekdaysPaint);
thursdayLabel = weekdays.add(gl, dates[3], weekdaysPaint);
fridayLabel = weekdays.add(gl, dates[4], weekdaysPaint);
saturdayLabel = weekdays.add(gl, dates[5], weekdaysPaint);
sundayLabel = weekdays.add(gl, dates[6], weekdaysPaint);
weekdays.endAdding(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
width = w;
height = h;
gl.glViewport(0, 0, w, h);
}
/*
* THE HEIGHT OF THE SURFACEVICE IS 373.0f
*/
@Override
public void onDrawFrame(GL10 gl) {
// define the color we want to be displayed as the clipping wall
gl.glClearColor(0.5294f, 0.8078f, 0.9215f, 1.0f);
// clear the color buffer to show the ClearColor we called above
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLineWidth(2f);
gl.glColor4f(red, green, blue, 0f);
for (int i = 0; i < 7; i++) {
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffers[i]);
gl.glDrawElements(GL10.GL_LINE_LOOP, 4 , GL10.GL_UNSIGNED_SHORT, indexBuffer);
}
title.beginDrawing(gl, width, height);
drawTitleLabel(gl, 78.0f, 390.0f, labelTitlePart1);
drawTitleLabel(gl, 55.0f, 365.0f, labelTitlePart2);
title.endDrawing(gl);
float[] offsets = vag.getOffsets();
weekdaysNumOfWords.beginDrawing(gl, width, height);
drawNumOfWordsLabel(gl, 52.0f, (0.5f+offsets[0]+0.03f)*height/2, mondayNumOfWords);
drawNumOfWordsLabel(gl, 84.0f, (0.5f+offsets[1]+0.03f)*height/2, tuesdayNumOfWords);
drawNumOfWordsLabel(gl, 116.0f, (0.5f+offsets[2]+0.03f)*height/2, wednesdayNumOfWords);
drawNumOfWordsLabel(gl, 147.0f, (0.5f+offsets[3]+0.03f)*height/2, thursdayNumOfWords);
drawNumOfWordsLabel(gl, 179.0f, (0.5f+offsets[4]+0.03f)*height/2, fridayNumOfWords);
drawNumOfWordsLabel(gl, 212.0f, (0.5f+offsets[5]+0.03f)*height/2, saturdayNumOfWords);
drawNumOfWordsLabel(gl, 245.0f, (0.5f+offsets[6]+0.03f)*height/2, sundayNumOfWords);
weekdaysNumOfWords.endDrawing(gl);
weekdays.beginDrawing(gl, width, height);
drawWeekdaysLabel(gl, 46.0f, 85.0f, mondayLabel);
drawWeekdaysLabel(gl, 78.0f, 85.0f, tuesdayLabel);
drawWeekdaysLabel(gl, 110.0f, 85.0f, wednesdayLabel);
drawWeekdaysLabel(gl, 142.0f, 85.0f, thursdayLabel);
drawWeekdaysLabel(gl, 174.0f, 85.0f, fridayLabel);
drawWeekdaysLabel(gl, 206.0f, 85.0f, saturdayLabel);
drawWeekdaysLabel(gl, 237.0f, 85.0f, sundayLabel);
weekdays.endDrawing(gl);
}
private void drawTitleLabel(GL10 gl, float x, float y, int labelId) {
title.draw(gl, x, y, labelId);
}
private void drawNumOfWordsLabel(GL10 gl, float x, float y, int labelId) {
weekdaysNumOfWords.draw(gl, x, y, labelId);
}
private void drawWeekdaysLabel(GL10 gl, float x, float y, int labelId) {
weekdays.draw(gl, x, y, labelId);
}
}
|