浏览代码

Merge pull request #1383 from KellyThomas/custom_gui_controls

c sharp samples for custom GUI controls
Max Hilbrunner 7 年之前
父节点
当前提交
cef5010d76
共有 1 个文件被更改,包括 104 次插入5 次删除
  1. 104 5
      tutorials/gui/custom_gui_controls.rst

+ 104 - 5
tutorials/gui/custom_gui_controls.rst

@@ -42,7 +42,8 @@ indicate that this is the currently focused control. To check for this
 status, the :ref:`Control.has_focus() <class_Control_has_focus>` method
 exists. Example
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
     func _draw():
         if has_focus():
@@ -50,6 +51,20 @@ exists. Example
         else:
              draw_normal()
 
+ .. code-tab:: csharp
+
+    public override void _Draw()
+    {
+        if(HasFocus())
+        {
+            DrawSelected()
+        }
+        else 
+        {
+            DrawNormal();
+        }
+    }
+
 Sizing
 ------
 
@@ -65,18 +80,34 @@ To provide this callback, just override
 :ref:`Control.get_minimum_size() <class_Control_get_minimum_size>`,
 for example:
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
     func get_minimum_size(): 
         return Vector2(30, 30)
 
+ .. code-tab:: csharp
+
+    public override Vector2 _GetMinimumSize()
+    {
+        return new Vector2(20, 20);
+    }
+
 Or alternatively, set it via function:
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
     func _ready():
         set_custom_minimum_size(Vector2(30, 30))
 
+ .. code-tab:: csharp
+
+    public override void _Ready()
+    {
+        SetCustomMinimumSize(new Vector2(20, 20));
+    }
+
 Input
 -----
 
@@ -100,7 +131,8 @@ This function is
 :ref:`Control._gui_input() <class_Control__gui_input>`.
 Simply override it in your control. No processing needs to be set.
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
     extends Control
 
@@ -108,6 +140,30 @@ Simply override it in your control. No processing needs to be set.
        if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
            print("Left mouse button was pressed!")
 
+ .. code-tab:: csharp
+
+    public override void _GuiInput(InputEvent @event)
+    {
+        var mouseButtonEvent = @event as InputEventMouseButton;
+        if (mouseButtonEvent != null)
+        {
+            if (mouseButtonEvent.ButtonIndex == (int)ButtonList.Left && mouseButtonEvent.Pressed)
+            {
+                GD.Print("Left mouse button was pressed!");
+            }
+        }
+    }
+
+    // or alternatively when using C# 7 or greater we can use pattern matching
+    public override void _GuiInput(InputEvent @event)
+    {
+        if (@event is InputEventMouseButton mbe && mbe.ButtonIndex == (int)ButtonList.Left && mbe.Pressed)
+            {
+                GD.Print("Left mouse button was pressed!");
+            }
+        }
+    }
+
 For more information about events themselves, check the :ref:`doc_inputevent`
 tutorial.
 
@@ -117,7 +173,8 @@ Notifications
 Controls also have many useful notifications for which no callback
 exists, but can be checked with the _notification callback:
 
-::
+.. tabs::
+ .. code-tab:: gdscript GDScript
 
     func _notification(what):
         match what:
@@ -141,3 +198,45 @@ exists, but can be checked with the _notification callback:
             NOTIFICATION_MODAL_CLOSED):
                 pass # for modal popups, notification
                 # that the popup was closed
+
+ .. code-tab:: csharp
+
+    public override void _Notification(int what)
+    {
+        switch(what)
+        {
+            case NotificationMouseEnter:
+                // mouse entered the area of this control
+                break; 
+
+            case NotificationMouseExit:
+                // mouse exited the area of this control
+                break;
+
+            case NotificationFocusEnter:
+                // control gained focus
+                break;
+
+            case NotificationFocusExit:
+                // control lost focus
+                break;
+
+            case NotificationThemeChanged:
+                // theme used to draw the control changed
+                // update and redraw is recommended if using a theme
+                break;
+
+            case NotificationVisibilityChanged:
+                // control became visible/invisible
+                // check new status with is_visible()
+                break;
+
+            case NotificationResized:
+                // control changed size, check new size with get_size()
+                break;
+
+            case NotificationModalClose:
+                // for modal popups, notification that the popup was closed
+                break;
+        }
+    }