Fri, 15 Mar 2013 21:53:50 +0200
Set window title dynamically based on filename
0 | 1 | #include <vector> |
2 | ||
3 | #include "common.h" | |
4 | #include "io.h" | |
5 | #include "ldtypes.h" | |
6 | #include "misc.h" | |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
7 | #include "gui.h" |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
8 | #include "bbox.h" |
0 | 9 | |
10 | // ============================================================================= | |
11 | // IO_FindLoadedFile (str) | |
12 | // | |
13 | // Returns a pointer to the first found open file with the given name. | |
14 | // ============================================================================= | |
15 | OpenFile* IO_FindLoadedFile (str name) { | |
16 | OpenFile* file; | |
17 | ||
18 | for (uint i = 0; i < g_LoadedFiles.size(); i++) { | |
19 | file = g_LoadedFiles[i]; | |
7
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
20 | if (!file->zFileName.icompare (name)) |
0 | 21 | return file; |
22 | } | |
23 | ||
24 | return NULL; | |
25 | } | |
26 | ||
27 | // ============================================================================= | |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
28 | // IO_OpenLDrawFile (str) |
0 | 29 | // |
30 | // Opens the given file and parses the LDraw code within. | |
31 | // ============================================================================= | |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
32 | OpenFile* IO_OpenLDrawFile (str path) { |
0 | 33 | FILE* fp = fopen (path.chars (), "r"); |
34 | ||
35 | if (!fp) { | |
36 | printf ("Couldn't open %s!\n", path.chars ()); | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | OpenFile* load = new OpenFile; | |
7
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
41 | load->zFileName = path; |
0 | 42 | |
43 | vector<str> lines; | |
44 | ||
45 | { | |
46 | char line[1024]; | |
47 | while (fgets (line, sizeof line, fp)) { | |
48 | // Trim the trailing newline | |
49 | str zLine = line; | |
50 | while (zLine[~zLine - 1] == '\n' || zLine[~zLine - 1] == '\r') | |
51 | zLine -= 1; | |
52 | ||
53 | lines.push_back (zLine); | |
54 | } | |
55 | } | |
56 | ||
57 | fclose (fp); | |
58 | ||
59 | for (ulong i = 0; i < lines.size(); ++i) | |
60 | load->objects.push_back (ParseLine (lines[i])); | |
61 | ||
62 | g_LoadedFiles.push_back (load); | |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
63 | g_CurrentFile = g_LoadedFiles[g_LoadedFiles.size() - 1]; |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
64 | |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
65 | // Recalculate the bounding box |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
66 | g_BBox.calculate(); |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
67 | |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
68 | // Rebuild the object tree view now. |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
69 | g_qWindow->buildObjList (); |
7
098e3c4949c6
Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
70 | g_qWindow->setTitle (); |
4
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
71 | |
758302636564
improve opening, don't auto-load 55966.dat (:P)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
72 | return g_CurrentFile; |
0 | 73 | } |
74 | ||
75 | // ============================================================================= | |
76 | // ParseLine (str) | |
77 | // | |
78 | // Parses a string line containing an LDraw object and returns the object parsed. | |
79 | // ============================================================================= | |
80 | LDObject* ParseLine (str zLine) { | |
81 | str zNoWhitespace = zLine; | |
82 | StripWhitespace (zNoWhitespace); | |
83 | if (!~zNoWhitespace) { | |
84 | // Line was empty, or only consisted of whitespace | |
85 | return new LDEmpty; | |
86 | } | |
87 | ||
88 | char c = zLine[0]; | |
89 | vector<str> tokens = zLine / " "; | |
90 | ||
91 | switch (c - '0') { | |
92 | case 0: | |
93 | { | |
94 | // Comment | |
95 | LDComment* obj = new LDComment; | |
96 | obj->zText = zLine.substr (1, -1); | |
97 | return obj; | |
98 | } | |
99 | ||
100 | case 1: | |
101 | { | |
102 | // Subfile | |
103 | LDSubfile* obj = new LDSubfile; | |
104 | obj->dColor = atoi (tokens[1]); | |
105 | obj->vPosition = ParseVertex (zLine, 2); // 2 - 4 | |
106 | ||
107 | for (short i = 0; i < 9; ++i) | |
108 | obj->faMatrix[i] = atof (tokens[i + 5]); // 5 - 13 | |
109 | ||
110 | obj->zFileName = tokens[14]; | |
111 | return obj; | |
112 | } | |
113 | ||
114 | case 2: | |
115 | { | |
116 | // Line | |
117 | LDLine* obj = new LDLine; | |
118 | obj->dColor = GetWordInt (zLine, 1); | |
119 | for (short i = 0; i < 2; ++i) | |
120 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 | |
121 | return obj; | |
122 | } | |
123 | ||
124 | case 3: | |
125 | { | |
126 | // Triangle | |
127 | LDTriangle* obj = new LDTriangle; | |
128 | obj->dColor = GetWordInt (zLine, 1); | |
129 | ||
130 | for (short i = 0; i < 3; ++i) | |
131 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 10 | |
132 | ||
133 | return obj; | |
134 | } | |
135 | ||
136 | case 4: | |
137 | { | |
138 | // Quadrilateral | |
139 | LDQuad* obj = new LDQuad; | |
140 | obj->dColor = GetWordInt (zLine, 1); | |
141 | ||
142 | for (short i = 0; i < 4; ++i) | |
143 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 13 | |
144 | ||
145 | return obj; | |
146 | } | |
147 | ||
148 | case 5: | |
149 | { | |
150 | // Conditional line | |
151 | LDCondLine* obj = new LDCondLine; | |
152 | obj->dColor = GetWordInt (zLine, 1); | |
153 | ||
154 | for (short i = 0; i < 2; ++i) | |
155 | obj->vaCoords[i] = ParseVertex (zLine, 2 + (i * 3)); // 2 - 7 | |
156 | ||
157 | for (short i = 0; i < 2; ++i) | |
158 | obj->vaControl[i] = ParseVertex (zLine, 8 + (i * 3)); // 8 - 13 | |
159 | return obj; | |
160 | } | |
161 | ||
162 | default: | |
163 | { | |
164 | // Strange line we couldn't parse | |
165 | LDGibberish* obj = new LDGibberish; | |
166 | obj->zContent = zLine; | |
167 | return obj; | |
168 | } | |
169 | } | |
170 | } |