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

View File

@ -2,5 +2,6 @@
<label id='%{id}.L' margin='0,3,0,0'>%{name}: %{value}%{postfix}</label>
<trackbar
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>

View File

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

View File

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

View File

@ -2,6 +2,7 @@
#define GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_
#include "UINode.hpp"
#include "../../../data/dynamic_fwd.hpp"
namespace gui {
class TrackBar : public UINode {
@ -9,6 +10,7 @@ namespace gui {
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier = nullptr;
doubleconsumer consumer = nullptr;
doubleconsumer subconsumer = nullptr;
double min;
double max;
double value;
@ -23,8 +25,9 @@ namespace gui {
int trackWidth=12);
virtual void draw(const DrawContext* pctx, Assets* assets) override;
virtual void setSupplier(doublesupplier supplier);
virtual void setConsumer(doubleconsumer consumer);
virtual void setSupplier(doublesupplier);
virtual void setConsumer(doubleconsumer);
virtual void setSubConsumer(doubleconsumer);
virtual void mouseMove(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) {
float min = element->attr("min", "0.0").asFloat();
float max = element->attr("max", "1.0").asFloat();
const auto& env = reader.getEnvironment();
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 step = element->attr("step", "1.0").asFloat();
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);
if (element->has("consumer")) {
bar->setConsumer(scripting::create_number_consumer(
reader.getEnvironment(),
element->attr("consumer").getText(),
reader.getFilename()
));
env, element->attr("consumer").getText(), file));
}
if (element->has("sub-consumer")) {
bar->setSubConsumer(scripting::create_number_consumer(
env, element->attr("sub-consumer").getText(), file));
}
if (element->has("supplier")) {
bar->setSupplier(scripting::create_number_supplier(
reader.getEnvironment(),
element->attr("supplier").getText(),
reader.getFilename()
));
env, element->attr("supplier").getText(), file));
}
if (element->has("track-color")) {
bar->setTrackColor(element->attr("track-color").asColor());

View File

@ -2,6 +2,7 @@
#include "lua/lua_engine.hpp"
#include "../../debug/Logger.hpp"
#include "../../coders/json.hpp"
#include "../../util/stringutil.hpp"
using namespace scripting;
@ -177,3 +178,25 @@ vec2supplier scripting::create_vec2_supplier(
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& 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_