Browse Source

Updated the GUI example to demonstrate ListBox.

David Piuva 5 years ago
parent
commit
7c899389e7
2 changed files with 49 additions and 76 deletions
  1. 29 8
      Source/SDK/guiExample/main.cpp
  2. 20 68
      Source/SDK/guiExample/media/interface.lof

+ 29 - 8
Source/SDK/guiExample/main.cpp

@@ -7,12 +7,17 @@ using namespace dsr;
 const String mediaPath = string_combine(U"media", file_separator());
 bool running = true;
 
-// The window handle
+// GUI handles
 Window window;
+Component buttonClear;
+Component buttonAdd;
+Component myListBox;
 
 int main(int argn, char **argv) {
 	// Create a window
 	window = window_create(U"GUI example", 1000, 700);
+	// Register your custom components here
+	//REGISTER_PERSISTENT_CLASS(className);
 	// Load an interface to the window
 	window_loadInterfaceFromFile(window, mediaPath + U"interface.lof");
 
@@ -22,15 +27,32 @@ int main(int argn, char **argv) {
 	});
 
 	// Look up components by name
-	Component buttonA = window_findComponentByName(window, U"buttonA");
-	Component buttonB = window_findComponentByName(window, U"buttonB");
+	buttonClear = window_findComponentByName(window, U"buttonClear");
+	buttonAdd = window_findComponentByName(window, U"buttonAdd");
+	myListBox = window_findComponentByName(window, U"myListBox");
 
 	// Connect components with actions
-	component_setPressedEvent(buttonA, []() {
-		printText("Pressed buttonA!\n");
+	component_setPressedEvent(buttonClear, []() {
+		// Clear list
+		component_call(myListBox, U"ClearAll");
 	});
-	component_setPressedEvent(buttonB, []() {
-		printText("Pressed buttonB!\n");
+	component_setPressedEvent(buttonAdd, []() {
+		// Add to list
+		component_call(myListBox, U"PushElement", U"New item");
+	});
+	component_setKeyDownEvent(myListBox, [](const KeyboardEvent& event) {
+		if (event.dsrKey == DsrKey_Delete) {
+			// Delete from list
+			int64_t index = string_parseInteger(component_call(myListBox, U"GetSelectedIndex"));
+			if (index > -1) {
+				component_call(myListBox, U"RemoveElement", string_combine(index));
+			}
+		}
+	});
+	component_setPressedEvent(myListBox, []() {
+		int64_t index = string_parseInteger(component_call(myListBox, U"GetSelectedIndex"));
+		String content = component_call(myListBox, U"GetSelectedText");
+		printText("content is (", content, ") at index ", index, "\n");
 	});
 
 	// Execute
@@ -47,4 +69,3 @@ int main(int argn, char **argv) {
 		window_showCanvas(window);
 	}
 }
-

+ 20 - 68
Source/SDK/guiExample/media/interface.lof

@@ -3,87 +3,39 @@ Begin : Panel
 	Solid = 0
 	Begin : Panel
 		Name = "menuBar"
-		Bottom = +55
+		Bottom = +50
 		Solid = 1
-		Color = 200,200,200
+		Color = 180,180,180
 		Begin : Button
-			Name = "menuButtonNew"
+			Name = "buttonClear"
 			Left = +5
 			Top = +5
-			Right = +45
+			Right = +95
 			Bottom = +45
-			Color = 150,150,150
-			Text = "New"
+			Color = 160,150,150
+			Text = "Clear"
 		End
 		Begin : Button
-			Name = "menuButtonLoad"
-			Left = +50
+			Name = "buttonAdd"
+			Left = +105
 			Top = +5
-			Right = +90
+			Right = +195
 			Bottom = +45
-			Color = 150,150,150
-			Text = "Load"
-		End
-		Begin : Button
-			Name = "menuButtonSave"
-			Left = +95
-			Top = +5
-			Right = +135
-			Bottom = +45
-			Color = 150,150,150
-			Text = "Save"
+			Color = 150,160,150
+			Text = "Add"
 		End
 	End
 	Begin : Panel
 		Name = "workspace"
-		Top = +55
-		Solid = 0
-		Begin : Panel
-			Name = "toolPanel"
-			Right = +150
-			Solid = 1
-			Color = 150,150,150
-			Begin : Button
-				Name = "buttonA"
-				Left = +10
-				Top = +10
-				Right = 100%-10
-				Bottom = +50
-				Color = 200,200,200
-				Text = "A"
-			End
-			Begin : Button
-				Name = "buttonB"
-				Left = +10
-				Top = +60
-				Right = 100%-10
-				Bottom = +100
-				Color = 200,200,200
-				Text = "B"
-			End
-		End
-		Begin : Panel
-			Name = "designBorder"
-			Left = +150
-			Right = 100%-150
-			Solid = 1
-			Color = 50,50,50
-			Begin : Panel
-				Name = "designWindow"
-				Left = +10
-				Top = +10
-				Right = 100%-10
-				Bottom = 100%-10
-				Solid = 1
-				Color = 150,150,150
-			End
-		End
-		Begin : Panel
-			Name = "propertyPanel"
-			Left = 100%-150
-			Solid = 1
-			Color = 150,150,150
+		Top = +50
+		Solid = 1
+		Color = 100,100,100
+		Begin : ListBox
+			Name = "myListBox"
+			right = 10%+100
+			Color = 180,180,180
+			List = "Testing", "1", "2", "3", "One Two Three"
+			SelectedIndex = 2
 		End
 	End
 End
-