CAttributeImpl.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CAttributes.h"
  5. #include "fast_atof.h"
  6. #include "ITexture.h"
  7. #include "IVideoDriver.h"
  8. namespace irr
  9. {
  10. namespace io
  11. {
  12. /*
  13. basic types
  14. */
  15. // Attribute implemented for boolean values
  16. class CBoolAttribute : public IAttribute
  17. {
  18. public:
  19. CBoolAttribute(const char* name, bool value)
  20. {
  21. Name = name;
  22. setBool(value);
  23. }
  24. virtual s32 getInt()
  25. {
  26. return BoolValue ? 1 : 0;
  27. }
  28. virtual f32 getFloat()
  29. {
  30. return BoolValue ? 1.0f : 0.0f;
  31. }
  32. virtual bool getBool()
  33. {
  34. return BoolValue;
  35. }
  36. virtual core::stringw getStringW()
  37. {
  38. return core::stringw( BoolValue ? L"true" : L"false" );
  39. }
  40. virtual void setInt(s32 intValue)
  41. {
  42. BoolValue = (intValue != 0);
  43. }
  44. virtual void setFloat(f32 floatValue)
  45. {
  46. BoolValue = (floatValue != 0);
  47. }
  48. virtual void setBool(bool boolValue)
  49. {
  50. BoolValue = boolValue;
  51. }
  52. virtual void setString(const char* string)
  53. {
  54. BoolValue = strcmp(string, "true") == 0;
  55. }
  56. virtual E_ATTRIBUTE_TYPE getType() const
  57. {
  58. return EAT_BOOL;
  59. }
  60. virtual const wchar_t* getTypeString() const
  61. {
  62. return L"bool";
  63. }
  64. bool BoolValue;
  65. };
  66. // Attribute implemented for integers
  67. class CIntAttribute : public IAttribute
  68. {
  69. public:
  70. CIntAttribute(const char* name, s32 value)
  71. {
  72. Name = name;
  73. setInt(value);
  74. }
  75. virtual s32 getInt()
  76. {
  77. return Value;
  78. }
  79. virtual f32 getFloat()
  80. {
  81. return (f32)Value;
  82. }
  83. virtual bool getBool()
  84. {
  85. return (Value != 0);
  86. }
  87. virtual core::stringw getStringW()
  88. {
  89. return core::stringw(Value);
  90. }
  91. virtual void setInt(s32 intValue)
  92. {
  93. Value = intValue;
  94. }
  95. virtual void setFloat(f32 floatValue)
  96. {
  97. Value = (s32)floatValue;
  98. };
  99. virtual void setString(const char* text)
  100. {
  101. Value = atoi(text);
  102. }
  103. virtual E_ATTRIBUTE_TYPE getType() const
  104. {
  105. return EAT_INT;
  106. }
  107. virtual const wchar_t* getTypeString() const
  108. {
  109. return L"int";
  110. }
  111. s32 Value;
  112. };
  113. // Attribute implemented for floats
  114. class CFloatAttribute : public IAttribute
  115. {
  116. public:
  117. CFloatAttribute(const char* name, f32 value)
  118. {
  119. Name = name;
  120. setFloat(value);
  121. }
  122. virtual s32 getInt()
  123. {
  124. return (s32)Value;
  125. }
  126. virtual f32 getFloat()
  127. {
  128. return Value;
  129. }
  130. virtual bool getBool()
  131. {
  132. return (Value != 0);
  133. }
  134. virtual core::stringw getStringW()
  135. {
  136. return core::stringw((double)Value);
  137. }
  138. virtual void setInt(s32 intValue)
  139. {
  140. Value = (f32)intValue;
  141. }
  142. virtual void setFloat(f32 floatValue)
  143. {
  144. Value = floatValue;
  145. }
  146. virtual void setString(const char* text)
  147. {
  148. Value = core::fast_atof(text);
  149. }
  150. virtual E_ATTRIBUTE_TYPE getType() const
  151. {
  152. return EAT_FLOAT;
  153. }
  154. virtual const wchar_t* getTypeString() const
  155. {
  156. return L"float";
  157. }
  158. f32 Value;
  159. };
  160. /*
  161. Types which can be represented as a list of numbers
  162. */
  163. // Base class for all attributes which are a list of numbers-
  164. // vectors, colors, positions, triangles, etc
  165. class CNumbersAttribute : public IAttribute
  166. {
  167. public:
  168. CNumbersAttribute(const char* name, video::SColorf value) :
  169. ValueI(), ValueF(), Count(4), IsFloat(true)
  170. {
  171. Name = name;
  172. ValueF.push_back(value.r);
  173. ValueF.push_back(value.g);
  174. ValueF.push_back(value.b);
  175. ValueF.push_back(value.a);
  176. }
  177. CNumbersAttribute(const char* name, video::SColor value) :
  178. ValueI(), ValueF(), Count(4), IsFloat(false)
  179. {
  180. Name = name;
  181. ValueI.push_back(value.getRed());
  182. ValueI.push_back(value.getGreen());
  183. ValueI.push_back(value.getBlue());
  184. ValueI.push_back(value.getAlpha());
  185. }
  186. CNumbersAttribute(const char* name, core::vector3df value) :
  187. ValueI(), ValueF(), Count(3), IsFloat(true)
  188. {
  189. Name = name;
  190. ValueF.push_back(value.X);
  191. ValueF.push_back(value.Y);
  192. ValueF.push_back(value.Z);
  193. }
  194. CNumbersAttribute(const char* name, core::rect<s32> value) :
  195. ValueI(), ValueF(), Count(4), IsFloat(false)
  196. {
  197. Name = name;
  198. ValueI.push_back(value.UpperLeftCorner.X);
  199. ValueI.push_back(value.UpperLeftCorner.Y);
  200. ValueI.push_back(value.LowerRightCorner.X);
  201. ValueI.push_back(value.LowerRightCorner.Y);
  202. }
  203. CNumbersAttribute(const char* name, core::rect<f32> value) :
  204. ValueI(), ValueF(), Count(4), IsFloat(true)
  205. {
  206. Name = name;
  207. ValueF.push_back(value.UpperLeftCorner.X);
  208. ValueF.push_back(value.UpperLeftCorner.Y);
  209. ValueF.push_back(value.LowerRightCorner.X);
  210. ValueF.push_back(value.LowerRightCorner.Y);
  211. }
  212. CNumbersAttribute(const char* name, core::matrix4 value) :
  213. ValueI(), ValueF(), Count(16), IsFloat(true)
  214. {
  215. Name = name;
  216. for (s32 r=0; r<4; ++r)
  217. for (s32 c=0; c<4; ++c)
  218. ValueF.push_back(value(r,c));
  219. }
  220. CNumbersAttribute(const char* name, core::quaternion value) :
  221. ValueI(), ValueF(), Count(4), IsFloat(true)
  222. {
  223. Name = name;
  224. ValueF.push_back(value.X);
  225. ValueF.push_back(value.Y);
  226. ValueF.push_back(value.Z);
  227. ValueF.push_back(value.W);
  228. }
  229. CNumbersAttribute(const char* name, core::aabbox3d<f32> value) :
  230. ValueI(), ValueF(), Count(6), IsFloat(true)
  231. {
  232. Name = name;
  233. ValueF.push_back(value.MinEdge.X);
  234. ValueF.push_back(value.MinEdge.Y);
  235. ValueF.push_back(value.MinEdge.Z);
  236. ValueF.push_back(value.MaxEdge.X);
  237. ValueF.push_back(value.MaxEdge.Y);
  238. ValueF.push_back(value.MaxEdge.Z);
  239. }
  240. CNumbersAttribute(const char* name, core::plane3df value) :
  241. ValueI(), ValueF(), Count(4), IsFloat(true)
  242. {
  243. Name = name;
  244. ValueF.push_back(value.Normal.X);
  245. ValueF.push_back(value.Normal.Y);
  246. ValueF.push_back(value.Normal.Z);
  247. ValueF.push_back(value.D);
  248. }
  249. CNumbersAttribute(const char* name, core::triangle3df value) :
  250. ValueI(), ValueF(), Count(9), IsFloat(true)
  251. {
  252. Name = name;
  253. ValueF.push_back(value.pointA.X);
  254. ValueF.push_back(value.pointA.Y);
  255. ValueF.push_back(value.pointA.Z);
  256. ValueF.push_back(value.pointB.X);
  257. ValueF.push_back(value.pointB.Y);
  258. ValueF.push_back(value.pointB.Z);
  259. ValueF.push_back(value.pointC.X);
  260. ValueF.push_back(value.pointC.Y);
  261. ValueF.push_back(value.pointC.Z);
  262. }
  263. CNumbersAttribute(const char* name, core::vector2df value) :
  264. ValueI(), ValueF(), Count(2), IsFloat(true)
  265. {
  266. Name = name;
  267. ValueF.push_back(value.X);
  268. ValueF.push_back(value.Y);
  269. }
  270. CNumbersAttribute(const char* name, core::vector2di value) :
  271. ValueI(), ValueF(), Count(2), IsFloat(false)
  272. {
  273. Name = name;
  274. ValueI.push_back(value.X);
  275. ValueI.push_back(value.Y);
  276. }
  277. CNumbersAttribute(const char* name, core::line2di value) :
  278. ValueI(), ValueF(), Count(4), IsFloat(false)
  279. {
  280. Name = name;
  281. ValueI.push_back(value.start.X);
  282. ValueI.push_back(value.start.Y);
  283. ValueI.push_back(value.end.X);
  284. ValueI.push_back(value.end.Y);
  285. }
  286. CNumbersAttribute(const char* name, core::line2df value) :
  287. ValueI(), ValueF(), Count(4), IsFloat(true)
  288. {
  289. Name = name;
  290. ValueF.push_back(value.start.X);
  291. ValueF.push_back(value.start.Y);
  292. ValueF.push_back(value.end.X);
  293. ValueF.push_back(value.end.Y);
  294. }
  295. CNumbersAttribute(const char* name, core::line3df value) :
  296. ValueI(), ValueF(), Count(6), IsFloat(true)
  297. {
  298. Name = name;
  299. ValueF.push_back(value.start.X);
  300. ValueF.push_back(value.start.Y);
  301. ValueF.push_back(value.start.Z);
  302. ValueF.push_back(value.end.X);
  303. ValueF.push_back(value.end.Y);
  304. ValueF.push_back(value.end.Z);
  305. }
  306. CNumbersAttribute(const char* name, core::dimension2du value) :
  307. ValueI(), ValueF(), Count(2), IsFloat(false)
  308. {
  309. Name = name;
  310. ValueI.push_back(value.Width);
  311. ValueI.push_back(value.Height);
  312. }
  313. CNumbersAttribute(const char* name, core::dimension2df value) :
  314. ValueI(), ValueF(), Count(2), IsFloat(true)
  315. {
  316. Name = name;
  317. ValueF.push_back(value.Width);
  318. ValueF.push_back(value.Height);
  319. }
  320. // getting values
  321. virtual s32 getInt()
  322. {
  323. if (Count==0)
  324. return 0;
  325. if (IsFloat)
  326. return (s32)ValueF[0];
  327. else
  328. return ValueI[0];
  329. }
  330. virtual f32 getFloat()
  331. {
  332. if (Count==0)
  333. return 0.0f;
  334. if (IsFloat)
  335. return ValueF[0];
  336. else
  337. return (f32)ValueI[0];
  338. }
  339. virtual bool getBool()
  340. {
  341. // return true if any number is nonzero
  342. bool ret=false;
  343. for (u32 i=0; i < Count; ++i)
  344. if ( IsFloat ? (ValueF[i] != 0) : (ValueI[i] != 0) )
  345. {
  346. ret=true;
  347. break;
  348. }
  349. return ret;
  350. }
  351. virtual core::stringc getString()
  352. {
  353. core::stringc outstr;
  354. for (u32 i=0; i <Count; ++i)
  355. {
  356. if (IsFloat)
  357. outstr += ValueF[i];
  358. else
  359. outstr += ValueI[i];
  360. if (i < Count-1)
  361. outstr += ", ";
  362. }
  363. return outstr;
  364. }
  365. virtual core::stringw getStringW()
  366. {
  367. core::stringw outstr;
  368. for (u32 i=0; i <Count; ++i)
  369. {
  370. if (IsFloat)
  371. outstr += ValueF[i];
  372. else
  373. outstr += ValueI[i];
  374. if (i < Count-1)
  375. outstr += L", ";
  376. }
  377. return outstr;
  378. }
  379. virtual core::position2di getPosition()
  380. {
  381. core::position2di p;
  382. if (IsFloat)
  383. {
  384. p.X = (s32)(Count > 0 ? ValueF[0] : 0);
  385. p.Y = (s32)(Count > 1 ? ValueF[1] : 0);
  386. }
  387. else
  388. {
  389. p.X = Count > 0 ? ValueI[0] : 0;
  390. p.Y = Count > 1 ? ValueI[1] : 0;
  391. }
  392. return p;
  393. }
  394. virtual core::vector3df getVector()
  395. {
  396. core::vector3df v;
  397. if (IsFloat)
  398. {
  399. v.X = Count > 0 ? ValueF[0] : 0;
  400. v.Y = Count > 1 ? ValueF[1] : 0;
  401. v.Z = Count > 2 ? ValueF[2] : 0;
  402. }
  403. else
  404. {
  405. v.X = (f32)(Count > 0 ? ValueI[0] : 0);
  406. v.Y = (f32)(Count > 1 ? ValueI[1] : 0);
  407. v.Z = (f32)(Count > 2 ? ValueI[2] : 0);
  408. }
  409. return v;
  410. }
  411. virtual core::vector2df getVector2d()
  412. {
  413. core::vector2df v;
  414. if (IsFloat)
  415. {
  416. v.X = Count > 0 ? ValueF[0] : 0;
  417. v.Y = Count > 1 ? ValueF[1] : 0;
  418. }
  419. else
  420. {
  421. v.X = (f32)(Count > 0 ? ValueI[0] : 0);
  422. v.Y = (f32)(Count > 1 ? ValueI[1] : 0);
  423. }
  424. return v;
  425. }
  426. virtual video::SColorf getColorf()
  427. {
  428. video::SColorf c;
  429. if (IsFloat)
  430. {
  431. c.setColorComponentValue(0, Count > 0 ? ValueF[0] : 0);
  432. c.setColorComponentValue(1, Count > 1 ? ValueF[1] : 0);
  433. c.setColorComponentValue(2, Count > 2 ? ValueF[2] : 0);
  434. c.setColorComponentValue(3, Count > 3 ? ValueF[3] : 0);
  435. }
  436. else
  437. {
  438. c.setColorComponentValue(0, Count > 0 ? (f32)(ValueI[0]) / 255.0f : 0);
  439. c.setColorComponentValue(1, Count > 1 ? (f32)(ValueI[1]) / 255.0f : 0);
  440. c.setColorComponentValue(2, Count > 2 ? (f32)(ValueI[2]) / 255.0f : 0);
  441. c.setColorComponentValue(3, Count > 3 ? (f32)(ValueI[3]) / 255.0f : 0);
  442. }
  443. return c;
  444. }
  445. virtual video::SColor getColor()
  446. {
  447. return getColorf().toSColor();
  448. }
  449. virtual core::rect<s32> getRect()
  450. {
  451. core::rect<s32> r;
  452. if (IsFloat)
  453. {
  454. r.UpperLeftCorner.X = (s32)(Count > 0 ? ValueF[0] : 0);
  455. r.UpperLeftCorner.Y = (s32)(Count > 1 ? ValueF[1] : 0);
  456. r.LowerRightCorner.X = (s32)(Count > 2 ? ValueF[2] : r.UpperLeftCorner.X);
  457. r.LowerRightCorner.Y = (s32)(Count > 3 ? ValueF[3] : r.UpperLeftCorner.Y);
  458. }
  459. else
  460. {
  461. r.UpperLeftCorner.X = Count > 0 ? ValueI[0] : 0;
  462. r.UpperLeftCorner.Y = Count > 1 ? ValueI[1] : 0;
  463. r.LowerRightCorner.X = Count > 2 ? ValueI[2] : r.UpperLeftCorner.X;
  464. r.LowerRightCorner.Y = Count > 3 ? ValueI[3] : r.UpperLeftCorner.Y;
  465. }
  466. return r;
  467. }
  468. virtual core::dimension2du getDimension2d()
  469. {
  470. core::dimension2d<u32> dim;
  471. if (IsFloat)
  472. {
  473. dim.Width = (u32)(Count > 0 ? ValueF[0] : 0);
  474. dim.Height = (u32)(Count > 1 ? ValueF[1] : 0);
  475. }
  476. else
  477. {
  478. dim.Width = (u32)(Count > 0 ? ValueI[0] : 0);
  479. dim.Height = (u32)(Count > 1 ? ValueI[1] : 0);
  480. }
  481. return dim;
  482. }
  483. virtual core::matrix4 getMatrix()
  484. {
  485. core::matrix4 ret;
  486. if (IsFloat)
  487. {
  488. for (u32 r=0; r<4; ++r)
  489. for (u32 c=0; c<4; ++c)
  490. if (Count > c+r*4)
  491. ret(r,c) = ValueF[c+r*4];
  492. }
  493. else
  494. {
  495. for (u32 r=0; r<4; ++r)
  496. for (u32 c=0; c<4; ++c)
  497. if (Count > c+r*4)
  498. ret(r,c) = (f32)ValueI[c+r*4];
  499. }
  500. return ret;
  501. }
  502. virtual core::quaternion getQuaternion()
  503. {
  504. core::quaternion ret;
  505. if (IsFloat)
  506. {
  507. ret.X = Count > 0 ? ValueF[0] : 0.0f;
  508. ret.Y = Count > 1 ? ValueF[1] : 0.0f;
  509. ret.Z = Count > 2 ? ValueF[2] : 0.0f;
  510. ret.W = Count > 3 ? ValueF[3] : 0.0f;
  511. }
  512. else
  513. {
  514. ret.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  515. ret.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  516. ret.Z = Count > 2 ? (f32)ValueI[2] : 0.0f;
  517. ret.W = Count > 3 ? (f32)ValueI[3] : 0.0f;
  518. }
  519. return ret;
  520. }
  521. virtual core::triangle3df getTriangle()
  522. {
  523. core::triangle3df ret;
  524. if (IsFloat)
  525. {
  526. ret.pointA.X = Count > 0 ? ValueF[0] : 0.0f;
  527. ret.pointA.Y = Count > 1 ? ValueF[1] : 0.0f;
  528. ret.pointA.Z = Count > 2 ? ValueF[2] : 0.0f;
  529. ret.pointB.X = Count > 3 ? ValueF[3] : 0.0f;
  530. ret.pointB.Y = Count > 4 ? ValueF[4] : 0.0f;
  531. ret.pointB.Z = Count > 5 ? ValueF[5] : 0.0f;
  532. ret.pointC.X = Count > 6 ? ValueF[6] : 0.0f;
  533. ret.pointC.Y = Count > 7 ? ValueF[7] : 0.0f;
  534. ret.pointC.Z = Count > 8 ? ValueF[8] : 0.0f;
  535. }
  536. else
  537. {
  538. ret.pointA.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  539. ret.pointA.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  540. ret.pointA.Z = Count > 2 ? (f32)ValueI[2] : 0.0f;
  541. ret.pointB.X = Count > 3 ? (f32)ValueI[3] : 0.0f;
  542. ret.pointB.Y = Count > 4 ? (f32)ValueI[4] : 0.0f;
  543. ret.pointB.Z = Count > 5 ? (f32)ValueI[5] : 0.0f;
  544. ret.pointC.X = Count > 6 ? (f32)ValueI[6] : 0.0f;
  545. ret.pointC.Y = Count > 7 ? (f32)ValueI[7] : 0.0f;
  546. ret.pointC.Z = Count > 8 ? (f32)ValueI[8] : 0.0f;
  547. }
  548. return ret;
  549. }
  550. virtual core::plane3df getPlane()
  551. {
  552. core::plane3df ret;
  553. if (IsFloat)
  554. {
  555. ret.Normal.X = Count > 0 ? ValueF[0] : 0.0f;
  556. ret.Normal.Y = Count > 1 ? ValueF[1] : 0.0f;
  557. ret.Normal.Z = Count > 2 ? ValueF[2] : 0.0f;
  558. ret.D = Count > 3 ? ValueF[3] : 0.0f;
  559. }
  560. else
  561. {
  562. ret.Normal.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  563. ret.Normal.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  564. ret.Normal.Z = Count > 2 ? (f32)ValueI[2] : 0.0f;
  565. ret.D = Count > 3 ? (f32)ValueI[3] : 0.0f;
  566. }
  567. return ret;
  568. }
  569. virtual core::aabbox3df getBBox()
  570. {
  571. core::aabbox3df ret;
  572. if (IsFloat)
  573. {
  574. ret.MinEdge.X = Count > 0 ? ValueF[0] : 0.0f;
  575. ret.MinEdge.Y = Count > 1 ? ValueF[1] : 0.0f;
  576. ret.MinEdge.Z = Count > 2 ? ValueF[2] : 0.0f;
  577. ret.MaxEdge.X = Count > 3 ? ValueF[3] : 0.0f;
  578. ret.MaxEdge.Y = Count > 4 ? ValueF[4] : 0.0f;
  579. ret.MaxEdge.Z = Count > 5 ? ValueF[5] : 0.0f;
  580. }
  581. else
  582. {
  583. ret.MinEdge.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  584. ret.MinEdge.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  585. ret.MinEdge.Z = Count > 2 ? (f32)ValueI[2] : 0.0f;
  586. ret.MaxEdge.X = Count > 3 ? (f32)ValueI[3] : 0.0f;
  587. ret.MaxEdge.Y = Count > 4 ? (f32)ValueI[4] : 0.0f;
  588. ret.MaxEdge.Z = Count > 5 ? (f32)ValueI[5] : 0.0f;
  589. }
  590. return ret;
  591. }
  592. virtual core::line2df getLine2d()
  593. {
  594. core::line2df ret;
  595. if (IsFloat)
  596. {
  597. ret.start.X = Count > 0 ? ValueF[0] : 0.0f;
  598. ret.start.Y = Count > 1 ? ValueF[1] : 0.0f;
  599. ret.end.X = Count > 2 ? ValueF[2] : 0.0f;
  600. ret.end.Y = Count > 3 ? ValueF[3] : 0.0f;
  601. }
  602. else
  603. {
  604. ret.start.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  605. ret.start.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  606. ret.end.X = Count > 2 ? (f32)ValueI[2] : 0.0f;
  607. ret.end.Y = Count > 3 ? (f32)ValueI[3] : 0.0f;
  608. }
  609. return ret;
  610. }
  611. virtual core::line3df getLine3d()
  612. {
  613. core::line3df ret;
  614. if (IsFloat)
  615. {
  616. ret.start.X = Count > 0 ? ValueF[0] : 0.0f;
  617. ret.start.Y = Count > 1 ? ValueF[1] : 0.0f;
  618. ret.start.Z = Count > 2 ? ValueF[2] : 0.0f;
  619. ret.end.X = Count > 3 ? ValueF[3] : 0.0f;
  620. ret.end.Y = Count > 4 ? ValueF[4] : 0.0f;
  621. ret.end.Z = Count > 5 ? ValueF[5] : 0.0f;
  622. }
  623. else
  624. {
  625. ret.start.X = Count > 0 ? (f32)ValueI[0] : 0.0f;
  626. ret.start.Y = Count > 1 ? (f32)ValueI[1] : 0.0f;
  627. ret.start.Z = Count > 2 ? (f32)ValueI[2] : 0.0f;
  628. ret.end.X = Count > 3 ? (f32)ValueI[3] : 0.0f;
  629. ret.end.Y = Count > 4 ? (f32)ValueI[4] : 0.0f;
  630. ret.end.Z = Count > 5 ? (f32)ValueI[5] : 0.0f;
  631. }
  632. return ret;
  633. }
  634. //! get float array
  635. virtual core::array<f32> getFloatArray()
  636. {
  637. if (!IsFloat)
  638. {
  639. ValueF.clear();
  640. for (u32 i=0; i<Count; ++i)
  641. ValueF.push_back( (f32) ValueI[i] );
  642. }
  643. return ValueF;
  644. }
  645. //! get int array
  646. virtual core::array<s32> getIntArray()
  647. {
  648. if (IsFloat)
  649. {
  650. ValueI.clear();
  651. for (u32 i=0; i<Count; ++i)
  652. ValueI.push_back( (s32) ValueF[i] );
  653. }
  654. return ValueI;
  655. }
  656. // setting values
  657. virtual void setInt(s32 intValue)
  658. {
  659. // set all values
  660. for (u32 i=0; i < Count; ++i)
  661. if (IsFloat)
  662. ValueF[i] = (f32)intValue;
  663. else
  664. ValueI[i] = intValue;
  665. }
  666. virtual void setFloat(f32 floatValue)
  667. {
  668. // set all values
  669. for (u32 i=0; i < Count; ++i)
  670. if (IsFloat)
  671. ValueF[i] = floatValue;
  672. else
  673. ValueI[i] = (s32)floatValue;
  674. }
  675. virtual void setBool(bool boolValue)
  676. {
  677. setInt( boolValue ? 1 : 0);
  678. }
  679. virtual void setString(const char* text)
  680. {
  681. // parse text
  682. const char* P = (const char*)text;
  683. reset();
  684. u32 i=0;
  685. for ( i=0; i<Count && *P; ++i )
  686. {
  687. while(*P && P[0]!='-' && ( P[0]==' ' || (P[0] < '0' || P[0] > '9') ) )
  688. ++P;
  689. // set value
  690. if ( *P)
  691. {
  692. if (IsFloat)
  693. {
  694. f32 c = 0;
  695. P = core::fast_atof_move(P, c);
  696. ValueF[i] = c;
  697. }
  698. else
  699. {
  700. // todo: fix this to read ints properly
  701. f32 c = 0;
  702. P = core::fast_atof_move(P, c);
  703. ValueI[i] = (s32)c;
  704. }
  705. }
  706. }
  707. // todo: warning message
  708. //if (i < Count-1)
  709. //{
  710. //
  711. //}
  712. }
  713. virtual void setPosition(core::position2di v)
  714. {
  715. reset();
  716. if (IsFloat)
  717. {
  718. if (Count > 0) ValueF[0] = (f32)v.X;
  719. if (Count > 1) ValueF[1] = (f32)v.Y;
  720. }
  721. else
  722. {
  723. if (Count > 0) ValueI[0] = v.X;
  724. if (Count > 1) ValueI[1] = v.Y;
  725. }
  726. }
  727. virtual void setVector(core::vector3df v)
  728. {
  729. reset();
  730. if (IsFloat)
  731. {
  732. if (Count > 0) ValueF[0] = v.X;
  733. if (Count > 1) ValueF[1] = v.Y;
  734. if (Count > 2) ValueF[2] = v.Z;
  735. }
  736. else
  737. {
  738. if (Count > 0) ValueI[0] = (s32)v.X;
  739. if (Count > 1) ValueI[1] = (s32)v.Y;
  740. if (Count > 2) ValueI[2] = (s32)v.Z;
  741. }
  742. }
  743. virtual void setColor(video::SColorf color)
  744. {
  745. reset();
  746. if (IsFloat)
  747. {
  748. if (Count > 0) ValueF[0] = color.r;
  749. if (Count > 1) ValueF[1] = color.g;
  750. if (Count > 2) ValueF[2] = color.b;
  751. if (Count > 3) ValueF[3] = color.a;
  752. }
  753. else
  754. {
  755. if (Count > 0) ValueI[0] = (s32)(color.r * 255);
  756. if (Count > 1) ValueI[1] = (s32)(color.g * 255);
  757. if (Count > 2) ValueI[2] = (s32)(color.b * 255);
  758. if (Count > 3) ValueI[3] = (s32)(color.a * 255);
  759. }
  760. }
  761. virtual void setColor(video::SColor color)
  762. {
  763. reset();
  764. if (IsFloat)
  765. {
  766. if (Count > 0) ValueF[0] = (f32)color.getRed() / 255.0f;
  767. if (Count > 1) ValueF[1] = (f32)color.getGreen() / 255.0f;
  768. if (Count > 2) ValueF[2] = (f32)color.getBlue() / 255.0f;
  769. if (Count > 3) ValueF[3] = (f32)color.getAlpha() / 255.0f;
  770. }
  771. else
  772. {
  773. if (Count > 0) ValueI[0] = color.getRed();
  774. if (Count > 1) ValueI[1] = color.getGreen();
  775. if (Count > 2) ValueI[2] = color.getBlue();
  776. if (Count > 3) ValueI[3] = color.getAlpha();
  777. }
  778. }
  779. virtual void setRect(core::rect<s32> value)
  780. {
  781. reset();
  782. if (IsFloat)
  783. {
  784. if (Count > 0) ValueF[0] = (f32)value.UpperLeftCorner.X;
  785. if (Count > 1) ValueF[1] = (f32)value.UpperLeftCorner.Y;
  786. if (Count > 2) ValueF[2] = (f32)value.LowerRightCorner.X;
  787. if (Count > 3) ValueF[3] = (f32)value.LowerRightCorner.Y;
  788. }
  789. else
  790. {
  791. if (Count > 0) ValueI[0] = value.UpperLeftCorner.X;
  792. if (Count > 1) ValueI[1] = value.UpperLeftCorner.Y;
  793. if (Count > 2) ValueI[2] = value.LowerRightCorner.X;
  794. if (Count > 3) ValueI[3] = value.LowerRightCorner.Y;
  795. }
  796. }
  797. virtual void setMatrix(core::matrix4 value)
  798. {
  799. reset();
  800. if (IsFloat)
  801. {
  802. for (u32 r=0; r<4; ++r)
  803. for (u32 c=0; c<4; ++c)
  804. if (Count > c+r*4)
  805. ValueF[c+r*4] = value(r,c);
  806. }
  807. else
  808. {
  809. for (u32 r=0; r<4; ++r)
  810. for (u32 c=0; c<4; ++c)
  811. if (Count > c+r*4)
  812. ValueI[c+r*4] = (s32)value(r,c);
  813. }
  814. }
  815. virtual void setQuaternion(core::quaternion value)
  816. {
  817. reset();
  818. if (IsFloat)
  819. {
  820. if (Count > 0) ValueF[0] = value.X;
  821. if (Count > 1) ValueF[1] = value.Y;
  822. if (Count > 2) ValueF[2] = value.Z;
  823. if (Count > 3) ValueF[3] = value.W;
  824. }
  825. else
  826. {
  827. if (Count > 0) ValueI[0] = (s32)value.X;
  828. if (Count > 1) ValueI[1] = (s32)value.Y;
  829. if (Count > 2) ValueI[2] = (s32)value.Z;
  830. if (Count > 3) ValueI[3] = (s32)value.W;
  831. }
  832. }
  833. virtual void setBoundingBox(core::aabbox3d<f32> value)
  834. {
  835. reset();
  836. if (IsFloat)
  837. {
  838. if (Count > 0) ValueF[0] = value.MinEdge.X;
  839. if (Count > 1) ValueF[1] = value.MinEdge.Y;
  840. if (Count > 2) ValueF[2] = value.MinEdge.Z;
  841. if (Count > 3) ValueF[3] = value.MaxEdge.X;
  842. if (Count > 4) ValueF[4] = value.MaxEdge.Y;
  843. if (Count > 5) ValueF[5] = value.MaxEdge.Z;
  844. }
  845. else
  846. {
  847. if (Count > 0) ValueI[0] = (s32)value.MinEdge.X;
  848. if (Count > 1) ValueI[1] = (s32)value.MinEdge.Y;
  849. if (Count > 2) ValueI[2] = (s32)value.MinEdge.Z;
  850. if (Count > 3) ValueI[3] = (s32)value.MaxEdge.X;
  851. if (Count > 4) ValueI[4] = (s32)value.MaxEdge.Y;
  852. if (Count > 5) ValueI[5] = (s32)value.MaxEdge.Z;
  853. }
  854. }
  855. virtual void setPlane(core::plane3df value)
  856. {
  857. reset();
  858. if (IsFloat)
  859. {
  860. if (Count > 0) ValueF[0] = value.Normal.X;
  861. if (Count > 1) ValueF[1] = value.Normal.Y;
  862. if (Count > 2) ValueF[2] = value.Normal.Z;
  863. if (Count > 3) ValueF[3] = value.D;
  864. }
  865. else
  866. {
  867. if (Count > 0) ValueI[0] = (s32)value.Normal.X;
  868. if (Count > 1) ValueI[1] = (s32)value.Normal.Y;
  869. if (Count > 2) ValueI[2] = (s32)value.Normal.Z;
  870. if (Count > 3) ValueI[3] = (s32)value.D;
  871. }
  872. }
  873. virtual void setTriangle3d(core::triangle3df value)
  874. {
  875. reset();
  876. if (IsFloat)
  877. {
  878. if (Count > 0) ValueF[0] = value.pointA.X;
  879. if (Count > 1) ValueF[1] = value.pointA.Y;
  880. if (Count > 2) ValueF[2] = value.pointA.Z;
  881. if (Count > 3) ValueF[3] = value.pointB.X;
  882. if (Count > 4) ValueF[4] = value.pointB.Y;
  883. if (Count > 5) ValueF[5] = value.pointB.Z;
  884. if (Count > 6) ValueF[6] = value.pointC.X;
  885. if (Count > 7) ValueF[7] = value.pointC.Y;
  886. if (Count > 8) ValueF[8] = value.pointC.Z;
  887. }
  888. else
  889. {
  890. if (Count > 0) ValueI[0] = (s32)value.pointA.X;
  891. if (Count > 1) ValueI[1] = (s32)value.pointA.Y;
  892. if (Count > 2) ValueI[2] = (s32)value.pointA.Z;
  893. if (Count > 3) ValueI[3] = (s32)value.pointB.X;
  894. if (Count > 4) ValueI[4] = (s32)value.pointB.Y;
  895. if (Count > 5) ValueI[5] = (s32)value.pointB.Z;
  896. if (Count > 6) ValueI[6] = (s32)value.pointC.X;
  897. if (Count > 7) ValueI[7] = (s32)value.pointC.Y;
  898. if (Count > 8) ValueI[8] = (s32)value.pointC.Z;
  899. }
  900. }
  901. virtual void setVector2d(core::vector2df v)
  902. {
  903. reset();
  904. if (IsFloat)
  905. {
  906. if (Count > 0) ValueF[0] = v.X;
  907. if (Count > 1) ValueF[1] = v.Y;
  908. }
  909. else
  910. {
  911. if (Count > 0) ValueI[0] = (s32)v.X;
  912. if (Count > 1) ValueI[1] = (s32)v.Y;
  913. }
  914. }
  915. virtual void setVector2d(core::vector2di v)
  916. {
  917. reset();
  918. if (IsFloat)
  919. {
  920. if (Count > 0) ValueF[0] = (f32)v.X;
  921. if (Count > 1) ValueF[1] = (f32)v.Y;
  922. }
  923. else
  924. {
  925. if (Count > 0) ValueI[0] = v.X;
  926. if (Count > 1) ValueI[1] = v.Y;
  927. }
  928. }
  929. virtual void setLine2d(core::line2di v)
  930. {
  931. reset();
  932. if (IsFloat)
  933. {
  934. if (Count > 0) ValueF[0] = (f32)v.start.X;
  935. if (Count > 1) ValueF[1] = (f32)v.start.Y;
  936. if (Count > 2) ValueF[2] = (f32)v.end.X;
  937. if (Count > 3) ValueF[3] = (f32)v.end.Y;
  938. }
  939. else
  940. {
  941. if (Count > 0) ValueI[0] = v.start.X;
  942. if (Count > 1) ValueI[1] = v.start.Y;
  943. if (Count > 2) ValueI[2] = v.end.X;
  944. if (Count > 3) ValueI[3] = v.end.Y;
  945. }
  946. }
  947. virtual void setLine2d(core::line2df v)
  948. {
  949. reset();
  950. if (IsFloat)
  951. {
  952. if (Count > 0) ValueF[0] = v.start.X;
  953. if (Count > 1) ValueF[1] = v.start.Y;
  954. if (Count > 2) ValueF[2] = v.end.X;
  955. if (Count > 3) ValueF[3] = v.end.Y;
  956. }
  957. else
  958. {
  959. if (Count > 0) ValueI[0] = (s32)v.start.X;
  960. if (Count > 1) ValueI[1] = (s32)v.start.Y;
  961. if (Count > 2) ValueI[2] = (s32)v.end.X;
  962. if (Count > 3) ValueI[3] = (s32)v.end.Y;
  963. }
  964. }
  965. virtual void setDimension2d(core::dimension2du v)
  966. {
  967. reset();
  968. if (IsFloat)
  969. {
  970. if (Count > 0) ValueF[0] = (f32)v.Width;
  971. if (Count > 1) ValueF[1] = (f32)v.Height;
  972. }
  973. else
  974. {
  975. if (Count > 0) ValueI[0] = (s32)v.Width;
  976. if (Count > 1) ValueI[1] = (s32)v.Height;
  977. }
  978. }
  979. //! set float array
  980. virtual void setFloatArray(core::array<f32> &vals)
  981. {
  982. reset();
  983. for (u32 i=0; i<vals.size() && i<Count; ++i)
  984. {
  985. if (IsFloat)
  986. ValueF[i] = vals[i];
  987. else
  988. ValueI[i] = (s32)vals[i];
  989. }
  990. }
  991. //! set int array
  992. virtual void setIntArray(core::array<s32> &vals)
  993. {
  994. reset();
  995. for (u32 i=0; i<vals.size() && i<Count; ++i)
  996. {
  997. if (IsFloat)
  998. ValueF[i] = (f32)vals[i];
  999. else
  1000. ValueI[i] = vals[i];
  1001. }
  1002. }
  1003. //! is it a number list?
  1004. virtual bool isNumberList()
  1005. {
  1006. return true;
  1007. }
  1008. //! is it a float list?
  1009. virtual bool isFloat()
  1010. {
  1011. return IsFloat;
  1012. }
  1013. virtual E_ATTRIBUTE_TYPE getType() const
  1014. {
  1015. if (IsFloat)
  1016. return EAT_FLOATARRAY;
  1017. else
  1018. return EAT_INTARRAY;
  1019. }
  1020. virtual const wchar_t* getTypeString() const
  1021. {
  1022. if (IsFloat)
  1023. return L"floatlist";
  1024. else
  1025. return L"intlist";
  1026. }
  1027. protected:
  1028. //! clear all values
  1029. void reset()
  1030. {
  1031. if (IsFloat)
  1032. for (u32 i=0; i < Count ; ++i)
  1033. ValueF[i] = 0.0f;
  1034. else
  1035. for (u32 i=0; i < Count ; ++i)
  1036. ValueI[i] = 0;
  1037. }
  1038. core::array<s32> ValueI;
  1039. core::array<f32> ValueF;
  1040. u32 Count;
  1041. bool IsFloat;
  1042. };
  1043. // Attribute implemented for floating point colors
  1044. class CColorfAttribute : public CNumbersAttribute
  1045. {
  1046. public:
  1047. CColorfAttribute(const char* name, video::SColorf value) : CNumbersAttribute(name, value) {}
  1048. virtual s32 getInt()
  1049. {
  1050. return getColor().color;
  1051. }
  1052. virtual f32 getFloat()
  1053. {
  1054. return (f32)getColor().color;
  1055. }
  1056. virtual void setInt(s32 intValue)
  1057. {
  1058. video::SColorf c = video::SColor(intValue);
  1059. ValueF[0] = c.r;
  1060. ValueF[1] = c.g;
  1061. ValueF[2] = c.b;
  1062. ValueF[3] = c.a;
  1063. }
  1064. virtual void setFloat(f32 floatValue)
  1065. {
  1066. setInt((s32)floatValue);
  1067. }
  1068. virtual E_ATTRIBUTE_TYPE getType() const
  1069. {
  1070. return EAT_COLORF;
  1071. }
  1072. virtual const wchar_t* getTypeString() const
  1073. {
  1074. return L"colorf";
  1075. }
  1076. };
  1077. // Attribute implemented for colors
  1078. class CColorAttribute : public CNumbersAttribute
  1079. {
  1080. public:
  1081. CColorAttribute(const char* name, const video::SColorf& value) : CNumbersAttribute(name, value) {}
  1082. CColorAttribute(const char* name, const video::SColor& value) : CNumbersAttribute(name, value) {}
  1083. virtual s32 getInt()
  1084. {
  1085. return getColor().color;
  1086. }
  1087. virtual f32 getFloat()
  1088. {
  1089. return (f32)getColor().color;
  1090. }
  1091. virtual void setInt(s32 intValue)
  1092. {
  1093. video::SColorf c = video::SColor(intValue);
  1094. ValueF[0] = c.r;
  1095. ValueF[1] = c.g;
  1096. ValueF[2] = c.b;
  1097. ValueF[3] = c.a;
  1098. }
  1099. virtual void setFloat(f32 floatValue)
  1100. {
  1101. setInt((s32)floatValue);
  1102. }
  1103. virtual core::stringw getStringW()
  1104. {
  1105. char tmp[10];
  1106. const video::SColor c = getColor();
  1107. sprintf(tmp, "%02x%02x%02x%02x", c.getAlpha(), c.getRed(), c.getGreen(), c.getBlue());
  1108. return core::stringw(tmp);
  1109. }
  1110. virtual void setString(const char* text)
  1111. {
  1112. u32 c;
  1113. if (sscanf(text, "%08x", &c)!=1)
  1114. {
  1115. CNumbersAttribute::setString(text);
  1116. }
  1117. else
  1118. setColor(c);
  1119. }
  1120. virtual E_ATTRIBUTE_TYPE getType() const
  1121. {
  1122. return EAT_COLOR;
  1123. }
  1124. virtual const wchar_t* getTypeString() const
  1125. {
  1126. return L"color";
  1127. }
  1128. };
  1129. // Attribute implemented for 3d vectors
  1130. class CVector3DAttribute : public CNumbersAttribute
  1131. {
  1132. public:
  1133. CVector3DAttribute(const char* name, core::vector3df value) : CNumbersAttribute(name, value) {}
  1134. virtual E_ATTRIBUTE_TYPE getType() const
  1135. {
  1136. return EAT_VECTOR3D;
  1137. }
  1138. virtual core::matrix4 getMatrix()
  1139. {
  1140. core::matrix4 ret;
  1141. ret.makeIdentity();
  1142. ret.setTranslation( core::vector3df(ValueF[0],ValueF[1],ValueF[2]) );
  1143. return ret;
  1144. }
  1145. virtual const wchar_t* getTypeString() const
  1146. {
  1147. return L"vector3d";
  1148. }
  1149. };
  1150. // Attribute implemented for 2d vectors
  1151. class CVector2DAttribute : public CNumbersAttribute
  1152. {
  1153. public:
  1154. CVector2DAttribute(const char* name, core::vector2df value) : CNumbersAttribute(name, value) {}
  1155. virtual E_ATTRIBUTE_TYPE getType() const
  1156. {
  1157. return EAT_VECTOR2D;
  1158. }
  1159. virtual const wchar_t* getTypeString() const
  1160. {
  1161. return L"vector2d";
  1162. }
  1163. };
  1164. // Attribute implemented for 2d vectors
  1165. class CPosition2DAttribute : public CNumbersAttribute
  1166. {
  1167. public:
  1168. CPosition2DAttribute(const char* name, core::position2di value) : CNumbersAttribute(name, value) {}
  1169. virtual E_ATTRIBUTE_TYPE getType() const
  1170. {
  1171. return EAT_POSITION2D;
  1172. }
  1173. virtual const wchar_t* getTypeString() const
  1174. {
  1175. return L"position";
  1176. }
  1177. };
  1178. // Attribute implemented for rectangles
  1179. class CRectAttribute : public CNumbersAttribute
  1180. {
  1181. public:
  1182. CRectAttribute(const char* name, core::rect<s32> value) : CNumbersAttribute(name, value) { }
  1183. virtual E_ATTRIBUTE_TYPE getType() const
  1184. {
  1185. return EAT_RECT;
  1186. }
  1187. virtual const wchar_t* getTypeString() const
  1188. {
  1189. return L"rect";
  1190. }
  1191. };
  1192. // Attribute implemented for dimension
  1193. class CDimension2dAttribute : public CNumbersAttribute
  1194. {
  1195. public:
  1196. CDimension2dAttribute (const char* name, core::dimension2d<u32> value) : CNumbersAttribute(name, value) { }
  1197. virtual E_ATTRIBUTE_TYPE getType() const
  1198. {
  1199. return EAT_DIMENSION2D;
  1200. }
  1201. virtual const wchar_t* getTypeString() const
  1202. {
  1203. return L"dimension2d";
  1204. }
  1205. };
  1206. // Attribute implemented for matrices
  1207. class CMatrixAttribute : public CNumbersAttribute
  1208. {
  1209. public:
  1210. CMatrixAttribute(const char* name, core::matrix4 value) : CNumbersAttribute(name, value) { }
  1211. virtual E_ATTRIBUTE_TYPE getType() const
  1212. {
  1213. return EAT_MATRIX;
  1214. }
  1215. virtual core::quaternion getQuaternion()
  1216. {
  1217. return core::quaternion(getMatrix());
  1218. }
  1219. virtual const wchar_t* getTypeString() const
  1220. {
  1221. return L"matrix";
  1222. }
  1223. };
  1224. // Attribute implemented for quaternions
  1225. class CQuaternionAttribute : public CNumbersAttribute
  1226. {
  1227. public:
  1228. CQuaternionAttribute(const char* name, core::quaternion value) : CNumbersAttribute(name, value) { }
  1229. virtual E_ATTRIBUTE_TYPE getType() const
  1230. {
  1231. return EAT_QUATERNION;
  1232. }
  1233. virtual core::matrix4 getMatrix()
  1234. {
  1235. return getQuaternion().getMatrix();
  1236. }
  1237. virtual const wchar_t* getTypeString() const
  1238. {
  1239. return L"quaternion";
  1240. }
  1241. };
  1242. // Attribute implemented for bounding boxes
  1243. class CBBoxAttribute : public CNumbersAttribute
  1244. {
  1245. public:
  1246. CBBoxAttribute(const char* name, core::aabbox3df value) : CNumbersAttribute(name, value) { }
  1247. virtual E_ATTRIBUTE_TYPE getType() const
  1248. {
  1249. return EAT_BBOX;
  1250. }
  1251. virtual const wchar_t* getTypeString() const
  1252. {
  1253. return L"box3d";
  1254. }
  1255. };
  1256. // Attribute implemented for planes
  1257. class CPlaneAttribute : public CNumbersAttribute
  1258. {
  1259. public:
  1260. CPlaneAttribute(const char* name, core::plane3df value) : CNumbersAttribute(name, value) { }
  1261. virtual E_ATTRIBUTE_TYPE getType() const
  1262. {
  1263. return EAT_PLANE;
  1264. }
  1265. virtual const wchar_t* getTypeString() const
  1266. {
  1267. return L"plane";
  1268. }
  1269. };
  1270. // Attribute implemented for triangles
  1271. class CTriangleAttribute : public CNumbersAttribute
  1272. {
  1273. public:
  1274. CTriangleAttribute(const char* name, core::triangle3df value) : CNumbersAttribute(name, value) { }
  1275. virtual E_ATTRIBUTE_TYPE getType() const
  1276. {
  1277. return EAT_TRIANGLE3D;
  1278. }
  1279. virtual core::plane3df getPlane()
  1280. {
  1281. return getTriangle().getPlane();
  1282. }
  1283. virtual const wchar_t* getTypeString() const
  1284. {
  1285. return L"triangle";
  1286. }
  1287. };
  1288. // Attribute implemented for 2d lines
  1289. class CLine2dAttribute : public CNumbersAttribute
  1290. {
  1291. public:
  1292. CLine2dAttribute(const char* name, core::line2df value) : CNumbersAttribute(name, value) { }
  1293. virtual E_ATTRIBUTE_TYPE getType() const
  1294. {
  1295. return EAT_LINE2D;
  1296. }
  1297. virtual const wchar_t* getTypeString() const
  1298. {
  1299. return L"line2d";
  1300. }
  1301. };
  1302. // Attribute implemented for 3d lines
  1303. class CLine3dAttribute : public CNumbersAttribute
  1304. {
  1305. public:
  1306. CLine3dAttribute(const char* name, core::line3df value) : CNumbersAttribute(name, value) { }
  1307. virtual E_ATTRIBUTE_TYPE getType() const
  1308. {
  1309. return EAT_LINE3D;
  1310. }
  1311. virtual const wchar_t* getTypeString() const
  1312. {
  1313. return L"line3d";
  1314. }
  1315. };
  1316. // vector2df
  1317. // dimension2du
  1318. /*
  1319. Special attributes
  1320. */
  1321. // Attribute implemented for enumeration literals
  1322. class CEnumAttribute : public IAttribute
  1323. {
  1324. public:
  1325. CEnumAttribute(const char* name, const char* value, const char* const* literals)
  1326. {
  1327. Name = name;
  1328. setEnum(value, literals);
  1329. }
  1330. virtual void setEnum(const char* enumValue, const char* const* enumerationLiterals)
  1331. {
  1332. int literalCount = 0;
  1333. if (enumerationLiterals)
  1334. {
  1335. s32 i;
  1336. for (i=0; enumerationLiterals[i]; ++i)
  1337. ++literalCount;
  1338. EnumLiterals.reallocate(literalCount);
  1339. for (i=0; enumerationLiterals[i]; ++i)
  1340. EnumLiterals.push_back(enumerationLiterals[i]);
  1341. }
  1342. setString(enumValue);
  1343. }
  1344. virtual s32 getInt()
  1345. {
  1346. for (s32 i=0; EnumLiterals.size(); ++i)
  1347. if (Value.equals_ignore_case(EnumLiterals[i]))
  1348. {
  1349. return i;
  1350. }
  1351. return -1;
  1352. }
  1353. virtual f32 getFloat()
  1354. {
  1355. return (f32)getInt();
  1356. }
  1357. virtual bool getBool()
  1358. {
  1359. return (getInt() != 0); // does not make a lot of sense, I know
  1360. }
  1361. virtual core::stringc getString()
  1362. {
  1363. return Value;
  1364. }
  1365. virtual core::stringw getStringW()
  1366. {
  1367. return core::stringw(Value.c_str());
  1368. }
  1369. virtual void setInt(s32 intValue)
  1370. {
  1371. if (intValue>=0 && intValue<(s32)EnumLiterals.size())
  1372. Value = EnumLiterals[intValue];
  1373. else
  1374. Value = "";
  1375. }
  1376. virtual void setFloat(f32 floatValue)
  1377. {
  1378. setInt((s32)floatValue);
  1379. };
  1380. virtual void setString(const char* text)
  1381. {
  1382. Value = text;
  1383. }
  1384. virtual const char* getEnum()
  1385. {
  1386. return Value.c_str();
  1387. }
  1388. virtual E_ATTRIBUTE_TYPE getType() const
  1389. {
  1390. return EAT_ENUM;
  1391. }
  1392. virtual const wchar_t* getTypeString() const
  1393. {
  1394. return L"enum";
  1395. }
  1396. core::stringc Value;
  1397. core::array<core::stringc> EnumLiterals;
  1398. };
  1399. // Attribute implemented for strings
  1400. class CStringAttribute : public IAttribute
  1401. {
  1402. public:
  1403. CStringAttribute(const char* name, const char* value)
  1404. {
  1405. IsStringW=false;
  1406. Name = name;
  1407. setString(value);
  1408. }
  1409. CStringAttribute(const char* name, const wchar_t* value)
  1410. {
  1411. IsStringW = true;
  1412. Name = name;
  1413. setString(value);
  1414. }
  1415. CStringAttribute(const char* name, void* binaryData, s32 lenghtInBytes)
  1416. {
  1417. IsStringW=false;
  1418. Name = name;
  1419. setBinary(binaryData, lenghtInBytes);
  1420. }
  1421. virtual s32 getInt()
  1422. {
  1423. if (IsStringW)
  1424. return atoi(core::stringc(ValueW.c_str()).c_str());
  1425. else
  1426. return atoi(Value.c_str());
  1427. }
  1428. virtual f32 getFloat()
  1429. {
  1430. if (IsStringW)
  1431. return core::fast_atof(core::stringc(ValueW.c_str()).c_str());
  1432. else
  1433. return core::fast_atof(Value.c_str());
  1434. }
  1435. virtual bool getBool()
  1436. {
  1437. if (IsStringW)
  1438. return ValueW.equals_ignore_case(L"true");
  1439. else
  1440. return Value.equals_ignore_case("true");
  1441. }
  1442. virtual core::stringc getString()
  1443. {
  1444. if (IsStringW)
  1445. return core::stringc(ValueW.c_str());
  1446. else
  1447. return Value;
  1448. }
  1449. virtual core::stringw getStringW()
  1450. {
  1451. if (IsStringW)
  1452. return ValueW;
  1453. else
  1454. return core::stringw(Value.c_str());
  1455. }
  1456. virtual void setInt(s32 intValue)
  1457. {
  1458. if (IsStringW)
  1459. ValueW = core::stringw(intValue);
  1460. else
  1461. Value = core::stringc(intValue);
  1462. }
  1463. virtual void setFloat(f32 floatValue)
  1464. {
  1465. if (IsStringW)
  1466. {
  1467. ValueW = core::stringw((double)floatValue);
  1468. }
  1469. else
  1470. {
  1471. Value = core::stringc((double)floatValue);
  1472. }
  1473. };
  1474. virtual void setString(const char* text)
  1475. {
  1476. if (IsStringW)
  1477. ValueW = core::stringw(text);
  1478. else
  1479. Value = text;
  1480. }
  1481. virtual void setString(const wchar_t* text)
  1482. {
  1483. if (IsStringW)
  1484. ValueW = text;
  1485. else
  1486. Value = core::stringc(text);
  1487. }
  1488. virtual E_ATTRIBUTE_TYPE getType() const
  1489. {
  1490. return EAT_STRING;
  1491. }
  1492. virtual const wchar_t* getTypeString() const
  1493. {
  1494. return L"string";
  1495. }
  1496. virtual void getBinary(void* outdata, s32 maxLength)
  1497. {
  1498. s32 dataSize = maxLength;
  1499. c8* datac8 = (c8*)(outdata);
  1500. s32 p = 0;
  1501. const c8* dataString = Value.c_str();
  1502. for (s32 i=0; i<dataSize; ++i)
  1503. datac8[i] = 0;
  1504. while(dataString[p] && p<dataSize)
  1505. {
  1506. s32 v = getByteFromHex((c8)dataString[p*2]) * 16;
  1507. if (dataString[(p*2)+1])
  1508. v += getByteFromHex((c8)dataString[(p*2)+1]);
  1509. datac8[p] = v;
  1510. ++p;
  1511. }
  1512. };
  1513. virtual void setBinary(void* data, s32 maxLength)
  1514. {
  1515. s32 dataSize = maxLength;
  1516. c8* datac8 = (c8*)(data);
  1517. char tmp[3];
  1518. tmp[2] = 0;
  1519. Value = "";
  1520. for (s32 b=0; b<dataSize; ++b)
  1521. {
  1522. getHexStrFromByte(datac8[b], tmp);
  1523. Value.append(tmp);
  1524. }
  1525. };
  1526. bool IsStringW;
  1527. core::stringc Value;
  1528. core::stringw ValueW;
  1529. protected:
  1530. static inline s32 getByteFromHex(c8 h)
  1531. {
  1532. if (h >= '0' && h <='9')
  1533. return h-'0';
  1534. if (h >= 'a' && h <='f')
  1535. return h-'a' + 10;
  1536. return 0;
  1537. }
  1538. static inline void getHexStrFromByte(c8 byte, c8* out)
  1539. {
  1540. s32 b = (byte & 0xf0) >> 4;
  1541. for (s32 i=0; i<2; ++i)
  1542. {
  1543. if (b >=0 && b <= 9)
  1544. out[i] = b+'0';
  1545. if (b >=10 && b <= 15)
  1546. out[i] = (b-10)+'a';
  1547. b = byte & 0x0f;
  1548. }
  1549. }
  1550. };
  1551. // Attribute implemented for binary data
  1552. class CBinaryAttribute : public CStringAttribute
  1553. {
  1554. public:
  1555. CBinaryAttribute(const char* name, void* binaryData, s32 lenghtInBytes)
  1556. : CStringAttribute(name, binaryData, lenghtInBytes)
  1557. {
  1558. }
  1559. virtual E_ATTRIBUTE_TYPE getType() const
  1560. {
  1561. return EAT_BINARY;
  1562. }
  1563. virtual const wchar_t* getTypeString() const
  1564. {
  1565. return L"binary";
  1566. }
  1567. };
  1568. // Attribute implemented for texture references
  1569. class CTextureAttribute : public IAttribute
  1570. {
  1571. // Explicitly hide base method
  1572. using IAttribute::setTexture;
  1573. public:
  1574. CTextureAttribute(const char* name, video::ITexture* value, video::IVideoDriver* driver, const io::path& filename)
  1575. : Value(0), Driver(driver), OverrideName(filename)
  1576. {
  1577. if (Driver)
  1578. Driver->grab();
  1579. Name = name;
  1580. setTexture(value);
  1581. }
  1582. ~CTextureAttribute()
  1583. {
  1584. if (Driver)
  1585. Driver->drop();
  1586. if (Value)
  1587. Value->drop();
  1588. }
  1589. virtual video::ITexture* getTexture()
  1590. {
  1591. return Value;
  1592. }
  1593. virtual bool getBool()
  1594. {
  1595. return (Value != 0);
  1596. }
  1597. virtual core::stringw getStringW()
  1598. {
  1599. return core::stringw(OverrideName.size()?OverrideName:
  1600. Value ? Value->getName().getPath().c_str() : 0);
  1601. }
  1602. virtual core::stringc getString()
  1603. {
  1604. // since texture names can be stringw we are careful with the types
  1605. return core::stringc(OverrideName.size()?OverrideName:
  1606. Value ? Value->getName().getPath().c_str() : 0);
  1607. }
  1608. virtual void setString(const char* text)
  1609. {
  1610. if (Driver)
  1611. {
  1612. if (text && *text)
  1613. {
  1614. setTexture(Driver->getTexture(text));
  1615. OverrideName=text;
  1616. }
  1617. else
  1618. setTexture(0);
  1619. }
  1620. }
  1621. virtual void setTexture(video::ITexture* value)
  1622. {
  1623. if ( value == Value )
  1624. return;
  1625. if (Value)
  1626. Value->drop();
  1627. Value = value;
  1628. if (Value)
  1629. Value->grab();
  1630. }
  1631. virtual E_ATTRIBUTE_TYPE getType() const
  1632. {
  1633. return EAT_TEXTURE;
  1634. }
  1635. virtual const wchar_t* getTypeString() const
  1636. {
  1637. return L"texture";
  1638. }
  1639. video::ITexture* Value;
  1640. video::IVideoDriver* Driver;
  1641. io::path OverrideName;
  1642. };
  1643. // Attribute implemented for array of stringw
  1644. class CStringWArrayAttribute : public IAttribute
  1645. {
  1646. public:
  1647. CStringWArrayAttribute(const char* name, const core::array<core::stringw>& value)
  1648. {
  1649. Name = name;
  1650. setArray(value);
  1651. }
  1652. virtual core::array<core::stringw> getArray()
  1653. {
  1654. return Value;
  1655. }
  1656. virtual void setArray(const core::array<core::stringw>& value)
  1657. {
  1658. Value = value;
  1659. }
  1660. virtual E_ATTRIBUTE_TYPE getType() const
  1661. {
  1662. return EAT_STRINGWARRAY;
  1663. }
  1664. virtual const wchar_t* getTypeString() const
  1665. {
  1666. return L"stringwarray";
  1667. }
  1668. core::array<core::stringw> Value;
  1669. };
  1670. // Attribute implemented for user pointers
  1671. class CUserPointerAttribute : public IAttribute
  1672. {
  1673. public:
  1674. CUserPointerAttribute(const char* name, void* value)
  1675. {
  1676. Name = name;
  1677. Value = value;
  1678. }
  1679. virtual s32 getInt()
  1680. {
  1681. return *static_cast<s32*>(Value);
  1682. }
  1683. virtual bool getBool()
  1684. {
  1685. return (Value != 0);
  1686. }
  1687. virtual core::stringw getStringW()
  1688. {
  1689. wchar_t buf[32];
  1690. swprintf(buf, 32, L"%p", Value);
  1691. return core::stringw(buf);
  1692. }
  1693. virtual void setString(const char* text)
  1694. {
  1695. u32 tmp;
  1696. sscanf(text, "0x%x", &tmp);
  1697. Value = reinterpret_cast<void *>(tmp);
  1698. }
  1699. virtual E_ATTRIBUTE_TYPE getType() const
  1700. {
  1701. return EAT_USER_POINTER;
  1702. }
  1703. virtual void setUserPointer(void* v)
  1704. {
  1705. Value = v;
  1706. }
  1707. virtual void* getUserPointer()
  1708. {
  1709. return Value;
  1710. }
  1711. virtual const wchar_t* getTypeString() const
  1712. {
  1713. return L"userPointer";
  1714. }
  1715. void* Value;
  1716. };
  1717. // todo: CGUIFontAttribute
  1718. } // end namespace io
  1719. } // end namespace irr