1 // Created on: 2020-09-07
2 // Created by: Maria KRYLOVA
3 // Copyright (c) 2020 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _AIS_LightSource_HeaderFile
17 #define _AIS_LightSource_HeaderFile
18 
19 #include <AIS_InteractiveObject.hxx>
20 #include <Graphic3d_CLight.hxx>
21 #include <SelectMgr_EntityOwner.hxx>
22 
23 class Select3D_SensitiveSphere;
24 
25 //! Interactive object for a light source.
26 //! Each type of light source has it's own presentation:
27 //! - Ambient light is displayed as a sphere at view corner;
28 //! - Positional light is represented by a sphere or marker;
29 //! - Spot light is represented by a cone;
30 //! - Directional light is represented by a set of arrows at the corner of view.
31 //! In addition, light source name could be displayed, and clicking on presentation will enable/disable light.
32 class AIS_LightSource : public AIS_InteractiveObject
33 {
34   friend class AIS_LightSourceOwner;
35   DEFINE_STANDARD_RTTIEXT(AIS_LightSource, AIS_InteractiveObject)
36 public:
37 
38   //! Initializes the light source by copying Graphic3d_CLight settings.
39   Standard_EXPORT AIS_LightSource (const Handle(Graphic3d_CLight)& theLightSource);
40 
41   //! Returns the light.
Handle(Graphic3d_CLight)42   const Handle(Graphic3d_CLight)& Light() const { return myLightSource; }
43 
44   //! Set the light.
SetLight(const Handle (Graphic3d_CLight)& theLight)45   void SetLight (const Handle(Graphic3d_CLight)& theLight)
46   {
47     myLightSource = theLight;
48     SetToUpdate();
49   }
50 
51 public: //! @name Light properties
52 
53   //! Returns TRUE if the light source name should be displayed; TRUE by default.
ToDisplayName() const54   Standard_Boolean ToDisplayName() const { return myToDisplayName; }
55 
56   //! Show/hide light source name.
SetDisplayName(Standard_Boolean theToDisplay)57   void SetDisplayName(Standard_Boolean theToDisplay)
58   {
59     if (myToDisplayName != theToDisplay)
60     {
61       myToDisplayName = theToDisplay;
62       SetToUpdate();
63     }
64   }
65 
66   //! Returns TRUE to display light source range as sphere (positional light) or cone (spot light); TRUE by default.
67   //! Has no effect for non-zoomable presentation.
ToDisplayRange() const68   Standard_Boolean ToDisplayRange() const { return myToDisplayRange; }
69 
70   //! Show/hide light source range shaded presentation.
SetDisplayRange(Standard_Boolean theToDisplay)71   void SetDisplayRange (Standard_Boolean theToDisplay)
72   {
73     if (myToDisplayRange != theToDisplay)
74     {
75       myToDisplayRange = theToDisplay;
76       SetToUpdate();
77     }
78   }
79 
80   //! Returns the size of presentation; 50 by default.
Size() const81   Standard_Real Size() const { return mySize; }
82 
83   //! Sets the size of presentation.
SetSize(Standard_Real theSize)84   void SetSize (Standard_Real theSize)
85   {
86     if (mySize != theSize)
87     {
88       mySize = theSize;
89       SetToUpdate();
90     }
91   }
92 
93   //! Returns Sensitive sphere arc size in pixels; 20 by default.
ArcSize() const94   Standard_Integer ArcSize() const { return mySensSphereArcSize; }
95 
96   //! Sets the size of sensitive sphere arc.
SetArcSize(Standard_Integer theSize)97   void SetArcSize (Standard_Integer theSize)
98   {
99     if (mySensSphereArcSize != theSize)
100     {
101       mySensSphereArcSize = theSize;
102       SetToUpdate();
103     }
104   }
105 
106   //! Returns TRUE if transform-persistence is allowed;
107   //! TRUE by default for Ambient and Directional lights
108   //! and FALSE by default for Positional and Spot lights.
IsZoomable() const109   bool IsZoomable() const { return myIsZoomable; }
110 
111   //! Sets if transform-persistence is allowed.
SetZoomable(bool theIsZoomable)112   void SetZoomable (bool theIsZoomable)
113   {
114     if (myIsZoomable != theIsZoomable)
115     {
116       myIsZoomable = theIsZoomable;
117       SetToUpdate();
118     }
119   }
120 
121   //! Sets if dragging is allowed.
SetDraggable(bool theIsDraggable)122   void SetDraggable (bool theIsDraggable)
123   {
124     if (myIsDraggable != theIsDraggable)
125     {
126       myIsDraggable = theIsDraggable;
127     }
128   }
129 
130   //! Returns TRUE if mouse click will turn light on/off; TRUE by default.
ToSwitchOnClick() const131   bool ToSwitchOnClick() const { return myToSwitchOnClick; }
132 
133   //! Sets if mouse click should turn light on/off.
SetSwitchOnClick(bool theToHandle)134   void SetSwitchOnClick (bool theToHandle) { myToSwitchOnClick = theToHandle; }
135 
136   //! Returns a number of directional light arrows to display; 5 by default.
NbArrows() const137   Standard_Integer NbArrows() const { return myNbArrows; }
138 
139   //! Returns a number of directional light arrows to display (supported values: 1, 3, 5, 9).
SetNbArrows(Standard_Integer theNbArrows)140   void SetNbArrows (Standard_Integer theNbArrows)
141   {
142     if (myNbArrows != theNbArrows)
143     {
144       myNbArrows = theNbArrows;
145       SetToUpdate();
146     }
147   }
148 
149   //! Returns light source icon.
150   //! @param theIsEnabled [in] marker index for enabled/disabled light source states
Handle(Graphic3d_MarkerImage)151   const Handle(Graphic3d_MarkerImage)& MarkerImage (bool theIsEnabled) const { return myMarkerImages[theIsEnabled ? 1 : 0]; }
152 
153   //! Returns light source icon.
154   //! @param theIsEnabled [in] marker index for enabled/disabled light source states
MarkerType(bool theIsEnabled) const155   Aspect_TypeOfMarker MarkerType (bool theIsEnabled) const { return myMarkerTypes[theIsEnabled ? 1 : 0]; }
156 
157   //! Sets custom icon to light source.
SetMarkerImage(const Handle (Graphic3d_MarkerImage)& theImage,bool theIsEnabled)158   void SetMarkerImage (const Handle(Graphic3d_MarkerImage)& theImage,
159                        bool theIsEnabled)
160   {
161     myMarkerImages[theIsEnabled ? 1 : 0] = theImage;
162     myMarkerTypes [theIsEnabled ? 1 : 0] = !theImage.IsNull()
163                                          ? Aspect_TOM_USERDEFINED
164                                          : (theIsEnabled ? Aspect_TOM_O_POINT : Aspect_TOM_O_X);
165   }
166 
167   //! Sets standard icon to light source.
SetMarkerType(Aspect_TypeOfMarker theType,bool theIsEnabled)168   void SetMarkerType (Aspect_TypeOfMarker theType,
169                       bool theIsEnabled)
170   {
171     myMarkerTypes[theIsEnabled ? 1 : 0] = theType;
172   }
173 
174   //! Returns tessellation level for quadric surfaces; 30 by default.
NbSplitsQuadric() const175   Standard_Integer NbSplitsQuadric() const { return myNbSplitsQuadric; }
176 
177   //! Sets tessellation level for quadric surfaces.
SetNbSplitsQuadric(Standard_Integer theNbSplits)178   void SetNbSplitsQuadric (Standard_Integer theNbSplits) { myNbSplitsQuadric = theNbSplits; }
179 
180   //! Returns tessellation level for arrows; 20 by default.
NbSplitsArrow() const181   Standard_Integer NbSplitsArrow() const { return myNbSplitsArrow; }
182 
183   //! Sets tessellation level for arrows.
SetNbSplitsArrow(Standard_Integer theNbSplits)184   void SetNbSplitsArrow (Standard_Integer theNbSplits) { myNbSplitsArrow = theNbSplits; }
185 
186   //! Returns kind of the object.
Type() const187   virtual AIS_KindOfInteractive Type() const Standard_OVERRIDE { return AIS_KindOfInteractive_LightSource; }
188 
189 protected:
190 
191   //! Return true if specified display mode is supported: 0 for main presentation and 1 for highlight.
AcceptDisplayMode(const Standard_Integer theMode) const192   virtual Standard_Boolean AcceptDisplayMode (const Standard_Integer theMode) const Standard_OVERRIDE
193   {
194     return theMode == 0
195         || theMode == 1;
196   }
197 
198   //! Computes selection sensitive zones(triangulation) for light source presentation.
199   Standard_EXPORT virtual void Compute (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
200                                         const Handle(Prs3d_Presentation)& thePrs,
201                                         const Standard_Integer theMode) Standard_OVERRIDE;
202 
203   //! Fills presentation.
204   Standard_EXPORT virtual void ComputeSelection (const Handle(SelectMgr_Selection)& theSel,
205                                                  const Standard_Integer theMode) Standard_OVERRIDE;
206 
207   //! Drag object in the viewer.
208   //! @param[in] theCtx      interactive context
209   //! @param[in] theView     active View
210   //! @param[in] theOwner    the owner of detected entity
211   //! @param[in] theDragFrom drag start point
212   //! @param[in] theDragTo   drag end point
213   //! @param[in] theAction   drag action
214   //! @return FALSE if object rejects dragging action (e.g. AIS_DragAction_Start)
215   Standard_EXPORT virtual Standard_Boolean ProcessDragging (const Handle(AIS_InteractiveContext)& theCtx,
216                                                             const Handle(V3d_View)& theView,
217                                                             const Handle(SelectMgr_EntityOwner)& theOwner,
218                                                             const Graphic3d_Vec2i& theDragFrom,
219                                                             const Graphic3d_Vec2i& theDragTo,
220                                                             const AIS_DragAction theAction) Standard_OVERRIDE;
221 
222   //! Sets new local transformation, which is propagated to Graphic3d_CLight instance.
223   Standard_EXPORT virtual void setLocalTransformation (const Handle(TopLoc_Datum3D)& theTrsf) Standard_OVERRIDE;
224 
225   //! Updates local transformation basing on a type of light source.
226   Standard_EXPORT virtual void updateLightLocalTransformation();
227 
228   //! Updates transform persistence basing on a type of light source.
229   Standard_EXPORT virtual void updateLightTransformPersistence();
230 
231   //! Sets color of light.
232   Standard_EXPORT virtual void updateLightAspects();
233 
234   //! Compute ambient light source presentation as a sphere at view corner.
235   Standard_EXPORT virtual void computeAmbient (const Handle(Prs3d_Presentation)& thePrs,
236                                                const Standard_Integer theMode);
237 
238   //! Compute directional light source presentation as a set of arrows at view corner.
239   Standard_EXPORT virtual void computeDirectional (const Handle(Prs3d_Presentation)& thePrs,
240                                                    const Standard_Integer theMode);
241 
242   //! Compute positional light source presentation as a sphere of either fixed size (no range) or of size representing a maximum range.
243   Standard_EXPORT virtual void computePositional (const Handle(Prs3d_Presentation)& thePrs,
244                                                   const Standard_Integer theMode);
245 
246   //! Compute spot light source presentation as a cone.
247   Standard_EXPORT virtual void computeSpot (const Handle(Prs3d_Presentation)& thePrs,
248                                             const Standard_Integer theMode);
249 
250 protected:
251 
252   Handle(Graphic3d_CLight)         myLightSource;           //!< displayed light source
253 
254   Handle(Graphic3d_AspectMarker3d) myDisabledMarkerAspect;  //!< disabled light source marker style
255   Handle(Graphic3d_AspectLine3d)   myArrowLineAspectShadow; //!< arrow shadow style
256   Handle(Graphic3d_MarkerImage)    myMarkerImages[2];       //!< icon of disabled (0) and enabled (1) light
257   Handle(Select3D_SensitiveSphere) mySensSphere;            //!< sensitive sphere of directional light source
258   Aspect_TypeOfMarker              myMarkerTypes[2];        //!< icon of disabled (0) and enabled (1) light
259   Aspect_TypeOfMarker              myCodirMarkerType;       //!< icon of arrow co-directional to camera direction (look from)
260   Aspect_TypeOfMarker              myOpposMarkerType;       //!< icon of arrow opposite to camera direction (look at)
261 
262   gp_Trsf          myLocTrsfStart;      //!< object transformation before transformation
263   Standard_Real    mySize;              //!< presentation size
264   Standard_Integer myNbArrows;          //!< number of directional light arrows
265   Standard_Integer myNbSplitsQuadric;   //!< tessellation level for quadric surfaces
266   Standard_Integer myNbSplitsArrow;     //!< tessellation level for arrows
267   Standard_Integer mySensSphereArcSize; //! sensitive sphere arc size in pixels
268   Standard_Boolean myIsZoomable;        //!< flag to allow/disallow transform-persistence when possible
269   Standard_Boolean myIsDraggable;       //!< flag to allow/disallow rotate directional light source by dragging
270   Standard_Boolean myToDisplayName;     //!< flag to show/hide name
271   Standard_Boolean myToDisplayRange;    //!< flag to show/hide range of positional/spot light
272   Standard_Boolean myToSwitchOnClick;   //!< flag to handle mouse click to turn light on/off
273 
274 };
275 
276 //! Owner of AIS_LightSource presentation.
277 class AIS_LightSourceOwner : public SelectMgr_EntityOwner
278 {
279   DEFINE_STANDARD_RTTIEXT(AIS_LightSourceOwner, SelectMgr_EntityOwner)
280 public:
281 
282   //! Main constructor.
283   Standard_EXPORT AIS_LightSourceOwner (const Handle(AIS_LightSource)& theObject,
284                                         Standard_Integer thePriority = 5);
285 
286   //! Handle mouse button click event.
287   Standard_EXPORT virtual Standard_Boolean HandleMouseClick (const Graphic3d_Vec2i& thePoint,
288                                                              Aspect_VKeyMouse theButton,
289                                                              Aspect_VKeyFlags theModifiers,
290                                                              bool theIsDoubleClick) Standard_OVERRIDE;
291 
292   //! Highlights selectable object's presentation with display mode in presentation manager with given highlight style.
293   //! Also a check for auto-highlight is performed - if selectable object manages highlighting on its own,
294   //! execution will be passed to SelectMgr_SelectableObject::HilightOwnerWithColor method.
295   Standard_EXPORT virtual void HilightWithColor (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
296                                                  const Handle(Prs3d_Drawer)& theStyle,
297                                                  const Standard_Integer theMode) Standard_OVERRIDE;
298 
299   //! Always update dynamic highlighting.
300   Standard_EXPORT virtual Standard_Boolean IsForcedHilight() const Standard_OVERRIDE;
301 
302 };
303 
304 #endif // _AIS_LightSource_HeaderFile
305