//================================================================================================
// Plugin for EQUINOX-3D
//
// Distribute points on a Mesh.
//
// AUTHOR:  Gabor Nagy
// DATE:    2024-Oct-11 00:54:19
//
// Copyright (C) 2014-2026 by Gabor Nagy. All rights reserved.
//================================================================================================
#include <assert.h>
#include <stdio.h>

#include <ELists.h>
#include <EMalloc.h>
#include <EPlugins.h>

#include <E3D/Scene.h>

#include <E3D/UI/Panel.h>
#include <E3D/UI/Panels.h>

#include <Xe/PushButton.h>
#include <Xe/Scale.h>


char*             E_PluginInfo = "Distribute points on a Mesh";

static EPlugin*   _Plugin = NULL;

static XeDWidget	_ToolBarButtons[16];static int  _ToolBarButtons_Count = 0;


//================================================
// ToolBar icon callback handler
//================================================
static void _CB(XeWidget PW, EPointer PClientData, EPointer PCallData)
{
	const EIndex  LNumOfPoints = 100000;

	EListAI_s(, E3dObjectPtr, LObjects);EIndex  LNumOfObjects = E3dScene_GetObjects(E3d_Scene, E3dItemMESH, E3dSELECTED, FALSE,  &LObjects);if(LNumOfObjects > 0)
	{
		EIndex  LObjI = 0;for(;LObjI < LNumOfObjects; LObjI++)
		{
			E3dObject*  LObject = LObjects.Items[LObjI];

			if(EObject_IsSubClass(LObject, E3dMesh))
			{
				E3dMesh*  LMesh = (E3dMesh*)LObject;

				E3dCoordinate*  LPoints = E3dMesh_GenerateSurfacePoints(NULL, LMesh, NULL,  LNumOfPoints, 42, FALSE,  0, 0,  NULL, NULL);

				E3d_VisualizePointsWithPointSet((E3dCoord3*)LPoints, LNumOfPoints, "Points");
				EFree(LPoints);
			}
		}
		EList_FreeA_s(LObjects);
	}

	E3dScene*  LScene = E3d_Scene;
	E3dWindows_RedrawByChangeFlags(LScene, E3dChgPointSetALL);
}


//========================================
// Entry point of the plugin
//========================================
int E_PluginInit(EPlugin* PPlugin)
{
	_Plugin = PPlugin;

// Use default plugin icon
//
	XePushButton_AddImg3(_ToolBarButtons[_ToolBarButtons_Count++], Plugin, "Tool->Create", "Points", '\0', NULL, NULL, FALSE, "Distribute points on a Mesh", _CB, (EPointer)0);

	return(E_SUCCESS);
}


//========================================
// Exit method of the plugin
//========================================
int E_PluginExit(const EBool PExiting)
{
	if(!PExiting || Xe_GUIFreeOnExit)
	{
		int  LI = 0;for(;LI < _ToolBarButtons_Count;LI++) { XeItemFree(_ToolBarButtons[LI]); }
		_ToolBarButtons_Count = 0;
	}

	return(E_SUCCESS);
}
