canvas_example.e 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. class CANVAS_EXAMPLE
  2. -- This example show how draw basic primitives at IUP_CANVAS (marks,
  3. -- lines, polygons, filled areas and text).
  4. insert
  5. IUP_INTERFACE
  6. create {ANY}
  7. make
  8. feature {ANY}
  9. angle: REAL_64
  10. canv: IUP_CANVAS
  11. make
  12. local
  13. gui: IUP
  14. i: STRING
  15. a: ARRAY[IUP_WIDGET]
  16. l: IUP_LABEL
  17. b: IUP_BUTTON
  18. v: IUP_VBOX
  19. w: IUP_DIALOG
  20. do
  21. gui := iup_open
  22. create l.label("First test of eiffel-cd!")
  23. create b.button("Rotate 30 degrees")
  24. b.set_cb_action(agent rotate_canvas(?))
  25. create canv.canvas
  26. canv.set_size(300, 150)
  27. canv.set_cb_map(agent map_canvas(?))
  28. canv.set_cb_canvas_action(agent draw(?, ?, ?))
  29. canv.set_cb_button(agent button(?,?,?,?,?,?))
  30. a := {ARRAY[IUP_WIDGET] 1, << l, b, canv >>}
  31. create v.vbox(a)
  32. v.set_alignment("ACENTER")
  33. v.set_gap(10)
  34. v.set_margin(10, 10)
  35. create w.dialog(v)
  36. w.set_title("CD test")
  37. i := w.show
  38. gui.main_loop
  39. gui.close
  40. end
  41. map_canvas (widget: IUP_CANVAS): STRING
  42. -- Create the internal canvas (CD_IUP) when the widget
  43. -- is mapped (IUP_CANVAS)
  44. local
  45. canvas: CD_IUP
  46. do
  47. create canvas.create_double_buffer_with_canvas(widget)
  48. Result := "IUP_DEFAULT"
  49. end
  50. draw (widget: IUP_CANVAS; x, y: REAL_32): STRING
  51. local
  52. ok: BOOLEAN
  53. canvas: CD_IUP
  54. do
  55. canvas := widget.get_cd_canvas
  56. ok := canvas.activate
  57. if ok then
  58. -- Rotate
  59. canvas.apply_transform_rotate(angle)
  60. -- Clear the canvas with a white background
  61. canvas.set_predefined_background_color(canvas.cd_white)
  62. canvas.clear
  63. canvas.set_interior_style_solid
  64. -- Draw two lines
  65. canvas.set_line_width(3)
  66. canvas.set_predefined_foreground_color(canvas.cd_blue)
  67. canvas.draws_line(40, 40, 450, 40)
  68. canvas.draws_line(45, 65, 455, 65)
  69. -- Draw a rect with dotted line
  70. canvas.set_line_style_dotted
  71. canvas.set_predefined_foreground_color(canvas.cd_red)
  72. canvas.draws_rect(60, 200, 80, 175)
  73. -- Draw an arc
  74. canvas.set_line_style_continuous
  75. canvas.draws_arc(150, 200, 100, 50, 0, 150)
  76. -- Draw a box (filled rect)
  77. canvas.set_predefined_foreground_color(canvas.cd_yellow)
  78. canvas.draws_box(240, 470, 80, 175)
  79. -- Draw a sector (filled arc)
  80. canvas.set_predefined_foreground_color(canvas.cd_magenta)
  81. canvas.draws_sector(260, 200, 100, 50, 0, 150)
  82. -- Draw a text
  83. --canvas.set_text_orientation(20)
  84. canvas.draws_text(370, 200, "Liberty Eiffel")
  85. -- Test of polygons
  86. canvas.set_line_width(1)
  87. -- Draw an open polygon
  88. canvas.set_predefined_foreground_color(canvas.cd_dark_red)
  89. canvas.begin_open_lines
  90. canvas.vertex(50,250)
  91. canvas.vertex(80,340)
  92. canvas.vertex(150,300)
  93. canvas.vertex(110,265)
  94. canvas.vertex(90,290)
  95. canvas.end_polygon
  96. -- Draw a closed polygon
  97. canvas.set_predefined_foreground_color(canvas.cd_cyan)
  98. canvas.begin_closed_lines
  99. canvas.vertex(200,250)
  100. canvas.vertex(230,340)
  101. canvas.vertex(300,300)
  102. canvas.vertex(260,265)
  103. canvas.vertex(240,290)
  104. canvas.end_polygon
  105. -- Draw a filled polygon
  106. canvas.set_predefined_foreground_color(canvas.cd_black)
  107. canvas.set_line_style_continuous
  108. canvas.set_hatch_backward_diagonal
  109. canvas.begin_fill
  110. canvas.vertex(350,250)
  111. canvas.vertex(380,340)
  112. canvas.vertex(450,300)
  113. canvas.vertex(410,265)
  114. canvas.vertex(390,290)
  115. canvas.end_polygon
  116. -- Draw three marks
  117. canvas.set_predefined_foreground_color(canvas.cd_green)
  118. canvas.draws_mark(100, 350)
  119. canvas.draws_mark(260, 156)
  120. canvas.draws_mark(358, 289)
  121. canvas.flush
  122. end
  123. Result := "IUP_DEFAULT"
  124. end
  125. button (widget: IUP_CANVAS; btn, prs, x, y: INTEGER; status: STRING): STRING
  126. -- Draws a mark at click coordinates.
  127. local
  128. --i: INTEGER
  129. --tup: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
  130. ok: BOOLEAN
  131. canvas: CD_IUP
  132. do
  133. canvas := widget.get_cd_canvas
  134. ok := canvas.activate
  135. --i := canvas.get_font_size
  136. --tup := canvas.get_text_bounds(100, 100, "Hello")
  137. if ok then
  138. canvas.set_predefined_foreground_color(canvas.cd_green)
  139. canvas.draws_mark(x, canvas.update_y_axis(y))
  140. canvas.flush
  141. end
  142. --io.put_integer(i)
  143. --io.put_new_line
  144. --io.put_string(tup.item_1.to_string + " " + tup.item_2.to_string + " " + tup.item_3.to_string + " " + tup.item_4.to_string + " " + tup.item_5.to_string + " " + tup.item_6.to_string + " " + tup.item_7.to_string + " " + tup.item_8.to_string)
  145. --io.put_new_line
  146. Result := "IUP_DEFAULT"
  147. end
  148. rotate_canvas (widget: IUP_WIDGET): STRING
  149. do
  150. angle := 5.to_real_64
  151. canv.redraw
  152. Result := "IUP_DEFAULT"
  153. end
  154. end -- class CAVAS_EXAMPLE