Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.2 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMEDEMO2_H
  7. #define DMEDEMO2_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattributevar.h"
  13. //-----------------------------------------------------------------------------
  14. // Demo 2: Defining editable versions of the in-game classes
  15. //
  16. // This is a tricky thing to get right. You want to design for several things:
  17. // 1) Ease of data change
  18. // 2) Separation of editable state from user interface
  19. // 3) Ease of discoverability of data from just looking at the output file
  20. //-----------------------------------------------------------------------------
  21. //-----------------------------------------------------------------------------
  22. // Dme version of a quad
  23. // Very straightforward, this is identical to the in-game representation
  24. // with the exception of the 'name' attribute all DmElements have.
  25. //-----------------------------------------------------------------------------
  26. class CDmeQuadV2 : public CDmElement
  27. {
  28. DEFINE_ELEMENT( CDmeQuadV2, CDmElement );
  29. public:
  30. CDmaVar< int > m_X0;
  31. CDmaVar< int > m_Y0;
  32. CDmaVar< int > m_X1;
  33. CDmaVar< int > m_Y1;
  34. CDmaColor m_Color;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // Dme version of a list of quads
  38. // Note that we hide the list of quads here and instead provide a set of
  39. // service functions limited to the types of editing operations we expect
  40. // to perform on the quad list
  41. //
  42. // Also note when you need to edit an array of struct data,
  43. // it often results in easier to use code if you create a dme class which
  44. // represents the array of structs (CDmeQuadListV2 in this case) with utility
  45. // methods as opposed to simply using CDmaElementArray< CDmeQuadV2 > in
  46. // containing classes (CDmeQuadDocV2 in this case)
  47. //
  48. // You also want to avoid using parallel arrays of CDmaIntArrays<> etc
  49. // for each field of the struct. It sucks having to add an element into
  50. // each array every time you add a struct
  51. //-----------------------------------------------------------------------------
  52. class CDmeQuadListV2 : public CDmElement
  53. {
  54. DEFINE_ELEMENT( CDmeQuadListV2, CDmElement );
  55. public:
  56. // List management
  57. void AddQuad( CDmeQuadV2 *pQuad );
  58. CDmeQuadV2 *FindQuadByName( const char *pName );
  59. void RemoveQuad( CDmeQuadV2 *pQuad );
  60. void RemoveAllQuads();
  61. // Render order management
  62. void MoveToFront( CDmeQuadV2 *pQuad );
  63. void MoveToBack( CDmeQuadV2 *pQuad );
  64. private:
  65. CDmaElementArray< CDmeQuadV2 > m_Quads;
  66. };
  67. //-----------------------------------------------------------------------------
  68. // Dme version of a the editor 'document'
  69. //
  70. // The interface here is designed to be able to be used directly from
  71. // python. I'm currently hiding direct access to CDmeQuadV2 to here to
  72. // make python usage easier, but python can handle it if we pass CDmeQuadV2s
  73. // in the interface. We may well want to start passing them around once
  74. // we get to the VGUI-based editor.
  75. //
  76. // Early editors we wrote didn't clearly separate data from UI at the doc
  77. // level which resulted in a bunch of complexity as our tools got bigger.
  78. // Actually making a Dm element which contains a notion of selection in it
  79. // I believe will reduce this problem in the future (this is still an untested
  80. // theory in-house, although other 3rd party editors use this technique also).
  81. //
  82. // Remember that only attributes can be saved and have undo support.
  83. // If you want to add members to a Dme element which are not saved and
  84. // never need undo, you can either use normal non-CDma members,
  85. // or mark attributes to not be saved. In this case, I make the
  86. // selection state be an attribute to get undo but mark the selection
  87. // attribute to not save it to the file.
  88. //-----------------------------------------------------------------------------
  89. class CDmeQuadDocV2 : public CDmElement
  90. {
  91. DEFINE_ELEMENT( CDmeQuadDocV2, CDmElement );
  92. public:
  93. // Adds quad, resets selection to new quad
  94. void AddQuad( const char *pName, int x0, int y0, int x1, int y1 );
  95. // Clears selection
  96. void ClearSelection();
  97. // Adds quad to selection
  98. void AddQuadToSelection( const char *pName );
  99. // Deletes selected quads
  100. void DeleteSelectedQuads();
  101. // Changes quad color
  102. void SetSelectedQuadColor( int r, int g, int b, int a );
  103. // Moves quads
  104. void MoveSelectedQuads( int dx, int dy );
  105. // Resizes selected quad (works only when 1 quad is selected)
  106. void ResizeSelectedQuad( int nWidth, int nHeight );
  107. // Moves selected quad to front/back (works only when 1 quad is selected)
  108. void MoveSelectedToFront();
  109. void MoveSelectedToBack();
  110. private:
  111. CDmaElement< CDmeQuadListV2 > m_Quads;
  112. CDmaElementArray< CDmeQuadV2 > m_SelectedQuads;
  113. };
  114. //-----------------------------------------------------------------------------
  115. // Usage in python (works from the debugger!)
  116. //-----------------------------------------------------------------------------
  117. // 1) Python at commandline
  118. // 2) import vs
  119. // 3) vs.dm.SetUndoEnabled( 0 )
  120. // 4) doc = vs.CreateElement( �DmeQuadDocV2�, �root�, -1 )
  121. // 5) � doc stuff, e.g. doc.AddQuad( 'quad1', 5, 5, 30, 40 )
  122. // 6) vs.dm.SaveToFile( �file name�, ��, �keyvalues2�, �dmx�, doc )
  123. #endif // DMEDEMO2_H