Merge pull request #972 from npwoods/fix_text_layout_actual_width

Fix text layout actual width
This commit is contained in:
Robert 2016-06-20 00:16:19 +10:00 committed by GitHub
commit 6d160ed72d
3 changed files with 29 additions and 13 deletions

View File

@ -78,9 +78,10 @@ CORE IMPLEMENTATION
//-------------------------------------------------
text_layout::text_layout(render_font &font, float xscale, float yscale, float width, text_layout::text_justify justify, text_layout::word_wrapping wrap)
: m_font(font), m_xscale(xscale), m_yscale(yscale), m_width(width), m_maximum_line_width(0.0f), m_justify(justify), m_wrap(wrap), m_current_line(nullptr), m_last_break(0), m_text_position(0), m_truncating(false)
: m_font(font), m_xscale(xscale), m_yscale(yscale), m_width(width), m_justify(justify), m_wrap(wrap), m_current_line(nullptr), m_last_break(0), m_text_position(0), m_truncating(false)
{
invalidate_calculated_actual_width();
}
@ -89,9 +90,10 @@ text_layout::text_layout(render_font &font, float xscale, float yscale, float wi
//-------------------------------------------------
text_layout::text_layout(text_layout &&that)
: m_font(that.m_font), m_xscale(that.m_xscale), m_yscale(that.m_yscale), m_width(that.m_width), m_maximum_line_width(that.m_maximum_line_width), m_justify(that.m_justify), m_wrap(that.m_wrap), m_lines(std::move(that.m_lines)),
: m_font(that.m_font), m_xscale(that.m_xscale), m_yscale(that.m_yscale), m_width(that.m_width), m_calculated_actual_width(that.m_calculated_actual_width), m_justify(that.m_justify), m_wrap(that.m_wrap), m_lines(std::move(that.m_lines)),
m_current_line(that.m_current_line), m_last_break(that.m_last_break), m_text_position(that.m_text_position), m_truncating(false)
{
that.invalidate_calculated_actual_width();
}
@ -115,6 +117,9 @@ void text_layout::add_text(const char *text, const char_style &style)
while(position < text_length)
{
// adding a character - we might change the width
invalidate_calculated_actual_width();
// do we need to create a new line?
if (m_current_line == nullptr)
{
@ -159,7 +164,6 @@ void text_layout::add_text(const char *text, const char_style &style)
start_new_line(LEFT, style.size);
// and then close up the current line
update_maximum_line_width();
m_current_line = nullptr;
}
else if (!m_truncating)
@ -202,12 +206,12 @@ void text_layout::add_text(const char *text, const char_style &style)
//-------------------------------------------------
// update_maximum_line_width
// invalidate_calculated_actual_width
//-------------------------------------------------
void text_layout::update_maximum_line_width()
void text_layout::invalidate_calculated_actual_width()
{
m_maximum_line_width = actual_width();
m_calculated_actual_width = -1;
}
@ -245,8 +249,18 @@ float text_layout::actual_left() const
float text_layout::actual_width() const
{
float current_line_width = m_current_line ? m_current_line->width() : 0;
return std::max(m_maximum_line_width, current_line_width);
// do we need to calculate the width?
if (m_calculated_actual_width < 0)
{
// calculate the actual width
m_calculated_actual_width = 0;
for (const auto &line : m_lines)
m_calculated_actual_width = std::max(m_calculated_actual_width, line->width());
}
// return it
return m_calculated_actual_width;
}
@ -275,7 +289,6 @@ void text_layout::start_new_line(text_layout::text_justify justify, float height
std::unique_ptr<line> new_line(global_alloc_clear<line>(*this, justify, actual_height(), height * yscale()));
// update the current line
update_maximum_line_width();
m_current_line = new_line.get();
m_last_break = 0;
m_truncating = false;

View File

@ -66,7 +66,7 @@ public:
void restyle(size_t start, size_t span, rgb_t *fgcolor, rgb_t *bgcolor);
int get_wrap_info(std::vector<int> &xstart, std::vector<int> &xend) const;
void emit(render_container *container, float x, float y);
void add_text(const char *text, rgb_t fgcolor = rgb_t::white, rgb_t bgcolor = rgb_t(0,0,0,0), float size = 1.0)
void add_text(const char *text, rgb_t fgcolor = rgb_t::white, rgb_t bgcolor = rgb_t::transparent, float size = 1.0)
{
// create the style
char_style style = { 0, };
@ -140,7 +140,7 @@ private:
float m_xscale;
float m_yscale;
float m_width;
float m_maximum_line_width;
mutable float m_calculated_actual_width;
text_justify m_justify;
word_wrapping m_wrap;
std::vector<std::unique_ptr<line>> m_lines;
@ -155,7 +155,7 @@ private:
float get_char_width(unicode_char ch, float size);
void truncate_wrap();
void word_wrap();
void update_maximum_line_width();
void invalidate_calculated_actual_width();
};
} // namespace ui

View File

@ -584,8 +584,11 @@ void mame_ui_manager::draw_text_full(render_container *container, const char *or
void mame_ui_manager::draw_text_box(render_container *container, const char *text, ui::text_layout::text_justify justify, float xpos, float ypos, rgb_t backcolor)
{
// cap the maximum width
float maximum_width = 1.0f - UI_BOX_LR_BORDER * 2;
// create a layout
ui::text_layout layout = create_layout(container, 1.0f, justify);
ui::text_layout layout = create_layout(container, maximum_width, justify);
// add text to it
layout.add_text(text);