Skip to content

Commit 04fbf47

Browse files
RealBadAngelsapier
authored and
sapier
committedJun 18, 2014
Add tooltips for button, imagebutton and checkbox.
1 parent 65a4630 commit 04fbf47

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed
 

‎doc/lua_api.txt

+6-9
Original file line numberDiff line numberDiff line change
@@ -986,32 +986,28 @@ vertlabel[<X>,<Y>;<label>]
986986
^ label is the text on the label
987987
^ Position and size units are inventory slots
988988

989-
button[<X>,<Y>;<W>,<H>;<name>;<label>]
989+
button[<X>,<Y>;<W>,<H>;<name>;<label>;<tooltip>]
990990
^ Clickable button. When clicked, fields will be sent.
991991
^ x, y and name work as per field
992992
^ w and h are the size of the button
993993
^ label is the text on the button
994994
^ Position and size units are inventory slots
995+
^ tooltip is optional
995996

996997
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>]
997998
^ x, y, w, h, and name work as per button
998999
^ texture name is the filename of an image
9991000
^ Position and size units are inventory slots
10001001

1001-
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>]
1002-
^ x, y, w, h, and name work as per button
1003-
^ texture name is the filename of an image
1004-
^ Position and size units are inventory slots
1005-
^ noclip true meand imagebutton doesn't need to be within specified formsize
1006-
^ drawborder draw button bodrer or not
1007-
10081002
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>]
1003+
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>;<tooltip>]
10091004
^ x, y, w, h, and name work as per button
10101005
^ texture name is the filename of an image
10111006
^ Position and size units are inventory slots
10121007
^ noclip true meand imagebutton doesn't need to be within specified formsize
10131008
^ drawborder draw button bodrer or not
10141009
^ pressed texture name is the filename of an image on pressed state
1010+
^ tooltip is optional
10151011

10161012
item_image_button[<X>,<Y>;<W>,<H>;<item name>;<name>;<label>]
10171013
^ 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>]
10681064
^ index of currently selected dropdown item
10691065
^ color in hexadecimal format RRGGBB (only)
10701066

1071-
checkbox[<X>,<Y>;<name>;<label>;<selected>]
1067+
checkbox[<X>,<Y>;<name>;<label>;<selected>;<tooltip>]
10721068
^ show a checkbox
10731069
^ x and y position of checkbox
10741070
^ name fieldname data is transfered to lua
10751071
^ label to be shown left of checkbox
10761072
^ selected (optional) true/false
1073+
^ tooltip (optional)
10771074

10781075
table[<X>,<Y>;<W>,<H>;<name>;<cell 1>,<cell 2>,...,<cell n>;<selected idx>]
10791076
^ show scrollable table using options defined by the previous tableoptions[]

‎src/guiFormSpecMenu.cpp

+43-35
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
6161
<< parts[b] << "\"" << std::endl; \
6262
return; \
6363
}
64-
65-
extern gui::IGUIEnvironment* guienv;
66-
6764
/*
6865
GUIFormSpecMenu
6966
*/
@@ -87,7 +84,7 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
8784
m_form_src(fsrc),
8885
m_text_dst(tdst),
8986
m_ext_ptr(ext_ptr),
90-
m_font(guienv->getSkin()->getFont())
87+
m_font(dev->getGUIEnvironment()->getSkin()->getFont())
9188
{
9289
current_keys_pending.key_down = false;
9390
current_keys_pending.key_up = false;
@@ -375,15 +372,15 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
375372
{
376373
std::vector<std::string> parts = split(element,';');
377374

378-
if ((parts.size() == 3) || (parts.size() == 4)) {
375+
if ((parts.size() >= 3) || (parts.size() <= 5)) {
379376
std::vector<std::string> v_pos = split(parts[0],',');
380377
std::string name = parts[1];
381378
std::string label = parts[2];
382379
std::string selected = "";
383-
384-
if (parts.size() == 4)
385-
selected = parts[3];
386-
380+
381+
if (parts.size() >= 4)
382+
selected = parts[3];
383+
387384
MY_CHECKPOS("checkbox",0);
388385

389386
v2s32 pos = padding;
@@ -416,7 +413,8 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
416413
if (spec.fname == data->focused_fieldname) {
417414
Environment->setFocus(e);
418415
}
419-
416+
if (parts.size() >= 5)
417+
spec.tooltip = parts[4];
420418
m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
421419
m_fields.push_back(spec);
422420
return;
@@ -501,7 +499,7 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
501499
{
502500
std::vector<std::string> parts = split(element,';');
503501

504-
if (parts.size() == 4) {
502+
if (parts.size() == 4 || parts.size() == 5) {
505503
std::vector<std::string> v_pos = split(parts[0],',');
506504
std::vector<std::string> v_geom = split(parts[1],',');
507505
std::string name = parts[2];
@@ -542,7 +540,9 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
542540
if (spec.fname == data->focused_fieldname) {
543541
Environment->setFocus(e);
544542
}
545-
543+
if (parts.size() >= 5)
544+
spec.tooltip = parts[4];
545+
546546
m_fields.push_back(spec);
547547
return;
548548
}
@@ -1062,7 +1062,7 @@ void GUIFormSpecMenu::parseField(parserData* data,std::string element,
10621062
{
10631063
std::vector<std::string> parts = split(element,';');
10641064

1065-
if (parts.size() == 3) {
1065+
if (parts.size() == 3 || parts.size() == 4) {
10661066
parseSimpleField(data,parts);
10671067
return;
10681068
}
@@ -1165,7 +1165,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
11651165
{
11661166
std::vector<std::string> parts = split(element,';');
11671167

1168-
if ((parts.size() == 5) || (parts.size() == 7) || (parts.size() == 8)) {
1168+
if (((parts.size() >= 5) && (parts.size() <= 9)) && (parts.size() != 6)) {
11691169
std::vector<std::string> v_pos = split(parts[0],',');
11701170
std::vector<std::string> v_geom = split(parts[1],',');
11711171
std::string image_name = parts[2];
@@ -1182,20 +1182,18 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
11821182
geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
11831183
geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
11841184

1185-
bool noclip = false;
1185+
bool noclip = false;
11861186
bool drawborder = true;
1187-
1188-
if ((parts.size() >= 7)) {
1187+
std::string pressed_image_name = "";
1188+
1189+
if (parts.size() >= 7) {
11891190
if (parts[5] == "true")
11901191
noclip = true;
1191-
11921192
if (parts[6] == "false")
11931193
drawborder = false;
11941194
}
1195-
1196-
std::string pressed_image_name = "";
1197-
1198-
if ((parts.size() == 8)) {
1195+
1196+
if (parts.size() >= 8) {
11991197
pressed_image_name = parts[7];
12001198
}
12011199

@@ -1223,7 +1221,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
12231221
video::ITexture *texture = 0;
12241222
video::ITexture *pressed_texture = 0;
12251223
texture = m_tsrc->getTexture(image_name);
1226-
if (parts.size() == 8)
1224+
if (pressed_image_name != "")
12271225
pressed_texture = m_tsrc->getTexture(pressed_image_name);
12281226
else
12291227
pressed_texture = texture;
@@ -1233,6 +1231,8 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
12331231
if (spec.fname == data->focused_fieldname) {
12341232
Environment->setFocus(e);
12351233
}
1234+
if (parts.size() >= 9)
1235+
spec.tooltip = parts[8];
12361236

12371237
e->setUseAlphaChannel(true);
12381238
e->setImage(texture);
@@ -1375,8 +1375,7 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
13751375
spec.ftype = f_Button;
13761376
rect+=data->basepos-padding;
13771377
spec.rect=rect;
1378-
if (tooltip!="")
1379-
spec.tooltip=tooltip;
1378+
spec.tooltip = tooltip;
13801379
m_fields.push_back(spec);
13811380
return;
13821381
}
@@ -2051,24 +2050,33 @@ void GUIFormSpecMenu::drawMenu()
20512050
/*
20522051
Draw fields/buttons tooltips
20532052
*/
2054-
for(u32 i=0; i<m_fields.size(); i++)
2055-
{
2056-
const FieldSpec &spec = m_fields[i];
2057-
if (spec.tooltip != "")
2058-
{
2059-
core::rect<s32> rect = spec.rect;
2060-
if (rect.isPointInside(m_pointer))
2061-
{
2053+
gui::IGUIElement *hovered =
2054+
Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
2055+
2056+
if (hovered != NULL) {
2057+
s32 id = hovered->getID();
2058+
for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
2059+
iter != m_fields.end(); iter++) {
2060+
if ( (iter->fid == id) && (iter->tooltip != "") ) {
20622061
m_tooltip_element->setVisible(true);
20632062
this->bringToFront(m_tooltip_element);
2064-
m_tooltip_element->setText(narrow_to_wide(spec.tooltip).c_str());
2063+
m_tooltip_element->setText(narrow_to_wide(iter->tooltip).c_str());
20652064
s32 tooltip_x = m_pointer.X + 15;
20662065
s32 tooltip_y = m_pointer.Y + 15;
20672066
s32 tooltip_width = m_tooltip_element->getTextWidth() + 15;
2068-
s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
2067+
if (tooltip_x + tooltip_width > (s32)screenSize.X)
2068+
tooltip_x = (s32)screenSize.X - tooltip_width - 15;
2069+
int lines_count = 1;
2070+
size_t i = 0;
2071+
while ((i = iter->tooltip.find("\n", i)) != std::string::npos) {
2072+
lines_count++;
2073+
i += 2;
2074+
}
2075+
s32 tooltip_height = m_tooltip_element->getTextHeight() * lines_count + 5;
20692076
m_tooltip_element->setRelativePosition(core::rect<s32>(
20702077
core::position2d<s32>(tooltip_x, tooltip_y),
20712078
core::dimension2d<s32>(tooltip_width, tooltip_height)));
2079+
break;
20722080
}
20732081
}
20742082
}

0 commit comments

Comments
 (0)
Please sign in to comment.