From 941df8099fe5fa350cfb22791e23dedfce722430 Mon Sep 17 00:00:00 2001 From: Kelly thomas Date: Sat, 21 Apr 2018 01:30:59 +0800 Subject: [PATCH] c sharp samples for custom GUI controls --- tutorials/gui/custom_gui_controls.rst | 109 ++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 5 deletions(-) diff --git a/tutorials/gui/custom_gui_controls.rst b/tutorials/gui/custom_gui_controls.rst index 884052805..f3882867a 100644 --- a/tutorials/gui/custom_gui_controls.rst +++ b/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() ` 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() `, 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() `. 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; + } + }