Cafu Engine
ToolSelection.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_SELECTION_HPP_INCLUDED
8 #define CAFU_TOOL_SELECTION_HPP_INCLUDED
9 
10 #include "Tool.hpp"
11 #include "TrafoBox.hpp"
12 #include "Templates/Array.hpp"
13 
14 
16 class Renderer2DT;
17 class ViewWindowT;
18 class ToolSelectionT;
19 
20 
21 class CycleHitsTimerT : public wxTimer
22 {
23  public:
24 
25  CycleHitsTimerT(ToolSelectionT& SelectionTool);
26 
27  void Notify();
28 
29 
30  private:
31 
32  ToolSelectionT& m_SelectionTool;
33 };
34 
35 
36 class ToolSelectionT : public ToolT, public ObserverT
37 {
38  public:
39 
40  ToolSelectionT(MapDocumentT& MapDoc, ToolManagerT& ToolMan, wxWindow* ParentOptionsBar);
41  ~ToolSelectionT();
42 
43  // Implementations/overrides of ToolT methods.
44  int GetWxEventID() const { return ChildFrameT::ID_MENU_TOOLS_TOOL_SELECTION; }
45  wxWindow* GetOptionsBar();
46  void OnActivate(ToolT* OldTool);
47  void OnDeactivate(ToolT* NewTool);
48 
49  bool OnKeyDown2D (ViewWindow2DT& ViewWindow, wxKeyEvent& KE);
50  bool OnKeyUp2D (ViewWindow2DT& ViewWindow, wxKeyEvent& KE);
51  bool OnLMouseDown2D (ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
52  bool OnLMouseUp2D (ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
53  bool OnMouseMove2D (ViewWindow2DT& ViewWindow, wxMouseEvent& ME);
54  int OnContextMenu2D(ViewWindow2DT& ViewWindow, wxContextMenuEvent& CE, wxMenu& Menu);
55 
56  bool OnKeyDown3D (ViewWindow3DT& ViewWindow, wxKeyEvent& KE);
57  bool OnLMouseDown3D (ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
58  bool OnLMouseUp3D (ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
59  bool OnMouseMove3D (ViewWindow3DT& ViewWindow, wxMouseEvent& ME);
60  int OnContextMenu3D(ViewWindow3DT& ViewWindow, wxContextMenuEvent& CE, wxMenu& Menu);
61 
62  void RenderTool2D(Renderer2DT& Renderer) const;
63  void RenderTool3D(Renderer3DT& Renderer) const;
64  bool UpdateStatusBar(ChildFrameT* ChildFrame) const;
65 
66  // ObserverT implementation.
67  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);
73  void Notify_VarChanged(SubjectT* Subject, const cf::TypeSys::VarBaseT& Var);
74  void NotifySubjectDies(SubjectT* dyingSubject);
75 
76  // The TypeSys related declarations for this class.
77  virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
78  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
79  static const cf::TypeSys::TypeInfoT TypeInfo;
80 
81 
82  private:
83 
84  /// This enumeration defines the essential states of this tool.
85  /// Note that in each tool state, the selection can contain any number of map elements.
86  enum ToolStateT
87  {
88  /// The LMB is up and nothing is currently happening.
89  /// The mouse cursor however is updated to indicate a likely next state if the button is pressed at the current position.
90  TS_IDLE,
91 
92  /// The LMB went down in a place where further input is required (e.g. mouse move or button release)
93  /// for deciding the action and the next state.
94  TS_UNDECIDED,
95 
96  /// There was a one-click selection, and the button is still down.
97  /// We cycle through subsets of the selected objects (each one separately, and their union) until the button is released.
98  TS_POINT_SEL,
99 
100  /// The user is dragging a box frame for map element selection.
101  TS_DRAG_SEL,
102 
103  /// The box around the selected objects is currently being transformed, that is, translated, scaled, rotated or sheared.
104  TS_BOX_TRAFO
105  };
106 
107  /// IDs used for the items in our RMB context menus.
108  enum
109  {
110  ID_CREATE_MODEL=wxID_HIGHEST+1000,
111  ID_CREATE_PLANT
112  };
113 
114  friend class CycleHitsTimerT;
115 
116  void OnEscape(ViewWindowT& ViewWindow); ///< Handles the ESC key event in the 2D and 3D views.
117  void UpdateTrafoBox();
118  void CreateModel(const Vector3fT& WorldPos);
119  void CreatePlant(const Vector3fT& WorldPos);
120  void NudgeSelection(const AxesInfoT& AxesInfo, const wxKeyEvent& KE);
121  void SetHitList(const ArrayT<MapElementT*>& NewHits, bool IsControlDown);
122  void StepCurHitNr(int Step);
123  void ToggleCurHitNr();
124  CommandT* CloneDrag() const;
125 
126 
127  ToolStateT m_ToolState; ///< The main state of this tool.
128  TrafoBoxT m_TrafoBox; ///< The transformation box around the current selection (only dependent on the selection, not the tool state). Depending on its own state, the trafo box can show scale, rotate or shear handles.
129 
130  wxPoint m_LDownPosWin; ///< The point where the LMB went down, in window coordinates. Set for all tool states.
131  Vector3fT m_LDownPosWorld; ///< The point where the LMB went down, in world space. Set for all tool states.
132  Vector3fT m_LDragPosWorld; ///< In state TS_DRAG_SEL, this is the current point in world space of the drag (second corner of the dragging rectangle).
133 
134  CycleHitsTimerT m_CycleHitsTimer;
135  ArrayT<MapElementT*> m_HitList; ///< The list of map elements that were hit by the last LMB click in a 2D or 3D view (along the ray through the pixel).
136  int m_CurHitNr; ///< As we're cycling through the m_HitList, this is the number of the currently considered (and selected) list element.
137 
138  OptionsBar_SelectionToolT* m_OptionsBar; ///< The options bar for this tool.
139 };
140 
141 #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
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
void Notify_VarChanged(SubjectT *Subject, const cf::TypeSys::VarBaseT &Var)
Notifies the observer that a variable has changed.
Definition: ToolSelection.cpp:803
void NotifySubjectChanged_Deleted(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GameSys::EntityT > > &Entities)
Notifies the observer that one or more entities have been deleted.
Definition: ToolSelection.cpp:737
This class implements a spatial box that can be used to define a transformation (translation, rotation, scale or shear).
Definition: TrafoBox.hpp:29
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: ToolSelection.cpp:725
The options bar for the Selection tool.
Definition: ToolOptionsBars.hpp:33
EntityModDetailE
Definition: ObserverPattern.hpp:47
This class represents a child frame.
Definition: ChildFrame.hpp:55
bool OnLMouseDown2D(ViewWindow2DT &ViewWindow, wxMouseEvent &ME)
Also used for LMB "double-click" events (use ME.ButtonDClick() for distinction).
Definition: ToolSelection.cpp:167
void NotifySubjectDies(SubjectT *dyingSubject)
This method is called whenever a subject is about the be destroyed (and become unavailable).
Definition: ToolSelection.cpp:819
Definition: ObserverPattern.hpp:64
Definition: ToolSelection.hpp:36
bool OnLMouseDown3D(ViewWindow3DT &ViewWindow, wxMouseEvent &ME)
Also used for LMB "double-click" events (use ME.ButtonDClick() for distinction).
Definition: ToolSelection.cpp:558
Definition: ToolSelection.hpp:21
This class represents a (superclass of a) 2D or 3D map view window.
Definition: ChildFrameViewWin.hpp:21
Definition: ObserverPattern.hpp:158
This class describes how the three world-space axes are mapped to the two screen- or window-space axe...
Definition: AxesInfo.hpp:15
This is the common base class for the VarT classes.
Definition: Variables.hpp:113
Definition: ChildFrameViewWin2D.hpp:24
Definition: ChildFrameViewWin3D.hpp:21
void NotifySubjectChanged_Modified(SubjectT *Subject, const ArrayT< MapElementT * > &MapElements, MapElemModDetailE Detail)
Definition: ToolSelection.cpp:753
This class represents a general command for implementing modifications to the applications document...
Definition: CommandPattern.hpp:30
void Notify_EntChanged(SubjectT *Subject, const ArrayT< IntrusivePtrT< MapEditor::CompMapEntityT > > &Entities, EntityModDetailE Detail)
This is called when a property of an entity changed, such as the "model" property.
Definition: ToolSelection.cpp:788
Definition: ToolManager.hpp:20
This is the base class for all tools in the map editor.
Definition: Tool.hpp:34
Definition: TypeSys.hpp:52
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
wxWindow * GetOptionsBar()
Returns the options bar window associated with this tool. NULL if no options bar has been assigned...
Definition: ToolSelection.cpp:102
int GetWxEventID() const
Returns the ID of the wxWidgets event (menu selection or toolbar button click) that is associated wit...
Definition: ToolSelection.hpp:44