Add tooltips for button, imagebutton and checkbox.

This commit is contained in:
RealBadAngel 2014-06-19 00:22:03 +02:00 committed by sapier
parent 65a4630f31
commit 04fbf47744
2 changed files with 49 additions and 44 deletions

@ -986,32 +986,28 @@ vertlabel[<X>,<Y>;<label>]
^ label is the text on the label ^ label is the text on the label
^ Position and size units are inventory slots ^ Position and size units are inventory slots
button[<X>,<Y>;<W>,<H>;<name>;<label>] button[<X>,<Y>;<W>,<H>;<name>;<label>;<tooltip>]
^ Clickable button. When clicked, fields will be sent. ^ Clickable button. When clicked, fields will be sent.
^ x, y and name work as per field ^ x, y and name work as per field
^ w and h are the size of the button ^ w and h are the size of the button
^ label is the text on the button ^ label is the text on the button
^ Position and size units are inventory slots ^ Position and size units are inventory slots
^ tooltip is optional
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>] image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>]
^ x, y, w, h, and name work as per button ^ x, y, w, h, and name work as per button
^ texture name is the filename of an image ^ texture name is the filename of an image
^ Position and size units are inventory slots ^ Position and size units are inventory slots
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>]
^ x, y, w, h, and name work as per button
^ texture name is the filename of an image
^ Position and size units are inventory slots
^ noclip true meand imagebutton doesn't need to be within specified formsize
^ drawborder draw button bodrer or not
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>] image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>]
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>;<tooltip>]
^ x, y, w, h, and name work as per button ^ x, y, w, h, and name work as per button
^ texture name is the filename of an image ^ texture name is the filename of an image
^ Position and size units are inventory slots ^ Position and size units are inventory slots
^ noclip true meand imagebutton doesn't need to be within specified formsize ^ noclip true meand imagebutton doesn't need to be within specified formsize
^ drawborder draw button bodrer or not ^ drawborder draw button bodrer or not
^ pressed texture name is the filename of an image on pressed state ^ pressed texture name is the filename of an image on pressed state
^ tooltip is optional
item_image_button[<X>,<Y>;<W>,<H>;<item name>;<name>;<label>] item_image_button[<X>,<Y>;<W>,<H>;<item name>;<name>;<label>]
^ x, y, w, h, name and label work as per button ^ x, y, w, h, name and label work as per button
@ -1068,12 +1064,13 @@ dropdown[<X>,<Y>;<W>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]
^ index of currently selected dropdown item ^ index of currently selected dropdown item
^ color in hexadecimal format RRGGBB (only) ^ color in hexadecimal format RRGGBB (only)
checkbox[<X>,<Y>;<name>;<label>;<selected>] checkbox[<X>,<Y>;<name>;<label>;<selected>;<tooltip>]
^ show a checkbox ^ show a checkbox
^ x and y position of checkbox ^ x and y position of checkbox
^ name fieldname data is transfered to lua ^ name fieldname data is transfered to lua
^ label to be shown left of checkbox ^ label to be shown left of checkbox
^ selected (optional) true/false ^ selected (optional) true/false
^ tooltip (optional)
table[<X>,<Y>;<W>,<H>;<name>;<cell 1>,<cell 2>,...,<cell n>;<selected idx>] table[<X>,<Y>;<W>,<H>;<name>;<cell 1>,<cell 2>,...,<cell n>;<selected idx>]
^ show scrollable table using options defined by the previous tableoptions[] ^ show scrollable table using options defined by the previous tableoptions[]

@ -61,9 +61,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<< parts[b] << "\"" << std::endl; \ << parts[b] << "\"" << std::endl; \
return; \ return; \
} }
extern gui::IGUIEnvironment* guienv;
/* /*
GUIFormSpecMenu GUIFormSpecMenu
*/ */
@ -87,7 +84,7 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
m_form_src(fsrc), m_form_src(fsrc),
m_text_dst(tdst), m_text_dst(tdst),
m_ext_ptr(ext_ptr), m_ext_ptr(ext_ptr),
m_font(guienv->getSkin()->getFont()) m_font(dev->getGUIEnvironment()->getSkin()->getFont())
{ {
current_keys_pending.key_down = false; current_keys_pending.key_down = false;
current_keys_pending.key_up = false; current_keys_pending.key_up = false;
@ -375,13 +372,13 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
{ {
std::vector<std::string> parts = split(element,';'); std::vector<std::string> parts = split(element,';');
if ((parts.size() == 3) || (parts.size() == 4)) { if ((parts.size() >= 3) || (parts.size() <= 5)) {
std::vector<std::string> v_pos = split(parts[0],','); std::vector<std::string> v_pos = split(parts[0],',');
std::string name = parts[1]; std::string name = parts[1];
std::string label = parts[2]; std::string label = parts[2];
std::string selected = ""; std::string selected = "";
if (parts.size() == 4) if (parts.size() >= 4)
selected = parts[3]; selected = parts[3];
MY_CHECKPOS("checkbox",0); MY_CHECKPOS("checkbox",0);
@ -416,7 +413,8 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
if (spec.fname == data->focused_fieldname) { if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e); Environment->setFocus(e);
} }
if (parts.size() >= 5)
spec.tooltip = parts[4];
m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e)); m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
m_fields.push_back(spec); m_fields.push_back(spec);
return; return;
@ -501,7 +499,7 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
{ {
std::vector<std::string> parts = split(element,';'); std::vector<std::string> parts = split(element,';');
if (parts.size() == 4) { if (parts.size() == 4 || parts.size() == 5) {
std::vector<std::string> v_pos = split(parts[0],','); std::vector<std::string> v_pos = split(parts[0],',');
std::vector<std::string> v_geom = split(parts[1],','); std::vector<std::string> v_geom = split(parts[1],',');
std::string name = parts[2]; std::string name = parts[2];
@ -542,6 +540,8 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
if (spec.fname == data->focused_fieldname) { if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e); Environment->setFocus(e);
} }
if (parts.size() >= 5)
spec.tooltip = parts[4];
m_fields.push_back(spec); m_fields.push_back(spec);
return; return;
@ -1062,7 +1062,7 @@ void GUIFormSpecMenu::parseField(parserData* data,std::string element,
{ {
std::vector<std::string> parts = split(element,';'); std::vector<std::string> parts = split(element,';');
if (parts.size() == 3) { if (parts.size() == 3 || parts.size() == 4) {
parseSimpleField(data,parts); parseSimpleField(data,parts);
return; return;
} }
@ -1165,7 +1165,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
{ {
std::vector<std::string> parts = split(element,';'); std::vector<std::string> parts = split(element,';');
if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) { if (((parts.size() >= 5) && (parts.size() <= 9)) && (parts.size() != 6)) {
std::vector<std::string> v_pos = split(parts[0],','); std::vector<std::string> v_pos = split(parts[0],',');
std::vector<std::string> v_geom = split(parts[1],','); std::vector<std::string> v_geom = split(parts[1],',');
std::string image_name = parts[2]; std::string image_name = parts[2];
@ -1184,18 +1184,16 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
bool noclip = false; bool noclip = false;
bool drawborder = true; bool drawborder = true;
std::string pressed_image_name = "";
if ((parts.size() >= 7)) { if (parts.size() >= 7) {
if (parts[5] == "true") if (parts[5] == "true")
noclip = true; noclip = true;
if (parts[6] == "false") if (parts[6] == "false")
drawborder = false; drawborder = false;
} }
std::string pressed_image_name = ""; if (parts.size() >= 8) {
if ((parts.size() == 8)) {
pressed_image_name = parts[7]; pressed_image_name = parts[7];
} }
@ -1223,7 +1221,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
video::ITexture *texture = 0; video::ITexture *texture = 0;
video::ITexture *pressed_texture = 0; video::ITexture *pressed_texture = 0;
texture = m_tsrc->getTexture(image_name); texture = m_tsrc->getTexture(image_name);
if (parts.size() == 8) if (pressed_image_name != "")
pressed_texture = m_tsrc->getTexture(pressed_image_name); pressed_texture = m_tsrc->getTexture(pressed_image_name);
else else
pressed_texture = texture; pressed_texture = texture;
@ -1233,6 +1231,8 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
if (spec.fname == data->focused_fieldname) { if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e); Environment->setFocus(e);
} }
if (parts.size() >= 9)
spec.tooltip = parts[8];
e->setUseAlphaChannel(true); e->setUseAlphaChannel(true);
e->setImage(texture); e->setImage(texture);
@ -1375,8 +1375,7 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
spec.ftype = f_Button; spec.ftype = f_Button;
rect+=data->basepos-padding; rect+=data->basepos-padding;
spec.rect=rect; spec.rect=rect;
if (tooltip!="") spec.tooltip = tooltip;
spec.tooltip=tooltip;
m_fields.push_back(spec); m_fields.push_back(spec);
return; return;
} }
@ -2051,24 +2050,33 @@ void GUIFormSpecMenu::drawMenu()
/* /*
Draw fields/buttons tooltips Draw fields/buttons tooltips
*/ */
for(u32 i=0; i<m_fields.size(); i++) gui::IGUIElement *hovered =
{ Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
const FieldSpec &spec = m_fields[i];
if (spec.tooltip != "") if (hovered != NULL) {
{ s32 id = hovered->getID();
core::rect<s32> rect = spec.rect; for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
if (rect.isPointInside(m_pointer)) iter != m_fields.end(); iter++) {
{ if ( (iter->fid == id) && (iter->tooltip != "") ) {
m_tooltip_element->setVisible(true); m_tooltip_element->setVisible(true);
this->bringToFront(m_tooltip_element); this->bringToFront(m_tooltip_element);
m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str()); m_tooltip_element->setText(narrow_to_wide(iter->tooltip).c_str());
s32 tooltip_x = m_pointer.X + 15; s32 tooltip_x = m_pointer.X + 15;
s32 tooltip_y = m_pointer.Y + 15; s32 tooltip_y = m_pointer.Y + 15;
s32 tooltip_width = m_tooltip_element->getTextWidth() + 15; s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5; if (tooltip_x + tooltip_width > (s32)screenSize.X)
tooltip_x = (s32)screenSize.X - tooltip_width - 15;
int lines_count = 1;
size_t i = 0;
while ((i = iter->tooltip.find("\n", i)) != std::string::npos) {
lines_count++;
i += 2;
}
s32 tooltip_height = m_tooltip_element->getTextHeight() * lines_count + 5;
m_tooltip_element->setRelativePosition(core::rect<s32>( m_tooltip_element->setRelativePosition(core::rect<s32>(
core::position2d<s32>(tooltip_x, tooltip_y), core::position2d<s32>(tooltip_x, tooltip_y),
core::dimension2d<s32>(tooltip_width, tooltip_height))); core::dimension2d<s32>(tooltip_width, tooltip_height)));
break;
} }
} }
} }