You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.6KB

  1. package de.hems.trafficsim;
  2. import android.graphics.Canvas;
  3. import android.graphics.Color;
  4. import android.graphics.Paint;
  5. import android.graphics.Rect;
  6. import android.view.SurfaceHolder;
  7. import java.util.ConcurrentModificationException;
  8. import java.util.List;
  9. public class Renderer {
  10. private List<VehicleTimeRecord> stepRecords;
  11. protected Track track;
  12. protected int pixelPerVehicle;
  13. protected int pixelPerLine;
  14. protected float tooShortPerTrackLength;
  15. protected float tooShortPerHeight;
  16. protected Paint paint;
  17. protected SurfaceHolder holder;
  18. protected Canvas canvas;
  19. protected int width;
  20. protected int height;
  21. public Renderer(Track track, SurfaceHolder holder) {
  22. this.track = track;
  23. this.holder = holder;
  24. this.paint = new Paint();
  25. }
  26. public void setSize(int width, int height) {
  27. if (width > 0 && height > 0) {
  28. this.pixelPerVehicle = width / (int) this.track.getTrackLength();
  29. this.tooShortPerTrackLength =
  30. (width - this.pixelPerVehicle * this.track.getTrackLength())
  31. / this.track.getTrackLength();
  32. this.pixelPerLine = height / this.track.getHistoryLength();
  33. this.tooShortPerHeight = (height - this.pixelPerLine * this.track.getHistoryLength())
  34. / (float)height;
  35. System.out.println("Viewport: "+width+"x"+height);
  36. this.width = width;
  37. this.height = height;
  38. }
  39. }
  40. public void setTrack(Track track) {
  41. this.track = track;
  42. this.setSize(width, height);
  43. }
  44. protected int getColor(float curVelocity, float maxVelocity) {
  45. float perc = curVelocity / maxVelocity;
  46. perc = 1 - perc;
  47. if (perc <= 0.5) {
  48. int red = ((int) (2*perc*0xFF)) << 16;
  49. int green = 0xFF << 8;
  50. int blue = 0;
  51. return 0xff000000 | red | green | blue;
  52. } else {
  53. int red = 0xFF << 16;
  54. int green = ((int)(0xFF-0xFF*(perc-0.5)*2)) << 8;
  55. int blue = 0;
  56. return 0xff000000 | red | green | blue;
  57. }
  58. }
  59. protected void draw() {
  60. try {
  61. if (this.pixelPerVehicle == 0) {
  62. Rect rect = this.holder.getSurfaceFrame();
  63. this.setSize(rect.right, rect.bottom);
  64. }
  65. canvas = this.holder.lockCanvas();
  66. synchronized (holder) {
  67. int y = 0;
  68. int left = 0;
  69. int hCompensateStart = 0;
  70. int hCompensateEnd = 0;
  71. int vCompensateStart = 0;
  72. int vCompensateEnd = 0;
  73. canvas.drawColor(Color.BLACK);
  74. for (int curStepIdx = this.track.getVtrList().size()-1;curStepIdx >= 0;
  75. curStepIdx--) {
  76. try {
  77. try {
  78. track.getListSemaphore().acquire();
  79. } catch (InterruptedException ex) {
  80. return;
  81. }
  82. vCompensateStart = Math.round(y * this.tooShortPerHeight);
  83. vCompensateEnd = Math.round((y+this.pixelPerLine) * this.tooShortPerHeight);
  84. stepRecords = this.track.getVtrList().get(curStepIdx);
  85. int i = 0;
  86. for (VehicleTimeRecord r : stepRecords) {
  87. left = (int) (this.pixelPerVehicle * r.getPosition());
  88. hCompensateStart = Math.round(r.getPosition() * this.tooShortPerTrackLength);
  89. hCompensateEnd = Math.round((r.getPosition() + this.pixelPerVehicle) * this.tooShortPerTrackLength);
  90. this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
  91. canvas.drawRect(left + hCompensateStart, y+vCompensateStart,
  92. left + this.pixelPerVehicle + hCompensateEnd,
  93. y + pixelPerLine + vCompensateEnd, this.paint);
  94. i++;
  95. }
  96. track.getListSemaphore().release();
  97. y += pixelPerLine;
  98. } catch (ConcurrentModificationException ex) {
  99. System.out.println("Concurrent Exception occured, skipping record");
  100. y += pixelPerLine;
  101. }
  102. }
  103. }
  104. } finally {
  105. if (canvas != null) {
  106. this.holder.unlockCanvasAndPost(canvas);
  107. }
  108. }
  109. }
  110. }