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