Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <Siv3D.hpp>
- //// 定数
- const double nodeCircleR = 50;
- const double nodeDist = 10;
- const int32 defaultEdgeCost = 10;
- const double edgeThickness = 2.5;
- const double infoRectWidth = 150;
- const double infoRectHeight = 55;
- //// 構造体
- // 頂点データ
- typedef struct nodeData
- {
- size_t index;
- String name;
- double x, y;
- } nodeData_s;
- // 辺データ
- typedef struct edgeData
- {
- size_t from, to;
- int32 cost;
- } edgeData_s;
- //// グローバル変数
- Array<nodeData>nodes;
- Array<edgeData>edges;
- size_t selectedMode = 0;
- Font mainFont, detailFont;
- TextEditState text(U"");
- Rect enterRect;
- //// 初期化
- void init()
- {
- mainFont = Font(28);
- detailFont = Font(18);
- enterRect = Rect(Window::Width() - 65, Window::Height() - 50, 50, 35);
- }
- //// 更新
- // 頂点
- size_t fromNodeIdx = -1, toNodeIdx = -1;
- void updateNodes()
- {
- bool textMouseoverFlag = Rect(Window::Width() - 270, Window::Height() - 50, 200, 50).mouseOver();
- bool statsMouseoverFlag = Rect(Window::Width() - 95, 10, 90, 150).mouseOver();
- if (!textMouseoverFlag && !enterRect.mouseOver() && !statsMouseoverFlag && MouseL.up())
- {
- bool erasedFlag = false;
- for (auto index : step(nodes.size()))
- {
- auto node = nodes[index];
- if (Circle(node.x, node.y, nodeCircleR).mouseOver())
- {
- nodes.erase(nodes.begin() + index);
- for (auto idx = index; idx < nodes.size(); ++idx)
- {
- nodes[idx].index = idx;
- nodes[idx].name = U"頂点 " + ToString(idx);
- }
- erasedFlag = true;
- break;
- }
- }
- if (!erasedFlag)
- {
- nodeData_s newNodeData;
- newNodeData.index = nodes.size();
- newNodeData.name = U"頂点 " + ToString(nodes.size());
- newNodeData.x = Cursor::Pos().x;
- newNodeData.y = Cursor::Pos().y;
- bool flag = true;
- for (auto node : nodes)
- {
- auto dist = (newNodeData.x - node.x) * (newNodeData.x - node.x) + (newNodeData.y - node.y) * (newNodeData.y - node.y);
- if (dist <= (nodeDist + nodeCircleR * 2) * (nodeDist + nodeCircleR * 2))
- {
- flag = false;
- break;
- }
- }
- if (flag) nodes << newNodeData;
- }
- }
- }
- // 辺
- void updateEdges()
- {
- if (MouseL.up())
- {
- for (auto index : step(nodes.size()))
- {
- auto node = nodes[index];
- if (Circle(node.x, node.y, nodeCircleR).mouseOver())
- {
- if (fromNodeIdx == -1) fromNodeIdx = index;
- else if (fromNodeIdx == index) fromNodeIdx = -1;
- else
- {
- toNodeIdx = index;
- edgeData_s newEdgeData;
- newEdgeData.from = fromNodeIdx;
- newEdgeData.to = toNodeIdx;
- newEdgeData.cost = defaultEdgeCost;
- edges << newEdgeData;
- fromNodeIdx = toNodeIdx = -1;
- }
- break;
- }
- }
- }
- }
- // 計算
- void updateCalc()
- {
- }
- void update()
- {
- // 頂点
- if (selectedMode == 0) updateNodes();
- // 辺
- if (selectedMode == 1) updateEdges();
- // 計算
- if (selectedMode == 2) updateCalc();
- }
- //// 描画
- // ステータス
- Array<String> modeOptions = { U"頂点", U"辺", U"計算" };
- void drawStats()
- {
- mainFont(ToString(nodes.size()) + U" nodes / " + ToString(edges.size()) + U" edges").draw(10, 10);
- SimpleGUI::RadioButtons(selectedMode, modeOptions, Vec2(Window::Width() - 95, 10));
- SimpleGUI::TextBox(text, Vec2(Window::Width() - 270, Window::Height() - 50));
- enterRect.draw();
- }
- // 頂点
- void drawNodes()
- {
- size_t index = 0;
- for (auto node : nodes)
- {
- auto circle = Circle(node.x, node.y, nodeCircleR);
- circle.draw((index == fromNodeIdx ? Palette::Orange : Palette::White));
- detailFont(node.name).drawAt(node.x, node.y, Palette::Black);
- ++index;
- }
- for (auto node : nodes)
- {
- if (Circle(node.x, node.y, nodeCircleR).mouseOver())
- {
- auto infoRect = Rect(Cursor::Pos().x + 10, Cursor::Pos().y + 10, infoRectWidth, infoRectHeight);
- infoRect.draw();
- detailFont(U"index : " + ToString(node.index)).draw(infoRect.x + 5, infoRect.y + 5, Palette::Black);
- detailFont(U"name : " + node.name).draw(infoRect.x + 5, infoRect.y + 25, Palette::Black);
- }
- }
- }
- // 辺
- void drawEdges()
- {
- for (auto edge : edges)
- {
- auto line = Line(nodes[edge.from].x, nodes[edge.from].y, nodes[edge.to].x, nodes[edge.to].y);
- line.draw(edgeThickness);
- }
- }
- // 計算
- void drawCalc()
- {
- }
- void draw()
- {
- // ステータス
- drawStats();
- // 辺
- drawEdges();
- // 頂点
- drawNodes();
- // 計算
- drawCalc();
- }
- void Main()
- {
- // 初期化
- init();
- // メインループ
- while (System::Update())
- {
- // 更新
- update();
- // 描画
- draw();
- }
- }
Add Comment
Please, Sign In to add comment