Cafu Engine
ToolMorph.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_TOOL_MORPH_HPP_INCLUDED
8 #define CAFU_TOOL_MORPH_HPP_INCLUDED
9 
10 #include "Tool.hpp"
11 #include "ObserverPattern.hpp"
12 
13 
14 class AxesInfoT;
15 class Renderer2DT;
16 class Renderer3DT;
17 class MorphPrimT;
19 struct MP_PartT;
20 
21 
23 {
24  /// The default constructor.
25  MorphHandleT() : MorphPrim(NULL), Part(NULL) { }
26 
27  MorphPrimT* MorphPrim;
28  MP_PartT* Part;
29 };
30 
31 
32 /// This class represents the "Edit Vertices" / "Morph" tool.
33 class ToolMorphT : public ToolT, public ObserverT
34 {
35  public:
36 
37  ToolMorphT(MapDocumentT& MapDoc, ToolManagerT& ToolMan, wxWindow* ParentOptionsBar);
38 
39  ~ToolMorphT();
40 
41 
42  void NoteEditModeChanged(); ///< This is called by the options bar whenever the edit mode has changed (edit vertices, edges, or both).
43  void InsertVertex(); ///< Called by the options bar when the user pressed the "Insert Vertex" button.
44 
45 
46  // Implementations/overrides of ToolT methods.
47  int GetWxEventID() const { return ChildFrameT::ID_MENU_TOOLS_TOOL_EDITVERTICES; }
48  wxWindow* GetOptionsBar();
49  void OnActivate(ToolT* OldTool);
50  void OnDeactivate(ToolT* NewTool);
51  bool CanDeactivate();
52 
53  bool OnKeyDown2D (ViewWindow2DT& ViewWindow, wxKeyEvent& KE);
54  bool OnLMouseDown2D(ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
55  bool OnMouseMove2D (ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
56  bool OnLMouseUp2D (ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
57 
58  bool OnKeyDown3D (ViewWindow3DT& ViewWindow, wxKeyEvent& KE);
59  bool OnLMouseDown3D(ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
60  bool OnMouseMove3D (ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
61  bool OnLMouseUp3D (ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
62 
63  bool IsHiddenByTool(const MapElementT* Elem) const;
64  void RenderTool2D(Renderer2DT& Renderer) const;
65  void RenderTool3D(Renderer3DT& Renderer) const;
66 
67  // ObserverT implementation.
68  void NotifySubjectChanged_Selection(SubjectT* Subject, const ArrayT<MapElementT*>& OldSelection, const ArrayT<MapElementT*>& NewSelection);
69  void NotifySubjectChanged_Deleted(SubjectT* Subject, const ArrayT<MapPrimitiveT*>& Primitives);
70  void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail);
71  void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const ArrayT<BoundingBox3fT>& OldBounds);
72  void NotifySubjectDies(SubjectT* dyingSubject);
73 
74  // The TypeSys related declarations for this class.
75  virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
76  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
77  static const cf::TypeSys::TypeInfoT TypeInfo;
78 
79 
80  private:
81 
82  enum DragStateT { DragNothing, DragBoxSelection, DragMorphHandles };
83 
84  /// Returns the handles of all objects that are currently being morphed.
85  ArrayT<MorphHandleT> GetHandles(bool SelectedOnly, bool Vertices=true, bool Edges=true) const;
86 
87  /// Finds all morph handles that are in ViewWindow at Point.
88  /// Note that this may return a mix of vertex and edge handles (if the tool options bar is set to "edit both"), selected and unselected!
89  ///
90  /// @param ViewWindow The window in which we should look at Point for handles.
91  /// @param Point The coordinate of interest in ViewWindow, given in Tool(!) Space.
92  /// @returns the array of all morph handles "under" Point in ViewWindow.
93  ArrayT<MorphHandleT> GetMorphHandlesAt(ViewWindow2DT& ViewWindow, const wxPoint& Point) const;
94 
95  /// Finds the morph handle that is in ViewWindow along the ray through Point.
96  /// @param ViewWindow The window in which we should look along the ray through Point for handles.
97  /// @param Point The coordinate through which the ray of interested is cast into the world, given in Window(!) Space.
98  /// @param FoundMH If a morph handle was found, it is returned via this reference.
99  /// If there is more than one morph handle along the ray through Point, the morph handle that is closest to the viewer is returned.
100  /// @returns true if a morph handle was found in ViewWindow along the ray through Point, false if no such handle exists.
101  bool GetMorphHandleAt(ViewWindow3DT& ViewWindow, const wxPoint& Point, MorphHandleT& FoundMH) const;
102 
103  int MorphPrims_Find(const MapElementT* Elem) const; ///< Returns the array index number of the MorphPrimT for the given Elem, -1 if there is none.
104  void MorphPrims_SyncTo(const ArrayT<MapElementT*>& Elems); ///< Sync's our m_MorphPrims to the given Elems, so that they correspond 1:1 to each other.
105  void MoveSelectedHandles(const Vector3fT& Delta); ///< WARNING: This method *DESTROYS* all handle pointers into any of the m_MorphPrims!!
106  void NudgeSelectedHandles(const AxesInfoT& AxesInfo, const wxKeyEvent& KE);
107  void FinishDragMorphHandles(); ///< Called from the 2D or 3D views, for morph-modified items, this function replaces the original primitives with the morphed ones in the map.
108  void OnEscape(ViewWindowT& ViewWindow);
109 
110 
111  // The crucial member variables that define the essential state of this tool are m_MorphPrims and m_DragState.
112  //
113  // A few general notes about dragging morph (vertex and edge) handles:
114  // a) At any time, any number of morph handles may be selected. The user then picks *one* to start dragging them *all*.
115  // b) During a drag, the original underlying MorphPrimT(s) can be entirely be re-created and re-formed, due to the nature of the convex-hull algorithm.
116  // Therefore, it's quasi impossible to keep any pointer to anything "inside" the MorphPrimT(s) during the drag,
117  // especially we can NOT keep a pointer to the handle (vertex or edge) that was used to start the drag!!
118  // Fortunately, we don't need any such information, because we just duplicate the current position of
119  // the handle that was chosen to start the drag in the m_DragHandleCurrentPos member.
120  ArrayT<MorphPrimT*> m_MorphPrims; ///< List of primitives currently being morphed by this tool.
121  DragStateT m_DragState; ///< If we are currently dragging anything (LMB is down), and if so, what.
122  Vector3fT m_DragHandleOrigPos; ///< If m_DragState==DragMorphHandles, this was the original position of the handle when the drag started. If m_DragState==DragBoxSelection, this is where dragging the new selection box began.
123  Vector3fT m_DragHandleCurrentPos; ///< If m_DragState==DragMorphHandles, this is the current position of the handle that was chosen for the drag. If m_DragState==DragBoxSelection, this the current position of the other corner of the selection box.
124  bool m_IsRecursiveSelfNotify; ///< Whether an observer message has been triggered by the tool itself.
125 
126  OptionsBar_EditVerticesToolT* m_OptionsBar; ///< The options bar for this tool.
127 };
128 
129 #endif
This class provides auxiliary means for rendering a 3D view.
Definition: Renderer3D.hpp:30
This class implements the rendering into a 2D view.
Definition: Renderer2D.hpp:22
MapElemModDetailE
Definition: ObserverPattern.hpp:37
void NotifySubjectChanged_Selection(SubjectT *Subject, const ArrayT< MapElementT * > &OldSelection, const ArrayT< MapElementT * > &NewSelection)
Notifies the observer that the selection in the current subject has been changed. ...
Definition: ToolMorph.cpp:1004
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
bool OnLMouseDown3D(ViewWindow3DT &ViewWindow, wxMouseEvent &ME)
Also used for LMB "double-click" events (use ME.ButtonDClick() for distinction).
Definition: ToolMorph.cpp:812
wxWindow * GetOptionsBar()
Returns the options bar window associated with this tool. NULL if no options bar has been assigned...
Definition: ToolMorph.cpp:66
int GetWxEventID() const
Returns the ID of the wxWidgets event (menu selection or toolbar button click) that is associated wit...
Definition: ToolMorph.hpp:47
void InsertVertex()
Called by the options bar when the user pressed the "Insert Vertex" button.
Definition: ToolMorph.cpp:440
Definition: ObserverPattern.hpp:64
This is a helper class for the ToolMorphT ("edit vertices") tool.
Definition: MorphPrim.hpp:85
bool IsHiddenByTool(const MapElementT *Elem) const
The caller calls this method in order to learn whether it should exempt the given map element Elem fr...
Definition: ToolMorph.cpp:336
bool OnLMouseDown2D(ViewWindow2DT &ViewWindow, wxMouseEvent &ME)
Also used for LMB "double-click" events (use ME.ButtonDClick() for distinction).
Definition: ToolMorph.cpp:521
This class represents the "Edit Vertices" / "Morph" tool.
Definition: ToolMorph.hpp:33
void NotifySubjectChanged_Deleted(SubjectT *Subject, const ArrayT< MapPrimitiveT * > &Primitives)
Notifies the observer that one or more map primitives have been deleted.
Definition: ToolMorph.cpp:1012
void NotifySubjectChanged_Modified(SubjectT *Subject, const ArrayT< MapElementT * > &MapElements, MapElemModDetailE Detail)
Definition: ToolMorph.cpp:1035
This class represents a (superclass of a) 2D or 3D map view window.
Definition: ChildFrameViewWin.hpp:21
Definition: ObserverPattern.hpp:158
void NotifySubjectDies(SubjectT *dyingSubject)
This method is called whenever a subject is about the be destroyed (and become unavailable).
Definition: ToolMorph.cpp:1066
This class describes how the three world-space axes are mapped to the two screen- or window-space axe...
Definition: AxesInfo.hpp:15
void NoteEditModeChanged()
This is called by the options bar whenever the edit mode has changed (edit vertices, edges, or both).
Definition: ToolMorph.cpp:434
Definition: ChildFrameViewWin2D.hpp:24
Definition: ChildFrameViewWin3D.hpp:21
Definition: ToolManager.hpp:20
This is the base class for all tools in the map editor.
Definition: Tool.hpp:34
This file provides the classes for the Observer pattern as described in the book by the GoF...
Definition: TypeSys.hpp:52
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
Definition: MorphPrim.hpp:23
This is the base class for all elements ("objects") that can exist in a map.
Definition: MapElement.hpp:57
MorphHandleT()
The default constructor.
Definition: ToolMorph.hpp:25
Definition: ToolMorph.hpp:22
The options bar for the Edit Vertices tool.
Definition: ToolOptionsBars.hpp:248