up down select and focus

This commit is contained in:
vrd
2023-01-02 17:31:29 +02:00
parent 1c91d7b775
commit 6cbe64f927
7 changed files with 154 additions and 50 deletions
+95 -21
View File
@@ -191,6 +191,17 @@ unsigned Tree::findCommonAncestor(const unsigned firstId, const unsigned secondI
return oldestId;
}
/* person_id Tree::findRelation(person_id first, person_id second) const */
/* { */
/* const auto& curRels = relations[first]; */
/* for(auto i =0; i<curRels.gNum(); ++i) */
/* { */
/* if(curRels[i].id == second) */
/* { */
/* } */
void Tree::saveTree() const
{
std::ofstream peopleFile(treeName+ ".people", std::ios::binary);
@@ -263,7 +274,7 @@ bool Tree::addRelation(const char* firstName, const RelType type, const char* se
bool Tree::addRelation(const unsigned firstId, const RelType type, const unsigned secondId, bool opposite)//
{
if (firstId == Nobody || firstId >= numPeople || secondId == Nobody || secondId >= numPeople || type == Invalid)
if (firstId == Nobody || firstId >= numPeople || secondId == Nobody || secondId >= numPeople || type == None)
return 0;
unsigned i = 0;
@@ -367,30 +378,45 @@ void Tree::removeRelation(const unsigned firstId, const unsigned secondId)
}
}
void Tree::draw(person_id focused) const
void Tree::display()
{
erase(); //Clear the screen
wnoutrefresh(stdscr);
draw();
U16 drawY = 0/* (LINES) / 2 */;
U16 drawX = 0/* (COLS) / 2 */;
drawLineWithWives(focused, drawY, drawX);
doupdate();
char c;
I32 c;
do
{
c = std::tolower(getch());
switch(c)
{
case '\n':
focused_ = selected_;
draw();
break;
case KEY_DOWN:
selectDown();
break;
case KEY_UP:
selectUp();
break;
default: break;
}
}
while(c != 'w');
}
void Tree::draw() const
{
erase(); //Clear the screen
wnoutrefresh(stdscr);
U16 drawY = 0/* (LINES) / 2 */;
U16 drawX = 0/* (COLS) / 2 */;
drawLineWithWives(focused_, drawY, drawX);
doupdate();
}
void Tree::print() const
{
//for(const auto& member: people)
@@ -465,12 +491,13 @@ void Tree::printOldestAncestors(const unsigned id)const
void Tree::commonAncestorRec(person_id id, const Array<char> &visited)const
{
for (unsigned i = 0; i < people[id].numRel_; ++i)
auto& curRels = relations[id];
for (unsigned i = 0; i < curRels.gNum(); ++i)
{
if (relations[id][i].type == Child)
if (curRels[i].type == Child)
{
++visited[relations[id][i].id];
commonAncestorRec(relations[id][i].id, visited);
++visited[curRels[i].id];
commonAncestorRec(curRels[i].id, visited);
}
}
}
@@ -543,10 +570,10 @@ static U8 linkToChild(I16 startY, I16 startX, I16 endX, bool moreSiblings)
/* return drawX; */
/* } */
/* return drawX + people[id].boxWidth() + dist_between; */
/* return drawX + people[id].boxWidth() + dist_between_; */
/* } */
I16 Tree::drawLineWithWives(person_id id, I16 drawY, const I16 drawX) const
I16 Tree::drawLineWithWives(const person_id id, I16 drawY, const I16 drawX) const
{
const Person& person = people[id];
const auto& rels = relations[id];
@@ -562,13 +589,15 @@ I16 Tree::drawLineWithWives(person_id id, I16 drawY, const I16 drawX) const
spouse = rels[i].id;
}
const size_t numKids = children.size();
person.draw(drawY, drawX, spouse!=Nobody, false, numKids!=0);
person.draw(drawY, drawX,
id == selected_, spouse!=Nobody, false, numKids!=0);
U16 spouse_width = 0;
if(spouse != Nobody)
{
people[spouse].draw(drawY, drawX + person.boxWidth() + spouse_dist, false, true);
spouse_width = spouse_dist + people[spouse].boxWidth();
people[spouse].draw(drawY, drawX + person.boxWidth() + spouse_dist_,
spouse == selected_, false, true);
spouse_width = spouse_dist_ + people[spouse].boxWidth();
}
auto childX = drawX;
@@ -588,6 +617,51 @@ I16 Tree::drawLineWithWives(person_id id, I16 drawY, const I16 drawX) const
}
}
return max(drawX + person.boxWidth() + dist_between + spouse_width,
return max(drawX + person.boxWidth() + dist_between_ + spouse_width,
childX);
}
void Tree::selectLeft() /* todo */
{
const auto& curRel = relations[selected_];
for(U32 i = 0; i < curRel.gNum(); ++i)
{
if(curRel[i].type == Sibling)
{
selected_ = curRel[i].id;
draw();
return;
}
}
}
void Tree::selectDown()
{
const auto& curRel = relations[selected_];
for(U32 i = 0; i < curRel.gNum(); ++i)
{
if(curRel[i].type == Parent)
{
selected_ = curRel[i].id;
draw();
return;
}
}
}
void Tree::selectUp()
{
const auto& curRel = relations[selected_];
for(U32 i = 0; i < curRel.gNum(); ++i)
{
if(curRel[i].type == Child && (people[curRel[i].id].isMale() || curRel[i].id == focused_))
{
selected_ = curRel[i].id;
draw();
return;
}
}
}