X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Fmenu%2Fitem%2Flabel.c;h=e1543ce2756f60579cf1eeb1b5c79be924bc5a7b;hb=2f7b0095fa29fa4c49e0e6b88e6e7b432cfb6a31;hp=1ac50be751e0d3853fd07f9b26b826edea4c4b83;hpb=0e7ed909bffb4ff21f0c68d163edfc17487e380a;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/menu/item/label.c b/qcsrc/menu/item/label.c index 1ac50be75..e1543ce27 100644 --- a/qcsrc/menu/item/label.c +++ b/qcsrc/menu/item/label.c @@ -5,6 +5,7 @@ CLASS(Label) EXTENDS(Item) METHOD(Label, resizeNotify, void(entity, vector, vector, vector, vector)) METHOD(Label, setText, void(entity, string)) METHOD(Label, toString, string(entity)) + METHOD(Label, recalcPositionWithText, void(entity, string)) ATTRIB(Label, text, string, string_null) ATTRIB(Label, fontSize, float, 8) ATTRIB(Label, align, float, 0.5) @@ -22,22 +23,96 @@ CLASS(Label) EXTENDS(Item) ATTRIB(Label, disabledAlpha, float, 0.3) ATTRIB(Label, textEntity, entity, NULL) ATTRIB(Label, allowWrap, float, 0) + ATTRIB(Label, recalcPos, float, 0) + ATTRIB(Label, condenseFactor, float, 1) + ATTRIB(Label, overrideRealOrigin, vector, '0 0 0') + ATTRIB(Label, overrideCondenseFactor, float, 0) ENDCLASS(Label) #endif #ifdef IMPLEMENTATION -string toStringLabel(entity me) +string Label_toString(entity me) { return me.text; } -void setTextLabel(entity me, string txt) +void Label_setText(entity me, string txt) { me.text = txt; - me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(me.text, me.allowColors, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft; + me.recalcPos = 1; } -void resizeNotifyLabel(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize) +void Label_recalcPositionWithText(entity me, string t) { - resizeNotifyItem(me, relOrigin, relSize, absOrigin, absSize); + float spaceAvail; + spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight; + + float spaceUsed; + spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize); + + if(spaceUsed <= spaceAvail) + { + if(!me.overrideRealOrigin_x) + me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft; + if(!me.overrideCondenseFactor) + me.condenseFactor = 1; + } + else if(me.allowCut || me.allowWrap) + { + if(!me.overrideRealOrigin_x) + me.realOrigin_x = me.keepspaceLeft; + if(!me.overrideCondenseFactor) + me.condenseFactor = 1; + } + else + { + if(!me.overrideRealOrigin_x) + me.realOrigin_x = me.keepspaceLeft; + if(!me.overrideCondenseFactor) + me.condenseFactor = spaceAvail / spaceUsed; + dprint(sprintf(_("NOTE: label text %s too wide for label, condensed by factor %f\n"), t, me.condenseFactor)); + } + + if(!me.overrideRealOrigin_y) + { + float lines; + vector dfs; + vector fs; + + // set up variables to draw in condensed size, but use hinting for original size + fs = me.realFontSize; + fs_x *= me.condenseFactor; + + dfs = draw_fontscale; + draw_fontscale_x *= me.condenseFactor; + + if(me.allowCut) // FIXME allowCut incompatible with align != 0 + lines = 1; + else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0 + { + getWrappedLine_remaining = me.text; + lines = 0; + while(getWrappedLine_remaining) + { + if (me.allowColors) + getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors); + else + getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors); + ++lines; + } + } + else + lines = 1; + + draw_fontscale = dfs; + + me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize_y); + } + + me.recalcPos = 0; +} +void Label_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize) +{ + SUPER(Label).resizeNotify(me, relOrigin, relSize, absOrigin, absSize); + // absSize_y is height of label me.realFontSize_y = me.fontSize / absSize_y; me.realFontSize_x = me.fontSize / absSize_x; @@ -45,16 +120,16 @@ void resizeNotifyLabel(entity me, vector relOrigin, vector relSize, vector absOr me.keepspaceLeft = me.marginLeft * me.realFontSize_x; if(me.marginRight) me.keepspaceRight = me.marginRight * me.realFontSize_x; - me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(me.text, me.allowColors, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft; - me.realOrigin_y = 0.5 * (1 - me.realFontSize_y); + + me.recalcPos = 1; } -void configureLabelLabel(entity me, string txt, float sz, float algn) +void Label_configureLabel(entity me, string txt, float sz, float algn) { me.fontSize = sz; me.align = algn; me.setText(me, txt); } -void drawLabel(entity me) +void Label_draw(entity me) { string t; vector o; @@ -64,16 +139,29 @@ void drawLabel(entity me) if(me.textEntity) { t = me.textEntity.toString(me.textEntity); - me.realOrigin_x = me.align * (1 - me.keepspaceLeft - me.keepspaceRight - min(draw_TextWidth(t, 0, me.realFontSize), (1 - me.keepspaceLeft - me.keepspaceRight))) + me.keepspaceLeft; + me.recalcPos = 1; } else t = me.text; - + + if(me.recalcPos) + me.recalcPositionWithText(me, t); + if(me.fontSize) if(t) { + vector dfs; + vector fs; + + // set up variables to draw in condensed size, but use hinting for original size + fs = me.realFontSize; + fs_x *= me.condenseFactor; + + dfs = draw_fontscale; + draw_fontscale_x *= me.condenseFactor; + if(me.allowCut) // FIXME allowCut incompatible with align != 0 - draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, me.realFontSize), me.realFontSize, me.colorL, me.alpha, me.allowColors); + draw_Text(me.realOrigin, draw_TextShortenToWidth(t, (1 - me.keepspaceLeft - me.keepspaceRight), me.allowColors, fs), fs, me.colorL, me.alpha, me.allowColors); else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0 { getWrappedLine_remaining = t; @@ -81,15 +169,19 @@ void drawLabel(entity me) while(getWrappedLine_remaining) { if (me.allowColors) - t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithColors); + t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors); else - t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), me.realFontSize, draw_TextWidth_WithoutColors); - draw_Text(o, t, me.realFontSize, me.colorL, me.alpha, me.allowColors); + t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithoutColors); + draw_Text(o, t, fs, me.colorL, me.alpha, me.allowColors); o_y += me.realFontSize_y; } } else - draw_Text(me.realOrigin, t, me.realFontSize, me.colorL, me.alpha, me.allowColors); + draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors); + + draw_fontscale = dfs; } + + SUPER(Label).draw(me); } #endif