From 322c13c576267647fe1538df1842e81de994ed9c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 21 Jan 2023 00:12:50 +0800 Subject: [PATCH] feat: support case skin --- src/graph/graph.cc | 110 ++++++++++++-------------------------------- src/graph/graph.h | 30 ++++++------ src/graph/svg/svg.h | 11 ++--- 3 files changed, 46 insertions(+), 105 deletions(-) diff --git a/src/graph/graph.cc b/src/graph/graph.cc index 36a6ca0..0629ecf 100644 --- a/src/graph/graph.cc +++ b/src/graph/graph.cc @@ -124,89 +124,29 @@ void Graph::svg_demo(Analyse::track_data_t track_data) { // printf("\n"); //} -//void SvgCase::render() const { -// -// std::string result; -// -// uint64_t raw_code = code; -// -//// std::cout << "start rander" << std::endl; -// -// auto tmp = SvgRect(); -// -// tmp.left = left; -// tmp.top = top; -// tmp.width = width * 4 + gap * 5; -// tmp.height = width * 5 + gap * 6; -//// std::cout << "main" << std::endl; -// tmp.to_xml(); -// -// for (int addr = 0; raw_code; ++addr, raw_code >>= 3) { -// -// uint32_t block_x = addr % 4; -// uint32_t block_y = (addr - block_x) / 4; -// -// switch (raw_code & 0b111) { -// case B_1x1: -// -// tmp.top = width * block_y + gap * (block_y + 1) + top; -// tmp.left = width * block_x + gap * (block_x + 1) + left; -// tmp.height = width; -// tmp.width = width; -//// std::cout << "1x1" << std::endl; -// tmp.to_xml(); -// -// break; -// case B_1x2: -// -// tmp.top = width * block_y + gap * (block_y + 1) + top; -// tmp.left = width * block_x + gap * (block_x + 1) + left; -// tmp.height = width; -// tmp.width = width * 2 + gap; -//// std::cout << "1x2" << std::endl; -// tmp.to_xml(); -// -// break; -// case B_2x1: -// -// tmp.top = width * block_y + gap * (block_y + 1) + top; -// tmp.left = width * block_x + gap * (block_x + 1) + left; -// tmp.height = width * 2 + gap; -// tmp.width = width; -//// std::cout << "2x1" << std::endl; -// tmp.to_xml(); -// -// break; -// case B_2x2: -// -// tmp.top = width * block_y + gap * (block_y + 1) + top; -// tmp.left = width * block_x + gap * (block_x + 1) + left; -// tmp.height = width * 2 + gap; -// tmp.width = width * 2 + gap; -//// std::cout << "2x2" << std::endl; -// tmp.to_xml(); -// -// break; -// default: -// continue; -// } -// } -// -//} - - void GraphCase::render(SvgGraph &svg, const CaseSkin &skin) const { // std::cout << "render begin" << std::endl; auto raw_code = (uint64_t)code; - uint32_t case_width = block_width * 4 + block_gap * 5; uint32_t case_height = block_width * 5 + block_gap * 6; auto skeleton = new SvgRect {start, case_width, case_height}; - skeleton->color = "pink"; + skeleton->color = skin.CASE_BG_COLOR; + skeleton->stroke = skin.CASE_BORDER_WIDTH; + skeleton->line_color = skin.CASE_BORDER_COLOR; + skeleton->radius = uint64_t(skin.CASE_RADIUS * (float)block_width); svg.insert(skeleton); + auto apply_skin = [&skin, this](SvgRect *block) { + block->color = skin.BLOCK_BG_COLOR; + block->stroke = skin.BLOCK_BORDER_WIDTH; + block->line_color = skin.BLOCK_BORDER_COLOR; + block->radius = uint64_t(skin.BLOCK_RADIUS * (float)block_width); + }; + + SvgRect *block; + for (int addr = 0; raw_code; ++addr, raw_code >>= 3) { uint32_t block_x = addr % 4; uint32_t block_y = (addr - block_x) / 4; @@ -216,46 +156,54 @@ void GraphCase::render(SvgGraph &svg, const CaseSkin &skin) const { switch (raw_code & 0b111) { case B_1x1: - svg.insert(new SvgRect { + block = new SvgRect { { start.x + block_x * block_width + (block_x + 1) * block_gap, start.y + block_y * block_width + (block_y + 1) * block_gap, }, block_width, block_width, - }); + }; + apply_skin(block); + svg.insert(block); break; case B_1x2: - svg.insert(new SvgRect { + block = new SvgRect { { start.x + block_x * block_width + (block_x + 1) * block_gap, start.y + block_y * block_width + (block_y + 1) * block_gap, }, block_width * 2 + block_gap, block_width, - }); + }; + apply_skin(block); + svg.insert(block); break; case B_2x1: - svg.insert(new SvgRect { + block = new SvgRect { { start.x + block_x * block_width + (block_x + 1) * block_gap, start.y + block_y * block_width + (block_y + 1) * block_gap, }, block_width, block_width * 2 + block_gap, - }); + }; + apply_skin(block); + svg.insert(block); break; case B_2x2: - svg.insert(new SvgRect { + block = new SvgRect { { start.x + block_x * block_width + (block_x + 1) * block_gap, start.y + block_y * block_width + (block_y + 1) * block_gap, }, block_width * 2 + block_gap, block_width * 2 + block_gap, - }); + }; + apply_skin(block); + svg.insert(block); break; default: diff --git a/src/graph/graph.h b/src/graph/graph.h index 32ce5d9..a317ac4 100644 --- a/src/graph/graph.h +++ b/src/graph/graph.h @@ -12,23 +12,21 @@ class CaseSkin { public: - std::string color = "pink"; -}; + /// case options + float CASE_RADIUS = 0.2; + uint32_t CASE_BORDER_WIDTH = 2; + std::string CASE_BG_COLOR = "pink"; + std::string CASE_BORDER_COLOR = "blue"; -//class SvgCase { -//public: -// -// uint64_t code; -// -// uint64_t top = 0; -// uint64_t left = 0; -// -// uint32_t gap = 4; -// uint32_t width = 12; -// -// void render() const; -// -//}; + /// block options + float BLOCK_RADIUS = 0.3; + uint32_t BLOCK_BORDER_WIDTH = 1; + std::string BLOCK_BG_COLOR = "green"; + std::string BLOCK_BORDER_COLOR = "yellow"; + + // TODO: opacity options + +}; class GraphCase { public: diff --git a/src/graph/svg/svg.h b/src/graph/svg/svg.h index f477efe..29c1c13 100644 --- a/src/graph/svg/svg.h +++ b/src/graph/svg/svg.h @@ -32,11 +32,8 @@ private: class SvgLine : public SvgObject { public: - uint64_t start_x; - uint64_t start_y; - - uint64_t end_x; - uint64_t end_y; + Point start; + Point end; // TODO: more options for svg-line @@ -49,13 +46,11 @@ public: class SvgRect : public SvgObject { public: Point start; -// uint64_t top; -// uint64_t left; uint64_t width; uint64_t height; uint64_t radius = 0; - uint32_t stroke = 1; + uint32_t stroke = 0; std::string color; std::string line_color;