1 /*
2 * CRRCsim - the Charles River Radio Control Club Flight Simulator Project
3 *
4 * Copyright (C) 2010 Jens Wilhelm Wulf (original author)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 */
21 #include "../i18n.h"
22 #include "../global.h"
23 #include "../aircraft.h"
24 #include "crrc_gui_main.h"
25 #include "crrc_loadrobot.h"
26 #include "../crrc_main.h"
27 #include "../mod_misc/filesystools.h"
28 #include "../mod_misc/lib_conversions.h"
29 #include "util.h"
30 #include "../robots.h"
31 #include "../mod_robots/robotfile.h"
32
33 #include <iostream>
34 #include <sys/types.h>
35 #include <dirent.h>
36 #include <string>
37
38 static void CGUIRobotSelCallback(puObject *obj);
39 static void CGUIRobotSelPlaneListCallback(puObject *obj);
40
41 #define LIST_WIDGET_H (200)
42 #define LIST_WIDGET_W (250)
43 #define DESCRIPTION_H (75)
44 #define DESCRIPTION_W (LIST_WIDGET_W+300)
45 #define BUTTON_BOX_HEIGHT (2*DLG_DEF_SPACE+DLG_DEF_BUTTON_HEIGHT)
46 #define SCROLL_W (16)
47
48
CGUILoadRobotDialog()49 CGUILoadRobotDialog::CGUILoadRobotDialog()
50 : CRRCDialog()
51 {
52 // height of a text label
53 int msg_height = puGetDefaultLegendFont().getStringHeight("jX")
54 + puGetDefaultLegendFont().getStringDescender()
55 + PUSTR_TGAP + PUSTR_BGAP;
56
57 int ymax = LIST_WIDGET_H + DESCRIPTION_H + 1*msg_height + 2*DLG_DEF_SPACE + BUTTON_BOX_HEIGHT;
58 int yleft = ymax;
59 int yright= ymax;
60 int HELP_H = LIST_WIDGET_H + msg_height - DLG_CHECK_H - DLG_DEF_SPACE;
61
62
63 // demo or robot checkbox
64 yright -= DLG_DEF_SPACE + DLG_CHECK_H;
65 check_demo = new puButton(2*DLG_DEF_SPACE + LIST_WIDGET_W,
66 yright,
67 2*DLG_DEF_SPACE + LIST_WIDGET_W + DLG_CHECK_W,
68 yright + DLG_CHECK_H);
69 check_demo->setButtonType(PUBUTTON_VCHECK);
70 check_demo->setLabelPlace(PUPLACE_CENTERED_RIGHT);
71 check_demo->setLabel(_("Load as demo, replacing user input"));
72 check_demo->setValue(0);
73
74 // the description box
75 yright -= HELP_H + DLG_DEF_SPACE;
76 puaLargeInput* help = new puaLargeInput(2*DLG_DEF_SPACE + LIST_WIDGET_W, yright,
77 DESCRIPTION_W - LIST_WIDGET_W - DLG_DEF_SPACE, HELP_H,
78 1, // num of arrow pairs
79 SCROLL_W, // slider width
80 1); // wrap text
81 help->setText(_("In case you check the box above, this robot will replace your manually controlled airplane.\n"
82 "Load an airplane to regain manual control."));
83 help->disableInput();
84
85 // file selection list
86 yleft -= LIST_WIDGET_H + DLG_DEF_SPACE + msg_height;
87 files = new puaFileBox(DLG_DEF_SPACE,
88 yleft,
89 LIST_WIDGET_W,
90 LIST_WIDGET_H,
91 FileSysTools::getHomePath().c_str(), "crrclog", true);
92 files->setLabelPlace(PUPLACE_TOP_LEFT);
93 files->setLabel(_("Select file:"));
94 files->setUserData(this);
95 files->setCallback(CGUIRobotSelPlaneListCallback);
96
97 // the description box
98 yleft -= DESCRIPTION_H + DLG_DEF_SPACE;
99 description = new puaLargeInput(DLG_DEF_SPACE,
100 yleft,
101 DESCRIPTION_W,
102 DESCRIPTION_H,
103 1, // num of arrow pairs
104 16, // slider width
105 0); // wrap text
106 description->disableInput();
107 description->setText(_("This is a short description of the selected record."));
108
109 // finalize the dialog
110 close();
111 setSize(DESCRIPTION_W+2*DLG_DEF_SPACE, ymax);
112 setCallback(CGUIRobotSelCallback);
113 centerOnScreen();
114 reveal();
115 }
116
117
118 /**
119 * Destroy the dialog.
120 */
~CGUILoadRobotDialog()121 CGUILoadRobotDialog::~CGUILoadRobotDialog()
122 {
123 }
124
getFilename()125 std::string CGUILoadRobotDialog::getFilename()
126 {
127 const char* path = files->getPath();
128 const char* file = files->getStringValue();
129
130 if (path != 0 && file != 0)
131 return(std::string(path) + "/" + file);
132 else
133 return("");
134 }
135
updateFileInfo()136 void CGUILoadRobotDialog::updateFileInfo()
137 {
138 RobotFile* rf = new RobotFile(getFilename());
139 description->setText(rf->ReadDescription().c_str());
140 delete rf;
141 }
142
143
144 /**
145 * This callback is invoked when a new file is selected from
146 * the file list
147 */
CGUIRobotSelPlaneListCallback(puObject * obj)148 void CGUIRobotSelPlaneListCallback(puObject *obj)
149 {
150 CGUILoadRobotDialog *dlg = (CGUILoadRobotDialog*)obj->getUserData();
151 dlg->updateFileInfo();
152 }
153
154
155 /** \brief The dialog's callback.
156 *
157 * Determine if a file was selected and load it.
158 */
CGUIRobotSelCallback(puObject * obj)159 void CGUIRobotSelCallback(puObject *obj)
160 {
161 CGUILoadRobotDialog *dlg = (CGUILoadRobotDialog*)obj;
162
163 if (obj->getIntegerValue() == CRRC_DIALOG_OK)
164 {
165 if (FileSysTools::fileExists(dlg->getFilename()))
166 {
167 // Dialog left by clicking OK
168 if (dlg->check_demo->getBooleanValue())
169 {
170 Global::aircraft->setModel(NULL);
171 Global::aircraft->loadDemo(dlg->getFilename());
172 initialize_flight_model();
173 }
174 else
175 {
176 Global::robots->AddRobot(dlg->getFilename());
177 }
178 }
179 }
180
181 Global::gui->hide();
182 puDeleteObject(obj);
183 }
184