UIButtonGrid.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include <TurboBadger/tb_widgets_common.h>
  24. #include <TurboBadger/tb_layout.h>
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "UIWidget.h"
  28. #include "UILayout.h"
  29. #include "UIButtonGrid.h"
  30. using namespace tb;
  31. namespace Atomic
  32. {
  33. UIButtonGrid::UIButtonGrid(Context* context, int numRows, int numCols, int margin, bool createWidget )
  34. : UIWidget(context, false),
  35. m_rows(numRows),
  36. m_cols(numCols),
  37. m_margin(margin),
  38. m_rowHeight(0),
  39. m_colWidth(0),
  40. m_count(0)
  41. {
  42. if (createWidget)
  43. {
  44. widget_ = new TBLayout(AXIS_Y); // build upon a stock TBLayout
  45. widget_->SetDelegate(this);
  46. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  47. GetSubsystem<UI>()->WrapWidget(this, widget_);
  48. GenerateGrid();
  49. }
  50. }
  51. UIButtonGrid::~UIButtonGrid()
  52. {
  53. }
  54. void UIButtonGrid::SetGridText (int row, int col, String str)
  55. {
  56. if (!widget_)
  57. return;
  58. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row
  59. if (lo0)
  60. {
  61. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(col); // find col
  62. if (b0)
  63. {
  64. b0->SetText( str.CString() );
  65. }
  66. }
  67. }
  68. int UIButtonGrid::AddGridText ( String str ) /// add strings starting at 0,0 and filling out cols, then next row, returns count
  69. {
  70. int row = 0;
  71. int col = 0;
  72. if ( m_count > ( m_rows * m_cols) ) return m_count; // dont write past the end of the grid
  73. row = m_count / m_cols;
  74. col = m_count % m_cols;
  75. SetGridText ( row, col, str );
  76. m_count++;
  77. return m_count-1;
  78. }
  79. int UIButtonGrid::GetNumRows() const /// returns number of rows that were programmed
  80. {
  81. return m_rows;
  82. }
  83. int UIButtonGrid::GetNumCols() const /// returns number of cols that were programmed
  84. {
  85. return m_cols;
  86. }
  87. int UIButtonGrid::GetRowHeight() const /// returns current row height
  88. {
  89. return m_rowHeight;
  90. }
  91. int UIButtonGrid::GetColWidth() const /// returns current col width
  92. {
  93. return m_colWidth;
  94. }
  95. int UIButtonGrid::GetMargin() const /// returns current margin value
  96. {
  97. return m_margin;
  98. }
  99. String UIButtonGrid::GetGridId( int row, int col )
  100. {
  101. return String( 'A' + row ) + String(col); // generate spreadsheet style id
  102. }
  103. String UIButtonGrid::GetGridText(int row, int col)
  104. {
  105. if (!widget_)
  106. return "";
  107. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row
  108. if (lo0)
  109. {
  110. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(col); // find col
  111. if (b0)
  112. {
  113. TBStr foo;
  114. if ( b0->GetText( foo ) )
  115. return foo.CStr();
  116. }
  117. }
  118. return "";
  119. }
  120. String UIButtonGrid::AtGridText( int count ) /// returns text at count
  121. {
  122. int row = 0;
  123. int col = 0;
  124. row = count / m_cols;
  125. col = count % m_cols;
  126. return GetGridText( row, col );
  127. }
  128. UIWidget* UIButtonGrid::GetGridWidget(int row, int col)
  129. {
  130. if (!widget_)
  131. return NULL;
  132. TBWidget *mywidget = NULL;
  133. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row
  134. if (lo0)
  135. {
  136. mywidget = lo0->GetChildFromIndex(col); // find col
  137. }
  138. if ( mywidget )
  139. {
  140. UI* ui = GetSubsystem<UI>();
  141. return ui->WrapWidget(mywidget);
  142. }
  143. return NULL;
  144. }
  145. UIWidget* UIButtonGrid::AtGridWidget( int count ) /// returns widget at count
  146. {
  147. int row = 0;
  148. int col = 0;
  149. row = count / m_cols;
  150. col = count % m_cols;
  151. return GetGridWidget ( row, col );
  152. }
  153. void UIButtonGrid::GenerateGrid()
  154. {
  155. if ( widget_ && widget_->numChildren() == 0 ) // build once.
  156. {
  157. ((TBLayout *)widget_)->SetSpacing(m_margin);
  158. for (int nn=0; nn<m_rows; nn++ )
  159. AddGridRow( nn );
  160. }
  161. }
  162. void UIButtonGrid::AddGridRow( int rownum )
  163. {
  164. TBLayout *lo0 = new TBLayout(); // make a new layout
  165. lo0->SetID( TBID (rownum) );
  166. lo0->SetLayoutConfig ( "XACAC" ); // do config + (spacing) margin
  167. lo0->SetSpacing(m_margin);
  168. int cc = 0;
  169. for ( cc=0; cc<m_cols; cc++) // stuff new button ( with preferred size, new id ) in.
  170. {
  171. LayoutParams *lp0 = new LayoutParams();
  172. lp0->SetWidth( m_colWidth <= 0 ? 1 : m_colWidth);
  173. lp0->SetHeight( m_rowHeight <= 0 ? 1 : m_rowHeight);
  174. TBButton *b0 = new TBButton();
  175. b0->SetLayoutParams(*lp0);
  176. b0->SetSqueezable(true);
  177. TBStr myid;
  178. myid.SetFormatted ( "%c%d", 'A' + rownum, cc+1 );
  179. b0->SetID( TBID (myid) );
  180. lo0->AddChild ( b0 );
  181. }
  182. widget_->AddChild ( lo0 );
  183. }
  184. void UIButtonGrid::DisableEmptyButtons() /// will disable buttons that havent set any text, and enable those with text.
  185. {
  186. int row = 0;
  187. int col = 0;
  188. for ( row=0; row<m_rows; row++ )
  189. {
  190. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row layout
  191. if (lo0)
  192. {
  193. for ( col=0; col<m_cols; col++ )
  194. {
  195. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(col); // find col button
  196. if (b0)
  197. {
  198. TBStr foo;
  199. if ( b0->GetText( foo ) )
  200. {
  201. b0->SetState(WIDGET_STATE_DISABLED, foo.IsEmpty() );
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. void UIButtonGrid::ResizeGrid()
  209. {
  210. if ( m_rows == 0 || m_cols == 0 ) return; // dont do bad maths.
  211. TBRect myrect = widget_->GetRect();
  212. m_rowHeight = (int)( (myrect.h - (m_margin * m_rows )) / m_rows );
  213. m_colWidth = (int)( (myrect.w -( m_margin * m_cols )) / m_cols );
  214. if ( m_rowHeight <= 1) m_rowHeight = 1;
  215. if ( m_colWidth <= 1) m_colWidth = 1;
  216. int row = 0;
  217. int col = 0;
  218. for ( row=0; row<m_rows; row++ )
  219. {
  220. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row layout
  221. if (lo0)
  222. {
  223. for ( col=0; col<m_cols; col++ )
  224. {
  225. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(col); // find col button
  226. if (b0)
  227. {
  228. LayoutParams *lp1 = new LayoutParams(); // replace with new calced values
  229. lp1->SetWidth(m_colWidth);
  230. lp1->SetHeight(m_rowHeight);
  231. b0->SetLayoutParams(*lp1);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. void UIButtonGrid::OnResized(int old_w, int old_h)
  238. {
  239. ResizeGrid();
  240. }
  241. bool UIButtonGrid::OnEvent(const tb::TBWidgetEvent &ev)
  242. {
  243. return UIWidget::OnEvent(ev);
  244. }
  245. }