floatywindow.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. let SCRIPT_CONF = require('./common/ScriptConf.js')
  2. /**
  3. * 作者: 大柒
  4. * 日期: 20190307
  5. *
  6. */
  7. let floatyWindow = {}
  8. floatyWindow.isShow = false
  9. floatyWindow.showWindow = function () {
  10. toastLog(floatyWindow.isShow)
  11. if(floatyWindow.isShow){
  12. return
  13. }
  14. threads.start(function () {
  15. floatyWindow.isShow = true
  16. var window = floaty.rawWindow(
  17. ` <frame id="xx1" h="875px" w="150px" >
  18. <img id="cd_1" layout_gravity="bottom|center" w="100px" h="100px" src="file://./start.png" rotation="-90" />
  19. <img id="cd_2" layout_gravity="bottom|center" w="100px" h="100px" src="file://./resume.png" rotation="-90" />
  20. <img id="cd_3" layout_gravity="bottom|center" w="100px" h="100px" src="file://./back.png" rotation="-90" />
  21. </frame>
  22. `);
  23. var window1 = floaty.window(
  24. ` <frame h="150px" w="150px" >
  25. <img id="jia" layout_gravity="bottom|left" w="150px" h="150px" src="file://./test.png" />
  26. </frame>`
  27. );
  28. //cd1值 菜单的默认加载位置 b,d,e 跟随C值 这个自己摸索
  29. //jiaodu值 加载图标的旋转开始角度
  30. //sleep0值 动画时间 0为无延迟 数值越小 加载越快 反之则慢
  31. //date0_sj值 按下手指不移动时等待多少毫秒关闭悬浮窗
  32. //move0值 手指移动的距离小于该值 则判断为 未移动
  33. //date1_sj 值 手指触摸时间小于该值 则判断为点击
  34. var cd1 = 180;
  35. var jiaodu = 0;
  36. var sleep0 = 1;
  37. var date0_sj = 1000;
  38. var move0 = 60;
  39. var date1_sj = 200;
  40. var y2 = false,
  41. y1 = true;
  42. let cd2 = cd1, cd3 = cd1, cd4 = cd1
  43. let windowWidth
  44. let windowHeight
  45. let window1Width
  46. let window1Height
  47. ui.post(function () {
  48. windowWidth = window.getWidth();
  49. windowHeight = window.getHeight();
  50. window1Width = window1.getWidth();
  51. window1Height = window1.getHeight();
  52. }, 1000);
  53. window.cd_1.setPivotY(cd1)
  54. window.cd_1.setPivotX(cd1)
  55. window.cd_2.setPivotY(cd2)
  56. window.cd_2.setPivotX(cd2)
  57. window.cd_3.setPivotY(cd3)
  58. window.cd_3.setPivotX(cd3)
  59. window.setTouchable(false);
  60. //两个悬浮窗Y值差值
  61. var Y_cz = 800
  62. //两个悬浮窗初始位置
  63. window.setPosition(50, device.height - (400 + Y_cz))
  64. window1.setPosition(50, device.height - 400)
  65. function jz() {
  66. threads.start(function () {
  67. if (y1) {
  68. y1 = false
  69. if (y2) {
  70. y2 = false
  71. for (let i = 0; i < 100; i++) {
  72. if (i % 2) {
  73. window1.jia.setRotation(jiaodu += 1)
  74. }
  75. window.cd_1.setPivotY(cd1 += 4)
  76. window.cd_1.setPivotX(cd1)
  77. window.cd_2.setPivotY(cd2 += 3)
  78. window.cd_2.setPivotX(cd2)
  79. window.cd_3.setPivotY(cd3 += 2)
  80. window.cd_3.setPivotX(cd3)
  81. sleep(sleep0)
  82. }
  83. } else {
  84. for (let i = 100; i > 0; i--) {
  85. if (i % 2) {
  86. window1.jia.setRotation(jiaodu -= 1)
  87. }
  88. window.cd_1.setPivotY(cd1 -= 4)
  89. window.cd_1.setPivotX(cd1)
  90. window.cd_2.setPivotY(cd2 -= 3)
  91. window.cd_2.setPivotX(cd2)
  92. window.cd_3.setPivotY(cd3 -= 2)
  93. window.cd_3.setPivotX(cd3)
  94. sleep(sleep0)
  95. }
  96. y2 = true;
  97. }
  98. y1 = true
  99. }
  100. })
  101. }
  102. window.cd_1.on("click", () => {
  103. if (y2) {
  104. if('myEngine' in SCRIPT_CONF){
  105. if (!SCRIPT_CONF.myEngine.getEngine().isDestroyed()) {
  106. toast(`任务运行中: ${SCRIPT_CONF.module}`)
  107. return
  108. }
  109. }
  110. try {
  111. let obj = SCRIPT_CONF.script
  112. let obj1 = SCRIPT_CONF.conf
  113. let obj2 = SCRIPT_CONF.module
  114. let obj3 = "const ENV_CONF = " + JSON.stringify(obj1) + ";\r\n";
  115. obj3 += obj
  116. SCRIPT_CONF.myEngine = engines.execScript(obj2, obj3)
  117. } catch (err) {
  118. }
  119. jz();
  120. window.setTouchable(false);
  121. }
  122. })
  123. window.cd_2.on("click", () => {
  124. if (y2) {
  125. if('myEngine' in SCRIPT_CONF){
  126. if(!SCRIPT_CONF.myEngine.getEngine().isDestroyed()){
  127. SCRIPT_CONF.myEngine.getEngine().forceStop()
  128. toastLog(`已停止任务:${SCRIPT_CONF.module}`)
  129. }
  130. }
  131. jz();
  132. window.setTouchable(false);
  133. }
  134. })
  135. window.cd_3.on("click", () => {
  136. if (y2) {
  137. if('myEngine' in SCRIPT_CONF){
  138. if(!SCRIPT_CONF.myEngine.getEngine().isDestroyed()){
  139. SCRIPT_CONF.myEngine.getEngine().forceStop()
  140. toastLog(`已停止任务:${SCRIPT_CONF.module}`)
  141. }
  142. }
  143. threads.start(function(){
  144. floatyWindow.hideWindow()
  145. })
  146. }
  147. })
  148. var fn = () => {
  149. }
  150. var id = setInterval(
  151. fn, 3000
  152. )
  153. //记录按键被按下时的触摸坐标
  154. var x = 0,
  155. y = 0;
  156. //记录按键被按下时的悬浮窗位置
  157. var windowX, windowY;
  158. //记录按键被按下的时间以便判断长按等动作
  159. var downTime;
  160. yd = false;
  161. window1.jia.setOnTouchListener(function (view, event) {
  162. switch (event.getAction()) {
  163. case event.ACTION_DOWN:
  164. x = event.getRawX();
  165. y = event.getRawY();
  166. windowX = window.getX();
  167. windowY = window.getY();
  168. windowX1 = window1.getX();
  169. windowY1 = window1.getY();
  170. downTime = new Date().getTime();
  171. return true;
  172. case event.ACTION_MOVE:
  173. if (!yd) { //如果移动的距离大于h值 则判断为移动 yd为真
  174. if (Math.abs(event.getRawY() - y) > move0 || Math.abs(event.getRawX() - x) > move0) {
  175. yd = true
  176. }
  177. //如果按下的时间超过g值并且yd值为假判断为长按,退出脚本
  178. if (new Date().getTime() - downTime > date0_sj) {
  179. log(new Date().getTime() - downTime);
  180. exit();
  181. //关闭悬浮窗
  182. //window.close()
  183. //window1.close()
  184. }
  185. } else { //移动手指时调整两个悬浮窗位置
  186. windowHeight = window1Height
  187. windowWidth = window1Width
  188. let xAxisMovingDistance = event.getRawX() - x;
  189. let yAxisMovingDistance = event.getRawY() - y;
  190. if (windowX + xAxisMovingDistance < 0) {
  191. if (windowY + yAxisMovingDistance < 0) {
  192. window.setPosition(10,
  193. windowY + (event.getRawY() - y));
  194. window1.setPosition(10,
  195. windowY + ((event.getRawY() - y) + Y_cz));
  196. console.log("触发了8")
  197. } else if (windowY + windowHeight + yAxisMovingDistance > device.height) {
  198. window.setPosition(10,
  199. device.height - windowHeight);
  200. window1.setPosition(10,
  201. device.height - windowHeight + Y_cz);
  202. console.log("触发了7")
  203. // window.setPosition(0, );
  204. } else {
  205. console.log("触发了6")
  206. window.setPosition(10,
  207. windowY + yAxisMovingDistance);
  208. window1.setPosition(10,
  209. windowY + yAxisMovingDistance + Y_cz);
  210. // window.setPosition(0, windowY + yAxisMovingDistance);
  211. }
  212. } else if (windowX + xAxisMovingDistance > device.width - windowWidth) {
  213. if (windowY + yAxisMovingDistance < 0) {
  214. console.log("触发了5")
  215. window.setPosition(device.width - windowWidth,
  216. windowY + yAxisMovingDistance);
  217. window1.setPosition(device.width - windowWidth,
  218. windowY + yAxisMovingDistance + Y_cz);
  219. } else if (windowY + windowHeight + yAxisMovingDistance > device.height) {
  220. console.log("触发了4")
  221. window.setPosition(device.width - windowWidth,
  222. device.height - window1Height);
  223. window1.setPosition(device.width - windowWidth,
  224. device.height - windowHeight + Y_cz);
  225. // window.setPosition(device.width - windowWidth, device.height - windowHeight);
  226. } else {
  227. console.log("触发了3")
  228. window.setPosition(device.width - windowWidth,
  229. windowY + yAxisMovingDistance);
  230. window1.setPosition(device.width - windowWidth,
  231. windowY + yAxisMovingDistance + Y_cz);
  232. // window.setPosition(device.width - windowWidth, windowY + yAxisMovingDistance);
  233. }
  234. } else {
  235. if (windowY + yAxisMovingDistance < 0) {
  236. console.log("触发了1")
  237. window.setPosition(windowX + xAxisMovingDistance,
  238. 0);
  239. window1.setPosition(windowX + xAxisMovingDistance,
  240. 0 + Y_cz);
  241. // window.setPosition(device.width - windowWidth, windowY + yAxisMovingDistance);
  242. // window.setPosition(windowX + xAxisMovingDistance, 0);
  243. } else if (windowY1 + windowHeight + yAxisMovingDistance + 50 > device.height) {
  244. console.log("触发了2")
  245. window.setPosition(windowX + xAxisMovingDistance,
  246. device.height - windowHeight - Y_cz);
  247. window1.setPosition(windowX + xAxisMovingDistance,
  248. device.height - windowHeight - 50);
  249. // window.setPosition(windowX + xAxisMovingDistance, device.height - windowHeight);
  250. } else {
  251. window.setPosition(windowX + xAxisMovingDistance,
  252. windowY + yAxisMovingDistance);
  253. window1.setPosition(windowX + xAxisMovingDistance,
  254. windowY + yAxisMovingDistance + Y_cz);
  255. // window.setPosition(windowX + xAxisMovingDistance, windowY + yAxisMovingDistance);
  256. }
  257. }
  258. }
  259. return true;
  260. case event.ACTION_UP:
  261. //触摸时间小于 200毫秒 则判断为 点击
  262. if (new Date().getTime() - downTime < date1_sj) {
  263. jz();
  264. //根据Y2值 设置悬浮窗是否可以触摸
  265. if (y2) {
  266. window.setTouchable(false);
  267. } else {
  268. window.setTouchable(true);
  269. }
  270. }
  271. yd = false
  272. return true;
  273. }
  274. return true;
  275. });
  276. })
  277. }
  278. floatyWindow.hideWindow = function () {
  279. floatyWindow.isShow = false
  280. floaty.closeAll()
  281. }
  282. // floatyWindow.showWindow()
  283. module.exports = floatyWindow;