Skip to content

Instantly share code, notes, and snippets.

@sapier
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapier/81453b7c276ec902d2b7 to your computer and use it in GitHub Desktop.
Save sapier/81453b7c276ec902d2b7 to your computer and use it in GitHub Desktop.
------------------------------- doc/lua_api.txt -------------------------------
index f4e1fd2..6ed5608 100644
@@ -986,32 +986,28 @@ vertlabel[<X>,<Y>;<label>]
^ label is the text on the label
^ 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.
^ x, y and name work as per field
^ w and h are the size of the button
^ label is the text on the button
^ Position and size units are inventory slots
+^ tooltip is optional
image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>]
^ 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
-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>;<tooltip>]
^ 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
^ 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>]
^ 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
^ color in hexadecimal format RRGGBB (only)
-checkbox[<X>,<Y>;<name>;<label>;<selected>]
+checkbox[<X>,<Y>;<name>;<label>;<selected>;<tooltip>]
^ show a checkbox
^ x and y position of checkbox
^ name fieldname data is transfered to lua
^ label to be shown left of checkbox
^ selected (optional) true/false
+^ tooltip (optional)
table[<X>,<Y>;<W>,<H>;<name>;<cell 1>,<cell 2>,...,<cell n>;<selected idx>]
^ show scrollable table using options defined by the previous tableoptions[]
--------------------------- src/guiFormSpecMenu.cpp ---------------------------
index 9e9e59f..48551f7 100644
@@ -61,9 +61,6 @@
<< parts[b] << "\"" << std::endl; \
return; \
}
-
-extern gui::IGUIEnvironment* guienv;
-
/*
GUIFormSpecMenu
*/
@@ -87,7 +84,7 @@
m_form_src(fsrc),
m_text_dst(tdst),
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_up = false;
@@ -375,15 +372,16 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string 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::string name = parts[1];
std::string label = parts[2];
std::string selected = "";
-
- if (parts.size() == 4)
- selected = parts[3];
-
+ std::string tooltip = "";
+
+ if (parts.size() >= 4)
+ selected = parts[3];
+
MY_CHECKPOS("checkbox",0);
v2s32 pos = padding;
@@ -416,7 +414,8 @@ void GUIFormSpecMenu::parseCheckbox(parserData* data,std::string element)
if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e);
}
-
+ if (parts.size() >= 5)
+ spec.tooltip = parts[4];
m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
m_fields.push_back(spec);
return;
@@ -501,11 +500,12 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string 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_geom = split(parts[1],',');
std::string name = parts[2];
std::string label = parts[3];
+ std::string tooltip = "";
MY_CHECKPOS("button",0);
MY_CHECKGEOM("button",1);
@@ -542,7 +542,9 @@ void GUIFormSpecMenu::parseButton(parserData* data,std::string element,
if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e);
}
-
+ if (parts.size() >= 5)
+ spec.tooltip = parts[4];
+
m_fields.push_back(spec);
return;
}
@@ -1062,7 +1064,7 @@ void GUIFormSpecMenu::parseField(parserData* data,std::string element,
{
std::vector<std::string> parts = split(element,';');
- if (parts.size() == 3) {
+ if (parts.size() == 3 || parts.size() == 4) {
parseSimpleField(data,parts);
return;
}
@@ -1165,7 +1167,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string 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_geom = split(parts[1],',');
std::string image_name = parts[2];
@@ -1182,23 +1184,23 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
- bool noclip = false;
+ bool noclip = false;
bool drawborder = true;
-
- if ((parts.size() >= 7)) {
+ std::string pressed_image_name = "";
+ std::string tooltip = "";
+
+ if (parts.size() >= 7) {
if (parts[5] == "true")
noclip = true;
-
if (parts[6] == "false")
drawborder = false;
}
-
- std::string pressed_image_name = "";
-
- if ((parts.size() == 8)) {
+
+ if (parts.size() >= 8) {
pressed_image_name = parts[7];
}
+
core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
if(data->bp_set != 2)
@@ -1223,7 +1225,7 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
video::ITexture *texture = 0;
video::ITexture *pressed_texture = 0;
texture = m_tsrc->getTexture(image_name);
- if (parts.size() == 8)
+ if (pressed_image_name != "")
pressed_texture = m_tsrc->getTexture(pressed_image_name);
else
pressed_texture = texture;
@@ -1233,6 +1235,8 @@ void GUIFormSpecMenu::parseImageButton(parserData* data,std::string element,
if (spec.fname == data->focused_fieldname) {
Environment->setFocus(e);
}
+ if (parts.size() >= 9)
+ spec.tooltip = parts[8];
e->setUseAlphaChannel(true);
e->setImage(texture);
@@ -1375,8 +1379,7 @@ void GUIFormSpecMenu::parseItemImageButton(parserData* data,std::string element)
spec.ftype = f_Button;
rect+=data->basepos-padding;
spec.rect=rect;
- if (tooltip!="")
- spec.tooltip=tooltip;
+ spec.tooltip = tooltip;
m_fields.push_back(spec);
return;
}
@@ -2048,31 +2051,41 @@ void GUIFormSpecMenu::drawMenu()
*/
gui::IGUIElement::draw();
+
+
/*
Draw fields/buttons tooltips
*/
- for(u32 i=0; i<m_fields.size(); i++)
- {
- const FieldSpec &spec = m_fields[i];
- if (spec.tooltip != "")
- {
- core::rect<s32> rect = spec.rect;
- if (rect.isPointInside(m_pointer))
- {
+ gui::IGUIElement *hovered =
+ Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
+
+ if (hovered != NULL) {
+ s32 id = hovered->getID();
+ for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
+ iter != m_fields.end(); iter++) {
+ if ( (iter->fid == id) && (iter->tooltip != "") ) {
m_tooltip_element->setVisible(true);
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_y = m_pointer.Y + 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>(
core::position2d<s32>(tooltip_x, tooltip_y),
core::dimension2d<s32>(tooltip_width, tooltip_height)));
+ break;
}
}
}
-
/*
Draw dragged item stack
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment