Skip to content

Commit

Permalink
Introduce add local actor and object (#5022)
Browse files Browse the repository at this point in the history
  • Loading branch information
StofflR authored Aug 28, 2024
1 parent cecc382 commit ddee76f
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 3 deletions.
5 changes: 5 additions & 0 deletions catroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing" />

<activity
android:name=".ui.recyclerview.backpack.ActorAndObjectActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing" />

<activity
android:name=".ui.ScratchConverterActivity"
android:screenOrientation="portrait"
Expand Down
15 changes: 15 additions & 0 deletions catroid/src/main/java/org/catrobat/catroid/ui/ProjectActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ import org.catrobat.catroid.stage.StageActivity
import org.catrobat.catroid.stage.TestResult
import org.catrobat.catroid.ui.BottomBar.showBottomBar
import org.catrobat.catroid.ui.controller.BackpackListManager
import org.catrobat.catroid.ui.controller.ActorsAndObjectsManager
import org.catrobat.catroid.ui.dialogs.LegoSensorConfigInfoDialog
import org.catrobat.catroid.ui.fragment.ProjectOptionsFragment
import org.catrobat.catroid.ui.recyclerview.backpack.ActorAndObjectActivity
import org.catrobat.catroid.ui.recyclerview.backpack.BackpackActivity
import org.catrobat.catroid.ui.recyclerview.controller.SceneController
import org.catrobat.catroid.ui.recyclerview.dialog.NewSpriteDialogFragment
Expand Down Expand Up @@ -430,6 +432,19 @@ class ProjectActivity : BaseCastActivity() {
}
alertDialog.dismiss()
}
dialogNewActorBinding.dialogNewLookFromActorsAndObjects.setOnClickListener {
if (ActorsAndObjectsManager.getInstance().sprites.isNotEmpty()) {
val intent = Intent(this, ActorAndObjectActivity::class.java)
intent.putExtra(
ActorAndObjectActivity.EXTRA_FRAGMENT_POSITION,
ActorAndObjectActivity.FRAGMENT_SPRITES
)
startActivity(intent)
} else {
ToastUtil.showError(this, R.string.backpack_empty)
}
alertDialog.dismiss()
}
dialogNewActorBinding.dialogNewLookFromLocal.setOnClickListener {
ImportFromLocalProjectListLauncher(
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2023 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.ui.controller;

import org.catrobat.catroid.ProjectManager;
import org.catrobat.catroid.R;
import org.catrobat.catroid.common.FlavoredConstants;
import org.catrobat.catroid.common.LookData;
import org.catrobat.catroid.content.Script;
import org.catrobat.catroid.content.Sprite;
import org.catrobat.catroid.content.StartScript;
import org.catrobat.catroid.content.bricks.MoveNStepsBrick;
import org.catrobat.catroid.content.bricks.PenDownBrick;
import org.catrobat.catroid.content.bricks.PlaceAtBrick;
import org.catrobat.catroid.content.bricks.RepeatBrick;
import org.catrobat.catroid.content.bricks.RepeatUntilBrick;
import org.catrobat.catroid.content.bricks.SetTransparencyBrick;
import org.catrobat.catroid.content.bricks.TurnRightBrick;
import org.catrobat.catroid.io.ResourceImporter;
import org.catrobat.catroid.ui.settingsfragments.SettingsFragment;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.catrobat.catroid.common.Constants.DEFAULT_IMAGE_EXTENSION;
import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME;

public final class ActorsAndObjectsManager {

private static ActorsAndObjectsManager instance = null;
public static ActorsAndObjectsManager getInstance() {
if (instance == null) {
instance = new ActorsAndObjectsManager();
}
return instance;
}

private ActorsAndObjectsManager() {
}

private Sprite generateSprite(String name, int imgId, double scale) throws IOException {
var sprite = new Sprite(name);
var folder = new File(FlavoredConstants.DEFAULT_ROOT_DIRECTORY, IMAGE_DIRECTORY_NAME);
if(!folder.exists())
folder.mkdirs();
var path =
ResourceImporter.createImageFileFromResourcesInDirectory(ProjectManager.getInstance().getApplicationContext().getResources(),
imgId,
folder,
"img" + DEFAULT_IMAGE_EXTENSION,
scale);
var lookData = new LookData(name, path);
sprite.getLookList().add(lookData);

return sprite;
}

public List<Sprite> getSprites() {
var sprites = new ArrayList<Sprite>();

try {
if(SettingsFragment.isEmroiderySharedPreferenceEnabled(ProjectManager.getInstance().getApplicationContext())){
var frame = generateSprite("10x10", R.drawable.frame, 1.0);
var script = new StartScript();
script.addBrick(new SetTransparencyBrick(100.0));
script.addBrick(new PlaceAtBrick(-250, 250));
script.addBrick(new PenDownBrick());
for (int i = 0; i < 4; i++) {
script.addBrick(new MoveNStepsBrick(500));
script.addBrick(new TurnRightBrick(90));
}
frame.addScript(script);
sprites.add(frame);

sprites.add(generateSprite("Needle", R.drawable.plotter, 0.2));
} else {
sprites.add(generateSprite("Plotter", R.drawable.plotter, 0.2));
sprites.add(generateSprite("PandaA", R.drawable.panda_a, 1.0));
sprites.add(generateSprite("PandaB", R.drawable.panda_b, 1.0));
sprites.add(generateSprite("Apple", R.drawable.apple, 1.0));
sprites.add(generateSprite("LynxA", R.drawable.lynx_a, 1.0));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sprites;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.catrobat.catroid.ui.recyclerview.backpack;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;

import org.catrobat.catroid.R;
import org.catrobat.catroid.ui.BaseActivity;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentTransaction;

public class ActorAndObjectActivity extends BaseActivity {

public static final String TAG = ActorAndObjectActivity.class.getSimpleName();

public static final String EXTRA_FRAGMENT_POSITION = "fragmentPosition";
public static final int FRAGMENT_SPRITES = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_recycler_backpack);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setTitle(R.string.actor_object_title);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#83B3C7")));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
switchToFragment();
}

private void switchToFragment() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new ActorAndObjectSpriteFragment(), ActorAndObjectSpriteFragment.TAG);
fragmentTransaction.commit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.catrobat.catroid.ui.recyclerview.backpack;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

import org.catrobat.catroid.R;
import org.catrobat.catroid.ui.recyclerview.adapter.ExtendedRVAdapter;
import org.catrobat.catroid.ui.recyclerview.adapter.RVAdapter;
import org.catrobat.catroid.ui.recyclerview.adapter.draganddrop.TouchHelperCallback;
import org.catrobat.catroid.ui.recyclerview.adapter.multiselection.MultiSelectionManager;
import org.catrobat.catroid.ui.recyclerview.viewholder.CheckableViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.annotation.PluralsRes;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

public abstract class ActorAndObjectRecyclerViewFragment<T> extends Fragment implements
RVAdapter.OnItemClickListener<T> {

protected View parentView;
protected RecyclerView recyclerView;

protected ExtendedRVAdapter<T> adapter;

protected String sharedPreferenceDetailsKey = "";
public boolean hasDetails = false;

protected ItemTouchHelper touchHelper;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.fragment_list_view, container, false);
recyclerView = parentView.findViewById(R.id.recycler_view);
setHasOptionsMenu(false);

return parentView;
}

@Override
public void onActivityCreated(Bundle savedInstance) {
super.onActivityCreated(savedInstance);
initializeAdapter();
}

public void onAdapterReady() {
recyclerView.setAdapter(adapter);

adapter.setOnItemClickListener(this);
adapter.showRipples = false;
adapter.showSettings = false;
adapter.showDetails = false;
setHasOptionsMenu(false);
adapter.notifyDataSetChanged();

ItemTouchHelper.Callback callback = new TouchHelperCallback(adapter);
touchHelper = new ItemTouchHelper(callback);
touchHelper.attachToRecyclerView(recyclerView);
}

@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}

@Override
public void onStop() {
super.onStop();
finishActionMode();
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}

protected void finishActionMode() {
adapter.clearSelection();
setShowProgressBar(false);
}

@Override
public void onItemClick(final T item, MultiSelectionManager selectionManager) {
unpackItems(new ArrayList<>(Collections.singletonList(item)));

}

public void setShowProgressBar(boolean show) {
parentView.findViewById(R.id.progress_bar).setVisibility(show ? View.VISIBLE : View.GONE);
recyclerView.setVisibility(show ? View.GONE : View.VISIBLE);
}

@Override
public void onItemLongClick(T item, CheckableViewHolder holder) {
onItemClick(item, null);
}

@Override
public void onSettingsClick(T item, View view) {

}

protected abstract void initializeAdapter();

protected abstract void unpackItems(List<T> selectedItems);

@PluralsRes
protected abstract int getDeleteAlertTitleId();
protected abstract void deleteItems(List<T> selectedItems);
}
Loading

0 comments on commit ddee76f

Please sign in to comment.