param_matrix

This commit is contained in:
Zeno Rogue 2023-08-08 17:20:39 +02:00
parent c914bf86e0
commit a13ba9bdbe
5 changed files with 139 additions and 22 deletions

View File

@ -204,6 +204,28 @@ struct color_setting : public setting {
}
};
struct matrix_setting : public setting {
trans23 *value;
trans23 dft;
trans23 last_value_matrix;
supersaver *make_saver() override;
bool affects(void *v) override { return v == value; }
void show_edit_option(int key) override;
cld get_cld() override { return 0; }
void set_cld(cld x) override { }
matrix_setting *editable(string menu_item_name, string help_text, char key) {
this->is_editable = true;
this->menu_item_name = menu_item_name;
this->help_text = help_text;
default_key = key;
return this;
}
void load_from(const string& s) override {
*value = parsematrix23(s);
}
};
struct char_setting : public setting {
char *value;
char dft;
@ -362,6 +384,34 @@ template<> struct saver<string> : dsaver<string> {
virtual void swap_with(supersaver *s) { swap(val, ((saver<string>*)s)->val); }
};
template<> struct saver<trans23> : supersaver {
trans23& val;
trans23 dft;
explicit saver(trans23& _val) : val(_val) { }
void reset() override { val = dft; }
bool affects(void* v) override { return v == &val; }
void set_default() override { dft = val; }
bool dosave() override { return !eqmatrix(val.v2, dft.v2) || !eqmatrix(val.v3, dft.v3); }
string save() override {
shstream ss;
for(int a=0; a<4; a++) for(int b=0; b<4; b++) print(ss, val.v2[a][b], " ");
for(int a=0; a<4; a++) for(int b=0; b<4; b++) print(ss, val.v3[a][b], " ");
return ss.s;
}
void load(const string& s) override {
shstream ss;
ss.s = s;
for(int a=0; a<4; a++) for(int b=0; b<4; b++) scan(ss, val.v2[a][b]);
for(int a=0; a<4; a++) for(int b=0; b<4; b++) scan(ss, val.v3[a][b]);
}
virtual void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(trans23*) value, lps.label + name); }
virtual void swap_with(supersaver *s) override { swap(val, ((saver<trans23>*)s)->val); }
};
template<> struct saver<ld> : dsaver<ld> {
explicit saver(ld& val) : dsaver<ld>(val) { }
string save() override { return fts(val, 10); }
@ -399,6 +449,14 @@ supersaver* color_setting::make_saver() {
#endif
}
supersaver* matrix_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver *char_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
@ -488,6 +546,13 @@ void color_setting::show_edit_option(int key) {
});
}
void matrix_setting::show_edit_option(int key) {
dialog::addMatrixItem(XLAT(menu_item_name), *value, key);
dialog::add_action([this] () {
dialog::editMatrix(*value, XLAT(menu_item_name), help_text);
});
}
void char_setting::show_edit_option(int key) {
string s = s0; s += value;
dialog::addSelItem(XLAT(menu_item_name), s, key);
@ -602,6 +667,20 @@ EX color_setting *param_color(color_t& val, const string s, bool has_alpha, colo
return f;
}
EX matrix_setting *param_matrix(trans23& val, const string s) {
unique_ptr<matrix_setting> u ( new matrix_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value_matrix = val;
u->dft = val;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX char_setting *param_char(char& val, const string s, char dft) {
unique_ptr<char_setting> u ( new char_setting );
u->parameter_name = param_esc(s);

View File

@ -17,7 +17,7 @@ EX namespace dialog {
static const int DONT_SHOW = 16;
enum tDialogItem {diTitle, diItem, diBreak, diHelp, diInfo, diIntSlider, diSlider, diBigItem, diKeyboard, diCustom, diColorItem, diListStart, diListEnd};
enum tDialogItem {diTitle, diItem, diBreak, diHelp, diInfo, diIntSlider, diSlider, diBigItem, diKeyboard, diCustom, diColorItem, diListStart, diListEnd, diMatrixItem};
struct item {
tDialogItem type;
@ -29,6 +29,7 @@ EX namespace dialog {
double param;
int p1, p2, p3;
int position;
void *ptr;
reaction_t customfun;
item(tDialogItem t = diBreak);
};
@ -268,6 +269,20 @@ EX namespace dialog {
it.param = value & 0xFF;
}
ld as_degrees(transmatrix T) {
hyperpoint h = T * point31(1, 0, 0);
ld alpha = atan2(h[1], h[0]);
return alpha / degree;
}
EX void addMatrixItem(string body, trans23& value, int key) {
addSelItem(body, COLORBAR, key);
auto& it = items.back();
it.type = diMatrixItem;
it.ptr = &value;
it.value = fts(as_degrees(value.get())) + "°";
}
EX void addHelp(string body) {
item it(diHelp);
it.body = body;
@ -708,7 +723,7 @@ EX namespace dialog {
displayfr(dcenter, mid, 2, dfsize * I.scale/100, I.body, I.color, 8);
if(xthis) getcstat = I.key;
}
else if(among(I.type, diItem, diBigItem, diColorItem)) {
else if(among(I.type, diItem, diBigItem, diColorItem, diMatrixItem)) {
bool xthis = (mousey >= top && mousey < tothei);
if(cmode & sm::DIALOG_STRICT_X)
xthis = xthis && (mousex >= dcenter - dialogwidth/2 && mousex <= dcenter + dialogwidth/2);
@ -1347,6 +1362,28 @@ EX namespace dialog {
#endif
}
EX void editMatrix(trans23& x, string title, string help) {
static ld angle = as_degrees(x.get());
ne.editwhat = &angle;
ne.s = disp(angle);
ne.vmin = -180;
ne.vmax = 180;
ne.step = 90;
ne.dft = 0;
ne.title = title;
ne.help = help;
ne.sc = identity;
ne.intval = NULL;
dialogflags = 0;
if(cmode & sm::SIDE) dialogflags |= sm::MAYDARK | sm::SIDE;
cmode |= sm::NUMBER;
pushScreen(drawNumberDialog);
reaction = [&] { x = spin(angle * degree); };
reaction_final = reaction;
extra_options = reaction_t();
ne.animatable = true;
}
EX void editNumber(int& x, int vmin, int vmax, ld step, int dft, string title, string help) {
editNumber(ne.intbuf, vmin, vmax, step, dft, title, help);
ne.intbuf = x; ne.intval = &x; ne.s = its(x);

View File

@ -442,6 +442,13 @@ EX shiftpoint kz(shiftpoint h) {
return h;
}
EX bool scan(shstream& hs, ld& val) {
int npos;
int qty = sscanf(hs.s.c_str() + hs.pos, "%lf%n", &val, &npos);
if(qty == 1) { hs.pos += npos; return true; }
return false;
}
#if HDR
template<class T> vector<T> kz(vector<T> v) {
for(auto& el: v) el = kz(el);

View File

@ -467,24 +467,18 @@ EX namespace models {
add_edit(vpconf.alpha);
}
/* TODO if(has_orientation(vpmodel)) {
dialog::addSelItem(XLAT("model orientation"), fts(vpconf.model_orientation) + "°", 'l');
if(has_orientation(vpmodel)) {
dialog::addMatrixItem(XLAT("model orientation"), vpconf.mori(), 'l');
dialog::add_action([] () {
dialog::editNumber(vpconf.model_orientation, 0, 360, 90, 0, XLAT("model orientation"), "");
dialog::editMatrix(vpconf.mori(), XLAT("model orientation"), "");
});
if(GDIM == 3) {
dialog::addSelItem(XLAT("model orientation (y/z plane)"), fts(vpconf.model_orientation_yz) + "°", 'L');
dialog::add_action([] () {
dialog::editNumber(vpconf.model_orientation_yz, 0, 360, 90, 0, XLAT("model orientation (y/z plane)"), "");
});
}
} */
}
if(among(vpmodel, mdPerspective, mdHorocyclic) && nil) {
/* TODO dialog::addSelItem(XLAT("model orientation"), fts(vpconf.model_orientation) + "°", 'l');
dialog::addMatrixItem(XLAT("model orientation"), vpconf.mori(), 'l');
dialog::add_action([] () {
dialog::editNumber(vpconf.model_orientation, 0, 360, 90, 0, XLAT("model orientation"), "");
}); */
dialog::editMatrix(vpconf.mori(), XLAT("model orientation"), "");
});
dialog::addSelItem(XLAT("rotational or Heisenberg"), fts(vpconf.rotational_nil), 'L');
dialog::add_action([] () {
dialog::editNumber(vpconf.rotational_nil, 0, 1, 1, 1, XLAT("1 = Heisenberg, 0 = rotational"), "");
@ -975,8 +969,7 @@ EX namespace models {
addsaverenum(p.model, pp+"used model", mdDisk);
if(&p.model == &pmodel) param_custom(pmodel, "projection|Poincare|Klein|half-plane|perspective", menuitem_projection, '1');
// TODO param_f(p.model_orientation, pp+"mori", sp+"model orientation", 0);
// TODO param_f(p.model_orientation_yz, pp+"mori_yz", sp+"model orientation-yz", 0);
param_matrix(p.mori(), pp+"mori");
param_f(p.top_z, sp+"topz", 5)
-> editable(1, 20, .25, "maximum z coordinate to show", "maximum z coordinate to show", 'l');

View File

@ -846,7 +846,7 @@ auto hook =
})
+ addHook(hooks_configfile, 100, [] {
param_f(racing::race_advance, "race_advance");
// TODO param_f(racing::race_angle, "race_angle");
param_matrix(racing::race_angle, "race_angle");
param_i(racing::ghosts_to_show, "race_ghosts_to_show");
param_i(racing::ghosts_to_save, "race_ghosts_to_save");
param_b(racing::guiding, "race_guiding");
@ -1035,11 +1035,12 @@ void race_projection() {
else dialog::addBreak(100);
if(GDIM == 2) {
// TODO dialog::addSelItem(XLAT("race angle"), fts(race_angle), 'a');
dialog::add_action([] () {
// TODO dialog::editNumber(race_angle, 0, 360, 15, 90, XLAT("race angle"), "");
dialog::addMatrixItem(XLAT("race angle"), race_angle, 'a');
dialog::add_action([] () {
dialog::editMatrix(race_angle, XLAT("model orientation"), "");
auto q = rot_inverse(race_angle) * pconf.mori();
dialog::reaction = [q] () { pconf.mori() = race_angle * q; };
auto last = dialog::reaction;
dialog::reaction = [q, last] () { last(); pconf.mori() = race_angle * q; };
});
}