Browse Source

Changed Hidden to visible and added integer access to properties.

David Piuva 5 years ago
parent
commit
19dfdca769

+ 53 - 5
Source/DFPSR/api/guiAPI.cpp

@@ -206,7 +206,7 @@ ReturnCode dsr::component_setProperty(const Component& component, const Readable
 	Persistent* target = component->findAttribute(propertyName);
 	if (target == nullptr) {
 		if (mustAssign) {
-			throwError("component_setProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
+			throwError("component_setProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
 		}
 		return ReturnCode::KeyNotFound;
 	} else {
@@ -215,7 +215,7 @@ ReturnCode dsr::component_setProperty(const Component& component, const Readable
 			return ReturnCode::Good;
 		} else {
 			if (mustAssign) {
-				throwError("component_setProperty_string: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
+				throwError("component_setProperty: The input ", value, " could not be assigned to property ", propertyName, " because of incorrect format.\n");
 			}
 			return ReturnCode::ParsingFailure;
 		}
@@ -226,7 +226,7 @@ String dsr::component_getProperty(const Component& component, const ReadableStri
 	Persistent* target = component->findAttribute(propertyName);
 	if (target == nullptr) {
 		if (mustExist) {
-			throwError("component_getProperty_string: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
+			throwError("component_getProperty: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
 		}
 		return U"";
 	} else {
@@ -234,7 +234,7 @@ String dsr::component_getProperty(const Component& component, const ReadableStri
 	}
 }
 ReturnCode dsr::component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign) {
-	MUST_EXIST(component, component_setProperty);
+	MUST_EXIST(component, component_setProperty_string);
 	Persistent* target = component->findAttribute(propertyName);
 	PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
 	if (target == nullptr) {
@@ -253,7 +253,7 @@ ReturnCode dsr::component_setProperty_string(const Component& component, const R
 	}
 }
 String dsr::component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist) {
-	MUST_EXIST(component, component_getProperty);
+	MUST_EXIST(component, component_getProperty_string);
 	Persistent* target = component->findAttribute(propertyName);
 	PersistentString* stringTarget = dynamic_cast<PersistentString*>(target);
 	if (target == nullptr) {
@@ -270,6 +270,54 @@ String dsr::component_getProperty_string(const Component& component, const Reada
 		return stringTarget->value;
 	}
 }
+ReturnCode dsr::component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign) {
+	MUST_EXIST(component, component_setProperty_integer);
+	Persistent* target = component->findAttribute(propertyName);
+	if (target == nullptr) {
+		if (mustAssign) {
+			throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
+		}
+		return ReturnCode::KeyNotFound;
+	} else {
+		PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
+		PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
+		if (integerTarget != nullptr) {
+			integerTarget->value = value;
+			return ReturnCode::Good;
+		} else if (booleanTarget != nullptr) {
+			booleanTarget->value = value ? 1 : 0;
+			return ReturnCode::Good;
+		} else {
+			if (mustAssign) {
+				throwError("component_setProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
+			}
+			return ReturnCode::KeyNotFound;
+		}
+	}
+}
+int64_t dsr::component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist, int64_t defaultValue) {
+	MUST_EXIST(component, component_getProperty_integer);
+	Persistent* target = component->findAttribute(propertyName);
+	if (target == nullptr) {
+		if (mustExist) {
+			throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " could not be found.\n");
+		}
+		return defaultValue;
+	} else {
+		PersistentInteger* integerTarget = dynamic_cast<PersistentInteger*>(target);
+		PersistentBoolean* booleanTarget = dynamic_cast<PersistentBoolean*>(target);
+		if (integerTarget != nullptr) {
+			return integerTarget->value;
+		} else if (booleanTarget != nullptr) {
+			return booleanTarget->value;
+		} else {
+			if (mustExist) {
+				throwError("component_getProperty_integer: ", propertyName, " in ", component->getClassName(), " was a ", target->getClassName(), " instead of an integer or boolean.\n");
+			}
+			return defaultValue;
+		}
+	}
+}
 
 String dsr::component_call(const Component& component, const ReadableString& methodName, const ReadableString& arguments) {
 	MUST_EXIST(component, component_call);

+ 13 - 2
Source/DFPSR/api/guiAPI.h

@@ -171,6 +171,14 @@ namespace dsr {
 	//   Unless mustAssign forces an exception.
 	//     Returns ReturnCode::ParsingFailure if propertyName was found but value couldn't be converted to its type.
 	ReturnCode component_setProperty(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
+	// A version for setting integers and booleans.
+	//   For integers:
+	//     Just set value to whatever you want assigned directly.
+	//     This is faster than using component_setProperty by not parsing the value from any string.
+	//   For booleans:
+	//     Use 1 (or another non-zero value) for true and 0 for false.
+	//     Boolean properties will convery any non-zero values into ones.
+	ReturnCode component_setProperty_integer(const Component& component, const ReadableString& propertyName, int64_t value, bool mustAssign = true);
 	// A version optimized for basic strings to bypass quote mangling
 	//   The new value is given as it is without unmangling.
 	ReturnCode component_setProperty_string(const Component& component, const ReadableString& propertyName, const ReadableString& value, bool mustAssign = true);
@@ -182,8 +190,11 @@ namespace dsr {
 	//   If mustExist is false
 	//     Returns an empty string when propertyName isn't found.
 	String component_getProperty(const Component& component, const ReadableString& propertyName, bool mustExist = true);
-	// A version optimized for basic strings to bypass quote mangling
-	//   Returns the result without adding any quote signs or escape characters
+	// A version for setting integers and booleans.
+	//   Returns defaultValue on failure, which should use a value that is never actually used.
+	int64_t component_getProperty_integer(const Component& component, const ReadableString& propertyName, bool mustExist = true, int64_t defaultValue = 0);
+	// A version optimized for basic strings to bypass quote mangling.
+	//   Returns the result without adding any quote signs or escape characters.
 	String component_getProperty_string(const Component& component, const ReadableString& propertyName, bool mustExist = true);
 
 	// Call a named method in the component using optional text arguments

+ 8 - 8
Source/DFPSR/gui/VisualComponent.cpp

@@ -58,12 +58,12 @@ FlexRegion VisualComponent::getRegion() const {
 	return this->region;
 }
 
-void VisualComponent::setHidden(bool hidden) {
-	this->hidden.value = hidden;
+void VisualComponent::setVisible(bool visible) {
+	this->visible.value = visible;
 }
 
-bool VisualComponent::getHidden() const {
-	return this->hidden.value;
+bool VisualComponent::getVisible() const {
+	return this->visible.value;
 }
 
 void VisualComponent::setName(const String& newName) {
@@ -104,7 +104,7 @@ void VisualComponent::updateLocationEvent(const IRect& oldLocation, const IRect&
 
 // Offset may become non-zero when the origin is outside of targetImage from being clipped outside of the parent region
 void VisualComponent::draw(ImageRgbaU8& targetImage, const IVector2D& offset) {
-	if (!this->getHidden()) {
+	if (this->getVisible()) {
 		IRect containerBound = this->getLocation() + offset;
 		this->drawSelf(targetImage, containerBound);
 		// Draw each child component
@@ -247,7 +247,7 @@ std::shared_ptr<VisualComponent> VisualComponent::getDirectChild(const IVector2D
 	for (int i = this->getChildCount() - 1; i >= 0; i--) {
 		std::shared_ptr<VisualComponent> currentChild = this->children[i];
 		// Check if the point is inside the child component
-		if ((!currentChild->getHidden() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
+		if ((currentChild->getVisible() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
 			return currentChild;
 		}
 	}
@@ -261,7 +261,7 @@ std::shared_ptr<VisualComponent> VisualComponent::getTopChild(const IVector2D& p
 	for (int i = this->getChildCount() - 1; i >= 0; i--) {
 		std::shared_ptr<VisualComponent> currentChild = this->children[i];
 		// Check if the point is inside the child component
-		if ((!currentChild->getHidden() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
+		if ((currentChild->getVisible() || includeInvisible) && currentChild->pointIsInside(pixelPosition)) {
 			// Check if a component inside the child component is even higher up
 			std::shared_ptr<VisualComponent> subChild = currentChild->getTopChild(pixelPosition - this->getLocation().upperLeft(), includeInvisible);
 			if (subChild.get() != nullptr) {
@@ -287,7 +287,7 @@ void VisualComponent::sendMouseEvent(const MouseEvent& event) {
 	if (this->holdCount > 0) {
 		// If we're grabbing a component, keep sending events to it
 		childComponent = this->dragComponent;
-	} else if (!this->getHidden() && this->pointIsInside(event.position)) {
+	} else if (this->getVisible() && this->pointIsInside(event.position)) {
 		// If we're not grabbing a component, see if we can send the action to another component
 		childComponent = this->getDirectChild(localEvent.position, false);
 	}

+ 6 - 6
Source/DFPSR/gui/VisualComponent.h

@@ -51,11 +51,11 @@ protected:
 	FlexRegion region;
 	PersistentString name;
 	PersistentInteger index;
-	PersistentBoolean hidden = PersistentBoolean(false);
+	PersistentBoolean visible = PersistentBoolean(true);
 	void declareAttributes(StructureDefinition &target) const override {
 		target.declareAttribute(U"Name");
 		target.declareAttribute(U"Index");
-		target.declareAttribute(U"Hidden");
+		target.declareAttribute(U"Visible");
 		target.declareAttribute(U"Left");
 		target.declareAttribute(U"Top");
 		target.declareAttribute(U"Right");
@@ -67,8 +67,8 @@ public:
 			return &(this->name);
 		} else if (string_caseInsensitiveMatch(name, U"Index")) {
 			return &(this->index);
-		} else if (string_caseInsensitiveMatch(name, U"Hidden")) {
-			return &(this->hidden);
+		} else if (string_caseInsensitiveMatch(name, U"Visible")) {
+			return &(this->visible);
 		} else if (string_caseInsensitiveMatch(name, U"Left")) {
 			return &(this->region.sides[0]);
 		} else if (string_caseInsensitiveMatch(name, U"Top")) {
@@ -101,8 +101,8 @@ public:
 	IVector2D getSize() const;
 	void setRegion(const FlexRegion &newRegion);
 	FlexRegion getRegion() const;
-	void setHidden(bool hidden);
-	bool getHidden() const;
+	void setVisible(bool visible);
+	bool getVisible() const;
 	void setName(const String& newName);
 	String getName() const;
 	void setIndex(int index);