sregion.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <precomp.h>
  2. #include "sregion.h"
  3. #include <api/script/script.h>
  4. #include <api/script/scriptmgr.h>
  5. RegionScriptController _regionController;
  6. RegionScriptController *regionController=&_regionController;
  7. // -- Functions table -------------------------------------
  8. function_descriptor_struct RegionScriptController::exportedFunction[] = {
  9. {L"add", 1, (void*)SRegion::script_vcpu_add },
  10. {L"sub", 1, (void*)SRegion::script_vcpu_sub },
  11. {L"offset", 2, (void*)SRegion::script_vcpu_offset },
  12. {L"stretch", 1, (void*)SRegion::script_vcpu_stretch },
  13. {L"copy", 1, (void*)SRegion::script_vcpu_copy },
  14. {L"loadFromMap", 3, (void*)SRegion::script_vcpu_loadFromMap },
  15. {L"loadFromBitmap", 1, (void*)SRegion::script_vcpu_loadFromBitmap },
  16. {L"getBoundingBoxX", 0, (void*)SRegion::script_vcpu_getBoundX },
  17. {L"getBoundingBoxY", 0, (void*)SRegion::script_vcpu_getBoundY },
  18. {L"getBoundingBoxW", 0, (void*)SRegion::script_vcpu_getBoundW },
  19. {L"getBoundingBoxH", 0, (void*)SRegion::script_vcpu_getBoundH },
  20. };
  21. // --------------------------------------------------------
  22. const wchar_t *RegionScriptController::getClassName() {
  23. return L"Region";
  24. }
  25. const wchar_t *RegionScriptController::getAncestorClassName() {
  26. return L"Object";
  27. }
  28. ScriptObjectController *RegionScriptController::getAncestorController() { return rootScriptObjectController; }
  29. ScriptObject *RegionScriptController::instantiate() {
  30. SRegion *r = new SRegion;
  31. ASSERT(r != NULL);
  32. return r->getScriptObject();
  33. }
  34. void RegionScriptController::destroy(ScriptObject *o) {
  35. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  36. ASSERT(r != NULL);
  37. delete r;
  38. }
  39. void *RegionScriptController::encapsulate(ScriptObject *o) {
  40. return NULL; // no encapsulation for regions yet
  41. }
  42. void RegionScriptController::deencapsulate(void *o) {
  43. }
  44. int RegionScriptController::getNumFunctions() {
  45. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  46. }
  47. const function_descriptor_struct *RegionScriptController::getExportedFunctions() {
  48. return exportedFunction;
  49. }
  50. GUID RegionScriptController::getClassGuid() {
  51. return regionGuid;
  52. }
  53. SRegion::SRegion() {
  54. getScriptObject()->vcpu_setInterface(regionGuid, (void *)static_cast<SRegion*>(this));
  55. getScriptObject()->vcpu_setClassName(L"Region");
  56. getScriptObject()->vcpu_setController(regionController);
  57. reg = new RegionI;
  58. }
  59. SRegion::~SRegion() {
  60. delete reg;
  61. }
  62. int SRegion::inRegion(int x, int y) {
  63. if (!reg) return 0;
  64. POINT pt={x,y};
  65. return reg->ptInRegion(&pt);
  66. }
  67. int SRegion::getBoundX() {
  68. if (!reg) return 0;
  69. RECT r;
  70. reg->getBox(&r);
  71. return r.left;
  72. }
  73. int SRegion::getBoundY() {
  74. if (!reg) return 0;
  75. RECT r;
  76. reg->getBox(&r);
  77. return r.top;
  78. }
  79. int SRegion::getBoundW() {
  80. if (!reg) return 0;
  81. RECT r;
  82. reg->getBox(&r);
  83. return r.right-r.left;
  84. }
  85. int SRegion::getBoundH() {
  86. if (!reg) return 0;
  87. RECT r;
  88. reg->getBox(&r);
  89. return r.bottom-r.top;
  90. }
  91. api_region *SRegion::getRegion() {
  92. return reg;
  93. }
  94. void SRegion::addRegion(SRegion *s) {
  95. if (!reg) reg = new RegionI;
  96. reg->addRegion(s->getRegion());
  97. }
  98. void SRegion::subRegion(SRegion *s) {
  99. if (!reg) return;
  100. reg->subtractRgn(s->getRegion());
  101. }
  102. void SRegion::offset(int x, int y) {
  103. if (!reg) return;
  104. reg->offset(x, y);
  105. }
  106. void SRegion::stretch(double s) {
  107. if (!reg) return;
  108. reg->scale(s, s);
  109. }
  110. void SRegion::copy(SRegion *s) {
  111. if (!reg) reg = new RegionI;
  112. else reg->empty();
  113. reg->addRegion(s->getRegion());
  114. }
  115. void SRegion::loadFromMap(SMap *m, int byte, int inverted) {
  116. delete reg;
  117. RECT r={m->getBitmap()->getX(), m->getBitmap()->getY(), m->getBitmap()->getWidth(), m->getBitmap()->getHeight()};
  118. reg = new RegionI(m->getBitmap(), &r, 0, 0, FALSE, 1, byte, inverted);
  119. }
  120. void SRegion::loadFromBitmap(const wchar_t *p)
  121. {
  122. delete reg;
  123. SkinBitmap *b = new SkinBitmap(p);
  124. ASSERT(b); // TODO: should be guru
  125. reg = new RegionI(b);
  126. delete b;
  127. }
  128. // -----------------------------------------------------------------------
  129. scriptVar SRegion::script_vcpu_loadFromMap(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar map, scriptVar byte, scriptVar inv) {
  130. SCRIPT_FUNCTION_INIT;
  131. ASSERT(SOM::isNumeric(&byte));
  132. ASSERT(SOM::isNumeric(&inv));
  133. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  134. SMap *m = static_cast<SMap *>(GET_SCRIPT_OBJECT_AS(map, mapGuid));
  135. if (r) r->loadFromMap(m, GET_SCRIPT_INT(byte), GET_SCRIPT_BOOLEAN(inv));
  136. RETURN_SCRIPT_VOID;
  137. }
  138. scriptVar SRegion::script_vcpu_loadFromBitmap(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar b)
  139. {
  140. SCRIPT_FUNCTION_INIT;
  141. ASSERT(b.type == SCRIPT_STRING);
  142. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  143. if (r) r->loadFromBitmap(GET_SCRIPT_STRING(b));
  144. RETURN_SCRIPT_VOID;
  145. }
  146. scriptVar SRegion::script_vcpu_inRegion(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar x, scriptVar y) {
  147. SCRIPT_FUNCTION_INIT;
  148. ASSERT(SOM::isNumeric(&x));
  149. ASSERT(SOM::isNumeric(&y));
  150. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  151. if (r) return MAKE_SCRIPT_INT(r->inRegion(GET_SCRIPT_INT(x), GET_SCRIPT_INT(y)));
  152. RETURN_SCRIPT_ZERO;
  153. }
  154. scriptVar SRegion::script_vcpu_add(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar r) {
  155. SCRIPT_FUNCTION_INIT;
  156. SRegion *r1 = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  157. SRegion *r2 = static_cast<SRegion *>(GET_SCRIPT_OBJECT_AS(r, regionGuid));
  158. if (r1) r1->addRegion(r2);
  159. RETURN_SCRIPT_VOID;
  160. }
  161. scriptVar SRegion::script_vcpu_sub(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar r) {
  162. SCRIPT_FUNCTION_INIT;
  163. SRegion *r1 = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  164. SRegion *r2 = static_cast<SRegion *>(GET_SCRIPT_OBJECT_AS(r, regionGuid));
  165. if (r1) r1->subRegion(r2);
  166. RETURN_SCRIPT_VOID;
  167. }
  168. scriptVar SRegion::script_vcpu_offset(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar x, scriptVar y) {
  169. SCRIPT_FUNCTION_INIT;
  170. ASSERT(SOM::isNumeric(&x));
  171. ASSERT(SOM::isNumeric(&y));
  172. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  173. if (r) r->offset(GET_SCRIPT_INT(x), GET_SCRIPT_INT(y));
  174. RETURN_SCRIPT_VOID;
  175. }
  176. scriptVar SRegion::script_vcpu_stretch(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar s) {
  177. SCRIPT_FUNCTION_INIT;
  178. ASSERT(SOM::isNumeric(&s));
  179. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  180. if (r) r->stretch(SOM::makeDouble(&s));
  181. RETURN_SCRIPT_VOID;
  182. }
  183. scriptVar SRegion::script_vcpu_getBoundX(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  184. SCRIPT_FUNCTION_INIT;
  185. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  186. if (r) return MAKE_SCRIPT_INT(r->getBoundX());
  187. RETURN_SCRIPT_ZERO;
  188. }
  189. scriptVar SRegion::script_vcpu_getBoundY(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  190. SCRIPT_FUNCTION_INIT;
  191. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  192. if (r) return MAKE_SCRIPT_INT(r->getBoundY());
  193. RETURN_SCRIPT_ZERO;
  194. }
  195. scriptVar SRegion::script_vcpu_getBoundW(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  196. SCRIPT_FUNCTION_INIT;
  197. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  198. if (r) return MAKE_SCRIPT_INT(r->getBoundW());
  199. RETURN_SCRIPT_ZERO;
  200. }
  201. scriptVar SRegion::script_vcpu_getBoundH(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  202. SCRIPT_FUNCTION_INIT;
  203. SRegion *r = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  204. if (r) return MAKE_SCRIPT_INT(r->getBoundH());
  205. RETURN_SCRIPT_ZERO;
  206. }
  207. scriptVar SRegion::script_vcpu_copy(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar r) {
  208. SCRIPT_FUNCTION_INIT;
  209. SRegion *r1 = static_cast<SRegion *>(o->vcpu_getInterface(regionGuid));
  210. SRegion *r2 = static_cast<SRegion *>(GET_SCRIPT_OBJECT_AS(r, regionGuid));
  211. if (r1) r1->copy(r2);
  212. RETURN_SCRIPT_VOID;
  213. }