Skip to content

Commit

Permalink
Formspec: Allow to specify frame loop for model[] (#10679)
Browse files Browse the repository at this point in the history
Add the ability to specify an animation frame loop range for the model[] formspec element.
  • Loading branch information
Thomas--S committed Dec 15, 2020
1 parent 3ed940f commit d0a38f6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
6 changes: 5 additions & 1 deletion doc/lua_api.txt
Expand Up @@ -2277,7 +2277,7 @@ Elements
* `frame duration`: Milliseconds between each frame. `0` means the frames don't advance.
* `frame start` (Optional): The index of the frame to start on. Default `1`.

### `model[<X>,<Y>;<W>,<H>;<name>;<mesh>;<textures>;<rotation X,Y>;<continuous>;<mouse control>]`
### `model[<X>,<Y>;<W>,<H>;<name>;<mesh>;<textures>;<rotation X,Y>;<continuous>;<mouse control>;<frame loop range>]`

* Show a mesh model.
* `name`: Element name that can be used for styling
Expand All @@ -2288,6 +2288,9 @@ Elements
The axes are euler angles in degrees.
* `continuous` (Optional): Whether the rotation is continuous. Default `false`.
* `mouse control` (Optional): Whether the model can be controlled with the mouse. Default `true`.
* `frame loop range` (Optional): Range of the animation frames.
* Defaults to the full range of all available frames.
* Syntax: `<begin>,<end>`

### `item_image[<X>,<Y>;<W>,<H>;<item name>]`

Expand Down Expand Up @@ -2789,6 +2792,7 @@ Some types may inherit styles from parent types.
* image_button
* item_image_button
* label
* model
* pwdfield, inherits from field
* scrollbar
* tabheader
Expand Down
21 changes: 16 additions & 5 deletions src/gui/guiFormSpecMenu.cpp
Expand Up @@ -70,15 +70,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#define MY_CHECKPOS(a,b) \
if (v_pos.size() != 2) { \
errorstream<< "Invalid pos for element " << a << "specified: \"" \
errorstream<< "Invalid pos for element " << a << " specified: \"" \
<< parts[b] << "\"" << std::endl; \
return; \
}

#define MY_CHECKGEOM(a,b) \
if (v_geom.size() != 2) { \
errorstream<< "Invalid geometry for element " << a << \
"specified: \"" << parts[b] << "\"" << std::endl; \
" specified: \"" << parts[b] << "\"" << std::endl; \
return; \
}
/*
Expand Down Expand Up @@ -2725,16 +2725,16 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
{
std::vector<std::string> parts = split(element, ';');

if (parts.size() < 5 || (parts.size() > 8 &&
if (parts.size() < 5 || (parts.size() > 9 &&
m_formspec_version <= FORMSPEC_API_VERSION)) {
errorstream << "Invalid model element (" << parts.size() << "): '" << element
<< "'" << std::endl;
return;
}

// Avoid length checks by resizing
if (parts.size() < 8)
parts.resize(8);
if (parts.size() < 9)
parts.resize(9);

std::vector<std::string> v_pos = split(parts[0], ',');
std::vector<std::string> v_geom = split(parts[1], ',');
Expand All @@ -2744,6 +2744,7 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
std::vector<std::string> vec_rot = split(parts[5], ',');
bool inf_rotation = is_yes(parts[6]);
bool mousectrl = is_yes(parts[7]) || parts[7].empty(); // default true
std::vector<std::string> frame_loop = split(parts[8], ',');

MY_CHECKPOS("model", 0);
MY_CHECKGEOM("model", 1);
Expand Down Expand Up @@ -2794,6 +2795,16 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
e->enableContinuousRotation(inf_rotation);
e->enableMouseControl(mousectrl);

s32 frame_loop_begin = 0;
s32 frame_loop_end = 0x7FFFFFFF;

if (frame_loop.size() == 2) {
frame_loop_begin = stoi(frame_loop[0]);
frame_loop_end = stoi(frame_loop[1]);
}

e->setFrameLoop(frame_loop_begin, frame_loop_end);

auto style = getStyleForElement("model", spec.fname);
e->setStyles(style);
e->drop();
Expand Down
9 changes: 9 additions & 0 deletions src/gui/guiScene.cpp
Expand Up @@ -152,6 +152,15 @@ void GUIScene::setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &sty
setBackgroundColor(style.getColor(StyleSpec::BGCOLOR, m_bgcolor));
}

/**
* Sets the frame loop range for the mesh
*/
void GUIScene::setFrameLoop(s32 begin, s32 end)
{
if (m_mesh->getStartFrame() != begin || m_mesh->getEndFrame() != end)
m_mesh->setFrameLoop(begin, end);
}

/* Camera control functions */

inline void GUIScene::calcOptimalDistance()
Expand Down
1 change: 1 addition & 0 deletions src/gui/guiScene.h
Expand Up @@ -36,6 +36,7 @@ class GUIScene : public gui::IGUIElement
scene::IAnimatedMeshSceneNode *setMesh(scene::IAnimatedMesh *mesh = nullptr);
void setTexture(u32 idx, video::ITexture *texture);
void setBackgroundColor(const video::SColor &color) noexcept { m_bgcolor = color; };
void setFrameLoop(s32 begin, s32 end);
void enableMouseControl(bool enable) noexcept { m_mouse_ctrl = enable; };
void setRotation(v2f rot) noexcept { m_custom_rot = rot; };
void enableContinuousRotation(bool enable) noexcept { m_inf_rot = enable; };
Expand Down

0 comments on commit d0a38f6

Please sign in to comment.