Skip to content

Commit

Permalink
upd: make world clickable to show toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenDuck committed Oct 21, 2024
1 parent c007202 commit f7f1aa7
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 104 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ dependencies {
api 'com.badlogicgames.gdx:gdx:1.12.1'

implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.compose.material3:material3-android:1.3.0'
implementation 'com.google.android.flexbox:flexbox:3.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.fragment:fragment:1.8.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.8.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.8.3'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'com.github.piegamesde:nbt:3.0.1'
implementation 'com.google.android.material:material:1.12.0'

def shizuku_version = '13+'
implementation "dev.rikka.shizuku:api:$shizuku_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.vn.nguyenduck.blocktopograph.Constants.WORLDS_FOLDER;
import static io.vn.nguyenduck.blocktopograph.utils.Utils.buildMinecraftDataDir;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -55,6 +56,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.world_list_fragment, container, false);
v.setFocusable(true);
v.setFocusableInTouchMode(true);
v.setOnClickListener(View::requestFocus);
RecyclerView recyclerView = v.findViewById(R.id.world_list);
recyclerView.setAdapter(ADAPTER);
return v;
Expand Down Expand Up @@ -85,19 +89,22 @@ private static class WorldListAdapter extends Adapter<ViewHolder> {
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.world_item, parent, false);
return new ViewHolder(view){};
return new ViewHolder(view) {
};
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
WorldPreLoader world = WORLDS.get(WORLD_PATH.get(position));
View view = holder.itemView;
View view = getView(holder);

assert world != null;
CompoundTag data = (CompoundTag) world.getData();

ImageView icon = view.findViewById(R.id.world_item_icon);
icon.setImageDrawable(world.getIconDrawable());
Drawable iconDrawable = world.getIconDrawable();
if (iconDrawable != null) icon.setImageDrawable(iconDrawable);
else icon.setImageResource(R.drawable.world_preview_default);

TextView name = view.findViewById(R.id.world_item_name);
name.setText(world.getName());
Expand Down Expand Up @@ -127,6 +134,25 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// BOGGER.info(data.toString());
}

private static @NonNull View getView(@NonNull ViewHolder holder) {
View view = holder.itemView;

view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setOnFocusChangeListener((v, f) ->
v.findViewById(R.id.toolbar_under_world_item)
.setVisibility(f ? View.VISIBLE : View.GONE)
);

view.setOnClickListener(v -> {
if (v.hasFocus()) {
v.clearFocus();
v.findViewById(R.id.toolbar_under_world_item).setVisibility(View.GONE);
} else v.requestFocus();
});
return view;
}

@Override
public int getItemCount() {
return WORLDS.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

import com.badlogic.gdx.utils.LittleEndianInputStream;

import java.io.BufferedReader;
Expand Down Expand Up @@ -41,21 +43,25 @@ public void update() {
data = fetchWorldData();
}

@Nullable
private File fetchIcon() {
if (icon != null) return icon;
var i = Arrays.stream(path.listFiles((v, n) -> n.startsWith("world_icon"))).findFirst();
return i.orElse(null);
}

@Nullable
private File fetchWorldData() {
if (data != null) return data;
var i = Arrays.stream(path.listFiles((v, n) -> n.equals("level.dat"))).findFirst();
return i.orElse(null);
}

@Nullable
public Drawable getIconDrawable() {
Bitmap icon = BitmapFactory.decodeFile(fetchIcon().getPath());
assert icon != null;
File iconFile = fetchIcon();
if (iconFile == null) return null;
Bitmap icon = BitmapFactory.decodeFile(iconFile.getPath());
return new BitmapDrawable(Resources.getSystem(), icon);
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/world_preview_flat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions app/src/main/res/layout/toolbar_under_world_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:justifyContent="space_evenly"
tools:showIn="@layout/world_item">

<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />

</com.google.android.flexbox.FlexboxLayout>
183 changes: 94 additions & 89 deletions app/src/main/res/layout/world_item.xml
Original file line number Diff line number Diff line change
@@ -1,105 +1,110 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/world_item"
android:layout_marginTop="5dp"
android:background="@color/activated_bg_color">
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/world_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@color/activated_bg_color"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="3dp"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="horizontal"
tools:ignore="UselessParent">

<ImageView
android:layout_width="176dp"
android:layout_height="100dp"
android:contentDescription="@string/world_image"
android:id="@+id/world_item_icon"
app:srcCompat="@drawable/world_demo_screen_big"/>
<ImageView
android:id="@+id/world_item_icon"
android:layout_width="176dp"
android:layout_height="100dp"
android:contentDescription="@string/world_image"
app:srcCompat="@drawable/world_preview_default" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginStart="2dp"
android:background="@color/colorBackgroundDark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:background="@color/colorBackgroundDark"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:textColor="@android:color/white"
android:id="@+id/world_item_name"
android:layout_marginStart="2dp"
android:singleLine="true"
tools:text="@string/world_item_name"/>
<TextView
android:id="@+id/world_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="@font/minecraft_seven"
android:singleLine="true"
android:textColor="@android:color/white"
tools:text="@string/world_item_name" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:background="@color/activated_bg_color"
android:textColor="@android:color/white"
android:layout_marginEnd="0dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:id="@+id/world_item_gamemode"
tools:text="Spectator"/>
<TextView
android:id="@+id/world_item_gamemode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:background="@color/activated_bg_color"
android:fontFamily="@font/minecraft_seven"
android:paddingStart="5dp"
android:paddingTop="2dp"
android:paddingEnd="5dp"
android:paddingBottom="2dp"
android:textColor="@android:color/white"
tools:text="Spectator" />

<TextView
android:text="@string/world_expermental_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:background="@color/yellow"
android:textColor="@android:color/black"
android:layout_marginStart="3dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:id="@+id/world_item_experimental"/>
<TextView
android:id="@+id/world_item_experimental"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:background="@color/yellow"
android:fontFamily="@font/minecraft_seven"
android:paddingStart="5dp"
android:paddingTop="2dp"
android:paddingEnd="5dp"
android:paddingBottom="2dp"
android:text="@string/world_expermental_mode"
android:textColor="@android:color/black" />

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:textColor="@android:color/darker_gray"
android:id="@+id/world_item_last_play"
tools:text="@string/world_last_play"/>
<TextView
android:id="@+id/world_item_last_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:textColor="@android:color/darker_gray"
tools:text="@string/world_last_play" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:textColor="@android:color/darker_gray"
android:id="@+id/world_item_size"
tools:text="@string/world_size"/>
</LinearLayout>
<TextView
android:id="@+id/world_item_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/minecraft_seven"
android:textColor="@android:color/darker_gray"
tools:text="@string/world_size" />
</LinearLayout>

</LinearLayout>
</LinearLayout>

</LinearLayout>
</LinearLayout>

<include
android:id="@+id/toolbar_under_world_item"
layout="@layout/toolbar_under_world_item"
android:visibility="gone" />

</LinearLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/layout/world_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/world_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:spanCount="@integer/world_list_span_count"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager">

</androidx.recyclerview.widget.RecyclerView>
10 changes: 2 additions & 8 deletions app/src/main/res/layout/world_list_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:showIn="@layout/main_activity">
android:orientation="vertical">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/world_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:spanCount="@integer/world_list_span_count"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" />
<include layout="@layout/world_list"/>

</LinearLayout>

0 comments on commit f7f1aa7

Please sign in to comment.