Guest User

Untitled

a guest
Feb 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. # include <Siv3D.hpp>
  2.  
  3. //// 定数
  4. const double nodeCircleR = 50;
  5. const double nodeDist = 10;
  6. const int32 defaultEdgeCost = 10;
  7. const double edgeThickness = 2.5;
  8. const double infoRectWidth = 150;
  9. const double infoRectHeight = 55;
  10.  
  11. //// 構造体
  12. // 頂点データ
  13. typedef struct nodeData
  14. {
  15. size_t index;
  16. String name;
  17. double x, y;
  18. } nodeData_s;
  19.  
  20. // 辺データ
  21. typedef struct edgeData
  22. {
  23. size_t from, to;
  24. int32 cost;
  25. } edgeData_s;
  26.  
  27. //// グローバル変数
  28. Array<nodeData>nodes;
  29. Array<edgeData>edges;
  30. size_t selectedMode = 0;
  31. Font mainFont, detailFont;
  32. TextEditState text(U"");
  33. Rect enterRect;
  34.  
  35. //// 初期化
  36. void init()
  37. {
  38. mainFont = Font(28);
  39. detailFont = Font(18);
  40. enterRect = Rect(Window::Width() - 65, Window::Height() - 50, 50, 35);
  41. }
  42.  
  43. //// 更新
  44. // 頂点
  45. size_t fromNodeIdx = -1, toNodeIdx = -1;
  46. void updateNodes()
  47. {
  48. bool textMouseoverFlag = Rect(Window::Width() - 270, Window::Height() - 50, 200, 50).mouseOver();
  49. bool statsMouseoverFlag = Rect(Window::Width() - 95, 10, 90, 150).mouseOver();
  50. if (!textMouseoverFlag && !enterRect.mouseOver() && !statsMouseoverFlag && MouseL.up())
  51. {
  52. bool erasedFlag = false;
  53. for (auto index : step(nodes.size()))
  54. {
  55. auto node = nodes[index];
  56. if (Circle(node.x, node.y, nodeCircleR).mouseOver())
  57. {
  58. nodes.erase(nodes.begin() + index);
  59. for (auto idx = index; idx < nodes.size(); ++idx)
  60. {
  61. nodes[idx].index = idx;
  62. nodes[idx].name = U"頂点 " + ToString(idx);
  63. }
  64. erasedFlag = true;
  65. break;
  66. }
  67. }
  68.  
  69. if (!erasedFlag)
  70. {
  71. nodeData_s newNodeData;
  72. newNodeData.index = nodes.size();
  73. newNodeData.name = U"頂点 " + ToString(nodes.size());
  74. newNodeData.x = Cursor::Pos().x;
  75. newNodeData.y = Cursor::Pos().y;
  76. bool flag = true;
  77. for (auto node : nodes)
  78. {
  79. auto dist = (newNodeData.x - node.x) * (newNodeData.x - node.x) + (newNodeData.y - node.y) * (newNodeData.y - node.y);
  80. if (dist <= (nodeDist + nodeCircleR * 2) * (nodeDist + nodeCircleR * 2))
  81. {
  82. flag = false;
  83. break;
  84. }
  85. }
  86. if (flag) nodes << newNodeData;
  87. }
  88. }
  89. }
  90.  
  91. // 辺
  92. void updateEdges()
  93. {
  94. if (MouseL.up())
  95. {
  96. for (auto index : step(nodes.size()))
  97. {
  98. auto node = nodes[index];
  99. if (Circle(node.x, node.y, nodeCircleR).mouseOver())
  100. {
  101. if (fromNodeIdx == -1) fromNodeIdx = index;
  102. else if (fromNodeIdx == index) fromNodeIdx = -1;
  103. else
  104. {
  105. toNodeIdx = index;
  106. edgeData_s newEdgeData;
  107. newEdgeData.from = fromNodeIdx;
  108. newEdgeData.to = toNodeIdx;
  109. newEdgeData.cost = defaultEdgeCost;
  110. edges << newEdgeData;
  111. fromNodeIdx = toNodeIdx = -1;
  112. }
  113. break;
  114. }
  115. }
  116. }
  117. }
  118.  
  119. // 計算
  120. void updateCalc()
  121. {
  122.  
  123. }
  124.  
  125. void update()
  126. {
  127. // 頂点
  128. if (selectedMode == 0) updateNodes();
  129.  
  130. // 辺
  131. if (selectedMode == 1) updateEdges();
  132.  
  133. // 計算
  134. if (selectedMode == 2) updateCalc();
  135. }
  136.  
  137. //// 描画
  138. // ステータス
  139. Array<String> modeOptions = { U"頂点", U"辺", U"計算" };
  140. void drawStats()
  141. {
  142. mainFont(ToString(nodes.size()) + U" nodes / " + ToString(edges.size()) + U" edges").draw(10, 10);
  143. SimpleGUI::RadioButtons(selectedMode, modeOptions, Vec2(Window::Width() - 95, 10));
  144. SimpleGUI::TextBox(text, Vec2(Window::Width() - 270, Window::Height() - 50));
  145. enterRect.draw();
  146. }
  147.  
  148. // 頂点
  149. void drawNodes()
  150. {
  151. size_t index = 0;
  152. for (auto node : nodes)
  153. {
  154. auto circle = Circle(node.x, node.y, nodeCircleR);
  155. circle.draw((index == fromNodeIdx ? Palette::Orange : Palette::White));
  156. detailFont(node.name).drawAt(node.x, node.y, Palette::Black);
  157. ++index;
  158. }
  159. for (auto node : nodes)
  160. {
  161. if (Circle(node.x, node.y, nodeCircleR).mouseOver())
  162. {
  163. auto infoRect = Rect(Cursor::Pos().x + 10, Cursor::Pos().y + 10, infoRectWidth, infoRectHeight);
  164. infoRect.draw();
  165. detailFont(U"index : " + ToString(node.index)).draw(infoRect.x + 5, infoRect.y + 5, Palette::Black);
  166. detailFont(U"name : " + node.name).draw(infoRect.x + 5, infoRect.y + 25, Palette::Black);
  167. }
  168. }
  169. }
  170.  
  171. // 辺
  172. void drawEdges()
  173. {
  174. for (auto edge : edges)
  175. {
  176. auto line = Line(nodes[edge.from].x, nodes[edge.from].y, nodes[edge.to].x, nodes[edge.to].y);
  177. line.draw(edgeThickness);
  178. }
  179. }
  180.  
  181. // 計算
  182. void drawCalc()
  183. {
  184.  
  185. }
  186.  
  187. void draw()
  188. {
  189. // ステータス
  190. drawStats();
  191.  
  192. // 辺
  193. drawEdges();
  194.  
  195. // 頂点
  196. drawNodes();
  197.  
  198. // 計算
  199. drawCalc();
  200. }
  201.  
  202. void Main()
  203. {
  204. // 初期化
  205. init();
  206.  
  207. // メインループ
  208. while (System::Update())
  209. {
  210. // 更新
  211. update();
  212.  
  213. // 描画
  214. draw();
  215. }
  216. }
Add Comment
Please, Sign In to add comment