1
0

wa2songticker.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include <precomp.h>
  2. #include "wa2songticker.h"
  3. #include <api.h>
  4. #include <tataki/color/skinclr.h>
  5. #include <api/core/api_core.h>
  6. #include <api/application/api_application.h>
  7. #include <wasabicfg.h>
  8. #include "wa2frontend.h"
  9. #include <api/skin/skinelem.h>
  10. #include <api/skin/skinparse.h>
  11. #include <api/config/items/attribs.h>
  12. #include <api/config/api_config.h>
  13. // {9149C445-3C30-4e04-8433-5A518ED0FDDE}
  14. static const GUID uioptions_guid =
  15. { 0x9149c445, 0x3c30, 0x4e04, { 0x84, 0x33, 0x5a, 0x51, 0x8e, 0xd0, 0xfd, 0xde } };
  16. // {280876CF-48C0-40bc-8E86-73CE6BB462E5}
  17. static const GUID options_guid =
  18. { 0x280876cf, 0x48c0, 0x40bc, { 0x8e, 0x86, 0x73, 0xce, 0x6b, 0xb4, 0x62, 0xe5 } };
  19. const wchar_t songtickerXuiObjectStr[] = L"SongTicker"; // This is the xml tag
  20. char songtickerXuiSvcName[] = "Song Ticker XUI object"; // this is the name of the xuiservice
  21. BEGIN_SERVICES(wa2SongTicker_Svcs);
  22. DECLARE_SERVICE(XuiObjectCreator<SongTickerXuiSvc>);
  23. END_SERVICES(wa2SongTicker_Svcs, _wa2SongTicker_Svcs);
  24. #ifdef _X86_
  25. extern "C"
  26. {
  27. int _link_wa2SongTicker_Svcs;
  28. }
  29. #else
  30. extern "C"
  31. {
  32. int __link_wa2SongTicker_Svcs;
  33. }
  34. #endif
  35. extern _float cfg_uioptions_textspeed;
  36. extern _int cfg_uioptions_textincrement;
  37. #define TIMER_SONGTICKER_SCROLL 0x777
  38. //#define SONGTICKER_SCROLL_MS 200 // TODO: make adjustable (api_config, xml param, maki script object)
  39. #define SONGTICKER_INCREMENT_PIXELS (cfg_uioptions_textincrement) // TODO: make adjustable?
  40. #define SONGTICKER_SCROLL_MS ((13.0f*(float)cfg_uioptions_textincrement)/cfg_uioptions_textspeed)
  41. #define SONGTICKER_SCROLL_ONE_PIXEL_MS (13.0f/cfg_uioptions_textspeed)
  42. #define SONGTICKER_SKIP_MS 2000 // TODO: make adjustable
  43. XMLParamPair SongTicker::params[] =
  44. {
  45. {SONGTICKER_TICKER, L"TICKER"},
  46. };
  47. SongTicker::SongTicker()
  48. {
  49. WASABI_API_MEDIACORE->core_addCallback(0, this);
  50. song_title[0]=0;
  51. song_length=-1;
  52. position=0;
  53. tickerMode=TICKER_SCROLL;
  54. ticker_direction=1;
  55. skipTimers=0;
  56. grab_x = 0;
  57. last_tick = Wasabi::Std::getTickCount();
  58. buffer_hw_valid=false;
  59. textW=0;
  60. /* register XML parameters */
  61. xuihandle = newXuiHandle();
  62. CreateXMLParameters(xuihandle);
  63. CfgItem *ci=WASABI_API_CONFIG->config_getCfgItemByGuid(uioptions_guid);
  64. if (ci)
  65. {
  66. viewer_addViewItem(ci->getDependencyPtr());
  67. }
  68. ci=WASABI_API_CONFIG->config_getCfgItemByGuid(options_guid);
  69. if (ci)
  70. {
  71. viewer_addViewItem(ci->getDependencyPtr());
  72. }
  73. }
  74. void SongTicker::CreateXMLParameters(int master_handle)
  75. {
  76. //SONGTICKER_PARENT::CreateXMLParameters(master_handle);
  77. int numParams = sizeof(params) / sizeof(params[0]);
  78. hintNumberOfParams(xuihandle, numParams);
  79. for (int i = 0;i < numParams;i++)
  80. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  81. }
  82. SongTicker::~SongTicker()
  83. {
  84. WASABI_API_MEDIACORE->core_delCallback(0, this);
  85. // TODO: benski> is this the right place for this?
  86. killTimer(TIMER_SONGTICKER_SCROLL);
  87. }
  88. int SongTicker::onResize()
  89. {
  90. killTimer(TIMER_SONGTICKER_SCROLL);
  91. buffer_hw_valid=false;
  92. invalidateBuffer();
  93. return SONGTICKER_PARENT::onResize();
  94. }
  95. int SongTicker::onInit()
  96. {
  97. int r = SONGTICKER_PARENT::onInit();
  98. BuildTitle();
  99. return r;
  100. }
  101. int SongTicker::corecb_onTitleChange(const wchar_t *title)
  102. {
  103. BuildTitle();
  104. return 1;
  105. }
  106. int SongTicker::corecb_onLengthChange(int newlength)
  107. {
  108. song_length = newlength;
  109. return 1;
  110. }
  111. void SongTicker::BuildTitle()
  112. {
  113. killTimer(TIMER_SONGTICKER_SCROLL);
  114. const wchar_t *curFilename = wa2.GetCurrentFile();
  115. if (curFilename && *curFilename)
  116. {
  117. wa2.GetFileInfo(curFilename, song_title, 1024, &song_length);
  118. WASABI_API_MEDIACORE->core_setTitle(song_title);
  119. }
  120. else
  121. {
  122. song_title[0]=0;
  123. song_length=-1;
  124. }
  125. if (!song_title[0] || ((unsigned long)song_title < 65536))
  126. {
  127. display = WASABI_API_APP->main_getVersionString();
  128. }
  129. else
  130. {
  131. // TODO: use TimeFmt:: to format the time
  132. if (song_length >= 0)
  133. display.printf(L"%s (%d:%02d)",song_title,song_length/60,song_length%60);
  134. else
  135. display.printf(L"%s",song_title);
  136. }
  137. rotatingDisplay.printf(L"%s *** %s", display, display);
  138. TextInfoCanvas c(this);
  139. Wasabi::FontInfo fontInfo;
  140. GetFontInfo(&fontInfo);
  141. width_of_str_padded = c.getTextWidth(display, &fontInfo) + lpadding - rpadding;
  142. width_of_str = c.getTextWidth(rotatingDisplay, &fontInfo) - c.getTextWidth(display, &fontInfo);
  143. buffer_hw_valid=false;
  144. invalidateBuffer();
  145. }
  146. int SongTicker::onBufferPaint(BltCanvas *canvas, int w, int h)
  147. {
  148. SONGTICKER_PARENT::onBufferPaint(canvas, w, h);
  149. // TODO: benski> need to optimize this :)
  150. RECT r = {0,0,w,h};
  151. canvas->fillRect(&r, RGB(0, 0, 0));
  152. getClientRect(&r);
  153. Wasabi::FontInfo fontInfo;
  154. GetFontInfo(&fontInfo);
  155. /*
  156. if (tickerMode == TICKER_OFF)
  157. {
  158. lpadding=min(lpadding, w);
  159. w=max(w-lpadding+rpadding, 0);
  160. canvas->textOut(lpadding, 0, w, h, display, &fontInfo);
  161. return 1;
  162. }
  163. */
  164. const int textAreaWidth = r.right - r.left;
  165. StringW *whichString = &display;
  166. if (width_of_str_padded > textAreaWidth) // too big to fit?
  167. {
  168. switch (tickerMode)
  169. {
  170. case TICKER_OFF:
  171. {
  172. int extra = width_of_str_padded - textAreaWidth;
  173. if (position>extra)
  174. position=extra;
  175. if (position<0)
  176. position=0;
  177. }
  178. break;
  179. case TICKER_SCROLL:
  180. {
  181. // make sure our position isn't out of bounds
  182. while (position < 0)
  183. position += width_of_str;
  184. position %= (width_of_str);
  185. whichString = &rotatingDisplay;
  186. skipTimers=0;
  187. setTimer(TIMER_SONGTICKER_SCROLL, (int)SONGTICKER_SCROLL_MS);
  188. }
  189. break;
  190. case TICKER_BOUNCE:
  191. {
  192. int extra = width_of_str_padded- textAreaWidth;
  193. if (position < 0)
  194. {
  195. position=0;
  196. ticker_direction=1;
  197. skipTimers=(int)(SONGTICKER_SKIP_MS/SONGTICKER_SCROLL_MS);
  198. }
  199. if (position>=extra)
  200. {
  201. position=extra-1;
  202. ticker_direction=-1;
  203. skipTimers=(int)(SONGTICKER_SKIP_MS/SONGTICKER_SCROLL_MS);
  204. }
  205. if (position < 0)
  206. position=0;
  207. setTimer(TIMER_SONGTICKER_SCROLL, (int)SONGTICKER_SCROLL_MS);
  208. }
  209. break;
  210. }
  211. }
  212. else // if there's enough room, just draw the string as-is
  213. {
  214. position=0;
  215. }
  216. int x =min(lpadding, w);
  217. w=max(w-x+rpadding, 0);
  218. canvas->textOut(lpadding, 0, w, h, whichString->getValueSafe(), &fontInfo);
  219. return 1;
  220. }
  221. void SongTicker::timerCallback(int id)
  222. {
  223. if (id == TIMER_SONGTICKER_SCROLL && !grab)
  224. {
  225. uint32_t this_tick = Wasabi::Std::getTickCount();
  226. if (TICKER_OFF == tickerMode)
  227. {
  228. killTimer(TIMER_SONGTICKER_SCROLL);
  229. return;
  230. }
  231. if (skipTimers)
  232. {
  233. last_tick=this_tick;
  234. skipTimers--;
  235. return;
  236. }
  237. int numTicks=SONGTICKER_INCREMENT_PIXELS;
  238. if (this_tick > last_tick) // make sure we havn't wrapped around
  239. {
  240. numTicks = (int)((this_tick - last_tick) / SONGTICKER_SCROLL_ONE_PIXEL_MS);
  241. last_tick += (uint32_t)(numTicks * SONGTICKER_SCROLL_ONE_PIXEL_MS); // we do this instead of last_tick = this_tick, so we get some error shaping
  242. }
  243. else
  244. last_tick=this_tick;
  245. // move the ticker
  246. position+=(ticker_direction*numTicks);
  247. // ask to be redrawn
  248. if (numTicks)
  249. invalidate();
  250. }
  251. else
  252. SONGTICKER_PARENT::timerCallback(id);
  253. }
  254. void SongTicker::getBufferPaintSource(RECT *r)
  255. {
  256. if (r)
  257. {
  258. RECT cr;
  259. getClientRect(&cr);
  260. const int textAreaWidth = cr.right - cr.left;
  261. if (width_of_str_padded > textAreaWidth) // too big to fit?
  262. {
  263. switch (tickerMode)
  264. {
  265. case TICKER_OFF:
  266. {
  267. int extra = width_of_str_padded- textAreaWidth;
  268. if (position>extra)
  269. position=extra;
  270. if (position<0)
  271. position=0;
  272. }
  273. break;
  274. case TICKER_SCROLL:
  275. {
  276. // make sure our position isn't out of bounds
  277. while (position < 0)
  278. position += width_of_str;
  279. position %= (width_of_str);
  280. }
  281. break;
  282. case TICKER_BOUNCE:
  283. {
  284. int extra = width_of_str_padded - textAreaWidth;
  285. if (position>=extra)
  286. {
  287. position=extra-1;
  288. ticker_direction=-1;
  289. skipTimers=(int)(SONGTICKER_SKIP_MS/SONGTICKER_SCROLL_MS);
  290. }
  291. if (position < 0)
  292. {
  293. position=0;
  294. ticker_direction=1;
  295. skipTimers=(int)(SONGTICKER_SKIP_MS/SONGTICKER_SCROLL_MS);
  296. }
  297. }
  298. break;
  299. }
  300. }
  301. else
  302. {
  303. position=0;
  304. }
  305. r->left = position;
  306. r->right = cr.right - cr.left + position;
  307. r->top = 0;
  308. r->bottom = cr.bottom - cr.top;
  309. }
  310. }
  311. void SongTicker::getBufferPaintSize(int *w, int *h)
  312. {
  313. RECT r;
  314. getClientRect(&r);
  315. int _w = r.right - r.left;
  316. int _h = r.bottom - r.top;
  317. if (!buffer_hw_valid)
  318. {
  319. const int textAreaWidth = r.right - r.left;
  320. StringW *whichString = &display;
  321. if (width_of_str_padded > textAreaWidth && tickerMode == TICKER_SCROLL) // too big to fit?
  322. {
  323. whichString = &rotatingDisplay;
  324. buffer_hw_valid=false;
  325. }
  326. TextInfoCanvas canvas(this);
  327. Wasabi::FontInfo fontInfo;
  328. GetFontInfo(&fontInfo);
  329. textW = canvas.getTextWidth(whichString->getValueSafe(), &fontInfo);
  330. //int textH = canvas.getTextHeight(whichString->getValueSafe(), &fontInfo);
  331. textW = textW + lpadding - rpadding;
  332. buffer_hw_valid=true;
  333. }
  334. *w = max(_w, textW);
  335. *h = _h;//max(_h, textH);
  336. }
  337. int SongTicker::setXuiParam(int _xuihandle, int attrid, const wchar_t *name, const wchar_t *strval)
  338. {
  339. if (xuihandle != _xuihandle) return SONGTICKER_PARENT::setXuiParam(_xuihandle, attrid, name, strval);
  340. switch (attrid)
  341. {
  342. case SONGTICKER_TICKER:
  343. if (!WCSICMP(strval, L"bounce"))
  344. tickerMode=TICKER_BOUNCE;
  345. else if (!WCSICMP(strval, L"scroll"))
  346. {
  347. tickerMode=TICKER_SCROLL;
  348. ticker_direction=1;
  349. }
  350. else if (!WCSICMP(strval, L"off"))
  351. {
  352. tickerMode=TICKER_OFF;
  353. position=0;
  354. killTimer(TIMER_SONGTICKER_SCROLL);
  355. }
  356. buffer_hw_valid=false;
  357. invalidateBuffer();
  358. break;
  359. default:
  360. return 0;
  361. }
  362. return 1;
  363. }
  364. int SongTicker::onAction(const wchar_t *action, const wchar_t *param, int x, int y, intptr_t p1, intptr_t p2, void *data, size_t datalen, ifc_window *source)
  365. {
  366. int r = SONGTICKER_PARENT::onAction(action, param, x, y, p1, p2, data, datalen, source);
  367. if(!WCSICMP(action, L"rebuildtitle"))
  368. BuildTitle();
  369. return r;
  370. }
  371. int SongTicker::viewer_onEvent(api_dependent *item, const GUID *classguid, int event, intptr_t param, void *ptr, size_t ptrlen)
  372. {
  373. if (*classguid == *CfgItem::depend_getClassGuid())
  374. {
  375. if (event==CfgItem::Event_ATTRIBUTE_CHANGED)
  376. {
  377. CfgItem *ci = (CfgItem *)item->dependent_getInterface(CfgItem::depend_getClassGuid());
  378. if (ci->getGuid() == uioptions_guid
  379. && ptr && (WCSCASEEQLSAFE((const wchar_t *)ptr, L"Text Ticker Speed") || WCSCASEEQLSAFE((const wchar_t *)ptr, L"Text Ticker Increment")))
  380. {
  381. BuildTitle();
  382. }
  383. }
  384. }
  385. return 1;
  386. }
  387. int SongTicker::onLeftButtonDown(int x, int y)
  388. {
  389. if (!SONGTICKER_PARENT::onLeftButtonDown(x, y))
  390. {
  391. grab = 0;
  392. return 0;
  393. }
  394. grab = 1;
  395. grab_x = x + position;
  396. // onMouseMove(x,y);
  397. return 1;
  398. }
  399. int SongTicker::onMouseMove(int x, int y)
  400. {
  401. if (!SONGTICKER_PARENT::onMouseMove(x, y))
  402. {
  403. grab = 0;
  404. }
  405. //POINT pos = {x, y};
  406. //clientToScreen(&pos);
  407. if (!grab) return 1;
  408. position = grab_x - x;
  409. // this forces us to calculate wraparound for position
  410. RECT dummy;
  411. getBufferPaintSource(&dummy);
  412. invalidate();
  413. return 1;
  414. }
  415. int SongTicker::onLeftButtonUp(int x, int y)
  416. {
  417. if (!SONGTICKER_PARENT::onLeftButtonUp(x, y))
  418. {
  419. grab = 0;
  420. return 0;
  421. }
  422. grab = 0;
  423. return 1;
  424. }