Skip to content

Commit bc88f3d

Browse files
authored
Merge pull request #25 from ARTEMKOPIK/codegen-bot/comprehensive-improvements-1766719476
🎨 Этап 4: Visual Editor Pro с Drag & Drop блоками
2 parents c1e010e + 31e9e80 commit bc88f3d

33 files changed

+1769
-113
lines changed
Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
package com.autoclicker.app
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import android.widget.*
6+
import androidx.recyclerview.widget.ItemTouchHelper
7+
import androidx.recyclerview.widget.LinearLayoutManager
8+
import androidx.recyclerview.widget.RecyclerView
9+
import com.autoclicker.app.adapters.BlocksAdapter
10+
import com.autoclicker.app.adapters.BlocksPaletteAdapter
11+
import com.autoclicker.app.base.BaseActivity
12+
import com.autoclicker.app.data.BlockType
13+
import com.autoclicker.app.data.ScriptBlock
14+
import com.google.android.material.bottomsheet.BottomSheetDialog
15+
import com.google.android.material.chip.Chip
16+
import com.google.android.material.chip.ChipGroup
17+
18+
/**
19+
* Визуальный редактор Pro с drag & drop блоками
20+
*/
21+
class VisualEditorProActivity : BaseActivity() {
22+
23+
private lateinit var rvBlocks: RecyclerView
24+
private lateinit var blocksAdapter: BlocksAdapter
25+
private val blocks = mutableListOf<ScriptBlock>()
26+
27+
private lateinit var tvScriptName: TextView
28+
private var scriptName = "Untitled Script"
29+
30+
override fun onCreate(savedInstanceState: Bundle?) {
31+
super.onCreate(savedInstanceState)
32+
setContentView(R.layout.activity_visual_editor_pro)
33+
34+
tvScriptName = findViewById(R.id.tvScriptName)
35+
rvBlocks = findViewById(R.id.rvBlocks)
36+
37+
setupRecyclerView()
38+
setupButtons()
39+
40+
tvScriptName.text = scriptName
41+
}
42+
43+
private fun setupRecyclerView() {
44+
blocksAdapter = BlocksAdapter(
45+
blocks = blocks,
46+
onBlockClick = { block -> editBlock(block) },
47+
onBlockDelete = { block -> deleteBlock(block) }
48+
)
49+
50+
rvBlocks.layoutManager = LinearLayoutManager(this)
51+
rvBlocks.adapter = blocksAdapter
52+
53+
// Drag & Drop для перестановки блоков
54+
val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
55+
ItemTouchHelper.UP or ItemTouchHelper.DOWN,
56+
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
57+
) {
58+
override fun onMove(
59+
recyclerView: RecyclerView,
60+
viewHolder: RecyclerView.ViewHolder,
61+
target: RecyclerView.ViewHolder
62+
): Boolean {
63+
val fromPosition = viewHolder.adapterPosition
64+
val toPosition = target.adapterPosition
65+
66+
// Перемещаем блок
67+
val block = blocks.removeAt(fromPosition)
68+
blocks.add(toPosition, block)
69+
70+
// Обновляем order
71+
blocks.forEachIndexed { index, scriptBlock ->
72+
scriptBlock.order = index
73+
}
74+
75+
blocksAdapter.notifyItemMoved(fromPosition, toPosition)
76+
return true
77+
}
78+
79+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
80+
val position = viewHolder.adapterPosition
81+
val block = blocks[position]
82+
deleteBlock(block)
83+
}
84+
})
85+
86+
itemTouchHelper.attachToRecyclerView(rvBlocks)
87+
}
88+
89+
private fun setupButtons() {
90+
findViewById<View>(R.id.btnBack).setOnClickListener {
91+
finish()
92+
}
93+
94+
findViewById<View>(R.id.btnAddBlock).setOnClickListener {
95+
showBlockPalette()
96+
}
97+
98+
findViewById<View>(R.id.btnGenerate).setOnClickListener {
99+
generateCode()
100+
}
101+
102+
findViewById<View>(R.id.btnSave).setOnClickListener {
103+
saveScript()
104+
}
105+
106+
findViewById<View>(R.id.btnRename).setOnClickListener {
107+
renameScript()
108+
}
109+
}
110+
111+
/**
112+
* Показать палитру блоков для добавления
113+
*/
114+
private fun showBlockPalette() {
115+
val dialog = BottomSheetDialog(this)
116+
val view = layoutInflater.inflate(R.layout.dialog_block_palette, null)
117+
118+
val chipGroup = view.findViewById<ChipGroup>(R.id.chipGroupCategories)
119+
val rvPalette = view.findViewById<RecyclerView>(R.id.rvBlockPalette)
120+
121+
// Все типы блоков
122+
var currentBlocks = BlockType.values().toList()
123+
124+
val paletteAdapter = BlocksPaletteAdapter { blockType ->
125+
addBlock(blockType)
126+
dialog.dismiss()
127+
}
128+
129+
rvPalette.layoutManager = LinearLayoutManager(this)
130+
rvPalette.adapter = paletteAdapter
131+
paletteAdapter.submitList(currentBlocks)
132+
133+
// Фильтрация по категориям
134+
chipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
135+
if (checkedIds.isEmpty()) {
136+
paletteAdapter.submitList(BlockType.values().toList())
137+
} else {
138+
// Получаем выбранную категорию из чипа
139+
val chip = chipGroup.findViewById<Chip>(checkedIds[0])
140+
val categoryName = chip.text.toString()
141+
142+
val filtered = BlockType.values().filter {
143+
getCategoryName(ScriptBlock.getCategoryForType(it)) == categoryName
144+
}
145+
paletteAdapter.submitList(filtered)
146+
}
147+
}
148+
149+
dialog.setContentView(view)
150+
dialog.show()
151+
}
152+
153+
/**
154+
* Добавить блок в список
155+
*/
156+
private fun addBlock(blockType: BlockType) {
157+
val block = ScriptBlock.createBlock(blockType)
158+
block.order = blocks.size
159+
blocks.add(block)
160+
blocksAdapter.notifyItemInserted(blocks.size - 1)
161+
rvBlocks.scrollToPosition(blocks.size - 1)
162+
}
163+
164+
/**
165+
* Редактировать параметры блока
166+
*/
167+
private fun editBlock(block: ScriptBlock) {
168+
val dialog = BottomSheetDialog(this)
169+
val view = layoutInflater.inflate(R.layout.dialog_edit_block, null)
170+
171+
val tvBlockTitle = view.findViewById<TextView>(R.id.tvBlockTitle)
172+
val layoutParams = view.findViewById<LinearLayout>(R.id.layoutParameters)
173+
val btnSave = view.findViewById<Button>(R.id.btnSave)
174+
val btnCancel = view.findViewById<Button>(R.id.btnCancel)
175+
176+
tvBlockTitle.text = getBlockDisplayName(block.type)
177+
178+
// Создаём поля для каждого параметра
179+
block.parameters.forEach { param ->
180+
val paramView = layoutInflater.inflate(R.layout.item_block_parameter, layoutParams, false)
181+
val tvLabel = paramView.findViewById<TextView>(R.id.tvParamLabel)
182+
val etValue = paramView.findViewById<EditText>(R.id.etParamValue)
183+
184+
tvLabel.text = param.label
185+
etValue.setText(param.value)
186+
187+
// Устанавливаем тип ввода
188+
when (param.type) {
189+
com.autoclicker.app.data.ParameterType.NUMBER,
190+
com.autoclicker.app.data.ParameterType.COORDINATE -> {
191+
etValue.inputType = android.text.InputType.TYPE_CLASS_NUMBER or
192+
android.text.InputType.TYPE_NUMBER_FLAG_SIGNED
193+
}
194+
else -> {
195+
etValue.inputType = android.text.InputType.TYPE_CLASS_TEXT
196+
}
197+
}
198+
199+
etValue.tag = param.name
200+
layoutParams.addView(paramView)
201+
}
202+
203+
btnSave.setOnClickListener {
204+
// Сохраняем значения параметров
205+
for (i in 0 until layoutParams.childCount) {
206+
val paramView = layoutParams.getChildAt(i)
207+
val etValue = paramView.findViewById<EditText>(R.id.etParamValue)
208+
val paramName = etValue.tag as String
209+
val value = etValue.text.toString()
210+
211+
block.parameters.find { it.name == paramName }?.let { param ->
212+
block.parameters[block.parameters.indexOf(param)] = param.copy(value = value)
213+
}
214+
}
215+
216+
blocksAdapter.notifyItemChanged(blocks.indexOf(block))
217+
dialog.dismiss()
218+
}
219+
220+
btnCancel.setOnClickListener {
221+
dialog.dismiss()
222+
}
223+
224+
dialog.setContentView(view)
225+
dialog.show()
226+
}
227+
228+
/**
229+
* Удалить блок
230+
*/
231+
private fun deleteBlock(block: ScriptBlock) {
232+
val position = blocks.indexOf(block)
233+
if (position != -1) {
234+
blocks.removeAt(position)
235+
blocksAdapter.notifyItemRemoved(position)
236+
237+
// Обновляем order
238+
blocks.forEachIndexed { index, scriptBlock ->
239+
scriptBlock.order = index
240+
}
241+
}
242+
}
243+
244+
/**
245+
* Генерировать код из блоков
246+
*/
247+
private fun generateCode() {
248+
if (blocks.isEmpty()) {
249+
Toast.makeText(this, R.string.visual_editor_no_blocks, Toast.LENGTH_SHORT).show()
250+
return
251+
}
252+
253+
val code = blocks.joinToString("\n") { it.generateCode() }
254+
255+
// Показываем диалог с кодом
256+
val dialog = android.app.AlertDialog.Builder(this)
257+
dialog.setTitle(R.string.visual_editor_generated_code)
258+
259+
val scrollView = ScrollView(this)
260+
val tvCode = TextView(this)
261+
tvCode.text = code
262+
tvCode.setTextIsSelectable(true)
263+
tvCode.setPadding(32, 32, 32, 32)
264+
tvCode.typeface = android.graphics.Typeface.MONOSPACE
265+
scrollView.addView(tvCode)
266+
267+
dialog.setView(scrollView)
268+
dialog.setPositiveButton(R.string.action_copy) { _, _ ->
269+
val clipboard = getSystemService(CLIPBOARD_SERVICE) as android.content.ClipboardManager
270+
val clip = android.content.ClipData.newPlainText("Script", code)
271+
clipboard.setPrimaryClip(clip)
272+
Toast.makeText(this, R.string.visual_editor_code_copied, Toast.LENGTH_SHORT).show()
273+
}
274+
dialog.setNegativeButton(R.string.action_close, null)
275+
dialog.show()
276+
}
277+
278+
/**
279+
* Сохранить скрипт
280+
*/
281+
private fun saveScript() {
282+
if (blocks.isEmpty()) {
283+
Toast.makeText(this, R.string.visual_editor_no_blocks, Toast.LENGTH_SHORT).show()
284+
return
285+
}
286+
287+
val code = blocks.joinToString("\n") { it.generateCode() }
288+
289+
// TODO: Сохранить скрипт в файл
290+
// Здесь должна быть интеграция с существующей системой сохранения скриптов
291+
292+
Toast.makeText(this, R.string.visual_editor_script_saved, Toast.LENGTH_SHORT).show()
293+
}
294+
295+
/**
296+
* Переименовать скрипт
297+
*/
298+
private fun renameScript() {
299+
val dialog = android.app.AlertDialog.Builder(this)
300+
dialog.setTitle(R.string.visual_editor_rename_script)
301+
302+
val input = EditText(this)
303+
input.setText(scriptName)
304+
input.selectAll()
305+
dialog.setView(input)
306+
307+
dialog.setPositiveButton(R.string.action_save) { _, _ ->
308+
scriptName = input.text.toString()
309+
tvScriptName.text = scriptName
310+
}
311+
dialog.setNegativeButton(R.string.action_cancel, null)
312+
dialog.show()
313+
}
314+
315+
/**
316+
* Получить отображаемое имя блока
317+
*/
318+
private fun getBlockDisplayName(type: BlockType): String {
319+
return when (type) {
320+
BlockType.CLICK -> getString(R.string.block_click)
321+
BlockType.LONG_CLICK -> getString(R.string.block_long_click)
322+
BlockType.SWIPE -> getString(R.string.block_swipe)
323+
BlockType.TAP -> getString(R.string.block_tap)
324+
BlockType.SLEEP -> getString(R.string.block_sleep)
325+
BlockType.WAIT_COLOR -> getString(R.string.block_wait_color)
326+
BlockType.WAIT_TEXT -> getString(R.string.block_wait_text)
327+
BlockType.IF_COLOR -> getString(R.string.block_if_color)
328+
BlockType.IF_TEXT -> getString(R.string.block_if_text)
329+
BlockType.IF_IMAGE -> getString(R.string.block_if_image)
330+
BlockType.LOOP -> getString(R.string.block_loop)
331+
BlockType.LOOP_COUNT -> getString(R.string.block_loop_count)
332+
BlockType.SET_VAR -> getString(R.string.block_set_var)
333+
BlockType.INC_VAR -> getString(R.string.block_inc_var)
334+
BlockType.DEC_VAR -> getString(R.string.block_dec_var)
335+
BlockType.LOG -> getString(R.string.block_log)
336+
BlockType.TOAST -> getString(R.string.block_toast)
337+
BlockType.TELEGRAM -> getString(R.string.block_telegram)
338+
BlockType.GET_TEXT -> getString(R.string.block_get_text)
339+
BlockType.FIND_TEXT -> getString(R.string.block_find_text)
340+
BlockType.BACK -> getString(R.string.block_back)
341+
BlockType.HOME -> getString(R.string.block_home)
342+
BlockType.RECENTS -> getString(R.string.block_recents)
343+
BlockType.FUNCTION -> getString(R.string.block_function)
344+
BlockType.CALL_FUNC -> getString(R.string.block_call_func)
345+
BlockType.RETURN -> getString(R.string.block_return)
346+
BlockType.COMMENT -> getString(R.string.block_comment)
347+
BlockType.BREAK -> getString(R.string.block_break)
348+
}
349+
}
350+
351+
/**
352+
* Получить имя категории
353+
*/
354+
private fun getCategoryName(category: com.autoclicker.app.data.BlockCategory): String {
355+
return when (category) {
356+
com.autoclicker.app.data.BlockCategory.ACTIONS -> getString(R.string.category_actions)
357+
com.autoclicker.app.data.BlockCategory.WAIT -> getString(R.string.category_wait)
358+
com.autoclicker.app.data.BlockCategory.CONDITIONS -> getString(R.string.category_conditions)
359+
com.autoclicker.app.data.BlockCategory.LOOPS -> getString(R.string.category_loops)
360+
com.autoclicker.app.data.BlockCategory.VARIABLES -> getString(R.string.category_variables)
361+
com.autoclicker.app.data.BlockCategory.OUTPUT -> getString(R.string.category_output)
362+
com.autoclicker.app.data.BlockCategory.OCR -> getString(R.string.category_ocr)
363+
com.autoclicker.app.data.BlockCategory.SYSTEM -> getString(R.string.category_system)
364+
com.autoclicker.app.data.BlockCategory.FUNCTIONS -> getString(R.string.category_functions)
365+
com.autoclicker.app.data.BlockCategory.SPECIAL -> getString(R.string.category_special)
366+
}
367+
}
368+
}
369+

0 commit comments

Comments
 (0)