Skip to content

Commit

Permalink
feat: Added add source block (built-in)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberGlitch01 committed Oct 26, 2023
1 parent 112ba1a commit a9d2d73
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 22 deletions.
46 changes: 46 additions & 0 deletions app/src/main/java/builtin/blocks/BuiltInBlocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package builtin.blocks;

import android.graphics.Color;
import com.dragon.ide.objects.Block;
import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.BlocksHolder;
import com.dragon.ide.objects.ComplexBlockContent;
import com.dragon.ide.objects.blockcontent.SourceContent;
import java.util.ArrayList;

public class BuiltInBlocks {
public static ArrayList<BlocksHolder> getBuiltInBlocksHolder() {
ArrayList<BlocksHolder> blocksHolder = new ArrayList<BlocksHolder>();
BlocksHolder holder1 = new BlocksHolder();
holder1.setColor("#009900");
holder1.setName("Operators");

ArrayList<Block> blockList = new ArrayList<Block>();

Block blockInHolder1 = new Block();
blockInHolder1.setColor("#009900");
blockInHolder1.setBlockType(Block.BlockType.defaultBlock);
blockInHolder1.setName("addSource");
blockInHolder1.setRawCode("%%%% DragonIDE param1 %%%%");

ArrayList<Object> block1ContentList = new ArrayList<Object>();

BlockContent block1Content1 = new BlockContent();
block1Content1.setText("add source");
block1ContentList.add(block1Content1);

SourceContent block1Content2 = new SourceContent();
block1Content2.setId("param1");
block1ContentList.add(block1Content2);

blockInHolder1.setBlockContent(block1ContentList);

blockList.add(blockInHolder1);

holder1.setBlocks(blockList);

blocksHolder.add(holder1);

return blocksHolder;
}
}
30 changes: 16 additions & 14 deletions app/src/main/java/com/dragon/ide/objects/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Block implements Serializable {
private ArrayList<Object> blockContent;
private int BlockType;
private String rawCode;
private String replacer;
private String returns;

This comment has been minimized.

Copy link
@SyntaxGalaxy

SyntaxGalaxy Oct 26, 2023

Contributor

Everything is good but this part may break block code generator system.

Idk what is your logic behind this

This comment has been minimized.

Copy link
@CyberGlitch01

CyberGlitch01 Oct 26, 2023

Author Collaborator

Block doesn't need replacer because default block don't need replacer and for complex and double complex block you defined their replacer with using this method.

This comment has been minimized.

Copy link
@CyberGlitch01

CyberGlitch01 Oct 26, 2023

Author Collaborator

*with > without


public String getColor() {
if (this.color != null) {
Expand Down Expand Up @@ -49,8 +49,7 @@ public String getCode() {
if (getBlockContent().get(i) instanceof ComplexBlockContent) {
blockRawCode =
blockRawCode.replaceAll(
CodeReplacer.getReplacer(
((ComplexBlockContent) getBlockContent().get(i)).getId()),
CodeReplacer.getReplacer(((ComplexBlockContent) getBlockContent().get(i)).getId()),
((ComplexBlockContent) getBlockContent().get(i)).getValue());
}
}
Expand All @@ -67,17 +66,9 @@ public void setRawCode(String rawCode) {
}

public final class BlockType {
public final int defaultBlock = 0;
public final int complexBlock = 1;
public final int doubleComplexBlock = 2;
}

public String getReplacer() {
return this.replacer;
}

public void setReplacer(String replacer) {
this.replacer = replacer;
public static final int defaultBlock = 0;
public static final int complexBlock = 1;
public static final int doubleComplexBlock = 2;
}

public ArrayList<Object> getBlockContent() {
Expand All @@ -87,4 +78,15 @@ public ArrayList<Object> getBlockContent() {
public void setBlockContent(ArrayList<Object> blockContent) {
this.blockContent = blockContent;
}

public String getReturns() {
if (returns != null) {
return this.returns;
}
return "";
}

public void setReturns(String returns) {
this.returns = returns;
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/dragon/ide/objects/BlockContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void setText(String text) {
public class BlockContentType {
public static final int String = 0;
public static final int InputSourceCode = 1;
public static final int Integer = 2;
public static final int Boolean = 3;
public static final int Integer = 2;
public static final int Boolean = 3;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dragon.ide.objects.complexblockcontent;
package com.dragon.ide.objects.blockcontent;

import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.ComplexBlockContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dragon.ide.objects.complexblockcontent;
package com.dragon.ide.objects.blockcontent;

import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.ComplexBlockContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dragon.ide.objects.complexblockcontent;
package com.dragon.ide.objects.blockcontent;

import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.ComplexBlockContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dragon.ide.objects.complexblockcontent;
package com.dragon.ide.objects.blockcontent;

import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.ComplexBlockContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.dragon.ide.R;
import com.dragon.ide.databinding.ActivityEventEditorBinding;
import com.dragon.ide.objects.BlocksHolder;
import com.dragon.ide.objects.WebFile;
import com.dragon.ide.ui.adapters.BlocksHolderEventEditorListItem;
import com.dragon.ide.utils.eventeditor.BlocksListLoader;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
Expand All @@ -17,6 +21,7 @@
public class EventEditorActivity extends BaseActivity {
private ActivityEventEditorBinding binding;
private ArrayList<WebFile> fileList;
private ArrayList<BlocksHolder> blocksHolder;
private String projectName;
private String projectPath;

Expand All @@ -28,8 +33,9 @@ protected void onCreate(Bundle savedInstanceState) {
// set content view to binding's root.
setContentView(binding.getRoot());

// Initialize fileList to avoid null error
// Initialize to avoid null error
fileList = new ArrayList<WebFile>();
blocksHolder = new ArrayList<BlocksHolder>();

// Setup toolbar.
binding.toolbar.setTitle(R.string.app_name);
Expand Down Expand Up @@ -62,6 +68,23 @@ public void onClick(View arg0) {
} else {
loadFileList();
}

/*
* Loads blocks holder
*/
BlocksListLoader blocksListLoader = new BlocksListLoader();
blocksListLoader.loadBlocks(
EventEditorActivity.this,
new BlocksListLoader.Progress() {

@Override
public void onCompleteLoading(ArrayList<BlocksHolder> holder) {
binding.blocksHolderList.setAdapter(
new BlocksHolderEventEditorListItem(holder, EventEditorActivity.this));
binding.blocksHolderList.setLayoutManager(new LinearLayoutManager(EventEditorActivity.this));
}
});

binding.fab.setOnClickListener(
(view) -> {
if (binding.blockArea.getVisibility() == View.GONE) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.dragon.ide.ui.adapters;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.dragon.ide.R;
import com.dragon.ide.databinding.LayoutBlocksHolderListItemBinding;
import com.dragon.ide.databinding.LayoutEventBlocksHolderListItemBinding;
import com.dragon.ide.objects.BlocksHolder;
import com.dragon.ide.ui.activities.BlockManagerActivity;
import java.util.ArrayList;

public class BlocksHolderEventEditorListItem extends RecyclerView.Adapter<BlocksHolderEventEditorListItem.ViewHolder> {

public ArrayList<BlocksHolder> list;
public Activity activity;

public BlocksHolderEventEditorListItem(ArrayList<BlocksHolder> _arr, Activity activity) {
list = _arr;
this.activity = activity;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutEventBlocksHolderListItemBinding item =
LayoutEventBlocksHolderListItemBinding.inflate(activity.getLayoutInflater());
View _v = item.getRoot();
RecyclerView.LayoutParams _lp =
new RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
_v.setLayoutParams(_lp);
return new ViewHolder(_v);
}

@Override
public void onBindViewHolder(ViewHolder _holder, int _position) {
LayoutEventBlocksHolderListItemBinding binding =
LayoutEventBlocksHolderListItemBinding.bind(_holder.itemView);
binding.holderName.setText(list.get(_position).getName());
int blocksCount = list.get(_position).getBlocks().size();
binding.color.setBackgroundColor(Color.parseColor(list.get(_position).getColor()));
binding
.getRoot()
.setOnClickListener(
(view) -> {

});
}

@Override
public int getItemCount() {
return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
}
75 changes: 75 additions & 0 deletions app/src/main/java/com/dragon/ide/ui/view/BlockDefaultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.dragon.ide.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dragon.ide.R;
import com.dragon.ide.objects.Block;
import com.dragon.ide.objects.BlockContent;
import com.dragon.ide.objects.ComplexBlock;
import com.dragon.ide.objects.ComplexBlockContent;
import com.dragon.ide.objects.DoubleComplexBlock;
import com.dragon.ide.objects.blockcontent.SourceContent;

public class BlockDefaultView extends LinearLayout {
public String returns;

public BlockDefaultView(Context context) {
super(context);
setOrientation(LinearLayout.HORIZONTAL);
}

public void setBlock(Block block) {
returns = block.getReturns();

if (!(block instanceof DoubleComplexBlock) && !(block instanceof ComplexBlock)) {
if (block instanceof Block) {
if (block.getBlockType() == Block.BlockType.defaultBlock) {
setBackgroundResource(R.drawable.block_default);

Drawable backgroundDrawable = getBackground();
backgroundDrawable.setTint(Color.parseColor(block.getColor()));
backgroundDrawable.setTintMode(PorterDuff.Mode.SRC_IN);
setBackground(backgroundDrawable);
}
}
}
for (int i = 0; i < block.getBlockContent().size(); ++i) {
if (block.getBlockContent().get(i) instanceof ComplexBlockContent) {
if (block.getBlockContent().get(i) instanceof SourceContent) {
LinearLayout ll_source = new LinearLayout(getContext());
ll_source.setPadding(2, 2, 2, 2);
setBackgroundColor(Color.WHITE);
TextView tvTextContent = new TextView(getContext());
tvTextContent.setText(((SourceContent) block.getBlockContent().get(i)).getValue());
ll_source.addView(tvTextContent, getChildCount());
addView(ll_source, getChildCount());
}

if (block.getBlockContent().get(i) instanceof SourceContent) {
LinearLayout ll_source = new LinearLayout(getContext());
ll_source.setPadding(2, 2, 2, 2);
setBackgroundColor(Color.WHITE);
TextView tvTextContent = new TextView(getContext());
tvTextContent.setText(((SourceContent) block.getBlockContent().get(i)).getValue());
ll_source.addView(tvTextContent, getChildCount());
addView(ll_source, getChildCount());
}
} else if (block.getBlockContent().get(i) instanceof BlockContent) {
TextView tvTextContent = new TextView(getContext());
tvTextContent.setText(((BlockContent) block.getBlockContent().get(i)).getText());
addView(tvTextContent, getChildCount());
}
}
}

public String getReturns() {
if (returns != null) {
return this.returns;
}
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.dragon.ide.utils.eventeditor;

import builtin.blocks.BuiltInBlocks;
import static com.dragon.ide.utils.Environments.BLOCKS;

import android.app.Activity;
import com.dragon.ide.objects.BlocksHolder;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class BlocksListLoader {

public void loadBlocks(Activity activity, Progress progress) {
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(
() -> {
ArrayList<BlocksHolder> blocksHolder = new ArrayList<BlocksHolder>();

ArrayList<BlocksHolder> builtInBlock = BuiltInBlocks.getBuiltInBlocksHolder();

for (int i = 0; i < builtInBlock.size(); ++i) {
blocksHolder.add(builtInBlock.get(i));
}

if (BLOCKS.exists()) {
try {
FileInputStream fis = new FileInputStream(BLOCKS);
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof ArrayList) {
for (int i = 0; i < ((ArrayList<BlocksHolder>) obj).size(); ++i) {
blocksHolder.add(((ArrayList<BlocksHolder>) obj).get(i));
}
} else {
}
fis.close();
ois.close();
} catch (Exception e) {
}
} else {
}
activity.runOnUiThread(
() -> {
progress.onCompleteLoading(blocksHolder);
});
});
}

public interface Progress {
void onCompleteLoading(ArrayList<BlocksHolder> holder);
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_event_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="35"
android:id="@+id/palleteList" />
android:id="@+id/blocksHolderList"
android:background="?attr/colorSurface" />

<androidx.recyclerview.widget.RecyclerView
android:layout_height="match_parent"
Expand Down
Loading

0 comments on commit a9d2d73

Please sign in to comment.