// extended label method for option lines
    // the purpose is to provide a little more visual context to the various options,
    // and make config-ui interface more human friendly
    void option_label(const int2& p, const color& c, rs::device& dev, rs::option opt, double max_width, bool enabled, double* value = nullptr)
    {
        auto name = find_and_replace(rs_option_to_string((rs_option)opt), "_", " "); // replacing _ with ' ' to reduce visual clutter
        std::string s(name);

        auto size = name.size(); // align the string to max. allowed width
        while (size > 0 && stb_easy_font_width((char*)s.c_str()) > max_width)
        {
            s = name.substr(0, size--) + "...";
        }

        // remove option prefixes converting them to visual hints through color:
        color newC = c;
#define STRING_CASE(S, C) std::string S = #S; if (s.compare(0, S.length(), S) == 0) { newC = C; s = find_and_replace(s, S + " ", ""); }
        color color1 = { 0.6f, 1.0f, 1.0f };
        color color2 = { 1.0f, 0.6f, 1.0f };
        color color3 = { 1.0f, 1.0f, 0.6f };
        color color4 = { 1.0f, 0.6f, 0.6f };
        color color5 = { 0.6f, 0.6f, 1.0f };
        color color6 = { 0.6f, 1.0f, 0.6f };
        STRING_CASE(ZR300, color1)
        STRING_CASE(F200, color2)
        STRING_CASE(SR300, color3)
        STRING_CASE(R200, color4)
        STRING_CASE(FISHEYE, color5)
        STRING_CASE(COLOR, color6)
        if (!enabled) newC = { 0.5f, 0.5f, 0.5f };

        auto w = stb_easy_font_width((char*)s.c_str());
        label(p, newC, s.c_str());
        // if value is required, append it at the end of the string
        if (value)
        {
            std::stringstream sstream;
            sstream << ": " << *value;
            int2 newP{ p.x + w, p.y };
            label(newP, c, sstream.str().c_str());
        }

        rect bbox{ p.x - 15, p.y - 10, p.x + w + 10, p.y + 5 };
        if (bbox.contains(cursor))
        {
            std::string hint = dev.get_option_description(opt);
            auto hint_w = stb_easy_font_width((char*)hint.c_str());
            fill_rect({ cursor.x - hint_w - 7, cursor.y + 5, cursor.x + 7, cursor.y - 17 }, { 1.0f, 1.0f, 1.0f });
            fill_rect({ cursor.x - hint_w - 6, cursor.y + 4, cursor.x + 6, cursor.y - 16 }, { 0.0f, 0.0f, 0.0f });
            label({ cursor.x - hint_w, cursor.y - 2 }, { 1.f, 1.f, 1.f }, hint.c_str());
        }
    }
Example #2
0
inline int get_text_width(const char * text)
{
    return stb_easy_font_width((char *)text);
}