wxArt2D
rendimg.cpp
Go to the documentation of this file.
1 /*! \file canextobj/src/rendimg.cpp
2  \author Klaas Holwerda
3 
4  Copyright: 2000-2004 (c) Klaas Holwerda
5 
6  Licence: wxWidgets Licence
7 
8  RCS-ID: $Id: rendimg.cpp,v 1.36 2009/07/24 16:35:01 titato Exp $
9 */
10 
11 #include "a2dprec.h"
12 
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16 
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20 
21 #include <wx/wfstream.h>
22 #include "wx/canvas/sttool.h"
23 
24 #include "wx/canextobj/rendimg.h"
25 
26 #include "wx/canvas/canobj.h"
27 #include "wx/canvas/cameleon.h"
28 #include "wx/editor/candoc.h"
29 #include "wx/canvas/drawer.h"
30 #include "wx/artbase/dcdrawer.h"
31 
32 //----------------------------------------------------------------------------
33 // globals
34 //----------------------------------------------------------------------------
35 
36 //----------------------------------------------------------------------------
37 // a2dRenderImage
38 //----------------------------------------------------------------------------
39 
40 IMPLEMENT_CLASS( a2dRenderImage, a2dCanvasObject )
41 
42 a2dRenderImage::a2dRenderImage( a2dCanvasObject* top, double xc, double yc, double w, double h )
43 {
44  int iw = 100;
45  int ih = 100;
46  if ( w > 1 )
47  iw = Round( w );
48  if ( h > 1 )
49  ih = Round( h );
50 
51  m_drawingPart = new a2dDrawingPart( iw, ih );
52  a2dDrawer2D* drawer2d = new a2dMemDcDrawer( iw, ih );
53  m_drawingPart->SetDrawer2D( drawer2d );
54 
55  m_drawingPart->SetShowObjectAndRender( top );
56 
57  m_scaleonresize = false;
58 
59  m_lworld.Translate( xc, yc );
60 
61  m_width = w;
62  m_height = h;
63 
64  m_is_cached = false;
65  m_b_is_cached = false;
66 }
67 
68 a2dRenderImage::~a2dRenderImage()
69 {
70 }
71 
72 a2dRenderImage::a2dRenderImage( const a2dRenderImage& other, CloneOptions options, a2dRefMap* refs )
73  : a2dCanvasObject( other, options, refs )
74 {
75  m_drawingPart = new a2dDrawingPart( *( other.m_drawingPart ) );
76  a2dDrawer2D* drawer2d = new a2dMemDcDrawer( *( ( a2dMemDcDrawer* ) other.m_drawingPart->GetDrawer2D()->GetHeight() ) );
77  m_drawingPart->SetDrawer2D( drawer2d );
78 
79  m_scaleonresize = other.m_scaleonresize;
80 
81  m_width = other.m_width;
82  m_height = other.m_height;
83 
84  m_is_cached = false;
85  m_b_is_cached = false;
86 #if defined(_DEBUG) && defined (SMART_POINTER_DEBUG)
87  //it is because the CurrentSmartPointerOwner can change in new a2dCanvasView()
88  CurrentSmartPointerOwner = this;
89 #endif
90 }
91 
93 {
94  return new a2dRenderImage( *this, options, refs );
95 }
96 
98 {
99  return m_drawingPart->GetShowObject();
100 }
101 
103 {
104  return m_drawingPart->SetShowObject( name );
105 }
106 
108 {
109  m_drawingPart->SetShowObjectAndRender( obj );
110  return true;
111 }
112 
113 void a2dRenderImage::SetWidth( double width )
114 {
115  m_width = width;
116  m_is_cached = false;
118  SetPending( true );
119 }
120 
121 void a2dRenderImage::SetHeight( double height )
122 {
123  m_height = height;
125  m_is_cached = false;
126  SetPending( true );
127 }
128 
129 bool a2dRenderImage::DoIsHitWorld( a2dIterC& WXUNUSED( ic ), a2dHitEvent& hitEvent )
130 {
131  return true;
132 }
133 
135 {
136  a2dBoundingBox bbox;
137  bbox.Expand( -m_width / 2, -m_height / 2 );
138  bbox.Expand( +m_width / 2, +m_height / 2 );
139  return bbox;
140 }
141 
142 bool a2dRenderImage::DoUpdate( UpdateMode mode, const a2dBoundingBox& childbox, const a2dBoundingBox& clipbox, const a2dBoundingBox& propbox )
143 {
144  if ( !m_bbox.GetValid() )
145  {
146  m_drawingPart->GetShowObject()->Update( mode );
147  m_bbox.Expand( -m_width / 2, -m_height / 2 );
148  m_bbox.Expand( +m_width / 2, +m_height / 2 );
150  return true;
151  }
152  return false;
153 }
154 
155 void a2dRenderImage::SetMappingWidthHeight( double vx1, double vy1, double width, double height )
156 {
157  m_drawingPart->GetDrawer2D()->SetMappingWidthHeight( vx1, vy1, width, height );
158  m_is_cached = false;
159  SetPending( true );
160 }
161 
162 void a2dRenderImage::SetMappingUpp( double vx1, double vy1, double xpp, double ypp )
163 {
164  m_drawingPart->GetDrawer2D()->SetMappingDeviceRect( 0, 0, m_drawingPart->GetDrawer2D()->GetBuffer().GetWidth(), m_drawingPart->GetDrawer2D()->GetBuffer().GetHeight() );
165  m_drawingPart->GetDrawer2D()->SetMappingUpp( vx1, vy1, xpp, ypp );
166  m_is_cached = false;
167  SetPending( true );
168 }
169 
170 void a2dRenderImage::SetMappingAbs( a2dIterC& ic, double vx1, double vy1, double width, double height )
171 {
172  double xs = m_drawingPart->GetDrawer2D()->GetBuffer().GetWidth() / ( double )m_cW;
173  double ys = m_drawingPart->GetDrawer2D()->GetBuffer().GetHeight() / ( double )m_cH;
174 
175  double x = m_drawingPart->GetDrawer2D()->DeviceToWorldX( ic.GetDrawer2D()->WorldToDeviceXRel( vx1 - GetBboxMinX() ) * xs );
176  double y;
177  if ( ic.GetDrawer2D()->GetYaxis() )
178  y = m_drawingPart->GetDrawer2D()->DeviceToWorldY( ic.GetDrawer2D()->WorldToDeviceYRel( vy1 - GetBboxMaxY() ) * ys );
179  else
180  y = m_drawingPart->GetDrawer2D()->DeviceToWorldY( ic.GetDrawer2D()->WorldToDeviceYRel( vy1 - GetBboxMinY() ) * ys );
181  double w = m_drawingPart->GetDrawer2D()->DeviceToWorldXRel( ic.GetDrawer2D()->WorldToDeviceXRel( width * xs ) );
182  double h = fabs( m_drawingPart->GetDrawer2D()->DeviceToWorldYRel( ic.GetDrawer2D()->WorldToDeviceYRel( height * ys ) ) );
183 
184  m_drawingPart->GetDrawer2D()->SetMappingWidthHeight( x, y, w, h );
185 
186  m_is_cached = false;
187  SetPending( true );
188 }
189 
190 void a2dRenderImage::DoRender( a2dIterC& ic, OVERLAP WXUNUSED( clipparent ) )
191 {
192  if ( !m_drawingPart->GetDrawing() ) return;
193 
194  int dw, dh;
195 
196  dw = ic.GetDrawer2D()->WorldToDeviceXRel( ic.GetTransform().Get_scaleX() * m_width );
197  dh = abs( ic.GetDrawer2D()->WorldToDeviceYRel( ic.GetTransform().Get_scaleY() * m_height ) );
198 
199  if ( dw == 0 || dh == 0 )
200  return;
201 
202  double x;
203  double y;
204  ic.GetTransform().TransformPoint( 0.0, 0.0, x, y );
205  x = ic.GetDrawer2D()->WorldToDeviceX( x );
206  y = ic.GetDrawer2D()->WorldToDeviceY( y );
207 
208  wxImage tmp;
209 
210  if ( m_is_cached && m_cImage.Ok() && ( m_cW == dw ) && ( m_cH == dh ) )
211  {
212  // use cached image
213  tmp = m_cImage;
214  }
215  else
216  {
217  if ( ( dw == m_drawingPart->GetDrawer2D()->GetBuffer().GetWidth() ) &&
218  ( dh == m_drawingPart->GetDrawer2D()->GetBuffer().GetHeight() ) )
219  {
220  //when world == device coordinates (a2dCanvas) do not need cache
221  tmp = m_drawingPart->GetDrawer2D()->GetBuffer().ConvertToImage();
222  }
223  else
224  {
225  //TODOif (m_scaleonresize)
226  m_drawingPart->GetDrawer2D()->SetMappingDeviceRect( 0, 0, ( int ) m_width, ( int ) m_height );
227 
228  m_drawingPart->UpdateArea( 0, 0, m_drawingPart->GetDrawer2D()->GetBuffer().GetWidth(), m_drawingPart->GetDrawer2D()->GetBuffer().GetHeight() );
229 
230  m_cImage.Destroy();
231  // create cached image
232  m_cImage = m_drawingPart->GetDrawer2D()->GetBuffer().ConvertToImage();
233  m_cImage = m_cImage.Scale( dw, dh );
234  m_is_cached = true;
235  m_cW = m_cImage.GetWidth();
236  m_cH = m_cImage.GetHeight();
237  m_b_is_cached = false;
238  tmp = m_cImage;
239  }
240  }
241 
242  wxPoint centr( dw / 2, dh / 2 ); //this doesn't make any difference (BUG?)
243  //anyway the rotation center is the middle of the image
244 
245  if ( ic.GetDrawer2D()->GetDrawStyle() != a2dWIREFRAME_INVERT ) //invert on image does not work optimal
246  {
247  ic.GetDrawer2D()->DrawImage( tmp, 0, 0, m_width, m_height, 255 );
248  }
249  else
250  {
251  if ( GetStroke() != *a2dTRANSPARENT_STROKE || GetFill() != *a2dTRANSPARENT_FILL )
252  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0.0, 0.0, ( int ) m_width, ( int ) m_height, 0 );
253  }
254  if ( m_flags.m_selected )
255  {
256  //layer pens for select layer are set already on higher levels
257  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0.0, 0.0, m_width, m_height, 0 );
258  }
259  else if ( GetStroke() != *a2dTRANSPARENT_STROKE || GetFill() != *a2dTRANSPARENT_FILL )
260  {
261  //feature of shape, so why not use it.
262  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0.0, 0.0, m_width, m_height, 0 );
263 
264  }
265 }
266 
267 
268 
269 
270 
271 
272 
273 
void SetMappingUpp(double vx1, double vy1, double xpp, double ypp)
Give the virtual size to be displayed, the mapping matrix will be calculated.
Definition: drawer2d.cpp:250
Display Part of a a2dDrawing, in which a2dCanvasObjects are shown.
Definition: drawer.h:470
int WorldToDeviceXRel(double x) const
convert x relative from world to device coordinates
Definition: drawer2d.h:460
int WorldToDeviceY(double y) const
convert y from world to device coordinates
Definition: drawer2d.h:455
class to map references to objects stored in XML, in order to make the connection later on...
Definition: gen.h:3462
a2dCanvasOFlags m_flags
holds flags for objects
Definition: canobj.h:2528
Ref Counted base object.
Definition: gen.h:1045
a2dRenderImage is an a2dCanvasObject that is able to display a complete a2dCanvasDocument as a a2dCan...
Definition: rendimg.h:42
double GetBboxMinY()
get minimum Y of the boundingbox in world coordinates relative to its parents
Definition: canobj.h:682
void SetMappingDeviceRect(int mapx, int mapy, int mapWidth, int mapHeight, bool remap=false)
to change the default mapping (to the complete buffer).
Definition: drawer2d.cpp:216
virtual bool Update(UpdateMode mode)
Update the state of the object according to its current position etc.
Definition: canobj.cpp:5149
double DeviceToWorldYRel(double y) const
convert y relative from device to world coordinates
Definition: drawer2d.h:449
a2dBoundingBox DoGetUnTransformedBbox(a2dBboxFlags flags=a2dCANOBJ_BBOX_NON) const
In derived object this should be overriden to calculate the boundingbox of the object without its chi...
Definition: rendimg.cpp:134
UpdateMode
Various mode flags for Update.
Definition: canobj.h:1091
void SetMappingWidthHeight(double vx1, double vy1, double width, double height)
Definition: rendimg.cpp:155
virtual void SetPending(bool pending)
set this object pending for update
Definition: canobj.cpp:2585
OVERLAP
Result of a a2dBoundingBox intersection or hittest.
Definition: bbox.h:24
bool GetYaxis() const
get y axis orientation
Definition: drawer2d.h:280
This class implements drawing functions for wxDC based drawing.
Definition: dcdrawer.h:203
bool m_scaleonresize
are scroll bars active?
Definition: rendimg.h:177
The base class for all drawable objects in a a2dCanvasDocument.
double GetBboxMinX()
get minimum X of the boundingbox in world coordinates relative to its parents
Definition: canobj.h:676
virtual void UpdateArea(int x, int y, int width, int height, wxUint8 id=0)
update/redraw part of the buffer, using the given a2dDrawing and ShowObject within that root...
Definition: drawer.cpp:1999
a2dCanvasObject is the base class for Canvas Objects.
Definition: canobj.h:371
double GetBboxMaxY()
get maximum Y of the boundingbox in world coordinates relative to its parents
Definition: canobj.h:694
void TransformPoint(double x, double y, double &tx, double &ty) const
Transform a point.
Definition: afmatrix.cpp:559
virtual void DrawCenterRoundedRectangle(double xc, double yc, double width, double height, double radius, bool pixelsize=false)
Draw CenterRoundedRectangle in world coordinates.
Definition: drawer2d.cpp:2040
bool m_selected
object is selected
Definition: candefs.h:235
a2dDrawStyle GetDrawStyle() const
get drawstyle used for drawing.
Definition: drawer2d.h:693
a2dAffineMatrix m_lworld
used for positioning the object (x,y,ang,scale etc.)
Definition: canobj.h:2559
int WorldToDeviceYRel(double y) const
convert y relative from world to device coordinates
Definition: drawer2d.h:465
double DeviceToWorldY(double y) const
convert y from device to world coordinates
Definition: drawer2d.h:439
bool GetValid() const
returns true if boundingbox is calculated properly and therefore its valid flag is set...
Definition: bbox.cpp:299
void Expand(const a2dPoint2D &, const a2dPoint2D &)
expand boundingbox width two points
Definition: bbox.cpp:155
a2dCanvasObject * GetShowObject() const
return pointer of then currently shown object on the canvas.
Definition: rendimg.cpp:97
a2dDrawer2D * GetDrawer2D() const
get current a2dDrawer2D
Definition: canobj.cpp:636
Drawing context abstraction.
Definition: drawer2d.h:177
a2dCanvasObject * GetShowObject() const
return pointer of then currently shown object on the drawer.
Definition: drawer.h:680
int GetHeight() const
get buffer/device height
Definition: drawer2d.h:395
virtual void DrawImage(const wxImage &image, double x, double y, double width, double height, wxUint8 Opacity=255)=0
Draw wxImage in world coordinates.
Contains graphical drawing context specific classes. a2dDcDrawer and derived classes are used for dra...
void SetShowObjectAndRender(a2dCanvasObject *obj)
Used temporarely in rendering bitmas or in printing, to Set the show object and redraw the whole part...
Definition: drawer.cpp:3069
int WorldToDeviceX(double x) const
convert x from world to device coordinates
Definition: drawer2d.h:453
a2dDrawer2D * GetDrawer2D()
get the internal m_drawer2D that is used for rendering the document
Definition: drawer.h:1125
virtual a2dObject * DoClone(CloneOptions options, a2dRefMap *refs) const
Clone this object and return a pointer to the new object.
Definition: rendimg.cpp:92
void SetMappingAbs(a2dIterC &ic, double vx1, double vy1, double width, double height)
set mapping using absolute world coordinates of parent
Definition: rendimg.cpp:170
while iterating a a2dCanvasDocument, this holds the context.
Definition: canobj.h:3212
All updates of these modes force an update (e.g. update non-pending valid bounding boxes) ...
Definition: canobj.h:1107
void MapBbox(const a2dAffineMatrix &matrix)
Definition: bbox.cpp:445
double Get_scaleX() const
return scaling in X
Definition: afmatrix.cpp:755
special a2dCanvasObject to make a multi view hierachy.
virtual wxBitmap GetBuffer() const =0
Return the buffer as a bitmap.
double DeviceToWorldX(double x) const
convert x from device to world coordinates
Definition: drawer2d.h:437
a2dCanvasObject * SetShowObject(const wxString &name)
set object available in the a2dDrawing to be shown on the drawer
Definition: drawer.cpp:2947
void DoRender(a2dIterC &ic, OVERLAP clipparent)
render derived object
Definition: rendimg.cpp:190
a2dCanvasObject * SetShowObject(const wxString &name)
set object available in the a2dCanvasDocument to be shown on the canvas
Definition: rendimg.cpp:102
const a2dStroke * a2dTRANSPARENT_STROKE
global a2dStroke stock object for TRANSPARENT stroking
The a2dBoundingBox class stores one a2dBoundingBox of a a2dCanvasObject.
Definition: bbox.h:39
bool DoIsHitWorld(a2dIterC &ic, a2dHitEvent &hitEvent)
Does hit test on the object (exclusif child objects)
Definition: rendimg.cpp:129
a2dDrawing * GetDrawing() const
get drawing via top object
Definition: drawer.cpp:726
double Get_scaleY() const
return scaling in Y
Definition: afmatrix.cpp:777
the a2dDrawingPart is a a2dView specially designed for displaying parts of a a2dDrawing. It uses a a2dDrawer2D to actually redraw things from the document, by giving that a2dDrawer2D as drawing context to the document, and telling the document to redraw a certain rectangular area. At that last is what this class is for. It optimizes the areas to be redrawn after object in the document were changed. To do that it combines redraw areas to a minimal set of redrawing areas. All the administration for this and the way things will be redrawn is from this view.
bool DoUpdate(UpdateMode mode, const a2dBoundingBox &childbox, const a2dBoundingBox &clipbox, const a2dBoundingBox &propbox)
calculate for top the boundingbox&#39;s that became invalid.
Definition: rendimg.cpp:142
#define Round(x)
round to integer such that e.g 2.5 &lt; x &lt; 3.5 becomes 3
Definition: artglob.h:60
a2dBoundingBox m_bbox
boundingbox in world coordinates
Definition: canobj.h:2539
const a2dAffineMatrix & GetTransform() const
Get the accumulated transform up to and including m_lworld of the current object. ...
Definition: canobj.cpp:663
stack based tools controller and tools for drawing and editing.
double DeviceToWorldXRel(double x) const
convert x relative from device to world coordinates
Definition: drawer2d.h:444
CloneOptions
options for cloning
Definition: gen.h:1200
virtual void SetMappingWidthHeight(double vx1, double vy1, double width, double height)
Give the virtual size to be displayed, the mapping matrix will be calculated.
Definition: drawer2d.cpp:333
structure to give as parameter to member functions of a2dCanvasObject
Definition: canobj.h:252
const a2dFill * a2dTRANSPARENT_FILL
global a2dFill stock object for TRANSPARENT filling
rendimg.cpp Source File -- Sun Oct 12 2014 17:04:23 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation