FS2_Open
Open source remastering of the Freespace 2 engine
addvariabledlg.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 // AddVariableDlg.cpp : implementation file
11 //
12 
13 #include "stdafx.h"
14 #include "FRED.h"
15 #include "AddVariableDlg.h"
16 #include "parse/sexp.h"
17 
18 #ifdef _DEBUG
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22 
23 #define NO_RESET_FOCUS 0
24 #define RESET_FOCUS 1
25 
27 // CAddVariableDlg dialog
28 
29 
30 CAddVariableDlg::CAddVariableDlg(CWnd* pParent /*=NULL*/)
31  : CDialog(CAddVariableDlg::IDD, pParent)
32 {
33  //{{AFX_DATA_INIT(CAddVariableDlg)
34  m_default_value = _T("");
35  m_variable_name = _T("");
36  //}}AFX_DATA_INIT
37 }
38 
39 
40 void CAddVariableDlg::DoDataExchange(CDataExchange* pDX)
41 {
42  CDialog::DoDataExchange(pDX);
43  //{{AFX_DATA_MAP(CAddVariableDlg)
45  DDV_MaxChars(pDX, m_default_value, 31);
46  DDX_Text(pDX, IDC_ADD_VARIABLE_NAME, m_variable_name);
47  DDV_MaxChars(pDX, m_variable_name, 31);
48  //}}AFX_DATA_MAP
49 }
50 
51 
52 BEGIN_MESSAGE_MAP(CAddVariableDlg, CDialog)
53  //{{AFX_MSG_MAP(CAddVariableDlg)
54  ON_BN_CLICKED(IDC_TYPE_NUMBER, OnTypeNumber)
55  ON_BN_CLICKED(IDC_TYPE_STRING, OnTypeString)
56  ON_BN_CLICKED(IDC_TYPE_CAMPAIGN_PERSISTENT, OnTypeCampaignPersistent)
57  ON_BN_CLICKED(IDC_TYPE_PLAYER_PERSISTENT, OnTypePlayerPersistent)
58  ON_BN_CLICKED(IDC_TYPE_NETWORK_VARIABLE, OnTypeNetworkVariable)
59  //}}AFX_MSG_MAP
60 END_MESSAGE_MAP()
61 
63 // CAddVariableDlg message handlers
64 
65 void CAddVariableDlg::OnOK()
66 {
67  // validation name
68  validate_variable_name(RESET_FOCUS);
69 
70  // validate data
71  if ( m_name_validated ) {
72  validate_data(RESET_FOCUS);
73  }
74 
75  // both ok, then store results
76  if ( m_name_validated && m_data_validated ) {
77 // int sexp_add_variable(char *text, char*, int type);
78 // char temp_name[32];
79 // char temp_value[32];
80 // strcpy_s(temp_name, m_variable_name);
81 // strcpy_s(temp_value, m_default_value);
82  // SEXP_VARIABLE_NUMBER SEXP_VARIABLE_STRING
83 // int type;
84 //
85 // if (m_type_number) {
86 // type = SEXP_VARIABLE_NUMBER;
87 // } else {
88 // type = SEXP_VARIABLE_STRING;
89 // }
90 //
91 // // Goober5000
92 // if (m_type_player_persistent) {
93 // type |= SEXP_VARIABLE_PLAYER_PERSISTENT;
94 // } else if (m_type_campaign_persistent) {
95 // type |= SEXP_VARIABLE_CAMPAIGN_PERSISTENT;
96 // }
97 //
98 // m_sexp_var_index = sexp_add_variable(temp_value, temp_name, type);
99 // this get done for free CDialog::OnOk() UpdateData(TRUE);
100  m_create = true;
101 
102  CDialog::OnOK();
103  }
104 }
105 
107 {
108  CDialog::OnInitDialog();
109 
110  // TODO: Add extra initialization here
111  m_variable_name = "<Variable Name>";
112  m_default_value = "<Default Value>";
113 
114  // Set variable type to number
115  m_type_number = true;
117  m_type_player_persistent = false;
118  m_type_network_variable = false;
120 
121  m_name_validated = false;
122  m_data_validated = false;
123  m_create = false;
124 
125  // Send default name and values into dialog box
126  UpdateData(FALSE);
127 
128  return TRUE; // return TRUE unless you set the focus to a control
129  // EXCEPTION: OCX Property Pages should return FALSE
130 }
131 
132 bool is_sexp_variable_name(const char* temp_name)
133 {
134  for (int i=0; i<MAX_SEXP_VARIABLES; i++) {
136  if ( !strcmp(Sexp_variables[i].text, temp_name) ) {
137  return false;
138  }
139  }
140  }
141  // name not found
142  return true;
143 }
144 
145 
146 // Check variable name (1) changed (2) > 0 length (3) does not already exist
148 {
149  CString temp_name;
150 
151  CEdit *edit = (CEdit *) GetDlgItem(IDC_ADD_VARIABLE_NAME);
152  edit->GetWindowText(temp_name);
153 
154  // Check if any change and not already in list
155  if ( stricmp(temp_name, "<Variable Name>") ) {
156  if ( (strlen(temp_name) > 0) && (get_index_sexp_variable_name(LPCTSTR(temp_name)) == -1) ) { //not already in list and length > 0 { //-V805
157  // Goober5000 - replace spaces with hyphens
158  if (strchr(temp_name, ' ') != NULL)
159  {
160  // display msg
161  MessageBox("Variable names cannot contain spaces. Replacing with hyphens.");
162 
163  // replace chars
164  //Modified to make the code build. - Thunderbird
165  temp_name.Replace((TCHAR) ' ', (TCHAR) '-');
166  //temp_name.Replace((TCHAR) ' ', (TCHAR) '-'));
167 
168  // fix what's displayed
169  edit->SetFocus();
170  edit->SetWindowText(temp_name);
171  edit->SetSel(0, -1);
172  }
173 
174  m_name_validated = true;
175  } else {
176  // conflicting variable name
177  if (strlen(temp_name) == 0) { //-V805
178  edit->SetWindowText("<Variable Name>");
179  }
180  m_name_validated = false;
181  if (set_focus == RESET_FOCUS) {
182  MessageBox("Conflicting variable name");
183  edit->SetFocus();
184  edit->SetSel(0, -1);
185  }
186  }
187  } else {
188  // name unchanged from default
189  m_name_validated = false;
190  if (set_focus == RESET_FOCUS) {
191  MessageBox("Invalid variable name");
192  edit->SetFocus();
193  edit->SetSel(0, -1);
194  }
195  }
196 
197 }
198 
200 {
201  CString temp_data;
202 
203  CEdit *edit = (CEdit *) GetDlgItem(IDC_ADD_VARIABLE_DEFAULT_VALUE);
204  edit->GetWindowText(temp_data);
205 
206  // check for 0 string length
207  if (strlen(temp_data) == 0) { //-V805
208  m_data_validated = false;
209  } else {
210  if (m_type_number) {
211  // verify valid number
212  int temp_num = atoi(temp_data);
213  char buf[TOKEN_LENGTH];
214  sprintf(buf, "%d", temp_num);
215 
216  if ( stricmp(buf, temp_data) ) {
217  m_data_validated = false;
218  } else {
219  m_data_validated = true;
220  }
221  } else {
222  m_data_validated = true;
223  }
224  }
225 
226  // Display message and reset focus
227  if ( (!m_data_validated) && (set_focus == RESET_FOCUS) ) {
228  MessageBox("Invalid Default Value.");
229  edit->SetFocus();
230  edit->SetSel(0, -1);
231  }
232 }
233 
234 // Set type to number
236 {
237  m_type_number = true;
239 }
240 
241 // Set type to string
243 {
244  m_type_number = false;
246 }
247 
249 {
250  m_type_player_persistent = ((CButton *) GetDlgItem(IDC_TYPE_PLAYER_PERSISTENT))->GetCheck() ? true : false;
251 
254 
256 }
257 
259 {
260  m_type_campaign_persistent = ((CButton *) GetDlgItem(IDC_TYPE_CAMPAIGN_PERSISTENT))->GetCheck() ? true : false;
261 
263  m_type_player_persistent = false;
264 
266 }
267 
269 {
270  m_type_network_variable = ((CButton *) GetDlgItem(IDC_TYPE_NETWORK_VARIABLE))->GetCheck() ? true : false;
272 }
273 
274 // Set type check boxes
276 {
277 
278  CButton *button_string = (CButton *) GetDlgItem(IDC_TYPE_STRING);
279  CButton *button_number = (CButton *) GetDlgItem(IDC_TYPE_NUMBER);
280 
281  button_number->SetCheck( m_type_number);
282  button_string->SetCheck(!m_type_number);
283 
284  ((CButton *) GetDlgItem(IDC_TYPE_CAMPAIGN_PERSISTENT))->SetCheck(m_type_campaign_persistent);
285  ((CButton *) GetDlgItem(IDC_TYPE_PLAYER_PERSISTENT))->SetCheck(m_type_player_persistent);
286  ((CButton *) GetDlgItem(IDC_TYPE_NETWORK_VARIABLE))->SetCheck(m_type_network_variable);
287 }
int i
Definition: multi_pxo.cpp:466
virtual void DoDataExchange(CDataExchange *pDX)
CString m_variable_name
afx_msg void set_variable_type()
CString m_default_value
#define RESET_FOCUS
afx_msg void OnTypeNumber()
#define SEXP_VARIABLE_SET
Definition: sexp.h:903
#define TRUE
Definition: pstypes.h:399
sexp_variable Sexp_variables[MAX_SEXP_VARIABLES]
Definition: sexp.cpp:846
int get_index_sexp_variable_name(const char *text)
Definition: sexp.cpp:29324
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: Glext.h:7308
GLenum type
Definition: Gl.h:1492
afx_msg void validate_data(int set_focus)
#define IDC_TYPE_NUMBER
Definition: resource.h:226
afx_msg void OnTypeString()
afx_msg void OnTypeCampaignPersistent()
CAddVariableDlg(CWnd *pParent=NULL)
sprintf(buf,"(%f,%f,%f)", v3->xyz.x, v3->xyz.y, v3->xyz.z)
#define IDC_ADD_VARIABLE_DEFAULT_VALUE
Definition: resource.h:779
afx_msg void OnTypePlayerPersistent()
bool m_type_player_persistent
bool m_type_network_variable
virtual BOOL OnInitDialog()
bool is_sexp_variable_name(const char *temp_name)
int BOOL
Definition: config.h:80
#define TOKEN_LENGTH
Definition: globals.h:41
#define IDC_TYPE_NETWORK_VARIABLE
Definition: resource.h:1085
#define IDC_TYPE_STRING
Definition: resource.h:215
#define IDC_TYPE_CAMPAIGN_PERSISTENT
Definition: resource.h:1076
int MessageBox(HWND h, const char *s1, const char *s2, int i)
#define IDC_TYPE_PLAYER_PERSISTENT
Definition: resource.h:1078
afx_msg void OnTypeNetworkVariable()
afx_msg void validate_variable_name(int set_focus)
bool m_type_campaign_persistent
#define IDC_ADD_VARIABLE_NAME
Definition: resource.h:984
#define FALSE
Definition: pstypes.h:400
#define stricmp(s1, s2)
Definition: config.h:271
#define MAX_SEXP_VARIABLES
Definition: sexp.h:23