/tutorial/vision/widget/window_palette1.e

http://github.com/tybor/Liberty · Specman e · 115 lines · 95 code · 11 blank · 9 comment · 4 complexity · 1aa6d71478e57eeaf69e2075c6137dd6 MD5 · raw file

  1. class WINDOW_PALETTE1
  2. -- Draw color palette on sub_window.
  3. -- Mouse wheel is used to change blue value.
  4. -- Color rectangle drawing appears on the fly on the screen.
  5. -- Drawing is uninterruptible.
  6. inherit
  7. GRAPHIC
  8. creation {ANY}
  9. make
  10. feature {}
  11. sub_window: SUB_WINDOW
  12. make is
  13. local
  14. toplevel_window: TOPLEVEL_WINDOW
  15. do
  16. create toplevel_window.default_create
  17. toplevel_window.when_close_requested(agent vision.loop_stack.break)
  18. toplevel_window.set_background_color(black_color)
  19. toplevel_window.map
  20. toplevel_window.set_shrink(True)
  21. create draw_kit
  22. create sub_window.make(toplevel_window)
  23. sub_window.set_shrink(True)
  24. sub_window.set_expand(True)
  25. sub_window.map
  26. sub_window.when_wheel_up(agent shift(sub_window, 25))
  27. sub_window.when_wheel_down(agent shift(sub_window, -25))
  28. sub_window.when_expose(agent update(sub_window))
  29. vision.start
  30. end
  31. update (w: WINDOW) is
  32. do
  33. draw_palette(w, blue)
  34. end
  35. blue: INTEGER
  36. shift (w: WINDOW; offset: INTEGER) is
  37. local
  38. blue_value: INTEGER
  39. do
  40. blue_value := blue + offset
  41. if blue_value > 255 then
  42. blue_value := 255
  43. elseif blue_value < 0 then
  44. blue_value := 0
  45. end
  46. if blue_value /= blue then
  47. blue := blue_value
  48. w.refresh
  49. end
  50. end
  51. draw_kit: DRAW_KIT
  52. draw_palette (w: WINDOW; blue_value: INTEGER) is
  53. require
  54. w /= Void
  55. blue_value.in_range(0, 255)
  56. local
  57. red, green: INTEGER; max_red, max_green: INTEGER; color: COLOR; x, y: INTEGER
  58. width, height: INTEGER; area: RECT
  59. do
  60. draw_kit.set_drawable(w)
  61. -- This line is very important here. As w internal characteristic change on each call (while signaling expose), you HAVE TO reassign the window to the draw_kit.
  62. area := vision.expose_area.intersect(w.area)
  63. if not area.is_empty then
  64. width := (w.width / 256).ceiling.force_to_integer_32
  65. height := (w.height / 256).ceiling.force_to_integer_32
  66. from
  67. red := scale_from_pos(area.y, w.height, 256)
  68. max_red := scale_from_pos(area.y + area.height - 1, w.height, 256)
  69. max_green := scale_from_pos(area.x + area.width - 1, w.width, 256)
  70. until
  71. red > max_red -- red increase to the bottom
  72. loop
  73. y := (w.height * red / 256).floor.force_to_integer_32
  74. from
  75. green := scale_from_pos(area.x, w.width, 256)
  76. until
  77. green > max_green -- green increase to the right
  78. loop
  79. x := (w.width * green / 256).floor.force_to_integer_32
  80. create color.like_rgb_8(red, green, blue_value)
  81. draw_kit.set_color(color)
  82. draw_kit.fill_rectangle(x, y, width, height)
  83. green := green + 1
  84. end
  85. red := red + 1
  86. end
  87. end
  88. end
  89. scale_from_pos (pos, nb_pos, nb_scale: INTEGER): INTEGER is
  90. -- Search nearest inferior scale. Scale position can be
  91. -- computed like this: ((scale*nb_pos)/nb_scale).floor.force_to_integer_32
  92. -- This is equivalent to (scale * nb_pos) // nb_scale
  93. -- scale range from 0 to nb_scale-1
  94. require
  95. pos.in_range(0, nb_pos - 1)
  96. do
  97. Result := (pos * nb_scale / nb_pos).floor.force_to_integer_32
  98. if ((Result + 1) * nb_pos / nb_scale).floor.force_to_integer_32 <= pos then
  99. Result := Result + 1
  100. end
  101. ensure
  102. Result.in_range(0, nb_scale - 1)
  103. end
  104. end -- class WINDOW_PALETTE1