widget::widget(const variant& v, game_logic::formula_callable* e) : environ_(e), w_(0), h_(0), x_(0), y_(0), zorder_(0), true_x_(0), true_y_(0), disabled_(false), disabled_opacity_(v["disabled_opacity"].as_int(127)), tooltip_displayed_(false), id_(v["id"].as_string_default()), align_h_(HALIGN_LEFT), align_v_(VALIGN_TOP) { if(v.has_key("width")) { w_ = v["width"].as_int(); } if(v.has_key("height")) { h_ = v["height"].as_int(); } if(v.has_key("wh")) { std::vector<int> iv = v["wh"].as_list_int(); ASSERT_LOG(iv.size() == 2, "WH attribute must be 2 integer elements."); w_ = iv[0]; h_ = iv[1]; } if(v.has_key("rect")) { std::vector<int> r = v["rect"].as_list_int(); ASSERT_LOG(r.size() == 4, "Four values must be supplied to the rect attribute"); set_loc(r[0], r[1]); set_dim(r[2], r[3]); } if(v.has_key("draw_area")) { std::vector<int> r = v["draw_area"].as_list_int(); ASSERT_LOG(r.size() == 4, "Four values must be supplied to the rect attribute"); set_loc(r[0], r[1]); set_dim(r[2], r[3]); } if(v.has_key("x")) { true_x_ = x_ = v["x"].as_int(); } if(v.has_key("y")) { true_y_ = y_ = v["y"].as_int(); } if(v.has_key("xy")) { std::vector<int> iv = v["xy"].as_list_int(); ASSERT_LOG(iv.size() == 2, "XY attribute must be 2 integer elements."); true_x_ = x_ = iv[0]; true_y_ = y_ = iv[1]; } zorder_ = v["zorder"].as_int(0); if(v.has_key("on_process")) { on_process_ = boost::bind(&widget::process_delegate, this); ffl_on_process_ = get_environment()->create_formula(v["on_process"]); } if(v.has_key("tooltip")) { if(v["tooltip"].is_string()) { set_tooltip(v["tooltip"].as_string(), v["tooltip_size"].as_int(18)); } else if(v["tooltip"].is_map()) { set_tooltip(v["tooltip"]["text"].as_string(), v["tooltip"]["size"].as_int(18)); } else { ASSERT_LOG(false, "Specify the tooltip as a string, e.g. \"tooltip\":\"Text to display on mouseover\", " "or a map, e.g. \"tooltip\":{\"text\":\"Text to display.\", \"size\":14}"); } } visible_ = v["visible"].as_bool(true); if(v.has_key("align_h")) { std::string align = v["align_h"].as_string(); if(align == "left") { align_h_ = HALIGN_LEFT; } else if(align == "middle" || align == "center" || align == "centre") { align_h_ = HALIGN_CENTER; } else if(align == "right") { align_h_ = HALIGN_RIGHT; } else { ASSERT_LOG(false, "Invalid align_h attribute given: " << align); } } if(v.has_key("align_v")) { std::string align = v["align_v"].as_string(); if(align == "top") { align_v_ = VALIGN_TOP; } else if(align == "middle" || align == "center" || align == "centre") { align_v_ = VALIGN_CENTER; } else if(align == "bottom") { align_v_ = VALIGN_BOTTOM; } else { ASSERT_LOG(false, "Invalid align_v attribute given: " << align); } } disabled_ = !v["enabled"].as_bool(true); recalc_loc(); }
widget::widget(const variant& v, game_logic::formula_callable* e) : environ_(e), w_(0), h_(0), x_(0), y_(0), zorder_(0), true_x_(0), true_y_(0), disabled_(false), disabled_opacity_(v["disabled_opacity"].as_int(127)), tooltip_displayed_(false), id_(v["id"].as_string_default()), align_h_(HALIGN_LEFT), align_v_(VALIGN_TOP), tooltip_display_delay_(v["tooltip_delay"].as_int(500)), tooltip_ticks_(INT_MAX), resolution_(v["frame_size"].as_int(0)), display_alpha_(v["alpha"].as_int(256)), pad_w_(0), pad_h_(0), claim_mouse_events_(v["claim_mouse_events"].as_bool(true)), draw_with_object_shader_(v["draw_with_object_shader"].as_bool(true)) { set_alpha(display_alpha_ < 0 ? 0 : (display_alpha_ > 256 ? 256 : display_alpha_)); if(v.has_key("width")) { w_ = v["width"].as_int(); } if(v.has_key("height")) { h_ = v["height"].as_int(); } if(v.has_key("wh")) { std::vector<int> iv = v["wh"].as_list_int(); ASSERT_LOG(iv.size() == 2, "WH attribute must be 2 integer elements."); w_ = iv[0]; h_ = iv[1]; } if(v.has_key("rect")) { std::vector<int> r = v["rect"].as_list_int(); ASSERT_LOG(r.size() == 4, "Four values must be supplied to the rect attribute"); set_loc(r[0], r[1]); set_dim(r[2], r[3]); } if(v.has_key("draw_area")) { std::vector<int> r = v["draw_area"].as_list_int(); ASSERT_LOG(r.size() == 4, "Four values must be supplied to the rect attribute"); set_loc(r[0], r[1]); set_dim(r[2], r[3]); } if(v.has_key("x")) { true_x_ = x_ = v["x"].as_int(); } if(v.has_key("y")) { true_y_ = y_ = v["y"].as_int(); } if(v.has_key("xy")) { std::vector<int> iv = v["xy"].as_list_int(); ASSERT_LOG(iv.size() == 2, "XY attribute must be 2 integer elements."); true_x_ = x_ = iv[0]; true_y_ = y_ = iv[1]; } zorder_ = v["zorder"].as_int(0); if(v.has_key("on_process")) { on_process_ = boost::bind(&widget::process_delegate, this); ffl_on_process_ = get_environment()->create_formula(v["on_process"]); } if(v.has_key("tooltip")) { if(v["tooltip"].is_string()) { SDL_Color color = v.has_key("tooltip_color") ? graphics::color(v["tooltip_color"]).as_sdl_color() : graphics::color_yellow(); set_tooltip(v["tooltip"].as_string(), v["tooltip_size"].as_int(18), color, v["tooltip_font"].as_string_default()); } else if(v["tooltip"].is_map()) { SDL_Color color = v["tooltip"].has_key("color") ? graphics::color(v["tooltip"]["color"]).as_sdl_color() : graphics::color_yellow(); set_tooltip(v["tooltip"]["text"].as_string(), v["tooltip"]["size"].as_int(18), color, v["tooltip"]["font"].as_string_default()); } else { ASSERT_LOG(false, "Specify the tooltip as a string, e.g. \"tooltip\":\"Text to display on mouseover\", " "or a map, e.g. \"tooltip\":{\"text\":\"Text to display.\", \"size\":14}"); } } visible_ = v["visible"].as_bool(true); if(v.has_key("align_h")) { std::string align = v["align_h"].as_string(); if(align == "left") { align_h_ = HALIGN_LEFT; } else if(align == "middle" || align == "center" || align == "centre") { align_h_ = HALIGN_CENTER; } else if(align == "right") { align_h_ = HALIGN_RIGHT; } else { ASSERT_LOG(false, "Invalid align_h attribute given: " << align); } } if(v.has_key("align_v")) { std::string align = v["align_v"].as_string(); if(align == "top") { align_v_ = VALIGN_TOP; } else if(align == "middle" || align == "center" || align == "centre") { align_v_ = VALIGN_CENTER; } else if(align == "bottom") { align_v_ = VALIGN_BOTTOM; } else { ASSERT_LOG(false, "Invalid align_v attribute given: " << align); } } disabled_ = !v["enabled"].as_bool(true); if(v.has_key("frame")) { set_frame_set(v["frame"].as_string()); } if(v.has_key("frame_padding")) { ASSERT_LOG(v["frame_padding"].is_list() && v["frame_padding"].num_elements() == 2, "'pad' must be two element list"); set_padding(v["frame_padding"][0].as_int(), v["frame_padding"][1].as_int()); } if(v.has_key("frame_pad_width")) { set_padding(v["frame_pad_width"].as_int(), get_pad_height()); } if(v.has_key("frame_pad_height")) { set_padding(get_pad_width(), v["frame_pad_height"].as_int()); } recalc_loc(); }