MCPcopy Index your code
hub / github.com/NetHack/NetHack / HitpointBar

Method HitpointBar

win/Qt/qt_stat.cpp:615–715  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

613}
614
615DISABLE_WARNING_FORMAT_NONLITERAL
616
617// when hitpoint bar is enabled, calculate and draw it, otherwise remove it
618void NetHackQtStatusWindow::HitpointBar()
619{
620 // a style sheet is used to specify color for otherwise blank labels;
621 // barcolors[][*]: column [0=left] is current health, [1=right] is injury
622 static const char
623 *styleformat = "QLabel { background-color : %s ; color : transparent ;"
624 " min-width : %d ; max-width %d }",
625 *barcolors[6][2] = {
626 { "black", "black" }, // 100% /* second black never shown */
627 { "blue", "darkBlue" }, //75..99
628 /* gray is darker than darkGray for some reason (at least on OSX);
629 default green is too dark compared to blue, yellow, orange,
630 and red so is changed here to green.lighter(150) */
631 { "#00c000", "gray" }, //50..74 /* "green"=="#008000" */
632 { "yellow", "darkGray" }, //25..49
633 { "orange", "lightGray" }, //10..24
634 { "red", "white" }, // 0..9
635 };
636
637 /*
638 * tty and curses use inverse video characters in the left portion
639 * of the name+rank string to reflect hero's health. We draw a
640 * separate line above the name+rank field instead. The left side
641 * of the line indicates current health. The right side is only
642 * shown when injured and indicates missing amount of maximum health.
643 */
644 if (iflags.wc2_hitpointbar) {
645 int colorindx, w,
646 ihp = Upolyd ? u.mh : u.uhp,
647 ihpmax = Upolyd ? u.mhmax : u.uhpmax;
648 ihp = std::max(std::min(ihp, ihpmax), 0);
649 int pct = 100 * ihp / ihpmax,
650 lox = hline1.x(),
651 hix = lox + hline1.width() - 1;
652 QRect geoH = hpbar_health.geometry(),
653 geoI = hpbar_injury.geometry();
654 QString styleH, styleI;
655
656 if (ihp < ihpmax) {
657 // health is less than full;
658 // use red for extreme low health even if the percentage is
659 // above the usual threshold (which will happen when maximum
660 // health is very low); do a similar threshold override for
661 // orange even though it can be distracting for low level hero
662 colorindx = (pct < 10 || ihp < 5) ? 5 // red | white
663 : (pct < 25 || ihp < 10 ) ? 4 // orange | lightGray
664 : (pct < 50) ? 3 // yellow | darkGray*
665 : (pct < 75) ? 2 // green | gray*
666 : 1; // blue | darkBlue
667
668 int pxl_health = (hix - lox + 1) * ihp / ihpmax;
669 geoH.setRight(std::min(lox + pxl_health - 1, hix));
670 hpbar_health.setGeometry(geoH);
671 w = geoH.right() - geoH.left() + 1; // might yield 0 (ie, if dead)
672 styleH = nh_qsprintf(styleformat, barcolors[colorindx][0], w, w);

Callers

nothing calls this directly

Calls 3

nh_qsprintfFunction · 0.85
widthMethod · 0.45
showMethod · 0.45

Tested by

no test coverage detected