Advertisement
Qugurun

zoom

Aug 28th, 2018
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.57 KB | None | 0 0
  1. --Скрыть StatusBar
  2. display.setStatusBar (display.HiddenStatusBar)
  3.  
  4. --параметры экрана
  5. _W = display.contentWidth
  6. _H = display.contentHeight
  7.  
  8. --пивот по умолчанию
  9. display.setDefault( "anchorX", 0 )
  10. display.setDefault( "anchorY", 0 )
  11. -- display.setDefault( "background", 1 )
  12.  
  13.  
  14.  
  15. --активация мультитача
  16. system.activate( "multitouch" )
  17.  
  18. --создаём изображение
  19. local img = display.newImage("image.jpg")
  20.  
  21. --количество нажатий
  22. local touch_count = 0
  23.  
  24. --для хранения данных о тапе
  25. local touch_id = {}
  26.  
  27. --стартовая дистанция между тапами
  28. local oldDistance = nil
  29.  
  30. --текущая дистанция между тапами
  31. local newDistance = nil
  32.  
  33. --объект для отображения середины между тапами
  34. local circle_green = display.newCircle(-10,-10,10)
  35.     circle_green.anchorX, circle_green.anchorY = 0.5, 0.5
  36.     circle_green.fill = {0,1,0}
  37.  
  38. --объект для зума
  39. local target = img
  40.  
  41.  
  42. function zoom ( event )
  43.     local phase = event.phase
  44.     local x,y = 0,0
  45.     local xNow,yNow
  46.     ---------------------------------------------------
  47.     --для перемещения по 1 тапу
  48.     local function tapBegan(event)
  49.         if (event.x > target.x) then
  50.             target.offsetX = event.x - target.x
  51.         elseif (event.x < target.x) then
  52.             target.offsetX = target.x - event.x
  53.         end
  54.  
  55.         if (event.y > target.y) then
  56.             target.offsetY = event.y - target.y
  57.         elseif (event.y < target.y) then
  58.             target.offsetY = target.y - event.y
  59.         end
  60.     end
  61.  
  62.     local function tapMoved(event)
  63.         if (event.x > target.x) then
  64.             target.x = event.x - target.offsetX
  65.         elseif (event.x < target.x) then
  66.             target.x = event.x + target.offsetX
  67.         end
  68.  
  69.         if (event.y > target.y) then
  70.             target.y = event.y - target.offsetY
  71.         elseif (event.y < target.y) then
  72.             target.y = event.y + target.offsetY
  73.         end
  74.     end
  75.     ---------------------------------------------------
  76.  
  77.     --длина отрезка
  78.     local function getDistance(x1,y1,x2,y2)
  79.         return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
  80.     end
  81.  
  82.     --середина на отрезке
  83.     local function centerDistance(x1,y1,x2,y2)  
  84.         local x,y = 0,0
  85.         --Y
  86.         if y2 > y1 then
  87.             y = (y2-y1)/2 + y1
  88.         else
  89.             y = (y1-y2)/2 + y2
  90.         end
  91.         --X
  92.         if x2 > x1 then
  93.             x = (x2-x1)/2 + x1
  94.         else
  95.             x = (x1-x2)/2 + x2
  96.         end
  97.         return x,y
  98.     end
  99.  
  100.     local function reposAnchor(x,y)
  101.         local posX, posY = x, y
  102.         local sizeX, sizeY = target.contentWidth, target.contentHeight
  103.         local xDiff, yDiff = posX - target.x , posY - target.y
  104.  
  105.         target.anchorX = target.anchorX + xDiff / sizeX
  106.         target.anchorY = target.anchorY + yDiff / sizeY
  107.         target.x, target.y = posX, posY
  108.  
  109.         -- debug:log( target.anchorX.."|"..target.anchorY)
  110.     end
  111.  
  112.     --обновление данных о тапе
  113.     local function update()
  114.         for i=1,touch_count do
  115.             if touch_id[i].id == event.id then
  116.                 touch_id[i].xNew=event.x
  117.                 touch_id[i].yNew=event.y
  118.             end
  119.         end
  120.     end
  121.  
  122.     if phase == "began" then
  123.         --увеличиваем количество тапов
  124.         touch_count = touch_count + 1
  125.  
  126.         --заносим.обновляем в таблицу данные о тапе
  127.         touch_id[touch_count]=
  128.             {
  129.                 id=event.id,
  130.                 xStart=event.xStart,
  131.                 yStart=event.yStart,
  132.                 xNew=event.x,
  133.                 yNew=event.y
  134.             }
  135.         --если 2 тапа
  136.         if touch_count == 2 then
  137.             --запоминаем стартовую дистанцию   
  138.             oldDistance = getDistance(touch_id[1].xStart, touch_id[1].yStart, touch_id[2].xStart, touch_id[2].yStart)
  139.             --распологаем зеленую точку
  140.             x,y = centerDistance(touch_id[1].xNew,touch_id[1].yNew,touch_id[2].xNew,touch_id[2].yNew)
  141.             circle_green.x,circle_green.y = x,y
  142.  
  143.             --устанавливаем анкор и поозицию
  144.             reposAnchor(x,y)
  145.  
  146.         elseif touch_count == 1 then
  147.             -- начало перемещения одним тапом
  148.             tapBegan(event)
  149.         end
  150.  
  151.     elseif phase == "moved" then
  152.         --обновляем данные о тапах
  153.         update()
  154.  
  155.         if touch_count == 1 then
  156.             --перемещение одним тапом
  157.             tapMoved(event)
  158.    
  159.         elseif touch_count == 2 then
  160.                 --если два тапа
  161.  
  162.             --получаем новую дистанцию
  163.             newDistance = getDistance(touch_id[1].xNew,touch_id[1].yNew,touch_id[2].xNew,touch_id[2].yNew)
  164.            
  165.             --устанавливаем позицию для отладочной точки
  166.             circle_green.x,circle_green.y = centerDistance(touch_id[1].xNew,touch_id[1].yNew,touch_id[2].xNew,touch_id[2].yNew)
  167.             circle_green.alpha = 1
  168.  
  169.  
  170.             --увеличение
  171.             if newDistance > oldDistance then
  172.                 if target.xScale < 3 then
  173.                     target.xScale = target.xScale + 0.01
  174.                     target.yScale = target.yScale + 0.01
  175.                     newDistance = newDistance - 0.05
  176.                 else
  177.                     target.xScale = 3
  178.                     target.yScale = 3
  179.                 end
  180.             else
  181.             --уменьшение
  182.                 if target.xScale > 1 then
  183.                     target.xScale = target.xScale - 0.01
  184.                     target.yScale = target.yScale - 0.01
  185.                     newDistance = newDistance + 0.2
  186.                 else
  187.                     target.xScale = 1
  188.                     target.yScale = 1  
  189.                 end
  190.             end
  191.         end
  192.     elseif phase == "cancelled" or phase == "ended" then
  193.        
  194.         --уменьшаем количество тапов
  195.         touch_count = touch_count - 1
  196.  
  197.         --скрываем зеленую точку
  198.         circle_green.alpha = 0
  199.     end
  200.     return true
  201. end
  202.  
  203. --листинер для зума
  204. Runtime:addEventListener("touch", zoom)
  205. -- target:addEventListener("touch", zoom)
  206.  
  207. require("debug_view")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement