tb_atomic_widgets.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. //
  6. // Copyright (c) 2016, THUNDERBEAST GAMES LLC All rights reserved
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #include "tb_widgets_reader.h"
  27. #include "tb_widgets_common.h"
  28. #include "tb_node_tree.h"
  29. #include "tb_system.h"
  30. #include "tb_atomic_widgets.h"
  31. #include "tb_editfield.h"
  32. #include "tb_menu_window.h"
  33. #include <math.h>
  34. namespace tb {
  35. // == TBColorWidget =======================================
  36. TBColorWidget::TBColorWidget() : color_(), alpha_ ( 1.0f)
  37. {
  38. }
  39. void TBColorWidget::SetColor ( const char *name )
  40. {
  41. if ( name )
  42. color_.SetFromString(name, strlen(name));
  43. InvalidateSkinStates();
  44. Invalidate();
  45. }
  46. void TBColorWidget::SetColor(float r, float g, float b, float a)
  47. {
  48. color_.Set(TBColor(r, g, b, a));
  49. InvalidateSkinStates();
  50. Invalidate();
  51. }
  52. void TBColorWidget::SetAlpha ( float value )
  53. {
  54. if ( value < 0.0 || value > 1.0 )
  55. {
  56. alpha_ = 1.0;
  57. return;
  58. }
  59. alpha_ = value;
  60. InvalidateSkinStates();
  61. Invalidate();
  62. }
  63. void TBColorWidget::OnPaint(const PaintProps &paint_props)
  64. {
  65. TBRect local_rect = GetRect();
  66. local_rect.x = 0;
  67. local_rect.y = 0;
  68. float old_opacity = g_renderer->GetOpacity();
  69. g_renderer->SetOpacity(alpha_);
  70. g_renderer->DrawRectFill(local_rect, color_);
  71. g_renderer->SetOpacity(old_opacity);
  72. }
  73. void TBColorWidget::OnInflate(const INFLATE_INFO &info)
  74. {
  75. if (const char *colr = info.node->GetValueString("color", nullptr))
  76. SetColor(colr);
  77. TBWidget::OnInflate(info);
  78. }
  79. TB_WIDGET_FACTORY(TBColorWidget, TBValue::TYPE_NULL, WIDGET_Z_TOP) {}
  80. // == TBColorWheel =======================================
  81. TBColorWheel::TBColorWheel() :
  82. markerx_(128),
  83. markery_(128),
  84. markercolor_(),
  85. hue_(0.0),
  86. saturation_(0.0)
  87. {
  88. }
  89. void TBColorWheel::OnPaint(const PaintProps &paint_props)
  90. {
  91. TBWidget::OnPaint(paint_props); // draw the widget stuff
  92. TBRect local_rect ( 0,0,4,4 ); // AND draw a marker where we clicked.
  93. local_rect.x = markerx_ - 2;
  94. local_rect.y = markery_ - 2;
  95. g_renderer->DrawRect( local_rect, markercolor_);
  96. local_rect.x -= 1;
  97. local_rect.y -= 1;
  98. local_rect.w += 2;
  99. local_rect.h += 2;
  100. g_renderer->DrawRect( local_rect, markercolor_); // draw double box
  101. }
  102. bool TBColorWheel::OnEvent(const TBWidgetEvent &ev)
  103. {
  104. if (ev.target == this && ev.type == EVENT_TYPE_CLICK)
  105. {
  106. SetMarkerX ( ev.target_x );
  107. SetMarkerY ( ev.target_y );
  108. CalcHueSaturation( markerx_, markery_ );
  109. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  110. InvokeEvent(ev);
  111. }
  112. return TBWidget::OnEvent(ev);
  113. }
  114. void TBColorWheel::SetHueSaturation ( float hue, float saturation )
  115. {
  116. // suppose to set the marker position to match HS here
  117. hue_ = hue * 360.0;
  118. saturation_ = saturation * 128.0;
  119. Invalidate();
  120. }
  121. void TBColorWheel::CalcHueSaturation ( int rawx, int rawy )
  122. {
  123. TBRect rect = GetRect();
  124. int centerx = rect.w / 2;
  125. int centery = rect.h / 2;
  126. float X1 = rawx;
  127. float Y1 = rawy;
  128. float X2 = centerx;
  129. float Y2 = centery;
  130. float angle = 0.0;
  131. float xd = X2-X1;
  132. float yd = Y2-Y1;
  133. float dx = sqrt(xd * xd + yd * yd);
  134. // angle in degrees
  135. angle = atan2(Y2 - Y1, X2 - X1) * 180 / 3.14159265358979323846;
  136. if (angle < 0) angle += 360.0;
  137. // if the distance > 128, can we calculate the line point at 128 and set the marker there?
  138. if( dx > 128.0 ) dx = 128.0; // limit value
  139. saturation_ = dx;
  140. hue_ = angle;
  141. }
  142. void TBColorWheel::SetMarkerX ( int value )
  143. {
  144. markerx_ = value;
  145. }
  146. void TBColorWheel::SetMarkerY ( int value )
  147. {
  148. markery_ = value;
  149. }
  150. void TBColorWheel::SetMarkerColor ( const char *name )
  151. {
  152. if ( name )
  153. markercolor_.SetFromString(name, strlen(name));
  154. Invalidate();
  155. }
  156. void TBColorWheel::OnInflate(const INFLATE_INFO &info)
  157. {
  158. if (const char *colr = info.node->GetValueString("color", nullptr))
  159. SetMarkerColor(colr);
  160. TBWidget::OnInflate(info);
  161. }
  162. TB_WIDGET_FACTORY(TBColorWheel, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  163. // == TBBarGraph =======================================
  164. TBBarGraph::TBBarGraph() : color_(255,255,255,255), m_value (0.0), m_axis(AXIS_X), m_margin(0)
  165. {
  166. SetSkinBg(TBIDC("background_solid"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  167. }
  168. void TBBarGraph::SetColor ( const char *name )
  169. {
  170. if ( name )
  171. color_.SetFromString(name, strlen(name));
  172. InvalidateSkinStates();
  173. Invalidate();
  174. }
  175. void TBBarGraph::SetColor(float r, float g, float b, float a)
  176. {
  177. color_.Set(TBColor(r, g, b, a));
  178. InvalidateSkinStates();
  179. Invalidate();
  180. }
  181. void TBBarGraph::OnPaint(const PaintProps &paint_props)
  182. {
  183. TBRect local_rect = GetRect();
  184. local_rect.x = 0;
  185. local_rect.y = 0;
  186. if ( m_axis == AXIS_X ) // horizontal bar
  187. {
  188. double w1 = (double)local_rect.w * ( m_value / 100.0 );
  189. local_rect.w = (int)w1;
  190. if ( m_margin > 0 && m_margin < (local_rect.h/2)-2)
  191. {
  192. local_rect.h -= (m_margin *2);
  193. local_rect.y += m_margin;
  194. }
  195. }
  196. else if ( m_axis == AXIS_Y ) // vertical bar
  197. {
  198. double h1 = (double)local_rect.h * ( m_value / 100.0 );
  199. local_rect.y = local_rect.h - (int)h1;
  200. local_rect.h = (int)h1;
  201. if ( m_margin > 0 && m_margin < (local_rect.w/2)-2 )
  202. {
  203. local_rect.w -= (m_margin*2);
  204. local_rect.x += m_margin;
  205. }
  206. }
  207. g_renderer->DrawRectFill(local_rect, color_);
  208. }
  209. void TBBarGraph::OnInflate(const INFLATE_INFO &info)
  210. {
  211. if (const char *colr = info.node->GetValueString("color", nullptr))
  212. SetColor(colr);
  213. if ( const char *axis = info.node->GetValueString("axis", "x") )
  214. SetAxis(*axis == 'x' ? AXIS_X : AXIS_Y);
  215. if (info.sync_type == TBValue::TYPE_FLOAT)
  216. SetValueDouble(info.node->GetValueFloat("value", 0));
  217. SetMargin( (unsigned)info.node->GetValueInt("margin", 0 ) );
  218. TBWidget::OnInflate(info);
  219. }
  220. void TBBarGraph::SetValueDouble(double value)
  221. {
  222. value = CLAMP(value, 0.0, 100.0);
  223. if (value == m_value)
  224. return;
  225. m_value = value;
  226. InvalidateSkinStates();
  227. Invalidate();
  228. }
  229. void TBBarGraph::SetAxis(AXIS axis)
  230. {
  231. m_axis = axis;
  232. InvalidateSkinStates();
  233. Invalidate();
  234. }
  235. TB_WIDGET_FACTORY(TBBarGraph, TBValue::TYPE_FLOAT, WIDGET_Z_TOP) {}
  236. // == TBPromptWindow =======================================
  237. TBPromptWindow::TBPromptWindow(TBWidget *target, TBID id)
  238. : m_target(target)
  239. {
  240. TBWidgetListener::AddGlobalListener(this);
  241. SetID(id);
  242. }
  243. TBPromptWindow::~TBPromptWindow()
  244. {
  245. TBWidgetListener::RemoveGlobalListener(this);
  246. if (TBWidget *dimmer = m_dimmer.Get())
  247. {
  248. dimmer->GetParent()->RemoveChild(dimmer);
  249. delete dimmer;
  250. }
  251. }
  252. bool TBPromptWindow::Show(const char *title, const char *message,
  253. const char *preset, int dimmer,
  254. int width, int height)
  255. {
  256. TBWidget *target = m_target.Get();
  257. if (!target)
  258. return false;
  259. TBWidget *root = target->GetParentRoot();
  260. const char *source = "TBLayout: axis: y, distribution: gravity, position: left\n"
  261. " TBLayout: axis: y, distribution: gravity, distribution-position: left\n"
  262. " TBTextField: id: 1, gravity: left right\n"
  263. " font: size: 14dp\n"
  264. " TBEditField: id: 2, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  265. " font: size: 14dp\n"
  266. " TBSeparator: gravity: left right\n"
  267. " TBLayout: distribution: gravity\n"
  268. " TBButton: text: \" OK \", id: \"TBPromptWindow.ok\"\n"
  269. " font: size: 14dp\n"
  270. " TBButton: text: \"Cancel\", id: \"TBPromptWindow.cancel\"\n"
  271. " font: size: 14dp\n";
  272. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  273. return false;
  274. SetText(title);
  275. TBTextField *editfield = GetWidgetByIDAndType<TBTextField>(1);
  276. editfield->SetText(message);
  277. editfield->SetSkinBg("");
  278. TBEditField *stringfield = GetWidgetByIDAndType<TBEditField>(2);
  279. if (preset)
  280. stringfield->SetText(preset);
  281. TBRect rect;
  282. // Size to fit content. This will use the default size of the textfield.
  283. if (width == 0 || height == 0)
  284. {
  285. ResizeToFitContent();
  286. rect = GetRect();
  287. }
  288. else
  289. {
  290. SetSize(width, height);
  291. rect = GetRect();
  292. }
  293. // Create background dimmer
  294. if (dimmer != 0)
  295. {
  296. if (TBDimmer *dimmer = new TBDimmer)
  297. {
  298. root->AddChild(dimmer);
  299. m_dimmer.Set(dimmer);
  300. }
  301. }
  302. // Center and size to the new height
  303. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  304. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  305. root->AddChild(this);
  306. return true;
  307. }
  308. bool TBPromptWindow::OnEvent(const TBWidgetEvent &ev)
  309. {
  310. if (ev.type == EVENT_TYPE_CLICK && ev.target->IsOfType<TBButton>())
  311. {
  312. TBWidgetSafePointer this_widget(this);
  313. // Invoke the click on the target
  314. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  315. target_ev.ref_id = ev.target->GetID();
  316. InvokeEvent(target_ev);
  317. // If target got deleted, close
  318. if (this_widget.Get())
  319. Close();
  320. return true;
  321. }
  322. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  323. {
  324. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  325. m_close_button.InvokeEvent(click_ev);
  326. return true;
  327. }
  328. return TBWindow::OnEvent(ev);
  329. }
  330. void TBPromptWindow::OnDie()
  331. {
  332. if (TBWidget *dimmer = m_dimmer.Get())
  333. dimmer->Die();
  334. }
  335. void TBPromptWindow::OnWidgetDelete(TBWidget *widget)
  336. {
  337. // If the target widget is deleted, close!
  338. if (!m_target.Get())
  339. Close();
  340. }
  341. bool TBPromptWindow::OnWidgetDying(TBWidget *widget)
  342. {
  343. // If the target widget or an ancestor of it is dying, close!
  344. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  345. Close();
  346. return false;
  347. }
  348. // == TBFinderWindow =======================================
  349. TBFinderWindow::TBFinderWindow(TBWidget *target, TBID id)
  350. : m_target(target),
  351. rightMenuParent(NULL),
  352. rightMenuChild(NULL)
  353. {
  354. TBWidgetListener::AddGlobalListener(this);
  355. SetID(id);
  356. }
  357. TBFinderWindow::~TBFinderWindow()
  358. {
  359. TBWidgetListener::RemoveGlobalListener(this);
  360. if (TBWidget *dimmer = m_dimmer.Get())
  361. {
  362. dimmer->GetParent()->RemoveChild(dimmer);
  363. delete dimmer;
  364. }
  365. rightMenuParent=NULL;
  366. rightMenuChild=NULL;
  367. }
  368. bool TBFinderWindow::Show(const char *title,
  369. const char *preset, int dimmer,
  370. int width, int height)
  371. {
  372. TBWidget *target = m_target.Get();
  373. if (!target)
  374. return false;
  375. TBWidget *root = target->GetParentRoot();
  376. const char *source =
  377. "TBLayout: axis: y, size: available, position: gravity, distribution: gravity\n"
  378. " lp: min-width: 512dp, min-height: 500dp\n"
  379. " TBLayout: distribution: gravity, distribution-position: left\n"
  380. " TBEditField: id: 1, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  381. " tooltip current folder location\n"
  382. " font: size: 14dp\n"
  383. " TBButton\n"
  384. " lp: height: 28dp, width: 28dp\n"
  385. " skin TBButton.uniformflat\n"
  386. " TBSkinImage: skin: FolderUp, id: up_image\n"
  387. " id 2\n"
  388. " tooltip Go up one folder\n"
  389. " TBWidget\n"
  390. " lp: height: 28dp, width: 28dp\n"
  391. " TBButton\n"
  392. " lp: height: 28dp, width: 28dp\n"
  393. " skin TBButton.uniformflat\n"
  394. " TBSkinImage: skin: BookmarkIcon, id: book_image\n"
  395. " id 3\n"
  396. " tooltip Bookmark current folder\n"
  397. " TBButton\n"
  398. " lp: height: 28dp, width: 28dp\n"
  399. " skin TBButton.uniformflat\n"
  400. " TBSkinImage: skin: FolderAdd, id: create_image\n"
  401. " id 4\n"
  402. " tooltip Create a new folder \n"
  403. " TBLayout: distribution: gravity, distribution-position: left\n"
  404. " TBLayout: axis: x, distribution: gravity\n"
  405. " TBSelectList: id: 5, gravity: all\n"
  406. " lp: max-width: 160dp, min-width: 160dp\n"
  407. " font: size: 14dp\n"
  408. " TBSelectList: id: 6, gravity: all\n"
  409. " font: size: 14dp\n"
  410. " TBLayout: distribution: gravity\n"
  411. " TBEditField: id: 7, multiline: 0, styling: 0, adapt-to-content: 0, gravity: left right\n"
  412. " tooltip name of the wanted file\n"
  413. " font: size: 14dp\n"
  414. " TBLayout: distribution: gravity\n"
  415. " TBButton: text: \" OK \", id: 8\n"
  416. " font: size: 14dp\n"
  417. " TBButton: text: \"Cancel\", id: 9\n"
  418. " font: size: 14dp\n";
  419. if (!g_widgets_reader->LoadData(GetContentRoot(), source))
  420. return false;
  421. SetText(title);
  422. TBRect rect;
  423. // Size to fit content. This will use the default size of the textfield.
  424. if (width == 0 || height == 0)
  425. {
  426. ResizeToFitContent();
  427. rect = GetRect();
  428. }
  429. else
  430. {
  431. SetSize(width, height);
  432. rect = GetRect();
  433. }
  434. // Create background dimmer
  435. if (dimmer != 0)
  436. {
  437. if (TBDimmer *dimmer = new TBDimmer)
  438. {
  439. root->AddChild(dimmer);
  440. m_dimmer.Set(dimmer);
  441. }
  442. }
  443. // Center and size to the new height
  444. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  445. SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  446. root->AddChild(this);
  447. return true;
  448. }
  449. bool TBFinderWindow::OnEvent(const TBWidgetEvent &ev)
  450. {
  451. if (ev.type == EVENT_TYPE_CLICK )
  452. {
  453. if ( ev.target->IsOfType<TBButton>())
  454. {
  455. TBWidgetSafePointer this_widget(this);
  456. // Invoke the click on the target
  457. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  458. target_ev.ref_id = ev.target->GetID();
  459. InvokeEvent(target_ev);
  460. // these are internal buttons that do not close the finder window!
  461. bool isbuttons = (ev.target->GetID() == 2 || ev.target->GetID() == 3 || ev.target->GetID() == 4 );
  462. // If target got deleted, close
  463. if (this_widget.Get() && !isbuttons )
  464. Close();
  465. return true;
  466. }
  467. else if ( ev.target->GetID() == TBIDC("popupmenu"))
  468. {
  469. if (ev.ref_id == TBIDC("delete"))
  470. {
  471. // send EVENT_TYPE_CUSTOM, were gonna used cached information to send info how we got here
  472. if(rightMenuParent)
  473. {
  474. TBWidgetEvent custom_ev(EVENT_TYPE_CUSTOM);
  475. custom_ev.target = rightMenuParent; // were want to operate on this list
  476. custom_ev.ref_id = rightMenuChild?rightMenuChild->GetID():ev.ref_id; // on this entry
  477. custom_ev.special_key = TB_KEY_DELETE; // and what we wanna do to it
  478. rightMenuParent->InvokeEvent(custom_ev); // forward to delegate
  479. rightMenuChild = NULL; // clear the cached values
  480. rightMenuParent = NULL;
  481. }
  482. }
  483. else
  484. return false;
  485. return true;
  486. }
  487. }
  488. else if (ev.type == EVENT_TYPE_KEY_DOWN && ev.special_key == TB_KEY_ESC)
  489. {
  490. TBWidgetEvent click_ev(EVENT_TYPE_CLICK);
  491. m_close_button.InvokeEvent(click_ev);
  492. return true;
  493. }
  494. else if (ev.type == EVENT_TYPE_CONTEXT_MENU || ev.type == EVENT_TYPE_RIGHT_POINTER_UP ) // want to embed popup menu on TBSelectList id: 5
  495. {
  496. rightMenuChild = ev.target; // save for later, this is where we started
  497. rightMenuParent = FindParentList(ev.target); // save for later, omg why is this so hard!
  498. if ( rightMenuParent && rightMenuParent->GetID() == 5 ) // if we clicked in bookmark list take action!
  499. {
  500. TBPoint pos_in_root(ev.target_x, ev.target_y);
  501. if (TBMenuWindow *menu = new TBMenuWindow(rightMenuParent, TBIDC("popupmenu")))
  502. {
  503. TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource();
  504. source->AddItem(new TBGenericStringItem("delete", TBIDC("delete")));
  505. menu->Show(source, TBPopupAlignment(pos_in_root), -1);
  506. }
  507. return true;
  508. }
  509. }
  510. return TBWindow::OnEvent(ev);
  511. }
  512. void TBFinderWindow::OnDie()
  513. {
  514. if (TBWidget *dimmer = m_dimmer.Get())
  515. dimmer->Die();
  516. }
  517. void TBFinderWindow::OnWidgetDelete(TBWidget *widget)
  518. {
  519. // If the target widget is deleted, close!
  520. if (!m_target.Get())
  521. Close();
  522. }
  523. bool TBFinderWindow::OnWidgetDying(TBWidget *widget)
  524. {
  525. // If the target widget or an ancestor of it is dying, close!
  526. if (widget == m_target.Get() || widget->IsAncestorOf(m_target.Get()))
  527. Close();
  528. return false;
  529. }
  530. TBWidget *TBFinderWindow::FindParentList( TBWidget *widget) // utility for dealing with menus.
  531. {
  532. TBWidget *tmp = widget;
  533. while (tmp)
  534. {
  535. if ( tmp->IsOfType<TBSelectList>() ) return tmp;
  536. tmp = tmp->GetParent();
  537. }
  538. return NULL;
  539. }
  540. }; // namespace tb