1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 Cirilo Bernardo <cirilo.bernardo@gmail.com>
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23  */
24 
25 #include <iostream>
26 #include <sstream>
27 #include <wx/log.h>
28 
29 #include "plugins/3dapi/ifsg_colors.h"
30 #include "3d_cache/sg/sg_colors.h"
31 
32 
33 extern char BadParent[];
34 extern char WrongParent[];
35 
36 
IFSG_COLORS(bool create)37 IFSG_COLORS::IFSG_COLORS( bool create )
38 {
39     m_node = nullptr;
40 
41     if( !create )
42         return;
43 
44     m_node = new SGCOLORS( nullptr );
45 
46     m_node->AssociateWrapper( &m_node );
47 }
48 
49 
IFSG_COLORS(SGNODE * aParent)50 IFSG_COLORS::IFSG_COLORS( SGNODE* aParent )
51 {
52     m_node = new SGCOLORS( nullptr );
53 
54     if( !m_node->SetParent( aParent ) )
55     {
56         delete m_node;
57         m_node = nullptr;
58 
59         wxLogTrace( MASK_3D_SG, "%s:%s:%d", __FILE__, __FUNCTION__, __LINE__ );
60 
61         return;
62     }
63 
64     m_node->AssociateWrapper( &m_node );
65 }
66 
67 
IFSG_COLORS(IFSG_NODE & aParent)68 IFSG_COLORS::IFSG_COLORS( IFSG_NODE& aParent )
69 {
70     SGNODE* pp = aParent.GetRawPtr();
71 
72 #ifdef DEBUG
73     // Braces needed due to dangling else warning from wxLogTrace macro
74     if( !pp )
75     {
76         wxLogTrace( MASK_3D_SG, "%s:%s:%d %s", __FILE__, __FUNCTION__, __LINE__, BadParent );
77     }
78 #endif
79 
80     m_node = new SGCOLORS( nullptr );
81 
82     if( !m_node->SetParent( pp ) )
83     {
84         delete m_node;
85         m_node = nullptr;
86 
87         wxLogTrace( MASK_3D_SG, "%s:%s:%d %s", __FILE__, __FUNCTION__, __LINE__,
88                     WrongParent );
89 
90         return;
91     }
92 
93     m_node->AssociateWrapper( &m_node );
94 }
95 
96 
Attach(SGNODE * aNode)97 bool IFSG_COLORS::Attach( SGNODE* aNode )
98 {
99     if( m_node )
100         m_node->DisassociateWrapper( &m_node );
101 
102     m_node = nullptr;
103 
104     if( !aNode )
105         return false;
106 
107     if( S3D::SGTYPE_COLORS != aNode->GetNodeType() )
108     {
109         return false;
110     }
111 
112     m_node = aNode;
113     m_node->AssociateWrapper( &m_node );
114 
115     return true;
116 }
117 
118 
NewNode(SGNODE * aParent)119 bool IFSG_COLORS::NewNode( SGNODE* aParent )
120 {
121     if( m_node )
122         m_node->DisassociateWrapper( &m_node );
123 
124     m_node = new SGCOLORS( aParent );
125 
126     if( aParent != m_node->GetParent() )
127     {
128         wxLogTrace( MASK_3D_SG, "%s:%s:%d  * [BUG] invalid SGNODE parent (%s) to SGCOLORS",
129                     __FILE__, __FUNCTION__, __LINE__,
130                     aParent->GetNodeTypeName( aParent->GetNodeType() ) );
131 
132         delete m_node;
133         m_node = nullptr;
134         return false;
135     }
136 
137     m_node->AssociateWrapper( &m_node );
138 
139     return true;
140 }
141 
142 
NewNode(IFSG_NODE & aParent)143 bool IFSG_COLORS::NewNode( IFSG_NODE& aParent )
144 {
145     SGNODE* np = aParent.GetRawPtr();
146 
147     wxCHECK( np, false );
148 
149     return NewNode( np );
150 }
151 
152 
GetColorList(size_t & aListSize,SGCOLOR * & aColorList)153 bool IFSG_COLORS::GetColorList( size_t& aListSize, SGCOLOR*& aColorList )
154 {
155     wxCHECK( m_node, false );
156 
157     return ( (SGCOLORS*) m_node )->GetColorList( aListSize, aColorList );
158 }
159 
160 
SetColorList(size_t aListSize,const SGCOLOR * aColorList)161 bool IFSG_COLORS::SetColorList( size_t aListSize, const SGCOLOR* aColorList )
162 {
163     wxCHECK( m_node, false );
164 
165     ( (SGCOLORS*) m_node )->SetColorList( aListSize, aColorList );
166 
167     return true;
168 }
169 
170 
AddColor(double aRedValue,double aGreenValue,double aBlueValue)171 bool IFSG_COLORS::AddColor( double aRedValue, double aGreenValue, double aBlueValue )
172 {
173     wxCHECK( m_node, false );
174 
175     ( (SGCOLORS*) m_node )->AddColor( aRedValue, aGreenValue, aBlueValue );
176 
177     return true;
178 }
179 
180 
AddColor(const SGCOLOR & aColor)181 bool IFSG_COLORS::AddColor( const SGCOLOR& aColor )
182 {
183     wxCHECK( m_node, false );
184 
185     ( (SGCOLORS*) m_node )->AddColor( aColor );
186 
187     return true;
188 }
189