Browse Source

Made GUI API calls for creating an interface from text in a parent component.

David Piuva 2 years ago
parent
commit
db857298db
2 changed files with 29 additions and 0 deletions
  1. 19 0
      Source/DFPSR/api/guiAPI.cpp
  2. 10 0
      Source/DFPSR/api/guiAPI.h

+ 19 - 0
Source/DFPSR/api/guiAPI.cpp

@@ -84,6 +84,25 @@ Component dsr::window_getRoot(const Window& window) {
 	return window->getRootComponent();
 }
 
+Component dsr::component_createWithInterfaceFromString(Component& parent, const String& content, const ReadableString &fromPath) {
+	MUST_EXIST(parent, component_createWithInterfaceFromString);
+	Component result = std::dynamic_pointer_cast<VisualComponent>(createPersistentClassFromText(content, fromPath));
+	if (result.get() == nullptr) {
+		throwError(U"component_createWithInterfaceFromString: The component could not be created!\n\nLayout:\n", content, "\n");
+	}
+	parent->addChildComponent(result);
+	result->applyLayout(parent->getSize());
+	return result;
+}
+
+Component dsr::component_createWithInterfaceFromString(Component& parent, const String& content) {
+	return component_createWithInterfaceFromString(parent, content, file_getCurrentPath());
+}
+
+Component dsr::component_createWithInterfaceFromFile(Component& parent, const String& filename) {
+	return component_createWithInterfaceFromString(parent, string_load(filename), file_getRelativeParentFolder(filename));
+}
+
 Component dsr::component_findChildByName(const Component& parent, const ReadableString& name, bool mustExist) {
 	MUST_EXIST(parent, component_findChildByName);
 	return parent->findChildByName(name);

+ 10 - 0
Source/DFPSR/api/guiAPI.h

@@ -210,6 +210,16 @@ namespace dsr {
 	// identifierName is used to find the component.
 	// index can be used to identify multiple components with the same name without having to generate numbered names.
 	Component component_create(const Component& parent, const ReadableString& className, const ReadableString& identifierName, int index = 0);
+	// An alternative to window_loadInterfaceFromString when you want the interface loaded into a panel instead of the whole window.
+	// Useful for dynamically creating and destroying interfaces to save memory and load the application faster.
+	// Loading an interface by parsing a layout file's content, with any external resources loaded relative to fromPath.
+	//   Embedded images do not count as external resources, but file paths need fromPath in order to know from where they will be loaded.
+	// Raises an exception if parent doesn't exist.
+	Component component_createWithInterfaceFromString(Component& parent, const String& content, const ReadableString &fromPath);
+	// With fromPath implicitly being the current path.
+	Component component_createWithInterfaceFromString(Component& parent, const String& content);
+	// Loading the interface from a file and loading resources relative to filename's parent folder.
+	Component component_createWithInterfaceFromFile(Component& parent, const String& filename);
 	// Returns true iff the component exists.
 	bool component_exists(const Component& component);
 	// Removed the component from the parent.