Ticket #723: 0adTicket_723.patch

File 0adTicket_723.patch, 5.4 KB (added by kingadami, 11 years ago)

Finished Philips patch by adding support for left and right aligned text.

  • binaries/data/mods/public/gui/common/common_styles.xml

     
    244244        dropdown_buffer="1"
    245245        font="serif-bold-stroke-14"
    246246        textcolor="white"
    247         text_align="center"
     247        text_align="left"
    248248        text_valign="center"
    249249
    250250        sprite="StoneButton"
  • source/gui/CGUI.cpp

     
    665665    // Easier to read.
    666666    bool WordWrapping = (Width != 0);
    667667
     668    // get the alignment type for the control we are computing the text for since
     669    // we are computing the horizontal alignment in this method in order to not have
     670    // to run through the TextCalls a second time in the CalculateTextPosition method again
     671    EAlign align;
     672    GUI<EAlign>::GetSetting(pObject, "text_align", align);
     673
    668674    // Go through string word by word
    669675    for (int i=0; i<(int)string.m_Words.size()-1 && !done; ++i)
    670676    {
     
    778784            // Reset X for the next loop
    779785            x = width_range[From];
    780786
    781             // Now we'll do another loop to figure out the height of
    782             //  the line (the height of the largest character). This
     787            // Now we'll do another loop to figure out the height and width of
     788            //  the line (the height of the largest character and the width is
     789            //  the sum of all of the individual widths). This
    783790            //  couldn't be determined in the first loop (main loop)
    784791            //  because it didn't regard images, so we don't know
    785792            //  if all characters processed, will actually be involved
    786793            //  in that line.
    787794            float line_height=0.f;
     795            float line_width=0.f;
    788796            for (int j=temp_from; j<=i; ++j)
    789797            {
    790798                // We don't want to use Feedback now, so we'll have to use
     
    807815                // Let line_height be the maximum m_Height we encounter.
    808816                line_height = std::max(line_height, Feedback2.m_Size.cy);
    809817
     818                line_width += Feedback2.m_Size.cx;
     819
    810820                if (WordWrapping && Feedback2.m_NewLine)
    811821                    break;
    812822            }
    813823
     824            float dx = 0.f;
     825            //compute offset based on what kind of alignment
     826            switch (align)
     827            {
     828            case EAlign_Left:
     829                //don't add an offset
     830                dx = 0.f;
     831                break;
     832
     833            case EAlign_Center:
     834                dx = ((width_range[To] - width_range[From]) - line_width) / 2;
     835                break;
     836
     837            case EAlign_Right:
     838                dx = width_range[To] - line_width;
     839                break;
     840
     841            default:
     842                debug_warn(L"Broken EAlign in CGUI::GenerateText()");
     843                break;
     844            }
    814845            // Reset x once more
    815846            x = width_range[From];
    816847            // Move down, because font drawing starts from the baseline
     
    837868                std::vector<SGUIText::STextCall>::iterator it;
    838869                for (it = Feedback2.m_TextCalls.begin(); it != Feedback2.m_TextCalls.end(); ++it)
    839870                {
    840                     it->m_Pos = CPos(x + x_pointer, y);
     871                    it->m_Pos = CPos(dx + x + x_pointer, y);
    841872
    842873                    x_pointer += it->m_Size.cx;
    843874
  • source/gui/IGUITextOwner.cpp

     
    100100
    101101void IGUITextOwner::CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text)
    102102{
    103     EAlign align;
    104103    EVAlign valign;
    105     GUI<EAlign>::GetSetting(this, "text_align", align);
    106104    GUI<EVAlign>::GetSetting(this, "text_valign", valign);
    107105
    108     switch (align)
    109     {
    110     case EAlign_Left:
    111         TextPos.x = ObjSize.left;
    112         break;
    113     case EAlign_Center:
    114         // Round to integer pixel values, else the fonts look awful
    115         TextPos.x = floorf(ObjSize.CenterPoint().x - Text.m_Size.cx/2.f);
    116         break;
    117     case EAlign_Right:
    118         TextPos.x = ObjSize.right - Text.m_Size.cx;
    119         break;
    120     default:
    121         debug_warn(L"Broken EAlign in CButton::SetupText()");
    122         break;
    123     }
     106    // The horizontal Alignment is now computed in GenerateText in order to not have to
     107    // loop through all of the TextCall objects again.
     108    TextPos.x = ObjSize.left;
    124109
    125110    switch (valign)
    126111    {
  • source/gui/CGUI.h

     
    241241     * Generate a SGUIText object from the inputted string.
    242242     * The function will break down the string and its
    243243     * tags to calculate exactly which rendering queries
    244      * will be sent to the Renderer.
     244     * will be sent to the Renderer. Also, Horizontal Alignment
     245     * is taken into acount in this method but NOT Vertical Alignment.
    245246     *
    246247     * Done through the CGUI since it can communicate with
    247248     *
  • source/gui/CTooltip.cpp

     
    3636    AddSetting(GUIST_float,                 "maxwidth");
    3737    AddSetting(GUIST_CPos,                  "offset");
    3838    AddSetting(GUIST_EVAlign,               "anchor");
     39    AddSetting(GUIST_EAlign,                "text_align");
    3940    // This is used for tooltips that are hidden/revealed manually by scripts, rather than through the standard tooltip display mechanism
    4041    AddSetting(GUIST_bool,                  "independent");
    4142
     
    4950    // Defaults
    5051    GUI<int>::SetSetting(this, "delay", 500);
    5152    GUI<EVAlign>::SetSetting(this, "anchor", EVAlign_Bottom);
     53    GUI<EAlign>::SetSetting(this, "text_align", EAlign_Left);
    5254
    5355    // Set up a blank piece of text, to be replaced with a more
    5456    // interesting message later