Skip to content

Commit 5d95783

Browse files
Merge pull request #1 from GabrielBRDeveloper/measure-by-parent
Measure each child by parent
2 parents b78457e + 8cebfa5 commit 5d95783

9 files changed

Lines changed: 92 additions & 138 deletions

File tree

src/br/nullexcept/mux/core/texel/WindowContainer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public void measure() {
5252
if (rootCanvas != null) {
5353
getBounds().set(0, 0, rootCanvas.getWidth(), rootCanvas.getHeight());
5454
}
55+
while (measureChildren()) {
56+
measure();
57+
}
5558
}
5659

5760
@Override
@@ -163,10 +166,7 @@ public void addChild(View view) {
163166

164167
@Override
165168
public void requestLayout() {
166-
for (View view: getChildren())
167-
view.measure();
168-
for (View view: getChildren())
169-
view.measureBounds();
169+
measure();
170170
}
171171

172172
public void dispose(){
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package br.nullexcept.mux.graphics;
2+
3+
public class Size {
4+
public int width;
5+
public int height;
6+
7+
public Size() {
8+
this(0,0);
9+
}
10+
11+
public Size(int width, int height) {
12+
this.width = width;
13+
this.height = height;
14+
}
15+
}

src/br/nullexcept/mux/test/Example.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ public static void main(String[] args) {
1010
Application.initialize(Example::new);
1111
}
1212

13-
private void printXml(String space, XmlElement element){
14-
System.out.println(space+" ["+element.name()+"]");
15-
for (String key: element.attrNames()){
16-
System.out.println(space+" - "+key+" = "+element.attr(key));
17-
}
18-
if (element.childCount() > 0) {
19-
System.out.println(space + " - Children: ");
20-
for (int i = 0; i < element.childCount(); i++) {
21-
printXml(space + " ", element.childAt(i));
22-
}
23-
}
24-
}
25-
2613
@Override
2714
public void onCreate() {
2815
super.onCreate();

src/br/nullexcept/mux/view/View.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -258,47 +258,16 @@ public void measure() {
258258
if (parent == null)
259259
return;
260260

261-
int parentWidth = parent.getMeasuredWidth() - parent.getPaddingLeft() -
262-
parent.getPaddingRight();
263-
int parentHeight = parent.getMeasuredHeight() - parent.getPaddingTop() -
264-
parent.getPaddingBottom();
265-
266-
Point location = parent.getChildLocation(this);
267-
int ow = getMeasuredWidth();
268-
int oh = getMeasuredHeight();
269-
{ // Measure sizes
270-
int w = 1;
271-
int h = 1;
272-
if (params.width >= 0) {
273-
w = params.width;
274-
} else if (params.width == ViewGroup.LayoutParams.MATCH_PARENT) {
275-
w = Math.max(0, parentWidth - location.x + parent.getPaddingLeft());
276-
} else {
277-
w = calculateWidth();
278-
}
279-
280-
if (params.height >= 0) {
281-
h = params.height;
282-
} else if (params.height == ViewGroup.LayoutParams.MATCH_PARENT) {
283-
h = Math.max(0, parentHeight - location.y + parent.getPaddingTop());
284-
} else {
285-
h = calculateHeight();
286-
}
287-
if (ow != w || oh != h) {
288-
onMeasure(w, h);
289-
parent.requestLayout();
290-
measure();
291-
invalidate();
261+
if (parent.measureChild(this)) {
262+
ViewGroup.LayoutParams pp = parent.getLayoutParams();
263+
if (pp.width < 0 || pp.height < 0) {
264+
parent.measure();
292265
}
293266
}
294267
}
295268

296-
protected int calculateWidth() {
297-
return padding.left + padding.right;
298-
}
299-
300-
protected int calculateHeight() {
301-
return padding.top + padding.bottom;
269+
protected Size onMeasureContent() {
270+
return new Size(0,0);
302271
}
303272

304273
public <T extends View> T findViewByTag(Object tag) {
@@ -398,6 +367,9 @@ public final void measureBounds() {
398367

399368
protected void onMeasure(int width, int height) {
400369
measured.set(width, height);
370+
getParent().requestLayout();
371+
measure();
372+
invalidate();
401373
}
402374

403375
final void setParent(ViewGroup group) {

src/br/nullexcept/mux/view/ViewGroup.java

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import br.nullexcept.mux.app.Context;
44
import br.nullexcept.mux.graphics.Point;
55
import br.nullexcept.mux.graphics.Rect;
6+
import br.nullexcept.mux.graphics.Size;
67
import br.nullexcept.mux.input.MotionEvent;
78
import br.nullexcept.mux.input.MouseEvent;
89
import br.nullexcept.mux.res.AttributeList;
@@ -32,57 +33,62 @@ public void requestLayout() {
3233
invalidate();
3334
} else {
3435
measure();
35-
measureBounds();
3636
}
3737
}
3838

39-
private void measureChildren(){
40-
for (View child : children) {
41-
child.measure();
39+
protected int measureSpec(int parent, int spec, int wrap, int padding) {
40+
if (spec == LayoutParams.WRAP_CONTENT) {
41+
return wrap + padding;
42+
} else if (spec == LayoutParams.MATCH_PARENT) {
43+
return Math.max(parent, 0);
44+
} else {
45+
return spec;
46+
}
47+
}
48+
49+
protected boolean measureChild(View child) {
50+
LayoutParams params = child.getLayoutParams();
51+
Size size = child.onMeasureContent();
52+
Point pos = getChildLocation(child);
53+
54+
int left = pos.x - getPaddingLeft();
55+
int top = pos.y - getPaddingTop();
56+
57+
int w = measureSpec(getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-left, params.width, size.width, child.getPaddingLeft() + child.getPaddingRight());
58+
int h = measureSpec(getMeasuredHeight()-getPaddingTop()-getPaddingBottom()-top, params.height, size.height, child.getPaddingTop() + child.getPaddingBottom());
59+
if (w != child.getMeasuredWidth() || h != child.getMeasuredHeight()) {
60+
child.onMeasure(w,h);
61+
return true;
4262
}
63+
return false;
64+
}
65+
66+
protected boolean measureChildren(){
67+
if (children == null)
68+
return false;
69+
70+
boolean changed = false;
4371
for (View child : children) {
72+
changed |= measureChild(child);
73+
}
74+
75+
for (View child: children) {
4476
child.measureBounds();
4577
}
78+
return changed;
4679
}
4780

4881
@Override
4982
public void measure() {
50-
LayoutParams params = getLayoutParams();
5183
ViewGroup parent = getParent();
5284
if (parent == null)
5385
return;
5486

55-
parent.getInternalSize(rect);
56-
57-
int ow = getMeasuredWidth();
58-
int oh = getMeasuredHeight();
59-
Point location = parent.getChildLocation(this);
60-
int width, height;
61-
if (params.hasWrap()) {
62-
measureChildren();
63-
if (params.width == LayoutParams.WRAP_CONTENT) {
64-
width = calculateWidth();
65-
} else {
66-
width = params.width == LayoutParams.MATCH_PARENT ? rect.width() - location.x : params.width;
67-
}
68-
if (params.height == LayoutParams.WRAP_CONTENT) {
69-
height = calculateHeight();
70-
} else {
71-
height = params.height == LayoutParams.MATCH_PARENT ? rect.height() - location.y : params.height;
72-
}
73-
} else {
74-
width = params.width == LayoutParams.MATCH_PARENT ? rect.width() - location.x : params.width;
75-
height = params.height == LayoutParams.MATCH_PARENT ? rect.height() - location.y : params.height;
76-
width = Math.max(0, width);
77-
height = Math.max(0, height);
78-
}
79-
80-
measureChildren();
81-
if (width != ow || height != oh) {
82-
onMeasure(width, height);
87+
parent.measureChild(this);
88+
while (measureChildren()) {
8389
measure();
84-
invalidate();
8590
}
91+
measureBounds();
8692
}
8793

8894
@Override
@@ -124,23 +130,15 @@ public final View getChildAt(int x,int y){
124130
}
125131

126132
@Override
127-
protected int calculateWidth() {
128-
int width = 0;
133+
protected Size onMeasureContent() {
134+
Size size = new Size();
129135
for (View child: children){
130136
int x = Math.max(0, getChildLocation(child).x - getPaddingLeft());
131-
width = Math.max(x+child.getMeasuredWidth(), width);
132-
}
133-
return width + getPaddingLeft() + getPaddingRight();
134-
}
135-
136-
@Override
137-
protected int calculateHeight() {
138-
int height = 0;
139-
for (View child: children){
137+
size.width = Math.max(x + child.getMeasuredWidth(), size.width);
140138
int y = Math.max(0, getChildLocation(child).y - getPaddingTop());
141-
height = Math.max(y+child.getMeasuredHeight(), height);
139+
size.height = Math.max(y+child.getMeasuredHeight(), size.height);
142140
}
143-
return height + getPaddingTop() + getPaddingBottom();
141+
return size;
144142
}
145143

146144
public int getChildrenCount(){

src/br/nullexcept/mux/widget/EditText.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,10 @@ public void setSingleLine(boolean singleLine) {
136136
}
137137

138138
@Override
139-
protected int calculateHeight() {
140-
int lineCount = Math.max(1, text.getLineCount());
141-
return (int) ((lineCount * font().getLineHeight()) + getPaddingTop() + getPaddingBottom());
142-
}
139+
protected Size onMeasureContent() {
140+
Size size = new Size();
141+
size.height = Math.round(font().getLineHeight()* Math.max(1, text.getLineCount()));
143142

144-
@Override
145-
protected int calculateWidth() {
146143
int width = 0;
147144
int line = 0;
148145
for (int i = 0; i < text.getLineCount(); i++) {
@@ -157,7 +154,9 @@ protected int calculateWidth() {
157154
width += font().measureChar(text.charAt(i));
158155
}
159156

160-
return width + getPaddingLeft() + getPaddingRight();
157+
size.width = width;
158+
159+
return size;
161160
}
162161

163162
public CharSequence getText() {

src/br/nullexcept/mux/widget/ImageView.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package br.nullexcept.mux.widget;
22

33
import br.nullexcept.mux.app.Context;
4-
import br.nullexcept.mux.graphics.Canvas;
5-
import br.nullexcept.mux.graphics.Drawable;
6-
import br.nullexcept.mux.graphics.Paint;
7-
import br.nullexcept.mux.graphics.Rect;
4+
import br.nullexcept.mux.graphics.*;
85
import br.nullexcept.mux.res.AttributeList;
96
import br.nullexcept.mux.view.AttrList;
107
import br.nullexcept.mux.view.Gravity;
@@ -54,12 +51,11 @@ public void onDraw(Canvas canvas) {
5451
}
5552

5653
@Override
57-
protected int calculateHeight() {
58-
return getPaddingBottom() + getPaddingTop() + image.getHeight();
59-
}
60-
61-
@Override
62-
protected int calculateWidth() {
63-
return getPaddingLeft() + getPaddingRight() + image.getWidth();
54+
protected Size onMeasureContent() {
55+
if (image != null) {
56+
return new Size(image.getWidth(), image.getHeight());
57+
} else {
58+
return super.onMeasureContent();
59+
}
6460
}
6561
}

src/br/nullexcept/mux/widget/ScrollView.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,6 @@ protected void onMeasure(int width, int height) {
152152
measureContent();
153153
}
154154

155-
@Override
156-
protected int calculateHeight() {
157-
return getPaddingTop() + getPaddingBottom();
158-
}
159-
160-
@Override
161-
protected int calculateWidth() {
162-
return getPaddingLeft() + getPaddingRight();
163-
}
164-
165155
private class ScrollContainer extends ViewGroup {
166156
public ScrollContainer(Context context) {
167157
super(context);

src/br/nullexcept/mux/widget/TextView.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import br.nullexcept.mux.graphics.Canvas;
55
import br.nullexcept.mux.graphics.Paint;
66
import br.nullexcept.mux.graphics.Rect;
7+
import br.nullexcept.mux.graphics.Size;
78
import br.nullexcept.mux.graphics.fonts.FontMetrics;
89
import br.nullexcept.mux.res.AttributeList;
910
import br.nullexcept.mux.view.Gravity;
@@ -29,18 +30,14 @@ public TextView(Context context, AttributeList attrs) {
2930
}
3031

3132
@Override
32-
protected int calculateWidth() {
33+
protected Size onMeasureContent() {
34+
Size size = new Size();
3335
FontMetrics metrics = paint.getFontMetrics();
34-
int width = 0;
3536
for (String line : lines){
36-
width = Math.max((int) metrics.measureText(line), width);
37+
size.width = Math.max((int) metrics.measureText(line), size.width);
3738
}
38-
return width + getPaddingLeft() + getPaddingRight();
39-
}
40-
41-
@Override
42-
protected int calculateHeight() {
43-
return (int) (paint.getFontMetrics().getLineHeight() * lines.length) + getPaddingTop() + getPaddingBottom();
39+
size.height = Math.round(paint.getFontMetrics().getLineHeight()*lines.length);
40+
return size;
4441
}
4542

4643
public void setTextColor(int color){

0 commit comments

Comments
 (0)