cd_canvas_coordinate_system.e 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. deferred class CD_CANVAS_COORDINATE_SYSTEM
  2. inherit
  3. CANVAS_DRAW
  4. feature {ANY}
  5. get_size_pixels: TUPLE[INTEGER, INTEGER]
  6. -- Returns the canvas size in pixels.
  7. local
  8. p: POINTER
  9. ix, iy: INTEGER
  10. tup: TUPLE[INTEGER, INTEGER]
  11. do
  12. int_canvas_get_size(cnvs, $ix, $iy, p, p)
  13. tup := [ix, iy]
  14. Result := tup
  15. end
  16. get_size_millimeters: TUPLE[REAL_64, REAL_64]
  17. -- Returns the canvas size in millimeters.
  18. local
  19. p: POINTER
  20. ix, iy: REAL_64
  21. tup: TUPLE[REAL_64, REAL_64]
  22. do
  23. int_canvas_get_size(cnvs, p, p, $ix, $iy)
  24. tup := [ix, iy]
  25. Result := tup
  26. end
  27. update_y_axis (y: INTEGER): INTEGER
  28. -- Invert the given Y coordinate if the native Y axis orientation is
  29. -- different from the CD axis orientation. The CD axis orientation is
  30. -- always bottom-top.
  31. local
  32. yc: INTEGER
  33. do
  34. yc := y
  35. Result := int_canvas_update_y_axis(cnvs, $yc)
  36. end
  37. invert_y_axis (y: INTEGER): INTEGER
  38. -- Invert the given Y coordinate independent of the driver Y axis
  39. -- orientation.
  40. do
  41. Result := int_canvas_invert_y_axis(cnvs, y)
  42. end
  43. convert_mm_to_pixels (mmx, mmy: REAL_64): TUPLE[INTEGER, INTEGER]
  44. -- Converts sizes in millimeters into pixels (canvas coordinates).
  45. -- Return integer values.
  46. local
  47. px, py: INTEGER
  48. tup: TUPLE[INTEGER, INTEGER]
  49. do
  50. int_canvas_mm_to_pixels(cnvs, mmx, mmy, $px, $py)
  51. tup := [px, py]
  52. Result := tup
  53. end
  54. convert_mm_to_pixels_real (mmx, mmy: REAL_64): TUPLE[REAL_64, REAL_64]
  55. -- Converts sizes in millimeters into pixels (canvas coordinates).
  56. -- Return real 64 values.
  57. local
  58. px, py: REAL_64
  59. tup: TUPLE[REAL_64, REAL_64]
  60. do
  61. int_canvas_c_double_mm_to_pixels(cnvs, mmx, mmy, $px, $py)
  62. tup := [px, py]
  63. Result := tup
  64. end
  65. convert_pixels_to_mm (px, py: INTEGER): TUPLE[REAL_64, REAL_64]
  66. -- Converts sizes in pixels (canvas coordinates as integer values) into
  67. -- millimeters. Use this function to obtain the horizontal and vertical
  68. -- resolution of the canvas by passing 1 as parameter in dx and dy. The
  69. -- resolution value is obtained using the formula res=1.0/mm.
  70. local
  71. mmx, mmy: REAL_64
  72. tup: TUPLE[REAL_64, REAL_64]
  73. do
  74. int_canvas_pixels_to_mm(cnvs, px, py, $mmx, $mmy)
  75. tup := [mmx, mmy]
  76. Result := tup
  77. end
  78. convert_pixels_to_mm_real (px, py: REAL_64): TUPLE[REAL_64, REAL_64]
  79. -- Converts sizes in pixels (canvas coordinates as real 64 values) into
  80. -- millimeters. Use this function to obtain the horizontal and vertical
  81. -- resolution of the canvas by passing 1 as parameter in dx and dy. The
  82. -- resolution value is obtained using the formula res=1.0/mm.
  83. local
  84. mmx, mmy: REAL_64
  85. tup: TUPLE[REAL_64, REAL_64]
  86. do
  87. int_canvas_c_double_pixels_to_mm(cnvs, px, py, $mmx, $mmy)
  88. tup := [mmx, mmy]
  89. Result := tup
  90. end
  91. set_origin (x, y: INTEGER)
  92. -- Allows translating the origin - for instance, to the center of the
  93. -- canvas. The function profits from the architecture of the library to
  94. -- simulate a translation of the origin, which in fact is never actually
  95. -- passed to the canvas in the respective driver. It is not related with
  96. -- WD nor Transformation Matrix. Default values: (0, 0)
  97. do
  98. int_canvas_origin(cnvs, x, y)
  99. end
  100. set_origin_real (x, y: REAL_64)
  101. -- As "set_origin" but the parematers are real 64 values.
  102. do
  103. int_canvas_c_double_origin(cnvs, x, y)
  104. end
  105. get_origin: TUPLE[INTEGER, INTEGER]
  106. -- Return the origin as integer values.
  107. local
  108. x, y: INTEGER
  109. do
  110. int_canvas_get_origin(cnvs, $x, $y)
  111. Result := [x, y]
  112. end
  113. get_origin_real: TUPLE[REAL_64, REAL_64]
  114. -- Return the origin as real 64 values.
  115. local
  116. x, y: REAL_64
  117. do
  118. int_canvas_c_double_get_origin(cnvs, $x, $y)
  119. Result := [x, y]
  120. end
  121. -- Transformation matrix
  122. set_transform (matrix: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64])
  123. -- Defines a transformation matrix with 6 elements. If the matrix is
  124. -- Void, the transformation is reset to the identity. Default value: Void.
  125. --
  126. -- The matrix contains scale (sx,sy), rotation (angle) and translation
  127. -- (dx,dy) elements as follows:
  128. --
  129. -- matrix[0] = sx*cos(angle) // Horizontal Scale and Rotation component
  130. -- matrix[1] = sin(angle) // Rotation component (can also contain an
  131. -- horizontal shear component)
  132. -- matrix[2] = -sin(angle) // Rotation component (can also contain a
  133. -- vertical shear component)
  134. -- matrix[3] = sy*cos(angle) // Vertical Scale and Rotation component
  135. -- matrix[4] = dx // Horizontal Translation component
  136. -- matrix[5] = dy // Vertical Translation component
  137. --
  138. -- Functions that retrieve images from the canvas are not affected by the
  139. -- transformation matrix, such as "get_image", "get_image_rgb" and
  140. -- "scroll_area".
  141. --
  142. -- Transformation matrix is independent of the World Coordinate and
  143. -- Origin functions. And those are affected if a transformation is set,
  144. -- just like other regular primitives.
  145. --
  146. -- The transformation matrix and world coordinates perform similar
  147. -- functions. World coordinates were developed before the transformation
  148. -- matrix support. The transformation matrix operates at a lower level
  149. -- than world coordinates, and, as such, might be faster, but might
  150. -- behave differently on different platforms. World coordinates behave
  151. -- consistently across platforms.
  152. local
  153. m: ARRAY[REAL_64]
  154. do
  155. create m.make_filled((0).to_double, 1, 6 + 1)
  156. m.put(matrix.real_64_item(1), 1)
  157. m.put(matrix.real_64_item(2), 2)
  158. m.put(matrix.real_64_item(3), 3)
  159. m.put(matrix.real_64_item(4), 4)
  160. m.put(matrix.real_64_item(5), 5)
  161. m.put(matrix.real_64_item(6), 6)
  162. int_canvas_transform(cnvs, get_pointer(m.to_c))
  163. end
  164. get_transform: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
  165. -- Returns the transformation matrix. If the identity is set,
  166. -- returns Void.
  167. local
  168. p: POINTER
  169. m: MANAGED_POINTER
  170. do
  171. p := int_canvas_get_transform(cnvs)
  172. if p /= default_pointer then
  173. create m.make_from_pointer(p, 6)
  174. Result := [m.read_real_64(0), m.read_real_64(1), m.read_real_64(2), m.read_real_64(3), m.read_real_64(4), m.read_real_64(5)]
  175. else
  176. io.put_string("Something goes wrong %N")
  177. Result := [(0).to_double, (0).to_double, (0).to_double, (0).to_double, (0).to_double, (0).to_double]
  178. end
  179. end
  180. apply_transform_multiply (matrix: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64])
  181. -- Left multiply the current transformation by the given transformation.
  182. local
  183. m: ARRAY[REAL_64]
  184. do
  185. create m.make_filled((0).to_double, 1, 4 + 1)
  186. m.put(matrix.real_64_item(1), 1)
  187. m.put(matrix.real_64_item(2), 2)
  188. m.put(matrix.real_64_item(3), 3)
  189. m.put(matrix.real_64_item(4), 4)
  190. int_canvas_transform_multiply(cnvs, get_pointer(m.to_c))
  191. end
  192. apply_transform_translate (dx, dy: REAL_64)
  193. -- Applies a translation to the current transformation.
  194. do
  195. int_canvas_transform_translate(cnvs, dx, dy)
  196. end
  197. apply_transform_scale (sx, sy: REAL_64)
  198. -- Applies a scale to the current transformation.
  199. do
  200. int_canvas_transform_scale(cnvs, sx, sy)
  201. end
  202. apply_transform_rotate (angle: REAL_64)
  203. -- Applies a rotation to the current transformation. Angle is in degrees,
  204. -- oriented counter-clockwise from the horizontal axis.
  205. do
  206. int_canvas_transform_rotate(cnvs, angle)
  207. end
  208. transform_point (x, y: INTEGER): TUPLE[INTEGER, INTEGER]
  209. -- Applies a transformation to a given point.
  210. local
  211. tx, ty: INTEGER
  212. do
  213. int_canvas_transform_point(cnvs, x, y, $tx, $ty)
  214. Result := [tx, ty]
  215. end
  216. transform_point_real (x, y: REAL_64): TUPLE[REAL_64, REAL_64]
  217. -- Like "transform_point" but with REAL_64 values.
  218. local
  219. tx, ty: REAL_64
  220. do
  221. int_canvas_c_double_transform_point(cnvs, x, y, $tx, $ty)
  222. Result := [tx, ty]
  223. end
  224. feature {NONE}
  225. int_canvas_get_size (wgt: POINTER; x, y, xm, ym: POINTER)
  226. external
  227. "C inline use %"eiffel-iup.h%""
  228. alias
  229. "cdCanvasGetSize ($wgt, $x, $y, $xm, $ym);"
  230. end
  231. int_canvas_update_y_axis (wgt, y: POINTER): INTEGER
  232. external
  233. "C inline use %"eiffel-iup.h%""
  234. alias
  235. "return cdCanvasUpdateYAxis ($wgt, $y);"
  236. end
  237. int_canvas_invert_y_axis (wgt: POINTER; yc: INTEGER): INTEGER
  238. external
  239. "C inline use %"eiffel-iup.h%""
  240. alias
  241. "return cdCanvasInvertYAxis ($wgt, $yc);"
  242. end
  243. int_canvas_mm_to_pixels (wgt: POINTER; mm_x, mm_y: REAL_64; cx, cy: POINTER)
  244. external
  245. "C inline use %"eiffel-iup.h%""
  246. alias
  247. "cdCanvasMM2Pixel ($wgt, $mm_x, $mm_y, $cx, $cy);"
  248. end
  249. int_canvas_c_double_mm_to_pixels (wgt: POINTER; mm_x, mm_y: REAL_64; cx, cy: POINTER)
  250. external
  251. "C inline use %"eiffel-iup.h%""
  252. alias
  253. "cdfCanvasMM2Pixel ($wgt, $mm_x, $mm_y, $cx, $cy);"
  254. end
  255. int_canvas_pixels_to_mm (wgt: POINTER; p_x, p_y: INTEGER; cx, cy: POINTER)
  256. external
  257. "C inline use %"eiffel-iup.h%""
  258. alias
  259. "cdCanvasPixel2MM ($wgt, $p_x, $p_y, $cx, $cy);"
  260. end
  261. int_canvas_c_double_pixels_to_mm (wgt: POINTER; p_x, p_y: REAL_64; cx, cy: POINTER)
  262. external
  263. "C inline use %"eiffel-iup.h%""
  264. alias
  265. "cdfCanvasPixel2MM ($wgt, $p_x, $p_y, $cx, $cy);"
  266. end
  267. int_canvas_origin (wgt: POINTER; x, y: INTEGER)
  268. external
  269. "C inline use %"eiffel-iup.h%""
  270. alias
  271. "cdCanvasOrigin ($wgt, $x, $y);"
  272. end
  273. int_canvas_c_double_origin (wgt: POINTER; x, y: REAL_64)
  274. external
  275. "C inline use %"eiffel-iup.h%""
  276. alias
  277. "cdfCanvasOrigin ($wgt, $x, $y);"
  278. end
  279. int_canvas_get_origin (wgt, x, y: POINTER)
  280. external
  281. "C inline use %"eiffel-iup.h%""
  282. alias
  283. "cdCanvasGetOrigin ($wgt, $x, $y);"
  284. end
  285. int_canvas_c_double_get_origin (wgt, x, y: POINTER)
  286. external
  287. "C inline use %"eiffel-iup.h%""
  288. alias
  289. "cdfCanvasGetOrigin ($wgt, $x, $y);"
  290. end
  291. int_canvas_transform (wgt, m: POINTER)
  292. external
  293. "C inline use %"eiffel-iup.h%""
  294. alias
  295. "cdCanvasTransform ($wgt, $m);"
  296. end
  297. int_canvas_get_transform (wgt: POINTER): POINTER
  298. external
  299. "C inline use %"eiffel-iup.h%""
  300. alias
  301. "return cdCanvasGetTransform ($wgt);"
  302. end
  303. int_canvas_transform_multiply (wgt, m: POINTER)
  304. external
  305. "C inline use %"eiffel-iup.h%""
  306. alias
  307. "cdCanvasTransformMultiply ($wgt, $m);"
  308. end
  309. int_canvas_transform_translate (wgt: POINTER; dx, dy: REAL_64)
  310. external
  311. "C inline use %"eiffel-iup.h%""
  312. alias
  313. "cdCanvasTransformTranslate ($wgt, $dx, $dy);"
  314. end
  315. int_canvas_transform_scale (wgt: POINTER; sx, sy: REAL_64)
  316. external
  317. "C inline use %"eiffel-iup.h%""
  318. alias
  319. "cdCanvasTransformScale ($wgt, $sx, $sy);"
  320. end
  321. int_canvas_transform_rotate (wgt: POINTER; ang: REAL_64)
  322. external
  323. "C inline use %"eiffel-iup.h%""
  324. alias
  325. "cdCanvasTransformRotate ($wgt, $ang);"
  326. end
  327. int_canvas_transform_point (wgt: POINTER; x, y: INTEGER; tx, ty: POINTER)
  328. external
  329. "C inline use %"eiffel-iup.h%""
  330. alias
  331. "cdCanvasTransformPoint ($wgt, $x, $y, $tx, $ty);"
  332. end
  333. int_canvas_c_double_transform_point (wgt: POINTER; x, y: REAL_64; tx, ty: POINTER)
  334. external
  335. "C inline use %"eiffel-iup.h%""
  336. alias
  337. "cdfCanvasTransformPoint ($wgt, $x, $y, $tx, $ty);"
  338. end
  339. end
  340. -- The MIT License (MIT)
  341. -- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias
  342. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  343. -- of this software and associated documentation files (the "Software"), to deal
  344. -- in the Software without restriction, including without limitation the rights
  345. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  346. -- copies of the Software, and to permit persons to whom the Software is
  347. -- furnished to do so, subject to the following conditions:
  348. --
  349. -- The above copyright notice and this permission notice shall be included in
  350. -- all copies or substantial portions of the Software.
  351. --
  352. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  353. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  354. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  355. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  356. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  357. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  358. -- SOFTWARE.