X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=qcsrc%2Fmenu%2Fitem%2Flabel.qc;h=f7a782dabbf877a3b0bcef5adee11db333d8edc0;hb=62d736d8c3a51baf5fa3a4265e39a2b773704a91;hp=45a5a7fac44461aefa6e9e5984d0f1ae80a22050;hpb=c67e77ba36fb8dd44b39722e41943b6671b549ff;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/menu/item/label.qc b/qcsrc/menu/item/label.qc index 45a5a7fac..f7a782dab 100644 --- a/qcsrc/menu/item/label.qc +++ b/qcsrc/menu/item/label.qc @@ -1,183 +1,51 @@ -#ifndef ITEM_LABEL_H -#define ITEM_LABEL_H -#include "../item.qc" -CLASS(Label, Item) - METHOD(Label, configureLabel, void(entity, string, float, float)); - METHOD(Label, draw, void(entity)); - 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, isBold, float, 0) - ATTRIB(Label, text, string, string_null) - ATTRIB(Label, currentText, string, string_null) - ATTRIB(Label, fontSize, float, 8) - ATTRIB(Label, align, float, 0.5) - ATTRIB(Label, allowCut, float, 0) - ATTRIB(Label, allowColors, float, 0) - ATTRIB(Label, keepspaceLeft, float, 0) // for use by subclasses (radiobuttons for example) - ATTRIB(Label, keepspaceRight, float, 0) - ATTRIB(Label, marginLeft, float, 0) // alternate way to specify keepspace* (in characters from the font) - ATTRIB(Label, marginRight, float, 0) - ATTRIB(Label, realFontSize, vector, '0 0 0') - ATTRIB(Label, realOrigin, vector, '0 0 0') - ATTRIB(Label, alpha, float, 0.7) - ATTRIB(Label, colorL, vector, SKINCOLOR_TEXT) - ATTRIB(Label, disabled, float, 0) - 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 Label_toString(entity me) -{ - return me.text; -} -void Label_setText(entity me, string txt) -{ - me.text = txt; - if(txt != me.currentText) - { - if(me.currentText) - strunzone(me.currentText); - me.currentText = strzone(txt); - me.recalcPos = 1; - } -} -void Label_recalcPositionWithText(entity me, string t) -{ - float spaceAvail; - spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight; - - if(me.isBold) - draw_beginBoldFont(); - - float spaceUsed; - spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize); +#include "label.qh" - if(spaceUsed <= spaceAvail) + string Label_toString(entity me) { - if(!me.overrideRealOrigin_x) - me.realOrigin_x = me.align * (spaceAvail - spaceUsed) + me.keepspaceLeft; - if(!me.overrideCondenseFactor) - me.condenseFactor = 1; + return me.text; } - else if(me.allowCut || me.allowWrap) + void Label_setText(entity me, string txt) { - 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; - dprintf("NOTE: label text %s too wide for label, condensed by factor %f\n", t, me.condenseFactor); + me.text = txt; + if (txt != me.currentText) + { + strcpy(me.currentText, txt); + me.recalcPos = 1; + } } - - if(!me.overrideRealOrigin_y) + void Label_recalcPositionWithText(entity me, string t) { - float lines; - vector dfs; - vector fs; + float spaceAvail; + spaceAvail = 1 - me.keepspaceLeft - me.keepspaceRight; - // set up variables to draw in condensed size, but use hinting for original size - fs = me.realFontSize; - fs.x *= me.condenseFactor; + if (me.isBold) draw_beginBoldFont(); - dfs = draw_fontscale; - draw_fontscale.x *= me.condenseFactor; + float spaceUsed; + spaceUsed = draw_TextWidth(t, me.allowColors, me.realFontSize); - if(me.allowCut) // FIXME allowCut incompatible with align != 0 - lines = 1; - else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0 + if (spaceUsed <= spaceAvail) { - 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; - } + 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 - lines = 1; - - draw_fontscale = dfs; - - me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y); - } - - if(me.isBold) - draw_endBoldFont(); - - 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; - if(me.marginLeft) - me.keepspaceLeft = me.marginLeft * me.realFontSize.x; - if(me.marginRight) - me.keepspaceRight = me.marginRight * me.realFontSize.x; - - me.recalcPos = 1; -} -void Label_configureLabel(entity me, string txt, float sz, float algn) -{ - me.fontSize = sz; - me.align = algn; - me.setText(me, txt); -} -void Label_draw(entity me) -{ - string t; - vector o; - if(me.disabled) - draw_alpha *= me.disabledAlpha; - - if(me.textEntity) - { - t = me.textEntity.toString(me.textEntity); - if(t != me.currentText) { - if(me.currentText) - strunzone(me.currentText); - me.currentText = strzone(t); - me.recalcPos = 1; + if (!me.overrideRealOrigin_x) me.realOrigin_x = me.keepspaceLeft; + if (!me.overrideCondenseFactor) me.condenseFactor = spaceAvail / spaceUsed; + LOG_TRACEF("NOTE: label text %s too wide for label, condensed by factor %f", t, me.condenseFactor); } - } - else - t = me.text; - if(me.recalcPos) - me.recalcPositionWithText(me, t); - - if(me.fontSize) - if(t) + if (!me.overrideRealOrigin_y) { + float lines; vector dfs; vector fs; - if(me.isBold) - draw_beginBoldFont(); - // set up variables to draw in condensed size, but use hinting for original size fs = me.realFontSize; fs.x *= me.condenseFactor; @@ -185,31 +53,115 @@ void Label_draw(entity me) 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, fs), fs, me.colorL, me.alpha, me.allowColors); - else if(me.allowWrap) // FIXME allowWrap incompatible with align != 0 + if (me.allowCut) // FIXME allowCut incompatible with align != 0 { - getWrappedLine_remaining = t; - o = me.realOrigin; - while(getWrappedLine_remaining) + lines = 1; + } + else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0 + { + getWrappedLine_remaining = me.text; + lines = 0; + while (getWrappedLine_remaining) { - if (me.allowColors) - t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors); - else - 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; + 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 - draw_Text(me.realOrigin, t, fs, me.colorL, me.alpha, me.allowColors); + { + lines = 1; + } draw_fontscale = dfs; - if(me.isBold) - draw_endBoldFont(); + me.realOrigin_y = 0.5 * (1 - lines * me.realFontSize.y); + } + + if (me.isBold) draw_endBoldFont(); + + 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 = absSize.y == 0 ? 0 : (me.fontSize / absSize.y); + me.realFontSize_x = absSize.x == 0 ? 0 : (me.fontSize / absSize.x); + if (me.marginLeft) me.keepspaceLeft = me.marginLeft * me.realFontSize.x; + if (me.marginRight) me.keepspaceRight = me.marginRight * me.realFontSize.x; + + me.recalcPos = 1; + } + void Label_configureLabel(entity me, string txt, float sz, float algn) + { + me.fontSize = sz; + me.align = algn; + me.setText(me, txt); + } + void Label_draw(entity me) + { + string t; + vector o; + if (me.disabled) draw_alpha *= me.disabledAlpha; + + if (me.textEntity) + { + t = me.textEntity.toString(me.textEntity); + if (t != me.currentText) + { + strcpy(me.currentText, t); + me.recalcPos = 1; + } + } + else + { + t = me.text; } - SUPER(Label).draw(me); -} -#endif + if (me.recalcPos) me.recalcPositionWithText(me, t); + + if (me.fontSize) + if (t) + { + vector dfs; + vector fs; + + if (me.isBold) draw_beginBoldFont(); + + // 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, fs), fs, me.colorL, me.alpha, me.allowColors); + } + else if (me.allowWrap) // FIXME allowWrap incompatible with align != 0 + { + getWrappedLine_remaining = t; + o = me.realOrigin; + while (getWrappedLine_remaining) + { + if (me.allowColors) t = getWrappedLine((1 - me.keepspaceLeft - me.keepspaceRight), fs, draw_TextWidth_WithColors); + else 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, fs, me.colorL, me.alpha, me.allowColors); + } + + draw_fontscale = dfs; + + if (me.isBold) draw_endBoldFont(); + } + + SUPER(Label).draw(me); + }