Browse Source

Added a setting to disable scrolling in a ListBox. Useful for fixed options.

David Piuva 5 years ago
parent
commit
c33c167b0c
2 changed files with 21 additions and 18 deletions
  1. 20 18
      Source/DFPSR/gui/components/ListBox.cpp
  2. 1 0
      Source/DFPSR/gui/components/ListBox.h

+ 20 - 18
Source/DFPSR/gui/components/ListBox.cpp

@@ -33,6 +33,7 @@ void ListBox::declareAttributes(StructureDefinition &target) const {
 	target.declareAttribute(U"Color");
 	target.declareAttribute(U"Color");
 	target.declareAttribute(U"List");
 	target.declareAttribute(U"List");
 	target.declareAttribute(U"SelectedIndex");
 	target.declareAttribute(U"SelectedIndex");
+	target.declareAttribute(U"LockScrolling");
 }
 }
 
 
 Persistent* ListBox::findAttribute(const ReadableString &name) {
 Persistent* ListBox::findAttribute(const ReadableString &name) {
@@ -42,6 +43,8 @@ Persistent* ListBox::findAttribute(const ReadableString &name) {
 		return &(this->list);
 		return &(this->list);
 	} else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
 	} else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
 		return &(this->selectedIndex);
 		return &(this->selectedIndex);
+	} else if (string_caseInsensitiveMatch(name, U"LockScrolling")) {
+		return &(this->lockScrolling);
 	} else {
 	} else {
 		return VisualComponent::findAttribute(name);
 		return VisualComponent::findAttribute(name);
 	}
 	}
@@ -111,15 +114,10 @@ void ListBox::receiveMouseEvent(const MouseEvent& event) {
 	} else if (event.mouseEventType == MouseEventType::Scroll) {
 	} else if (event.mouseEventType == MouseEventType::Scroll) {
 		if (event.key == MouseKeyEnum::ScrollUp) {
 		if (event.key == MouseKeyEnum::ScrollUp) {
 			this->firstVisible--;
 			this->firstVisible--;
-			if (this->firstVisible < 0) {
-				this->firstVisible = 0;
-			}
 		} else if (event.key == MouseKeyEnum::ScrollDown) {
 		} else if (event.key == MouseKeyEnum::ScrollDown) {
 			this->firstVisible++;
 			this->firstVisible++;
-			if (this->firstVisible > maxIndex) {
-				this->firstVisible = maxIndex;
-			}
 		}
 		}
+		this->limitScrolling();
 		this->hasImages = false; // Force redraw
 		this->hasImages = false; // Force redraw
 	}
 	}
 	VisualComponent::receiveMouseEvent(event);
 	VisualComponent::receiveMouseEvent(event);
@@ -181,18 +179,22 @@ void ListBox::limitSelection() {
 // Optional limit of scrolling, to be applied when the user don't explicitly scroll away from the selection
 // Optional limit of scrolling, to be applied when the user don't explicitly scroll away from the selection
 // limitSelection should be called before limitScrolling, because scrolling limits depend on selection
 // limitSelection should be called before limitScrolling, because scrolling limits depend on selection
 void ListBox::limitScrolling() {
 void ListBox::limitScrolling() {
-	// Try to load the font before estimating how big the view is
-	this->completeAssets();
-	if (this->font.get() == nullptr) {
-		throwError("Cannot get the font size because ListBox failed to get a font!\n");
-	}
-	int visibleRange = this->location.height() / (this->font->size);
-	int maxScroll = this->selectedIndex.value;
-	int minScroll = maxScroll + 1 - visibleRange;
-	if (this->firstVisible < minScroll) {
-		this->firstVisible = minScroll;
-	} else if (this->firstVisible > maxScroll) {
-		this->firstVisible = maxScroll;
+	if (this->lockScrolling.value) {
+		this->firstVisible = 0;
+	} else {
+		// Try to load the font before estimating how big the view is
+		this->completeAssets();
+		if (this->font.get() == nullptr) {
+			throwError("Cannot get the font size because ListBox failed to get a font!\n");
+		}
+		int visibleRange = this->location.height() / (this->font->size);
+		int maxScroll = this->selectedIndex.value;
+		int minScroll = maxScroll + 1 - visibleRange;
+		if (this->firstVisible < minScroll) {
+			this->firstVisible = minScroll;
+		} else if (this->firstVisible > maxScroll) {
+			this->firstVisible = maxScroll;
+		}
 	}
 	}
 }
 }
 
 

+ 1 - 0
Source/DFPSR/gui/components/ListBox.h

@@ -36,6 +36,7 @@ public:
 	PersistentColor color;
 	PersistentColor color;
 	PersistentStringList list;
 	PersistentStringList list;
 	PersistentInteger selectedIndex; // Should always be inside of the list's 0..length-1 bound or zero.
 	PersistentInteger selectedIndex; // Should always be inside of the list's 0..length-1 bound or zero.
+	PersistentBoolean lockScrolling; // When true, the first item will always be visible at the top.
 	void declareAttributes(StructureDefinition &target) const override;
 	void declareAttributes(StructureDefinition &target) const override;
 	Persistent* findAttribute(const ReadableString &name) override;
 	Persistent* findAttribute(const ReadableString &name) override;
 private:
 private: