Jamba C++ API 8.0.0
Loading...
Searching...
No Matches
CustomViewCreator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2019 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <vstgui4/vstgui/uidescription/iviewcreator.h>
22#include <vstgui4/vstgui/uidescription/uiviewcreator.h>
23#include <vstgui4/vstgui/uidescription/uiviewfactory.h>
24#include <vstgui4/vstgui/uidescription/uiattributes.h>
25#include <vstgui4/vstgui/uidescription/detail/uiviewcreatorattributes.h>
26#include <vstgui4/vstgui/lib/crect.h>
27#include <vstgui4/vstgui/lib/ccolor.h>
28#include <vstgui4/vstgui/lib/cbitmap.h>
29#include <map>
30#include <memory>
31#include <functional>
32#include <concepts>
33#include <pongasoft/logging/logging.h>
39#include <pongasoft/VST/Types.h>
40
42
43using namespace VSTGUI;
44
45//------------------------------------------------------------------------
46// Implementation Note: Half of the API (the "getter"/"read" part of it) is not used in Release mode because the
47// editor code is built only during Debug mode (EDITOR_MODE is defined) and only the editor code is using it.
48// As a result, it is conditionally compiled so that it doesn't use unnecessary space in Release mode.
49//------------------------------------------------------------------------
50
55{
56public:
58 virtual ~ViewAttribute() = default;
59
60 // Constructor
61 explicit ViewAttribute(std::string iName) :
62 fName(std::move(iName))
63 {}
64
68 virtual IViewCreator::AttrType getType() = 0;
69
74 std::string getName() const
75 {
76 return fName;
77 }
78
85 virtual bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) = 0;
86
87#ifdef EDITOR_MODE
94 virtual bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const = 0;
95
99 virtual bool getPossibleListValues(std::list<const std::string *> &iValues) const
100 {
101 return false;
102 }
103#endif
104
105private:
106 std::string fName;
107};
108
113template<CViewSubclass TView>
114TView *createCustomView(CRect const &iSize,
115 const UIAttributes &iAttributes,
116 const IUIDescription * /* iDescription */) { return new TView(iSize); }
117
120template<typename T>
121using AttrValMap = std::map<std::string, T>;
122
126template<typename T>
127using AttrValInitList = std::initializer_list<typename AttrValMap<T>::value_type>;
128
161template<typename TView>
162class TCustomViewCreator : public ViewCreatorAdapter
163{
173private:
179 template<typename T, typename TGetter, typename TSetter>
181 {
182 public:
183 using Getter = TGetter;
184 using Setter = TSetter;
185
186 // Constructor
187 TAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
188 ViewAttribute(iName),
189#ifdef EDITOR_MODE
190 fGetter{iGetter},
191#endif
192 fSetter{iSetter}
193 { }
194
195 // getType
196 IViewCreator::AttrType getType() override
197 {
198 return IViewCreator::kUnknownType;
199 }
200
205 virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
206 {
207 return false;
208 }
209
211 bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
212 {
213 auto *tv = dynamic_cast<TView *>(iView);
214 if(tv != nullptr)
215 {
216 auto attributeValue = iAttributes.getAttributeValue(getName());
217 if(attributeValue)
218 {
219 T value;
220 if(fromString(iDescription, *attributeValue, value))
221 {
222 std::invoke(fSetter, tv, value);
223 return true;
224 }
225 }
226 }
227 return false;
228 }
229
230#ifdef EDITOR_MODE
235 virtual bool toString(IUIDescription const *iDescription, T const &iValue, std::string &oStringValue) const
236 {
237 return false;
238 }
239
240
242 bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const override
243 {
244 auto *tv = dynamic_cast<TView *>(iView);
245 if(tv != nullptr)
246 {
247 auto value = std::invoke(fGetter, tv);
248 return toString(iDescription, value, oStringValue);
249 }
250 return false;
251 }
252
253 private:
254 Getter fGetter;
255#endif
256
257 private:
259 };
260
264 template<typename T>
265 using ByValAttribute = TAttribute<T, T (TView::*)() const, void (TView::*)(T)>;
266
270 template<typename T>
271 using ByRefAttribute = TAttribute<T, T const &(TView::*)() const, void (TView::*)(T const &)>;
272
278 {
279 public:
280 TagAttribute(std::string const &iName,
283 ByValAttribute<TagID>(iName, iGetter, iSetter) {}
284
285 // getType
286 IViewCreator::AttrType getType() override
287 {
288 return IViewCreator::kTagType;
289 }
290
291 // fromString
292 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
293 {
294 if(iAttributeValue.length() != 0)
295 {
296 auto tag = static_cast<TagID>(iDescription->getTagForName(iAttributeValue.c_str()));
297 if(tag == UNDEFINED_TAG_ID)
298 {
299 char *endPtr = nullptr;
300 tag = static_cast<TagID>(strtol(iAttributeValue.c_str(), &endPtr, 10));
301 if(endPtr == iAttributeValue.c_str())
302 {
303 return false;
304 }
305 }
306 oValue = tag;
307 }
308 else
309 // when selecting "NONE" iAttributeValue is an empty string
310 oValue = UNDEFINED_PARAM_ID;
311
312 return true;
313 }
314
315#ifdef EDITOR_MODE
316 // toString
317 bool toString(IUIDescription const *iDescription, const TagID &iValue, std::string &oStringValue) const override
318 {
319 if(iValue != UNDEFINED_TAG_ID)
320 {
321 UTF8StringPtr controlTag = iDescription->lookupControlTagName(iValue);
322 if(controlTag)
323 {
324 oStringValue = controlTag;
325 return true;
326 }
327 }
328
329 return false;
330 }
331#endif
332 };
333
338 template<Utils::IntegralOrEnum TInt>
339 class IntegerAttribute : public ByValAttribute<TInt>
340 {
341 public:
344
345 // Constructor
346 IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
347 ByValAttribute<TInt>(iName, iGetter, iSetter) {}
348
349 // getType
350 IViewCreator::AttrType getType() override
351 {
352 return IViewCreator::kIntegerType;
353 }
354
355 // fromString
356 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
357 {
358 char *endPtr = nullptr;
359 auto value = static_cast<TInt>(strtol(iAttributeValue.c_str(), &endPtr, 10));
360 if(endPtr == iAttributeValue.c_str())
361 {
362 DLOG_F(WARNING, "could not convert <%s> to an integer", iAttributeValue.c_str());
363 return false;
364 }
365 oValue = value;
366 return true;
367 }
368
369#ifdef EDITOR_MODE
370 // toString
371 bool toString(IUIDescription const *iDescription, const TInt &iValue, std::string &oStringValue) const override
372 {
373 std::stringstream str;
374 str << iValue;
375 oStringValue = str.str();
376 return true;
377 }
378#endif
379 };
380
385 template<std::floating_point TFloat>
386 class FloatAttribute : public ByValAttribute<TFloat>
387 {
388 public:
391
392 // Constructor
393 FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
394 ByValAttribute<TFloat>(iName, iGetter, iSetter) {}
395
396 // getType
397 IViewCreator::AttrType getType() override
398 {
399 return IViewCreator::kFloatType;
400 }
401
402 // fromString
403 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
404 {
405 TFloat value;
406 if(Utils::stringToFloat<TFloat>(iAttributeValue, value))
407 {
408 oValue = value;
409 return true;
410 }
411 DLOG_F(WARNING, "could not convert <%s> to a float", iAttributeValue.c_str());
412 return false;
413 }
414
415#ifdef EDITOR_MODE
416 // toString
417 bool toString(IUIDescription const *iDescription, const TFloat &iValue, std::string &oStringValue) const override
418 {
419 std::stringstream str;
420 str << iValue;
421 oStringValue = str.str();
422 return true;
423 }
424#endif
425 };
426
431 class ColorAttribute : public ByRefAttribute<CColor>
432 {
433 public:
434
435 // Constructor
436 ColorAttribute(std::string const &iName,
439 ByRefAttribute<CColor>(iName, iGetter, iSetter) {}
440
441 // getType
442 IViewCreator::AttrType getType() override
443 {
444 return IViewCreator::kColorType;
445 }
446
447 // fromString
448 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
449 {
450 CColor color;
451 if(UIViewCreator::stringToColor(&iAttributeValue, color, iDescription))
452 {
453 oValue = color;
454 return true;
455 }
456 return false;
457 }
458
459#ifdef EDITOR_MODE
460 // toString
461 bool toString(IUIDescription const *iDescription, const CColor &iValue, std::string &oStringValue) const override
462 {
463 return UIViewCreator::colorToString(iValue, oStringValue, iDescription);
464 }
465#endif
466 };
467
472 class GradientAttribute : public ByValAttribute<GradientPtr>
473 {
474 public:
475
476 // Constructor
477 GradientAttribute(std::string const &iName,
480 ByValAttribute<GradientPtr>(iName, iGetter, iSetter) {}
481
482 // getType
483 IViewCreator::AttrType getType() override
484 {
485 return IViewCreator::kGradientType;
486 }
487
488 // fromString
489 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
490 {
491 auto gradient = iDescription->getGradient(iAttributeValue.c_str());
492 if(gradient)
493 {
494 oValue = gradient;
495 return true;
496 }
497 return false;
498 }
499
500#ifdef EDITOR_MODE
501 // toString
502 bool toString(IUIDescription const *iDescription, GradientPtr const &iValue, std::string &oStringValue) const override
503 {
504 if(iValue)
505 {
506 auto name = iDescription->lookupGradientName(iValue);
507 if(name)
508 {
509 oStringValue = name;
510 return true;
511 }
512
513 }
514 return false;
515 }
516#endif
517 };
518
523 class BooleanAttribute : public ByValAttribute<bool>
524 {
525 public:
526 // Constructor
527 BooleanAttribute(std::string const &iName,
530 ByValAttribute<bool>(iName, iGetter, iSetter) {}
531
532 // getType
533 IViewCreator::AttrType getType() override
534 {
535 return IViewCreator::kBooleanType;
536 }
537
538 // fromString
539 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
540 {
541 if(iAttributeValue == "true")
542 {
543 oValue = true;
544 return true;
545 }
546
547 if(iAttributeValue == "false")
548 {
549 oValue = false;
550 return true;
551 }
552
553 return false;
554 }
555
556#ifdef EDITOR_MODE
557 // toString
558 bool toString(IUIDescription const *iDescription, const bool &iValue, std::string &oStringValue) const override
559 {
560 oStringValue = iValue ? "true" : "false";
561 return true;
562 }
563#endif
564 };
565
570 class BitmapAttribute : public ByValAttribute<BitmapPtr>
571 {
572 public:
573 // Constructor
574 BitmapAttribute(std::string const &iName,
577 ByValAttribute<BitmapPtr>(iName, iGetter, iSetter) {}
578
579 // getType
580 IViewCreator::AttrType getType() override
581 {
582 return IViewCreator::kBitmapType;
583 }
584
585 // fromString
586 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
587 {
588 BitmapPtr bitmap;
589 if(UIViewCreator::stringToBitmap(&iAttributeValue, bitmap, iDescription))
590 {
591 oValue = bitmap;
592 return true;
593 }
594 return false;
595 }
596
597#ifdef EDITOR_MODE
598 // toString
599 bool toString(IUIDescription const *iDescription, BitmapPtr const &iValue, std::string &oStringValue) const override
600 {
601 if(iValue)
602 return UIViewCreator::bitmapToString(iValue, oStringValue, iDescription);
603 else
604 return false;
605 }
606#endif
607 };
608
613 class FontAttribute : public ByValAttribute<FontPtr>
614 {
615 public:
616 // Constructor
617 FontAttribute(std::string const &iName,
620 ByValAttribute<FontPtr>(iName, iGetter, iSetter) {}
621
622 // getType
623 IViewCreator::AttrType getType() override
624 {
625 return IViewCreator::kFontType;
626 }
627
628 // fromString
629 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
630 {
631 auto font = iDescription->getFont(iAttributeValue.c_str());
632 if(font)
633 {
634 oValue = font;
635 return true;
636 }
637 return false;
638 }
639
640#ifdef EDITOR_MODE
641 // toString
642 bool toString(IUIDescription const *iDescription, FontPtr const &iValue, std::string &oStringValue) const override
643 {
644 if(iValue)
645 {
646 auto fontName = iDescription->lookupFontName(iValue);
647 if(fontName)
648 {
649 oStringValue = fontName;
650 return true;
651 }
652 }
653 return false;
654 }
655#endif
656 };
657
662 class MarginAttribute : public ByRefAttribute<Margin>
663 {
664 public:
665 MarginAttribute(std::string const &iName,
668 ByRefAttribute<Margin>(iName, iGetter, iSetter) {}
669
670 // fromString
671 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
672 {
673 auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
674
675 if(parts.empty())
676 return false;
677
678 // look for nan in the array
679 if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
680 return false;
681
682 if(parts.size() == 1)
683 {
684 oValue = Margin{parts[0]};
685 }
686 else
687 {
688 if(parts.size() < 4)
689 return false;
690
691 oValue = Margin{parts[0], parts[1], parts[2], parts[3]};
692 }
693
694 return true;
695 }
696
697#ifdef EDITOR_MODE
698 // toString
699 bool toString(IUIDescription const *iDescription, const Margin &iValue, std::string &oStringValue) const override
700 {
701 std::stringstream str;
702 if(iValue.fTop == iValue.fRight && iValue.fTop == iValue.fBottom && iValue.fTop == iValue.fLeft)
703 str << iValue.fTop;
704 else
705 {
706 str << iValue.fTop;
707 str << ",";
708 str << iValue.fRight;
709 str << ",";
710 str << iValue.fBottom;
711 str << ",";
712 str << iValue.fLeft;
713 }
714 oStringValue = str.str();
715 return true;
716 }
717#endif
718 };
719
724 class RangeAttribute : public ByRefAttribute<Range>
725 {
726 public:
727 RangeAttribute(std::string const &iName,
730 ByRefAttribute<Range>(iName, iGetter, iSetter) {}
731
732 // fromString
733 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
734 {
735 auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
736
737 if(parts.empty())
738 return false;
739
740 // look for nan in the array
741 if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
742 return false;
743
744 if(parts.size() == 1)
745 oValue = Range{parts[0]};
746 else
747 oValue = Range{parts[0], parts[1]};
748
749 return true;
750 }
751
752#ifdef EDITOR_MODE
753 // toString
754 bool toString(IUIDescription const *iDescription, const Range &iValue, std::string &oStringValue) const override
755 {
756 std::stringstream str;
757 if(iValue.fFrom == iValue.fTo)
758 str << iValue.fFrom;
759 else
760 {
761 str << iValue.fFrom;
762 str << ",";
763 str << iValue.fTo;
764 }
765 oStringValue = str.str();
766 return true;
767 }
768#endif
769 };
770
774 class VectorStringAttribute : public ByRefAttribute<std::vector<std::string>>
775 {
777
778 public:
779 // Constructor
780 VectorStringAttribute(std::string const &iName,
781 super_type::Getter iGetter,
782 super_type::Setter iSetter,
783 char iDelimiter = ',',
784 bool iSkipEmptyEntries = false) :
785 super_type(iName, iGetter, iSetter),
786 fDelimiter{iDelimiter},
787 fSkipEmptyEntries{iSkipEmptyEntries}
788 {}
789
790 // fromString
791 bool fromString(IUIDescription const *iDescription,
792 std::string const &iAttributeValue,
793 std::vector<std::string> &oValue) const override
794 {
795 oValue = Utils::splitString(iAttributeValue, fDelimiter, fSkipEmptyEntries);
796 return true;
797 }
798
799#ifdef EDITOR_MODE
800 // toString
801 bool toString(IUIDescription const *iDescription,
802 const std::vector<std::string> &iValue,
803 std::string &oStringValue) const override
804 {
805 oStringValue.clear();
806 int i = 0;
807 for(auto &entry : iValue)
808 {
809 if(i > 0)
810 oStringValue += fDelimiter;
811 if(!entry.empty() || !fSkipEmptyEntries)
812 {
813 oStringValue += entry;
814 i++;
815 }
816 }
817 return true;
818 }
819#endif
820
821 protected:
824 };
825
829 template<typename T>
831 {
833
834 public:
835 // Constructor
836 ListAttribute(std::string const &iName,
837 super_type::Getter iGetter,
838 super_type::Setter iSetter,
839 AttrValInitList<T> const &iAttributeValues) :
840 super_type(iName, iGetter, iSetter),
841 fAttributeValuesMap(iAttributeValues)
842#ifdef EDITOR_MODE
843 ,fAttributeValuesList{iAttributeValues}
844#endif
845 {
846 }
847
848 // getType
849 IViewCreator::AttrType getType() override
850 {
851 return IViewCreator::kListType;
852 }
853
854 // fromString
855 bool fromString(IUIDescription const *iDescription,
856 std::string const &iAttributeValue,
857 T &oValue) const override
858 {
859 if(fAttributeValuesMap.find(iAttributeValue) != fAttributeValuesMap.cend())
860 {
861 oValue = fAttributeValuesMap.at(iAttributeValue);
862 return true;
863 }
864
865 DLOG_F(WARNING, "Attribute value '%s' is not valid for '%s'", iAttributeValue.c_str(), ViewAttribute::getName().c_str());
866 return false;
867 }
868
869#ifdef EDITOR_MODE
870 // toString
871 bool toString(IUIDescription const *iDescription,
872 T const &iValue,
873 std::string &oStringValue) const override
874 {
875 auto pos = std::find_if(std::begin(fAttributeValuesList),
876 std::end(fAttributeValuesList),
877 [&iValue](auto entry) -> bool {
878 return entry.second == iValue;
879 });
880 if(pos != std::end(fAttributeValuesList))
881 {
882 oStringValue = pos->first;
883 return true;
884 }
885
886 return false;
887 }
888
889 // getPossibleListValues
890 bool getPossibleListValues(std::list<const std::string *> &iValues) const override
891 {
892 for(auto const &p : fAttributeValuesList)
893 {
894 iValues.emplace_back(&p.first);
895 }
896 return true;
897 }
898#endif
899
900 protected:
902#ifdef EDITOR_MODE
903 std::vector<typename AttrValMap<T>::value_type> const fAttributeValuesList;
904#endif
905 };
906
907public:
908 // Constructor
909 explicit TCustomViewCreator(char const *iViewName = nullptr,
910 char const *iDisplayName = nullptr,
911 char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
912 fViewName{iViewName},
913 fDisplayName{iDisplayName},
914 fBaseViewName{iBaseViewName},
916 {
917 // this allows for inheritance!
918 if(iViewName != nullptr && iDisplayName != nullptr)
919 VSTGUI::UIViewFactory::registerViewCreator(*this);
920 }
921
922 // Destructor
924 {
925 // we simply clear the map since it holds shared pointers which will be discarded when they are no longer
926 // held by another object
927 fAttributes.clear();
928 }
929
930 // getViewName
931 IdStringPtr getViewName() const override
932 {
933 return fViewName;
934 }
935
936 // getDisplayName
937 UTF8StringPtr getDisplayName() const override
938 {
939 return fDisplayName;
940 }
941
942 // getBaseViewName
943 IdStringPtr getBaseViewName() const override
944 {
945 return fBaseViewName;
946 }
947
951 template<CViewSubclass XView>
953 {
954 for(auto attribute : iOther.fAttributes)
955 {
956 registerAttribute(attribute.second);
957 }
958
959 if(std::string(fBaseViewName) == std::string(VSTGUI::UIViewCreator::kCView))
961 }
962
966 void registerColorAttribute(std::string const &iName,
969 {
970 registerAttribute<ColorAttribute>(iName, iGetter, iSetter);
971 }
972
976 void registerGradientAttribute(std::string const &iName,
979 {
980 registerAttribute<GradientAttribute>(iName, iGetter, iSetter);
981 }
982
986 void registerBitmapAttribute(std::string const &iName,
989 {
990 registerAttribute<BitmapAttribute>(iName, iGetter, iSetter);
991 }
992
996 void registerFontAttribute(std::string const &iName,
997 FontAttribute::Getter iGetter,
998 FontAttribute::Setter iSetter)
999 {
1000 registerAttribute<FontAttribute>(iName, iGetter, iSetter);
1001 }
1002
1006 void registerMarginAttribute(std::string const &iName,
1009 {
1010 registerAttribute<MarginAttribute>(iName, iGetter, iSetter);
1011 }
1012
1016 void registerRangeAttribute(std::string const &iName,
1017 RangeAttribute::Getter iGetter,
1018 RangeAttribute::Setter iSetter)
1019 {
1020 registerAttribute<RangeAttribute>(iName, iGetter, iSetter);
1021 }
1022
1026 void registerVectorStringAttribute(std::string const &iName,
1029 char iDelimiter = ',',
1030 bool iSkipEmptyEntries = false)
1031 {
1032 registerAttribute<VectorStringAttribute>(iName, iGetter, iSetter, iDelimiter, iSkipEmptyEntries);
1033 }
1034
1051 template<typename T>
1052 void registerListAttribute(std::string const &iName,
1053 ListAttribute<T>::Getter iGetter,
1054 ListAttribute<T>::Setter iSetter,
1055 AttrValInitList<T> const &iAttributeValues)
1056 {
1057 registerAttribute<ListAttribute<T>>(iName, iGetter, iSetter, iAttributeValues);
1058 }
1059
1063 void registerTagAttribute(std::string const &iName,
1064 TagAttribute::Getter iGetter,
1065 TagAttribute::Setter iSetter)
1066 {
1067 registerAttribute<TagAttribute>(iName, iGetter, iSetter);
1068 }
1069
1073 template<Utils::IntegralOrEnum TInt>
1074 void registerIntegerAttribute(std::string const &iName,
1077 {
1078 registerAttribute<IntegerAttribute<TInt>>(iName, iGetter, iSetter);
1079 }
1080
1084 void registerIntAttribute(std::string const &iName,
1087 {
1088 registerIntegerAttribute<int32_t>(iName, iGetter, iSetter);
1089 }
1090
1094 void registerFloatAttribute(std::string const &iName,
1097 {
1098 registerAttribute<FloatAttribute<float>>(iName, iGetter, iSetter);
1099 }
1100
1104 void registerDoubleAttribute(std::string const &iName,
1107 {
1108 registerAttribute<FloatAttribute<double>>(iName, iGetter, iSetter);
1109 }
1110
1114 void registerBooleanAttribute(std::string const &iName,
1117 {
1118 registerAttribute<BooleanAttribute>(iName, iGetter, iSetter);
1119 }
1120
1124 CView *create(const UIAttributes &attributes, const IUIDescription *description) const override
1125 {
1126#ifdef JAMBA_DEBUG_LOGGING
1127 DLOG_F(INFO, "CustomViewCreator<%s>::create()", getViewName());
1128#endif
1129
1130 return createCustomView<TView>(CRect(0, 0, 0, 0), attributes, description);
1131 }
1132
1137 bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
1138 {
1139 auto *tv = dynamic_cast<TView *>(view);
1140
1141 if(tv == nullptr)
1142 return false;
1143
1144 for(auto attribute : fAttributes)
1145 {
1146 attribute.second->apply(tv, attributes, description);
1147 }
1148
1149 return true;
1150 }
1151
1152 // getAttributeNames
1153 bool getAttributeNames(std::list<std::string> &attributeNames) const override
1154 {
1155 for(auto attribute : fAttributes)
1156 {
1157 attributeNames.emplace_back(attribute.first);
1158 }
1159 return true;
1160 }
1161
1162 // getAttributeType
1163 AttrType getAttributeType(const std::string &attributeName) const override
1164 {
1165 auto iter = fAttributes.find(attributeName);
1166 if(iter != fAttributes.cend())
1167 {
1168 return iter->second->getType();
1169 }
1170 return kUnknownType;
1171 }
1172
1173#ifdef EDITOR_MODE
1177 bool getAttributeValue(CView *iView,
1178 const std::string &iAttributeName,
1179 std::string &oStringValue,
1180 const IUIDescription *iDescription) const override
1181 {
1182 auto *cdv = dynamic_cast<TView *>(iView);
1183
1184 if(cdv == nullptr)
1185 return false;
1186
1187 auto iter = fAttributes.find(iAttributeName);
1188 if(iter != fAttributes.cend())
1189 {
1190 return iter->second->getAttributeValue(cdv, iDescription, oStringValue);
1191 }
1192
1193 return false;
1194 }
1195
1199 bool getPossibleListValues(const std::string &iAttributeName, std::list<const std::string *> &iValues) const override
1200 {
1201 auto iter = fAttributes.find(iAttributeName);
1202 if(iter != fAttributes.cend())
1203 {
1204 return iter->second->getPossibleListValues(iValues);
1205 }
1206 return false;
1207 }
1208#endif
1209
1210private:
1211
1212 // somehow this is required...
1213 template<typename XView>
1215
1219 void registerAttribute(std::shared_ptr<ViewAttribute> iAttribute)
1220 {
1221 // making sure there are no duplicates (cannot use loguru here!)
1222#ifndef NDEBUG
1223 assert(fAttributes.find(iAttribute->getName()) == fAttributes.cend());
1224#endif
1225 fAttributes[iAttribute->getName()] = std::move(iAttribute);
1226 }
1227
1231 template<typename TViewAttribute, typename... Args>
1232 void registerAttribute(std::string const &iName,
1233 TViewAttribute::Getter iGetter,
1234 TViewAttribute::Setter iSetter,
1235 Args&& ...iArgs)
1236 {
1237 std::shared_ptr<ViewAttribute> cva;
1238 cva.reset(new TViewAttribute(iName, iGetter, iSetter, std::forward<Args>(iArgs)...));
1239 registerAttribute(cva);
1240 }
1241
1242
1243 char const *fViewName;
1244 char const *fDisplayName;
1245 char const *fBaseViewName;
1246
1247 // use a map of shared pointers so that they can easily be copied (see registerAttributes)
1248 std::map<std::string, std::shared_ptr<ViewAttribute>> fAttributes;
1249};
1250
1251namespace impl {
1253 template<typename T>
1254 concept HasCreator =
1255 requires { typename T::Creator; } && std::default_initializable<typename T::Creator>; }
1256
1326template<typename TView, typename TBaseView = void>
1328{
1329public:
1330 explicit CustomViewCreator(char const *iViewName = nullptr,
1331 char const *iDisplayName = nullptr,
1332 char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
1333 TCustomViewCreator<TView>(iViewName, iDisplayName, iBaseViewName)
1334 {
1335 if constexpr(impl::HasCreator<TBaseView>)
1336 TCustomViewCreator<TView>::registerAttributes(typename TBaseView::Creator());
1337 }
1338};
1339
1340}
1341
CustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition CustomViewCreator.h:1330
BitmapAttribute(std::string const &iName, ByValAttribute< BitmapPtr >::Getter iGetter, ByValAttribute< BitmapPtr >::Setter iSetter)
Definition CustomViewCreator.h:574
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:580
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
Definition CustomViewCreator.h:586
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:533
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
Definition CustomViewCreator.h:539
BooleanAttribute(std::string const &iName, ByValAttribute< bool >::Getter iGetter, ByValAttribute< bool >::Setter iSetter)
Definition CustomViewCreator.h:527
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
Definition CustomViewCreator.h:448
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:442
ColorAttribute(std::string const &iName, ByRefAttribute< CColor >::Getter iGetter, ByRefAttribute< CColor >::Setter iSetter)
Definition CustomViewCreator.h:436
FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:393
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:397
ByValAttribute< TFloat >::Setter Setter
Definition CustomViewCreator.h:390
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
Definition CustomViewCreator.h:403
ByValAttribute< TFloat >::Getter Getter
Definition CustomViewCreator.h:389
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:623
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
Definition CustomViewCreator.h:629
FontAttribute(std::string const &iName, ByValAttribute< FontPtr >::Getter iGetter, ByValAttribute< FontPtr >::Setter iSetter)
Definition CustomViewCreator.h:617
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:483
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
Definition CustomViewCreator.h:489
GradientAttribute(std::string const &iName, ByValAttribute< GradientPtr >::Getter iGetter, ByValAttribute< GradientPtr >::Setter iSetter)
Definition CustomViewCreator.h:477
ByValAttribute< TInt >::Setter Setter
Definition CustomViewCreator.h:343
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:350
ByValAttribute< TInt >::Getter Getter
Definition CustomViewCreator.h:342
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
Definition CustomViewCreator.h:356
IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:346
ListAttribute(std::string const &iName, super_type::Getter iGetter, super_type::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Definition CustomViewCreator.h:836
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const override
Subclasses need to implement this method to convert a string (iAttributeValue) to a T.
Definition CustomViewCreator.h:855
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:849
ByValAttribute< T > super_type
Definition CustomViewCreator.h:832
AttrValMap< T > const fAttributeValuesMap
Definition CustomViewCreator.h:901
MarginAttribute(std::string const &iName, ByRefAttribute< Margin >::Getter iGetter, ByRefAttribute< Margin >::Setter iSetter)
Definition CustomViewCreator.h:665
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
Definition CustomViewCreator.h:671
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
Definition CustomViewCreator.h:733
RangeAttribute(std::string const &iName, ByRefAttribute< Range >::Getter iGetter, ByRefAttribute< Range >::Setter iSetter)
Definition CustomViewCreator.h:727
Setter fSetter
Definition CustomViewCreator.h:258
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:196
TAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:187
TGetter Getter
Definition CustomViewCreator.h:183
virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
Definition CustomViewCreator.h:205
TSetter Setter
Definition CustomViewCreator.h:184
bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
Definition CustomViewCreator.h:211
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
Definition CustomViewCreator.h:292
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:286
TagAttribute(std::string const &iName, ByValAttribute< TagID >::Getter iGetter, ByValAttribute< TagID >::Setter iSetter)
Definition CustomViewCreator.h:280
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, std::vector< std::string > &oValue) const override
Definition CustomViewCreator.h:791
ByRefAttribute< std::vector< std::string > > super_type
Definition CustomViewCreator.h:776
VectorStringAttribute(std::string const &iName, super_type::Getter iGetter, super_type::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Definition CustomViewCreator.h:780
TAttribute< T, T const &(CustomViewAdapter::*)() const, void(CustomViewAdapter::*)(T const &)> ByRefAttribute
Definition CustomViewCreator.h:271
void registerFloatAttribute(std::string const &iName, FloatAttribute< float >::Getter iGetter, FloatAttribute< float >::Setter iSetter)
Registers a float attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1094
void registerBitmapAttribute(std::string const &iName, BitmapAttribute::Getter iGetter, BitmapAttribute::Setter iSetter)
Registers a bitmap attribute with the given name and getter/setter.
Definition CustomViewCreator.h:986
void registerBooleanAttribute(std::string const &iName, BooleanAttribute::Getter iGetter, BooleanAttribute::Setter iSetter)
Registers a boolean attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1114
char const * fBaseViewName
Definition CustomViewCreator.h:1245
void registerMarginAttribute(std::string const &iName, MarginAttribute::Getter iGetter, MarginAttribute::Setter iSetter)
Registers a Margin attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1006
UTF8StringPtr getDisplayName() const override
Definition CustomViewCreator.h:937
CView * create(const UIAttributes &attributes, const IUIDescription *description) const override
This is the factory method which will instantiate the view.
Definition CustomViewCreator.h:1124
void registerVectorStringAttribute(std::string const &iName, VectorStringAttribute::Getter iGetter, VectorStringAttribute::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Registers a Range attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1026
void registerColorAttribute(std::string const &iName, ColorAttribute::Getter iGetter, ColorAttribute::Setter iSetter)
Registers a color attribute with the given name and getter/setter.
Definition CustomViewCreator.h:966
void registerDoubleAttribute(std::string const &iName, FloatAttribute< double >::Getter iGetter, FloatAttribute< double >::Setter iSetter)
Registers a double attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1104
void registerRangeAttribute(std::string const &iName, RangeAttribute::Getter iGetter, RangeAttribute::Setter iSetter)
Registers a Range attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1016
void registerFontAttribute(std::string const &iName, FontAttribute::Getter iGetter, FontAttribute::Setter iSetter)
Registers a font attribute with the given name and getter/setter.
Definition CustomViewCreator.h:996
~TCustomViewCreator() override
Definition CustomViewCreator.h:923
void registerIntegerAttribute(std::string const &iName, IntegerAttribute< TInt >::Getter iGetter, IntegerAttribute< TInt >::Setter iSetter)
Registers an Integer (any kind) attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1074
IdStringPtr getViewName() const override
Definition CustomViewCreator.h:931
void registerAttributes(TCustomViewCreator< XView > const &iOther)
This method is called to register all the attributes from another CustomViewCreator (used in case of ...
Definition CustomViewCreator.h:952
friend class TCustomViewCreator
Definition CustomViewCreator.h:1214
bool getAttributeNames(std::list< std::string > &attributeNames) const override
Definition CustomViewCreator.h:1153
IdStringPtr getBaseViewName() const override
Definition CustomViewCreator.h:943
void registerIntAttribute(std::string const &iName, IntegerAttribute< int32_t >::Getter iGetter, IntegerAttribute< int32_t >::Setter iSetter)
Registers an int attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1084
AttrType getAttributeType(const std::string &attributeName) const override
Definition CustomViewCreator.h:1163
void registerTagAttribute(std::string const &iName, TagAttribute::Getter iGetter, TagAttribute::Setter iSetter)
Registers a tag attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1063
void registerAttribute(std::shared_ptr< ViewAttribute > iAttribute)
Internal method to register an attribute... check that names are not duplicate!
Definition CustomViewCreator.h:1219
void registerAttribute(std::string const &iName, TViewAttribute::Getter iGetter, TViewAttribute::Setter iSetter, Args &&...iArgs)
Generic register attribute.
Definition CustomViewCreator.h:1232
TAttribute< T, T(CustomViewAdapter::*)() const, void(CustomViewAdapter::*)(T)> ByValAttribute
Definition CustomViewCreator.h:265
void registerGradientAttribute(std::string const &iName, GradientAttribute::Getter iGetter, GradientAttribute::Setter iSetter)
Registers a gradient attribute with the given name and getter/setter.
Definition CustomViewCreator.h:976
std::map< std::string, std::shared_ptr< ViewAttribute > > fAttributes
Definition CustomViewCreator.h:1248
void registerListAttribute(std::string const &iName, ListAttribute< T >::Getter iGetter, ListAttribute< T >::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Registers a list attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1052
char const * fViewName
Definition CustomViewCreator.h:1243
char const * fDisplayName
Definition CustomViewCreator.h:1244
TCustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition CustomViewCreator.h:909
bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
Extract all the attribute values and apply them to the view.
Definition CustomViewCreator.h:1137
Base abstract class for an attribute of a view.
Definition CustomViewCreator.h:55
std::string fName
Definition CustomViewCreator.h:106
virtual ~ViewAttribute()=default
Destructor (needed for polymorphic base type).
virtual bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription)=0
Extracts the value from iAttributes for getName() attribute and "apply" it on the view provided.
std::string getName() const
Name of the attribute (which ends up being an attribute in the xml file) Ex: <view back-color="~ Blac...
Definition CustomViewCreator.h:74
virtual IViewCreator::AttrType getType()=0
ViewAttribute(std::string iName)
Definition CustomViewCreator.h:61
Checks that T has a nested Creator class and that it can be default constructed.
Definition CustomViewCreator.h:1254
std::vector< std::string > splitString(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries)
Split a string according to a delimiter and returns a vector.
Definition StringUtils.cpp:31
std::vector< T > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to an array of floating points (floats or doubles).
Definition StringUtils.h:109
bool stringToFloat(const std::string &iString, T &oValue)
Converts the string to a floating point (float or double).
Definition StringUtils.h:77
Definition CustomViewCreator.h:1251
Definition CustomController.h:26
std::initializer_list< typename AttrValMap< T >::value_type > AttrValInitList
Defines the type to initialize an [AttrValMap], for an example check [TCustomViewCreator::registerLis...
Definition CustomViewCreator.h:127
std::map< std::string, T > AttrValMap
Defines a map of string to attribute value.
Definition CustomViewCreator.h:121
TView * createCustomView(CRect const &iSize, const UIAttributes &iAttributes, const IUIDescription *)
Factory method which creates the actual view.
Definition CustomViewCreator.h:114
CGradient * GradientPtr
Definition Types.h:56
CFontDesc * FontPtr
Definition Types.h:52
CBitmap * BitmapPtr
Definition Types.h:48
Utils::Range< CCoord > Range
Defines a Range.
Definition Types.h:62
ParamID TagID
Defining a type for tags.
Definition Types.h:58
constexpr TagID UNDEFINED_TAG_ID
Constant used to test whether the TagID represents a valid id or an undefined one.
Definition Types.h:62
constexpr ParamID UNDEFINED_PARAM_ID
Constant used throughout the code to test whether the ParamID represents a valid id or an undefined o...
Definition Types.h:48
T fFrom
Definition Lerp.h:350
T fTo
Definition Lerp.h:351
Margin is a similar concept to css: used to create space around elements, outside of any defined bord...
Definition LookAndFeel.h:34
CCoord fLeft
Definition LookAndFeel.h:62
CCoord fTop
Definition LookAndFeel.h:59
CCoord fRight
Definition LookAndFeel.h:60
CCoord fBottom
Definition LookAndFeel.h:61