UIButtonGrid.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. rows_(numRows),
  36. columns_(numCols),
  37. margin_(margin),
  38. rowHeight_(0),
  39. columnWidth_(0),
  40. addCount_(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 column, 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(column); // find column
  62. if (b0)
  63. {
  64. b0->SetText( str.CString() );
  65. }
  66. }
  67. }
  68. /// add strings starting at 0,0 and filling out columns, then next row, returns count
  69. int UIButtonGrid::AddGridText ( String str )
  70. {
  71. int row = 0;
  72. int column = 0;
  73. if ( addCount_ > ( rows_ * columns_) ) return addCount_; // dont write past the end of the grid
  74. row = addCount_ / columns_;
  75. column = addCount_ % columns_;
  76. SetGridText ( row, column, str );
  77. addCount_++;
  78. return addCount_-1;
  79. }
  80. /// returns number of rows that were programmed
  81. int UIButtonGrid::GetNumRows() const
  82. {
  83. return rows_;
  84. }
  85. /// returns number of columns that were programmed
  86. int UIButtonGrid::GetNumColumns() const
  87. {
  88. return columns_;
  89. }
  90. /// returns current row height
  91. int UIButtonGrid::GetRowHeight() const
  92. {
  93. return rowHeight_;
  94. }
  95. /// returns current column width
  96. int UIButtonGrid::GetColumnWidth() const
  97. {
  98. return columnWidth_;
  99. }
  100. /// returns current margin value
  101. int UIButtonGrid::GetMargin() const
  102. {
  103. return margin_;
  104. }
  105. String UIButtonGrid::GetGridId( int row, int column )
  106. {
  107. return String( 'A' + row ) + String(column); // generate spreadsheet style id
  108. }
  109. String UIButtonGrid::GetGridText(int row, int column)
  110. {
  111. if (!widget_)
  112. return "";
  113. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row
  114. if (lo0)
  115. {
  116. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(column); // find column
  117. if (b0)
  118. {
  119. TBStr foo;
  120. if ( b0->GetText( foo ) )
  121. return foo.CStr();
  122. }
  123. }
  124. return "";
  125. }
  126. /// returns text at count
  127. String UIButtonGrid::AtGridText( int count )
  128. {
  129. int row = 0;
  130. int column = 0;
  131. row = count / columns_;
  132. column = count % columns_;
  133. return GetGridText( row, column );
  134. }
  135. UIWidget* UIButtonGrid::GetGridWidget(int row, int column)
  136. {
  137. if (!widget_)
  138. return NULL;
  139. TBWidget *mywidget = NULL;
  140. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row
  141. if (lo0)
  142. {
  143. mywidget = lo0->GetChildFromIndex(column); // find column
  144. }
  145. if ( mywidget )
  146. {
  147. UI* ui = GetSubsystem<UI>();
  148. return ui->WrapWidget(mywidget);
  149. }
  150. return NULL;
  151. }
  152. /// returns widget at count
  153. UIWidget* UIButtonGrid::AtGridWidget( int count )
  154. {
  155. int row = 0;
  156. int column = 0;
  157. row = count / columns_;
  158. column = count % columns_;
  159. return GetGridWidget ( row, column );
  160. }
  161. void UIButtonGrid::GenerateGrid()
  162. {
  163. if ( widget_ && widget_->numChildren() == 0 ) // build once.
  164. {
  165. ((TBLayout *)widget_)->SetSpacing(margin_);
  166. for (int nn=0; nn<rows_; nn++ )
  167. AddGridRow( nn );
  168. }
  169. }
  170. void UIButtonGrid::AddGridRow( int rownum )
  171. {
  172. TBLayout *lo0 = new TBLayout(); // make a new layout
  173. lo0->SetID( TBID (rownum) );
  174. lo0->SetLayoutConfig ( "XACAC" ); // do config + (spacing) margin
  175. lo0->SetSpacing(margin_);
  176. int cc = 0;
  177. for ( cc=0; cc<columns_; cc++) // stuff new button ( with preferred size, new id ) in.
  178. {
  179. LayoutParams *lp0 = new LayoutParams();
  180. lp0->SetWidth( columnWidth_ <= 0 ? 1 : columnWidth_);
  181. lp0->SetHeight( rowHeight_ <= 0 ? 1 : rowHeight_);
  182. TBButton *b0 = new TBButton();
  183. b0->SetLayoutParams(*lp0);
  184. b0->SetSqueezable(true);
  185. TBStr myid;
  186. myid.SetFormatted ( "%c%d", 'A' + rownum, cc+1 );
  187. b0->SetID( TBID (myid) );
  188. lo0->AddChild ( b0 );
  189. }
  190. widget_->AddChild ( lo0 );
  191. }
  192. /// will disable buttons that havent set any text, and enable those with text.
  193. void UIButtonGrid::DisableEmptyButtons()
  194. {
  195. int row = 0;
  196. int column = 0;
  197. for ( row=0; row<rows_; row++ )
  198. {
  199. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row layout
  200. if (lo0)
  201. {
  202. for ( column=0; column<columns_; column++ )
  203. {
  204. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(column); // find column button
  205. if (b0)
  206. {
  207. TBStr foo;
  208. if ( b0->GetText( foo ) )
  209. {
  210. b0->SetState(WIDGET_STATE_DISABLED, foo.IsEmpty() );
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. void UIButtonGrid::ResizeGrid()
  218. {
  219. if ( rows_ == 0 || columns_ == 0 ) return; // dont do bad maths.
  220. TBRect myrect = widget_->GetRect();
  221. rowHeight_ = (int)( (myrect.h - (margin_ * rows_ )) / rows_ );
  222. columnWidth_ = (int)( (myrect.w -( margin_ * columns_ )) / columns_ );
  223. if ( rowHeight_ <= 1) rowHeight_ = 1;
  224. if ( columnWidth_ <= 1) columnWidth_ = 1;
  225. int row = 0;
  226. int column = 0;
  227. for ( row=0; row<rows_; row++ )
  228. {
  229. TBLayout *lo0 = (TBLayout *)widget_->GetChildFromIndex(row); // find row layout
  230. if (lo0)
  231. {
  232. for ( column=0; column<columns_; column++ )
  233. {
  234. TBButton *b0 = (TBButton *)lo0->GetChildFromIndex(column); // find column button
  235. if (b0)
  236. {
  237. LayoutParams *lp1 = new LayoutParams(); // replace with new calced values
  238. lp1->SetWidth(columnWidth_);
  239. lp1->SetHeight(rowHeight_);
  240. b0->SetLayoutParams(*lp1);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. void UIButtonGrid::OnResized(int old_w, int old_h)
  247. {
  248. ResizeGrid();
  249. }
  250. bool UIButtonGrid::OnEvent(const tb::TBWidgetEvent &ev)
  251. {
  252. return UIWidget::OnEvent(ev);
  253. }
  254. }