SGraphyScene.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * iFHS7.
  5. * ;BBMBMBMc rZMBMBR BMB
  6. * MBEr:;PBM, 7MBMMEOBB: BBB RBW
  7. * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2;
  8. * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB
  9. * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv
  10. * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc
  11. * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7
  12. * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2
  13. * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO;
  14. * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB
  15. * :0BMRDG RESSSKR. 2WOMBW; BMBMR
  16. * i0BM: SWKHKGO MBDv
  17. * .UB OOGDM. MK, Copyright (c) 2015-2018. 斯伯坦机器人世界
  18. * , XMW ..
  19. * r All rights reserved.
  20. *
  21. * ********************************************************************************************************************
  22. */
  23. import SPoint from './types/SPoint'
  24. import SRect from './types/SRect'
  25. import SGraphyItem from './SGraphyItem'
  26. const toGrabItemMotionEvent = Symbol('toGrabItemMotionEvent')
  27. /**
  28. * SGraphy图形引擎场景类
  29. *
  30. * @author Andy
  31. */
  32. export default class SGraphyScene {
  33. /**
  34. * 构造函数
  35. */
  36. constructor() {
  37. this.view = null
  38. console.log(this, 'this')
  39. this.root = new SGraphyItem(null)
  40. this.root._scene = this
  41. this.pos = new SPoint(0, 0)
  42. this.scale = new SPoint(1, 1)
  43. this.grabItem = null
  44. } // Function constructor()
  45. /**
  46. * 添加item对象到场景。
  47. *
  48. * @param item 添加的对象
  49. */
  50. addItem(item) {
  51. item.parent = this.root
  52. } // Function addItem()
  53. /**
  54. * 从场景中移除Item。
  55. *
  56. * @param item 被移除的对象
  57. */
  58. removeItem(item) {
  59. item.parent = null
  60. } // Function removeItem()
  61. /**
  62. * 绘制场景
  63. *
  64. * @param canvas 画布
  65. * @param rect 更新绘制区域
  66. */
  67. drawScene(canvas, rect) {
  68. this.root.onDraw(canvas, rect)
  69. } // Function drawScene()
  70. /**
  71. * 绘制背景
  72. *
  73. * @param canvas 画布
  74. * @param rect 更新绘制区域
  75. */
  76. drawBackground(canvas, rect) {
  77. // DO NOTHING
  78. } // Function drawBackground()
  79. /**
  80. * 绘制前景
  81. *
  82. * @param canvas 画布
  83. * @param rect 更新绘制区域
  84. */
  85. drawForeground(canvas, rect) {
  86. // DO NOTHING
  87. } // Function drawForeground()
  88. /**
  89. * 返回场景的item边界。即所有item边界的并集。
  90. *
  91. * @return SRect
  92. */
  93. worldRect() {
  94. let rect = null
  95. for (let item of this.root.children) { // 依次取item列中的所有item。将所有item的边界做并焦处理。
  96. if (rect == null) {
  97. rect = item.boundingRect().adjusted(item.pos)
  98. } else {
  99. rect.union(item.boundingRect().adjusted(item.pos))
  100. }
  101. }
  102. return rect
  103. } // Function worldsRect()
  104. /**
  105. * 更新
  106. */
  107. update() {} // Function update()
  108. // ===================================================================================================================
  109. // 事件
  110. /**
  111. * 鼠标单击事件
  112. *
  113. * @param e 保存事件参数
  114. * @return boolean
  115. */
  116. onClick(e) {
  117. if (this.grabItem != null) {
  118. return this.grabItem.onClick(this[toGrabItemMotionEvent](this.grabItem, e))
  119. }
  120. return this.root.onClick(e)
  121. } // onClick
  122. /**
  123. * 鼠标双击事件
  124. *
  125. * @param e 保存事件参数
  126. * @return boolean
  127. */
  128. onDbClick(e) {
  129. if (this.grabItem != null) {
  130. return this.grabItem.onDbClick(this[toGrabItemMotionEvent](this.grabItem, e))
  131. }
  132. return this.root.onDbClick(e)
  133. } // onClick
  134. /**
  135. * 鼠标按下事件
  136. *
  137. * @param e 保存事件参数
  138. * @return boolean
  139. */
  140. onMouseDown(e) {
  141. if (this.grabItem != null) {
  142. return this.grabItem.onMouseDown(this[toGrabItemMotionEvent](this.grabItem, e))
  143. }
  144. return this.root.onMouseDown(e)
  145. } // onMouseDown
  146. /**
  147. * 鼠标移动事件
  148. *
  149. * @param e 保存事件参数
  150. * @return boolean
  151. */
  152. onMouseMove(e) {
  153. if (this.grabItem != null) {
  154. return this.grabItem.onMouseMove(this[toGrabItemMotionEvent](this.grabItem, e))
  155. }
  156. return this.root.onMouseMove(e)
  157. } // onMouseMove
  158. /**
  159. * 释放鼠标事件
  160. *
  161. * @param e 保存事件参数
  162. * @return boolean
  163. */
  164. onMouseUp(e) {
  165. if (this.grabItem != null) {
  166. return this.grabItem.onMouseUp(this[toGrabItemMotionEvent](this.grabItem, e))
  167. }
  168. return this.root.onMouseUp(e)
  169. } // onMouseUp
  170. /**
  171. * 转换场景事件坐标到指定Item坐标事件
  172. *
  173. * @param item 指定的item对象
  174. * @param e 场景事件
  175. * @return {}
  176. */
  177. [toGrabItemMotionEvent](item, e) {
  178. let se = {...e }
  179. let p = item.mapFromScene(e.x, e.y)
  180. se.x = p.x
  181. se.y = p.y
  182. return se
  183. } // Function toGrabItemMotionEvent()
  184. } // Class SGraphyScene