/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct Obj2DPosStr = [ OB2DP_father : SO3_OBJECT, OB2DP_iFatherMode : I, OB2DP_tPos : [F F], OB2DP_tRatiosize : [I I I I], OB2DP_tAlign : [I I], OB2DP_fDist : F, OB2DP_iDistType : I, OB2DP_bTarget : I, OB2DP_tLastPos : [I I F], OB2DP_tInitPos : [[F F F] [F F F F]], OB2DP_bTranslation : I ]mkObj2DPosStr;; fun deleteOb(inst, obstr)= setPluginInstanceCbCameraChange inst nil; setPluginInstanceCbScenePreRender2 inst nil; let obstr.OB2DP_tInitPos -> [cpos cquat] in ( SO3ObjectSetGlobalPosition obstr.OB2DP_father cpos; SO3ObjectSetGlobalOrientation obstr.OB2DP_father cquat; ); 0;; fun getDistFromCamera(cpos)= let V3DgetSessionView c3dXsession -> viewstr in let V3DgetDefaultViewport viewstr -> viewportstr in let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in getVectorDistanceF (SO3ObjectGetGlobalPosition camera) cpos;; fun get2DPosFrom2DCoords(x, y, px, py, ox, oy, ax, ay)= let V3DgetSessionView c3dXsession -> viewstr in let V3DgetDefaultViewport viewstr -> viewportstr in let V3DgetViewportSize viewstr viewportstr -> [_ _ vw vh] in let if px then (ftoi (x *. (itof vw)) /. 100.0) + ox else ftoi x -> posx in let if py then (ftoi (y *. (itof vh)) /. 100.0) + oy else ftoi y -> posy in let if ax == 1 then vw / 2 + posx else if ax == 2 then vw - posx else posx -> posx in let if ay == 1 then vh / 2 + posy else if ay == 2 then vh - posy else posy -> posy in [posx posy];; fun getObject3DPosFrom2DPos(posx, posy, dist, disttype)= let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let SO3ViewportGetWorldPosFromPixelPosition viewportstr.V3D_viewport posx posy dist -> [pos _] in if (disttype == 0) then ( let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in let SO3ObjectGetGlobalPosition camera -> campos in let SO3ObjectGetGlobalOrientation camera -> camquat in let SO3MathsQuatGetDirection camquat [0.0 0.0 (-.dist)] -> camdir in let addVectorF (SO3ObjectGetGlobalPosition camera) camdir -> planepos in ( //addLogMessageVector3F "campos: " campos; //addLogMessageVector3F "camdir: " camdir; //addLogMessageVector3F "planepos: " planepos; vectorPlaneIntersectionF (subVectorF campos pos) campos camdir planepos; ); ) else pos;; fun getObject3DPosFrom2DCoords(x, y, px, py, ox, oy, ax, ay, dist, disttype)= let get2DPosFrom2DCoords x y px py ox oy ax ay -> [posx posy] in getObject3DPosFrom2DPos posx posy dist disttype;; fun cbSetPosition(inst, from, action, param, reply, obstr)= let obstr.OB2DP_tAlign -> [xalign yalign] in let if (!strcmp "" (strtrim param)) || param == nil then ( let obstr.OB2DP_tPos -> [x y] in let obstr.OB2DP_tRatiosize -> [px py ox oy] in let get2DPosFrom2DCoords x y px py ox oy xalign yalign -> [posx posy] in [posx posy obstr.OB2DP_fDist (getObject3DPosFrom2DPos posx posy obstr.OB2DP_fDist obstr.OB2DP_iDistType)]; ) else ( let strextr param -> lp in let (nth_list (hd lp) 0) -> sx in let (nth_list (hd lp) 1) -> sy in let atoi (nth_list (hd lp) 2) -> ox in let atoi (nth_list (hd lp) 3) -> oy in let atof (nth_list (hd lp) 4) -> dist in let atof sx -> x in let atof sy -> y in let [(if x == nil then 0.0 else x) (if y == nil then 0.0 else y)] -> [x y] in let if (!strcmp "%" (substr sx ((strlen sx) - 1) 1)) then 1 else 0 -> px in let if (!strcmp "%" (substr sy ((strlen sy) - 1) 1)) then 1 else 0 -> py in let if (ox == nil) && (oy == nil) then let obstr.OB2DP_tRatiosize -> [_ _ ox oy] in [ox oy] else let [(if ox == nil then 0 else ox) (if oy == nil then 0 else oy)] -> [ox oy] in [ox oy] -> [ox oy] in let if (dist != nil) then dist else obstr.OB2DP_fDist -> dist in let get2DPosFrom2DCoords x y px py ox oy xalign yalign -> [posx posy] in ( [posx posy dist (getObject3DPosFrom2DPos posx posy dist obstr.OB2DP_iDistType)]; ); ) -> [posx posy dist cpos] in ( SO3ObjectSetGlobalPosition obstr.OB2DP_father cpos; set obstr.OB2DP_tLastPos = [posx posy dist]; if (!obstr.OB2DP_bTarget) then nil else ( let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let SO3ViewportGetCamera viewportstr.V3D_viewport -> camera in let if (obstr.OB2DP_iDistType == 0) then SO3ObjectGetGlobalOrientation camera else SO3ObjectGetNodeFacingOrientation obstr.OB2DP_father camera nil nil -> quat in SO3ObjectSetGlobalOrientation obstr.OB2DP_father quat; ); ); 0;; fun cbGetPosition(inst, from, action, param, reply, obstr)= let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let SO3ObjectGetGlobalPosition obstr.OB2DP_father -> pos in let SO3ViewportGetPixelPositionFromWorldPos viewportstr.V3D_viewport pos -> [posx posy] in let getDistFromCamera (SO3ObjectGetGlobalPosition obstr.OB2DP_father) -> dist in SendPluginEvent inst "Position" strcatnSep (itoa posx)::(itoa posy)::(ftoa dist)::nil " " nil; 0;; fun cbGetPreRender(inst, sessionstr, etime, obstr)= let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let SO3ObjectGetGlobalPosition obstr.OB2DP_father -> pos in let SO3ViewportGetPixelPositionFromWorldPos viewportstr.V3D_viewport pos -> [posx posy] in let getDistFromCamera (SO3ObjectGetGlobalPosition obstr.OB2DP_father) -> dist in ( if (!obstr.OB2DP_bTranslation) then nil else let obstr.OB2DP_tLastPos -> [lposx lposy ldist] in let [(posx - lposx) (posy - lposy) (dist -. ldist)] -> [tx ty tz] in SendPluginEvent inst "Translation" strcatnSep (itoa tx)::(itoa ty)::(ftoa tz)::nil " " nil; set obstr.OB2DP_tLastPos = [posx posy dist]; ); 0;; fun cbChangeCamera(inst, viewstr, sessionstr, camera, obstr)= let V3DgetCameraByType sessionstr camera obstr.OB2DP_iFatherMode -> nfather in let SO3ObjectGetGlobalPosition nfather -> cpos in let SO3ObjectGetGlobalOrientation nfather -> cquat in ( set obstr.OB2DP_father = nfather; set obstr.OB2DP_tInitPos = [cpos cquat]; ); 0;; fun cbSetObject(inst, from, action, param, reply, obstr)= if param == nil then nil else let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let V3DgetObjectByName c3dXsession param -> father in let V3DgetObjectTypeByName param -> iobjmode in let SO3ObjectGetGlobalPosition father -> cpos in let SO3ObjectGetGlobalOrientation father -> cquat in let SO3ViewportGetPixelPositionFromWorldPos viewportstr.V3D_viewport cpos -> [lposx lposy] in let getDistFromCamera cpos -> lastdist in ( set obstr.OB2DP_father = father; set obstr.OB2DP_iFatherMode = iobjmode; set obstr.OB2DP_tLastPos = [lposx lposy lastdist]; set obstr.OB2DP_tInitPos = [cpos cquat]; if !iobjmode then nil else setPluginInstanceCbCameraChange inst mkfun5 @cbChangeCamera obstr; ); 0;; fun cbResetObject(inst, from, action, param, reply, obstr)= let obstr.OB2DP_tInitPos -> [cpos cquat] in ( SO3ObjectSetGlobalPosition obstr.OB2DP_father cpos; SO3ObjectSetGlobalOrientation obstr.OB2DP_father cquat; ); 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object") -> objname in // for compatibility let if ((atoi (getPluginInstanceParam inst "iscamera")) == 1) then "Current camera" else objname -> objname in let atof (getPluginInstanceParam inst "posx") -> posx in let if posx == nil then 0.0 else posx -> posx in let atoi (getPluginInstanceParam inst "xpercent") -> xpercent in let if xpercent == nil then 0 else xpercent -> xpercent in let atoi (getPluginInstanceParam inst "xoffset") -> xoffset in let if xoffset == nil then 0 else xoffset -> xoffset in let atoi (getPluginInstanceParam inst "xalign") -> xalign in let if xalign == nil then 0 else xalign -> xalign in let atof (getPluginInstanceParam inst "posy") -> posy in let if posy == nil then 0.0 else posy -> posy in let atoi (getPluginInstanceParam inst "ypercent") -> ypercent in let if ypercent == nil then 0 else ypercent -> ypercent in let atoi (getPluginInstanceParam inst "yoffset") -> yoffset in let if yoffset == nil then 0 else yoffset -> yoffset in let atoi (getPluginInstanceParam inst "yalign") -> yalign in let if yalign == nil then 0 else yalign -> yalign in let atof (getPluginInstanceParam inst "dist") -> dist in let if dist == nil then 5.0 else dist -> dist in let atoi (getPluginInstanceParam inst "disttype") -> disttype in let if disttype == nil then 0 else disttype -> disttype in let atoi (getPluginInstanceParam inst "target") -> target in let if (target == nil) then 0 else target -> target in let atoi (getPluginInstanceParam inst "init") -> init in let if (init == nil) then 1 else init -> init in let (V3DgetSessionView c3dXsession) -> viewstr in let (V3DgetDefaultViewport viewstr) -> viewportstr in let V3DgetObjectByName c3dXsession objname -> father in let V3DgetObjectTypeByName objname -> iobjmode in let SO3ObjectGetGlobalPosition father -> cpos in let SO3ObjectGetGlobalOrientation father -> cquat in let SO3ViewportGetPixelPositionFromWorldPos viewportstr.V3D_viewport cpos -> [lposx lposy] in let getDistFromCamera cpos -> lastdist in let (IsInEditor inst) || IsEventLinked inst "Translation" -> btranslation in let mkObj2DPosStr [father iobjmode [posx posy] [xpercent ypercent xoffset yoffset] [xalign yalign] dist disttype target [lposx lposy lastdist] [cpos cquat] btranslation] -> obstr in ( PluginRegisterAction inst "Set position" mkfun6 @cbSetPosition obstr; PluginRegisterAction inst "Get position" mkfun6 @cbGetPosition obstr; PluginRegisterAction inst "Set object" mkfun6 @cbSetObject obstr; PluginRegisterAction inst "Reset object" mkfun6 @cbResetObject obstr; if !iobjmode then nil else setPluginInstanceCbCameraChange inst mkfun5 @cbChangeCamera obstr; if (!init) then nil else //SO3ObjectSetGlobalPosition father (getObject3DPosFrom2DCoords posx posy xpercent ypercent xoffset yoffset xalign yalign dist disttype); cbSetPosition inst nil nil nil nil obstr; setPluginInstanceCbScenePreRender2 inst mkfun4 @cbGetPreRender obstr; setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;