KiCad PCB EDA Suite
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.h>
28 #include <id.h>
29 #include <kiface_i.h>
30 #include <3d_viewer_id.h>
31 #include <tools/3d_controller.h>
32 #include "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_NEG: m_camera->RotateX( -rotIncrement ); break;
156  case ROTATION_DIR::X_POS: m_camera->RotateX( rotIncrement ); break;
157  case ROTATION_DIR::Y_NEG: m_camera->RotateY( -rotIncrement ); break;
158  case ROTATION_DIR::Y_POS: m_camera->RotateY( rotIncrement ); break;
159  case ROTATION_DIR::Z_NEG: m_camera->RotateZ( -rotIncrement ); break;
160  case ROTATION_DIR::Z_POS: m_camera->RotateZ( rotIncrement ); break;
161  default: wxFAIL; break;
162  }
163 
166  else
168 
169  return 0;
170 }
171 
172 
174 {
175  MATERIAL_MODE mode = aEvent.Parameter<MATERIAL_MODE>();
176 
178 
179  if( EDA_3D_VIEWER* viewer = dynamic_cast<EDA_3D_VIEWER*>( m_toolMgr->GetToolHolder() ) )
180  viewer->NewDisplay( true );
181  else
183 
184  return 0;
185 }
186 
187 
189 {
191 
194  else
196 
197  return 0;
198 }
199 
200 
202 {
203  DISPLAY3D_FLG flag = aEvent.Parameter<DISPLAY3D_FLG>();
204 
205  m_boardAdapter->SetFlag( flag, !m_boardAdapter->GetFlag( flag ) );
206 
207  switch( flag )
208  {
214  case FL_AXIS:
216  break;
217 
218  default:
219  {
220  EDA_3D_VIEWER* viewer = dynamic_cast<EDA_3D_VIEWER*>( m_toolMgr->GetToolHolder() );
221 
222  if( viewer )
223  viewer->NewDisplay( true );
224  else
226  break;
227  }
228  }
229 
230  return 0;
231 }
232 
233 
235 {
236  GRID3D_TYPE grid = aEvent.Parameter<GRID3D_TYPE>();
237  m_boardAdapter->GridSet( grid );
238 
239  if( m_canvas )
241 
242  return 0;
243 }
244 
245 
247 {
249  return 0;
250 }
251 
252 
254 {
255  bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
256  return doZoomInOut( direction, true );
257 }
258 
259 
261 {
262  bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
263  return doZoomInOut( direction, false );
264 }
265 
266 
267 int EDA_3D_CONTROLLER::doZoomInOut( bool aDirection, bool aCenterOnCursor )
268 {
269  if( m_canvas )
270  {
271  m_canvas->SetView3D( aDirection ? WXK_F1 : WXK_F2 );
273  }
274 
275  return 0;
276 }
277 
278 
280 {
281  if( m_canvas )
282  {
283  m_canvas->SetView3D( WXK_HOME );
285  }
286 
287  return 0;
288 }
289 
290 
292 {
295 
296  // Pan control
305 
306  // View rotation
323 
324  // Zoom control
331 
332  // Grid
338 
339  // Material
343 
344  // Visibility
367 }
368 
369 
static TOOL_ACTION rotateXCW
Definition: 3d_actions.h:47
int ZoomFitScreen(const TOOL_EVENT &aEvent)
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
bool GetFlag(DISPLAY3D_FLG aFlag) const
GetFlag - get a configuration status of a flag.
void GridSet(GRID3D_TYPE aGridType) noexcept
GridSet - set the current grid.
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
static TOOL_ACTION homeView
Definition: 3d_actions.h:57
int ToggleOrtho(const TOOL_EVENT &aEvent)
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
static TOOL_ACTION zoomInCenter
Definition: actions.h:91
static TOOL_ACTION show10mmGrid
Definition: 3d_actions.h:70
double m_rotationIncrement
Rotation increment for the rotate actions (degrees)
int ZoomInOutCenter(const TOOL_EVENT &aEvent)
static TOOL_ACTION materialCAD
Definition: 3d_actions.h:77
static TOOL_ACTION pivotCenter
Definition: 3d_actions.h:46
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:43
static TOOL_ACTION showBoundingBoxes
Definition: 3d_actions.h:83
void SetFlag(DISPLAY3D_FLG aFlag, bool aState)
SetFlag - set the status of a flag.
static TOOL_ACTION toggleAdhesive
Definition: 3d_actions.h:95
void RotateY(float aAngleInRadians)
Definition: ccamera.cpp:527
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
int SetMaterial(const TOOL_EVENT &aEvent)
static TOOL_ACTION zoomFitScreen
Definition: actions.h:94
static TOOL_ACTION attributesVirtual
Definition: 3d_actions.h:82
static TOOL_ACTION toggleSilk
Definition: 3d_actions.h:96
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
static TOOL_ACTION toggleSolderMask
Definition: 3d_actions.h:97
static TOOL_ACTION showRefractions
Definition: 3d_actions.h:87
void UpdateAll()
Runs update handlers for the menu and its submenus.
int ZoomRedraw(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
void DisplayStatus()
DisplayStatus - Update the status bar with the position information.
static TOOL_ACTION panLeft
Definition: actions.h:127
void Request_refresh(bool aRedrawImmediately=true)
Request_refresh - Schedule a refresh update of the canvas.
int On3DGridSelection(const TOOL_EVENT &aEvent)
static TOOL_ACTION resetView
Definition: 3d_actions.h:58
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))
Function Go()
static TOOL_ACTION controlActivate
Definition: 3d_actions.h:44
static TOOL_ACTION zoomOutCenter
Definition: actions.h:92
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:67
static TOOL_ACTION flipView
Definition: 3d_actions.h:59
static TOOL_ACTION toggleZones
Definition: 3d_actions.h:94
static TOOL_ACTION viewRight
Definition: 3d_actions.h:65
static TOOL_ACTION viewLeft
Definition: 3d_actions.h:64
static TOOL_ACTION toggleECO
Definition: 3d_actions.h:100
void setTransitions() override
Sets up handlers for various events.
static TOOL_ACTION antiAliasing
Definition: 3d_actions.h:89
void RotateZ(float aAngleInRadians)
Definition: ccamera.cpp:534
void RotateX(float aAngleInRadians)
Definition: ccamera.cpp:520
static TOOL_ACTION moveLeft
Definition: 3d_actions.h:53
void RenderRaytracingRequest()
RenderRaytracingRequest - Request to render the current view in Raytracing mode.
static TOOL_ACTION toggleComments
Definition: 3d_actions.h:99
static TOOL_ACTION show5mmGrid
Definition: 3d_actions.h:71
int ViewControl(const TOOL_EVENT &aEvent)
static TOOL_ACTION panDown
Definition: actions.h:126
virtual wxWindow * GetToolCanvas() const =0
Canvas access.
static TOOL_ACTION toggleSolderPaste
Definition: 3d_actions.h:98
T Parameter() const
Function Parameter() Returns a non-standard parameter assigned to the event.
Definition: tool_event.h:435
static TOOL_ACTION rotateYCW
Definition: 3d_actions.h:49
TOOL_EVENT.
Definition: tool_event.h:171
static TOOL_ACTION panRight
Definition: actions.h:128
void ToggleProjection()
Definition: ccamera.cpp:441
int ZoomInOut(const TOOL_EVENT &aEvent)
int PanControl(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateXCCW
Definition: 3d_actions.h:48
RENDER_ENGINE RenderEngineGet() const noexcept
RenderEngineGet.
static TOOL_ACTION materialNormal
Definition: 3d_actions.h:75
static TOOL_ACTION viewBottom
Definition: 3d_actions.h:67
static TOOL_ACTION rotateZCCW
Definition: 3d_actions.h:52
EDA_3D_CANVAS * m_canvas
static TOOL_ACTION updateMenu
Definition: actions.h:167
static TOOL_ACTION addFloor
Definition: 3d_actions.h:86
static TOOL_ACTION showAxis
Definition: 3d_actions.h:93
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:117
int ToggleVisibility(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateZCW
Definition: 3d_actions.h:51
ROTATION_DIR
Rotation direction for the 3d canvas.
Definition: 3d_enums.h:79
static TOOL_ACTION viewTop
Definition: 3d_actions.h:66
GRID3D_TYPE
Grid types.
Definition: 3d_enums.h:98
static TOOL_ACTION viewBack
Definition: 3d_actions.h:63
void AddSeparator(int aOrder=ANY_ORDER)
Adds a separator to the menu.
static TOOL_ACTION attributesSMD
Definition: 3d_actions.h:81
static TOOL_ACTION materialDiffuse
Definition: 3d_actions.h:76
static TOOL_ACTION toggleRealisticMode
Definition: 3d_actions.h:91
DISPLAY3D_FLG
Flags used in rendering options.
Definition: 3d_enums.h:34
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:301
static TOOL_ACTION moveDown
Definition: 3d_actions.h:56
int Main(const TOOL_EVENT &aEvent)
static TOOL_ACTION proceduralTextures
Definition: 3d_actions.h:85
static TOOL_ACTION show2_5mmGrid
Definition: 3d_actions.h:72
static TOOL_ACTION show1mmGrid
Definition: 3d_actions.h:73
Declaration of the eda_3d_viewer class.
static TOOL_ACTION attributesTHT
Definition: 3d_actions.h:80
static TOOL_ACTION viewFront
Definition: 3d_actions.h:62
int RotateView(const TOOL_EVENT &aEvent)
static TOOL_ACTION rotateYCCW
Definition: 3d_actions.h:50
static TOOL_ACTION zoomRedraw
Definition: actions.h:88
BOARD_ADAPTER * m_boardAdapter
bool SetView3D(int aKeycode)
SetView3D - Helper function to call view commands.
static TOOL_ACTION postProcessing
Definition: 3d_actions.h:90
static TOOL_ACTION moveUp
Definition: 3d_actions.h:55
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
static TOOL_ACTION noGrid
Definition: 3d_actions.h:69
static TOOL_ACTION panUp
Definition: actions.h:125
static TOOL_ACTION zoomIn
Definition: actions.h:89
static TOOL_ACTION zoomOut
Definition: actions.h:90
static TOOL_ACTION showReflections
Definition: 3d_actions.h:88
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
Definition: eda_3d_viewer.h:65
static TOOL_ACTION toggleOrtho
Definition: 3d_actions.h:60
void NewDisplay(bool aForceImmediateRedraw=false)
Reload and refresh (rebuild) the 3D scene.
static TOOL_ACTION renderShadows
Definition: 3d_actions.h:84
static TOOL_ACTION moveRight
Definition: 3d_actions.h:54
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Adds a menu entry to run a TOOL_ACTION on selected items.
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
void MaterialModeSet(MATERIAL_MODE aMaterialMode) noexcept
MaterialModeSet.
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION toggleBoardBody
Definition: 3d_actions.h:92