clock2.pde 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import ketai.sensors.KetaiSensor;
  2. import android.content.Context;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. KetaiSensor sensor;
  6. PShader blur;
  7. String timeString = "";
  8. PVector center;
  9. PVector shadowOffset;
  10. PVector fontPosition;
  11. PGraphics shadowText;
  12. //setting for how much the shadow can move away from center
  13. int shadowSize = 15;
  14. final float EARTH_GRAVITY = 9.8;
  15. public void setup() {
  16. //render method must be P2D
  17. fullScreen(P2D);
  18. //size(360, 360, P2D);
  19. orientation(PORTRAIT);
  20. //load in glsl shader for blurring
  21. blur = loadShader("sepblur.glsl");
  22. background(0, 0, 0);
  23. noStroke();
  24. textAlign(CENTER);
  25. textSize((int)(width/4));
  26. text("loading", width/2, height/2 + (textAscent() / 2));
  27. reset();
  28. }
  29. private void reset() {
  30. center = new PVector(width/2, height/2);
  31. fontPosition = new PVector(center.x, center.y + (textAscent() / 2));
  32. shadowOffset = new PVector();
  33. shadowText = createGraphics(width, height, P2D);
  34. shadowText.beginDraw();
  35. shadowText.textAlign(CENTER);
  36. shadowText.textSize((int)(width/4));
  37. shadowText.fill(0, 0, 0);
  38. shadowText.endDraw();
  39. sensor = new KetaiSensor(this);
  40. sensor.start();
  41. }
  42. public void draw() {
  43. /**********
  44. * update *
  45. **********/
  46. timeString = getTimeString();
  47. /********
  48. * draw *
  49. ********/
  50. //clear frame
  51. background(0, 0, 0);
  52. //draw clockface
  53. fill(128, 128, 128);
  54. ellipse(center.x, center.y, width, width);
  55. //draw time shadow
  56. shadowText.beginDraw();
  57. shadowText.clear();
  58. shadowText.text(timeString, fontPosition.x + shadowOffset.x, fontPosition.y + shadowOffset.y);
  59. doBlur(shadowText);
  60. shadowText.endDraw();
  61. image(shadowText, 0, 0);
  62. //draw time
  63. fill(255, 255, 255);
  64. text(timeString, fontPosition.x, fontPosition.y);
  65. }
  66. private void doBlur(PGraphics graphics) {
  67. //set blur settings
  68. blur.set("blurSize", 9);
  69. blur.set("sigma", 5.0f);
  70. blur.set("horizontalPass", 1);
  71. //apply horizontal blur
  72. graphics.filter(blur);
  73. //apply vertical blur
  74. blur.set("horizontalPass", 0);
  75. graphics.filter(blur);
  76. }
  77. private String addZeroPrefix(int value) {
  78. if (value < 10) {
  79. return "0" + value;
  80. }
  81. return "" + value;
  82. }
  83. private String getTimeString() {
  84. //24 hour, 0-padded time
  85. return addZeroPrefix(hour()) + ":" + addZeroPrefix(minute()) + ":" + addZeroPrefix(second());
  86. }
  87. public void onAccelerometerEvent(float x, float y, float z, long timestamp, int accuracy) {
  88. //accelerometer data in m/s^2 , divided by the force of gravity on earth
  89. //light source is assumed to be directly above, so "down" determines where the shadow should go
  90. shadowOffset.set(shadowSize * (-x / EARTH_GRAVITY), shadowSize * (y / EARTH_GRAVITY), 0);
  91. }
  92. //android specific called method when app is put in background
  93. public void onPause() {
  94. super.onPause();
  95. sensor.stop();
  96. }
  97. //android specific called method when app is put in foreground
  98. public void onResume() {
  99. super.onResume();
  100. reset();
  101. }