1-
2- class Grid {
3- private int numRows;
4- private int numCols;
5-
6- private int [] colOffset;
7- private int [] rowOffset;
8- private int rowHeight;
9- private boolean horizontallyCenterTextInCells = false ;
10- private boolean drawTableBorder = false ;
11- private boolean drawTableInnerLines = true ;
12-
13- private int x, y, w;
14- private int pad_horiz = 5 ;
15- private int pad_vert = 5 ;
16-
17- private PFont tableFont = p5;
18- private int tableFontSize = 12 ;
19-
20- private color [][] textColors;
21-
22- private String [][] strings;
23-
24- Grid (int _numRows , int _numCols , int _rowHeight ) {
25- numRows = _numRows;
26- numCols = _numCols;
27- rowHeight = _rowHeight;
28-
29- colOffset = new int [numCols];
30- rowOffset = new int [numRows];
31-
32- strings = new String [numRows][numCols];
33- textColors = new color [numRows][numCols];
34-
35- color defaultTextColor = OPENBCI_DARKBLUE ;
36- for (color [] row: textColors) {
37- Arrays . fill(row, defaultTextColor);
38- }
39- }
40-
41- public void draw () {
42- pushStyle ();
43- textAlign (LEFT );
44- stroke (OPENBCI_DARKBLUE );
45- textFont (p5, 12 );
46-
47- if (drawTableInnerLines) {
48- // draw row lines
49- for (int i = 0 ; i < numRows - 1 ; i++ ) {
50- line (x, y + rowOffset[i], x + w, y + rowOffset[i]);
51- }
52-
53- // draw column lines
54- for (int i = 1 ; i < numCols; i++ ) {
55- line (x + colOffset[i], y, x + colOffset[i], y + rowOffset[numRows - 1 ]);
56- }
57- }
58-
59- // draw cell strings
60- for (int row = 0 ; row < numRows; row++ ) {
61- for (int col = 0 ; col < numCols; col++ ) {
62- if (strings[row][col] != null ) {
63- fill (textColors[row][col]);
64- textAlign (horizontallyCenterTextInCells ? CENTER : LEFT );
65- text (strings[row][col], x + colOffset[col] + pad_horiz, y + rowOffset[row] - pad_vert);
66- }
67- }
68- }
69-
70- if (drawTableBorder) {
71- noFill ();
72- stroke (OPENBCI_DARKBLUE );
73- rect (x, y, w, rowOffset[numRows - 1 ]);
74- }
75-
76- popStyle ();
77- }
78-
79- public RectDimensions getCellDims (int row , int col ) {
80- RectDimensions result = new RectDimensions ();
81- result. x = x + colOffset[col] + 1 ; // +1 accounts for line thickness
82- result. y = y + rowOffset[row] - rowHeight;
83- result. w = w / numCols - 1 ; // -1 account for line thickness
84- result. h = rowHeight;
85-
86- return result;
87- }
88-
89- public void setDim (int _x , int _y , int _w ) {
90- x = _x;
91- y = _y;
92- w = _w;
93-
94- final float colFraction = 1.f / numCols;
95-
96- for (int i = 0 ; i < numCols; i++ ) {
97- colOffset[i] = round (w * colFraction * i);
98- }
99-
100- for (int i = 0 ; i < numRows; i++ ) {
101- rowOffset[i] = rowHeight * (i + 1 );
102- }
103- }
104-
105- public void setString (String s , int row , int col ) {
106- strings[row][col] = s;
107- }
108-
109- public void setTableFontAndSize (PFont _font , int _fontSize ) {
110- tableFont = _font;
111- tableFontSize = _fontSize;
112- }
113-
114- public void setRowHeight (int _height ) {
115- rowHeight = _height;
116- }
117-
118- // This overrides the rowHeight and rowOffset when setting the total height of the Grid.
119- public void setTableHeight (int _height ) {
120- rowHeight = _height / numRows;
121- for (int i = 0 ; i < numRows; i++ ) {
122- rowOffset[i] = rowHeight * (i + 1 );
123- }
124- }
125-
126- public void setTextColor (color c , int row , int col ) {
127- textColors[row][col] = c;
128- }
129-
130- // Change vertical padding for all cells based on the string/text height from a given cell
131- public void dynamicallySetTextVerticalPadding (int row , int col ) {
132- float _textH = getFontStringHeight(tableFont, strings[row][col]);
133- pad_vert = int ( (rowHeight - _textH) / 2 ); // Force round down here
134- }
135-
136- public void setHorizontalCenterTextInCells (boolean b ) {
137- horizontallyCenterTextInCells = b;
138- pad_horiz = b ? getCellDims(0 ,0 ). w/ 2 : 5 ;
139- }
140-
141- public void setDrawTableBorder (boolean b ) {
142- drawTableBorder = b;
143- }
144-
145- public void setDrawTableInnerLines (boolean b ) {
146- drawTableInnerLines = b;
147- }
1+
2+ class Grid {
3+ private int numRows;
4+ private int numCols;
5+
6+ private int [] colOffset;
7+ private int [] rowOffset;
8+ private int rowHeight;
9+ private boolean horizontallyCenterTextInCells = false ;
10+ private boolean drawTableBorder = false ;
11+ private boolean drawTableInnerLines = true ;
12+
13+ private int x, y, w;
14+ private int pad_horiz = 5 ;
15+ private int pad_vert = 5 ;
16+
17+ private PFont tableFont = p5;
18+ private int tableFontSize = 12 ;
19+
20+ private color [][] textColors;
21+
22+ private String [][] strings;
23+
24+ Grid (int _numRows , int _numCols , int _rowHeight ) {
25+ numRows = _numRows;
26+ numCols = _numCols;
27+ rowHeight = _rowHeight;
28+
29+ colOffset = new int [numCols];
30+ rowOffset = new int [numRows];
31+
32+ strings = new String [numRows][numCols];
33+ textColors = new color [numRows][numCols];
34+
35+ color defaultTextColor = OPENBCI_DARKBLUE ;
36+ for (color [] row: textColors) {
37+ Arrays . fill(row, defaultTextColor);
38+ }
39+ }
40+
41+ public void draw () {
42+ pushStyle ();
43+ textAlign (LEFT );
44+ stroke (OPENBCI_DARKBLUE );
45+ textFont (p5, 12 );
46+
47+ if (drawTableInnerLines) {
48+ // draw row lines
49+ for (int i = 0 ; i < numRows - 1 ; i++ ) {
50+ line (x, y + rowOffset[i], x + w, y + rowOffset[i]);
51+ }
52+
53+ // draw column lines
54+ for (int i = 1 ; i < numCols; i++ ) {
55+ line (x + colOffset[i], y, x + colOffset[i], y + rowOffset[numRows - 1 ]);
56+ }
57+ }
58+
59+ // draw cell strings
60+ for (int row = 0 ; row < numRows; row++ ) {
61+ for (int col = 0 ; col < numCols; col++ ) {
62+ if (strings[row][col] != null ) {
63+ fill (textColors[row][col]);
64+ textAlign (horizontallyCenterTextInCells ? CENTER : LEFT );
65+ text (strings[row][col], x + colOffset[col] + pad_horiz, y + rowOffset[row] - pad_vert);
66+ }
67+ }
68+ }
69+
70+ if (drawTableBorder) {
71+ noFill ();
72+ stroke (OPENBCI_DARKBLUE );
73+ rect (x, y, w, rowOffset[numRows - 1 ]);
74+ }
75+
76+ popStyle ();
77+ }
78+
79+ public RectDimensions getCellDims (int row , int col ) {
80+ RectDimensions result = new RectDimensions ();
81+ result. x = x + colOffset[col] + 1 ; // +1 accounts for line thickness
82+ result. y = y + rowOffset[row] - rowHeight;
83+ result. w = w / numCols - 1 ; // -1 account for line thickness
84+ result. h = rowHeight;
85+
86+ return result;
87+ }
88+
89+ public void setDim (int _x , int _y , int _w ) {
90+ x = _x;
91+ y = _y;
92+ w = _w;
93+
94+ final float colFraction = 1.f / numCols;
95+
96+ for (int i = 0 ; i < numCols; i++ ) {
97+ colOffset[i] = round (w * colFraction * i);
98+ }
99+
100+ for (int i = 0 ; i < numRows; i++ ) {
101+ rowOffset[i] = rowHeight * (i + 1 );
102+ }
103+ }
104+
105+ public void setString (String s , int row , int col ) {
106+ strings[row][col] = s;
107+ }
108+
109+ public void setTableFontAndSize (PFont _font , int _fontSize ) {
110+ tableFont = _font;
111+ tableFontSize = _fontSize;
112+ }
113+
114+ public void setRowHeight (int _height ) {
115+ rowHeight = _height;
116+ }
117+
118+ // This overrides the rowHeight and rowOffset when setting the total height of the Grid.
119+ public void setTableHeight (int _height ) {
120+ rowHeight = _height / numRows;
121+ for (int i = 0 ; i < numRows; i++ ) {
122+ rowOffset[i] = rowHeight * (i + 1 );
123+ }
124+ }
125+
126+ public void setTextColor (color c , int row , int col ) {
127+ textColors[row][col] = c;
128+ }
129+
130+ // Change vertical padding for all cells based on the string/text height from a given cell
131+ public void dynamicallySetTextVerticalPadding (int row , int col ) {
132+ float _textH = getFontStringHeight(tableFont, strings[row][col]);
133+ pad_vert = int ( (rowHeight - _textH) / 2 ); // Force round down here
134+ }
135+
136+ public void setHorizontalCenterTextInCells (boolean b ) {
137+ horizontallyCenterTextInCells = b;
138+ pad_horiz = b ? getCellDims(0 ,0 ). w/ 2 : 5 ;
139+ }
140+
141+ public void setDrawTableBorder (boolean b ) {
142+ drawTableBorder = b;
143+ }
144+
145+ public void setDrawTableInnerLines (boolean b ) {
146+ drawTableInnerLines = b;
147+ }
148148}
0 commit comments