Files
recastnavigation/RecastDemo/Include/Sample.h
2025-03-31 01:02:50 -04:00

194 lines
5.6 KiB
C++

//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#pragma once
#include <cstdint>
#include "SampleInterfaces.h"
class Sample;
class InputGeom;
class dtNavMesh;
class dtNavMeshQuery;
class dtCrowd;
/// Tool types.
enum class SampleToolType : uint8_t
{
NONE = 0,
TILE_EDIT,
TILE_HIGHLIGHT,
TEMP_OBSTACLE,
NAVMESH_TESTER,
NAVMESH_PRUNE,
OFFMESH_CONNECTION,
CONVEX_VOLUME,
CROWD,
MAX_TOOLS
};
/// These are just sample areas to use consistent values across the samples.
/// The use should specify these base on his needs.
enum SamplePolyAreas
{
SAMPLE_POLYAREA_GROUND,
SAMPLE_POLYAREA_WATER,
SAMPLE_POLYAREA_ROAD,
SAMPLE_POLYAREA_DOOR,
SAMPLE_POLYAREA_GRASS,
SAMPLE_POLYAREA_JUMP
};
enum SamplePolyFlags
{
SAMPLE_POLYFLAGS_WALK = 1 << 0, //0x01, // Ability to walk (ground, grass, road)
SAMPLE_POLYFLAGS_SWIM = 1 << 1, //0x02, // Ability to swim (water).
SAMPLE_POLYFLAGS_DOOR = 1 << 2, //0x04, // Ability to move through doors.
SAMPLE_POLYFLAGS_JUMP = 1 << 3, //0x08, // Ability to jump.
SAMPLE_POLYFLAGS_DISABLED = 1 << 4, //0x10, // Disabled polygon
SAMPLE_POLYFLAGS_ALL = ~0 //0xffff // All abilities.
};
enum SamplePartitionType
{
SAMPLE_PARTITION_WATERSHED,
SAMPLE_PARTITION_MONOTONE,
SAMPLE_PARTITION_LAYERS
};
class SampleDebugDraw : public DebugDrawGL
{
public:
virtual unsigned int areaToCol(unsigned int area);
};
struct SampleTool
{
virtual ~SampleTool();
virtual SampleToolType type() = 0;
virtual void init(Sample* sample) = 0;
virtual void reset() = 0;
virtual void handleMenu() = 0;
virtual void handleClick(const float* s, const float* p, bool shift) = 0;
virtual void handleRender() = 0;
virtual void handleRenderOverlay(double* proj, double* model, int* view) = 0;
virtual void handleToggle() = 0;
virtual void handleStep() = 0;
virtual void handleUpdate(float dt) = 0;
};
struct SampleToolState {
virtual ~SampleToolState();
virtual void init(Sample* sample) = 0;
virtual void reset() = 0;
virtual void handleRender() = 0;
virtual void handleRenderOverlay(double* proj, double* model, int* view) = 0;
virtual void handleUpdate(float dt) = 0;
};
class Sample
{
protected:
InputGeom* m_inputGeometry;
dtNavMesh* m_navMesh;
dtNavMeshQuery* m_navQuery;
dtCrowd* m_crowd;
unsigned char m_navMeshDrawFlags;
float m_cellSize;
float m_cellHeight;
float m_agentHeight;
float m_agentRadius;
float m_agentMaxClimb;
float m_agentMaxSlope;
float m_regionMinSize;
float m_regionMergeSize;
float m_edgeMaxLen;
float m_edgeMaxError;
float m_vertsPerPoly;
float m_detailSampleDist;
float m_detailSampleMaxError;
int m_partitionType;
bool m_filterLowHangingObstacles;
bool m_filterLedgeSpans;
bool m_filterWalkableLowHeightSpans;
SampleTool* m_tool;
SampleToolState* m_toolStates[static_cast<unsigned long>(SampleToolType::MAX_TOOLS)];
BuildContext* m_buildContext;
SampleDebugDraw m_debugDraw;
dtNavMesh* loadAll(const char* path);
void saveAll(const char* path, const dtNavMesh* mesh);
public:
Sample();
virtual ~Sample();
Sample(const Sample&) = delete;
Sample(const Sample&&) = delete;
Sample& operator=(const Sample&) = delete;
Sample& operator=(const Sample&&) = delete;
void setContext(BuildContext* ctx) { m_buildContext = ctx; }
void setTool(SampleTool* tool);
SampleToolState* getToolState(const int type) const { return m_toolStates[type]; }
void setToolState(const int type, SampleToolState* s) { m_toolStates[type] = s; }
SampleDebugDraw& getDebugDraw() { return m_debugDraw; }
virtual void handleSettings();
virtual void handleTools();
virtual void handleDebugMode();
virtual void handleClick(const float* s, const float* p, bool shift);
virtual void handleToggle();
virtual void handleStep();
virtual void handleRender();
virtual void handleRenderOverlay(double* proj, double* model, int* view);
virtual void handleMeshChanged(InputGeom* geom);
virtual bool handleBuild();
virtual void handleUpdate(float dt);
virtual void collectSettings(struct BuildSettings& settings);
virtual InputGeom* getInputGeom() { return m_inputGeometry; }
virtual dtNavMesh* getNavMesh() { return m_navMesh; }
virtual dtNavMeshQuery* getNavMeshQuery() { return m_navQuery; }
virtual dtCrowd* getCrowd() { return m_crowd; }
virtual float getAgentRadius() { return m_agentRadius; }
virtual float getAgentHeight() { return m_agentHeight; }
virtual float getAgentClimb() { return m_agentMaxClimb; }
unsigned char getNavMeshDrawFlags() const { return m_navMeshDrawFlags; }
void setNavMeshDrawFlags(unsigned char flags) { m_navMeshDrawFlags = flags; }
void updateToolStates(float dt);
void initToolStates(Sample* sample);
void resetToolStates();
void renderToolStates();
void renderOverlayToolStates(double* proj, double* model, int* view);
void resetCommonSettings();
void handleCommonSettings();
};