Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[blender.git] / source / blender / editors / space_view3d / view3d_view.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_view.c
28  *  \ingroup spview3d
29  */
30
31 #include "DNA_camera_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_math.h"
38 #include "BLI_rect.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_action.h"
42 #include "BKE_camera.h"
43 #include "BKE_context.h"
44 #include "BKE_object.h"
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 #include "BKE_report.h"
48 #include "BKE_scene.h"
49
50 #include "DEG_depsgraph.h"
51
52 #include "UI_resources.h"
53
54 #include "GPU_glew.h"
55 #include "GPU_select.h"
56 #include "GPU_matrix.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "ED_screen.h"
62
63 #include "DRW_engine.h"
64
65 #include "DEG_depsgraph_query.h"
66
67 #ifdef WITH_GAMEENGINE
68 #  include "BLI_listbase.h"
69 #  include "BLI_callbacks.h"
70
71 #  include "GPU_draw.h"
72
73 #  include "BL_System.h"
74 #endif
75
76 #include "view3d_intern.h"  /* own include */
77
78 /* -------------------------------------------------------------------- */
79 /** \name Smooth View Operator & Utilities
80  *
81  * Use for view transitions to have smooth (animated) transitions.
82  * \{ */
83
84 /* This operator is one of the 'timer refresh' ones like animation playback */
85
86 struct SmoothView3DState {
87         float dist;
88         float lens;
89         float quat[4];
90         float ofs[3];
91 };
92
93 struct SmoothView3DStore {
94         /* source*/
95         struct SmoothView3DState src;  /* source */
96         struct SmoothView3DState dst;  /* destination */
97         struct SmoothView3DState org;  /* original */
98
99         bool to_camera;
100
101         bool use_dyn_ofs;
102         float dyn_ofs[3];
103
104         /* When smooth-view is enabled, store the 'rv3d->view' here,
105          * assign back when the view motion is completed. */
106         char org_view;
107
108         double time_allowed;
109 };
110
111 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
112                                             const View3D *v3d, const RegionView3D *rv3d)
113 {
114         copy_v3_v3(sms_state->ofs,   rv3d->ofs);
115         copy_qt_qt(sms_state->quat,  rv3d->viewquat);
116         sms_state->dist            = rv3d->dist;
117         sms_state->lens            = v3d->lens;
118 }
119
120 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
121                                              View3D *v3d, RegionView3D *rv3d)
122 {
123         copy_v3_v3(rv3d->ofs,      sms_state->ofs);
124         copy_qt_qt(rv3d->viewquat, sms_state->quat);
125         rv3d->dist               = sms_state->dist;
126         v3d->lens                = sms_state->lens;
127 }
128
129 /* will start timer if appropriate */
130 /* the arguments are the desired situation */
131 void ED_view3d_smooth_view_ex(
132         /* avoid passing in the context */
133         wmWindowManager *wm, wmWindow *win, ScrArea *sa,
134         View3D *v3d, ARegion *ar, const int smooth_viewtx,
135         const V3D_SmoothParams *sview)
136 {
137         RegionView3D *rv3d = ar->regiondata;
138         struct SmoothView3DStore sms = {{0}};
139         bool ok = false;
140         
141         /* initialize sms */
142         view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
143         view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
144         /* if smoothview runs multiple times... */
145         if (rv3d->sms == NULL) {
146                 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
147         }
148         else {
149                 sms.org = rv3d->sms->org;
150         }
151         sms.org_view = rv3d->view;
152
153         /* sms.to_camera = false; */  /* initizlized to zero anyway */
154
155         /* note on camera locking, this is a little confusing but works ok.
156          * we may be changing the view 'as if' there is no active camera, but in fact
157          * there is an active camera which is locked to the view.
158          *
159          * In the case where smooth view is moving _to_ a camera we don't want that
160          * camera to be moved or changed, so only when the camera is not being set should
161          * we allow camera option locking to initialize the view settings from the camera.
162          */
163         if (sview->camera == NULL && sview->camera_old == NULL) {
164                 ED_view3d_camera_lock_init(v3d, rv3d);
165         }
166
167         /* store the options we want to end with */
168         if (sview->ofs)
169                 copy_v3_v3(sms.dst.ofs, sview->ofs);
170         if (sview->quat)
171                 copy_qt_qt(sms.dst.quat, sview->quat);
172         if (sview->dist)
173                 sms.dst.dist = *sview->dist;
174         if (sview->lens)
175                 sms.dst.lens = *sview->lens;
176
177         if (sview->dyn_ofs) {
178                 BLI_assert(sview->ofs  == NULL);
179                 BLI_assert(sview->quat != NULL);
180
181                 copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
182                 sms.use_dyn_ofs = true;
183
184                 /* calculate the final destination offset */
185                 view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
186         }
187
188         if (sview->camera) {
189                 sms.dst.dist = ED_view3d_offset_distance(sview->camera->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
190                 ED_view3d_from_object(sview->camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
191                 sms.to_camera = true; /* restore view3d values in end */
192         }
193         
194         /* skip smooth viewing for render engine draw */
195         if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
196                 bool changed = false; /* zero means no difference */
197                 
198                 if (sview->camera_old != sview->camera)
199                         changed = true;
200                 else if (sms.dst.dist != rv3d->dist)
201                         changed = true;
202                 else if (sms.dst.lens != v3d->lens)
203                         changed = true;
204                 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
205                         changed = true;
206                 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
207                         changed = true;
208                 
209                 /* The new view is different from the old one
210                  * so animate the view */
211                 if (changed) {
212                         /* original values */
213                         if (sview->camera_old) {
214                                 sms.src.dist = ED_view3d_offset_distance(sview->camera_old->obmat, rv3d->ofs, 0.0f);
215                                 /* this */
216                                 ED_view3d_from_object(sview->camera_old, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
217                         }
218                         /* grid draw as floor */
219                         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
220                                 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
221                                 rv3d->view = RV3D_VIEW_USER;
222                         }
223
224                         sms.time_allowed = (double)smooth_viewtx / 1000.0;
225                         
226                         /* if this is view rotation only
227                          * we can decrease the time allowed by
228                          * the angle between quats 
229                          * this means small rotations wont lag */
230                         if (sview->quat && !sview->ofs && !sview->dist) {
231                                 /* scale the time allowed by the rotation */
232                                 sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) / M_PI; /* 180deg == 1.0 */
233                         }
234
235                         /* ensure it shows correct */
236                         if (sms.to_camera) {
237                                 /* use ortho if we move from an ortho view to an ortho camera */
238                                 rv3d->persp = (((rv3d->is_persp == false) &&
239                                                 (sview->camera->type == OB_CAMERA) &&
240                                                 (((Camera *)sview->camera->data)->type == CAM_ORTHO)) ?
241                                                 RV3D_ORTHO : RV3D_PERSP);
242                         }
243
244                         rv3d->rflag |= RV3D_NAVIGATING;
245                         
246                         /* not essential but in some cases the caller will tag the area for redraw,
247                          * and in that case we can get a flicker of the 'org' user view but we want to see 'src' */
248                         view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
249
250                         /* keep track of running timer! */
251                         if (rv3d->sms == NULL) {
252                                 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
253                         }
254                         *rv3d->sms = sms;
255                         if (rv3d->smooth_timer) {
256                                 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
257                         }
258                         /* TIMER1 is hardcoded in keymap */
259                         rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
260
261                         ok = true;
262                 }
263         }
264         
265         /* if we get here nothing happens */
266         if (ok == false) {
267                 if (sms.to_camera == false) {
268                         copy_v3_v3(rv3d->ofs, sms.dst.ofs);
269                         copy_qt_qt(rv3d->viewquat, sms.dst.quat);
270                         rv3d->dist = sms.dst.dist;
271                         v3d->lens = sms.dst.lens;
272
273                         ED_view3d_camera_lock_sync(v3d, rv3d);
274                 }
275
276                 if (rv3d->viewlock & RV3D_BOXVIEW) {
277                         view3d_boxview_copy(sa, ar);
278                 }
279
280                 ED_region_tag_redraw(ar);
281         }
282 }
283
284 void ED_view3d_smooth_view(
285         bContext *C,
286         View3D *v3d, ARegion *ar, const int smooth_viewtx,
287         const struct V3D_SmoothParams *sview)
288 {
289         wmWindowManager *wm = CTX_wm_manager(C);
290         wmWindow *win = CTX_wm_window(C);
291         ScrArea *sa = CTX_wm_area(C);
292
293         ED_view3d_smooth_view_ex(
294                 wm, win, sa,
295                 v3d, ar, smooth_viewtx,
296                 sview);
297 }
298
299 /* only meant for timer usage */
300 static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *ar, bool sync_boxview)
301 {
302         RegionView3D *rv3d = ar->regiondata;
303         struct SmoothView3DStore *sms = rv3d->sms;
304         float step, step_inv;
305         
306         if (sms->time_allowed != 0.0)
307                 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
308         else
309                 step = 1.0f;
310         
311         /* end timer */
312         if (step >= 1.0f) {
313                 
314                 /* if we went to camera, store the original */
315                 if (sms->to_camera) {
316                         rv3d->persp = RV3D_CAMOB;
317                         view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
318                 }
319                 else {
320                         view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
321
322                         ED_view3d_camera_lock_sync(v3d, rv3d);
323                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
324                 }
325                 
326                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
327                         rv3d->view = sms->org_view;
328                 }
329
330                 MEM_freeN(rv3d->sms);
331                 rv3d->sms = NULL;
332                 
333                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
334                 rv3d->smooth_timer = NULL;
335                 rv3d->rflag &= ~RV3D_NAVIGATING;
336         }
337         else {
338                 /* ease in/out */
339                 step = (3.0f * step * step - 2.0f * step * step * step);
340
341                 step_inv = 1.0f - step;
342
343                 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
344
345                 if (sms->use_dyn_ofs) {
346                         view3d_orbit_apply_dyn_ofs(rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
347                 }
348                 else {
349                         interp_v3_v3v3(rv3d->ofs, sms->src.ofs,  sms->dst.ofs,  step);
350                 }
351                 
352                 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
353                 v3d->lens  = sms->dst.lens * step + sms->src.lens * step_inv;
354
355                 ED_view3d_camera_lock_sync(v3d, rv3d);
356                 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
357                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
358                 }
359
360                 /* Event handling won't know if a UI item has been moved under the pointer. */
361                 WM_event_add_mousemove(C);
362         }
363         
364         if (sync_boxview && (rv3d->viewlock & RV3D_BOXVIEW)) {
365                 view3d_boxview_copy(CTX_wm_area(C), ar);
366         }
367
368         /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
369          * when switching camera in quad-view the other ortho views would zoom & reset.
370          *
371          * For now only redraw all regions when smoothview finishes.
372          */
373         if (step >= 1.0f) {
374                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
375         }
376         else {
377                 ED_region_tag_redraw(ar);
378         }
379 }
380
381 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
382 {
383         View3D *v3d = CTX_wm_view3d(C);
384         ARegion *ar = CTX_wm_region(C);
385         RegionView3D *rv3d = ar->regiondata;
386
387         /* escape if not our timer */
388         if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
389                 return OPERATOR_PASS_THROUGH;
390         }
391
392         view3d_smoothview_apply(C, v3d, ar, true);
393
394         return OPERATOR_FINISHED;
395 }
396
397 /**
398  * Apply the smoothview immediately, use when we need to start a new view operation.
399  * (so we don't end up half-applying a view operation when pressing keys quickly).
400  */
401 void ED_view3d_smooth_view_force_finish(
402         bContext *C,
403         View3D *v3d, ARegion *ar)
404 {
405         RegionView3D *rv3d = ar->regiondata;
406
407         if (rv3d && rv3d->sms) {
408                 rv3d->sms->time_allowed = 0.0;  /* force finishing */
409                 view3d_smoothview_apply(C, v3d, ar, false);
410
411                 /* force update of view matrix so tools that run immediately after
412                  * can use them without redrawing first */
413                 Depsgraph *depsgraph = CTX_data_depsgraph(C);
414                 Scene *scene = CTX_data_scene(C);
415                 ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, NULL, NULL, NULL);
416         }
417 }
418
419 void VIEW3D_OT_smoothview(wmOperatorType *ot)
420 {
421         /* identifiers */
422         ot->name = "Smooth View";
423         ot->description = "";
424         ot->idname = "VIEW3D_OT_smoothview";
425
426         /* api callbacks */
427         ot->invoke = view3d_smoothview_invoke;
428
429         /* flags */
430         ot->flag = OPTYPE_INTERNAL;
431
432         ot->poll = ED_operator_view3d_active;
433 }
434
435 /** \} */
436
437 /* -------------------------------------------------------------------- */
438 /** \name Camera to View Operator
439  * \{ */
440
441 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
442 {
443         View3D *v3d;
444         ARegion *ar;
445         RegionView3D *rv3d;
446
447         ObjectTfmProtectedChannels obtfm;
448
449         ED_view3d_context_user_region(C, &v3d, &ar);
450         rv3d = ar->regiondata;
451
452         ED_view3d_lastview_store(rv3d);
453
454         BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
455
456         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
457
458         BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
459
460         DEG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
461         rv3d->persp = RV3D_CAMOB;
462         
463         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
464         
465         return OPERATOR_FINISHED;
466
467 }
468
469 static int view3d_camera_to_view_poll(bContext *C)
470 {
471         View3D *v3d;
472         ARegion *ar;
473
474         if (ED_view3d_context_user_region(C, &v3d, &ar)) {
475                 RegionView3D *rv3d = ar->regiondata;
476                 if (v3d && v3d->camera && !ID_IS_LINKED(v3d->camera)) {
477                         if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) {
478                                 if (rv3d->persp != RV3D_CAMOB) {
479                                         return 1;
480                                 }
481                         }
482                 }
483         }
484
485         return 0;
486 }
487
488 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
489 {
490         /* identifiers */
491         ot->name = "Align Camera To View";
492         ot->description = "Set camera view to active view";
493         ot->idname = "VIEW3D_OT_camera_to_view";
494         
495         /* api callbacks */
496         ot->exec = view3d_camera_to_view_exec;
497         ot->poll = view3d_camera_to_view_poll;
498         
499         /* flags */
500         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
501 }
502
503 /** \} */
504
505 /* -------------------------------------------------------------------- */
506 /** \name Camera Fit Frame to Selected Operator
507  * \{ */
508
509 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
510  * meant to take into account vertex/bone selection for eg. */
511 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
512 {
513         Depsgraph *depsgraph = CTX_data_depsgraph(C);
514         Scene *scene = CTX_data_scene(C);
515         ViewLayer *view_layer = CTX_data_view_layer(C);
516         View3D *v3d = CTX_wm_view3d(C);  /* can be NULL */
517         Object *camera_ob = v3d ? v3d->camera : scene->camera;
518
519         float r_co[3]; /* the new location to apply */
520         float r_scale; /* only for ortho cameras */
521
522         if (camera_ob == NULL) {
523                 BKE_report(op->reports, RPT_ERROR, "No active camera");
524                 return OPERATOR_CANCELLED;
525         }
526
527         /* this function does all the important stuff */
528         if (BKE_camera_view_frame_fit_to_scene(depsgraph, scene, view_layer, camera_ob, r_co, &r_scale)) {
529                 ObjectTfmProtectedChannels obtfm;
530                 float obmat_new[4][4];
531
532                 if ((camera_ob->type == OB_CAMERA) && (((Camera *)camera_ob->data)->type == CAM_ORTHO)) {
533                         ((Camera *)camera_ob->data)->ortho_scale = r_scale;
534                 }
535
536                 copy_m4_m4(obmat_new, camera_ob->obmat);
537                 copy_v3_v3(obmat_new[3], r_co);
538
539                 /* only touch location */
540                 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
541                 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
542                 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
543
544                 /* notifiers */
545                 DEG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
546                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
547                 return OPERATOR_FINISHED;
548         }
549         else {
550                 return OPERATOR_CANCELLED;
551         }
552 }
553
554 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
555 {
556         /* identifiers */
557         ot->name = "Camera Fit Frame to Selected";
558         ot->description = "Move the camera so selected objects are framed";
559         ot->idname = "VIEW3D_OT_camera_to_view_selected";
560
561         /* api callbacks */
562         ot->exec = view3d_camera_to_view_selected_exec;
563         ot->poll = ED_operator_scene_editable;
564
565         /* flags */
566         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
567 }
568
569 /** \} */
570
571 /* -------------------------------------------------------------------- */
572 /** \name Object as Camera Operator
573  * \{ */
574
575 static void sync_viewport_camera_smoothview(bContext *C, View3D *v3d, Object *ob, const int smooth_viewtx)
576 {
577         Main *bmain = CTX_data_main(C);
578         for (bScreen *screen = bmain->screen.first; screen != NULL; screen = screen->id.next) {
579                 for (ScrArea *area = screen->areabase.first; area != NULL; area = area->next) {
580                         for (SpaceLink *space_link = area->spacedata.first; space_link != NULL; space_link = space_link->next) {
581                                 if (space_link->spacetype == SPACE_VIEW3D) {
582                                         View3D *other_v3d = (View3D *)space_link;
583                                         if (other_v3d == v3d) {
584                                                 continue;
585                                         }
586                                         if (other_v3d->camera == ob) {
587                                                 continue;
588                                         }
589                                         if (v3d->scenelock) {
590                                                 ListBase *lb = (space_link == area->spacedata.first)
591                                                                    ? &area->regionbase
592                                                                    : &space_link->regionbase;
593                                                 for (ARegion *other_ar = lb->first; other_ar != NULL; other_ar = other_ar->next) {
594                                                         if (other_ar->regiontype == RGN_TYPE_WINDOW) {
595                                                                 if (other_ar->regiondata) {
596                                                                         RegionView3D *other_rv3d = other_ar->regiondata;
597                                                                         if (other_rv3d->persp == RV3D_CAMOB) {
598                                                                                 Object *other_camera_old = other_v3d->camera;
599                                                                                 other_v3d->camera = ob;
600                                                                                 ED_view3d_lastview_store(other_rv3d);
601                                                                                 ED_view3d_smooth_view(
602                                                                                         C, other_v3d, other_ar, smooth_viewtx,
603                                                                                         &(const V3D_SmoothParams) {
604                                                                                             .camera_old = other_camera_old,
605                                                                                             .camera = other_v3d->camera,
606                                                                                             .ofs = other_rv3d->ofs,
607                                                                                             .quat = other_rv3d->viewquat,
608                                                                                             .dist = &other_rv3d->dist,
609                                                                                             .lens = &other_v3d->lens});
610                                                                         }
611                                                                         else {
612                                                                                 other_v3d->camera = ob;
613                                                                         }
614                                                                 }
615                                                         }
616                                                 }
617                                         }
618                                 }
619                         }
620                 }
621         }
622 }
623
624 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
625 {
626         View3D *v3d;
627         ARegion *ar;
628         RegionView3D *rv3d;
629
630         Scene *scene = CTX_data_scene(C);
631         Object *ob = CTX_data_active_object(C);
632
633         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
634
635         /* no NULL check is needed, poll checks */
636         ED_view3d_context_user_region(C, &v3d, &ar);
637         rv3d = ar->regiondata;
638
639         if (ob) {
640                 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
641                 rv3d->persp = RV3D_CAMOB;
642                 v3d->camera = ob;
643                 if (v3d->scenelock)
644                         scene->camera = ob;
645
646                 /* unlikely but looks like a glitch when set to the same */
647                 if (camera_old != ob) {
648                         ED_view3d_lastview_store(rv3d);
649
650                         ED_view3d_smooth_view(
651                                 C, v3d, ar, smooth_viewtx,
652                                 &(const V3D_SmoothParams) {
653                                     .camera_old = camera_old, .camera = v3d->camera,
654                                     .ofs = rv3d->ofs, .quat = rv3d->viewquat,
655                                     .dist = &rv3d->dist, .lens = &v3d->lens});
656                 }
657
658                 if (v3d->scenelock) {
659                         sync_viewport_camera_smoothview(C, v3d, ob, smooth_viewtx);
660                         WM_event_add_notifier(C, NC_SCENE, scene);
661                 }
662                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
663         }
664         
665         return OPERATOR_FINISHED;
666 }
667
668 int ED_operator_rv3d_user_region_poll(bContext *C)
669 {
670         View3D *v3d_dummy;
671         ARegion *ar_dummy;
672
673         return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
674 }
675
676 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
677 {
678         /* identifiers */
679         ot->name = "Set Active Object as Camera";
680         ot->description = "Set the active object as the active camera for this view or scene";
681         ot->idname = "VIEW3D_OT_object_as_camera";
682         
683         /* api callbacks */
684         ot->exec = view3d_setobjectascamera_exec;
685         ot->poll = ED_operator_rv3d_user_region_poll;
686         
687         /* flags */
688         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
689 }
690
691 /** \} */
692
693 /* -------------------------------------------------------------------- */
694 /** \name Window and View Matrix Calculation
695  * \{ */
696
697 /**
698  * \param rect optional for picking (can be NULL).
699  */
700 void view3d_winmatrix_set(Depsgraph *depsgraph, ARegion *ar, const View3D *v3d, const rcti *rect)
701 {
702         RegionView3D *rv3d = ar->regiondata;
703         rctf viewplane;
704         float clipsta, clipend;
705         bool is_ortho;
706         
707         is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
708         rv3d->is_persp = !is_ortho;
709
710 #if 0
711         printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
712                viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
713                clipsta, clipend);
714 #endif
715
716         if (rect) {  /* picking */
717                 rctf r;
718                 r.xmin = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmin / (float)ar->winx));
719                 r.ymin = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymin / (float)ar->winy));
720                 r.xmax = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmax / (float)ar->winx));
721                 r.ymax = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymax / (float)ar->winy));
722                 viewplane = r;
723         }
724
725         if (is_ortho) {
726                 gpuOrtho(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
727         }
728         else {
729                 gpuFrustum(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
730         }
731
732         /* update matrix in 3d view region */
733         gpuGetProjectionMatrix(rv3d->winmat);
734 }
735
736 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
737 {
738         float bmat[4][4];
739
740         rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
741
742         normalize_m4_m4(bmat, ob->obmat);
743         invert_m4_m4(rv3d->viewmat, bmat);
744
745         /* view quat calculation, needed for add object */
746         mat4_normalized_to_quat(rv3d->viewquat, rv3d->viewmat);
747 }
748
749 /**
750  * Sets #RegionView3D.viewmat
751  *
752  * \param depsgraph: Depsgraph.
753  * \param scene: Scene for camera and cursor location.
754  * \param v3d: View 3D space data.
755  * \param rv3d: 3D region which stores the final matrices.
756  * \param rect_scale: Optional 2D scale argument,
757  * Use when displaying a sub-region, eg: when #view3d_winmatrix_set takes a 'rect' argument.
758  *
759  * \note don't set windows active in here, is used by renderwin too.
760  */
761 void view3d_viewmatrix_set(
762         Depsgraph *depsgraph, Scene *scene,
763         const View3D *v3d, RegionView3D *rv3d, const float rect_scale[2])
764 {
765         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
766                 if (v3d->camera) {
767                         Object *camera_object = DEG_get_evaluated_object(depsgraph, v3d->camera);
768                         BKE_object_where_is_calc(depsgraph, scene, camera_object);
769                         obmat_to_viewmat(rv3d, camera_object);
770                 }
771                 else {
772                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
773                         rv3d->viewmat[3][2] -= rv3d->dist;
774                 }
775         }
776         else {
777                 bool use_lock_ofs = false;
778
779
780                 /* should be moved to better initialize later on XXX */
781                 if (rv3d->viewlock & RV3D_LOCKED)
782                         ED_view3d_lock(rv3d);
783                 
784                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
785                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
786                 if (v3d->ob_centre) {
787                         Object *ob = v3d->ob_centre;
788                         float vec[3];
789                         
790                         copy_v3_v3(vec, ob->obmat[3]);
791                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
792                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
793                                 if (pchan) {
794                                         copy_v3_v3(vec, pchan->pose_mat[3]);
795                                         mul_m4_v3(ob->obmat, vec);
796                                 }
797                         }
798                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
799                         use_lock_ofs = true;
800                 }
801                 else if (v3d->ob_centre_cursor) {
802                         float vec[3];
803                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, (View3D *)v3d));
804                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
805                         use_lock_ofs = true;
806                 }
807                 else {
808                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
809                 }
810
811                 /* lock offset */
812                 if (use_lock_ofs) {
813                         float persmat[4][4], persinv[4][4];
814                         float vec[3];
815
816                         /* we could calculate the real persmat/persinv here
817                          * but it would be unreliable so better to later */
818                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
819                         invert_m4_m4(persinv, persmat);
820
821                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
822                         vec[2] = 0.0f;
823
824                         if (rect_scale) {
825                                 /* Since 'RegionView3D.winmat' has been calculated and this function doesn't take the 'ARegion'
826                                  * we don't know about the region size.
827                                  * Use 'rect_scale' when drawing a sub-region to apply 2D offset,
828                                  * scaled by the difference between the sub-region and the region size.
829                                  */
830                                 vec[0] /= rect_scale[0];
831                                 vec[1] /= rect_scale[1];
832                         }
833
834                         mul_mat3_m4_v3(persinv, vec);
835                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
836                 }
837                 /* end lock offset */
838         }
839 }
840
841 /** \} */
842
843 /* -------------------------------------------------------------------- */
844 /** \name OpenGL Select Utilities
845  * \{ */
846
847 /**
848  * Optionally cache data for multiple calls to #view3d_opengl_select
849  *
850  * just avoid GPU_select headers outside this file
851  */
852 void view3d_opengl_select_cache_begin(void)
853 {
854         GPU_select_cache_begin();
855 }
856
857 void view3d_opengl_select_cache_end(void)
858 {
859         GPU_select_cache_end();
860 }
861
862 #ifndef WITH_OPENGL_LEGACY
863 struct DrawSelectLoopUserData {
864         uint  pass;
865         uint  hits;
866         uint *buffer;
867         uint  buffer_len;
868         const rcti *rect;
869         char gpu_select_mode;
870 };
871
872 static bool drw_select_loop_pass(eDRWSelectStage stage, void *user_data)
873 {
874         bool continue_pass = false;
875         struct DrawSelectLoopUserData *data = user_data;
876         if (stage == DRW_SELECT_PASS_PRE) {
877                 GPU_select_begin(data->buffer, data->buffer_len, data->rect, data->gpu_select_mode, data->hits);
878                 /* always run POST after PRE. */
879                 continue_pass = true;
880         }
881         else if (stage == DRW_SELECT_PASS_POST) {
882                 int hits = GPU_select_end();
883                 if (data->pass == 0) {
884                         /* quirk of GPU_select_end, only take hits value from first call. */
885                         data->hits = hits;
886                 }
887                 if (data->gpu_select_mode == GPU_SELECT_NEAREST_FIRST_PASS) {
888                         data->gpu_select_mode = GPU_SELECT_NEAREST_SECOND_PASS;
889                         continue_pass = (hits > 0);
890                 }
891                 data->pass += 1;
892         }
893         else {
894                 BLI_assert(0);
895         }
896         return continue_pass;
897
898 }
899 #endif /* WITH_OPENGL_LEGACY */
900
901 /**
902  * \warning be sure to account for a negative return value
903  * This is an error, "Too many objects in select buffer"
904  * and no action should be taken (can crash blender) if this happens
905  *
906  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
907  */
908 int view3d_opengl_select(
909         ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input,
910         eV3DSelectMode select_mode)
911 {
912         struct bThemeState theme_state;
913         Depsgraph *graph = vc->depsgraph;
914         Scene *scene = vc->scene;
915         View3D *v3d = vc->v3d;
916         ARegion *ar = vc->ar;
917         rcti rect;
918         int hits;
919         const bool use_obedit_skip = (OBEDIT_FROM_VIEW_LAYER(vc->view_layer) != NULL) && (vc->obedit == NULL);
920         const bool is_pick_select = (U.gpu_select_pick_deph != 0);
921         const bool do_passes = (
922                 (is_pick_select == false) &&
923                 (select_mode == VIEW3D_SELECT_PICK_NEAREST) &&
924                 GPU_select_query_check_active());
925         const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST);
926
927         char gpu_select_mode;
928
929         /* case not a border select */
930         if (input->xmin == input->xmax) {
931                 /* seems to be default value for bones only now */
932                 BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12);
933         }
934         else {
935                 rect = *input;
936         }
937
938         if (is_pick_select) {
939                 if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) {
940                         gpu_select_mode = GPU_SELECT_PICK_NEAREST;
941                 }
942                 else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) {
943                         gpu_select_mode = GPU_SELECT_PICK_ALL;
944                 }
945                 else {
946                         gpu_select_mode = GPU_SELECT_ALL;
947                 }
948         }
949         else {
950                 if (do_passes) {
951                         gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS;
952                 }
953                 else {
954                         gpu_select_mode = GPU_SELECT_ALL;
955                 }
956         }
957
958         /* Tools may request depth outside of regular drawing code. */
959         UI_Theme_Store(&theme_state);
960         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
961
962         /* Re-use cache (rect must be smaller then the cached)
963          * other context is assumed to be unchanged */
964         if (GPU_select_is_cached()) {
965                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
966                 GPU_select_cache_load_id();
967                 hits = GPU_select_end();
968                 goto finally;
969         }
970
971 #ifndef WITH_OPENGL_LEGACY
972         /* All of the queries need to be perform on the drawing context. */
973         DRW_opengl_context_enable();
974 #endif
975
976         G.f |= G_PICKSEL;
977
978         /* Important we use the 'viewmat' and don't re-calculate since
979          * the object & bone view locking takes 'rect' into account, see: T51629. */
980         ED_view3d_draw_setup_view(vc->win, graph, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect);
981
982         if (v3d->drawtype > OB_WIRE) {
983                 v3d->zbuf = true;
984                 glEnable(GL_DEPTH_TEST);
985         }
986
987         if (vc->rv3d->rflag & RV3D_CLIPPING)
988                 ED_view3d_clipping_set(vc->rv3d);
989         
990
991 #ifdef WITH_OPENGL_LEGACY
992         if (IS_VIEWPORT_LEGACY(vc->v3d)) {
993                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
994                 ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
995                 hits = GPU_select_end();
996
997                 if (do_passes && (hits > 0)) {
998                         GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits);
999                         ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
1000                         GPU_select_end();
1001                 }
1002         }
1003         else
1004 #else
1005         {
1006                 /* We need to call "GPU_select_*" API's inside DRW_draw_select_loop
1007                  * because the OpenGL context created & destroyed inside this function. */
1008                 struct DrawSelectLoopUserData drw_select_loop_user_data = {
1009                         .pass = 0,
1010                         .hits = 0,
1011                         .buffer = buffer,
1012                         .buffer_len = bufsize,
1013                         .rect = &rect,
1014                         .gpu_select_mode = gpu_select_mode,
1015                 };
1016                 DRW_draw_select_loop(
1017                         graph, ar, v3d,
1018                         use_obedit_skip, use_nearest, &rect,
1019                         drw_select_loop_pass, &drw_select_loop_user_data);
1020                 hits = drw_select_loop_user_data.hits;
1021         }
1022 #endif /* WITH_OPENGL_LEGACY */
1023
1024         G.f &= ~G_PICKSEL;
1025         ED_view3d_draw_setup_view(vc->win, graph, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL);
1026         
1027         if (v3d->drawtype > OB_WIRE) {
1028                 v3d->zbuf = 0;
1029                 glDisable(GL_DEPTH_TEST);
1030         }
1031         
1032         if (vc->rv3d->rflag & RV3D_CLIPPING)
1033                 ED_view3d_clipping_disable();
1034
1035 #ifndef WITH_OPENGL_LEGACY
1036         DRW_opengl_context_disable();
1037 #endif
1038
1039 finally:
1040
1041         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1042
1043         UI_Theme_Restore(&theme_state);
1044
1045         return hits;
1046 }
1047
1048 /** \} */
1049
1050 /* -------------------------------------------------------------------- */
1051 /** \name View Layer Utilities
1052  * \{ */
1053
1054 int ED_view3d_view_layer_set(int lay, const int *values, int *active)
1055 {
1056         int i, tot = 0;
1057         
1058         /* ensure we always have some layer selected */
1059         for (i = 0; i < 20; i++)
1060                 if (values[i])
1061                         tot++;
1062         
1063         if (tot == 0)
1064                 return lay;
1065         
1066         for (i = 0; i < 20; i++) {
1067                 
1068                 if (active) {
1069                         /* if this value has just been switched on, make that layer active */
1070                         if (values[i] && (lay & (1 << i)) == 0) {
1071                                 *active = (1 << i);
1072                         }
1073                 }
1074                         
1075                 if (values[i]) lay |= (1 << i);
1076                 else lay &= ~(1 << i);
1077         }
1078         
1079         /* ensure always an active layer */
1080         if (active && (lay & *active) == 0) {
1081                 for (i = 0; i < 20; i++) {
1082                         if (lay & (1 << i)) {
1083                                 *active = 1 << i;
1084                                 break;
1085                         }
1086                 }
1087         }
1088         
1089         return lay;
1090 }
1091
1092 /** \} */
1093
1094 /* -------------------------------------------------------------------- */
1095 /** \name Game Engine Operator
1096  *
1097  * Start the game engine (handles context switching).
1098  * \{ */
1099
1100 #ifdef WITH_GAMEENGINE
1101
1102 static ListBase queue_back;
1103 static void game_engine_save_state(bContext *C, wmWindow *win)
1104 {
1105         Object *obact = CTX_data_active_object(C);
1106
1107         glPushAttrib(GL_ALL_ATTRIB_BITS);
1108
1109         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
1110                 GPU_paint_set_mipmap(1);
1111         }
1112
1113         queue_back = win->queue;
1114
1115         BLI_listbase_clear(&win->queue);
1116 }
1117
1118 static void game_engine_restore_state(bContext *C, wmWindow *win)
1119 {
1120         Object *obact = CTX_data_active_object(C);
1121
1122         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
1123                 GPU_paint_set_mipmap(0);
1124         }
1125         /* check because closing win can set to NULL */
1126         if (win) {
1127                 win->queue = queue_back;
1128         }
1129
1130         GPU_state_init();
1131
1132         glPopAttrib();
1133 }
1134
1135 /* was space_set_commmandline_options in 2.4x */
1136 static void game_set_commmandline_options(GameData *gm)
1137 {
1138         SYS_SystemHandle syshandle;
1139         int test;
1140
1141         if ((syshandle = SYS_GetSystem())) {
1142                 /* User defined settings */
1143                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1144                 GPU_set_mipmap(!test);
1145                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1146
1147                 /* File specific settings: */
1148                 /* Only test the first one. These two are switched
1149                  * simultaneously. */
1150                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1151                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1152                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1153
1154                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1155                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1156
1157                 test = (gm->flag & GAME_SHOW_PHYSICS);
1158                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1159
1160                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1161                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1162
1163                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1164                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1165
1166                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1167                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1168
1169                 test = (gm->matmode == GAME_MAT_MULTITEX);
1170                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1171                 test = (gm->matmode == GAME_MAT_GLSL);
1172                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1173         }
1174 }
1175
1176 #endif /* WITH_GAMEENGINE */
1177
1178 static int game_engine_poll(bContext *C)
1179 {
1180         const wmWindow *win = CTX_wm_window(C);
1181         const Scene *scene = WM_window_get_active_scene(win);
1182
1183         /* we need a context and area to launch BGE
1184          * it's a temporary solution to avoid crash at load time
1185          * if we try to auto run the BGE. Ideally we want the
1186          * context to be set as soon as we load the file. */
1187
1188         if (win == NULL) return 0;
1189         if (CTX_wm_screen(C) == NULL) return 0;
1190
1191         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1192                 return 0;
1193
1194         if (!BKE_scene_uses_blender_game(scene))
1195                 return 0;
1196
1197         return 1;
1198 }
1199
1200 static int game_engine_exec(bContext *C, wmOperator *op)
1201 {
1202 #ifdef WITH_GAMEENGINE
1203         Scene *startscene = CTX_data_scene(C);
1204         Main *bmain = CTX_data_main(C);
1205         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1206         ARegion *ar, *prevar = CTX_wm_region(C);
1207         wmWindow *prevwin = CTX_wm_window(C);
1208         RegionView3D *rv3d;
1209         rcti cam_frame;
1210
1211         UNUSED_VARS(op);
1212
1213         /* bad context switch .. */
1214         if (!ED_view3d_context_activate(C))
1215                 return OPERATOR_CANCELLED;
1216
1217         /* redraw to hide any menus/popups, we don't go back to
1218          * the window manager until after this operator exits */
1219         WM_redraw_windows(C);
1220
1221         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1222
1223         rv3d = CTX_wm_region_view3d(C);
1224         /* sa = CTX_wm_area(C); */ /* UNUSED */
1225         ar = CTX_wm_region(C);
1226
1227         view3d_operator_needs_opengl(C);
1228
1229         game_set_commmandline_options(&startscene->gm);
1230
1231         if ((rv3d->persp == RV3D_CAMOB) &&
1232             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1233             (startscene->gm.stereoflag != STEREO_DOME))
1234         {
1235                 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1236                 /* Letterbox */
1237                 rctf cam_framef;
1238                 ED_view3d_calc_camera_border(startscene, depsgraph, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1239                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1240                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1241                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1242                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1243                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1244         }
1245         else {
1246                 cam_frame.xmin = ar->winrct.xmin;
1247                 cam_frame.xmax = ar->winrct.xmax;
1248                 cam_frame.ymin = ar->winrct.ymin;
1249                 cam_frame.ymax = ar->winrct.ymax;
1250         }
1251
1252
1253         game_engine_save_state(C, prevwin);
1254
1255         StartKetsjiShell(C, ar, &cam_frame, 1);
1256
1257         /* window wasnt closed while the BGE was running */
1258         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1259                 prevwin = NULL;
1260                 CTX_wm_window_set(C, NULL);
1261         }
1262
1263         ED_area_tag_redraw(CTX_wm_area(C));
1264
1265         if (prevwin) {
1266                 /* restore context, in case it changed in the meantime, for
1267                  * example by working in another window or closing it */
1268                 CTX_wm_region_set(C, prevar);
1269                 CTX_wm_window_set(C, prevwin);
1270                 CTX_wm_area_set(C, prevsa);
1271         }
1272
1273         game_engine_restore_state(C, prevwin);
1274
1275         //XXX restore_all_scene_cfra(scene_cfra_store);
1276         BKE_scene_set_background(CTX_data_main(C), startscene);
1277         //XXX BKE_scene_graph_update_for_newframe(depsgraph, bmain);
1278
1279         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1280
1281         return OPERATOR_FINISHED;
1282 #else
1283         UNUSED_VARS(C);
1284         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1285         return OPERATOR_CANCELLED;
1286 #endif
1287 }
1288
1289 void VIEW3D_OT_game_start(wmOperatorType *ot)
1290 {
1291         /* identifiers */
1292         ot->name = "Start Game Engine";
1293         ot->description = "Start game engine";
1294         ot->idname = "VIEW3D_OT_game_start";
1295
1296         /* api callbacks */
1297         ot->exec = game_engine_exec;
1298
1299         ot->poll = game_engine_poll;
1300 }
1301
1302 /** \} */