]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DTreePlanter.h
eol style
[xonotic/netradiant.git] / contrib / bobtoolz / DTreePlanter.h
1 /*
2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #ifndef __DTREE_H__
21 #define __DTREE_H__
22
23 #include "../include/igl.h"
24 #include "DEntity.h"
25 #include "misc.h"
26 #include "ScriptParser.h"
27
28 #define MAX_QPATH 64
29
30 typedef struct treeModel_s {
31         char name[MAX_QPATH];
32 } treeModel_t;
33
34 #define MAX_TP_MODELS 256
35
36 class DTreePlanter : public IWindowListener {
37 public:
38         virtual bool OnMouseMove(guint32 nFlags, gdouble x, gdouble y);
39         virtual bool OnLButtonDown(guint32 nFlags, gdouble x, gdouble y);
40         virtual bool OnMButtonDown(guint32 nFlags, gdouble x, gdouble y);
41         virtual bool OnRButtonDown(guint32 nFlags, gdouble x, gdouble y);
42         virtual bool OnLButtonUp(guint32 nFlags, gdouble x, gdouble y);
43         virtual bool OnMButtonUp(guint32 nFlags, gdouble x, gdouble y);
44         virtual bool OnRButtonUp(guint32 nFlags, gdouble x, gdouble y);
45         virtual bool OnKeyPressed(char *s) { return false; }
46         virtual bool Paint() { return true; }
47         virtual void Close() { }
48         
49         DTreePlanter() {
50                 m_refCount =    1;
51                 m_hooked =              false;
52                 m_XYWrapper =   NULL;
53                 m_numModels =   0;
54                 m_offset =              0;
55                 m_maxPitch =    0;
56                 m_minPitch =    0;
57                 m_maxYaw =              0;
58                 m_minYaw =              0;
59                 m_setAngles =   false;
60                 m_useScale =    false;
61                 m_autoLink =    false;
62                 m_linkNum =             0;
63
64                 Register();
65
66                 m_world.LoadSelectedBrushes();
67
68                 char buffer[256];
69                 GetFilename( buffer, "bt/tp_ent.txt" );
70
71                 FILE* file = fopen( buffer, "rb" );
72                 if(file) {
73                         fseek( file, 0, SEEK_END );
74                         int len = ftell( file );
75                         fseek( file, 0, SEEK_SET );
76
77                         if(len) {
78                                 char* buf = new char[len+1];
79                                 buf[len] = '\0';
80                                 // parser will do the cleanup, dont delete.
81
82                                 fread( buf, len, 1, file );
83
84                                 CScriptParser parser;
85                                 parser.SetScript( buf );
86
87                                 ReadConfig( &parser );
88                         }
89
90                         fclose( file );
91                 }
92         }
93
94 #define MT(t)   !stricmp( pToken, t )
95 #define GT              pToken = pScriptParser->GetToken( true )
96 #define CT              if(!*pToken) { return; }
97
98         void ReadConfig( CScriptParser* pScriptParser ) {
99                 const char* GT;
100                 CT;
101
102                 do {
103                         GT;
104                         if(*pToken == '}') {
105                                 break;
106                         }
107
108                         if(MT("model")) {
109                                 if(m_numModels >= MAX_TP_MODELS) {
110                                         return;
111                                 }
112
113                                 GT; CT;
114
115                                 strncpy( m_trees[m_numModels++].name, pToken, MAX_QPATH );
116                         } else if(MT("link")) {
117                                 GT; CT;
118
119                                 strncpy( m_linkName, pToken, MAX_QPATH );
120
121                                 m_autoLink = true;
122                         } else if(MT("entity")) {
123                                 GT; CT;
124
125                                 strncpy( m_entType, pToken, MAX_QPATH );
126                         } else if(MT("offset")) {
127                                 GT; CT;
128
129                                 m_offset = atoi(pToken);
130                         } else if(MT("pitch")) {
131                                 GT; CT;
132
133                                 m_minPitch = atoi(pToken);
134
135                                 GT; CT;
136
137                                 m_maxPitch = atoi(pToken);
138
139                                 m_setAngles = true;
140                         } else if(MT("yaw")) {
141                                 GT; CT;
142
143                                 m_minYaw = atoi(pToken);
144
145                                 GT; CT;
146
147                                 m_maxYaw = atoi(pToken);
148
149                                 m_setAngles = true;
150                         } else if(MT("scale")) {
151                                 GT; CT;
152
153                                 m_minScale = static_cast< float >( atof( pToken ) );
154
155                                 GT; CT;
156
157                                 m_maxScale = static_cast< float >( atof( pToken ) );
158
159                                 m_useScale = true;
160                         } else if(MT("numlinks")) {
161                                 GT; CT;
162
163                                 m_linkNum = atoi( pToken );
164                         }
165                 } while( true );
166         }
167
168         virtual ~DTreePlanter() {
169                 UnRegister();
170         }
171
172         virtual void IncRef() { m_refCount++; }
173         virtual void DecRef() { m_refCount--; if (m_refCount <= 0) delete this; }
174
175         void Register() {
176                 if(!m_hooked) {
177                         g_MessageTable.m_pfnHookWindow( this );
178                         m_XYWrapper = g_MessageTable.m_pfnGetXYWndWrapper();
179                         m_hooked = true;
180                 }
181         }
182
183         void UnRegister() {
184                 if(m_hooked) {
185                         g_MessageTable.m_pfnUnHookWindow( this );
186                         m_XYWrapper = NULL;
187                         m_hooked = false;
188                 }
189         }
190
191         bool FindDropPoint(vec3_t in, vec3_t out);
192         void DropEntsToGround( void );
193         void MakeChain( void );
194         void SelectChain( void );
195
196 private:
197         IXYWndWrapper*  m_XYWrapper;
198         DEntity                 m_world;
199
200         treeModel_t             m_trees[MAX_TP_MODELS];
201
202         int                             m_refCount;
203         int                             m_numModels;
204         int                             m_offset;
205         int                             m_maxPitch;
206         int                             m_minPitch;
207         int                             m_maxYaw;
208         int                             m_minYaw;
209
210         char                    m_entType[MAX_QPATH];
211         char                    m_linkName[MAX_QPATH];
212         int                             m_linkNum;
213
214         float                   m_minScale;
215         float                   m_maxScale;
216
217         bool                    m_hooked;
218         bool                    m_useScale;
219         bool                    m_setAngles;
220         bool                    m_autoLink;
221 };
222
223 #endif