You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2011 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. // wxWidgets imports
  25. #include <wx/wx.h>
  26. #include <wx/graphics.h>
  27. #include <wx/timer.h>
  28. #include <wx/event.h>
  29. #include <wx/wxprec.h>
  30. #include <wx/cmdline.h>
  31. // Graphics Abstraction Layer imports
  32. #include <gal/common/definitions.h>
  33. #include <gal/common/graphics_abstraction_layer.h>
  34. #include <gal/opengl/opengl_gal.h>
  35. #include <gal/common/stroke_font.h>
  36. #include <gal/common/color4d.h>
  37. #include <gal/font/newstroke_font.h>
  38. #include <math/vector2d.h>
  39. #include <math/matrix3x3.h>
  40. #include "sweet_editor_panel.h"
  41. #define screenSizeX 640
  42. #define screenSizeY 480
  43. /**
  44. * Class SWEET_EDIT
  45. * implements an editor for SWEET, including support for inheritance and
  46. * LIB_TABLE configuration.
  47. */
  48. class SWEET_FRAME: public wxFrame
  49. {
  50. public:
  51. // @brief Constructor
  52. SWEET_FRAME( wxWindow* parent, wxWindowID id, const wxString& title,
  53. const wxPoint& pos, const wxSize& size );
  54. // @brief Destructor
  55. ~SWEET_FRAME();
  56. private:
  57. bool m_isReady;
  58. bool m_isPanning;
  59. SCH::CANVAS* m_gal;
  60. wxSize m_screenSize;
  61. VECTOR2D m_worldSize;
  62. VECTOR2D m_startMousePoint;
  63. VECTOR2D m_startLookAtPoint;
  64. /*
  65. double m_alpha;
  66. MATRIX3x3D m_startMatrix;
  67. STROKE_FONT m_font;
  68. */
  69. // Event handlers
  70. void OnMotion( wxMouseEvent& event );
  71. void OnMouseWheel( wxMouseEvent& event );
  72. void OnRightDown( wxMouseEvent& event );
  73. void OnRightUp( wxMouseEvent& event );
  74. // void OnRedraw( wxCommandEvent& event );
  75. };
  76. SWEET_FRAME::SWEET_FRAME( wxWindow* parent, wxWindowID id, const wxString& title,
  77. const wxPoint& pos, const wxSize& size ) :
  78. wxFrame( parent, id, title, pos, size ),
  79. m_screenSize( size.x, size.y )
  80. {
  81. m_isPanning = false;
  82. SWEET_EDITOR_PANEL* panel =
  83. new SWEET_EDITOR_PANEL( this, wxID_ANY, wxPoint( 0, 0 ), wxSize( -1, -1 ), 0 );
  84. m_gal = (SCH::CANVAS*) panel->FindWindowByName( wxT( "GLCanvas" ), panel );
  85. wxASSERT( m_gal );
  86. m_gal->SetMouseListener( this );
  87. Connect( wxEVT_MOTION, wxMouseEventHandler( SWEET_FRAME::OnMotion ) );
  88. Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( SWEET_FRAME::OnMouseWheel ) );
  89. Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( SWEET_FRAME::OnRightDown ) );
  90. Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( SWEET_FRAME::OnRightUp ) );
  91. }
  92. SWEET_FRAME::~SWEET_FRAME()
  93. {
  94. delete m_gal;
  95. }
  96. void SWEET_FRAME::OnMotion( wxMouseEvent& event )
  97. {
  98. VECTOR2D mousePoint( event.GetX(), event.GetY() );
  99. if( event.Dragging() )
  100. {
  101. if( m_isPanning )
  102. {
  103. MATRIX3x3D matrix = m_gal->GetWorldScreenMatrix().Inverse();
  104. VECTOR2D delta = matrix.GetScale().x * ( m_startMousePoint - mousePoint );
  105. m_gal->SetLookAtPoint( m_startLookAtPoint + delta );
  106. m_gal->PaintScene();
  107. }
  108. }
  109. else
  110. {
  111. m_gal->DrawCursor( mousePoint );
  112. }
  113. }
  114. void SWEET_FRAME::OnMouseWheel( wxMouseEvent& event )
  115. {
  116. double zoomScale = ( event.GetWheelRotation() > 0.0 ) ? 1.1 : 0.9;
  117. m_gal->SetZoomFactor( m_gal->GetZoomFactor() * zoomScale );
  118. m_gal->PaintScene();
  119. }
  120. void SWEET_FRAME::OnRightDown( wxMouseEvent& event )
  121. {
  122. m_isPanning = true;
  123. m_startMousePoint = VECTOR2D( event.GetX(), event.GetY() );
  124. m_startLookAtPoint = m_gal->GetLookAtPoint();
  125. }
  126. void SWEET_FRAME::OnRightUp( wxMouseEvent& event )
  127. {
  128. m_isPanning = false;
  129. }
  130. static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
  131. {
  132. wxCMD_LINE_PARAM,
  133. NULL,
  134. NULL,
  135. #if wxCHECK_VERSION( 2, 9, 0 )
  136. "filename of libtable",
  137. #else
  138. wxT( "filename of libtable" ),
  139. #endif
  140. wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE
  141. },
  142. { wxCMD_LINE_NONE }
  143. };
  144. class GAL_TEST_APPLICATION: public wxApp
  145. {
  146. public:
  147. GAL_TEST_APPLICATION();
  148. private:
  149. virtual bool OnInit();
  150. virtual int OnExit();
  151. virtual int OnRun();
  152. virtual void OnInitCmdLine( wxCmdLineParser& aParser );
  153. virtual bool OnCmdLineParsed( wxCmdLineParser& aParser );
  154. };
  155. GAL_TEST_APPLICATION::GAL_TEST_APPLICATION()
  156. {
  157. }
  158. bool GAL_TEST_APPLICATION::OnInit()
  159. {
  160. if ( !wxApp::OnInit() )
  161. return false;
  162. SWEET_FRAME* frame = new SWEET_FRAME( (wxFrame *) NULL,
  163. -1, wxT( "SWEET Editor" ),
  164. wxPoint( screenSizeX + 10, 0 ),
  165. wxSize( screenSizeX, screenSizeY ) );
  166. frame->Show( true );
  167. return true;
  168. }
  169. int GAL_TEST_APPLICATION::OnExit()
  170. {
  171. return 0;
  172. }
  173. int GAL_TEST_APPLICATION::OnRun()
  174. {
  175. int exitcode = wxApp::OnRun();
  176. return exitcode;
  177. }
  178. void GAL_TEST_APPLICATION::OnInitCmdLine( wxCmdLineParser& parser )
  179. {
  180. parser.SetDesc( g_cmdLineDesc );
  181. parser.SetSwitchChars( wxT( "-" ) );
  182. }
  183. bool GAL_TEST_APPLICATION::OnCmdLineParsed( wxCmdLineParser& parser )
  184. {
  185. return true;
  186. }
  187. DECLARE_APP( GAL_TEST_APPLICATION )
  188. IMPLEMENT_APP( GAL_TEST_APPLICATION )