Fix #34040: Moving Normal Node with enabled Cycles Material Preview crashes
[blender.git] / source / blender / blenkernel / intern / speaker.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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Jörg Müller.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/speaker.c
29  *  \ingroup bke
30  */
31
32 #include "DNA_object_types.h"
33 #include "DNA_sound_types.h"
34 #include "DNA_speaker_types.h"
35
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_animsys.h"
40 #include "BKE_global.h"
41 #include "BKE_library.h"
42 #include "BKE_main.h"
43 #include "BKE_speaker.h"
44
45 void *BKE_speaker_add(Main *bmain, const char *name)
46 {
47         Speaker *spk;
48
49         spk =  BKE_libblock_alloc(&bmain->speaker, ID_SPK, name);
50
51         spk->attenuation = 1.0f;
52         spk->cone_angle_inner = 360.0f;
53         spk->cone_angle_outer = 360.0f;
54         spk->cone_volume_outer = 1.0f;
55         spk->distance_max = FLT_MAX;
56         spk->distance_reference = 1.0f;
57         spk->flag = 0;
58         spk->pitch = 1.0f;
59         spk->sound = NULL;
60         spk->volume = 1.0f;
61         spk->volume_max = 1.0f;
62         spk->volume_min = 0.0f;
63
64         return spk;
65 }
66
67 Speaker *BKE_speaker_copy(Speaker *spk)
68 {
69         Speaker *spkn;
70
71         spkn = BKE_libblock_copy(&spk->id);
72         if (spkn->sound)
73                 spkn->sound->id.us++;
74
75         return spkn;
76 }
77
78 void BKE_speaker_make_local(Speaker *spk)
79 {
80         Main *bmain = G.main;
81         Object *ob;
82         int is_local = FALSE, is_lib = FALSE;
83
84         /* - only lib users: do nothing
85          * - only local users: set flag
86          * - mixed: make copy
87          */
88
89         if (spk->id.lib == NULL) return;
90         if (spk->id.us == 1) {
91                 id_clear_lib_data(bmain, &spk->id);
92                 return;
93         }
94
95         ob = bmain->object.first;
96         while (ob) {
97                 if (ob->data == spk) {
98                         if (ob->id.lib) is_lib = TRUE;
99                         else is_local = TRUE;
100                 }
101                 ob = ob->id.next;
102         }
103
104         if (is_local && is_lib == FALSE) {
105                 id_clear_lib_data(bmain, &spk->id);
106         }
107         else if (is_local && is_lib) {
108                 Speaker *spk_new = BKE_speaker_copy(spk);
109                 spk_new->id.us = 0;
110
111                 /* Remap paths of new ID using old library as base. */
112                 BKE_id_lib_local_paths(bmain, spk->id.lib, &spk_new->id);
113
114                 ob = bmain->object.first;
115                 while (ob) {
116                         if (ob->data == spk) {
117
118                                 if (ob->id.lib == NULL) {
119                                         ob->data = spk_new;
120                                         spk_new->id.us++;
121                                         spk->id.us--;
122                                 }
123                         }
124                         ob = ob->id.next;
125                 }
126         }
127 }
128
129 void BKE_speaker_free(Speaker *spk)
130 {
131         if (spk->sound)
132                 spk->sound->id.us--;
133
134         BKE_free_animdata((ID *)spk);
135 }