KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2021 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"
34
35
37{
38 CONDITIONAL_MENU& ctxMenu = m_menu.GetMenu();
39
42
43 ctxMenu.AddSeparator();
46
47 ctxMenu.AddSeparator();
50
51 ctxMenu.AddSeparator();
54
55 ctxMenu.AddSeparator();
57
58 ctxMenu.AddSeparator();
63
64 return true;
65}
66
67
69{
71
72 wxASSERT( holder );
73
74 m_canvas = nullptr;
75 m_boardAdapter = nullptr;
76 m_camera = nullptr;
77
78 if( holder )
79 {
80 m_canvas = dynamic_cast<EDA_3D_CANVAS*>( holder->GetToolCanvas() );
81
82 EDA_3D_BOARD_HOLDER* holder3d =
83 dynamic_cast<EDA_3D_BOARD_HOLDER*>( holder );
84
85 wxASSERT( holder3d );
86
87 if( holder3d )
88 {
89 m_boardAdapter = &holder3d->GetAdapter();
90 m_camera = &holder3d->GetCurrentCamera();
91 }
92 }
93}
94
95
97{
98 ACTION_MENU* actionMenu = aEvent.Parameter<ACTION_MENU*>();
99 CONDITIONAL_MENU* conditionalMenu = dynamic_cast<CONDITIONAL_MENU*>( actionMenu );
100 SELECTION dummySel;
101
102 if( conditionalMenu )
103 conditionalMenu->Evaluate( dummySel );
104
105 if( actionMenu )
106 actionMenu->UpdateAll();
107
108 return 0;
109}
110
111
113{
114 // Main loop: keep receiving events
115 while( TOOL_EVENT* evt = Wait() )
116 {
117 if( evt->IsCancelInteractive() )
118 {
119 wxWindow* canvas = m_toolMgr->GetToolHolder()->GetToolCanvas();
120 wxWindow* topLevelParent = canvas->GetParent();
121
122 while( topLevelParent && !topLevelParent->IsTopLevel() )
123 topLevelParent = topLevelParent->GetParent();
124
125 if( topLevelParent && dynamic_cast<DIALOG_SHIM*>( topLevelParent ) )
126 {
127 DIALOG_SHIM* dialog = static_cast<DIALOG_SHIM*>( topLevelParent );
128
129 if( dialog->IsQuasiModal() )
130 dialog->EndQuasiModal( wxID_CANCEL );
131 else
132 dialog->EndModal( wxID_CANCEL );
133 }
134 else
135 {
136 evt->SetPassEvent();
137 }
138 }
139 else if( evt->IsClick( BUT_RIGHT ) )
140 {
142 }
143 else
144 {
145 evt->SetPassEvent();
146 }
147 }
148
149 return 0;
150}
151
152
154{
155 m_canvas->SetView3D( aEvent.Parameter<intptr_t>() );
156
157 return 0;
158}
159
160
162{
163 switch( aEvent.Parameter<intptr_t>() )
164 {
165 case ACTIONS::CURSOR_UP: m_canvas->SetView3D( WXK_UP ); break;
166 case ACTIONS::CURSOR_DOWN: m_canvas->SetView3D( WXK_DOWN ); break;
167 case ACTIONS::CURSOR_LEFT: m_canvas->SetView3D( WXK_LEFT ); break;
168 case ACTIONS::CURSOR_RIGHT: m_canvas->SetView3D( WXK_RIGHT ); break;
169 default: wxFAIL; break;
170 }
171
172 return 0;
173}
174
175
177{
178 double rotIncrement = glm::radians( m_rotationIncrement );
179
180 switch( aEvent.Parameter<ROTATION_DIR>() )
181 {
182 case ROTATION_DIR::X_CW: m_camera->RotateX( -rotIncrement ); break;
183 case ROTATION_DIR::X_CCW: m_camera->RotateX( rotIncrement ); break;
185 case ROTATION_DIR::Y_CW: m_camera->RotateY( rotIncrement ); break;
186 case ROTATION_DIR::Y_CCW: m_camera->RotateY( -rotIncrement ); break;
187 case ROTATION_DIR::Z_CW: m_camera->RotateZ( -rotIncrement ); break;
188 case ROTATION_DIR::Z_CCW: m_camera->RotateZ( rotIncrement ); break;
189 default: wxFAIL; break;
190 }
191
192 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
194 else
196
197 return 0;
198}
199
200
202{
204
205 if( auto* viewer = dynamic_cast<EDA_3D_VIEWER_FRAME*>( m_toolMgr->GetToolHolder() ) )
206 viewer->NewDisplay( true );
207 else
209
210 return 0;
211}
212
213
215{
217
218 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
220 else
222
223 return 0;
224}
225
227{
228 bool reload = false;
229
230#define FLIP( x ) x = !x
231
232 if( aEvent.IsAction( &EDA_3D_ACTIONS::showTHT ) )
233 {
235 reload = true;
236 }
237 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showSMD ) )
238 {
240 reload = true;
241 }
242 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showVirtual ) )
243 {
245 reload = true;
246 }
247 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showNotInPosFile ) )
248 {
250 reload = true;
251 }
252 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showDNP ) )
253 {
255 reload = true;
256 }
257 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showBBoxes ) )
258 {
260 }
262 {
264 }
265 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleBoardBody ) )
266 {
268 }
269 else if( aEvent.IsAction( &EDA_3D_ACTIONS::showAxis ) )
270 {
272 }
273 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleZones ) )
274 {
276 }
277 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleAdhesive ) )
278 {
280 }
281 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleSilk ) )
282 {
284 }
285 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleSolderMask ) )
286 {
288 }
289 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleSolderPaste ) )
290 {
292 }
293 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleComments ) )
294 {
296 }
297 else if( aEvent.IsAction( &EDA_3D_ACTIONS::toggleECO ) )
298 {
300 }
301
302 if( reload )
303 {
304 if( m_boardAdapter->m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL )
305 {
306 auto* renderer = static_cast<RENDER_3D_OPENGL*>( m_canvas->GetCurrentRender() );
307 renderer->Load3dModelsIfNeeded();
309 }
310 else
311 {
313 }
314 }
315 else
316 {
317 if( auto viewer = dynamic_cast<EDA_3D_VIEWER_FRAME*>( m_toolMgr->GetToolHolder() ) )
318 viewer->NewDisplay( true );
319 else
321 }
322
323 return 0;
324}
325
326
328{
330
331 if( m_canvas )
333
334 return 0;
335}
336
337
339{
341 return 0;
342}
343
344
346{
347 bool direction = aEvent.IsAction( &ACTIONS::zoomIn );
348 return doZoomInOut( direction, true );
349}
350
351
353{
354 bool direction = aEvent.IsAction( &ACTIONS::zoomInCenter );
355 return doZoomInOut( direction, false );
356}
357
358
359int EDA_3D_CONTROLLER::doZoomInOut( bool aDirection, bool aCenterOnCursor )
360{
361 if( m_canvas )
362 {
363 m_canvas->SetView3D( aDirection ? WXK_F1 : WXK_F2 );
365 }
366
367 return 0;
368}
369
370
372{
373 if( m_canvas )
374 {
375 m_canvas->SetView3D( WXK_HOME );
377 }
378
379 return 0;
380}
381
382
384{
387
388 // Pan control
397
398 // View rotation
415
416 // Zoom control
421 // zoom in/out at cursor does not exist in 3D viewer but because F1 and F2 keys generate
422 // a zoomIn/zoomOut event, these events must be captured to use these hot keys. The actual
423 // zoom is the same as ZoomInOutCenter
426
427 // Grid
433
434 // Material
438
439 // Visibility
458}
459
460
ROTATION_DIR
Rotation direction for the 3d canvas.
Definition: 3d_enums.h:35
GRID3D_TYPE
Grid types.
Definition: 3d_enums.h:54
MATERIAL_MODE
Render 3d model shape materials mode.
Definition: 3d_enums.h:71
static TOOL_ACTION zoomRedraw
Definition: actions.h:93
static TOOL_ACTION zoomOutCenter
Definition: actions.h:97
static TOOL_ACTION zoomIn
Definition: actions.h:94
static TOOL_ACTION zoomOut
Definition: actions.h:95
static TOOL_ACTION panDown
Definition: actions.h:132
@ CURSOR_RIGHT
Definition: actions.h:191
@ CURSOR_LEFT
Definition: actions.h:191
@ CURSOR_UP
Definition: actions.h:191
@ CURSOR_DOWN
Definition: actions.h:191
static TOOL_ACTION panLeft
Definition: actions.h:133
static TOOL_ACTION updateMenu
Definition: actions.h:172
static TOOL_ACTION zoomFitScreen
Definition: actions.h:99
static TOOL_ACTION panUp
Definition: actions.h:131
static TOOL_ACTION zoomInCenter
Definition: actions.h:96
static TOOL_ACTION panRight
Definition: actions.h:134
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:49
void UpdateAll()
Run update handlers for the menu and its submenus.
EDA_3D_VIEWER_SETTINGS * m_Cfg
void RotateY(float aAngleInRadians)
Definition: camera.cpp:598
void RotateX(float aAngleInRadians)
Definition: camera.cpp:591
void RotateZ(float aAngleInRadians)
Definition: camera.cpp:605
void ToggleProjection()
Definition: camera.cpp:500
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 AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:83
bool IsQuasiModal() const
Definition: dialog_shim.h:106
void EndQuasiModal(int retCode)
static TOOL_ACTION resetView
static TOOL_ACTION toggleSolderMask
static TOOL_ACTION toggleBoardBody
static TOOL_ACTION rotateXCCW
static TOOL_ACTION showNotInPosFile
static TOOL_ACTION showTHT
static TOOL_ACTION noGrid
static TOOL_ACTION toggleComments
static TOOL_ACTION show2_5mmGrid
static TOOL_ACTION rotateZCCW
static TOOL_ACTION rotateZCW
static TOOL_ACTION toggleSilk
static TOOL_ACTION rotateYCCW
static TOOL_ACTION rotateXCW
static TOOL_ACTION viewTop
static TOOL_ACTION show1mmGrid
static TOOL_ACTION showDNP
static TOOL_ACTION toggleZones
static TOOL_ACTION toggleOrtho
static TOOL_ACTION moveLeft
static TOOL_ACTION viewLeft
static TOOL_ACTION show10mmGrid
static TOOL_ACTION viewBack
static TOOL_ACTION show5mmGrid
static TOOL_ACTION viewRight
static TOOL_ACTION showSMD
static TOOL_ACTION homeView
static TOOL_ACTION moveUp
static TOOL_ACTION flipView
static TOOL_ACTION moveDown
static TOOL_ACTION viewBottom
static TOOL_ACTION moveRight
static TOOL_ACTION materialDiffuse
static TOOL_ACTION toggleAdhesive
static TOOL_ACTION pivotCenter
static TOOL_ACTION controlActivate
static TOOL_ACTION showVirtual
static TOOL_ACTION rotateYCW
static TOOL_ACTION materialCAD
static TOOL_ACTION viewFront
static TOOL_ACTION toggleRealisticMode
static TOOL_ACTION showBBoxes
static TOOL_ACTION toggleECO
static TOOL_ACTION materialNormal
static TOOL_ACTION showAxis
static TOOL_ACTION toggleSolderPaste
virtual BOARD_ADAPTER & GetAdapter()=0
virtual CAMERA & GetCurrentCamera()=0
Implement a canvas based on a wxGLCanvas.
Definition: eda_3d_canvas.h:49
RENDER_3D_BASE * GetCurrentRender() const
bool SetView3D(int aKeycode)
Helper function to call view commands.
void DisplayStatus()
Update the status bar with the position information.
void RenderRaytracingRequest()
Request to render the current view in Raytracing mode.
void Request_refresh(bool aRedrawImmediately=true)
Schedule a refresh update of the canvas.
int On3DGridSelection(const TOOL_EVENT &aEvent)
double m_rotationIncrement
Rotation increment for the rotate actions (degrees)
int ZoomFitScreen(const TOOL_EVENT &aEvent)
EDA_3D_CANVAS * m_canvas
int doZoomInOut(bool aDirection, bool aCenterOnCursor)
int Main(const TOOL_EVENT &aEvent)
BOARD_ADAPTER * m_boardAdapter
int ZoomRedraw(const TOOL_EVENT &aEvent)
int ToggleOrtho(const TOOL_EVENT &aEvent)
int PanControl(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
int ToggleVisibility(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
void setTransitions() override
< Sets up handlers for various events.
int RotateView(const TOOL_EVENT &aEvent)
int SetMaterial(const TOOL_EVENT &aEvent)
int UpdateMenu(const TOOL_EVENT &aEvent)
int ViewControl(const TOOL_EVENT &aEvent)
int ZoomInOutCenter(const TOOL_EVENT &aEvent)
int ZoomInOut(const TOOL_EVENT &aEvent)
Create and handle a window for the 3d viewer connected to a Kiway and a pcbboard.
Object to render the board using openGL.
void Load3dModelsIfNeeded()
Load footprint models if they are not already loaded, i.e.
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
virtual wxWindow * GetToolCanvas() const =0
Canvas access.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:216
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:156
T Parameter() const
Return a non-standard parameter assigned to the event.
Definition: tool_event.h:442
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:81
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).
TOOL_MENU m_menu
The functions below are not yet implemented - their interface may change.
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.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:296
CONDITIONAL_MENU & GetMenu()
Definition: tool_menu.cpp:44
void ShowContextMenu(SELECTION &aSelection)
Helper function to set and immediately show a CONDITIONAL_MENU in concert with the given SELECTION.
Definition: tool_menu.cpp:57
#define FLIP(x)
Declaration of the eda_3d_viewer class.
@ BUT_RIGHT
Definition: tool_event.h:128