@@ -10,6 +10,7 @@ import colors from "../../colors"
1010import fixTwisted from "./fix-twisted"
1111import convertExpandingLineToPolygon from "./convert-expanding-line-to-polygon"
1212import clamp from "clamp"
13+ import getLandmarksWithTransform from "../../utils/get-landmarks-with-transform"
1314
1415const getRandomId = ( ) => Math . random ( ) . toString ( ) . split ( "." ) [ 1 ]
1516
@@ -206,6 +207,16 @@ export default (state: MainLayoutState, action: Action) => {
206207 pointIndex,
207208 } )
208209 }
210+ case "BEGIN_MOVE_KEYPOINT" : {
211+ const { region, keypointId } = action
212+ state = closeEditors ( state )
213+ state = saveToHistory ( state , "Move Keypoint" )
214+ return setIn ( state , [ "mode" ] , {
215+ mode : "MOVE_KEYPOINT" ,
216+ regionId : region . id ,
217+ keypointId,
218+ } )
219+ }
209220 case "ADD_POLYGON_POINT" : {
210221 const { polygon, point, pointIndex } = action
211222 const regionIndex = getRegionIndex ( polygon )
@@ -240,6 +251,22 @@ export default (state: MainLayoutState, action: Action) => {
240251 [ x , y ]
241252 )
242253 }
254+ case "MOVE_KEYPOINT" : {
255+ const { keypointId, regionId } = state . mode
256+ const [ region , regionIndex ] = getRegion ( regionId )
257+ if ( regionIndex === null ) return state
258+ return setIn (
259+ state ,
260+ [
261+ ...pathToActiveImage ,
262+ "regions" ,
263+ regionIndex ,
264+ "points" ,
265+ keypointId ,
266+ ] ,
267+ { ...( region : any ) . points [ keypointId ] , x, y }
268+ )
269+ }
243270 case "MOVE_REGION" : {
244271 const { regionId } = state . mode
245272 if ( regionId === "$$allowed_area" ) {
@@ -312,6 +339,20 @@ export default (state: MainLayoutState, action: Action) => {
312339 h : dh ,
313340 } )
314341 }
342+ case "RESIZE_KEYPOINTS" : {
343+ const { regionId, landmarks, centerX, centerY } = state . mode
344+ const distFromCenter = Math . sqrt (
345+ ( centerX - x ) ** 2 + ( centerY - y ) ** 2
346+ )
347+ const scale = distFromCenter / 0.15
348+ return modifyRegion ( regionId , {
349+ points : getLandmarksWithTransform ( {
350+ landmarks,
351+ center : { x : centerX , y : centerY } ,
352+ scale,
353+ } ) ,
354+ } )
355+ }
315356 case "DRAW_POLYGON" : {
316357 const { regionId } = state . mode
317358 const [ region , regionIndex ] = getRegion ( regionId )
@@ -541,6 +582,34 @@ export default (state: MainLayoutState, action: Action) => {
541582 } )
542583 break
543584 }
585+ case "create-keypoints" : {
586+ state = saveToHistory ( state , "Create Keypoints" )
587+ const [
588+ [ keypointsDefinitionId , { landmarks, connections } ] ,
589+ ] = ( Object . entries ( state . keypointDefinitions ) : any )
590+
591+ newRegion = {
592+ type : "keypoints" ,
593+ keypointsDefinitionId,
594+ points : getLandmarksWithTransform ( {
595+ landmarks,
596+ center : { x, y } ,
597+ scale : 1 ,
598+ } ) ,
599+ highlighted : true ,
600+ editingLabels : false ,
601+ id : getRandomId ( ) ,
602+ }
603+ state = setIn ( state , [ "mode" ] , {
604+ mode : "RESIZE_KEYPOINTS" ,
605+ landmarks,
606+ centerX : x ,
607+ centerY : y ,
608+ regionId : newRegion . id ,
609+ isNew : true ,
610+ } )
611+ break
612+ }
544613 default :
545614 break
546615 }
@@ -581,9 +650,13 @@ export default (state: MainLayoutState, action: Action) => {
581650 }
582651 }
583652 case "MOVE_REGION" :
653+ case "RESIZE_KEYPOINTS" :
584654 case "MOVE_POLYGON_POINT" : {
585655 return { ...state , mode : null }
586656 }
657+ case "MOVE_KEYPOINT" : {
658+ return { ...state , mode : null }
659+ }
587660 case "CREATE_POINT_LINE" : {
588661 return state
589662 }
0 commit comments