package com.gctool.mobile import android.content.ClipData import android.content.ClipboardManager import android.app.Activity import android.content.Context import android.os.Bundle import android.view.Gravity import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import android.widget.HorizontalScrollView import android.widget.LinearLayout import android.widget.TextView import android.widget.Toast import com.gctool.mobile.data.Data import com.gctool.mobile.net.Net import com.gctool.mobile.pages.AchievementPage import com.gctool.mobile.pages.ArtifactPage import com.gctool.mobile.pages.AvatarPage import com.gctool.mobile.pages.CommandPage import com.gctool.mobile.pages.ItemPage import com.gctool.mobile.pages.MonsterPage import com.gctool.mobile.pages.Page import com.gctool.mobile.pages.RemotePage import com.gctool.mobile.pages.ScenePage import com.gctool.mobile.pages.SettingsPage import com.gctool.mobile.pages.WeaponPage import com.gctool.mobile.ui.Theme import com.gctool.mobile.ui.Ui import org.json.JSONObject class MainActivity : Activity() { private val pages: List = listOf( ItemPage(), AvatarPage(), WeaponPage(), ArtifactPage(), MonsterPage(), AchievementPage(), ScenePage(), CommandPage(), RemotePage(), SettingsPage() ) private lateinit var content: FrameLayout private lateinit var commandView: TextView private lateinit var statusView: TextView private val tabs = ArrayList() private var currentCommand: String = "" private var currentPage = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Data.load(applicationContext) setContentView(buildRoot()) pages.forEach { page -> page.onCommand = { cmd -> setCommand(cmd) } page.onToast = { msg -> toastMsg(msg) } } select(0) refreshStatus() } // ---------- 界面骨架 ---------- private fun buildRoot(): View { val root = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL setBackgroundColor(Theme.bg) } root.addView(buildHeader()) root.addView(buildTabs()) content = FrameLayout(this).apply { layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f ) } root.addView(content) root.addView(buildCommandBar()) return root } private fun buildHeader(): View { val box = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL setPadding(Ui.dp(this@MainActivity, 16), Ui.dp(this@MainActivity, 14), Ui.dp(this@MainActivity, 16), Ui.dp(this@MainActivity, 8)) background = Ui.round(this@MainActivity, Theme.bgSoft, 0) } val top = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL gravity = Gravity.CENTER_VERTICAL } val titles = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) } titles.addView(TextView(this).apply { text = "GcTools · 手机版" textSize = 19f setTextColor(Theme.text) setTypeface(typeface, android.graphics.Typeface.BOLD) }) titles.addView(TextView(this).apply { text = "Grasscutter 命令生成器 · 重构版 0.3.0" textSize = 11.5f setTextColor(Theme.dim) }) statusView = TextView(this).apply { textSize = 12f setTextColor(Theme.dim) setPadding(Ui.dp(this@MainActivity, 10), Ui.dp(this@MainActivity, 5), Ui.dp(this@MainActivity, 10), Ui.dp(this@MainActivity, 5)) background = Ui.round(this@MainActivity, Theme.accentSoft, 999) } top.addView(titles) top.addView(statusView) box.addView(top) return box } private fun buildTabs(): View { val scroll = HorizontalScrollView(this).apply { isHorizontalScrollBarEnabled = false setBackgroundColor(Theme.bg) } val row = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL setPadding(Ui.dp(this@MainActivity, 12), Ui.dp(this@MainActivity, 6), Ui.dp(this@MainActivity, 12), Ui.dp(this@MainActivity, 8)) } pages.forEachIndexed { index, page -> val chip = Ui.chip(this, page.title, index == 0) { select(index) } val lp = LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) if (index > 0) lp.marginStart = Ui.dp(this, 6) chip.layoutParams = lp tabs.add(chip) row.addView(chip) } scroll.addView(row) return scroll } private fun buildCommandBar(): View { val box = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL setPadding(Ui.dp(this@MainActivity, 14), Ui.dp(this@MainActivity, 10), Ui.dp(this@MainActivity, 14), Ui.dp(this@MainActivity, 12)) setBackgroundColor(Theme.bgSoft) } box.addView(TextView(this).apply { text = "当前命令" textSize = 11.5f setTextColor(Theme.dim) }) commandView = TextView(this).apply { text = "(点击上方功能生成命令)" textSize = 13.5f setTextColor(Theme.dim) setPadding(Ui.dp(this@MainActivity, 10), Ui.dp(this@MainActivity, 9), Ui.dp(this@MainActivity, 10), Ui.dp(this@MainActivity, 9)) background = Ui.round(this@MainActivity, Theme.input, 10, 1, Theme.border) setTextIsSelectable(true) } box.addView(commandView) box.addView(Ui.vspace(this, 8)) val row = LinearLayout(this).apply { orientation = LinearLayout.HORIZONTAL } row.addView(Ui.button(this, "复制") { copyCommand() }) row.addView(View(this).apply { layoutParams = LinearLayout.LayoutParams(Ui.dp(this@MainActivity, 8), 1) }) val send = Ui.button(this, "发送到服务器", true) { sendCurrent() } val lp = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) lp.marginStart = Ui.dp(this, 8) send.layoutParams = lp row.addView(send) box.addView(row) return box } // ---------- 行为 ---------- private fun select(index: Int) { currentPage = index pages.getOrNull(index)?.let { page -> content.removeAllViews() content.addView(page.build(this)) } tabs.forEachIndexed { i, chip -> val active = i == index chip.setTextColor(if (active) Theme.bg else Theme.text) chip.background = Ui.round(this, if (active) Theme.accent else Theme.accentSoft, 999, 1, if (active) Theme.accent else Theme.border) } refreshStatus() } private fun refreshStatus() { val host = Net.host(this) statusView.text = if (host.isBlank()) "未连接" else "服务器 $host" statusView.setTextColor(Theme.dim) } private fun setCommand(command: String) { currentCommand = command commandView.text = command commandView.setTextColor(Theme.text) if (Net.autoSend(this)) sendCommand(command) else toastMsg("已生成,点「发送到服务器」执行") } private fun copyCommand() { if (currentCommand.isBlank()) { toastMsg("先在上面的列表里选一项"); return } val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager cm.setPrimaryClip(ClipData.newPlainText("GcTools", currentCommand)) toastMsg("已复制") } private fun sendCurrent() { if (currentCommand.isBlank()) { toastMsg("先输入命令"); return } sendCommand(currentCommand) } private fun sendCommand(command: String) { statusView.text = "发送中…" statusView.setTextColor(Theme.warn) Net.call(this, "command", JSONObject().put("command", command), object : Net.Cb { override fun ok(data: String) { toastMsg("已发送") statusView.text = "已连接" statusView.setTextColor(Theme.good) } override fun err(message: String) { toastMsg("发送失败") statusView.text = "连接失败" statusView.setTextColor(Theme.danger) } }) } private fun toastMsg(msg: String) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() } }