00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_widgetskin.hpp"
00012
00013 #include "xrb_screen.hpp"
00014 #include "xrb_widgetbackground.hpp"
00015
00016 namespace Xrb
00017 {
00018
00019 WidgetSkin::WidgetSkin (Screen const *screen)
00020 {
00021 ASSERT1(screen != NULL);
00022 m_screen = screen;
00023
00024
00025
00026
00027
00028 m_widget_background[MODAL_WIDGET_BACKGROUND] =
00029 new WidgetBackgroundColored(Color(0.0f, 0.0f, 0.0f, 1.0f));
00030 m_widget_background[BUTTON_BACKGROUND] =
00031 new WidgetBackgroundColored(Color(0.0f, 0.0f, 0.6f, 1.0f));
00032 m_widget_background[BUTTON_MOUSEOVER_BACKGROUND] =
00033 new WidgetBackgroundColored(Color(0.2f, 0.2f, 0.7f, 1.0f));
00034 m_widget_background[BUTTON_PRESSED_BACKGROUND] =
00035 new WidgetBackgroundColored(Color(0.4f, 0.4f, 0.8f, 1.0f));
00036 m_widget_background[LINE_EDIT_BACKGROUND] =
00037 new WidgetBackgroundColored(Color(0.2f, 0.2f, 0.2f, 1.0f));
00038 m_widget_background[CHECK_BOX_BACKGROUND] =
00039 new WidgetBackgroundColored(Color(0.9f, 0.8f, 0.0f, 1.0f));
00040 m_widget_background[RADIO_BUTTON_BACKGROUND] =
00041 new WidgetBackgroundColored(Color(0.9f, 0.1f, 0.0f, 1.0f));
00042 m_widget_background[TOOLBAR_BUTTON_BACKGROUND] =
00043 new WidgetBackgroundColored(Color(0.0f, 0.4f, 0.0f, 1.0f));
00044 m_widget_background[TOOLBAR_BUTTON_MOUSEOVER_BACKGROUND] =
00045 new WidgetBackgroundColored(Color(0.4f, 0.8f, 0.4f, 1.0f));
00046 m_widget_background[TOOLBAR_BUTTON_CHECKED_BACKGROUND] =
00047 new WidgetBackgroundColored(Color(0.2f, 0.6f, 0.2f, 1.0f));
00048 m_widget_background[TOOLBAR_BUTTON_PRESSED_BACKGROUND] =
00049 new WidgetBackgroundColored(Color(0.6f, 1.0f, 0.6f, 1.0f));
00050
00051
00052
00053
00054
00055
00056
00057 m_font_specification[DEFAULT_FONT].m_font_height_ratio = 0.023f;
00058 UpdateFontHeight(DEFAULT_FONT);
00059 SetFontFacePath(
00060 DEFAULT_FONT,
00061 "resources/FreeSansBoldCustom.ttf");
00062
00063
00064
00065
00066
00067 SetTexturePath(CHECK_BOX_CHECK_TEXTURE, "resources/ui/black_checkmark.png");
00068 SetTexturePath(RADIO_BUTTON_CHECK_TEXTURE, "resources/ui/radiobutton_dot.png");
00069
00070
00071
00072
00073
00074 SetMarginRatios(DEFAULT_FRAME_MARGINS, FloatVector2(0.006667f, 0.006667f));
00075 SetMarginRatios(DEFAULT_CONTENT_MARGINS, FloatVector2::ms_zero);
00076 SetMarginRatios(LAYOUT_FRAME_MARGINS, FloatVector2(0.006667f, 0.006667f));
00077 SetMarginRatios(LAYOUT_SPACING_MARGINS, FloatVector2(0.006667f, 0.006667f));
00078 }
00079
00080 WidgetSkin::~WidgetSkin ()
00081 {
00082 for (Uint32 i = 0; i < WIDGET_BACKGROUND_TYPE_COUNT; ++i)
00083 Delete(m_widget_background[i]);
00084 }
00085
00086 WidgetSkin *WidgetSkin::CreateClone () const
00087 {
00088 WidgetSkin *retval = new WidgetSkin;
00089
00090 ASSERT1(m_screen != NULL);
00091 retval->m_screen = m_screen;
00092
00093 for (Uint32 i = 0; i < WIDGET_BACKGROUND_TYPE_COUNT; ++i)
00094 retval->m_widget_background[i] =
00095 m_widget_background[i] != NULL ?
00096 m_widget_background[i]->CreateClone() :
00097 NULL;
00098
00099 for (Uint32 i = 0; i < FONT_TYPE_COUNT; ++i)
00100 retval->m_font_specification[i] = m_font_specification[i];
00101
00102 for (Uint32 i = 0; i < TEXTURE_TYPE_COUNT; ++i)
00103 retval->m_texture[i] = m_texture[i];
00104
00105 for (Uint32 i = 0; i < MARGINS_TYPE_COUNT; ++i)
00106 retval->m_margins_specification[i] = m_margins_specification[i];
00107
00108 return retval;
00109 }
00110
00111 Float WidgetSkin::RatioFromScreenCoord (ScreenCoord const screen_coord) const
00112 {
00113 ASSERT1(m_screen != NULL);
00114 ASSERT1(m_screen->SizeRatioBasis() > 0);
00115 return static_cast<Float>(screen_coord) / m_screen->SizeRatioBasis();
00116 }
00117
00118 ScreenCoord WidgetSkin::ScreenCoordFromRatio (Float const ratio) const
00119 {
00120 ASSERT1(m_screen != NULL);
00121 ASSERT1(m_screen->SizeRatioBasis() > 0);
00122 return static_cast<ScreenCoord>(ratio * m_screen->SizeRatioBasis());
00123 }
00124
00125 FloatVector2 WidgetSkin::RatiosFromScreenCoords (
00126 ScreenCoordVector2 const &screen_coords) const
00127 {
00128 ASSERT1(m_screen != NULL);
00129 ASSERT1(m_screen->SizeRatioBasis() > 0);
00130 return screen_coords.StaticCast<Float>() /
00131 static_cast<Float>(m_screen->SizeRatioBasis());
00132 }
00133
00134 ScreenCoordVector2 WidgetSkin::ScreenCoordsFromRatios (
00135 FloatVector2 const &ratios) const
00136 {
00137 ASSERT1(m_screen != NULL);
00138 ASSERT1(m_screen->SizeRatioBasis() > 0);
00139 return (ratios *
00140 static_cast<Float>(m_screen->SizeRatioBasis())
00141 ).StaticCast<ScreenCoord>();
00142 }
00143
00144 void WidgetSkin::SetWidgetBackground (
00145 WidgetBackgroundType const widget_background_type,
00146 WidgetBackground const *const widget_background)
00147 {
00148 ASSERT1(widget_background_type < WIDGET_BACKGROUND_TYPE_COUNT);
00149 delete m_widget_background[widget_background_type];
00150 m_widget_background[widget_background_type] = widget_background;
00151 }
00152
00153 void WidgetSkin::SetFont (
00154 FontType const font_type,
00155 Resource<Font> const &font)
00156 {
00157 ASSERT1(font_type < FONT_TYPE_COUNT);
00158 ASSERT1(font.IsValid());
00159 m_font_specification[font_type].m_font = font;
00160 m_font_specification[font_type].m_font_height = font->PixelHeight();
00161 m_font_specification[font_type].m_font_height_ratio =
00162 RatioFromScreenCoord(font->PixelHeight());
00163 }
00164
00165 void WidgetSkin::SetFontFacePath (
00166 FontType const font_type,
00167 std::string const &font_face_path)
00168 {
00169 ASSERT1(font_type < FONT_TYPE_COUNT);
00170 m_font_specification[font_type].m_font_height =
00171 ScreenCoordFromRatio(
00172 m_font_specification[font_type].m_font_height_ratio);
00173 m_font_specification[font_type].m_font =
00174 Singleton::ResourceLibrary().LoadPath<Font>(
00175 Font::Create,
00176 font_face_path,
00177 m_font_specification[font_type].m_font_height);
00178 ASSERT1(m_font_specification[font_type].m_font.IsValid());
00179 }
00180
00181
00182 void WidgetSkin::SetFontHeightRatio (
00183 FontType const font_type,
00184 Float const font_height_ratio)
00185 {
00186 ASSERT1(font_type < FONT_TYPE_COUNT);
00187 ASSERT1(font_height_ratio > 0.0f);
00188 m_font_specification[font_type].m_font_height_ratio = font_height_ratio;
00189 m_font_specification[font_type].m_font_height =
00190 ScreenCoordFromRatio(font_height_ratio);
00191 m_font_specification[font_type].m_font =
00192 Singleton::ResourceLibrary().LoadPath<Font>(
00193 Font::Create,
00194 m_font_specification[font_type].m_font.Path(),
00195 m_font_specification[font_type].m_font_height);
00196 ASSERT1(m_font_specification[font_type].m_font.IsValid());
00197 }
00198
00199 void WidgetSkin::SetFontHeight (
00200 FontType const font_type,
00201 ScreenCoord const font_height)
00202 {
00203 ASSERT1(font_type < FONT_TYPE_COUNT);
00204 ASSERT1(font_height > 0);
00205 m_font_specification[font_type].m_font_height = font_height;
00206 m_font_specification[font_type].m_font_height_ratio =
00207 RatioFromScreenCoord(font_height);
00208 m_font_specification[font_type].m_font =
00209 Singleton::ResourceLibrary().LoadPath<Font>(
00210 Font::Create,
00211 m_font_specification[font_type].m_font.Path(),
00212 m_font_specification[font_type].m_font_height);
00213 ASSERT1(m_font_specification[font_type].m_font.IsValid());
00214 }
00215
00216 void WidgetSkin::SetTexture (
00217 TextureType const texture_type,
00218 Resource<GLTexture> const &texture)
00219 {
00220 ASSERT1(texture_type < TEXTURE_TYPE_COUNT);
00221 m_texture[texture_type] = texture;
00222 }
00223
00224 void WidgetSkin::SetTexturePath (
00225 TextureType const texture_type,
00226 std::string const &texture_path)
00227 {
00228 ASSERT1(texture_type < TEXTURE_TYPE_COUNT);
00229 m_texture[texture_type] =
00230 Singleton::ResourceLibrary().LoadPath<GLTexture>(
00231 GLTexture::Create,
00232 texture_path);
00233 }
00234
00235 void WidgetSkin::SetMarginRatios (
00236 MarginsType const margins_type,
00237 FloatVector2 const &margin_ratios)
00238 {
00239 ASSERT1(margins_type < MARGINS_TYPE_COUNT);
00240 ASSERT1(margin_ratios[Dim::X] >= static_cast<Float>(0));
00241 ASSERT1(margin_ratios[Dim::Y] >= static_cast<Float>(0));
00242 m_margins_specification[margins_type].m_margin_ratios = margin_ratios;
00243 m_margins_specification[margins_type].m_margins =
00244 ScreenCoordsFromRatios(margin_ratios);
00245 }
00246
00247 void WidgetSkin::SetMargins (
00248 MarginsType const margins_type,
00249 ScreenCoordVector2 const &margins)
00250 {
00251 ASSERT1(margins_type < MARGINS_TYPE_COUNT);
00252 ASSERT1(margins[Dim::X] >= 0);
00253 ASSERT1(margins[Dim::Y] >= 0);
00254 m_margins_specification[margins_type].m_margins = margins;
00255 m_margins_specification[margins_type].m_margin_ratios =
00256 RatiosFromScreenCoords(margins);
00257 }
00258
00259 void WidgetSkin::UpdateFontHeight (FontType const font_type)
00260 {
00261 ASSERT1(font_type < FONT_TYPE_COUNT);
00262 m_font_specification[font_type].m_font_height =
00263 ScreenCoordFromRatio(
00264 m_font_specification[font_type].m_font_height_ratio);
00265 }
00266
00267 void WidgetSkin::UpdateMargins (MarginsType const margins_type)
00268 {
00269 ASSERT1(margins_type < MARGINS_TYPE_COUNT);
00270 m_margins_specification[margins_type].m_margins =
00271 ScreenCoordsFromRatios(
00272 m_margins_specification[margins_type].m_margin_ratios);
00273 }
00274
00275 }