Browse code

add onLose/GainFocus callback to Widget

devnewton authored on 17/05/2015 at 16:28:07
Showing 16 changed files
... ...
@@ -4,7 +4,7 @@
4 4
     <parent>
5 5
         <groupId>im.bci</groupId>
6 6
         <artifactId>jnuit</artifactId>
7
-        <version>0.12</version>
7
+        <version>0.13-SNAPSHOT</version>
8 8
         <relativePath>../pom.xml</relativePath>
9 9
     </parent>
10 10
     <groupId>im.bci</groupId>
... ...
@@ -24,7 +24,7 @@ THE SOFTWARE. --><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h
24 24
     <parent>
25 25
         <groupId>im.bci</groupId>
26 26
         <artifactId>jnuit</artifactId>
27
-        <version>0.12</version>
27
+        <version>0.13-SNAPSHOT</version>
28 28
         <relativePath>../pom.xml</relativePath>
29 29
     </parent>
30 30
     <artifactId>jnuit-artemis</artifactId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -27,114 +27,123 @@ import im.bci.jnuit.visitors.WidgetVisitor;
27 27
 
28 28
 /**
29 29
  * Widget container with free (fixed position) layout.
30
+ * 
30 31
  * @author devnewton
31 32
  */
32 33
 public class Container extends Widget {
33 34
 
34
-    private Widget focusedChild;
35
-
36
-    @Override
37
-    public Widget getFocusedChild() {
38
-        if (null == focusedChild) {
39
-            focusedChild = getTopLeftFocusableChild();
40
-        }
41
-        return focusedChild;
42
-    }
43
-
44
-    @Override
45
-    public void onLeft() {
46
-        if (isFocusSucked()) {
47
-            super.onLeft();
48
-        } else {
49
-            final Widget currentFocusedChild = getFocusedChild();
50
-            Widget closest = findClosestLeftFocusableWidget(currentFocusedChild);
51
-            if (null != closest) {
52
-                setFocusedChild(closest);
53
-            }
54
-        }
55
-    }
56
-
57
-    public boolean isFocusSucked() {
58
-        final Widget currentFocusedChild = getFocusedChild();
59
-        return null == currentFocusedChild || currentFocusedChild.isSuckingFocus();
60
-    }
61
-
62
-    @Override
63
-    public void onRight() {
64
-        if (isFocusSucked()) {
65
-            super.onRight();
66
-        } else {
67
-            final Widget currentFocusedChild = getFocusedChild();
68
-            Widget closest = findClosestRightFocusableWidget(currentFocusedChild);
69
-            if (null != closest) {
70
-                setFocusedChild(closest);
71
-            }
72
-        }
73
-    }
74
-
75
-    @Override
76
-    public void onUp() {
77
-        if (isFocusSucked()) {
78
-            super.onUp();
79
-        } else {
80
-            final Widget currentFocusedChild = getFocusedChild();
81
-            Widget closest = findClosestUpFocusableWidget(currentFocusedChild);
82
-            if (null != closest) {
83
-                setFocusedChild(closest);
84
-            }
85
-        }
86
-    }
87
-
88
-    @Override
89
-    public void onDown() {
90
-        if (isFocusSucked()) {
91
-            super.onDown();
92
-        } else {
93
-            final Widget currentFocusedChild = getFocusedChild();
94
-            Widget closest = findClosestDownFocusableWidget(currentFocusedChild);
95
-            if (null != closest) {
96
-                setFocusedChild(closest);
97
-            }
98
-        }
99
-    }
100
-
101
-    @Override
102
-    public void onOK() {
103
-        final Widget currentFocusedChild = getFocusedChild();
104
-        if (null != currentFocusedChild) {
105
-            if (currentFocusedChild.isFocusWhore() && !currentFocusedChild.isSuckingFocus()) {
106
-                currentFocusedChild.suckFocus();
107
-                return;
108
-            }
109
-        }
110
-        super.onOK();
111
-    }
112
-
113
-    @Override
114
-    public void onCancel() {
115
-        if (isFocusSucked()) {
116
-            super.onCancel();
117
-        }
118
-    }
119
-
120
-    @Override
121
-    public void onMouseMove(float mouseX, float mouseY) {
122
-        for (Widget child : getChildren()) {
123
-            if (mouseX >= child.getX() && mouseX <= (child.getX() + child.getWidth()) && mouseY >= child.getY() && mouseY <= (child.getY() + child.getHeight())) {
124
-                if (child.isFocusable() && !isFocusSucked()) {
125
-                    setFocusedChild(child);
126
-                }
127
-                child.onMouseMove(mouseX, mouseY);
128
-            }
129
-        }
130
-    }
131
-
132
-    public void setFocusedChild(Widget focusedChild) {
133
-        this.focusedChild = focusedChild;
134
-    }
135
-
136
-    @Override
137
-    public void accept(WidgetVisitor visitor) {
138
-        visitor.visit(this);
139
-    }
35
+	private Widget focusedChild;
36
+
37
+	@Override
38
+	public Widget getFocusedChild() {
39
+		if (null == focusedChild) {
40
+			focusedChild = getTopLeftFocusableChild();
41
+		}
42
+		return focusedChild;
43
+	}
44
+
45
+	@Override
46
+	public void onLeft() {
47
+		if (isFocusSucked()) {
48
+			super.onLeft();
49
+		} else {
50
+			final Widget currentFocusedChild = getFocusedChild();
51
+			Widget closest = findClosestLeftFocusableWidget(currentFocusedChild);
52
+			if (null != closest) {
53
+				setFocusedChild(closest);
54
+			}
55
+		}
56
+	}
57
+
58
+	public boolean isFocusSucked() {
59
+		final Widget currentFocusedChild = getFocusedChild();
60
+		return null == currentFocusedChild || currentFocusedChild.isSuckingFocus();
61
+	}
62
+
63
+	@Override
64
+	public void onRight() {
65
+		if (isFocusSucked()) {
66
+			super.onRight();
67
+		} else {
68
+			final Widget currentFocusedChild = getFocusedChild();
69
+			Widget closest = findClosestRightFocusableWidget(currentFocusedChild);
70
+			if (null != closest) {
71
+				setFocusedChild(closest);
72
+			}
73
+		}
74
+	}
75
+
76
+	@Override
77
+	public void onUp() {
78
+		if (isFocusSucked()) {
79
+			super.onUp();
80
+		} else {
81
+			final Widget currentFocusedChild = getFocusedChild();
82
+			Widget closest = findClosestUpFocusableWidget(currentFocusedChild);
83
+			if (null != closest) {
84
+				setFocusedChild(closest);
85
+			}
86
+		}
87
+	}
88
+
89
+	@Override
90
+	public void onDown() {
91
+		if (isFocusSucked()) {
92
+			super.onDown();
93
+		} else {
94
+			final Widget currentFocusedChild = getFocusedChild();
95
+			Widget closest = findClosestDownFocusableWidget(currentFocusedChild);
96
+			if (null != closest) {
97
+				setFocusedChild(closest);
98
+			}
99
+		}
100
+	}
101
+
102
+	@Override
103
+	public void onOK() {
104
+		final Widget currentFocusedChild = getFocusedChild();
105
+		if (null != currentFocusedChild) {
106
+			if (currentFocusedChild.isFocusWhore() && !currentFocusedChild.isSuckingFocus()) {
107
+				currentFocusedChild.suckFocus();
108
+				return;
109
+			}
110
+		}
111
+		super.onOK();
112
+	}
113
+
114
+	@Override
115
+	public void onCancel() {
116
+		if (isFocusSucked()) {
117
+			super.onCancel();
118
+		}
119
+	}
120
+
121
+	@Override
122
+	public void onMouseMove(float mouseX, float mouseY) {
123
+		for (Widget child : getChildren()) {
124
+			if (mouseX >= child.getX() && mouseX <= (child.getX() + child.getWidth()) && mouseY >= child.getY() && mouseY <= (child.getY() + child.getHeight())) {
125
+				if (child.isFocusable() && !isFocusSucked()) {
126
+					setFocusedChild(child);
127
+				}
128
+				child.onMouseMove(mouseX, mouseY);
129
+			}
130
+		}
131
+	}
132
+
133
+	public void setFocusedChild(Widget focusedChild) {
134
+		if (this.focusedChild != focusedChild) {
135
+			if (null != this.focusedChild) {
136
+				this.focusedChild.onLoseFocus();
137
+			}
138
+			this.focusedChild = focusedChild;
139
+			if (null != focusedChild) {
140
+				focusedChild.onGainFocus();
141
+			}
142
+		}
143
+	}
144
+
145
+	@Override
146
+	public void accept(WidgetVisitor visitor) {
147
+		visitor.visit(this);
148
+	}
140 149
 }
... ...
@@ -462,4 +462,10 @@ public abstract class Widget {
462 462
     public void setTextColor(TextColor textColor) {
463 463
         this.textColor = textColor;
464 464
     }
465
+
466
+	public void onLoseFocus() {
467
+	}
468
+
469
+	public void onGainFocus() {
470
+	}
465 471
 }
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -29,7 +29,7 @@ THE SOFTWARE. -->
29 29
         <version>9</version>
30 30
     </parent>
31 31
     <artifactId>jnuit</artifactId>
32
-    <version>0.12</version>
32
+    <version>0.13-SNAPSHOT</version>
33 33
     <name>jnuit</name>
34 34
     <packaging>pom</packaging>
35 35
     <description>Simple gui toolkit for lwjgl or playn videogames.</description>
... ...
@@ -24,7 +24,7 @@ THE SOFTWARE. --><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h
24 24
     <parent>
25 25
         <groupId>im.bci</groupId>
26 26
         <artifactId>jnuit</artifactId>
27
-        <version>0.12</version>
27
+        <version>0.13-SNAPSHOT</version>
28 28
         <relativePath>../pom.xml</relativePath>
29 29
     </parent>
30 30
     <artifactId>jnuit-samples</artifactId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>
... ...
@@ -25,7 +25,7 @@ THE SOFTWARE. -->
25 25
     <parent>
26 26
         <groupId>im.bci</groupId>
27 27
         <artifactId>jnuit</artifactId>
28
-        <version>0.12</version>
28
+        <version>0.13-SNAPSHOT</version>
29 29
         <relativePath>../pom.xml</relativePath>
30 30
     </parent>
31 31
     <groupId>im.bci</groupId>