瀏覽代碼

Reworked the function handling toggle button groups

Reworked the `executeOnAllControls` function handling toggle button groups to utilize the `_getTypeName()` function. Only running if this toggle button has a group defined.
Jordan Kintzle 4 年之前
父節點
當前提交
f3681a469a
共有 1 個文件被更改,包括 15 次插入13 次删除
  1. 15 13
      gui/src/2D/controls/toggleButton.ts

+ 15 - 13
gui/src/2D/controls/toggleButton.ts

@@ -122,20 +122,22 @@ export class ToggleButton extends Rectangle {
 
         this.onIsActiveChangedObservable.notifyObservers(value);
 
-        if (this._isActive && this._host) {
-            // Update all controls from same group
+        if (this._isActive && this._host && this._group) {
+            // A toggle button in a group can only have 1 active element at a given time.
+            // If this toggle button has a group, set other toggle buttons in the group to inactive.
             this._host.executeOnAllControls((control) => {
-                if (control == this) {
-                    return;
-                }
-
-                if ((<any>control).group === undefined) {
-                    return;
-                }
-                const childToggle = <ToggleButton>control;
-                // A toggle group can only have 1 active element at a given time. So if this toggle has a group, we need to ensure other toggles in this group get set to inactive.
-                if (childToggle.group === this.group) {
-                    childToggle.isActive = false; // Set other toggles in group as inactive
+                // Check for control type ToggleButton
+                if ((<any>control)._getTypeName === "ToggleButton") {
+                    // Don't do anything to this toggle button
+                    if (control === this) {
+                        return;
+                    }
+
+                    const childToggle = <ToggleButton>control;
+                    // If toggle button is in same group, set isActive to false
+                    if (childToggle.group === this.group) {
+                        childToggle.isActive = false;
+                    }
                 }
             });
         }