add trackbar 'sub-consumer' for value label refresh when 'change-on-release'

This commit is contained in:
MihailRis 2024-07-28 22:15:18 +03:00
parent 0e3bd4dcac
commit f46e4a7989
9 changed files with 65 additions and 20 deletions

View File

@ -1,7 +1,8 @@
function create_setting(id, name, step, postfix, tooltip) function create_setting(id, name, step, postfix, tooltip, changeonrelease)
local info = core.get_setting_info(id) local info = core.get_setting_info(id)
postfix = postfix or "" postfix = postfix or ""
tooltip = tooltip or "" tooltip = tooltip or ""
changeonrelease = changeonrelease or ""
document.root:add(gui.template("track_setting", { document.root:add(gui.template("track_setting", {
id=id, id=id,
name=gui.str(name, "settings"), name=gui.str(name, "settings"),
@ -10,13 +11,13 @@ function create_setting(id, name, step, postfix, tooltip)
max=info.max, max=info.max,
step=step, step=step,
postfix=postfix, postfix=postfix,
tooltip=tooltip tooltip=tooltip,
changeonrelease=changeonrelease
})) }))
update_setting(core.get_setting(id), id, name, postfix) update_setting(core.get_setting(id), id, name, postfix)
end end
function update_setting(x, id, name, postfix) function update_setting(x, id, name, postfix)
core.set_setting(id, x)
-- updating label -- updating label
document[id..".L"].text = string.format( document[id..".L"].text = string.format(
"%s: %s%s", "%s: %s%s",

View File

@ -1,7 +1,8 @@
function create_setting(id, name, step, postfix, tooltip) function create_setting(id, name, step, postfix, tooltip, changeonrelease)
local info = core.get_setting_info(id) local info = core.get_setting_info(id)
postfix = postfix or "" postfix = postfix or ""
tooltip = tooltip or "" tooltip = tooltip or ""
changeonrelease = changeonrelease or ""
document.root:add(gui.template("track_setting", { document.root:add(gui.template("track_setting", {
id=id, id=id,
name=gui.str(name, "settings"), name=gui.str(name, "settings"),
@ -10,13 +11,13 @@ function create_setting(id, name, step, postfix, tooltip)
max=info.max, max=info.max,
step=step, step=step,
postfix=postfix, postfix=postfix,
tooltip=tooltip tooltip=tooltip,
changeonrelease=changeonrelease
})) }))
update_setting(core.get_setting(id), id, name, postfix) update_setting(core.get_setting(id), id, name, postfix)
end end
function update_setting(x, id, name, postfix) function update_setting(x, id, name, postfix)
core.set_setting(id, x)
-- updating label -- updating label
document[id..".L"].text = string.format( document[id..".L"].text = string.format(
"%s: %s%s", "%s: %s%s",

View File

@ -2,5 +2,6 @@
<label id='%{id}.L' margin='0,3,0,0'>%{name}: %{value}%{postfix}</label> <label id='%{id}.L' margin='0,3,0,0'>%{name}: %{value}%{postfix}</label>
<trackbar <trackbar
value='%{value}' min='%{min}' max='%{max}' step='%{step}' tooltip='%{tooltip}' value='%{value}' min='%{min}' max='%{max}' step='%{step}' tooltip='%{tooltip}'
consumer='function(x) update_setting(x, "%{id}", "%{name}", "%{postfix}") end'/> consumer='function(x) core.set_setting("%{id}", x) end' change-on-release='%{changeonrelease}'
sub-consumer='function(x) update_setting(x, "%{id}", "%{name}", "%{postfix}") end'/>
</panel> </panel>

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <variant> #include <variant>
#include <functional>
namespace dynamic { namespace dynamic {
class Map; class Map;
@ -27,6 +28,8 @@ namespace dynamic {
bool, bool,
integer_t integer_t
>; >;
using to_string_func = std::function<std::string(const Value&)>;
} }
#endif // DATA_DYNAMIC_FWD_HPP_ #endif // DATA_DYNAMIC_FWD_HPP_

View File

@ -50,6 +50,10 @@ void TrackBar::setConsumer(doubleconsumer consumer) {
this->consumer = std::move(consumer); this->consumer = std::move(consumer);
} }
void TrackBar::setSubConsumer(doubleconsumer consumer) {
this->subconsumer = std::move(consumer);
}
void TrackBar::mouseMove(GUI*, int x, int) { void TrackBar::mouseMove(GUI*, int x, int) {
glm::vec2 pos = calcPos(); glm::vec2 pos = calcPos();
value = x - trackWidth/2; value = x - trackWidth/2;
@ -63,6 +67,9 @@ void TrackBar::mouseMove(GUI*, int x, int) {
if (consumer && !changeOnRelease) { if (consumer && !changeOnRelease) {
consumer(value); consumer(value);
} }
if (subconsumer) {
subconsumer(value);
}
} }
void TrackBar::mouseRelease(GUI*, int, int) { void TrackBar::mouseRelease(GUI*, int, int) {

View File

@ -2,6 +2,7 @@
#define GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_ #define GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_
#include "UINode.hpp" #include "UINode.hpp"
#include "../../../data/dynamic_fwd.hpp"
namespace gui { namespace gui {
class TrackBar : public UINode { class TrackBar : public UINode {
@ -9,6 +10,7 @@ namespace gui {
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f}; glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier = nullptr; doublesupplier supplier = nullptr;
doubleconsumer consumer = nullptr; doubleconsumer consumer = nullptr;
doubleconsumer subconsumer = nullptr;
double min; double min;
double max; double max;
double value; double value;
@ -23,8 +25,9 @@ namespace gui {
int trackWidth=12); int trackWidth=12);
virtual void draw(const DrawContext* pctx, Assets* assets) override; virtual void draw(const DrawContext* pctx, Assets* assets) override;
virtual void setSupplier(doublesupplier supplier); virtual void setSupplier(doublesupplier);
virtual void setConsumer(doubleconsumer consumer); virtual void setConsumer(doubleconsumer);
virtual void setSubConsumer(doubleconsumer);
virtual void mouseMove(GUI*, int x, int y) override; virtual void mouseMove(GUI*, int x, int y) override;
virtual void mouseRelease(GUI*, int x, int y) override; virtual void mouseRelease(GUI*, int x, int y) override;

View File

@ -392,26 +392,26 @@ static std::shared_ptr<UINode> readImage(UiXmlReader& reader, const xml::xmlelem
} }
static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, const xml::xmlelement& element) { static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, const xml::xmlelement& element) {
float min = element->attr("min", "0.0").asFloat(); const auto& env = reader.getEnvironment();
float max = element->attr("max", "1.0").asFloat(); const auto& file = reader.getFilename();
float minv = element->attr("min", "0.0").asFloat();
float maxv = element->attr("max", "1.0").asFloat();
float def = element->attr("value", "0.0").asFloat(); float def = element->attr("value", "0.0").asFloat();
float step = element->attr("step", "1.0").asFloat(); float step = element->attr("step", "1.0").asFloat();
int trackWidth = element->attr("track-width", "12").asInt(); int trackWidth = element->attr("track-width", "12").asInt();
auto bar = std::make_shared<TrackBar>(min, max, def, step, trackWidth); auto bar = std::make_shared<TrackBar>(minv, maxv, def, step, trackWidth);
_readUINode(reader, element, *bar); _readUINode(reader, element, *bar);
if (element->has("consumer")) { if (element->has("consumer")) {
bar->setConsumer(scripting::create_number_consumer( bar->setConsumer(scripting::create_number_consumer(
reader.getEnvironment(), env, element->attr("consumer").getText(), file));
element->attr("consumer").getText(), }
reader.getFilename() if (element->has("sub-consumer")) {
)); bar->setSubConsumer(scripting::create_number_consumer(
env, element->attr("sub-consumer").getText(), file));
} }
if (element->has("supplier")) { if (element->has("supplier")) {
bar->setSupplier(scripting::create_number_supplier( bar->setSupplier(scripting::create_number_supplier(
reader.getEnvironment(), env, element->attr("supplier").getText(), file));
element->attr("supplier").getText(),
reader.getFilename()
));
} }
if (element->has("track-color")) { if (element->has("track-color")) {
bar->setTrackColor(element->attr("track-color").asColor()); bar->setTrackColor(element->attr("track-color").asColor());

View File

@ -2,6 +2,7 @@
#include "lua/lua_engine.hpp" #include "lua/lua_engine.hpp"
#include "../../debug/Logger.hpp" #include "../../debug/Logger.hpp"
#include "../../coders/json.hpp"
#include "../../util/stringutil.hpp" #include "../../util/stringutil.hpp"
using namespace scripting; using namespace scripting;
@ -177,3 +178,25 @@ vec2supplier scripting::create_vec2_supplier(
return glm::vec2(0, 0); return glm::vec2(0, 0);
}; };
} }
dynamic::to_string_func scripting::create_tostring(
const scriptenv& env,
const std::string& src,
const std::string& file
) {
auto L = lua::get_main_thread();
try {
lua::loadbuffer(L, *env, src, file);
lua::call(L, 0, 1);
auto func = lua::create_lambda(L);
return [func](const dynamic::Value& value) {
auto result = func({value});
return json::stringify(result, true, " ");
};
} catch (const lua::luaerror& err) {
logger.error() << err.what();
return [](const auto& value) {
return json::stringify(value, true, " ");
};
}
}

View File

@ -72,6 +72,12 @@ namespace scripting {
const std::string& src, const std::string& src,
const std::string& file="<string>" const std::string& file="<string>"
); );
dynamic::to_string_func create_tostring(
const scriptenv& env,
const std::string& src,
const std::string& file="<string>"
);
} }
#endif // LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_HPP_ #endif // LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_HPP_