
#ifndef __tmeshform_h__
#define __tmeshform_h__

#ifndef __map_actions_h__
#include "map_actions.h"
#endif

#ifndef __markers_h__
#include "markers.h"
#endif

#ifndef __mesh_h__
#include "mesh.h"
#endif

#ifndef __ttageditform_h__
#include "TTagEditForm.h"
#endif

#ifndef __vec3_h__
#include "vec3.h"
#endif

#ifndef PandaWave_XDYNARRAY
#include <XDynArray.h>
#endif



enum {
	_mesh_tool_first_index = 100,
	_mesh_tool_last_index = 105,
	
	_mesh_tool_band_grid = 100,
	_mesh_tool_band_markers = 101,
	_mesh_tool_water = 102,
	_mesh_tool_terrain = 103,
	_mesh_tool_pull_grid = 104,
	_mesh_tool_smooth_grid = 105
};


class TMeshForm: public TTagEditForm {
friend class TMeshView;
public:
						TMeshForm( struct tag_entry *e );
	virtual				~TMeshForm();
			
	DECLAREDISPATCH
	virtual long		DoControlClick( long arg, void *ptr );
	virtual long		DoKeyClick( long arg, void *ptr );
	virtual long		DoPopValue( long arg, void *ptr );
	virtual long		DoListValue( long arg, void *ptr );
	virtual long		DoKeyEvent( long arg, void *ptr );
	virtual long		DoGetMenuStatus(long arg, void *ptr);
	virtual long		DoMenuCommand(long arg, void *);
	virtual long		DoSliderValue(long arg, void *);
	
	virtual void		HandleTagChanged( tag type, tag id );
	virtual void		CommitToTag();
	virtual void		ReloadTag();
	virtual bool		NeedCommit();
	
private:
	void free_all();
	bool fModified;
	void MarkModified() { fModified = true; }
	mesh_definition		header;
	int width, height;
	
	class TMeshView *mesh_view;
	
	// the controlling values
	vec3 view_focus;
	float view_pitch, view_yaw;
	float view_dist;
	
	void UpdateViewData();
	
	// computed from above
	vec3 view_origin;
	vec3 view_forward, view_right, view_up;
	
	
	mesh_cell			*cells;
	
	mesh_cell *get_cell( int x, int y )
		{
			if (!( x >= 0 && x < width )) return 0;
			if (!( y >= 0 && y < height )) return 0;
			if (!cells) return 0;
			return cells + y * width + x;
		}
	
	XGSDynArray<marker_palette_entry> marker_palette;
	XGSDynArray<marker_data> markers;
	
	
	int currentMeshTool;
	void SetCurrentTool( int newTool );
	
	int targetTerrainType;
	void SetTargetTerrainType( int type );
	
	
	void *media_coverage_region;
	long media_coverage_region_size;
	
	void *mesh_LOD_data;
	long mesh_LOD_data_size;
	
	void *connector_data;
	long connector_data_size;
	
	void *map_action_data;
	long map_action_data_size;
	
	class XGPopControl *markerPaletteType;
	class XGListControl *markerPalette;
	int selectedPaletteType;
	int selectedPaletteEntry[NUMBER_OF_MARKER_TYPES];
	
	int next_identifier;
	
	void RefreshMarkerPalette();
	marker_data *CreateNewMarker();
	
	struct marker_palette_dlog_info {
		marker_palette_entry m;
		tag_file *f;
	};
	static long MarkerPaletteDlogProc( class XGDialog *dlog, void *data, long msg, long arg, void * /*parg*/ );
	
	void fix_vertex_media_flag( int cell_x, int cell_y );
	void menucontrol( int id, const struct XGSMenuStatus *e );
	void UpdateSoftBrush();
};

#endif
