00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_ENGINE2_WORLDVIEW_HPP_)
00012 #define _XRB_ENGINE2_WORLDVIEW_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include "xrb_color.hpp"
00017 #include "xrb_engine2_types.hpp"
00018 #include "xrb_framehandler.hpp"
00019 #include "xrb_rendercontext.hpp"
00020 #include "xrb_transform2.hpp"
00021 #include "xrb_vector.hpp"
00022
00023 namespace Xrb
00024 {
00025
00026 class EventKey;
00027 class EventMouseButton;
00028 class EventMouseWheel;
00029 class EventMouseMotion;
00030
00031 namespace Engine2
00032 {
00033
00034 class WorldViewWidget;
00035 class World;
00036 class ObjectLayer;
00037
00038
00039
00040
00041
00042
00043 class WorldView : protected FloatTransform2, public FrameHandler
00044 {
00045 public:
00046
00047 struct DrawInfo
00048 {
00049
00050 Uint32 m_drawn_opaque_object_count;
00051
00052 Uint32 m_drawn_transparent_object_count;
00053
00054 void Reset ()
00055 {
00056 m_drawn_opaque_object_count = 0;
00057 m_drawn_transparent_object_count = 0;
00058 }
00059 };
00060
00061 WorldView (WorldViewWidget *parent_world_view_widget);
00062 virtual ~WorldView ();
00063
00064 inline WorldViewWidget *ParentWorldViewWidget () const { return m_parent_world_view_widget; }
00065 inline World *GetWorld () const { return m_world; }
00066 inline DrawInfo const &GetDrawInfo () const { return m_draw_info; }
00067 inline Float ZoomFactor () const { return m_zoom_factor; }
00068 inline FloatVector2 Center () const { return -Translation(); }
00069 inline Float Angle () const { return -FloatTransform2::Angle(); }
00070 inline bool IsViewLocked () const { return m_is_view_locked; }
00071 inline bool DrawBorderGridLines () const { return m_draw_border_grid_lines; }
00072 FloatMatrix2 CompoundTransformation () const;
00073 Float ViewDepth (ObjectLayer const *object_layer) const;
00074 inline Float ParallaxFactor (
00075 Float const view_depth,
00076 Float const layer_depth) const
00077 {
00078 ASSERT1(view_depth < layer_depth);
00079 return layer_depth - view_depth;
00080 }
00081 Float ParallaxedViewRadius (ObjectLayer const *object_layer) const;
00082 FloatMatrix2 ParallaxedTransformation (
00083 FloatMatrix2 const &world_to_view,
00084 FloatMatrix2 const &view_to_whatever,
00085 ObjectLayer const *object_layer) const;
00086 Float MinorAxisRadius () const;
00087 Float MajorAxisRadius () const;
00088 Float CornerRadius () const;
00089 inline bool IsTransformScalingBasedUponWidgetRadius () const
00090 {
00091 return m_is_transform_scaling_based_upon_widget_radius;
00092 }
00093
00094 inline void SetIsViewLocked (bool const is_view_locked) { m_is_view_locked = is_view_locked; }
00095 inline void SetDrawBorderGridLines (bool const draw_border_grid_lines) { m_draw_border_grid_lines = draw_border_grid_lines; }
00096
00097 void SetCenter (FloatVector2 const &position);
00098 void SetZoomFactor (Float zoom_factor);
00099 void SetAngle (Float angle);
00100
00101 void SetMinZoomFactor (Float min_zoom_factor);
00102 void SetMaxZoomFactor (Float max_zoom_factor);
00103
00104 inline void SetWorld (World *world)
00105 {
00106 m_world = world;
00107 HandleAttachedWorld();
00108 }
00109 void SetIsTransformScalingBasedUponWidgetRadius (bool is_transform_scaling_based_upon_widget_radius);
00110
00111 inline void LockView ()
00112 {
00113 m_is_view_locked = true;
00114 }
00115 inline void UnlockView ()
00116 {
00117 m_is_view_locked = false;
00118 }
00119
00120 void MoveView (FloatVector2 const &delta_position);
00121 void ZoomView (Float delta_zoom_factor);
00122 void RotateView (Float delta_angle);
00123
00124
00125 void DetachFromWorld ();
00126
00127
00128 virtual void Draw (RenderContext const &render_context);
00129
00130
00131 virtual bool ProcessKeyEvent (EventKey const *e);
00132
00133 virtual bool ProcessMouseButtonEvent (EventMouseButton const *e);
00134
00135 virtual bool ProcessMouseWheelEvent (EventMouseWheel const *e);
00136
00137 virtual bool ProcessMouseMotionEvent (EventMouseMotion const *e);
00138
00139 virtual void HandleFocus () { }
00140 virtual void HandleUnfocus () { }
00141
00142 protected:
00143
00144 ObjectLayer *MainObjectLayer () const;
00145 Float GridScaleUnit (Uint32 grid_scale) const;
00146
00147 FloatMatrix2 const &ParallaxedWorldToWorldView () const;
00148 FloatMatrix2 const &ParallaxedWorldViewToWorld () const;
00149 FloatMatrix2 const &ParallaxedWorldToScreen () const;
00150 FloatMatrix2 const &ParallaxedScreenToWorld () const;
00151
00152
00153 virtual void HandleAttachedWorld () { }
00154
00155
00156 virtual void HandleFrame () { }
00157
00158 void DrawGridLines (RenderContext const &render_context);
00159 void DrawGridLineSet (
00160 RenderContext const &render_context,
00161 Uint32 grid_scale,
00162 bool is_border_grid,
00163 bool is_wrapped,
00164 FloatVector2 const &view_center,
00165 Float view_radius,
00166 Color color);
00167
00168
00169 Float CalculateViewRadius (
00170 FloatMatrix2 const &screen_to_world,
00171 ScreenCoordRect const &view_rect,
00172 FloatVector2 const &view_center) const;
00173
00174
00175
00176 void PushParallaxedGLProjectionMatrix (
00177 RenderContext const &render_context,
00178 ObjectLayer const *object_layer);
00179 void PopGLProjectionMatrix ();
00180
00181 enum GridLineType
00182 {
00183 GR_BELOW_MAIN_LAYER = 0,
00184 GR_NO_DRAW,
00185 GR_ABOVE_MAIN_LAYER,
00186
00187 GR_COUNT
00188 };
00189
00190
00191 WorldViewWidget *m_parent_world_view_widget;
00192
00193 World *m_world;
00194
00195 DrawInfo m_draw_info;
00196
00198
00199
00200
00201 GridLineType m_grid_line_type;
00202
00203 Uint8 m_grid_number_base;
00204
00205
00206 Uint32 m_current_grid_scale;
00207
00208 private:
00209
00210 void DirtyAllParallaxedTransformations ();
00211
00212
00213
00214
00215 Float m_zoom_factor;
00216
00217
00218 Float m_min_zoom_factor;
00219
00220
00221 Float m_max_zoom_factor;
00222
00223
00224
00225 bool m_is_view_locked;
00226
00227
00228 bool m_is_gl_projection_matrix_in_use;
00229
00230 bool m_draw_border_grid_lines;
00231
00232
00233
00234
00235 bool m_is_transform_scaling_based_upon_widget_radius;
00236
00237
00238 TransparentObjectVector m_transparent_object_vector;
00239
00241
00242
00243 mutable bool m_is_parallaxed_world_to_view_dirty;
00244 mutable FloatMatrix2 m_parallaxed_world_to_view;
00245 mutable bool m_is_parallaxed_view_to_world_dirty;
00246 mutable FloatMatrix2 m_parallaxed_view_to_world;
00247 mutable bool m_is_parallaxed_world_to_screen_dirty;
00248 mutable FloatMatrix2 m_parallaxed_world_to_screen;
00249 mutable bool m_is_parallaxed_screen_to_world_dirty;
00250 mutable FloatMatrix2 m_parallaxed_screen_to_world;
00251
00252 };
00253
00254 }
00255
00256 }
00257
00258 #endif // !defined(_XRB_ENGINE2_WORLDVIEW_HPP_)
00259