/tutorial/vision/multi_tasking/kitchen_clock.e

http://github.com/tybor/Liberty · Specman e · 117 lines · 101 code · 13 blank · 3 comment · 3 complexity · 233058dc742d05fe8cf236b2f16007d2 MD5 · raw file

  1. class KITCHEN_CLOCK
  2. --
  3. -- An old style kitchen clock.
  4. --
  5. inherit
  6. GRAPHIC
  7. LAYOUT
  8. redefine set_container
  9. end
  10. creation {ANY}
  11. default_create
  12. feature {}
  13. pen1, pen2: DRAW_KIT
  14. feature {ANY}
  15. w: WINDOW
  16. set_container (c: CONTAINER) is
  17. local
  18. update: SIMPLE_PERIODIC_JOB
  19. do
  20. check
  21. c /= Void
  22. end
  23. w ::= c
  24. container := c
  25. create pen1
  26. create pen2
  27. pen2.set_line_width(5)
  28. pen2.set_color(magenta_color)
  29. w.set_background_color(white_color)
  30. w.set_requisition(10, 10, 30, 30)
  31. create update.set_work(agent redraw, Void, 1, 1.0)
  32. vision.loop_stack.add_job(update)
  33. end
  34. redo_layout (x, y: INTEGER) is
  35. local
  36. i: INTEGER; widget: WIDGET
  37. do
  38. from
  39. i := container.child.upper
  40. until
  41. i < container.child.lower
  42. loop
  43. widget := container.child.item(i)
  44. if not widget.valid_width(widget.width) or else not widget.valid_height(widget.height) then
  45. widget.set_geometry(0, 20, widget.std_width, widget.std_height)
  46. end
  47. i := i - 1
  48. end
  49. end
  50. update_requisition is
  51. do
  52. w.set_requisition(w.min_width, w.min_height, w.std_width, w.std_height)
  53. end
  54. feature {CONTAINER}
  55. expose_paint is
  56. do
  57. draw
  58. end
  59. feature {}
  60. draw is
  61. local
  62. time: TIME; angle: REAL_64; english_string: STRING
  63. do
  64. pen1.set_drawable(w)
  65. pen2.set_drawable(w)
  66. english_string := once " ... local private persistant buffer ... "
  67. time.update
  68. angle := (time.second - 15) / 60 * 2 * 3.1415926
  69. pen2.line(w.width // 2,
  70. w.height // 2,
  71. w.width // 2 + (w.width / 2 * angle.cos).floor.force_to_integer_32,
  72. w.height // 2 + (w.height / 2 * angle.sin).floor.force_to_integer_32)
  73. angle := ((time.hour - 3) / 12 + time.minute / (60 * 12)) * 2 * 3.1415926
  74. pen1.line(w.width // 2,
  75. w.height // 2,
  76. w.width // 2 + (w.width / 4 * angle.cos).floor.force_to_integer_32,
  77. w.height // 2 + (w.height / 4 * angle.sin).floor.force_to_integer_32)
  78. angle := (time.minute - 15) / 60 * 2 * 3.1415926
  79. pen1.line(w.width // 2,
  80. w.height // 2,
  81. w.width // 2 + (w.width / 2 * angle.cos).floor.force_to_integer_32,
  82. w.height // 2 + (w.height / 2 * angle.sin).floor.force_to_integer_32)
  83. english_string.clear_count
  84. time_formatter.set_time(time)
  85. time_formatter.append_in(english_string)
  86. unicode_string.clear_count
  87. if unicode_string.utf8_decode_from(english_string) then
  88. pen2.put_string(unicode_string, 0, 0)
  89. end
  90. end
  91. time_formatter: TIME_FORMATTER is
  92. once
  93. create {TIME_IN_ENGLISH}Result
  94. end
  95. redraw: BOOLEAN is
  96. do
  97. w.clear
  98. Result := True
  99. end
  100. unicode_string: UNICODE_STRING is
  101. once
  102. create Result.make(0)
  103. end
  104. end -- class KITCHEN_CLOCK