From 156ff7cf561f3678bfafd82f98b4cb5c4c40fcfd Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Tue, 28 Dec 2004 12:17:04 +0000 Subject: [PATCH] Minimal text-alignment ability in Text GUI objects This was SVN commit r1584. --- source/gui/CButton.cpp | 69 ++++--------------- source/gui/CText.cpp | 13 ++-- source/gui/CText.h | 6 ++ source/gui/IGUITextOwner.cpp | 44 ++++++++++++ source/gui/IGUITextOwner.h | 5 ++ .../gui/scripting/JSInterface_IGUIObject.cpp | 64 ++++++++++++++++- 6 files changed, 140 insertions(+), 61 deletions(-) diff --git a/source/gui/CButton.cpp b/source/gui/CButton.cpp index 3f06a1b55d..022c8b2ef4 100755 --- a/source/gui/CButton.cpp +++ b/source/gui/CButton.cpp @@ -17,22 +17,22 @@ using namespace std; //------------------------------------------------------------------- CButton::CButton() { - AddSetting(GUIST_float, "buffer-zone"); - AddSetting(GUIST_CGUIString, "caption"); - AddSetting(GUIST_CStr, "font"); + AddSetting(GUIST_float, "buffer-zone"); + AddSetting(GUIST_CGUIString, "caption"); + AddSetting(GUIST_CStr, "font"); AddSetting(GUIST_CGUISpriteInstance, "sprite"); AddSetting(GUIST_CGUISpriteInstance, "sprite-over"); AddSetting(GUIST_CGUISpriteInstance, "sprite-pressed"); AddSetting(GUIST_CGUISpriteInstance, "sprite-disabled"); - AddSetting(GUIST_int, "cell-id"); - AddSetting(GUIST_EAlign, "text-align"); - AddSetting(GUIST_EVAlign, "text-valign"); - AddSetting(GUIST_CColor, "textcolor"); - AddSetting(GUIST_CColor, "textcolor-over"); - AddSetting(GUIST_CColor, "textcolor-pressed"); - AddSetting(GUIST_CColor, "textcolor-disabled"); - AddSetting(GUIST_CStr, "tooltip"); - AddSetting(GUIST_CStr, "tooltip-style"); + AddSetting(GUIST_int, "cell-id"); + AddSetting(GUIST_EAlign, "text-align"); + AddSetting(GUIST_EVAlign, "text-valign"); + AddSetting(GUIST_CColor, "textcolor"); + AddSetting(GUIST_CColor, "textcolor-over"); + AddSetting(GUIST_CColor, "textcolor-pressed"); + AddSetting(GUIST_CColor, "textcolor-disabled"); + AddSetting(GUIST_CStr, "tooltip"); + AddSetting(GUIST_CStr, "tooltip-style"); // Add text AddText(new SGUIText()); @@ -60,50 +60,7 @@ void CButton::SetupText() *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, m_CachedActualSize.GetWidth(), 0, this); - // Set position of text - - // Check which alignment to use! - EAlign align; - EVAlign valign; - float bz; - GUI::GetSetting(this, "text-align", align); - GUI::GetSetting(this, "text-valign", valign); - GUI::GetSetting(this, "buffer-zone", bz); - - switch (align) - { - case EAlign_Left: - m_TextPos.x = m_CachedActualSize.left + bz; - break; - case EAlign_Center: - // Round to integer pixel values, else the fonts look awful - m_TextPos.x = floorf(m_CachedActualSize.CenterPoint().x - m_GeneratedTexts[0]->m_Size.cx/2.f); - break; - case EAlign_Right: - m_TextPos.x = m_CachedActualSize.right - m_GeneratedTexts[0]->m_Size.cx - bz; - break; - default: - debug_warn("Broken EAlign in CButton::SetupText()"); - break; - } - - switch (valign) - { - case EVAlign_Top: - m_TextPos.y = m_CachedActualSize.top + bz; - break; - case EVAlign_Center: - // Round to integer pixel values, else the fonts look awful - m_TextPos.y = floorf(m_CachedActualSize.CenterPoint().y - m_GeneratedTexts[0]->m_Size.cy/2.f); - break; - case EVAlign_Bottom: - m_TextPos.y = m_CachedActualSize.bottom - m_GeneratedTexts[0]->m_Size.cy - bz; - break; - default: - debug_warn("Broken EVAlign in CButton::SetupText()"); - break; - } - + CalculateTextPosition(m_CachedActualSize, m_TextPos, *m_GeneratedTexts[0]); } void CButton::HandleMessage(const SGUIMessage &Message) diff --git a/source/gui/CText.cpp b/source/gui/CText.cpp index fd19ced348..0083989d02 100755 --- a/source/gui/CText.cpp +++ b/source/gui/CText.cpp @@ -27,12 +27,11 @@ CText::CText() AddSetting(GUIST_CStr, "scrollbar-style"); AddSetting(GUIST_CGUISpriteInstance, "sprite"); AddSetting(GUIST_int, "cell-id"); + AddSetting(GUIST_EAlign, "text-align"); + AddSetting(GUIST_EVAlign, "text-valign"); AddSetting(GUIST_CColor, "textcolor"); AddSetting(GUIST_CStr, "tooltip"); AddSetting(GUIST_CStr, "tooltip-style"); - // TODO Gee: (2004-08-14) - // Add a setting for buffer zone - //AddSetting(GUIST_int, " //GUI::SetSetting(this, "ghost", true); GUI::SetSetting(this, "scrollbar", false); @@ -79,6 +78,9 @@ void CText::SetupText() GUI::GetSetting(this, "buffer-zone", buffer_zone); *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, width, buffer_zone, this); + if (! scrollbar) + CalculateTextPosition(m_CachedActualSize, m_TextPos, *m_GeneratedTexts[0]); + // Setup scrollbar if (scrollbar) { @@ -178,6 +180,9 @@ void CText::Draw() GUI::GetSetting(this, "textcolor", color); // Draw text - IGUITextOwner::Draw(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz+0.1f); + if (scrollbar) + IGUITextOwner::Draw(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz+0.1f); + else + IGUITextOwner::Draw(0, color, m_TextPos, bz+0.1f); } } diff --git a/source/gui/CText.h b/source/gui/CText.h index f1067b58ad..7b43c16cef 100755 --- a/source/gui/CText.h +++ b/source/gui/CText.h @@ -71,6 +71,12 @@ protected: * Draws the Text */ virtual void Draw(); + + /** + * Placement of text. Ignored when scrollbars are active. + */ + CPos m_TextPos; + }; #endif diff --git a/source/gui/IGUITextOwner.cpp b/source/gui/IGUITextOwner.cpp index 8a65aa0766..ede1f5cfb0 100755 --- a/source/gui/IGUITextOwner.cpp +++ b/source/gui/IGUITextOwner.cpp @@ -75,3 +75,47 @@ void IGUITextOwner::Draw(const int &index, const CColor &color, const CPos &pos, GetGUI()->DrawText(*m_GeneratedTexts[index], color, pos, z); } } + +void IGUITextOwner::CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text) +{ + EAlign align; + EVAlign valign; + float bz; + GUI::GetSetting(this, "text-align", align); + GUI::GetSetting(this, "text-valign", valign); + GUI::GetSetting(this, "buffer-zone", bz); + + switch (align) + { + case EAlign_Left: + TextPos.x = ObjSize.left + bz; + break; + case EAlign_Center: + // Round to integer pixel values, else the fonts look awful + TextPos.x = floorf(ObjSize.CenterPoint().x - Text.m_Size.cx/2.f); + break; + case EAlign_Right: + TextPos.x = ObjSize.right - Text.m_Size.cx - bz; + break; + default: + debug_warn("Broken EAlign in CButton::SetupText()"); + break; + } + + switch (valign) + { + case EVAlign_Top: + TextPos.y = ObjSize.top + bz; + break; + case EVAlign_Center: + // Round to integer pixel values, else the fonts look awful + TextPos.y = floorf(ObjSize.CenterPoint().y - Text.m_Size.cy/2.f); + break; + case EVAlign_Bottom: + TextPos.y = ObjSize.bottom - Text.m_Size.cy - bz; + break; + default: + debug_warn("Broken EVAlign in CButton::SetupText()"); + break; + } +} \ No newline at end of file diff --git a/source/gui/IGUITextOwner.h b/source/gui/IGUITextOwner.h index 4de2d175b8..6fd9004427 100755 --- a/source/gui/IGUITextOwner.h +++ b/source/gui/IGUITextOwner.h @@ -83,6 +83,11 @@ protected: * Texts that are generated and ready to be rendered. */ std::vector m_GeneratedTexts; + + /** + * Calculate the position for the text, based on the alignment. + */ + void CalculateTextPosition(CRect &ObjSize, CPos &TextPos, SGUIText &Text); }; #endif diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index 49ce650634..d5b869e0f8 100755 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -200,7 +200,37 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval break; } - // TODO Gee: (2004-09-01) EAlign and EVAlign too. + case GUIST_EAlign: + { + EAlign value; + GUI::GetSetting(e, propName, value); + CStr word; + switch (value) + { + case EAlign_Left: word = "left"; break; + case EAlign_Right: word = "right"; break; + case EAlign_Center: word = "center"; break; + default: debug_warn("Invalid EAlign!"); word = "error"; break; + } + *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, word)); + break; + } + + case GUIST_EVAlign: + { + EVAlign value; + GUI::GetSetting(e, propName, value); + CStr word; + switch (value) + { + case EVAlign_Top: word = "top"; break; + case EVAlign_Bottom: word = "bottom"; break; + case EVAlign_Center: word = "center"; break; + default: debug_warn("Invalid EVAlign!"); word = "error"; break; + } + *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, word)); + break; + } default: JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str()); @@ -273,6 +303,38 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval break; } + case GUIST_EAlign: + { + CStr value (JS_GetStringBytes(JS_ValueToString(cx, *vp))); + EAlign a; + if (value == "left") a = EAlign_Left; + else if (value == "right") a = EAlign_Right; + else if (value == "center" || value == "centre") a = EAlign_Center; + else + { + JS_ReportError(cx, "Invalid alignment (should be 'left', 'right' or 'center')"); + return JS_FALSE; + } + GUI::SetSetting(e, propName, a); + break; + } + + case GUIST_EVAlign: + { + CStr value (JS_GetStringBytes(JS_ValueToString(cx, *vp))); + EVAlign a; + if (value == "top") a = EVAlign_Top; + else if (value == "bottom") a = EVAlign_Bottom; + else if (value == "center" || value == "centre") a = EVAlign_Center; + else + { + JS_ReportError(cx, "Invalid alignment (should be 'top', 'bottom' or 'center')"); + return JS_FALSE; + } + GUI::SetSetting(e, propName, a); + break; + } + case GUIST_int: { int32 value;