浏览代码

Update button.ts

Added static "CreateImageWithCenterTextButton" with centered text and full-size image background.
Which i think is a very common button setup in any game :)

I used zIndex to render text infront of image instead of adding it as second control.
this way we keep consistency with other text buttons, and can change .text value in the same way & child id, e.g. button.children[0].text

https://www.babylonjs-playground.com/#XCPP9Y#288
aWeirdo 7 年之前
父节点
当前提交
5732dc81ca
共有 1 个文件被更改,包括 19 次插入1 次删除
  1. 19 1
      gui/src/controls/button.ts

+ 19 - 1
gui/src/controls/button.ts

@@ -136,5 +136,23 @@ module BABYLON.GUI {
 
 
             return result;
             return result;
         }
         }
+        
+        public static CreateImageWithCenterTextButton(name: string, text: string, imageUrl: string): Button {
+            var result = new Button(name);
+
+            // Adding text
+            var textBlock = new BABYLON.GUI.TextBlock(name + "_button", text);
+            textBlock.textWrapping = true;
+            textBlock.zIndex = 10; 
+		    textBlock.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
+            result.addControl(textBlock);   
+
+            // Adding image
+            var iconImage = new BABYLON.GUI.Image(name + "_icon", imageUrl);
+            iconImage.stretch = BABYLON.GUI.Image.STRETCH_FILL;
+            result.addControl(iconImage);            
+
+            return result;
+        }
     }    
     }    
-}
+}