Jamba C++ API 7.5.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 <pongasoft/logging/logging.h>
37#include <pongasoft/VST/Types.h>
38
40
41using namespace VSTGUI;
42
43//------------------------------------------------------------------------
44// Implementation Note: Half of the API (the "getter"/"read" part of it) is not used in Release mode because the
45// editor code is built only during Debug mode (EDITOR_MODE is defined) and only the editor code is using it.
46// As a result, it is conditionally compiled so that it doesn't use unnecessary space in Release mode.
47//------------------------------------------------------------------------
48
53{
54public:
55 // Constructor
56 explicit ViewAttribute(std::string iName) :
57 fName(std::move(iName))
58 {}
59
63 virtual IViewCreator::AttrType getType() = 0;
64
69 std::string getName() const
70 {
71 return fName;
72 }
73
80 virtual bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) = 0;
81
82#ifdef EDITOR_MODE
89 virtual bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const = 0;
90
94 virtual bool getPossibleListValues(std::list<const std::string *> &iValues) const
95 {
96 return false;
97 }
98#endif
99
100private:
101 std::string fName;
102};
103
108template<typename TView>
109inline TView *createCustomView(CRect const &iSize,
110 const UIAttributes &iAttributes,
111 const IUIDescription * /* iDescription */) { return new TView(iSize); }
112
115template<typename T>
116using AttrValMap = std::map<std::string, T>;
117
121template<typename T>
122using AttrValInitList = std::initializer_list<typename AttrValMap<T>::value_type>;
123
156template<typename TView>
157class TCustomViewCreator : public ViewCreatorAdapter
158{
159private:
165 template<typename T, typename TGetter, typename TSetter>
167 {
168 public:
169 using Getter = TGetter;
170 using Setter = TSetter;
171
172 // Constructor
173 TAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
174 ViewAttribute(iName),
175#ifdef EDITOR_MODE
176 fGetter{iGetter},
177#endif
178 fSetter{iSetter}
179 { }
180
181 // getType
182 IViewCreator::AttrType getType() override
183 {
184 return IViewCreator::kUnknownType;
185 }
186
191 virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
192 {
193 return false;
194 }
195
197 bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
198 {
199 auto *tv = dynamic_cast<TView *>(iView);
200 if(tv != nullptr)
201 {
202 auto attributeValue = iAttributes.getAttributeValue(getName());
203 if(attributeValue)
204 {
205 T value;
206 if(fromString(iDescription, *attributeValue, value))
207 {
208 std::invoke(fSetter, tv, value);
209 return true;
210 }
211 }
212 }
213 return false;
214 }
215
216#ifdef EDITOR_MODE
221 virtual bool toString(IUIDescription const *iDescription, T const &iValue, std::string &oStringValue) const
222 {
223 return false;
224 }
225
226
228 bool getAttributeValue(CView *iView, const IUIDescription *iDescription, std::string &oStringValue) const override
229 {
230 auto *tv = dynamic_cast<TView *>(iView);
231 if(tv != nullptr)
232 {
233 auto value = std::invoke(fGetter, tv);
234 return toString(iDescription, value, oStringValue);
235 }
236 return false;
237 }
238
239 private:
240 Getter fGetter;
241#endif
242
243 private:
245 };
246
250 template<typename T>
251 using ByValAttribute = TAttribute<T, T (TView::*)() const, void (TView::*)(T)>;
252
256 template<typename T>
257 using ByRefAttribute = TAttribute<T, T const &(TView::*)() const, void (TView::*)(T const &)>;
258
264 {
265 public:
266 TagAttribute(std::string const &iName,
267 typename ByValAttribute<TagID>::Getter iGetter,
268 typename ByValAttribute<TagID>::Setter iSetter) :
269 ByValAttribute<TagID>(iName, iGetter, iSetter) {}
270
271 // getType
272 IViewCreator::AttrType getType() override
273 {
274 return IViewCreator::kTagType;
275 }
276
277 // fromString
278 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
279 {
280 if(iAttributeValue.length() != 0)
281 {
282 auto tag = static_cast<TagID>(iDescription->getTagForName(iAttributeValue.c_str()));
283 if(tag == UNDEFINED_TAG_ID)
284 {
285 char *endPtr = nullptr;
286 tag = static_cast<TagID>(strtol(iAttributeValue.c_str(), &endPtr, 10));
287 if(endPtr == iAttributeValue.c_str())
288 {
289 return false;
290 }
291 }
292 oValue = tag;
293 }
294 else
295 // when selecting "NONE" iAttributeValue is an empty string
296 oValue = UNDEFINED_PARAM_ID;
297
298 return true;
299 }
300
301#ifdef EDITOR_MODE
302 // toString
303 bool toString(IUIDescription const *iDescription, const TagID &iValue, std::string &oStringValue) const override
304 {
305 if(iValue != UNDEFINED_TAG_ID)
306 {
307 UTF8StringPtr controlTag = iDescription->lookupControlTagName(iValue);
308 if(controlTag)
309 {
310 oStringValue = controlTag;
311 return true;
312 }
313 }
314
315 return false;
316 }
317#endif
318 };
319
324 template<typename TInt>
325 class IntegerAttribute : public ByValAttribute<TInt>
326 {
327 public:
330
331 // Constructor
332 IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
333 ByValAttribute<TInt>(iName, iGetter, iSetter) {}
334
335 // getType
336 IViewCreator::AttrType getType() override
337 {
338 return IViewCreator::kIntegerType;
339 }
340
341 // fromString
342 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
343 {
344 char *endPtr = nullptr;
345 auto value = static_cast<TInt>(strtol(iAttributeValue.c_str(), &endPtr, 10));
346 if(endPtr == iAttributeValue.c_str())
347 {
348 DLOG_F(WARNING, "could not convert <%s> to an integer", iAttributeValue.c_str());
349 return false;
350 }
351 oValue = value;
352 return true;
353 }
354
355#ifdef EDITOR_MODE
356 // toString
357 bool toString(IUIDescription const *iDescription, const TInt &iValue, std::string &oStringValue) const override
358 {
359 std::stringstream str;
360 str << iValue;
361 oStringValue = str.str();
362 return true;
363 }
364#endif
365 };
366
371 template<typename TFloat>
372 class FloatAttribute : public ByValAttribute<TFloat>
373 {
374 public:
377
378 // Constructor
379 FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter) :
380 ByValAttribute<TFloat>(iName, iGetter, iSetter) {}
381
382 // getType
383 IViewCreator::AttrType getType() override
384 {
385 return IViewCreator::kFloatType;
386 }
387
388 // fromString
389 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
390 {
391 TFloat value;
392 if(Utils::stringToFloat<TFloat>(iAttributeValue, value))
393 {
394 oValue = value;
395 return true;
396 }
397 DLOG_F(WARNING, "could not convert <%s> to a float", iAttributeValue.c_str());
398 return false;
399 }
400
401#ifdef EDITOR_MODE
402 // toString
403 bool toString(IUIDescription const *iDescription, const TFloat &iValue, std::string &oStringValue) const override
404 {
405 std::stringstream str;
406 str << iValue;
407 oStringValue = str.str();
408 return true;
409 }
410#endif
411 };
412
417 class ColorAttribute : public ByRefAttribute<CColor>
418 {
419 public:
420
421 // Constructor
422 ColorAttribute(std::string const &iName,
423 typename ByRefAttribute<CColor>::Getter iGetter,
424 typename ByRefAttribute<CColor>::Setter iSetter) :
425 ByRefAttribute<CColor>(iName, iGetter, iSetter) {}
426
427 // getType
428 IViewCreator::AttrType getType() override
429 {
430 return IViewCreator::kColorType;
431 }
432
433 // fromString
434 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
435 {
436 CColor color;
437 if(UIViewCreator::stringToColor(&iAttributeValue, color, iDescription))
438 {
439 oValue = color;
440 return true;
441 }
442 return false;
443 }
444
445#ifdef EDITOR_MODE
446 // toString
447 bool toString(IUIDescription const *iDescription, const CColor &iValue, std::string &oStringValue) const override
448 {
449 return UIViewCreator::colorToString(iValue, oStringValue, iDescription);
450 }
451#endif
452 };
453
458 class GradientAttribute : public ByValAttribute<GradientPtr>
459 {
460 public:
461
462 // Constructor
463 GradientAttribute(std::string const &iName,
465 typename ByValAttribute<GradientPtr>::Setter iSetter) :
466 ByValAttribute<GradientPtr>(iName, iGetter, iSetter) {}
467
468 // getType
469 IViewCreator::AttrType getType() override
470 {
471 return IViewCreator::kGradientType;
472 }
473
474 // fromString
475 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
476 {
477 auto gradient = iDescription->getGradient(iAttributeValue.c_str());
478 if(gradient)
479 {
480 oValue = gradient;
481 return true;
482 }
483 return false;
484 }
485
486#ifdef EDITOR_MODE
487 // toString
488 bool toString(IUIDescription const *iDescription, GradientPtr const &iValue, std::string &oStringValue) const override
489 {
490 if(iValue)
491 {
492 auto name = iDescription->lookupGradientName(iValue);
493 if(name)
494 {
495 oStringValue = name;
496 return true;
497 }
498
499 }
500 return false;
501 }
502#endif
503 };
504
509 class BooleanAttribute : public ByValAttribute<bool>
510 {
511 public:
512 // Constructor
513 BooleanAttribute(std::string const &iName,
514 typename ByValAttribute<bool>::Getter iGetter,
515 typename ByValAttribute<bool>::Setter iSetter) :
516 ByValAttribute<bool>(iName, iGetter, iSetter) {}
517
518 // getType
519 IViewCreator::AttrType getType() override
520 {
521 return IViewCreator::kBooleanType;
522 }
523
524 // fromString
525 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
526 {
527 if(iAttributeValue == "true")
528 {
529 oValue = true;
530 return true;
531 }
532
533 if(iAttributeValue == "false")
534 {
535 oValue = false;
536 return true;
537 }
538
539 return false;
540 }
541
542#ifdef EDITOR_MODE
543 // toString
544 bool toString(IUIDescription const *iDescription, const bool &iValue, std::string &oStringValue) const override
545 {
546 oStringValue = iValue ? "true" : "false";
547 return true;
548 }
549#endif
550 };
551
556 class BitmapAttribute : public ByValAttribute<BitmapPtr>
557 {
558 public:
559 // Constructor
560 BitmapAttribute(std::string const &iName,
561 typename ByValAttribute<BitmapPtr>::Getter iGetter,
562 typename ByValAttribute<BitmapPtr>::Setter iSetter) :
563 ByValAttribute<BitmapPtr>(iName, iGetter, iSetter) {}
564
565 // getType
566 IViewCreator::AttrType getType() override
567 {
568 return IViewCreator::kBitmapType;
569 }
570
571 // fromString
572 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
573 {
574 BitmapPtr bitmap;
575 if(UIViewCreator::stringToBitmap(&iAttributeValue, bitmap, iDescription))
576 {
577 oValue = bitmap;
578 return true;
579 }
580 return false;
581 }
582
583#ifdef EDITOR_MODE
584 // toString
585 bool toString(IUIDescription const *iDescription, BitmapPtr const &iValue, std::string &oStringValue) const override
586 {
587 if(iValue)
588 return UIViewCreator::bitmapToString(iValue, oStringValue, iDescription);
589 else
590 return false;
591 }
592#endif
593 };
594
599 class FontAttribute : public ByValAttribute<FontPtr>
600 {
601 public:
602 // Constructor
603 FontAttribute(std::string const &iName,
604 typename ByValAttribute<FontPtr>::Getter iGetter,
605 typename ByValAttribute<FontPtr>::Setter iSetter) :
606 ByValAttribute<FontPtr>(iName, iGetter, iSetter) {}
607
608 // getType
609 IViewCreator::AttrType getType() override
610 {
611 return IViewCreator::kFontType;
612 }
613
614 // fromString
615 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
616 {
617 auto font = iDescription->getFont(iAttributeValue.c_str());
618 if(font)
619 {
620 oValue = font;
621 return true;
622 }
623 return false;
624 }
625
626#ifdef EDITOR_MODE
627 // toString
628 bool toString(IUIDescription const *iDescription, FontPtr const &iValue, std::string &oStringValue) const override
629 {
630 if(iValue)
631 {
632 auto fontName = iDescription->lookupFontName(iValue);
633 if(fontName)
634 {
635 oStringValue = fontName;
636 return true;
637 }
638 }
639 return false;
640 }
641#endif
642 };
643
648 class MarginAttribute : public ByRefAttribute<Margin>
649 {
650 public:
651 MarginAttribute(std::string const &iName,
652 typename ByRefAttribute<Margin>::Getter iGetter,
653 typename ByRefAttribute<Margin>::Setter iSetter) :
654 ByRefAttribute<Margin>(iName, iGetter, iSetter) {}
655
656 // fromString
657 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
658 {
659 auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
660
661 if(parts.empty())
662 return false;
663
664 // look for nan in the array
665 if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
666 return false;
667
668 if(parts.size() == 1)
669 {
670 oValue = Margin{parts[0]};
671 }
672 else
673 {
674 if(parts.size() < 4)
675 return false;
676
677 oValue = Margin{parts[0], parts[1], parts[2], parts[3]};
678 }
679
680 return true;
681 }
682
683#ifdef EDITOR_MODE
684 // toString
685 bool toString(IUIDescription const *iDescription, const Margin &iValue, std::string &oStringValue) const override
686 {
687 std::stringstream str;
688 if(iValue.fTop == iValue.fRight && iValue.fTop == iValue.fBottom && iValue.fTop == iValue.fLeft)
689 str << iValue.fTop;
690 else
691 {
692 str << iValue.fTop;
693 str << ",";
694 str << iValue.fRight;
695 str << ",";
696 str << iValue.fBottom;
697 str << ",";
698 str << iValue.fLeft;
699 }
700 oStringValue = str.str();
701 return true;
702 }
703#endif
704 };
705
710 class RangeAttribute : public ByRefAttribute<Range>
711 {
712 public:
713 RangeAttribute(std::string const &iName,
714 typename ByRefAttribute<Range>::Getter iGetter,
715 typename ByRefAttribute<Range>::Setter iSetter) :
716 ByRefAttribute<Range>(iName, iGetter, iSetter) {}
717
718 // fromString
719 bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
720 {
721 auto parts = Utils::splitFloats<CCoord>(iAttributeValue, ',');
722
723 if(parts.empty())
724 return false;
725
726 // look for nan in the array
727 if(std::find_if(parts.cbegin(), parts.cend(), [] (auto f) {return std::isnan(f);}) != parts.cend())
728 return false;
729
730 if(parts.size() == 1)
731 oValue = Range{parts[0]};
732 else
733 oValue = Range{parts[0], parts[1]};
734
735 return true;
736 }
737
738#ifdef EDITOR_MODE
739 // toString
740 bool toString(IUIDescription const *iDescription, const Range &iValue, std::string &oStringValue) const override
741 {
742 std::stringstream str;
743 if(iValue.fFrom == iValue.fTo)
744 str << iValue.fFrom;
745 else
746 {
747 str << iValue.fFrom;
748 str << ",";
749 str << iValue.fTo;
750 }
751 oStringValue = str.str();
752 return true;
753 }
754#endif
755 };
756
760 class VectorStringAttribute : public ByRefAttribute<std::vector<std::string>>
761 {
763
764 public:
765 // Constructor
766 VectorStringAttribute(std::string const &iName,
767 typename super_type::Getter iGetter,
768 typename super_type::Setter iSetter,
769 char iDelimiter = ',',
770 bool iSkipEmptyEntries = false) :
771 super_type(iName, iGetter, iSetter),
772 fDelimiter{iDelimiter},
773 fSkipEmptyEntries{iSkipEmptyEntries}
774 {}
775
776 // fromString
777 bool fromString(IUIDescription const *iDescription,
778 std::string const &iAttributeValue,
779 std::vector<std::string> &oValue) const override
780 {
781 oValue = Utils::splitString(iAttributeValue, fDelimiter, fSkipEmptyEntries);
782 return true;
783 }
784
785#ifdef EDITOR_MODE
786 // toString
787 bool toString(IUIDescription const *iDescription,
788 const std::vector<std::string> &iValue,
789 std::string &oStringValue) const override
790 {
791 oStringValue.clear();
792 int i = 0;
793 for(auto &entry : iValue)
794 {
795 if(i > 0)
796 oStringValue += fDelimiter;
797 if(!entry.empty() || !fSkipEmptyEntries)
798 {
799 oStringValue += entry;
800 i++;
801 }
802 }
803 return true;
804 }
805#endif
806
807 protected:
810 };
811
815 template<typename T>
817 {
819
820 public:
821 // Constructor
822 ListAttribute(std::string const &iName,
823 typename super_type::Getter iGetter,
824 typename super_type::Setter iSetter,
825 AttrValInitList<T> const &iAttributeValues) :
826 super_type(iName, iGetter, iSetter),
827 fAttributeValuesMap(iAttributeValues)
828#ifdef EDITOR_MODE
829 ,fAttributeValuesList{iAttributeValues}
830#endif
831 {
832 }
833
834 // getType
835 IViewCreator::AttrType getType() override
836 {
837 return IViewCreator::kListType;
838 }
839
840 // fromString
841 bool fromString(IUIDescription const *iDescription,
842 std::string const &iAttributeValue,
843 T &oValue) const override
844 {
845 if(fAttributeValuesMap.find(iAttributeValue) != fAttributeValuesMap.cend())
846 {
847 oValue = fAttributeValuesMap.at(iAttributeValue);
848 return true;
849 }
850
851 DLOG_F(WARNING, "Attribute value '%s' is not valid for '%s'", iAttributeValue.c_str(), ViewAttribute::getName().c_str());
852 return false;
853 }
854
855#ifdef EDITOR_MODE
856 // toString
857 bool toString(IUIDescription const *iDescription,
858 T const &iValue,
859 std::string &oStringValue) const override
860 {
861 auto pos = std::find_if(std::begin(fAttributeValuesList),
862 std::end(fAttributeValuesList),
863 [&iValue](auto entry) -> bool {
864 return entry.second == iValue;
865 });
866 if(pos != std::end(fAttributeValuesList))
867 {
868 oStringValue = pos->first;
869 return true;
870 }
871
872 return false;
873 }
874
875 // getPossibleListValues
876 bool getPossibleListValues(std::list<const std::string *> &iValues) const override
877 {
878 for(auto const &p : fAttributeValuesList)
879 {
880 iValues.emplace_back(&p.first);
881 }
882 return true;
883 }
884#endif
885
886 protected:
888#ifdef EDITOR_MODE
889 std::vector<typename AttrValMap<T>::value_type> const fAttributeValuesList;
890#endif
891 };
892
893public:
894 // Constructor
895 explicit TCustomViewCreator(char const *iViewName = nullptr,
896 char const *iDisplayName = nullptr,
897 char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
898 fViewName{iViewName},
899 fDisplayName{iDisplayName},
900 fBaseViewName{iBaseViewName},
902 {
903 // this allows for inheritance!
904 if(iViewName != nullptr && iDisplayName != nullptr)
905 VSTGUI::UIViewFactory::registerViewCreator(*this);
906 }
907
908 // Destructor
910 {
911 // we simply clear the map since it holds shared pointers which will be discarded when they are no longer
912 // held by another object
913 fAttributes.clear();
914 }
915
916 // getViewName
917 IdStringPtr getViewName() const override
918 {
919 return fViewName;
920 }
921
922 // getDisplayName
923 UTF8StringPtr getDisplayName() const override
924 {
925 return fDisplayName;
926 }
927
928 // getBaseViewName
929 IdStringPtr getBaseViewName() const override
930 {
931 return fBaseViewName;
932 }
933
937 template<typename XView>
939 {
940 for(auto attribute : iOther.fAttributes)
941 {
942 registerAttribute(attribute.second);
943 }
944
945 if(std::string(fBaseViewName) == std::string(VSTGUI::UIViewCreator::kCView))
947 }
948
952 void registerColorAttribute(std::string const &iName,
953 typename ColorAttribute::Getter iGetter,
954 typename ColorAttribute::Setter iSetter)
955 {
956 registerAttribute<ColorAttribute>(iName, iGetter, iSetter);
957 }
958
962 void registerGradientAttribute(std::string const &iName,
963 typename GradientAttribute::Getter iGetter,
964 typename GradientAttribute::Setter iSetter)
965 {
966 registerAttribute<GradientAttribute>(iName, iGetter, iSetter);
967 }
968
972 void registerBitmapAttribute(std::string const &iName,
973 typename BitmapAttribute::Getter iGetter,
974 typename BitmapAttribute::Setter iSetter)
975 {
976 registerAttribute<BitmapAttribute>(iName, iGetter, iSetter);
977 }
978
982 void registerFontAttribute(std::string const &iName,
983 typename FontAttribute::Getter iGetter,
984 typename FontAttribute::Setter iSetter)
985 {
986 registerAttribute<FontAttribute>(iName, iGetter, iSetter);
987 }
988
992 void registerMarginAttribute(std::string const &iName,
993 typename MarginAttribute::Getter iGetter,
994 typename MarginAttribute::Setter iSetter)
995 {
996 registerAttribute<MarginAttribute>(iName, iGetter, iSetter);
997 }
998
1002 void registerRangeAttribute(std::string const &iName,
1003 typename RangeAttribute::Getter iGetter,
1004 typename RangeAttribute::Setter iSetter)
1005 {
1006 registerAttribute<RangeAttribute>(iName, iGetter, iSetter);
1007 }
1008
1012 void registerVectorStringAttribute(std::string const &iName,
1013 typename VectorStringAttribute::Getter iGetter,
1014 typename VectorStringAttribute::Setter iSetter,
1015 char iDelimiter = ',',
1016 bool iSkipEmptyEntries = false)
1017 {
1018 registerAttribute<VectorStringAttribute>(iName, iGetter, iSetter, iDelimiter, iSkipEmptyEntries);
1019 }
1020
1037 template<typename T>
1038 void registerListAttribute(std::string const &iName,
1039 typename ListAttribute<T>::Getter iGetter,
1040 typename ListAttribute<T>::Setter iSetter,
1041 AttrValInitList<T> const &iAttributeValues)
1042 {
1043 registerAttribute<ListAttribute<T>>(iName, iGetter, iSetter, iAttributeValues);
1044 }
1045
1049 void registerTagAttribute(std::string const &iName,
1050 typename TagAttribute::Getter iGetter,
1051 typename TagAttribute::Setter iSetter)
1052 {
1053 registerAttribute<TagAttribute>(iName, iGetter, iSetter);
1054 }
1055
1059 template<typename TInt>
1060 void registerIntegerAttribute(std::string const &iName,
1061 typename IntegerAttribute<TInt>::Getter iGetter,
1062 typename IntegerAttribute<TInt>::Setter iSetter)
1063 {
1064 registerAttribute<IntegerAttribute<TInt>>(iName, iGetter, iSetter);
1065 }
1066
1070 void registerIntAttribute(std::string const &iName,
1071 typename IntegerAttribute<int32_t>::Getter iGetter,
1072 typename IntegerAttribute<int32_t>::Setter iSetter)
1073 {
1074 registerIntegerAttribute<int32_t>(iName, iGetter, iSetter);
1075 }
1076
1080 void registerFloatAttribute(std::string const &iName,
1081 typename FloatAttribute<float>::Getter iGetter,
1082 typename FloatAttribute<float>::Setter iSetter)
1083 {
1084 registerAttribute<FloatAttribute<float>>(iName, iGetter, iSetter);
1085 }
1086
1090 void registerDoubleAttribute(std::string const &iName,
1091 typename FloatAttribute<double>::Getter iGetter,
1092 typename FloatAttribute<double>::Setter iSetter)
1093 {
1094 registerAttribute<FloatAttribute<double>>(iName, iGetter, iSetter);
1095 }
1096
1100 void registerBooleanAttribute(std::string const &iName,
1101 typename BooleanAttribute::Getter iGetter,
1102 typename BooleanAttribute::Setter iSetter)
1103 {
1104 registerAttribute<BooleanAttribute>(iName, iGetter, iSetter);
1105 }
1106
1110 CView *create(const UIAttributes &attributes, const IUIDescription *description) const override
1111 {
1112#ifdef JAMBA_DEBUG_LOGGING
1113 DLOG_F(INFO, "CustomViewCreator<%s>::create()", getViewName());
1114#endif
1115
1116 return createCustomView<TView>(CRect(0, 0, 0, 0), attributes, description);
1117 }
1118
1123 bool apply(CView *view, const UIAttributes &attributes, const IUIDescription *description) const override
1124 {
1125 auto *tv = dynamic_cast<TView *>(view);
1126
1127 if(tv == nullptr)
1128 return false;
1129
1130 for(auto attribute : fAttributes)
1131 {
1132 attribute.second->apply(tv, attributes, description);
1133 }
1134
1135 return true;
1136 }
1137
1138 // getAttributeNames
1139 bool getAttributeNames(std::list<std::string> &attributeNames) const override
1140 {
1141 for(auto attribute : fAttributes)
1142 {
1143 attributeNames.emplace_back(attribute.first);
1144 }
1145 return true;
1146 }
1147
1148 // getAttributeType
1149 AttrType getAttributeType(const std::string &attributeName) const override
1150 {
1151 auto iter = fAttributes.find(attributeName);
1152 if(iter != fAttributes.cend())
1153 {
1154 return iter->second->getType();
1155 }
1156 return kUnknownType;
1157 }
1158
1159#ifdef EDITOR_MODE
1163 bool getAttributeValue(CView *iView,
1164 const std::string &iAttributeName,
1165 std::string &oStringValue,
1166 const IUIDescription *iDescription) const override
1167 {
1168 auto *cdv = dynamic_cast<TView *>(iView);
1169
1170 if(cdv == nullptr)
1171 return false;
1172
1173 auto iter = fAttributes.find(iAttributeName);
1174 if(iter != fAttributes.cend())
1175 {
1176 return iter->second->getAttributeValue(cdv, iDescription, oStringValue);
1177 }
1178
1179 return false;
1180 }
1181
1185 bool getPossibleListValues(const std::string &iAttributeName, std::list<const std::string *> &iValues) const override
1186 {
1187 auto iter = fAttributes.find(iAttributeName);
1188 if(iter != fAttributes.cend())
1189 {
1190 return iter->second->getPossibleListValues(iValues);
1191 }
1192 return false;
1193 }
1194#endif
1195
1196private:
1197
1198 // somehow this is required...
1199 template<typename XView>
1201
1205 void registerAttribute(std::shared_ptr<ViewAttribute> iAttribute)
1206 {
1207 // making sure there are no duplicates (cannot use loguru here!)
1208#ifndef NDEBUG
1209 assert(fAttributes.find(iAttribute->getName()) == fAttributes.cend());
1210#endif
1211 fAttributes[iAttribute->getName()] = std::move(iAttribute);
1212 }
1213
1217 template<typename TViewAttribute, typename... Args>
1218 void registerAttribute(std::string const &iName,
1219 typename TViewAttribute::Getter iGetter,
1220 typename TViewAttribute::Setter iSetter,
1221 Args&& ...iArgs)
1222 {
1223 std::shared_ptr<ViewAttribute> cva;
1224 cva.reset(new TViewAttribute(iName, iGetter, iSetter, std::forward<Args>(iArgs)...));
1225 registerAttribute(cva);
1226 }
1227
1228
1229 char const *fViewName;
1230 char const *fDisplayName;
1231 char const *fBaseViewName;
1232
1233 // use a map of shared pointers so that they can easily be copied (see registerAttributes)
1234 std::map<std::string, std::shared_ptr<ViewAttribute>> fAttributes;
1235};
1236
1237namespace impl {
1238 template<typename T>
1239 using creator_ctor_t = decltype(typename T::Creator());
1240
1241 template<typename T>
1243}
1244
1314template<typename TView, typename TBaseView = void>
1316{
1317public:
1318 explicit CustomViewCreator(char const *iViewName = nullptr,
1319 char const *iDisplayName = nullptr,
1320 char const *iBaseViewName = VSTGUI::UIViewCreator::kCView) :
1321 TCustomViewCreator<TView>(iViewName, iDisplayName, iBaseViewName)
1322 {
1324 TCustomViewCreator<TView>::registerAttributes(typename TBaseView::Creator());
1325 }
1326};
1327
1328}
1329
CustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition CustomViewCreator.h:1318
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:566
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, BitmapPtr &oValue) const override
Definition CustomViewCreator.h:572
BitmapAttribute(std::string const &iName, typename ByValAttribute< BitmapPtr >::Getter iGetter, typename ByValAttribute< BitmapPtr >::Setter iSetter)
Definition CustomViewCreator.h:560
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:519
BooleanAttribute(std::string const &iName, typename ByValAttribute< bool >::Getter iGetter, typename ByValAttribute< bool >::Setter iSetter)
Definition CustomViewCreator.h:513
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, bool &oValue) const override
Definition CustomViewCreator.h:525
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, CColor &oValue) const override
Definition CustomViewCreator.h:434
ColorAttribute(std::string const &iName, typename ByRefAttribute< CColor >::Getter iGetter, typename ByRefAttribute< CColor >::Setter iSetter)
Definition CustomViewCreator.h:422
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:428
FloatAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:379
typename ByValAttribute< TFloat >::Setter Setter
Definition CustomViewCreator.h:376
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:383
typename ByValAttribute< TFloat >::Getter Getter
Definition CustomViewCreator.h:375
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TFloat &oValue) const override
Definition CustomViewCreator.h:389
FontAttribute(std::string const &iName, typename ByValAttribute< FontPtr >::Getter iGetter, typename ByValAttribute< FontPtr >::Setter iSetter)
Definition CustomViewCreator.h:603
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:609
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, FontPtr &oValue) const override
Definition CustomViewCreator.h:615
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:469
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, GradientPtr &oValue) const override
Definition CustomViewCreator.h:475
GradientAttribute(std::string const &iName, typename ByValAttribute< GradientPtr >::Getter iGetter, typename ByValAttribute< GradientPtr >::Setter iSetter)
Definition CustomViewCreator.h:463
typename ByValAttribute< TInt >::Setter Setter
Definition CustomViewCreator.h:329
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:336
typename ByValAttribute< TInt >::Getter Getter
Definition CustomViewCreator.h:328
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TInt &oValue) const override
Definition CustomViewCreator.h:342
IntegerAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:332
ListAttribute(std::string const &iName, typename super_type::Getter iGetter, typename super_type::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Definition CustomViewCreator.h:822
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:841
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:835
ByValAttribute< T > super_type
Definition CustomViewCreator.h:818
AttrValMap< T > const fAttributeValuesMap
Definition CustomViewCreator.h:887
MarginAttribute(std::string const &iName, typename ByRefAttribute< Margin >::Getter iGetter, typename ByRefAttribute< Margin >::Setter iSetter)
Definition CustomViewCreator.h:651
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Margin &oValue) const override
Definition CustomViewCreator.h:657
RangeAttribute(std::string const &iName, typename ByRefAttribute< Range >::Getter iGetter, typename ByRefAttribute< Range >::Setter iSetter)
Definition CustomViewCreator.h:713
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, Range &oValue) const override
Definition CustomViewCreator.h:719
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:182
TAttribute(std::string const &iName, Getter iGetter, Setter iSetter)
Definition CustomViewCreator.h:173
TGetter Getter
Definition CustomViewCreator.h:169
virtual bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, T &oValue) const
Definition CustomViewCreator.h:191
TSetter Setter
Definition CustomViewCreator.h:170
bool apply(CView *iView, const UIAttributes &iAttributes, const IUIDescription *iDescription) override
Definition CustomViewCreator.h:197
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, TagID &oValue) const override
Definition CustomViewCreator.h:278
TagAttribute(std::string const &iName, typename ByValAttribute< TagID >::Getter iGetter, typename ByValAttribute< TagID >::Setter iSetter)
Definition CustomViewCreator.h:266
IViewCreator::AttrType getType() override
Definition CustomViewCreator.h:272
bool fromString(IUIDescription const *iDescription, std::string const &iAttributeValue, std::vector< std::string > &oValue) const override
Definition CustomViewCreator.h:777
VectorStringAttribute(std::string const &iName, typename super_type::Getter iGetter, typename super_type::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Definition CustomViewCreator.h:766
ByRefAttribute< std::vector< std::string > > super_type
Definition CustomViewCreator.h:762
TAttribute< T, T const &(CustomViewAdapter::*)() const, void(CustomViewAdapter::*)(T const &)> ByRefAttribute
Definition CustomViewCreator.h:257
void registerColorAttribute(std::string const &iName, typename ColorAttribute::Getter iGetter, typename ColorAttribute::Setter iSetter)
Registers a color attribute with the given name and getter/setter.
Definition CustomViewCreator.h:952
char const * fBaseViewName
Definition CustomViewCreator.h:1231
void registerAttribute(std::string const &iName, typename TViewAttribute::Getter iGetter, typename TViewAttribute::Setter iSetter, Args &&...iArgs)
Generic register attribute.
Definition CustomViewCreator.h:1218
UTF8StringPtr getDisplayName() const override
Definition CustomViewCreator.h:923
CView * create(const UIAttributes &attributes, const IUIDescription *description) const override
This is the factory method which will instantiate the view.
Definition CustomViewCreator.h:1110
void registerFloatAttribute(std::string const &iName, typename FloatAttribute< float >::Getter iGetter, typename FloatAttribute< float >::Setter iSetter)
Registers a float attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1080
void registerVectorStringAttribute(std::string const &iName, typename VectorStringAttribute::Getter iGetter, typename VectorStringAttribute::Setter iSetter, char iDelimiter=',', bool iSkipEmptyEntries=false)
Registers a Range attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1012
~TCustomViewCreator() override
Definition CustomViewCreator.h:909
IdStringPtr getViewName() const override
Definition CustomViewCreator.h:917
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:938
void registerIntAttribute(std::string const &iName, typename IntegerAttribute< int32_t >::Getter iGetter, typename IntegerAttribute< int32_t >::Setter iSetter)
Registers an int attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1070
friend class TCustomViewCreator
Definition CustomViewCreator.h:1200
void registerBitmapAttribute(std::string const &iName, typename BitmapAttribute::Getter iGetter, typename BitmapAttribute::Setter iSetter)
Registers a bitmap attribute with the given name and getter/setter.
Definition CustomViewCreator.h:972
bool getAttributeNames(std::list< std::string > &attributeNames) const override
Definition CustomViewCreator.h:1139
void registerIntegerAttribute(std::string const &iName, typename IntegerAttribute< TInt >::Getter iGetter, typename IntegerAttribute< TInt >::Setter iSetter)
Registers an Integer (any kind) attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1060
IdStringPtr getBaseViewName() const override
Definition CustomViewCreator.h:929
AttrType getAttributeType(const std::string &attributeName) const override
Definition CustomViewCreator.h:1149
void registerFontAttribute(std::string const &iName, typename FontAttribute::Getter iGetter, typename FontAttribute::Setter iSetter)
Registers a font attribute with the given name and getter/setter.
Definition CustomViewCreator.h:982
void registerAttribute(std::shared_ptr< ViewAttribute > iAttribute)
Internal method to register an attribute... check that names are not duplicate!
Definition CustomViewCreator.h:1205
void registerGradientAttribute(std::string const &iName, typename GradientAttribute::Getter iGetter, typename GradientAttribute::Setter iSetter)
Registers a gradient attribute with the given name and getter/setter.
Definition CustomViewCreator.h:962
TAttribute< T, T(CustomViewAdapter::*)() const, void(CustomViewAdapter::*)(T)> ByValAttribute
Definition CustomViewCreator.h:251
std::map< std::string, std::shared_ptr< ViewAttribute > > fAttributes
Definition CustomViewCreator.h:1234
void registerRangeAttribute(std::string const &iName, typename RangeAttribute::Getter iGetter, typename RangeAttribute::Setter iSetter)
Registers a Range attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1002
void registerBooleanAttribute(std::string const &iName, typename BooleanAttribute::Getter iGetter, typename BooleanAttribute::Setter iSetter)
Registers a boolean attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1100
char const * fViewName
Definition CustomViewCreator.h:1229
void registerTagAttribute(std::string const &iName, typename TagAttribute::Getter iGetter, typename TagAttribute::Setter iSetter)
Registers a tag attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1049
char const * fDisplayName
Definition CustomViewCreator.h:1230
TCustomViewCreator(char const *iViewName=nullptr, char const *iDisplayName=nullptr, char const *iBaseViewName=VSTGUI::UIViewCreator::kCView)
Definition CustomViewCreator.h:895
void registerListAttribute(std::string const &iName, typename ListAttribute< T >::Getter iGetter, typename ListAttribute< T >::Setter iSetter, AttrValInitList< T > const &iAttributeValues)
Registers a list attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1038
void registerDoubleAttribute(std::string const &iName, typename FloatAttribute< double >::Getter iGetter, typename FloatAttribute< double >::Setter iSetter)
Registers a double attribute with the given name and getter/setter.
Definition CustomViewCreator.h:1090
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:1123
void registerMarginAttribute(std::string const &iName, typename MarginAttribute::Getter iGetter, typename MarginAttribute::Setter iSetter)
Registers a Margin attribute with the given name and getter/setter.
Definition CustomViewCreator.h:992
Base abstract class for an attribute of a view.
Definition CustomViewCreator.h:53
std::string fName
Definition CustomViewCreator.h:101
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:69
virtual IViewCreator::AttrType getType()=0
ViewAttribute(std::string iName)
Definition CustomViewCreator.h:56
constexpr bool is_detected_v
Definition Cpp17.h:63
bool stringToFloat(const std::string &iString, TFloat &oValue)
Converts the string to a TFloat (float or double).
Definition StringUtils.h:71
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< TFloat > splitFloats(const std::string &iString, char iDelimiter, bool iSkipEmptyEntries=false)
Converts the string to a an array of floats.
Definition StringUtils.h:104
Definition CustomViewCreator.h:1237
decltype(typename T::Creator()) creator_ctor_t
Definition CustomViewCreator.h:1239
constexpr auto is_creator_ctor_detected
Definition CustomViewCreator.h:1242
Definition CustomController.h:25
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:122
std::map< std::string, T > AttrValMap
Defines a map of string to attribute value.
Definition CustomViewCreator.h:116
TView * createCustomView(CRect const &iSize, const UIAttributes &iAttributes, const IUIDescription *)
Factory method which creates the actual view.
Definition CustomViewCreator.h:109
CGradient * GradientPtr
Definition Types.h:58
CFontDesc * FontPtr
Definition Types.h:54
CBitmap * BitmapPtr
Definition Types.h:50
Utils::Range< CCoord > Range
Defines a Range.
Definition Types.h:64
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:341
T fTo
Definition Lerp.h:342
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