1
0

tgbutton.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <precomp.h>
  2. #include "tgbutton.h"
  3. #include <api/script/scriptmgr.h>
  4. #include <bfc/parse/paramparser.h>
  5. const wchar_t toggleButtonXuiObjectStr[] = L"ToggleButton"; // This is the xml tag
  6. char toggleButtonXuiSvcName[] = "ToggleButton xui object"; // this is the name of the xuiservice
  7. XMLParamPair ToggleButton::params[] = {
  8. {TOGGLEBUTTON_AUTOTOGGLE, L"AUTOTOGGLE"},
  9. #ifdef WASABI_COMPILE_CONFIG
  10. {TOGGLEBUTTON_CFGVAL, L"CFGVAL"},
  11. #endif
  12. };
  13. ToggleButton::ToggleButton() {
  14. param=0;
  15. getScriptObject()->vcpu_setInterface(toggleButtonGuid, (void *)static_cast<ToggleButton *>(this));
  16. getScriptObject()->vcpu_setClassName(L"ToggleButton");
  17. getScriptObject()->vcpu_setController(tgbuttonController);
  18. autotoggle = 1;
  19. #ifdef WASABI_COMPILE_CONFIG
  20. cfgVal = 1;
  21. #endif
  22. xuihandle = newXuiHandle();
  23. CreateXMLParameters(xuihandle);
  24. }
  25. void ToggleButton::CreateXMLParameters(int master_handle)
  26. {
  27. //TOGGLEBUTTON_PARENT::CreateXMLParameters(master_handle);
  28. int numParams = sizeof(params) / sizeof(params[0]);
  29. hintNumberOfParams(xuihandle, numParams);
  30. for (int i = 0;i < numParams;i++)
  31. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  32. }
  33. ToggleButton::~ToggleButton() {
  34. }
  35. void ToggleButton::onLeftPush(int x, int y) {
  36. autoToggle();
  37. TOGGLEBUTTON_PARENT::onLeftPush(x, y);
  38. onToggle(getActivatedButton());
  39. }
  40. void ToggleButton::autoToggle() {
  41. if (autotoggle) {
  42. if (!getActivatedButton())
  43. setActivatedButton(1);
  44. else
  45. setActivatedButton(0);
  46. }
  47. }
  48. void ToggleButton::onToggle(int i) {
  49. scriptVar _y = SOM::makeVar(SCRIPT_INT);
  50. SOM::assign(&_y, i ? 1 : 0);
  51. script_onToggle(SCRIPT_CALL, getScriptObject(), _y);
  52. #ifdef WASABI_COMPILE_CONFIG
  53. getGuiObject()->guiobject_setCfgInt(i ? cfgVal : 0);
  54. #endif
  55. }
  56. int ToggleButton::setXuiParam(int _xuihandle, int xmlattributeid, const wchar_t *name, const wchar_t *value) {
  57. if (xuihandle == _xuihandle) {
  58. switch (xmlattributeid) {
  59. case TOGGLEBUTTON_AUTOTOGGLE: autotoggle = WTOI(value); return 1;
  60. #ifdef WASABI_COMPILE_CONFIG
  61. case TOGGLEBUTTON_CFGVAL:
  62. cfgVal = WTOI(value); return 1;
  63. #endif
  64. }
  65. }
  66. return TOGGLEBUTTON_PARENT::setXuiParam(_xuihandle, xmlattributeid, name, value);
  67. }
  68. #ifdef WASABI_COMPILE_CONFIG
  69. int ToggleButton::onReloadConfig() {
  70. TOGGLEBUTTON_PARENT::onReloadConfig();
  71. setActivatedButton(getGuiObject()->guiobject_getCfgInt());
  72. return 1;
  73. }
  74. #endif
  75. int ToggleButton::getCurCfgVal() {
  76. return cfgVal;
  77. }
  78. TgButtonScriptController _tgbuttonController;
  79. TgButtonScriptController *tgbuttonController=&_tgbuttonController;
  80. // -- Functions table -------------------------------------
  81. function_descriptor_struct TgButtonScriptController::exportedFunction[] = {
  82. {L"onToggle", 1, (void*)ToggleButton::script_onToggle },
  83. {L"getCurCfgVal", 0, (void*)ToggleButton::script_getCurCfgVal},
  84. };
  85. // --------------------------------------------------------
  86. const wchar_t *TgButtonScriptController::getClassName() {
  87. return L"ToggleButton";
  88. }
  89. const wchar_t *TgButtonScriptController::getAncestorClassName() {
  90. return L"Button";
  91. }
  92. ScriptObject *TgButtonScriptController::instantiate() {
  93. ToggleButton *tb = new ToggleButton;
  94. ASSERT(tb != NULL);
  95. return tb->getScriptObject();
  96. }
  97. void TgButtonScriptController::destroy(ScriptObject *o) {
  98. ToggleButton *tb = static_cast<ToggleButton *>(o->vcpu_getInterface(toggleButtonGuid));
  99. ASSERT(tb != NULL);
  100. delete tb;
  101. }
  102. void *TgButtonScriptController::encapsulate(ScriptObject *o) {
  103. return NULL;
  104. }
  105. void TgButtonScriptController::deencapsulate(void *o) {
  106. }
  107. int TgButtonScriptController::getNumFunctions() {
  108. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  109. }
  110. const function_descriptor_struct *TgButtonScriptController::getExportedFunctions() {
  111. return exportedFunction;
  112. }
  113. GUID TgButtonScriptController::getClassGuid() {
  114. return toggleButtonGuid;
  115. }
  116. const wchar_t *ToggleButton::vcpu_getClassName() {
  117. return L"ToggleButton";
  118. }
  119. scriptVar ToggleButton::script_onToggle(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar is) {
  120. SCRIPT_FUNCTION_INIT;
  121. PROCESS_HOOKS1(o, tgbuttonController, is);
  122. SCRIPT_FUNCTION_CHECKABORTEVENT;
  123. SCRIPT_EXEC_EVENT1(o, is);
  124. }
  125. scriptVar ToggleButton::script_getCurCfgVal(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  126. SCRIPT_FUNCTION_INIT;
  127. ToggleButton *tg = static_cast<ToggleButton*>(o->vcpu_getInterface(toggleButtonGuid));
  128. return MAKE_SCRIPT_INT(tg->getCurCfgVal());
  129. }
  130. //---
  131. const wchar_t nStatesTgButtonXuiObjectStr[] = L"NStatesButton"; // This is the xml tag
  132. char nStatesTgButtonXuiSvcName[] = "NStatesButton xui object"; // this is the name of the xuiservice
  133. XMLParamPair NStatesTgButton::params[] = {
  134. {NSTATESTGBUTTON_NSTATES, L"NSTATES"},
  135. {NSTATESTGBUTTON_ONEVSTATE, L"AUTOELEMENTS"},
  136. #ifdef WASABI_COMPILE_CONFIG
  137. {NSTATESTGBUTTON_CFGVALS, L"CFGVALS"},
  138. #endif
  139. };
  140. NStatesTgButton::NStatesTgButton() {
  141. getScriptObject()->vcpu_setInterface(NStatesTgButtonGuid, (void *)static_cast<NStatesTgButton*>(this));
  142. xuihandle = newXuiHandle();
  143. CreateXMLParameters(xuihandle);
  144. onevstate = 0;
  145. }
  146. void NStatesTgButton::CreateXMLParameters(int master_handle)
  147. {
  148. //NSTATESTGBUTTON_PARENT::CreateXMLParameters(master_handle);
  149. int numParams = sizeof(params) / sizeof(params[0]);
  150. hintNumberOfParams(xuihandle, numParams);
  151. for (int i = 0;i < numParams;i++)
  152. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  153. }
  154. NStatesTgButton::~NStatesTgButton() {
  155. }
  156. int NStatesTgButton::setXuiParam(int _xuihandle, int xmlattributeid, const wchar_t *paramname, const wchar_t *strvalue) {
  157. if (_xuihandle == btn_getXuiHandle()) {
  158. switch (xmlattributeid) {
  159. case BUTTON_IMAGE: image = strvalue; break;
  160. case BUTTON_HOVERIMAGE: hover = strvalue; break;
  161. case BUTTON_DOWNIMAGE: down = strvalue; break;
  162. case BUTTON_ACTIVEIMAGE: active = strvalue; break;
  163. }
  164. }
  165. if (xuihandle == _xuihandle) {
  166. switch (xmlattributeid) {
  167. case NSTATESTGBUTTON_NSTATES:
  168. setNStates(WTOI(strvalue));
  169. return 1;
  170. case NSTATESTGBUTTON_ONEVSTATE:
  171. setOneVisualState(!WTOI(strvalue));
  172. return 1;
  173. #ifdef WASABI_COMPILE_CONFIG
  174. case NSTATESTGBUTTON_CFGVALS:
  175. cfgvals = strvalue;
  176. return 1;
  177. #endif
  178. }
  179. }
  180. return NSTATESTGBUTTON_PARENT::setXuiParam(_xuihandle, xmlattributeid, paramname, strvalue);
  181. }
  182. int NStatesTgButton::onInit() {
  183. setState(0);
  184. return NSTATESTGBUTTON_PARENT::onInit();
  185. }
  186. void NStatesTgButton::setOneVisualState(int v) {
  187. if (!!onevstate == !!v) return;
  188. onevstate = v;
  189. if (isPostOnInit()) {
  190. setupBitmaps();
  191. invalidate();
  192. }
  193. }
  194. void NStatesTgButton::setState(int n) {
  195. if (nstates <= 1) return;
  196. state = n;
  197. setupBitmaps();
  198. #ifdef WASABI_COMPILE_CONFIG
  199. if (!cfgvals.isempty()) {
  200. ParamParser pp(cfgvals);
  201. const wchar_t *p = pp.enumItem(state);
  202. if (p != NULL)
  203. setXmlParam(L"cfgval", p);
  204. } else {
  205. // if the skinner doesn't ask for custom config values,
  206. // simply use the current state number as the cfgval.
  207. setXmlParam(L"cfgval", StringPrintfW(L"%d", state));
  208. }
  209. #endif
  210. }
  211. int NStatesTgButton::getActivatedButton() {
  212. if (nstates <= 1) return NSTATESTGBUTTON_PARENT::getActivatedButton();
  213. return (getState() != 0);
  214. }
  215. void NStatesTgButton::autoToggle() {
  216. if (nstates <= 1) {
  217. NSTATESTGBUTTON_PARENT::autoToggle();
  218. return;
  219. } else {
  220. int s = (state+1) % nstates;
  221. setState(s);
  222. }
  223. }
  224. void NStatesTgButton::setupBitmaps() {
  225. if (nstates <= 1 || onevstate)
  226. setBitmaps(image, down, hover, active);
  227. else
  228. setBitmaps(StringPrintfW(L"%s%d", image.v(), state),
  229. StringPrintfW(L"%s%d", down.v(), state),
  230. StringPrintfW(L"%s%d", hover.v(), state) /*, StringPrintf("%s%d", image.v(), (state+1) % nstates)*/);
  231. }
  232. void NStatesTgButton::setActivatedButton(int a) {
  233. if (nstates <= 1) {
  234. NSTATESTGBUTTON_PARENT::setActivatedButton(a);
  235. return;
  236. }
  237. #ifdef WASABI_COMPILE_CONFIG
  238. if (!cfgvals.isempty()) {
  239. ParamParser pp(cfgvals);
  240. wchar_t t[64] = {0};
  241. wcsncpy(t, StringPrintfW(L"%d", a), 64);
  242. for (int i=0;i<pp.getNumItems();i++) {
  243. const wchar_t *p = pp.enumItem(i);
  244. if (WCSCASEEQLSAFE(p, t)) {
  245. setState(i);
  246. return;
  247. }
  248. }
  249. } else {
  250. if (!a)
  251. setState(0);
  252. else
  253. setState(a);
  254. }
  255. #endif
  256. }
  257. int NStatesTgButton::getCurCfgVal() {
  258. #ifdef WASABI_COMPILE_CONFIG
  259. if (!cfgvals.isempty()) {
  260. ParamParser pp(cfgvals);
  261. const wchar_t *p = pp.enumItem(state);
  262. if (p) return WTOI(p);
  263. return 0;
  264. } else {
  265. // if the skinner doesn't ask for custom config values,
  266. // simply use the current state number as the cfgval.
  267. return state;
  268. }
  269. #else
  270. return ToggleButton::getCurCfgVal();
  271. #endif
  272. }