Skip to content

Commit

Permalink
Merge pull request #7 from memeToasty/trees
Browse files Browse the repository at this point in the history
Trees
  • Loading branch information
memeToasty authored Sep 5, 2022
2 parents c38aa1b + c4a58c3 commit 3fbc0ad
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
15 changes: 8 additions & 7 deletions source/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
#include "node.hpp"
#include <citro2d.h>

Node::Node(const char* text, float x, float y, float size) {
this->size = size * 20;
Node::Node(const char* text, float x, float y, float scale) {
this->scale = scale;
this->x = x;
this->y = y;
//TODO Fix text positioning on Circle
this->text = new Text(text, &this->x, &this->y, size);
*this->text->xPos -= this->text->staticText.width / 2;

this->text = new Text(text, x, y, scale);
}

void Node::render(bool active) {

this->text->xPos = this->x - (this->text->staticText.width / 2 * scale);
this->text->yPos = this->y - (this->scale * 15);

if (active) {
C2D_DrawCircleSolid(x, y, 0, size, activeColor);
C2D_DrawCircleSolid(x, y, 0, scale * 15, activeColor);
} else {
C2D_DrawCircleSolid(x, y, 0, size, color);
C2D_DrawCircleSolid(x, y, 0, scale * 15, color);
}
this->text->render();
}
6 changes: 3 additions & 3 deletions source/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Node {
static const u32 activeColor = C2D_Color32(0xFF, 0x00, 0x00, 0xFF);
public:
Text* text;
float x,y ;
float size;
float x,y;
float scale;

Node(const char* text, float x, float y, float size);
Node(const char* text, float x, float y, float scale);

void render(bool active);
};
Expand Down
4 changes: 2 additions & 2 deletions source/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Text::deInit()
cfguExit();
}

Text::Text(const char* text, float* x, float* y, float scale)
Text::Text(const char* text, float x, float y, float scale)
{
this->xPos = x;
this->yPos = y;
Expand All @@ -33,5 +33,5 @@ void Text::render()
{
C2D_TextFontParse(&staticText, font, textBuf, text);
C2D_TextOptimize(&staticText);
C2D_DrawText(&staticText, 0, *xPos, *yPos, 0.0f, textScale, textScale);
C2D_DrawText(&staticText, 0, xPos, yPos, 0.0f, textScale, textScale);
}
6 changes: 3 additions & 3 deletions source/text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class Text
static C2D_TextBuf textBuf;
C2D_Text staticText;
static C2D_Font font;
float* xPos;
float* yPos;
float xPos;
float yPos;
float textScale;
const char* text;

Text(const char* text, float* x, float* y, float scale);
Text(const char* text, float x, float y, float scale);


void render();
Expand Down
17 changes: 6 additions & 11 deletions source/visuals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
SwkbdState swkbd;
char mybuf[10];

static const int PADDING = 30;

void accessElement(unsigned int accessedIndex)
{
activeIndex = accessedIndex;
Expand Down Expand Up @@ -94,13 +96,9 @@ void initArray()
}
// Init Tree view
nodeArray.clear();
for (unsigned short i = 0; i < arrayLen; i++)
{
nodeArray.push_back(new Node(&std::to_string(array[i]).c_str()[0],0,i*10, 0.5f));
}

unsigned short height = (unsigned short) floor(log2(nodeArray.size()));
float yGap = SCREEN_HEIGHT / height;
unsigned short height = (unsigned short) floor(log2(arrayLen));
float yGap = (SCREEN_HEIGHT - (2 * PADDING)) / height;
//Go through every layer of binary tree with size of the nodeArray
for (unsigned short h = 0; h <= height; ++h) {

Expand All @@ -110,14 +108,11 @@ void initArray()
for (unsigned short i = 0; i < (unsigned short) pow(2,h); ++i) {
int absoluteI = pow(2,h) - 1 + i;

if (absoluteI > (int) nodeArray.size() - 1) {
if (absoluteI > (int) arrayLen - 1) {
break;
}

Node* currentNode = nodeArray.at(absoluteI);

currentNode->x = leftOffset + i * xGap;
currentNode->y = h * yGap;
nodeArray.push_back(new Node(std::to_string(array[i]).c_str(), leftOffset + i * xGap, h * yGap + PADDING, 10.0f / arrayLen ));
}
}
}
Expand Down

0 comments on commit 3fbc0ad

Please sign in to comment.