24 #include <qcolor.h> |
24 #include <qcolor.h> |
25 |
25 |
26 static color* g_LDColors[MAX_COLORS]; |
26 static color* g_LDColors[MAX_COLORS]; |
27 |
27 |
28 void initColors () { |
28 void initColors () { |
29 printf ("%s: initializing color information.\n", __func__); |
29 print ("%1: initializing color information.\n", __func__); |
30 |
30 |
31 color* col; |
31 color* col; |
32 |
32 |
33 // Always make sure there's 16 and 24 available. They're special like that. |
33 // Always make sure there's 16 and 24 available. They're special like that. |
34 col = new color; |
34 col = new color; |
35 col->hexcode = "#AAAAAA"; |
35 col->hexcode = "#AAAAAA"; |
36 col->faceColor = col->hexcode.chars (); |
36 col->faceColor = col->hexcode; |
37 col->edgeColor = Qt::black; |
37 col->edgeColor = Qt::black; |
38 g_LDColors[maincolor] = col; |
38 g_LDColors[maincolor] = col; |
39 |
39 |
40 col = new color; |
40 col = new color; |
41 col->hexcode = "#000000"; |
41 col->hexcode = "#000000"; |
79 // ============================================================================= |
79 // ============================================================================= |
80 void parseLDConfig () { |
80 void parseLDConfig () { |
81 FILE* fp = openLDrawFile ("LDConfig.ldr", false); |
81 FILE* fp = openLDrawFile ("LDConfig.ldr", false); |
82 |
82 |
83 if (!fp) { |
83 if (!fp) { |
84 critical (fmt ("Unable to open LDConfig.ldr for parsing! (%s)", strerror (errno))); |
84 critical (fmt ("Unable to open LDConfig.ldr for parsing! (%1)", strerror (errno))); |
85 return; |
85 return; |
86 } |
86 } |
87 |
87 |
88 // Read in the lines |
88 // Read in the lines |
89 char buf[1024]; |
89 char buf[1024]; |
90 while (fgets (buf, sizeof buf, fp)) { |
90 while (fgets (buf, sizeof buf, fp)) { |
91 if (strlen (buf) == 0 || buf[0] != '0') |
91 if (strlen (buf) == 0 || buf[0] != '0') |
92 continue; // empty or illogical |
92 continue; // empty or illogical |
93 |
93 |
94 // Use StringParser to parse the LDConfig.ldr file. |
94 // Use StringParser to parse the LDConfig.ldr file. |
95 str line = str (buf).strip ({'\n', '\r'}); |
95 str line = buf; |
|
96 line.remove ('\r'); |
|
97 line.remove ('\n'); |
96 StringParser pars (line, ' '); |
98 StringParser pars (line, ' '); |
97 |
99 |
98 short code = 0, alpha = 255; |
100 short code = 0, alpha = 255; |
99 str name, facename, edgename, valuestr; |
101 str name, facename, edgename, valuestr; |
100 |
102 |
111 |
113 |
112 if (!isNumber (valuestr)) |
114 if (!isNumber (valuestr)) |
113 continue; // not a number |
115 continue; // not a number |
114 |
116 |
115 // Ensure that the code is within [0 - 511] |
117 // Ensure that the code is within [0 - 511] |
116 code = atoi (valuestr); |
118 bool ok; |
117 if (code < 0 || code >= 512) |
119 code = valuestr.toShort (&ok); |
|
120 if (!ok || code < 0 || code >= 512) |
118 continue; |
121 continue; |
119 |
122 |
120 // VALUE and EDGE tags |
123 // VALUE and EDGE tags |
121 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename)) |
124 if (!parseLDConfigTag (pars, "VALUE", facename) || !parseLDConfigTag (pars, "EDGE", edgename)) |
122 continue; |
125 continue; |
123 |
126 |
124 // Ensure that our colors are correct |
127 // Ensure that our colors are correct |
125 QColor faceColor (facename.chars()), |
128 QColor faceColor (facename), |
126 edgeColor (edgename.chars()); |
129 edgeColor (edgename); |
127 |
130 |
128 if (!faceColor.isValid () || !edgeColor.isValid ()) |
131 if (!faceColor.isValid () || !edgeColor.isValid ()) |
129 continue; |
132 continue; |
130 |
133 |
131 // Parse alpha if given. |
134 // Parse alpha if given. |
132 if (parseLDConfigTag (pars, "ALPHA", valuestr)) |
135 if (parseLDConfigTag (pars, "ALPHA", valuestr)) |
133 alpha = clamp<short> (atoi (valuestr), 0, 255); |
136 alpha = clamp<short> (valuestr.toShort (), 0, 255); |
134 |
137 |
135 color* col = new color; |
138 color* col = new color; |
136 col->name = name; |
139 col->name = name; |
137 col->faceColor = faceColor; |
140 col->faceColor = faceColor; |
138 col->edgeColor = edgeColor; |
141 col->edgeColor = edgeColor; |
139 col->hexcode = facename; |
142 col->hexcode = facename; |
140 col->faceColor.setAlpha (alpha); |
143 col->faceColor.setAlpha (alpha); |
141 col->index = code; |
144 col->index = code; |
142 |
|
143 g_LDColors[code] = col; |
145 g_LDColors[code] = col; |
144 } |
146 } |
145 |
147 |
146 fclose (fp); |
148 fclose (fp); |
147 } |
149 } |