improved handling and displaying dropped level set zip files into window
authorHolger Schemel <info@artsoft.org>
Thu, 24 Jan 2019 00:11:17 +0000 (01:11 +0100)
committerHolger Schemel <info@artsoft.org>
Thu, 24 Jan 2019 00:11:17 +0000 (01:11 +0100)
This change added the possibility to drop and extract zip files into
the currently displayed level set sub-directory on the "choose level
set" screen.

After dropping and extracting zip files on the "choose level set" or
"choose custom artwork" screens, the newly added level or artwork set
is displayed and pre-selected in the list of level or artwork sets,
so it can easily be loaded. (When dropping and extracting zip files
with level sets on the main menu screen, the last added level set will
be automatically selected and loaded.)

src/events.c
src/libgame/setup.c
src/libgame/setup.h
src/libgame/system.h
src/screens.c
src/screens.h

index 3b48fc01faf7634fb22812e986878d92b641d147..bc6e2eb083604889ca75802a0ab1c139df8bd418 100644 (file)
@@ -1537,6 +1537,7 @@ static boolean HandleDropFileEvent(char *filename)
     return FALSE;
   }
 
+  TreeInfo *tree_node = NULL;
   int tree_type = GetZipFileTreeType(filename);
   char *directory = TREE_USERDIR(tree_type);
 
@@ -1547,6 +1548,23 @@ static boolean HandleDropFileEvent(char *filename)
     return FALSE;
   }
 
+  if (tree_type == TREE_TYPE_LEVEL_DIR &&
+      game_status == GAME_MODE_LEVELS &&
+      leveldir_current->node_parent != NULL)
+  {
+    // extract new level set next to currently selected level set
+    tree_node = leveldir_current;
+
+    // get parent directory of currently selected level set directory
+    directory = getLevelDirFromTreeInfo(leveldir_current->node_parent);
+
+    // use private level directory instead of top-level package level directory
+    if (strPrefix(directory, options.level_directory) &&
+       strEqual(leveldir_current->node_parent->fullpath, "."))
+      directory = getUserLevelDir(NULL);
+  }
+
+  // extract level or artwork set from zip file to target directory
   char *top_dir = ExtractZipFileIntoDirectory(filename, directory, tree_type);
 
   if (top_dir == NULL)
@@ -1556,26 +1574,11 @@ static boolean HandleDropFileEvent(char *filename)
     return FALSE;
   }
 
-  AddUserTreeSetToTreeInfo(top_dir, tree_type);
+  // add extracted level or artwork set to tree info structure
+  AddTreeSetToTreeInfo(tree_node, directory, top_dir, tree_type);
 
-  // when adding new level set in main menu, select it as current level set
-  if (tree_type == TREE_TYPE_LEVEL_DIR &&
-      game_status == GAME_MODE_MAIN &&
-      !game.request_active)
-  {
-    // change current level set to newly added level set from zip file
-    leveldir_current = getTreeInfoFromIdentifier(leveldir_first, top_dir);
-
-    // change current level number to first level of newly added level set
-    level_nr = leveldir_current->first_level;
-
-    // redraw screen to reflect changed level set
-    DrawMainMenu();
-
-    // save this level set and level number as last selected level set
-    SaveLevelSetup_LastSeries();
-    SaveLevelSetup_SeriesInfo();
-  }
+  // update menu screen (and possibly change current level set)
+  DrawScreenAfterAddingSet(top_dir, tree_type);
 
   return TRUE;
 }
index 85f6e1f088c6e5f6bd1e3e7e2296f814557002b7..c64c95c8ee84c6d5fdb17d6b110eaded193a01a7 100644 (file)
@@ -178,7 +178,7 @@ static char *getNetworkDir(void)
   return network_dir;
 }
 
-static char *getLevelDirFromTreeInfo(TreeInfo *node)
+char *getLevelDirFromTreeInfo(TreeInfo *node)
 {
   static char *level_dir = NULL;
 
@@ -3860,32 +3860,29 @@ void LoadLevelArtworkInfo(void)
   print_timestamp_done("LoadLevelArtworkInfo");
 }
 
-static boolean AddUserTreeSetToTreeInfoExt(char *tree_subdir_new, int type)
+static boolean AddTreeSetToTreeInfoExt(TreeInfo *tree_node_old, char *tree_dir,
+                                      char *tree_subdir_new, int type)
 {
-  TreeInfo **tree_node_first, *tree_node_old, *tree_node_new;
-  char *tree_user_dir = TREE_USERDIR(type);
-
-  if (tree_user_dir == NULL)           // should not happen
-    return FALSE;
-
-  // get first node of level or artwork tree
-  tree_node_first = TREE_FIRST_NODE_PTR(type);
-
-  if (tree_node_first == NULL)         // should not happen
-    return FALSE;
-
-  if (type == TREE_TYPE_LEVEL_DIR)
+  if (tree_node_old == NULL)
   {
-    // get level info tree node of personal user level set
-    tree_node_old = getTreeInfoFromIdentifier(*tree_node_first, getLoginName());
-  }
-  else
-  {
-    // get artwork info tree node of first artwork set
-    tree_node_old = *tree_node_first;
+    if (type == TREE_TYPE_LEVEL_DIR)
+    {
+      // get level info tree node of personal user level set
+      tree_node_old = getTreeInfoFromIdentifier(leveldir_first, getLoginName());
+    }
+    else
+    {
+      // get artwork info tree node of first artwork set
+      tree_node_old = ARTWORK_FIRST_NODE(artwork, type);
+    }
   }
 
-  if (tree_node_old == NULL)           // should not happen
+  if (tree_dir == NULL)
+    tree_dir = TREE_USERDIR(type);
+
+  if (tree_node_old   == NULL ||
+      tree_dir        == NULL ||
+      tree_subdir_new == NULL)         // should not happen
     return FALSE;
 
   int draw_deactivation_mask = GetDrawDeactivationMask();
@@ -3896,21 +3893,27 @@ static boolean AddUserTreeSetToTreeInfoExt(char *tree_subdir_new, int type)
   if (type == TREE_TYPE_LEVEL_DIR)
   {
     // load new level set config and add it next to first user level set
-    LoadLevelInfoFromLevelConf(&tree_node_old->next, NULL,
-                              tree_user_dir, tree_subdir_new);
+    LoadLevelInfoFromLevelConf(&tree_node_old->next,
+                              tree_node_old->node_parent,
+                              tree_dir, tree_subdir_new);
   }
   else
   {
     // load new artwork set config and add it next to first artwork set
-    LoadArtworkInfoFromArtworkConf(&tree_node_old->next, NULL,
-                                  tree_user_dir, tree_subdir_new, type);
+    LoadArtworkInfoFromArtworkConf(&tree_node_old->next,
+                                  tree_node_old->node_parent,
+                                  tree_dir, tree_subdir_new, type);
   }
 
   // set draw deactivation mask to previous value
   SetDrawDeactivationMask(draw_deactivation_mask);
 
-  // get tree info tree node of newly added tree set
-  tree_node_new = getTreeInfoFromIdentifier(*tree_node_first, tree_subdir_new);
+  // get first node of level or artwork info tree
+  TreeInfo **tree_node_first = TREE_FIRST_NODE_PTR(type);
+
+  // get tree info node of newly added level or artwork set
+  TreeInfo *tree_node_new = getTreeInfoFromIdentifier(*tree_node_first,
+                                                     tree_subdir_new);
 
   if (tree_node_new == NULL)           // should not happen
     return FALSE;
@@ -3919,21 +3922,22 @@ static boolean AddUserTreeSetToTreeInfoExt(char *tree_subdir_new, int type)
   tree_node_new->node_top    = tree_node_old->node_top;
   tree_node_new->node_parent = tree_node_old->node_parent;
 
-  // sort tree info tree to adjust position of newly added tree set
+  // sort tree info to adjust position of newly added tree set
   sortTreeInfo(tree_node_first);
 
   return TRUE;
 }
 
-void AddUserTreeSetToTreeInfo(char *tree_subdir_new, int type)
+void AddTreeSetToTreeInfo(TreeInfo *tree_node, char *tree_dir,
+                         char *tree_subdir_new, int type)
 {
-  if (!AddUserTreeSetToTreeInfoExt(tree_subdir_new, type))
-    Error(ERR_EXIT, "internal tree set structure corrupted -- aborting");
+  if (!AddTreeSetToTreeInfoExt(tree_node, tree_dir, tree_subdir_new, type))
+    Error(ERR_EXIT, "internal tree info structure corrupted -- aborting");
 }
 
 void AddUserLevelSetToLevelInfo(char *level_subdir_new)
 {
-  AddUserTreeSetToTreeInfo(level_subdir_new, TREE_TYPE_LEVEL_DIR);
+  AddTreeSetToTreeInfo(NULL, NULL, level_subdir_new, TREE_TYPE_LEVEL_DIR);
 }
 
 char *getArtworkIdentifierForUserLevelSet(int type)
index 581ebb1722e643a59989443b9c5579b47228fa95..a60585993513299d880294c761173fd9bc3fc496 100644 (file)
@@ -280,6 +280,7 @@ char *getCommonDataDir(void);
 char *getPersonalDataDir(void);
 char *getUserGameDataDir(void);
 char *getSetupDir(void);
+char *getLevelDirFromTreeInfo(TreeInfo *);
 char *getUserLevelDir(char *);
 char *getNetworkLevelDir(char *);
 char *getCurrentLevelDir(void);
@@ -330,7 +331,7 @@ char *getArtworkIdentifierForUserLevelSet(int);
 TreeInfo *getArtworkTreeInfoForUserLevelSet(int);
 boolean checkIfCustomArtworkExistsForCurrentLevelSet(void);
 void AddUserLevelSetToLevelInfo(char *);
-void AddUserTreeSetToTreeInfo(char *, int);
+void AddTreeSetToTreeInfo(TreeInfo *, char *, char *, int);
 boolean UpdateUserLevelSet(char *, char *, char *, int);
 boolean CreateUserLevelSet(char *, char *, char *, int, boolean);
 
index 5249adfcb599df787c497ba9aa6ba153949eedfb..bab4957e9f861437e39851d7b8fb945dbe7bf8b5 100644 (file)
                                 &artwork.mus_first :                   \
                                 NULL)
 
+#define TREE_FIRST_NODE(t)     ((t) == TREE_TYPE_LEVEL_DIR ?           \
+                                leveldir_first :                       \
+                                (t) == TREE_TYPE_GRAPHICS_DIR ?        \
+                                artwork.gfx_first :                    \
+                                (t) == TREE_TYPE_SOUNDS_DIR ?          \
+                                artwork.snd_first :                    \
+                                (t) == TREE_TYPE_MUSIC_DIR ?           \
+                                artwork.mus_first :                    \
+                                NULL)
+
 // values for artwork handling
 #define LEVELDIR_ARTWORK_SET_PTR(leveldir, type)                       \
                                ((type) == ARTWORK_TYPE_GRAPHICS ?      \
index 92c752afb09beb956f0902d4b767d44008270cb0..c8324210de2648e2cec37e8865946783b2148b0b 100644 (file)
@@ -8905,3 +8905,71 @@ boolean DoScreenAction(int image_id)
 
   return FALSE;
 }
+
+void DrawScreenAfterAddingSet(char *tree_subdir_new, int tree_type)
+{
+  // get tree info node of newly added level or artwork set
+  TreeInfo *tree_node_first = TREE_FIRST_NODE(tree_type);
+  TreeInfo *tree_node_new = getTreeInfoFromIdentifier(tree_node_first,
+                                                     tree_subdir_new);
+  if (tree_node_new == NULL)   // should not happen
+    return;
+
+  // if request dialog is active, do nothing
+  if (game.request_active)
+    return;
+
+  if (game_status == GAME_MODE_MAIN &&
+      tree_type == TREE_TYPE_LEVEL_DIR)
+  {
+    // when adding new level set in main menu, select it as current level set
+
+    // change current level set to newly added level set from zip file
+    leveldir_current = tree_node_new;
+
+    // change current level number to first level of newly added level set
+    level_nr = leveldir_current->first_level;
+
+    // redraw screen to reflect changed level set
+    DrawMainMenu();
+
+    // save this level set and level number as last selected level set
+    SaveLevelSetup_LastSeries();
+    SaveLevelSetup_SeriesInfo();
+  }
+  else if (game_status == GAME_MODE_LEVELS &&
+          tree_type == TREE_TYPE_LEVEL_DIR)
+  {
+    // when adding new level set in level set menu, set cursor and update screen
+
+    leveldir_current = tree_node_new;
+
+    DrawChooseTree(&leveldir_current);
+  }
+  else if (game_status == GAME_MODE_SETUP)
+  {
+    // when adding new artwork set in setup menu, set cursor and update screen
+
+    if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS &&
+       tree_type == TREE_TYPE_GRAPHICS_DIR)
+    {
+      artwork.gfx_current = tree_node_new;
+
+      DrawChooseTree(&artwork.gfx_current);
+    }
+    else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS &&
+            tree_type == TREE_TYPE_SOUNDS_DIR)
+    {
+      artwork.snd_current = tree_node_new;
+
+      DrawChooseTree(&artwork.snd_current);
+    }
+    else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC &&
+            tree_type == TREE_TYPE_MUSIC_DIR)
+    {
+      artwork.mus_current = tree_node_new;
+
+      DrawChooseTree(&artwork.mus_current);
+    }
+  }
+}
index 256fddb09d75a28aaf2b1e3e0e9ec44275477353..7d282efad621db7a3e0ae5314031bb9f9f36cc3a 100644 (file)
@@ -23,6 +23,7 @@ void DrawMainMenuExt(int);
 void DrawAndFadeInMainMenu(int);
 void DrawMainMenu(void);
 void DrawHallOfFame(int, int);
+void DrawScreenAfterAddingSet(char *, int);
 
 void RedrawSetupScreenAfterFullscreenToggle(void);
 void RedrawSetupScreenAfterScreenRotation(int);