lvector2_ext_src.I 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file lvector2_ext_src.I
  10. * @author rdb
  11. * @date 2011-01-02
  12. */
  13. /**
  14. *
  15. */
  16. INLINE_LINMATH std::string Extension<FLOATNAME(LVector2)>::
  17. __repr__() const {
  18. char buf[96] = "LVector2";
  19. char *p = buf + strlen(buf);
  20. *(p++) = FLOATTOKEN;
  21. *(p++) = '(';
  22. FLOATTYPE_REPR(_this->_v(0), p);
  23. p += strlen(p);
  24. *(p++) = ',';
  25. *(p++) = ' ';
  26. FLOATTYPE_REPR(_this->_v(1), p);
  27. p += strlen(p);
  28. *(p++) = ')';
  29. *p = '\0';
  30. return std::string(buf, p - buf);
  31. }
  32. /**
  33. * This is used to implement swizzle masks.
  34. */
  35. INLINE_LINMATH PyObject *Extension<FLOATNAME(LVector2)>::
  36. __getattr__(PyObject *self, const std::string &attr_name) const {
  37. #ifndef CPPPARSER
  38. extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVector2);
  39. extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVector3);
  40. extern struct Dtool_PyTypedObject FLOATNAME(Dtool_LVector4);
  41. #endif
  42. // Validate the attribute name.
  43. for (std::string::const_iterator it = attr_name.begin(); it < attr_name.end(); it++) {
  44. if (*it != 'x' && *it != 'y') {
  45. return Dtool_Raise_AttributeError(self, attr_name.c_str());
  46. }
  47. }
  48. switch (attr_name.size()) {
  49. case 1:
  50. return Dtool_WrapValue(_this->_v(attr_name[0] - 'x'));
  51. case 2: {
  52. FLOATNAME(LVector2) *vec = new FLOATNAME(LVector2);
  53. vec->_v(0) = _this->_v(attr_name[0] - 'x');
  54. vec->_v(1) = _this->_v(attr_name[1] - 'x');
  55. return DTool_CreatePyInstance((void *)vec, FLOATNAME(Dtool_LVector2), true, false);
  56. } case 3: {
  57. FLOATNAME(LVector3) *vec = new FLOATNAME(LVector3);
  58. vec->_v(0) = _this->_v(attr_name[0] - 'x');
  59. vec->_v(1) = _this->_v(attr_name[1] - 'x');
  60. vec->_v(2) = _this->_v(attr_name[2] - 'x');
  61. return DTool_CreatePyInstance((void *)vec, FLOATNAME(Dtool_LVector3), true, false);
  62. } case 4: {
  63. FLOATNAME(LVector4) *vec = new FLOATNAME(LVector4);
  64. vec->_v(0) = _this->_v(attr_name[0] - 'x');
  65. vec->_v(1) = _this->_v(attr_name[1] - 'x');
  66. vec->_v(2) = _this->_v(attr_name[2] - 'x');
  67. vec->_v(3) = _this->_v(attr_name[3] - 'x');
  68. return DTool_CreatePyInstance((void *)vec, FLOATNAME(Dtool_LVector4), true, false);
  69. }
  70. }
  71. return Dtool_Raise_AttributeError(self, attr_name.c_str());
  72. }
  73. /**
  74. * This is used to implement write masks.
  75. */
  76. INLINE_LINMATH int Extension<FLOATNAME(LVector2)>::
  77. __setattr__(PyObject *self, const std::string &attr_name, PyObject *assign) {
  78. // Upcall to LVecBase2.
  79. return invoke_extension<FLOATNAME(LVecBase2)>(_this).__setattr__(self, attr_name, assign);
  80. }
  81. /**
  82. *
  83. */
  84. INLINE_LINMATH FLOATNAME(LVector2) Extension<FLOATNAME(LVector2)>::
  85. __rmul__(FLOATTYPE scalar) const {
  86. return *_this * scalar;
  87. }