rename remaining class members with m_ prefix.
[blender.git] / source / blender / compositor / intern / COM_InputSocket.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
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  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_Socket.h"
24 #include "COM_Node.h"
25 #include "COM_SocketConnection.h"
26 #include "COM_ExecutionSystem.h"
27
28 InputSocket::InputSocket(DataType datatype) : Socket(datatype)
29 {
30         this->m_connection = NULL;
31         this->m_resizeMode = COM_SC_CENTER;
32 }
33 InputSocket::InputSocket(DataType datatype, InputSocketResizeMode resizeMode) : Socket(datatype)
34 {
35         this->m_connection = NULL;
36         this->m_resizeMode = resizeMode;
37 }
38
39 InputSocket::InputSocket(InputSocket *from) : Socket(from->getDataType())
40 {
41         this->m_connection = NULL;
42         this->m_resizeMode = from->getResizeMode();
43 }
44
45 int InputSocket::isInputSocket() const { return true; }
46 const int InputSocket::isConnected() const { return this->m_connection != NULL; }
47
48 void InputSocket::setConnection(SocketConnection *connection)
49 {
50         this->m_connection = connection;
51 }
52 SocketConnection *InputSocket::getConnection()
53 {
54         return this->m_connection;
55 }
56
57 void InputSocket::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
58 {
59         if (this->isConnected()) {
60                 this->m_connection->getFromSocket()->determineResolution(resolution, preferredResolution);
61         }
62         else {
63                 return;
64         }
65 }
66
67 void InputSocket::relinkConnections(InputSocket *relinkToSocket)
68 {
69         if (!isConnected()) {
70                 return;
71         }
72         SocketConnection *connection = this->getConnection();
73         connection->setToSocket(relinkToSocket);
74         relinkToSocket->setConnection(connection);
75         this->setConnection(NULL);
76 }
77
78 void InputSocket::relinkConnectionsDuplicate(InputSocket *relinkToSocket, int editorNodeInputSocketIndex, ExecutionSystem *graph)
79 {
80         if (!this->isConnected()) {
81                 Node *node = (Node *)this->getNode();
82                 switch (this->getDataType()) {
83                         case COM_DT_COLOR:
84                                 node->addSetColorOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
85                                 break;
86                         case COM_DT_VECTOR:
87                                 node->addSetVectorOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
88                                 break;
89                         case COM_DT_VALUE:
90                                 node->addSetValueOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
91                                 break;
92                 }
93                 return;
94         }
95         SocketConnection *newConnection = new SocketConnection();
96         OutputSocket *fromSocket = this->getConnection()->getFromSocket();
97         newConnection->setToSocket(relinkToSocket);
98         newConnection->setFromSocket(fromSocket);
99         relinkToSocket->setConnection(newConnection);
100         fromSocket->addConnection(newConnection);
101         graph->addSocketConnection(newConnection);
102 }
103
104 void InputSocket::relinkConnections(InputSocket *relinkToSocket,  int editorNodeInputSocketIndex, ExecutionSystem *graph)
105 {
106         if (isConnected()) {
107                 relinkConnections(relinkToSocket);
108         }
109         else {
110                 Node *node = (Node *)this->getNode();
111                 switch (this->getDataType()) {
112                         case COM_DT_COLOR:
113                                 node->addSetColorOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
114                                 break;
115                         case COM_DT_VECTOR:
116                                 node->addSetVectorOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
117                                 break;
118                         case COM_DT_VALUE:
119                                 node->addSetValueOperation(graph, relinkToSocket, editorNodeInputSocketIndex);
120                                 break;
121                 }
122         }
123 }
124
125 bool InputSocket::isStatic()
126 {
127         if (isConnected()) {
128                 NodeBase *node = this->getConnection()->getFromNode();
129                 if (node) {
130                         return node->isStatic();
131                 }
132         }
133         return true;
134 }
135 SocketReader *InputSocket::getReader()
136 {
137         return this->getOperation();
138 }
139
140 NodeOperation *InputSocket::getOperation() const
141 {
142         if (isConnected()) {
143                 return (NodeOperation *)this->m_connection->getFromSocket()->getNode();
144         }
145         else {
146                 return NULL;
147         }
148 }
149
150 float *InputSocket::getStaticValues()
151 {
152         /* XXX only works for socket types with actual float input values.
153          * currently all compositor socket types (value, rgba, vector) support this.
154          */
155         bNodeSocket *b_socket = this->getbNodeSocket();
156         static float default_null = 0.0f;
157         
158         switch (this->getDataType()) {
159                 case COM_DT_VALUE:
160                         return &((bNodeSocketValueFloat *)b_socket->default_value)->value;
161                 case COM_DT_COLOR:
162                         return ((bNodeSocketValueRGBA *)b_socket->default_value)->value;
163                 case COM_DT_VECTOR:
164                         return ((bNodeSocketValueVector *)b_socket->default_value)->value;
165                 default:
166                         /* XXX this should never happen, just added to please the compiler */
167                         return &default_null;
168         }
169 }