Browse Source

Added ListBox events

Marko Pintera 12 years ago
parent
commit
2c6fd54cb4

+ 0 - 2
CSharpWrap.txt

@@ -115,8 +115,6 @@ ScrollArea will likely need:
  - Implement C# Font
  - Implement C# SpriteTexture
 
- - Need a way to update Listbox after it has been created (C++ and C#)
-
 When loading resources I need to be able to load both project resources and engine ones. 
  - BuiltinResources can be used for accessing default resources like GUI skin textures and similar
  - They're all loaded from a special folder that is set in EditorApplication

+ 10 - 0
MBansheeEngine/GUIListBox.cs

@@ -5,6 +5,10 @@ namespace BansheeEngine
 {
     public sealed class GUIListBox : ScriptObject
     {
+        public delegate void OnSelectionChangedDelegate(int index);
+
+        public event OnSelectionChangedDelegate OnSelectionChanged;
+
         internal GUIListBox(GUILayout parentLayout, LocString[] elements, GUIElementStyle style, params GUIOption[] options)
         {
             Internal_CreateInstance(this, parentLayout, elements, style, options);
@@ -15,6 +19,12 @@ namespace BansheeEngine
             Internal_SetElements(mCachedPtr, elements);
         }
 
+        private void DoOnSelectionChanged(int index)
+        {
+            if (OnSelectionChanged != null)
+                OnSelectionChanged(index);
+        }
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_CreateInstance(GUIListBox instance, GUILayout parentLayout, LocString[] elements, GUIElementStyle style, params GUIOption[] options);
 

+ 5 - 1
SBansheeEngine/Include/BsScriptGUIListBox.h

@@ -17,13 +17,17 @@ namespace BansheeEngine
 	private:
 		static void internal_createInstance(MonoObject* instance, MonoObject* parentLayout, MonoArray* elements, MonoObject* style, MonoArray* guiOptions);
 		static void internal_destroyInstance(ScriptGUIListBox* nativeInstance);
-
 		static void internal_setElements(ScriptGUIListBox* nativeInstance, MonoArray* elements);
 
 		static void initRuntimeData();
 
+		static void onSelectionChanged(MonoObject* instance, CM::UINT32 index);
+
 		ScriptGUIListBox(GUIListBox* listBox);
 
 		GUIListBox* mListBox;
+
+		typedef void (__stdcall *OnSelectionChangedThunkDef) (MonoObject*, CM::UINT32, MonoException**);
+		static OnSelectionChangedThunkDef onSelectionChangedThunk;
 	};
 }

+ 15 - 0
SBansheeEngine/Source/BsScriptGUIListBox.cpp

@@ -2,6 +2,7 @@
 #include "BsScriptMeta.h"
 #include "BsMonoField.h"
 #include "BsMonoClass.h"
+#include "BsMonoMethod.h"
 #include "BsMonoManager.h"
 #include "BsSpriteTexture.h"
 #include "BsMonoUtil.h"
@@ -17,6 +18,8 @@ using namespace CamelotFramework;
 
 namespace BansheeEngine
 {
+	ScriptGUIListBox::OnSelectionChangedThunkDef ScriptGUIListBox::onSelectionChangedThunk;
+
 	ScriptGUIListBox::ScriptGUIListBox(GUIListBox* listBox)
 		:mListBox(listBox)
 	{
@@ -35,6 +38,8 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBox::internal_createInstance);
 		metaData.scriptClass->addInternalCall("Internal_DestroyInstance", &ScriptGUIListBox::internal_destroyInstance);
 		metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBox::internal_setElements);
+
+		onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1).getThunk();
 	}
 
 	void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoObject* parentLayout, MonoArray* elements, MonoObject* style, MonoArray* guiOptions)
@@ -67,6 +72,8 @@ namespace BansheeEngine
 		}
 
 		GUIListBox* guiListBox = GUIListBox::create(scriptLayout->getParentWidget(), nativeElements, options, elemStyle);
+		guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
+
 		GUILayout* nativeLayout = scriptLayout->getInternalValue();
 		nativeLayout->addElement(guiListBox);
 
@@ -101,4 +108,12 @@ namespace BansheeEngine
 
 		nativeInstance->getInternalValue()->setElements(nativeElements);
 	}
+
+	void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, CM::UINT32 index)
+	{
+		MonoException* exception = nullptr;
+		onSelectionChangedThunk(instance, index, &exception);
+
+		MonoUtil::throwIfException(exception);
+	}
 }