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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
#pragma once
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <imgui.h>
#include "uicontext.h"
#include "stx.h"
#include "utils.h"
#include "cpp_parser.h"
#include "binding_type.h"


struct property_base
{
    virtual std::string to_arg(std::string_view a = "", std::string_view b = "") const = 0;
    virtual bool set_from_arg(std::string_view s) = 0;
    virtual const char* c_str() const = 0;
    virtual std::vector<std::string> used_variables() const = 0;
    virtual void rename_variable(const std::string& oldn, const std::string& newn) = 0;
};

//member variable expression like id, id.member, id[0], id.size()
template <class T = void>
struct field_ref : property_base
{
    using type = T;

    bool empty() const {
        return str.empty();
    }
    const std::string& value() const {
        return str;
    }
    bool has_single_variable() const {
        if (empty())
            return false;
        return cpp::is_id(str);
    }

    T eval(const UIContext& ctx) const;

    bool set_from_arg(std::string_view s) {
        str = s;
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        return str;
    }
    std::vector<std::string> used_variables() const {
        size_t i = 0;
        auto id = cpp::find_id(str, i);
        if (id == "")
            return {};
        return { std::string(id.data(), id.size()) };
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        size_t i = 0;
        auto id = cpp::find_id(str, i);
        if (id == oldn)
            str.replace(id.data() - str.data(), id.size(), newn);
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
private:
    std::string str;
};

//member function name
template <class FuncSig = void()>
struct event : property_base
{
    bool empty() const {
        return str.empty();
    }
    bool set_from_arg(std::string_view s) {
        str = s;
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        return str;
    }
    std::vector<std::string> used_variables() const {
        if (empty())
            return {};
        return { str };
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        if (str == oldn)
            str = newn;
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
private:
    std::string str;
};

//value only

template <class T, bool E = std::is_enum_v<T>>
struct direct_val;

template <class T>
struct direct_val<T, false> : property_base
{
    direct_val(T v) : val(v) {}

    operator T&() { return val; }
    operator const T&() const { return val; }
    /*bool operator== (const direct_val& dv) const {
        return val == dv.val;
    }
    bool operator!= (const direct_val& dv) const {
        return val != dv.val;
    }*/
    direct_val& operator= (const T& v) {
        val = v;
        return *this;
    }
    bool set_from_arg(std::string_view str) {
        std::istringstream is;
        is.str(std::string(str));
        if constexpr (std::is_enum_v<T>) {
            int tmp;
            is >> tmp;
            val = (T)tmp;
        }
        else {
            is >> std::boolalpha >> val;
        }
        return is.good();
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        std::ostringstream os;
        os << std::boolalpha << val;
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    T* access() { return &val; }
    const char* c_str() const { return nullptr; }

private:
    T val;
};

#define add$(f) add(#f, f)
template <class T>
struct direct_val<T, true> : property_base
{
    direct_val(int v = 0) : val(v) {}

    void clear() {
        ids.clear();
    }
    direct_val& add(const char* id, int v) {
        ids.push_back({ id, v });
        return *this;
    }
    direct_val& separator() {
        ids.push_back({ "", 0 });
        return *this;
    }
    auto find_id(int fl) const {
        return stx::find_if(ids, [this,fl](const auto& id) {
            return id.first != "" && id.second == fl;
            });
    }
    const auto& get_ids() const { return ids; }
    const std::string& get_id() const {
        static std::string none;
        auto it = find_id(val);
        return it != ids.end() ? it->first : none;
    }
    void set_id(const std::string& v) {
        auto it = stx::find_if(ids, [&v](const auto& id) { return id.first == v; });
        if (it != ids.end())
            val = it->second;
    }

    operator T() const { return (T)val; }

    direct_val& operator= (int v) {
        val = v;
        return *this;
    }
    direct_val& operator|= (int v) {
        val |= v;
        return *this;
    }
    direct_val& operator&= (int v) {
        val &= v;
        return *this;
    }
    bool set_from_arg(std::string_view str) {
        bool ok = true;
        val = {};
        size_t i = 0;
        while (true) {
            std::string s;
            size_t j = str.find('|', i);
            if (j == std::string::npos)
                s = str.substr(i);
            else
                s = str.substr(i, j - i);
            auto id = stx::find_if(ids, [&](const auto& id) { return id.first == s; });
            //assert(id != ids.end());
            if (s == "0" ||
                (!s.compare(0, 5, "ImGui") && !s.compare(s.size() - 5, 5, "_None")))
                val;
            else if (id != ids.end())
                val |= id->second;
            else
                ok = false;
            if (j == std::string::npos)
                break;
            i = j + 1;
        }
        return ok;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        std::string str;
        bool simpleEnum = false;
        for (const auto& id : ids) {
            if (id.first != "" && !id.second)
                simpleEnum = true;
        }
        if (simpleEnum) {
            auto it = stx::find_if(ids, [this](const auto& id) { return id.second == val; });
            str = it == ids.end() ? "0" : it->first;
        }
        else {
            for (const auto& id : ids)
                if ((val & id.second) == id.second && id.first != "")
                    str += id.first + " | ";
            if (str != "")
                str.resize(str.size() - 3);
            else
                str = "0";
        }
        return str;
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    int* access() { return &val; }
    const char* c_str() const { return nullptr; }

private:
    int val;
    std::vector<std::pair<std::string, int>> ids;
};

template <>
struct direct_val<dimension_t> : property_base
{
    direct_val(float v) : val(v) {}

    operator float&() { return val; }
    operator const float() const { return val; }
    bool operator== (float dv) const {
        return val == dv;
    }
    bool operator!= (float dv) const {
        return val != dv;
    }
    direct_val& operator= (float v) {
        val = v;
        return *this;
    }
    float eval_px(const UIContext& ctx) const;

    bool set_from_arg(std::string_view s) {
        std::istringstream is;
        is.str(std::string(s));
        is >> val;
        //strip unit calculation
        std::string_view factor = s.size() > 3 ? s.substr(s.size() - 3) : "";
        if (factor == "*dp")
        {
            std::istringstream is(std::string(s.substr(0, s.size() - 3)));
            float v;
            if ((is >> v) && is.eof())
                val = v;
        }
        return true;
    }
    std::string to_arg(std::string_view unit, std::string_view = "") const {
        std::ostringstream os;
        os << val;
        if (unit != "")
            os << "*" << unit;
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    float* access() { return &val; }
    const char* c_str() const { return nullptr; }

private:
    float val;
};

template <>
struct direct_val<pzdimension_t> : property_base
{
    direct_val(float v = -1) : val(v) {}

    operator float&() { return val; }
    operator const float() const { return val; }
    bool empty() const { return val == -1; }
    void clear() { val = -1; }
    bool has_value() const { return !empty(); }
    bool operator== (float dv) const {
        return val == dv;
    }
    bool operator!= (float dv) const {
        return val != dv;
    }
    direct_val& operator= (float v) {
        val = v;
        return *this;
    }
    float eval_px(const UIContext& ctx) const;

    bool set_from_arg(std::string_view s) {
        std::istringstream is;
        is.str(std::string(s));
        is >> val;
        //strip unit calculation
        std::string_view factor = s.size() > 3 ? s.substr(s.size() - 3) : "";
        if (factor == "*dp")
        {
            std::istringstream is(std::string(s.substr(0, s.size() - 3)));
            float v;
            if ((is >> v) && is.eof())
                val = v;
        }
        return true;
    }
    std::string to_arg(std::string_view unit, std::string_view = "") const {
        std::ostringstream os;
        os << val;
        if (unit != "")
            os << "*" << unit;
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    float* access() { return &val; }
    const char* c_str() const { return nullptr; }

private:
    float val;
};

template <>
struct direct_val<pzdimension2_t> : property_base
{
    direct_val(ImVec2 dim = { -1, -1 }) : val(dim) {}

    operator ImVec2&() { return val; }
    operator const ImVec2() const { return val; }
    float& operator[] (int i) { return val[i]; }
    float operator[] (int i) const { return val[i]; }

    bool operator== (ImVec2 dv) const {
        return val[0] == dv[0] && val[1] == dv[1];
    }
    bool operator!= (ImVec2 dv) const {
        return val[0] != dv[0] && val[1] != dv[1];
    }
    direct_val& operator= (ImVec2 v) {
        val = v;
        return *this;
    }
    void clear() { val[0] = val[1] = -1; }
    bool empty() const { return val[0] == -1 && val[1] == -1; }
    bool has_value() const { return !empty(); }
    ImVec2 eval_px(const UIContext& ctx) const;

    bool set_from_arg(std::string_view s) {
        val = cpp::parse_fsize(std::string(s));
        return true;
    }
    std::string to_arg(std::string_view unit, std::string_view = "") const {
        bool hasv = has_value();
        std::ostringstream os;
        os << "{ " << val[0];
        if (unit != "" && hasv && val[0])
            os << "*" << unit;
        os << ", " << val[1];
        if (unit != "" && hasv && val[1])
            os << "*" << unit;
        os << " }";
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    ImVec2* access() { return &val; }
    const char* c_str() const { return nullptr; }

private:
    ImVec2 val;
};

template <>
struct direct_val<std::string> : property_base
{
    direct_val(const std::string& v) : val(v) {}
    direct_val(const char* v) : val(v) {}

    operator std::string&() { return val; }
    operator const std::string&() const { return val; }
    //operator std::string_view() const { return val; }
    const std::string& value() const { return val; }

    bool set_from_arg(std::string_view s) {
        val = cpp::parse_str_arg(s);
        return val != cpp::INVALID_TEXT;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        return "\"" + cpp::escape(val) + "\"";
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    std::string* access() { return &val; }
    const char* c_str() const { return val.c_str(); }
    bool empty() const { return val.empty(); }
    bool operator== (const std::string& dv) const {
        return val == dv;
    }
    bool operator!= (const std::string& dv) const {
        return val != dv;
    }
private:
    std::string val;
};

template <>
struct direct_val<shortcut_t> : property_base
{
    direct_val(const std::string& v) : sh(v) {}
    direct_val(const char* v) : sh(v) {}

    int flags() const { return flags_; }
    void set_flags(int f) { flags_ = f; }
    //const std::string& value() const { return val; }

    bool set_from_arg(std::string_view s) {
        sh = ParseShortcut(s);
        flags_ = 0;
        if (s.find("ImGuiInputFlags_RouteGlobal") != std::string::npos)
            flags_ |= ImGuiInputFlags_RouteGlobal;
        if (s.find("ImGuiInputFlags_Repat") != std::string::npos)
            flags_ |= ImGuiInputFlags_Repeat;
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        std::ostringstream os;
        os << CodeShortcut(sh);
        std::vector<std::string> fl;
        if (flags_ & ImGuiInputFlags_RouteGlobal)
            fl.push_back("ImGuiInputFlags_RouteGlobal");
        if (flags_ & ImGuiInputFlags_Repeat)
            fl.push_back("ImGuiInputFlags_Repeat");
        if (fl.size())
            os << ", " << stx::join(fl, " | ");
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        return {};
    }
    void rename_variable(const std::string& oldn, const std::string& newn)
    {}
    std::string* access() { return &sh; }
    const char* c_str() const { return sh.c_str(); }
    bool empty() const { return sh.empty(); }
private:
    std::string sh;
    int flags_;
};

//value or binding expression
//2*w
template <class T = void>
struct bindable : property_base
{
    bindable() {
    }
    template <class U = std::conditional_t<std::is_same_v<T, void>, std::nullptr_t, T>>
    bindable(U val) {
        std::ostringstream os;
        os << std::boolalpha << val;
        str = os.str();
    }
    bool operator== (const bindable& b) const {
        return str == b.str;
    }
    bool operator!= (const bindable& b) const {
        return str != b.str;
    }
    bool empty() const { return str.empty(); }
    bool has_value() const {
        return cpp::is_literal(str);
    }
    T value() const {
        if (!has_value())
            return {};
        std::istringstream is(str);
        T val{};
        if (is >> std::boolalpha >> val)
            return val;
        if constexpr (std::is_convertible_v<bool, T>) {
            if (str == "true")
                return true;
            if (str == "false")
                return false;
        }
        return {};
    }
    T eval(const UIContext& ctx) const;

    bool has_single_variable() const {
        return cpp::is_id(str);
    }
    bool is_reference() const {
        return cpp::is_lvalue(str);
    }

    bool set_from_arg(std::string_view s) {
        str = s;
        //try to remove trailing f
        if (std::is_same_v<T, float> && str.size() >= 3 && !str.compare(str.size() - 2, 2, ".f")) {
            str.resize(str.size() - 2);
            if (!has_value())
                str += ".f";
        }
        else if (std::is_same_v<T, float> && str.size() && str.back() == 'f') {
            str.pop_back();
            if (!has_value())
                str.push_back('f');
        }
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        if (std::is_same_v<T, float> && has_value()) {
            std::string tmp = str;
            if (str.find('.') == std::string::npos)
                tmp += ".f";
            else
                tmp += "f";
            return tmp;
        }
        return str;
    }
    std::vector<std::string> used_variables() const {
        if (empty() || has_value())
            return {};
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            vars.push_back(std::string(id));
        }
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        if (empty() || has_value())
            return;
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            if (id == oldn)
                str.replace(id.data() - str.data(), id.size(), newn);
        }
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
private:
    std::string str;
};

template <>
struct bindable<dimension_t> : property_base
{
    bindable() {
    }
    bindable(float val)
    {
        std::ostringstream os;
        os << val;
        str = os.str();
    }
    bool operator== (const bindable& b) const {
        return str == b.str && grow == b.grow;
    }
    bool operator!= (const bindable& b) const {
        return str != b.str || grow != b.grow;
    }
    bool empty() const {
        return str.empty();
    }
    bool zero() const {
        if (empty())
            return false;
        std::istringstream is(str);
        float val;
        if (!(is >> val) || val)
            return false;
        return is.eof() || is.tellg() == str.size();
    }
    bool stretched() const {
        return grow;
    }
    bool has_value() const {
        return cpp::is_literal(str);
    }
    float value() const {
        if (!has_value())
            return {};
        std::istringstream is(str);
        float val{};
        is >> val;
        return val;
    }
    float eval_px(int axis, const UIContext& ctx) const;

    bool set_from_arg(std::string_view s) {
        str = s;
        stretch(false);
        //strip unit calculation
        std::string_view factor = s.size() > 3 ? s.substr(s.size() - 3) : "";
        if (factor == "*fs" || factor == "*dp")
        {
            std::istringstream is(std::string(s.substr(0, s.size() - 3)));
            float val;
            if ((is >> val) && is.eof())
                str = s.substr(0, s.size() - 3);
        }
        else if (s.size() && s.back() == 'x')
        {
            std::istringstream is(std::string(s.substr(0, s.size() - 1)));
            float val;
            if ((is >> val) && is.eof())
                str.pop_back();
            stretch(true);
        }
        return true;
    }
    std::string to_arg(std::string_view unit, std::string_view stretchCode = "") const {
        if (stretched())
        {
            if (stretchCode != "")
                return std::string(stretchCode);
            return str + "x";
        }
        if (unit != "" && has_value() &&
            str != "0" && str != "-1") //don't suffix special values
        {
            return str + "*" + unit;
        }
        return str;
    }
    std::vector<std::string> used_variables() const {
        if (empty() || has_value())
            return {};
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            vars.push_back(std::string(id));
        }
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        if (empty() || has_value())
            return;
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            if (id == oldn)
                str.replace(id.data() - str.data(), id.size(), newn);
        }
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }

    void stretch(bool s) {
        grow = s;
        std::ostringstream os;
        if (grow) {
            auto val = value();
            os << std::fixed << std::setprecision(1) << val;
            str = os.str();
        }
        else if (has_value()) {
            auto val = value();
            os << std::defaultfloat << val;
            str = os.str();
        }
    }

private:
    std::string str;
    bool grow = false;
};

//Hi {names[i]} you are {ages[i].exact():2} years old
template <>
struct bindable<std::string> : property_base
{
    bindable() {}
    bindable(const char* val) {
        str = val;
    }
    bindable(const std::string& val) {
        str = val;
    }
    bool operator== (const bindable& b) const {
        return str == b.str;
    }
    bool operator!= (const bindable& b) const {
        return str != b.str;
    }
    bool empty() const { return str.empty(); }
    const std::string& value() const { return str; }
    bool set_from_arg(std::string_view s)
    {
        str = cpp::parse_str_arg(s);
        return str != cpp::INVALID_TEXT;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const
    {
        return cpp::to_str_arg(str); 
        //todo: prefer single variable. Does it need to be c_str()??
        // , has_single_variable());
    }
    bool has_single_variable() const {
        if (empty() || str[0] != '{' || str.back() != '}')
            return false;
        auto vars = used_variables();
        if (vars.size() != 1 || str.compare(1, str.size() - 2, vars[0]))
            return false;
        return true;
    }
    std::vector<std::string> used_variables() const {
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            size_t j = str.find('{', i);
            if (j == std::string::npos)
                break;
            if (j + 1 < str.size() && str[j + 1] == '{')
                i = j + 2;
            else {
                i = j + 1;
                size_t e;
                for (e = i + 1; e < str.size(); ++e)
                    if (str[e] == ':' || str[e] == '}')
                        break;
                if (e >= str.size())
                    break;
                if (e > i) {
                    auto s = str.substr(i, e - i);
                    size_t k = 0;
                    while (true) {
                        auto id = cpp::find_id(s, k);
                        if (id == "")
                            break;
                        vars.push_back(std::string(id));
                    }
                }
            }
        }
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn) {
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            size_t j = str.find('{', i);
            if (j == std::string::npos)
                break;
            if (j + 1 < str.size() && str[j + 1] == '{')
                i = j + 2;
            else {
                i = j + 1;
                size_t e = std::min(str.find(':', i), str.find('}', i));
                if (e == std::string::npos)
                    break;
                auto s = str.substr(i, e - i);
                size_t k = 0;
                while (true) {
                    auto id = cpp::find_id(s, k);
                    if (id == "")
                        break;
                    if (id == oldn)
                        str.replace(id.data() - s.data() + i, id.size(), newn);
                }
            }
        }
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
protected:
    std::string str;
};

//{items} or prvni\0druha\0
template <>
struct bindable<std::vector<std::string>> : bindable<std::string>
{
    bindable() {}
    bool has_single_variable() const
    {
        return str.size() > 2 && str[0] == '{' &&
            str.find('{', 1) == std::string::npos &&
            str.back() == '}';
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const
    {
        return cpp::to_str_arg(str, true);
    }
};

template <>
struct bindable<font_name_t> : property_base
{
    bindable() {
    }
    bindable(std::string_view fn) {
        set_font_name(fn);
    }
    bool operator== (const bindable& b) const {
        return str == b.str;
    }
    bool operator!= (const bindable& b) const {
        return str != b.str;
    }
    bool empty() const { return str.empty(); }
    bool has_value() const {
        return !str.compare(0, 22, "ImRad::GetFontByName(\"");
    }
    std::string eval(const UIContext& ctx) const;

    void set_font_name(std::string_view fn) {
        str = "ImRad::GetFontByName(\"" + std::string(fn) + "\")";
    }
    bool set_from_arg(std::string_view s) {
        str = s;
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        return str;
    }
    std::vector<std::string> used_variables() const {
        if (empty())
            return {};
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            vars.push_back(std::string(id));
        }
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        if (empty())
            return;
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            if (id == oldn)
                str.replace(id.data() - str.data(), id.size(), newn);
        }
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
private:
    std::string str;
};

template <>
struct bindable<color_t> : property_base
{
    bindable() {
    }
    bindable(ImU32 c) {
        std::ostringstream os;
        if (c)
            os << "0x" << std::hex << std::setw(2 * 4) << std::setfill('0') << c;
        str = os.str();
    }
    bool operator== (const bindable& b) const {
        return str == b.str;
    }
    bool operator!= (const bindable& b) const {
        return str != b.str;
    }
    bool empty() const { return str.empty(); }
    /*bool has_value() const {
        if (empty())
            return false;
        std::istringstream is(str);
        color_t val;
        if (!(is >> val))
            return false;
        if (is.eof())
            return true;
        return is.tellg() == str.size();
    }*/
    ImU32 eval(int col, const UIContext& ctx) const;

    bool has_style_color() const {
        if (!str.compare(0, 34, "ImGui::GetStyle().Colors[ImGuiCol_") && str.back() == ']') //compatibility
            return true;
        if (!str.compare(0, 34, "ImGui::GetStyleColorVec4(ImGuiCol_") && str.back() == ')')
            return true;
        return false;
    }
    int style_color() const {
        if (!has_style_color())
            return -1;
        std::string code = str.substr(34, str.size() - 34 - 1);
        for (int i = 0; i < ImGuiCol_COUNT; ++i)
            if (code == ImGui::GetStyleColorName(i))
                return i;
        return -1;
    }
    void set_style_color(int i) {
        std::ostringstream os;
        os << "ImGui::GetStyleColorVec4(ImGuiCol_" << ImGui::GetStyleColorName(i) << ")";
        str = os.str();
    }
    bool set_from_arg(std::string_view s) {
        str = s;
        return true;
    }
    std::string to_arg(std::string_view = "", std::string_view = "") const {
        return str;
    }
    std::vector<std::string> used_variables() const {
        if (empty())// || has_value())
            return {};
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            vars.push_back(std::string(id));
        }
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        if (empty()) // || has_value())
            return;
        std::vector<std::string> vars;
        size_t i = 0;
        while (true) {
            auto id = cpp::find_id(str, i);
            if (id == "")
                break;
            if (id == oldn)
                str.replace(id.data() - str.data(), id.size(), newn);
        }
    }
    const char* c_str() const { return str.c_str(); }
    std::string* access() { return &str; }
private:
    std::string str;
};

struct data_loop : property_base
{
    bindable<int> limit;
    field_ref<int> index; //int indexes are easier than size_t

    bool empty() const {
        return limit.empty();
    }
    std::string index_name_or(std::string_view s) const {
        return index.empty() ? std::string(s) : index.to_arg();
    }
    std::string container_var() const {
        auto vars = limit.used_variables();
        if (vars.size())
            return vars[0];
        return "";
    }
    std::string container_expr() {
        std::string var = container_var();
        const std::string& str = *limit.access();
        size_t i = str.find(var);
        if ((i && str[i - 1] == '*') || !str.compare(i + var.size(), 2, "->"))
            return "(*" + var + ")";
        return var;
    }

    bool set_from_arg(std::string_view code) {
        if (code.compare(0, 4, "for("))
            return false;
        bool local = !code.compare(4, 3, "int") || !code.compare(4, 6, "size_t");
        auto i = code.find(";");
        if (i == std::string::npos)
            return false;
        code.remove_prefix(i + 1);

        i = code.find(";");
        if (i == std::string::npos)
            return false;
        code.remove_suffix(code.size() - i);

        i = code.find("<");
        if (local)
            *index.access() = "";
        else
            *index.access() = code.substr(0, i);

        limit.set_from_arg(code.substr(i + 1));
        return true;
    }
    std::string to_arg(std::string_view forVarName, std::string_view = "") const {
        if (empty())
            return "";
        std::ostringstream os;
        std::string name = index_name_or(std::string(forVarName));
        os << "for (";
        if (index.empty())
            os << "int ";
        os << name << " = 0; " << name << " < " << limit.to_arg()
            << "; ++" << name << ")";
        return os.str();
    }
    std::vector<std::string> used_variables() const {
        auto vars = index.used_variables();
        auto vars2 = limit.used_variables();
        vars.insert(vars.end(), vars2.begin(), vars2.end());
        return vars;
    };
    void rename_variable(const std::string& oldn, const std::string& newn)
    {
        limit.rename_variable(oldn, newn);
        index.rename_variable(oldn, newn);
    }
    const char* c_str() const { return limit.c_str(); }
    std::string* access() { return limit.access(); }
};