Cafu Engine
ChildFrameViewWin2D.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_CHILDFRAME_VIEW_WIN_2D_HPP_INCLUDED
8 #define CAFU_CHILDFRAME_VIEW_WIN_2D_HPP_INCLUDED
9 
10 #include "ChildFrameViewWin.hpp"
11 
12 #include "../AxesInfo.hpp"
13 
14 #include "Math3D/Vector3.hpp"
15 #include "Templates/Array.hpp"
16 
17 #include "wx/wx.h"
18 
19 
20 class MapElementT;
21 class ViewWindow2DT;
22 
23 
24 class ViewWindow2DT : public wxWindow, public ViewWindowT
25 {
26  public:
27 
28  /// This class defines if the associated view is currently being grabbed (for scrolling) with the mouse.
29  /// It is similar to class ViewWindow3DT::MouseControlT, but much simpler as grabbing is the only type of control.
30  class MouseGrabT
31  {
32  public:
33 
34  MouseGrabT(ViewWindow2DT& ViewWin);
35 
36  /// Activates mouse grabbing at the given reference point,
37  /// or updates the reference point when mouse grabbing is already active.
38  /// Note that activating can fail, especially if some other code has already captured the mouse pointer.
39  void Activate(const wxPoint& RefPt);
40 
41  /// Deactivates the mouse control.
42  void Deactivate();
43 
44  /// Returns whether the mouse control is active.
45  bool IsActive() const { return m_IsActive; }
46 
47  /// Returns the position of the reference point in window coordinates.
48  const wxPoint& GetRefPt() const { return m_RefPtWin; }
49 
50 
51  private:
52 
53  ViewWindow2DT& m_ViewWin;
54  bool m_IsActive;
55  wxPoint m_RefPtWin; ///< The position of the reference point in window coordinates.
56  };
57 
58 
59  /// The constructor.
60  ViewWindow2DT(wxWindow* Parent, ChildFrameT* ChildFrame, ViewTypeT InitialViewType);
61 
62  // Methods inherited from ObserverT.
63  void NotifySubjectChanged(SubjectT* Subject, MapDocOtherDetailT OtherDetail);
64  void NotifySubjectChanged_Selection(SubjectT* Subject, const ArrayT<MapElementT*>& OldSelection, const ArrayT<MapElementT*>& NewSelection);
66  void NotifySubjectChanged_Created(SubjectT* Subject, const ArrayT<MapPrimitiveT*>& Primitives);
68  void NotifySubjectChanged_Deleted(SubjectT* Subject, const ArrayT<MapPrimitiveT*>& Primitives);
69  void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail);
70  void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const ArrayT<BoundingBox3fT>& OldBounds);
72  void Notify_VarChanged(SubjectT* Subject, const cf::TypeSys::VarBaseT& Var);
73  // void NotifySubjectDies(SubjectT* dyingSubject); // Already implemented by ViewWindowT.
74 
75  // Methods inherited from ToolsObserverT.
76  void NotifySubjectChanged(ToolsSubjectT* Subject, ToolT* Tool, ToolsUpdatePriorityT Priority);
77 
78  // Inherited methods from the ViewWindowT base class.
79  wxWindow* GetWindow();
80  ViewTypeT GetViewType() const { return m_ViewType; }
81  AxesInfoT GetAxesInfo() const { return m_AxesInfo; }
82 
83  // Helper methods (all for public and some also for private use).
84  void SetZoom(float NewZoom);
85  float GetZoom() const { return m_ZoomFactor; }
86  void CenterView(const Vector3fT& Point);
87 
88  /// This method returns all visible map elements at the given pixel in the 2D view window.
89  /// More precisely, all visible map elements that intersect the disc centered around Pixel with radius Radius are returned.
90  /// @param Pixel The pixel in window coordinates for which the map elements are to be found.
91  /// @param Radius The error tolerance around Pixel within which map elements also count as "at Pixel".
92  /// @returns The array of visible map elements that are at the given disc (in no particular order).
93  ArrayT<MapElementT*> GetElementsAt(const wxPoint& Pixel, int Radius=3) const;
94 
95  wxPoint GetScrollPosXY() const { return wxPoint(GetScrollPos (wxHORIZONTAL), GetScrollPos (wxVERTICAL)); } ///< Conveniently return the positions of both scroll bars in a wxPoint.
96  wxPoint GetScrollRangeXY() const { return wxPoint(GetScrollRange(wxHORIZONTAL), GetScrollRange(wxVERTICAL)); } ///< Conveniently return the ranges of both scroll bars in a wxPoint.
97  wxPoint GetClientCenter() const { const wxSize Size=GetClientSize(); return wxPoint(Size.x/2, Size.y/2); } ///< Returns the center point of the client area of this window.
98 
99  // Methods for converting points between the coordinate systems.
100  // TODO: Should window and tool coordinates also be of type float rather than int???
101  // Floats would avoid the forced round-off error with intermediate results and work well when OpenGL is used also for 2D rendering.
102  // Moreover, I'd even suggest to just say typedef CoordT Vector3fT; because we could even convert the third axis in both directions.
103  wxPoint WindowToTool (const wxPoint& p) const; ///< Transforms point p from window to tool coordinates/space. This is the inverse of ToolToWindow().
104  wxPoint ToolToWindow (const wxPoint& p) const; ///< Transforms point p from tool to window coordinates/space. This is the inverse of WindowToTool().
105 
106  Vector3fT ToolToWorld (const wxPoint& p, float Third) const; ///< Transforms point p from tool to world coordinates/space. This is the inverse of WorldToTool(). The value for the third axis of the returned vector is set to Third.
107  wxPoint WorldToTool (const Vector3fT& v) const; ///< Transforms vector v from world to tool coordinates/space. This is the inverse of ToolToWorld().
108 
109  Vector3fT WindowToWorld(const wxPoint& p, float Third) const; ///< Transforms point p from window to world coordinates/space. This is the inverse of WorldToWindow(), and equivalent to calling ToolToWorld(WindowToTool(p)). The value for the third axis of the returned vector is set to Third.
110  wxPoint WorldToWindow(const Vector3fT& v) const; ///< Transforms vector v from world to window coordinates/space. This is the inverse of WindowToWorld(), and equivalent to calling ToolToWindow(WorldToTool(p)).
111 
112  /// Determines whether the given rectangle Rect is intersected by the line segment through points A and B.
113  static bool RectIsIntersected(const wxRect& Rect, const wxPoint& A, const wxPoint& B);
114 
115 
116  private:
117 
118  enum RightMBStateT { RMB_UP_IDLE, RMB_DOWN_UNDECIDED, RMB_DOWN_DRAGGING }; ///< This enumeration describes the states that the right mouse button can take.
119 
120  ViewWindow2DT(const ViewWindow2DT&); ///< Use of the Copy Constructor is not allowed.
121  void operator = (const ViewWindow2DT&); ///< Use of the Assignment Operator is not allowed.
122 
123  ViewTypeT m_ViewType; ///< The type of this 2D view (top, front, side).
124  AxesInfoT m_AxesInfo; ///< Describes how the three world-space axes map to our two window-space axes.
125  wxBitmap m_BitmapMapOnly; ///< This bitmap caches our 2D rendering of the map only.
126  wxBitmap m_BitmapMapAndTools; ///< This bitmap caches our 2D rendering of the map and the tools overlay.
127  float m_ZoomFactor; ///< The zoom factor.
128  MouseGrabT m_MouseGrab; ///< If this view is currently being grabbed and scrolled with the mouse.
129  RightMBStateT m_RightMBState; ///< The state of the right mouse button. This is required because the RMB has a dual function: a click can bring up the context menu, or initiate mouse-grabbing.
130  wxPoint m_RDownPosWin; ///< The point where the RMB went down, in window coordinates.
131 
132  // Private helper methods.
133  void SetViewType(ViewTypeT NewViewType);
134  void ScrollWindow(int AmountX, int AmountY, const wxRect* Rect=NULL); ///< Overridden method of base class wxWindow.
135  void UpdateScrollbars(); ///< Updates the scrollbars (and thus the virtual area of the window) according to window client size and map zoom factor.
136  int GetBestGridSpacing() const;
137 
138  void Render(const wxRect& UpdateRect); ///< Called by DoPaint().
139  enum LayerT { TOOL_LAYER, MAP_AND_TOOL_LAYER }; ///< The bitmap layers to repaint in DoPaint(). Note that there is no MAP_LAYER, because re-painting the map layer requires re-painting the tool layer (an inherent property of our implementation).
140  void DoPaint(LayerT Layer, wxDC& dc, wxRegion* UpdateRegion=NULL); ///< Called by OnPaint() and NotifySubjectChanged(Tool).
141 
142  // Event handlers.
143  void OnKeyDown (wxKeyEvent& KE);
144  void OnKeyUp (wxKeyEvent& KE);
145  void OnKeyChar (wxKeyEvent& KE);
146  void OnMouseLeftDown (wxMouseEvent& ME); ///< We also handle "double-click" events in this method (use ME.ButtonDClick() for distinction).
147  void OnMouseLeftUp (wxMouseEvent& ME);
148  void OnMouseMiddleDown (wxMouseEvent& ME); ///< We also handle "double-click" events in this method (use ME.ButtonDClick() for distinction).
149  void OnMouseMiddleUp (wxMouseEvent& ME);
150  void OnMouseRightDown (wxMouseEvent& ME); ///< We also handle "double-click" events in this method (use ME.ButtonDClick() for distinction).
151  void OnMouseRightUp (wxMouseEvent& ME);
152  void OnMouseWheel (wxMouseEvent& ME);
153  void OnMouseMove (wxMouseEvent& ME);
154  void OnScroll (wxScrollWinEvent& SE);
155  void OnContextMenu (wxContextMenuEvent& CE);
156  void OnPaint (wxPaintEvent& PE);
157  void OnSize (wxSizeEvent& SE);
158  void OnKillFocus (wxFocusEvent& FE);
159  void OnMouseCaptureLost(wxMouseCaptureLostEvent& ME);
160 
161  DECLARE_EVENT_TABLE()
162 };
163 
164 #endif
MapElemModDetailE
Definition: ObserverPattern.hpp:37
wxPoint WorldToTool(const Vector3fT &v) const
Transforms vector v from world to tool coordinates/space. This is the inverse of ToolToWorld().
Definition: ChildFrameViewWin2D.cpp:402
ViewTypeT GetViewType() const
Returns the view type of this view window.
Definition: ChildFrameViewWin2D.hpp:80
AxesInfoT GetAxesInfo() const
This method returns the axes info for this window. In the case of a 3D window, it computes the 2D axe...
Definition: ChildFrameViewWin2D.hpp:81
void NotifySubjectChanged_Modified(SubjectT *Subject, const ArrayT< MapElementT * > &MapElements, MapElemModDetailE Detail)
Definition: ChildFrameViewWin2D.cpp:195
wxPoint GetScrollPosXY() const
Conveniently return the positions of both scroll bars in a wxPoint.
Definition: ChildFrameViewWin2D.hpp:95
wxWindow * GetWindow()
This function is not const because we can mutate this(!) object via the returned pointer.
Definition: ChildFrameViewWin2D.cpp:284
This class defines if the associated view is currently being grabbed (for scrolling) with the mouse...
Definition: ChildFrameViewWin2D.hpp:30
ToolsUpdatePriorityT
An enumeration that determines the urgency with which observers should update themselves when their s...
Definition: ObserverPatternTools.hpp:21
void Notify_VarChanged(SubjectT *Subject, const cf::TypeSys::VarBaseT &Var)
Notifies the observer that a variable has changed.
Definition: ChildFrameViewWin2D.cpp:259
void NotifySubjectChanged_Deleted(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GameSys::EntityT > > &Entities)
Notifies the observer that one or more entities have been deleted.
Definition: ChildFrameViewWin2D.cpp:183
EntityModDetailE
Definition: ObserverPattern.hpp:47
This class represents a child frame.
Definition: ChildFrame.hpp:55
wxPoint WindowToTool(const wxPoint &p) const
Transforms point p from window to tool coordinates/space. This is the inverse of ToolToWindow().
Definition: ChildFrameViewWin2D.cpp:373
void Deactivate()
Deactivates the mouse control.
Definition: ChildFrameViewWin2D.cpp:64
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: ChildFrameViewWin2D.cpp:136
void Activate(const wxPoint &RefPt)
Activates mouse grabbing at the given reference point, or updates the reference point when mouse grab...
Definition: ChildFrameViewWin2D.cpp:48
wxPoint GetClientCenter() const
Returns the center point of the client area of this window.
Definition: ChildFrameViewWin2D.hpp:97
const wxPoint & GetRefPt() const
Returns the position of the reference point in window coordinates.
Definition: ChildFrameViewWin2D.hpp:48
ArrayT< MapElementT * > GetElementsAt(const wxPoint &Pixel, int Radius=3) const
This method returns all visible map elements at the given pixel in the 2D view window.
Definition: ChildFrameViewWin2D.cpp:352
bool IsActive() const
Returns whether the mouse control is active.
Definition: ChildFrameViewWin2D.hpp:45
void NotifySubjectChanged_Created(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GameSys::EntityT > > &Entities)
Notifies the observer that one or more entities have been created.
Definition: ChildFrameViewWin2D.cpp:156
Vector3fT ToolToWorld(const wxPoint &p, float Third) const
Transforms point p from tool to world coordinates/space. This is the inverse of WorldToTool(). The value for the third axis of the returned vector is set to Third.
Definition: ChildFrameViewWin2D.cpp:387
This class represents a (superclass of a) 2D or 3D map view window.
Definition: ChildFrameViewWin.hpp:21
Definition: ObserverPattern.hpp:158
static bool RectIsIntersected(const wxRect &Rect, const wxPoint &A, const wxPoint &B)
Determines whether the given rectangle Rect is intersected by the line segment through points A and B...
Definition: ChildFrameViewWin2D.cpp:426
This class describes how the three world-space axes are mapped to the two screen- or window-space axe...
Definition: AxesInfo.hpp:15
void NotifySubjectChanged(SubjectT *Subject, MapDocOtherDetailT OtherDetail)
Notifies the observer that some other detail than those specifically addressed below has changed...
Definition: ChildFrameViewWin2D.cpp:130
This is the common base class for the VarT classes.
Definition: Variables.hpp:113
wxPoint GetScrollRangeXY() const
Conveniently return the ranges of both scroll bars in a wxPoint.
Definition: ChildFrameViewWin2D.hpp:96
wxPoint WorldToWindow(const Vector3fT &v) const
Transforms vector v from world to window coordinates/space. This is the inverse of WindowToWorld()...
Definition: ChildFrameViewWin2D.cpp:420
Definition: ChildFrameViewWin2D.hpp:24
Vector3fT WindowToWorld(const wxPoint &p, float Third) const
Transforms point p from window to world coordinates/space. This is the inverse of WorldToWindow()...
Definition: ChildFrameViewWin2D.cpp:414
void Notify_EntChanged(SubjectT *Subject, const ArrayT< IntrusivePtrT< MapEditor::CompMapEntityT > > &Entities, EntityModDetailE Detail)
Definition: ChildFrameViewWin2D.cpp:234
ViewWindow2DT(wxWindow *Parent, ChildFrameT *ChildFrame, ViewTypeT InitialViewType)
The constructor.
This is the base class for all tools in the map editor.
Definition: Tool.hpp:34
This is the base class for all elements ("objects") that can exist in a map.
Definition: MapElement.hpp:57
wxPoint ToolToWindow(const wxPoint &p) const
Transforms point p from tool to window coordinates/space. This is the inverse of WindowToTool().
Definition: ChildFrameViewWin2D.cpp:380
Definition: ObserverPatternTools.hpp:49