From 3fce1f0228b11a5cb828402f57230aa6f94e45c1 Mon Sep 17 00:00:00 2001
From: Frantisek Simon <frantisek.simon@nic.cz>
Date: Thu, 5 May 2016 11:16:47 +0200
Subject: [PATCH] #337 dialogs always shows on openGL main thread

---
 .../screen/createuser/PanoramaScreen.java     | 60 +++++++++--------
 .../ui/dialog/TablexiaComponentDialog.java    | 65 +++++++++++++------
 2 files changed, 78 insertions(+), 47 deletions(-)

diff --git a/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java b/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
index 2b7fff6b4..28c808655 100644
--- a/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
+++ b/core/src/cz/nic/tablexia/screen/createuser/PanoramaScreen.java
@@ -425,15 +425,8 @@ public class PanoramaScreen extends AbstractTablexiaScreen<int[][]> {
                     return;
                 }
 
-                showNewsDetailDialog(dialogIndex);
 
-                text = getText("newuseranim_topic_" + (dialogIndex + 1));
-
-				if (dialogIndex == 4) {
-					showContinueTextDialog(text);
-				} else {
-					showTextDialog(text);
-				}
+				showNewsDetailDialog(dialogIndex);
 
 				playMusic(music);
 			}
@@ -482,28 +475,39 @@ public class PanoramaScreen extends AbstractTablexiaScreen<int[][]> {
                 components.toArray(new TablexiaDialogComponentAdapter[]{})
         );
 
-        int origNewsDetailWidth  = getScreenTextureRegion(GFX_PATH + NEWSPAPER_DETAIL + (dialogIndex + 1)).getRegionWidth();
-        int origNewsDetailHeight = getScreenTextureRegion(GFX_PATH + NEWSPAPER_DETAIL + (dialogIndex + 1)).getRegionHeight();
-
-        this.newsDetailDialog.show(
-            origNewsDetailWidth,
-            origNewsDetailHeight
-        );
+		final int origNewsDetailWidth  = getScreenTextureRegion(GFX_PATH + NEWSPAPER_DETAIL + (dialogIndex + 1)).getRegionWidth();
+		final int origNewsDetailHeight = getScreenTextureRegion(GFX_PATH + NEWSPAPER_DETAIL + (dialogIndex + 1)).getRegionHeight();
 
-        if(newsDetailDialog.getOutterHeight() > getSceneInnerHeight() * NEWS_DIALOG_MAX_HEIGHT) {
-            int currNewsDetailHeight;
-            int currNewsDetailWidth;
-
-            currNewsDetailHeight = (int) (getSceneInnerHeight() * NEWS_DIALOG_MAX_HEIGHT);
-            currNewsDetailWidth = (int) (currNewsDetailHeight * ((float)(origNewsDetailWidth) / origNewsDetailHeight));
-
-            if(currNewsDetailWidth > getSceneWidth() * NEWS_DIALOG_MAX_WIDTH) {
-                currNewsDetailWidth = (int) (getSceneWidth() * NEWS_DIALOG_MAX_WIDTH);
-                currNewsDetailHeight = (int) (currNewsDetailWidth * ((float)(origNewsDetailHeight) / origNewsDetailWidth));
-            }
+		this.newsDetailDialog.show(
+				origNewsDetailWidth,
+				origNewsDetailHeight
+		);
 
-            this.newsDetailDialog.setSize(currNewsDetailWidth, currNewsDetailHeight);
-        }
+		this.newsDetailDialog.setComponentsSizeComputedCallback(new Runnable() {
+			@Override
+			public void run() {
+				if (newsDetailDialog.getOutterHeight() > getSceneInnerHeight() * NEWS_DIALOG_MAX_HEIGHT) {
+					int currNewsDetailHeight;
+					int currNewsDetailWidth;
+
+					currNewsDetailHeight = (int) (getSceneInnerHeight() * NEWS_DIALOG_MAX_HEIGHT);
+					currNewsDetailWidth = (int) (currNewsDetailHeight * ((float)(origNewsDetailWidth) / origNewsDetailHeight));
+
+					if(currNewsDetailWidth > getSceneWidth() * NEWS_DIALOG_MAX_WIDTH) {
+						currNewsDetailWidth = (int) (getSceneWidth() * NEWS_DIALOG_MAX_WIDTH);
+						currNewsDetailHeight = (int) (currNewsDetailWidth * ((float)(origNewsDetailHeight) / origNewsDetailWidth));
+					}
+
+					newsDetailDialog.setSize(currNewsDetailWidth, currNewsDetailHeight);
+
+					if (dialogIndex == 4) {
+						showContinueTextDialog(getText("newuseranim_topic_" + (dialogIndex + 1)));
+					} else {
+						showTextDialog(getText("newuseranim_topic_" + (dialogIndex + 1)));
+					}
+				}
+			}
+		});
     }
 
 	private void moveToStreet() {
diff --git a/core/src/cz/nic/tablexia/util/ui/dialog/TablexiaComponentDialog.java b/core/src/cz/nic/tablexia/util/ui/dialog/TablexiaComponentDialog.java
index 3dc343f91..d984db467 100644
--- a/core/src/cz/nic/tablexia/util/ui/dialog/TablexiaComponentDialog.java
+++ b/core/src/cz/nic/tablexia/util/ui/dialog/TablexiaComponentDialog.java
@@ -84,6 +84,10 @@ public class TablexiaComponentDialog extends Stack {
     private float               originalWidth;
     private float               originalHeight;
 
+	//Called when size for every component has been computed (happens after first draw)
+	private Runnable            componentsSizeComputedCallback;
+	private boolean             componentsSizeComputed = false;
+
     private TablexiaDialogType dialogType;
 
     protected Group               backgroundLayer;
@@ -93,6 +97,7 @@ public class TablexiaComponentDialog extends Stack {
 
     protected Image               contentBackgroundImage;
     protected NinePatch           contentBackgroundPatch;
+	private boolean				  componentsInitiated = false;
 
     protected List<TablexiaDialogComponentAdapter>    dialogComponents;
 
@@ -121,8 +126,6 @@ public class TablexiaComponentDialog extends Stack {
 
         add(contentBackgroundLayer);
         add(contentLayer);
-
-        init();
     }
 
     public NinePatch getContentBackgroundPatch() {
@@ -170,27 +173,39 @@ public class TablexiaComponentDialog extends Stack {
 		show(width, height, true);
 	}
 
-    public void show(float width, float height, boolean withFadeAnim) {
-        setSize(width, height);
+    public void show(final float width, final float height, final boolean withFadeAnim) {
 
-        if (withFadeAnim) {
-			addAction(Actions.alpha(0f));
-			backgroundLayer.addAction(Actions.alpha(0f));
-		}
+		Gdx.app.postRunnable(new Runnable() {
+			@Override
+			public void run() {
 
-        getStage().addActor(backgroundLayer);
-        getStage().addActor(this);
-        for (TablexiaDialogComponentAdapter tablexiaDialogComponentAdapter : dialogComponents) {
-            tablexiaDialogComponentAdapter.show();
-        }
-        registerInputListener();
+				if (!componentsInitiated) {
+					componentsInitiated = true;
+					init();
+				}
 
-        ApplicationBus.getInstance().post(new DialogVisibleEvent(true)).asynchronously();
+				setSize(width, height);
 
-		if (withFadeAnim) {
-			addAction(Actions.fadeIn(FADE_IN_DURATION, FADE_IN_INTERPOLATION));
-			backgroundLayer.addAction(Actions.fadeIn(FADE_IN_DURATION, FADE_IN_INTERPOLATION));
-		}
+				if (withFadeAnim) {
+					addAction(Actions.alpha(0f));
+					backgroundLayer.addAction(Actions.alpha(0f));
+				}
+
+				getStage().addActor(backgroundLayer);
+				getStage().addActor(TablexiaComponentDialog.this);
+				for (TablexiaDialogComponentAdapter tablexiaDialogComponentAdapter : dialogComponents) {
+					tablexiaDialogComponentAdapter.show();
+				}
+				registerInputListener();
+
+				ApplicationBus.getInstance().post(new DialogVisibleEvent(true)).asynchronously();
+
+				if (withFadeAnim) {
+					addAction(Actions.fadeIn(FADE_IN_DURATION, FADE_IN_INTERPOLATION));
+					backgroundLayer.addAction(Actions.fadeIn(FADE_IN_DURATION, FADE_IN_INTERPOLATION));
+				}
+			}
+		});
     }
 
     public void hide() {
@@ -241,6 +256,11 @@ public class TablexiaComponentDialog extends Stack {
         for (TablexiaDialogComponentAdapter tablexiaDialogComponentAdapter : dialogComponents) {
             tablexiaDialogComponentAdapter.afterDraw();
         }
+
+		if (!componentsSizeComputed && componentsSizeComputedCallback != null) {
+			componentsSizeComputed = true;
+			componentsSizeComputedCallback.run();
+		}
     }
 
     public void processSizeThresholdChanged() {
@@ -439,6 +459,13 @@ public class TablexiaComponentDialog extends Stack {
         return contentBackgroundPatch;
     }
 
+	public void setComponentsSizeComputedCallback(Runnable componentsSizeComputedCallback) {
+		this.componentsSizeComputedCallback = componentsSizeComputedCallback;
+	}
+
+	public List<TablexiaDialogComponentAdapter> getDialogComponents() {
+		return dialogComponents;
+	}
 
 //////////////////////////// PAUSE EVENT
 
-- 
GitLab