KiCad PCB EDA Suite
eda_3d_controller.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <tool/actions.h>
25 #include <tool/tool_manager.h>
26 #include <eda_3d_canvas.h>
27 #include <eda_3d_viewer_frame.h>
28 #include <id.h>
29 #include <kiface_base.h>
31 #include "eda_3d_actions.h"
33 
34 
36 {
37  CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
38 
41 
42  ctxMenu.AddSeparator();
45 
46  ctxMenu.AddSeparator();
49 
50  ctxMenu.AddSeparator();
53 
54  ctxMenu.AddSeparator();
56 
57  ctxMenu.AddSeparator();
62 
63  return true;
64 }
65 
66 
68 {
70 
71  wxASSERT( holder );
72 
73  m_canvas = nullptr;
74  m_boardAdapter = nullptr;
75  m_camera = nullptr;
76 
77  if( holder )
78  {
79  m_canvas = dynamic_cast<EDA_3D_CANVAS*>( holder->GetToolCanvas() );
80 
81  EDA_3D_BOARD_HOLDER* holder3d =
82  dynamic_cast<EDA_3D_BOARD_HOLDER*>( holder );
83 
84  wxASSERT( holder3d );
85 
86  if( holder3d )
87  {
88  m_boardAdapter = &holder3d->GetAdapter();
89  m_camera = &holder3d->GetCurrentCamera();
90  }
91  }
92 }
93 
94 
96 {
97  ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
98  CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
99  SELECTION dummySel;
100 
101  if( conditionalMenu )
102  conditionalMenu->Evaluate( dummySel );
103 
104  if( actionMenu )
105  actionMenu->UpdateAll();
106 
107  return 0;
108 }
109 
110 
112 {
113  // Main loop: keep receiving events
114  while( TOOL_EVENT* evt = Wait() )
115  {
116  if( evt->IsClick( BUT_RIGHT ) )
118  else
119  evt->SetPassEvent();
120  }
121 
122  return 0;
123 }
124 
125 
127 {
128  m_canvas->SetView3D( aEvent.Parameter<intptr_t>() );
129 
130  return 0;
131 }
132 
133 
135 {
136  switch( aEvent.Parameter<intptr_t>() )
137  {
138  case ACTIONS::CURSOR_UP: m_canvas->SetView3D( WXK_UP ); break;
139  case ACTIONS::CURSOR_DOWN: m_canvas->SetView3D( WXK_DOWN ); break;
140  case ACTIONS::CURSOR_LEFT: m_canvas->SetView3D( WXK_LEFT ); break;
141  case ACTIONS::CURSOR_RIGHT: m_canvas->SetView3D( WXK_RIGHT ); break;
142  default: wxFAIL; break;
143  }
144 
145  return 0;
146 }
147 
148 
150 {
151  double rotIncrement = glm::radians( m_rotationIncrement );
152 
153  switch( aEvent.Parameter<ROTATION_DIR>() )
154  {
155  case ROTATION_DIR::X_CW: m_camera->RotateX( -rotIncrement ); break;
156  case ROTATION_DIR::X_CCW: m_camera->RotateX( rotIncrement ); break;
158  case ROTATION_DIR::Y_CW: m_camera->RotateY( rotIncrement ); break;
159  case ROTATION_DIR::Y_CCW: m_camera->RotateY( -rotIncrement ); break;
160  case ROTATION_DIR::Z_CW: m_camera->RotateZ( -rotIncrement ); break;
161  case ROTATION_DIR::Z_CCW: m_camera->RotateZ( rotIncrement ); break;
162  default: wxFAIL; break;
163  }
164 
167  else
169 
170  return 0;
171 }
172 
173 
175 {
176  MATERIAL_MODE mode = aEvent.Parameter<MATERIAL_MODE>();
177 
179 
180  if( auto* viewer = dynamic_cast<EDA_3D_VIEWER_FRAME*>( m_toolMgr->GetToolHolder() ) )
181  viewer->NewDisplay( true );
182  else
184 
185  return 0;
186 }
187 
188 
190 {
192 
195  else
197 
198  return 0;
199 }
200 
201 
203 {
205 
207 
208  switch( flag )
209  {
210  // These commands do not request a 3D scene rebuild (and do not exist in raytracing):
212  case FL_AXIS:
214  break;
215 
216  // These commands do not request a 3D scene rebuild and exist in raytracing:
223  else
225 
226  break;
227 
231  // Loading 3D shapes can be needed if not yet loaded
233  {
234  RENDER_3D_LEGACY* render = static_cast< RENDER_3D_LEGACY* > ( m_canvas->GetCurrentRender() );
235  render->Load3dModelsIfNeeded();
237  }
238  else
240 
241  break;
242 
243  default:
244  {
245  if( auto viewer = dynamic_cast<EDA_3D_VIEWER_FRAME*>( m_toolMgr->GetToolHolder() ) )
246  viewer->NewDisplay( true );
247  else
249  break;
250  }
251  }
252 
253  return 0;
254 }
255 
256 
258 {
261 
262  if( m_canvas )
264 
265  return 0;
266 }
267 
268 
270 {
272  return 0;
273 }
274 
275 
277 {
278  bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
279  return doZoomInOut( direction, true );
280 }
281 
282 
284 {
285  bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
286  return doZoomInOut( direction, false );
287 }
288 
289 
290 int EDA_3D_CONTROLLER::doZoomInOut( bool aDirection, bool aCenterOnCursor )
291 {
292  if( m_canvas )
293  {
294  m_canvas->SetView3D( aDirection ? WXK_F1 : WXK_F2 );
296  }
297 
298  return 0;
299 }
300 
301 
303 {
304  if( m_canvas )
305  {
306  m_canvas->SetView3D( WXK_HOME );
308  }
309 
310  return 0;
311 }
312 
313 
315 {
318 
319  // Pan control
328 
329  // View rotation
346 
347  // Zoom control
352  // zoom in/out at cursor does not exist in 3D viewer but because F1 and F2 keys generate
353  // a zoomIn/zoomOut event, these events must be captured to use these hot keys. The actual
354  // zoom is the same as ZoomInOutCenter
357 
358  // Grid
364 
365  // Material
369 
370  // Visibility
386 }
387 
388 
static TOOL_ACTION rotateXCW
int ZoomFitScreen(const TOOL_EVENT &aEvent)
void RotateX(float aAngleInRadians)
Definition: camera.cpp:511
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
bool GetFlag(DISPLAY3D_FLG aFlag) const
Get a configuration status of a flag.
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
static TOOL_ACTION homeView
int ToggleOrtho(const TOOL_EVENT &aEvent)
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
static TOOL_ACTION zoomInCenter
Definition: actions.h:93
static TOOL_ACTION show10mmGrid
double m_rotationIncrement
Rotation increment for the rotate actions (degrees)
int ZoomInOutCenter(const TOOL_EVENT &aEvent)
static TOOL_ACTION materialCAD
static TOOL_ACTION pivotCenter
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
void SetFlag(DISPLAY3D_FLG aFlag, bool aState)
Set the status of a flag.
static TOOL_ACTION toggleAdhesive
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
int SetMaterial(const TOOL_EVENT &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:96
static TOOL_ACTION toggleSilk
bool Init() override
Init() is called once upon a registration of the tool.
static TOOL_ACTION toggleSolderMask
void UpdateAll()
Run update handlers for the menu and its submenus.
void SetGridType(GRID3D_TYPE aGridType) noexcept
Set the current grid.
int ZoomRedraw(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
void DisplayStatus()
Update the status bar with the position information.
static TOOL_ACTION panLeft
Definition: actions.h:129
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
int On3DGridSelection(const TOOL_EVENT &aEvent)
static TOOL_ACTION resetView
int doZoomInOut(bool aDirection, bool aCenterOnCursor)
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
static TOOL_ACTION controlActivate
static TOOL_ACTION showBBoxes
static TOOL_ACTION zoomOutCenter
Definition: actions.h:94
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
Definition: tool_event.cpp:88
static TOOL_ACTION flipView
static TOOL_ACTION toggleZones
static TOOL_ACTION viewRight
static TOOL_ACTION viewLeft
static TOOL_ACTION toggleECO
RENDER_ENGINE GetRenderEngine() const noexcept
void setTransitions() override
< Sets up handlers for various events.
static TOOL_ACTION showSMD
static TOOL_ACTION showVirtual
static TOOL_ACTION moveLeft
void RenderRaytracingRequest()
Request to render the current view in Raytracing mode.
static TOOL_ACTION toggleComments
static TOOL_ACTION show5mmGrid
int ViewControl(const TOOL_EVENT &aEvent)
static TOOL_ACTION showTHT
static TOOL_ACTION panDown
Definition: actions.h:128
virtual wxWindow * GetToolCanvas() const =0
Canvas access.
static TOOL_ACTION toggleSolderPaste
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:432
Object to render the board using openGL legacy mode.
static TOOL_ACTION rotateYCW
Generic, UI-independent tool event.
Definition: tool_event.h:152
static TOOL_ACTION panRight
Definition: actions.h:130
void RotateY(float aAngleInRadians)
Definition: camera.cpp:518
int ZoomInOut(const TOOL_EVENT &aEvent)
void SetMaterialMode(MATERIAL_MODE aMaterialMode) noexcept
int PanControl(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateXCCW
static TOOL_ACTION materialNormal
static TOOL_ACTION viewBottom
static TOOL_ACTION rotateZCCW
EDA_3D_CANVAS * m_canvas
static TOOL_ACTION updateMenu
Definition: actions.h:167
static TOOL_ACTION showAxis
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:118
int ToggleVisibility(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateZCW
ROTATION_DIR
Rotation direction for the 3d canvas.
Definition: 3d_enums.h:80
static TOOL_ACTION viewTop
GRID3D_TYPE
Grid types.
Definition: 3d_enums.h:99
static TOOL_ACTION viewBack
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
static TOOL_ACTION materialDiffuse
static TOOL_ACTION toggleRealisticMode
DISPLAY3D_FLG
Flags used in rendering options.
Definition: 3d_enums.h:34
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:294
static TOOL_ACTION moveDown
int Main(const TOOL_EVENT &aEvent)
void Load3dModelsIfNeeded()
Load footprint models if they are not already loaded, i.e.
static TOOL_ACTION show2_5mmGrid
static TOOL_ACTION show1mmGrid
static TOOL_ACTION viewFront
int RotateView(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateYCCW
static TOOL_ACTION zoomRedraw
Definition: actions.h:90
BOARD_ADAPTER * m_boardAdapter
bool SetView3D(int aKeycode)
Helper function to call view commands.
RENDER_3D_BASE * GetCurrentRender() const
static TOOL_ACTION moveUp
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:525
static TOOL_ACTION noGrid
void ToggleProjection()
Definition: camera.cpp:425
static TOOL_ACTION panUp
Definition: actions.h:127
static TOOL_ACTION zoomIn
Definition: actions.h:91
static TOOL_ACTION zoomOut
Definition: actions.h:92
static TOOL_ACTION toggleOrtho
static TOOL_ACTION moveRight
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:59
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
static TOOL_ACTION toggleBoardBody