00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_textwidget.hpp"
00012
00013 #include <stdarg.h>
00014
00015 #include "xrb_gl.hpp"
00016 #include "xrb_render.hpp"
00017 #include "xrb_screen.hpp"
00018
00019 namespace Xrb
00020 {
00021
00022 TextWidget::TextWidget (
00023 std::string const &text,
00024 ContainerWidget *const parent,
00025 std::string const &name)
00026 :
00027 Widget(parent, name),
00028 m_text(text),
00029 m_receiver_set_text(&TextWidget::SetText, this),
00030 m_receiver_set_text_v(&TextWidget::SetTextV, this)
00031 {
00032 m_accepts_focus = false;
00033 m_accepts_mouseover = false;
00034
00035 m_text_color = Color(1.0, 1.0, 1.0, 1.0);
00036 SetRenderTextColor(m_text_color);
00037 m_font = WidgetSkinFont(WidgetSkin::DEFAULT_FONT);
00038 m_is_min_width_fixed_to_text_width = false;
00039 m_is_max_width_fixed_to_text_width = false;
00040 m_is_min_height_fixed_to_text_height = false;
00041 m_is_max_height_fixed_to_text_height = false;
00042
00043 TextWidget::UpdateRenderBackground();
00044 TextWidget::UpdateRenderTextColor();
00045 TextWidget::UpdateRenderFont();
00046 }
00047
00048 void TextWidget::SetText (std::string const &text)
00049 {
00050 m_text = text;
00051 UpdateMinAndMaxSizesFromText();
00052 }
00053
00054 void TextWidget::SetTextColor (Color const &color)
00055 {
00056 m_text_color = color;
00057 UpdateRenderTextColor();
00058 }
00059
00060 void TextWidget::SetFont (Resource<Font> const &font)
00061 {
00062 if (m_font != font)
00063 {
00064 m_font = font;
00065 HandleChangedFont();
00066 }
00067 }
00068
00069 void TextWidget::SetFontFacePath (std::string const &font_face_path)
00070 {
00071 ASSERT1(!font_face_path.empty());
00072 ASSERT1(m_font.IsValid());
00073
00074 Resource<Font> font =
00075 Singleton::ResourceLibrary().LoadPath<Font>(
00076 Font::Create,
00077 font_face_path,
00078 m_font->PixelHeight());
00079 ASSERT1(font.IsValid());
00080
00081 if (m_font != font)
00082 {
00083 m_font = font;
00084 HandleChangedFont();
00085 }
00086 }
00087
00088 void TextWidget::SetFontHeightRatio (Float const font_height_ratio)
00089 {
00090 ASSERT1(font_height_ratio > 0.0f);
00091 ASSERT1(m_font.IsValid());
00092
00093 ScreenCoord font_height =
00094 GetWidgetSkin()->ScreenCoordFromRatio(font_height_ratio);
00095
00096 if (m_font->PixelHeight() != font_height)
00097 {
00098 m_font =
00099 Singleton::ResourceLibrary().LoadPath<Font>(
00100 Font::Create,
00101 m_font.Path(),
00102 font_height);
00103 ASSERT1(m_font.IsValid());
00104 HandleChangedFont();
00105 }
00106 }
00107
00108 void TextWidget::SetFontHeight (ScreenCoord const font_height)
00109 {
00110 ASSERT1(font_height > 0);
00111 ASSERT1(m_font.IsValid());
00112
00113 if (m_font->PixelHeight() != font_height)
00114 {
00115 m_font =
00116 Singleton::ResourceLibrary().LoadPath<Font>(
00117 Font::Create,
00118 m_font.Path(),
00119 font_height);
00120 ASSERT1(m_font.IsValid());
00121 HandleChangedFont();
00122 }
00123 }
00124
00125 void TextWidget::SetIsMinWidthFixedToTextWidth (bool const is_min_width_fixed_to_text_width)
00126 {
00127 if (m_is_min_width_fixed_to_text_width != is_min_width_fixed_to_text_width)
00128 {
00129 m_is_min_width_fixed_to_text_width = is_min_width_fixed_to_text_width;
00130 UpdateMinAndMaxSizesFromText();
00131 }
00132 }
00133
00134 void TextWidget::SetIsMaxWidthFixedToTextWidth (bool const is_max_width_fixed_to_text_width)
00135 {
00136 if (m_is_max_width_fixed_to_text_width != is_max_width_fixed_to_text_width)
00137 {
00138 m_is_max_width_fixed_to_text_width = is_max_width_fixed_to_text_width;
00139 UpdateMinAndMaxSizesFromText();
00140 }
00141 }
00142
00143 void TextWidget::SetIsWidthFixedToTextWidth (bool const is_width_fixed_to_text_width)
00144 {
00145 if (m_is_min_width_fixed_to_text_width != is_width_fixed_to_text_width ||
00146 m_is_max_width_fixed_to_text_width != is_width_fixed_to_text_width)
00147 {
00148 m_is_min_width_fixed_to_text_width = is_width_fixed_to_text_width;
00149 m_is_max_width_fixed_to_text_width = is_width_fixed_to_text_width;
00150 UpdateMinAndMaxSizesFromText();
00151 }
00152 }
00153
00154 void TextWidget::SetIsMinHeightFixedToTextHeight (bool const is_min_height_fixed_to_text_height)
00155 {
00156 if (m_is_min_height_fixed_to_text_height != is_min_height_fixed_to_text_height)
00157 {
00158 m_is_min_height_fixed_to_text_height = is_min_height_fixed_to_text_height;
00159 UpdateMinAndMaxSizesFromText();
00160 }
00161 }
00162
00163 void TextWidget::SetIsMaxHeightFixedToTextHeight (bool const is_max_height_fixed_to_text_height)
00164 {
00165 if (m_is_max_height_fixed_to_text_height != is_max_height_fixed_to_text_height)
00166 {
00167 m_is_max_height_fixed_to_text_height = is_max_height_fixed_to_text_height;
00168 UpdateMinAndMaxSizesFromText();
00169 }
00170 }
00171
00172 void TextWidget::SetIsHeightFixedToTextHeight (bool const is_height_fixed_to_text_height)
00173 {
00174 if (m_is_min_height_fixed_to_text_height != is_height_fixed_to_text_height ||
00175 m_is_max_height_fixed_to_text_height != is_height_fixed_to_text_height)
00176 {
00177 m_is_min_height_fixed_to_text_height = is_height_fixed_to_text_height;
00178 m_is_max_height_fixed_to_text_height = is_height_fixed_to_text_height;
00179 UpdateMinAndMaxSizesFromText();
00180 }
00181 }
00182
00183 void TextWidget::SetIsMinSizeFixedToTextSize (bool const is_min_size_fixed_to_text_size)
00184 {
00185 if (m_is_min_width_fixed_to_text_width != is_min_size_fixed_to_text_size ||
00186 m_is_min_height_fixed_to_text_height != is_min_size_fixed_to_text_size)
00187 {
00188 m_is_min_width_fixed_to_text_width = is_min_size_fixed_to_text_size;
00189 m_is_min_height_fixed_to_text_height = is_min_size_fixed_to_text_size;
00190 UpdateMinAndMaxSizesFromText();
00191 }
00192 }
00193
00194 void TextWidget::SetIsMaxSizeFixedToTextSize (bool const is_max_size_fixed_to_text_size)
00195 {
00196 if (m_is_max_width_fixed_to_text_width != is_max_size_fixed_to_text_size ||
00197 m_is_max_height_fixed_to_text_height != is_max_size_fixed_to_text_size)
00198 {
00199 m_is_max_width_fixed_to_text_width = is_max_size_fixed_to_text_size;
00200 m_is_max_height_fixed_to_text_height = is_max_size_fixed_to_text_size;
00201 UpdateMinAndMaxSizesFromText();
00202 }
00203 }
00204
00205 void TextWidget::SetIsSizeFixedToTextSize (bool const is_size_fixed_to_text_size)
00206 {
00207 if (m_is_min_width_fixed_to_text_width != is_size_fixed_to_text_size ||
00208 m_is_max_width_fixed_to_text_width != is_size_fixed_to_text_size ||
00209 m_is_min_height_fixed_to_text_height != is_size_fixed_to_text_size ||
00210 m_is_max_height_fixed_to_text_height != is_size_fixed_to_text_size)
00211 {
00212 m_is_min_width_fixed_to_text_width = is_size_fixed_to_text_size;
00213 m_is_max_width_fixed_to_text_width = is_size_fixed_to_text_size;
00214 m_is_min_height_fixed_to_text_height = is_size_fixed_to_text_size;
00215 m_is_max_height_fixed_to_text_height = is_size_fixed_to_text_size;
00216 UpdateMinAndMaxSizesFromText();
00217 }
00218 }
00219
00220 void TextWidget::SetRenderFont (Resource<Font> const &render_font)
00221 {
00222 if (m_render_font != render_font)
00223 {
00224 m_render_font = render_font;
00225 UpdateMinAndMaxSizesFromText();
00226 }
00227 }
00228
00229 void TextWidget::HandleChangedFont ()
00230 {
00231 UpdateRenderFont();
00232 }
00233
00234 void TextWidget::HandleChangedFrameMargins ()
00235 {
00236 Widget::HandleChangedFrameMargins();
00237 UpdateMinAndMaxSizesFromText();
00238 }
00239
00240 void TextWidget::HandleChangedContentMargins ()
00241 {
00242 Widget::HandleChangedContentMargins();
00243 UpdateMinAndMaxSizesFromText();
00244 }
00245
00246 void TextWidget::UpdateRenderTextColor ()
00247 {
00248 SetRenderTextColor(TextColor());
00249 }
00250
00251 void TextWidget::UpdateRenderFont ()
00252 {
00253 SetRenderFont(GetFont());
00254 }
00255
00256 ScreenCoordRect TextWidget::TextRect () const
00257 {
00258 return RenderFont()->StringRect(Text().c_str());
00259 }
00260
00261 void TextWidget::UpdateMinAndMaxSizesFromText ()
00262 {
00263 if (!RenderFont().IsValid())
00264 return;
00265
00266 ScreenCoordVector2 size(TextRect().Size() + 2 * (FrameMargins() + ContentMargins()));
00267
00268 if (m_is_min_width_fixed_to_text_width)
00269 SetSizeProperty(SizeProperties::MIN, Dim::X, size[Dim::X]);
00270 if (m_is_max_width_fixed_to_text_width)
00271 SetSizeProperty(SizeProperties::MAX, Dim::X, size[Dim::X]);
00272 if (m_is_min_height_fixed_to_text_height)
00273 SetSizeProperty(SizeProperties::MIN, Dim::Y, size[Dim::Y]);
00274 if (m_is_max_height_fixed_to_text_height)
00275 SetSizeProperty(SizeProperties::MAX, Dim::Y, size[Dim::Y]);
00276 SetSizePropertyEnabled(
00277 SizeProperties::MIN,
00278 Bool2(m_is_min_width_fixed_to_text_width, m_is_min_height_fixed_to_text_height));
00279 SetSizePropertyEnabled(
00280 SizeProperties::MAX,
00281 Bool2(m_is_max_width_fixed_to_text_width, m_is_max_height_fixed_to_text_height));
00282 }
00283
00284 }