Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

GUIWx.cpp

Go to the documentation of this file.
00001 #include "GUIWx.h"
00002 #include "Vector2.h"
00003 #include <wx/textctrl.h>
00004 #include <wx/dcmemory.h>
00005 #include <wx/statusbr.h>
00006 #include <wx/pen.h>
00007 
00008 GUIWx::GUIWx(int iniPanX, int iniPanY, float zoom, wxStatusBar *iniStatusBar) :
00009     GUI(iniPanX, iniPanY, zoom),//int iniPanX, int iniPanY, float zoom, int signX = 1, int signY = -1
00010     dc(0),
00011     statusBar(iniStatusBar){  
00012 }
00013 
00014 GUIWx::~GUIWx(){
00015 }
00016 
00017 void GUIWx::setDC(wxDC *iniDC) {
00018     dc = iniDC;
00019 }
00020 
00021 void GUIWx::clear(){
00022    dc->Clear();
00023 }
00024 
00025 void GUIWx::drawCircle(float x, float y, float r) {
00026     dc->DrawCircle(mapX(x), mapY(y), mapLen(r));
00027 }
00028 
00029 void GUIWx::drawLine(float x1, float y1, float x2, float y2) {
00030     dc->DrawLine(mapX(x1), mapY(y1), mapX(x2), mapY(y2));
00031 }
00032 
00033 void GUIWx::drawRectangle(float x, float y, float w, float h) {
00034     dc->DrawRectangle(mapX(x), mapY(y), (int)(zoomX * w), (int)(zoomY * h));
00035 }
00036 
00037 inline static float cosSum(float cos1, float sin1, float cos2, float sin2) {
00038     return cos1 * cos2 - sin1 * sin2;
00039 }
00040 
00041 inline static float sinSum(float cos1, float sin1, float cos2, float sin2) {
00042     return cos1 * sin2 + sin1 * cos2;
00043 }
00044 
00045 void GUIWx::drawCappedRectangle(float x, float y, float l, float R, float alpha) {
00046     static const float cos_pi5  = cos(M_PI/5);
00047     static const float sin_pi5  = sin(M_PI/5);
00048     static const float cos_2pi5 = cos(2 * M_PI / 5);
00049     static const float sin_2pi5 = sin(2 * M_PI / 5);
00050     float cosAlpha = cos(alpha);
00051     float sinAlpha = sin(alpha);
00052     float lcos = l * cosAlpha;
00053     float lsin = l * sinAlpha;
00054     float Rcos = R * cosAlpha;
00055     float Rsin = R * sinAlpha;
00056 
00057     float xc[6];
00058     float yc[6];
00059 
00060     xc[0] = Rsin;
00061     yc[0] = -Rcos;
00062     
00063     xc[1] = cosSum(Rsin, -Rcos, cos_pi5, sin_pi5);
00064     yc[1] = sinSum(Rsin, -Rcos, cos_pi5, sin_pi5);
00065 
00066     xc[2] = cosSum(Rsin, -Rcos, cos_2pi5, sin_2pi5);
00067     yc[2] = sinSum(Rsin, -Rcos, cos_2pi5, sin_2pi5);
00068     
00069     xc[3] = cosSum(Rsin, -Rcos, -cos_2pi5, sin_2pi5);
00070     yc[3] = sinSum(Rsin, -Rcos, -cos_2pi5, sin_2pi5);
00071 
00072     xc[4] = cosSum(Rsin, -Rcos, -cos_pi5, sin_pi5);
00073     yc[4] = sinSum(Rsin, -Rcos, -cos_pi5, sin_pi5);
00074 
00075     xc[5] = -Rsin;
00076     yc[5] = Rcos;
00077 
00078     wxPoint points[12];
00079     for (int i = 0; i < 6; ++i) {
00080         points[i].x     = mapX(x + lcos + xc[i]);
00081         points[i].y     = mapY(y + lsin + yc[i]);
00082         points[i + 6].x = mapX(x - lcos - xc[i]);
00083         points[i + 6].y = mapY(y - lsin - yc[i]);
00084     }
00085 
00086     dc->DrawPolygon(12, points, 0, 0);
00087 }
00088 
00089 void GUIWx::setBrushColor(Color color) {
00090    if(color.transparent){
00091       dc->SetBrush(*wxTRANSPARENT_BRUSH);
00092    } else {
00093       dc->SetBrush(*(wxTheBrushList->FindOrCreateBrush(wxColour(color.r, color.g, color.b),wxSOLID)));
00094    }
00095 }
00096 
00097 void GUIWx::setPenColor(Color color) {
00098    if(color.transparent){
00099       dc->SetPen(*wxTRANSPARENT_PEN);
00100    } else {
00101       dc->SetPen(*(wxThePenList->FindOrCreatePen(wxColour(color.r, color.g, color.b),1,wxSOLID)));
00102    }
00103 }
00104 
00105 void GUIWx::outText(const char* s){
00106    outTextStatusBar(s);
00107 }
00108 
00109 void GUIWx::outTextStatusBar(const char* s, int pos){
00110     if (statusBar) {
00111         statusBar->SetStatusText(s, pos);
00112     }
00113 }
00114 
00116 void GUIWx::drawArrow(float x1, float y1, float x2, float y2) {
00117     static const float arrowAngle = (float) M_PI / 8;
00118     static const float sinArrow   = sinf(arrowAngle);
00119     static const float cosArrow   = cosf(arrowAngle);
00120    //the length of the arrow tip:
00121    static const float arrowLength= (float) 0.08; 
00122    //the minimum length of the vector at which the arrow tip is drawn with 
00123    //default length; otherwise the arrow tip is drawn at vector length /2
00124    static const float minLength= 2*arrowLength; 
00125 
00126     drawLine(x1, y1, x2, y2);
00127 
00128     float dx = x1 - x2;
00129     float dy = y1 - y2;
00130    float length=sqrt(dx*dx+dy*dy);
00131     float tipDx, tipDy;
00132    if (length < minLength) {
00133       tipDx = dx / 2;
00134       tipDy = dy / 2;
00135    } else {
00136       float tipScale = arrowLength / length;
00137       tipDx = dx * tipScale;
00138       tipDy = dy * tipScale;
00139    }
00140    
00141     float dxCos = tipDx * cosArrow;
00142     float dxSin = tipDx * sinArrow;
00143     float dyCos = tipDy * cosArrow;
00144     float dySin = tipDy * sinArrow;
00145     
00146     drawLine(x2, y2, x2 + dxCos - dySin, y2 + dxSin + dyCos);
00147     drawLine(x2, y2, x2 + dxCos + dySin, y2 - dxSin + dyCos);
00148 }
00149 
00150 void GUIWx::drawForce(const Vector2& r, const Vector2& force){
00151    static const real maxForce = (const real) 0.2;
00152    static const real lengthPerForce=(const real) 0.3 / maxForce;
00153    drawArrow(r.x, r.y,
00154               r.x + force.x * lengthPerForce,
00155               r.y + force.y * lengthPerForce);
00156 }
00157 
00158 void GUIWx::drawTorque(const Vector2& r, const real alpha, const real torque){
00159    static const real maxTorque = (const real)1.2;
00160    static const real maxRotation=4*M_PI;
00161    static const real maxRadius=0.1f;
00162    static const real deltaRadius=0.05f;
00163    static const int steps=50;
00164    static const real arrowLength=M_PI/10;
00165    static const real arrowWidth= .04;
00166    int i;
00167    int sign;
00168    real angle, radius;
00169    wxCoord x1,x2,y1,y2;
00170 
00171    if(fabs(torque)*steps/maxTorque>1){
00172         drawLine(r.x, r.y, 
00173                  r.x + maxRadius * cos(alpha), 
00174                  r.y + maxRadius * sin(alpha)); 
00175       if (torque<0) sign=-1; else sign=1;
00176 
00177       x1 = mapX(r.x + maxRadius * cos(alpha));
00178       y1 = mapY(r.y + maxRadius * sin(alpha));
00179       
00180       i=1;
00181       while(i<steps && i<fabs(torque)*steps/maxTorque){
00182          angle=alpha+sign*i*maxRotation/steps;
00183          radius=maxRadius-i*deltaRadius/steps;
00184          x2=mapX(r.x+radius*cos(angle));
00185          y2=mapY(r.y+radius*sin(angle));
00186          dc->DrawLine(x1, y1, x2, y2);
00187          x1 = x2; 
00188             y1 = y2;
00189          i++;
00190       }
00191       //draw the arrow of the torque
00192       x2=x1; y2=y1;
00193       x1 = mapX(r.x+(radius-arrowWidth)*cos(angle-sign*arrowLength));
00194       y1 = mapY(r.y+(radius-arrowWidth)*sin(angle-sign*arrowLength));
00195       dc->DrawLine(x1, y1, x2, y2);
00196       x1=x2; y1=y2;
00197       x2 = mapX(r.x+(radius+arrowWidth)*cos(angle-sign*arrowLength));
00198       y2 = mapY(r.y+(radius+arrowWidth)*sin(angle-sign*arrowLength));
00199       dc->DrawLine(x1, y1, x2, y2);
00200    }
00201 }

Thyrix homepageUsers' guide

(C) Arxia 2004-2005