From dfa6357372cb075921772e9eeafc7e8379320ad5 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 22 Jul 2023 02:32:26 +0300 Subject: [PATCH 9/9] sdl2 tech tree: Fix crash when reqs not in order Help was assuming that if tech has any reqs, those are in first slots. See osdn #48436 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/helpdlg.c | 88 ++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/client/gui-sdl2/helpdlg.c b/client/gui-sdl2/helpdlg.c index b18f3fb16d..41db373e05 100644 --- a/client/gui-sdl2/helpdlg.c +++ b/client/gui-sdl2/helpdlg.c @@ -1664,7 +1664,7 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, struct widget *pWindow, struct TECHS_BUTTONS *pStore) { - int i, w, h, req_count , targets_count, sub_req_count, sub_targets_count; + int i, w, h, req_count, targets_count, sub_req_count, sub_targets_count; struct widget *pWidget; struct widget *pTech; utf8_str *pstr; @@ -1686,7 +1686,7 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, pTech = pWidget; pDock = pWidget; - req_count = 0; + req_count = 0; for (i = AR_ONE; i <= AR_TWO; i++) { Tech_type_id ar = advance_required(tech, i); struct advance *vap = valid_advance_by_number(ar); @@ -1701,16 +1701,13 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, pWidget->ID = MAX_ID - ar; DownAdd(pWidget, pDock); pDock = pWidget; - pStore->pRequirementButton[i] = pWidget; - req_count++; - } else { - pStore->pRequirementButton[i] = NULL; + pStore->pRequirementButton[req_count++] = pWidget; } } sub_req_count = 0; - if (pStore->show_full_tree && req_count) { + if (pStore->show_full_tree && req_count > 0) { int j, sub_tech; for (j = 0; j < req_count; j++) { @@ -1762,7 +1759,7 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, } sub_targets_count = 0; - if (targets_count) { + if (targets_count > 0) { int sub_tech; for (i = 0; i < targets_count; i++) { @@ -1796,13 +1793,15 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, FREEUTF8STR(pstr); /* ------------------------------------------ */ - if (sub_req_count) { + if (sub_req_count > 0) { w = (adj_size(20) + pStore->pSub_Req[0]->size.w) * 2; w += (pWindow->size.w - (20 + pStore->pSub_Req[0]->size.w + w + pTech->size.w)) / 2; } else { - if (req_count) { - w = (pWindow->area.x + 1 + width + pStore->pRequirementButton[0]->size.w * 2 + adj_size(20)); - w += (pWindow->size.w - ((adj_size(20) + pStore->pRequirementButton[0]->size.w) + w + pTech->size.w)) / 2; + if (req_count > 0) { + w = (pWindow->area.x + 1 + + width + pStore->pRequirementButton[0]->size.w * 2 + adj_size(20)); + w += (pWindow->size.w - ((adj_size(20) + pStore->pRequirementButton[0]->size.w) + + w + pTech->size.w)) / 2; } else { w = (pWindow->size.w - pTech->size.w) / 2; } @@ -1811,29 +1810,31 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, pTech->size.x = pWindow->size.x + w; pTech->size.y = pWindow->area.y + (pWindow->area.h - pTech->size.h) / 2; - if (req_count) { + if (req_count > 0) { h = (req_count == 1 ? pStore->pRequirementButton[0]->size.h : req_count * (pStore->pRequirementButton[0]->size.h + adj_size(80)) - adj_size(80)); h = pTech->size.y + (pTech->size.h - h) / 2; for (i = 0; i < req_count; i++) { - pStore->pRequirementButton[i]->size.x = pTech->size.x - adj_size(20) - pStore->pRequirementButton[i]->size.w; + pStore->pRequirementButton[i]->size.x + = pTech->size.x - adj_size(20) - pStore->pRequirementButton[i]->size.w; pStore->pRequirementButton[i]->size.y = h; h += (pStore->pRequirementButton[i]->size.h + adj_size(80)); } } - if (sub_req_count) { + if (sub_req_count > 0) { h = (sub_req_count == 1 ? pStore->pSub_Req[0]->size.h : sub_req_count * (pStore->pSub_Req[0]->size.h + adj_size(20)) - adj_size(20)); h = pTech->size.y + (pTech->size.h - h) / 2; for (i = 0; i < sub_req_count; i++) { - pStore->pSub_Req[i]->size.x = pTech->size.x - (adj_size(20) + pStore->pSub_Req[i]->size.w) * 2; + pStore->pSub_Req[i]->size.x + = pTech->size.x - (adj_size(20) + pStore->pSub_Req[i]->size.w) * 2; pStore->pSub_Req[i]->size.y = h; h += (pStore->pSub_Req[i]->size.h + adj_size(20)); } } - if (targets_count) { + if (targets_count > 0) { h = (targets_count == 1 ? pStore->pTargets[0]->size.h : targets_count * (pStore->pTargets[0]->size.h + adj_size(20)) - adj_size(20)); h = pTech->size.y + (pTech->size.h - h) / 2; @@ -1844,22 +1845,30 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, } } - if (sub_targets_count) { + if (sub_targets_count > 0) { if (sub_targets_count < 3) { - pStore->pSub_Targets[0]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[0]->size.w; - pStore->pSub_Targets[0]->size.y = pTech->size.y - pStore->pSub_Targets[0]->size.h - adj_size(10); + pStore->pSub_Targets[0]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[0]->size.w; + pStore->pSub_Targets[0]->size.y + = pTech->size.y - pStore->pSub_Targets[0]->size.h - adj_size(10); if (pStore->pSub_Targets[1]) { - pStore->pSub_Targets[1]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[1]->size.w; - pStore->pSub_Targets[1]->size.y = pTech->size.y + pTech->size.h + adj_size(10); + pStore->pSub_Targets[1]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[1]->size.w; + pStore->pSub_Targets[1]->size.y + = pTech->size.y + pTech->size.h + adj_size(10); } } else { if (sub_targets_count < 5) { for (i = 0; i < MIN(sub_targets_count, 4); i++) { - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; if (i < 2) { - pStore->pSub_Targets[i]->size.y = pTech->size.y - (pStore->pSub_Targets[i]->size.h + adj_size(5)) * ( 2 - i ); + pStore->pSub_Targets[i]->size.y + = pTech->size.y - (pStore->pSub_Targets[i]->size.h + adj_size(5)) * (2 - i); } else { - pStore->pSub_Targets[i]->size.y = pTech->size.y + pTech->size.h + adj_size(5) + (pStore->pSub_Targets[i]->size.h + adj_size(5)) * ( i - 2 ); + pStore->pSub_Targets[i]->size.y + = pTech->size.y + pTech->size.h + adj_size(5) + + (pStore->pSub_Targets[i]->size.h + adj_size(5)) * (i - 2); } } } else { @@ -1867,28 +1876,39 @@ static struct widget *create_tech_tree(Tech_type_id tech, int width, for (i = 0; i < MIN(sub_targets_count, 6); i++) { switch(i) { case 0: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; pStore->pSub_Targets[i]->size.y = pTech->size.y - h * 2; break; case 1: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w * 2 - adj_size(10); + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w * 2 + - adj_size(10); pStore->pSub_Targets[i]->size.y = pTech->size.y - h - h / 2; break; case 2: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; pStore->pSub_Targets[i]->size.y = pTech->size.y - h; break; case 3: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; - pStore->pSub_Targets[i]->size.y = pTech->size.y + pTech->size.h + adj_size(6); + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; + pStore->pSub_Targets[i]->size.y + = pTech->size.y + pTech->size.h + adj_size(6); break; case 4: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; - pStore->pSub_Targets[i]->size.y = pTech->size.y + pTech->size.h + adj_size(6) + h; + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w; + pStore->pSub_Targets[i]->size.y + = pTech->size.y + pTech->size.h + adj_size(6) + h; break; default: - pStore->pSub_Targets[i]->size.x = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w * 2 - adj_size(10); - pStore->pSub_Targets[i]->size.y = pTech->size.y + pTech->size.h + adj_size(6) + h / 2 ; + pStore->pSub_Targets[i]->size.x + = pTech->size.x + pTech->size.w - pStore->pSub_Targets[i]->size.w * 2 + - adj_size(10); + pStore->pSub_Targets[i]->size.y + = pTech->size.y + pTech->size.h + adj_size(6) + h / 2 ; break; } } -- 2.40.1