Procházet zdrojové kódy

RemoteConsole: add registerCdbMenu, minor fixes

Yuxiao Mao před 2 měsíci
rodič
revize
c71d0bcc44

+ 1 - 0
hide/comp/cdb/Cell.hx

@@ -134,6 +134,7 @@ class Cell {
 						keys : this.editor.config.get("key.cdb.showUnreferenced"),
 					}
 				];
+				menu.append(hide.view.RemoteConsoleView.getCdbMenuActions(this.table.sheet.name, this.value));
 		case TRef(sname):
 			if( value != null && value != "" )
 				menu = [

+ 28 - 3
hide/view/RemoteConsoleView.hx

@@ -97,7 +97,7 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 		if( c != null ) {
 			// Find the first empty or disconnected panel
 			for( p in panels ) {
-				if( p.connection == null || p.connection == c || !p.connection.isConnected() ) {
+				if( p.connection == c || !p.isConnected() ) {
 					p.connection = c;
 					panel = p;
 					break;
@@ -146,6 +146,8 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 		rcmd.startServer(function(c) {
 			if( inst != null )
 				inst.addPanel(c);
+			else
+				c.onClose = () -> refreshStatusIcon();
 			refreshStatusIcon();
 		});
 		refreshStatusIcon();
@@ -160,13 +162,36 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 		stopServer();
 	}
 
-	// allow hide-plugin to send console command to connected game instances
+	public static function getCdbMenuActions( sheet : String, id : String ) : Array<hide.comp.ContextMenu.MenuItem> {
+		if( rcmd == null || !rcmd.isConnected() || rcmd.connections.length == 0)
+			return [];
+		var actions = [];
+		for( c in rcmd.connections ) {
+			if( c.isConnected() && c.menuActions != null ) {
+				for( pa in c.menuActions ) {
+					if( pa.cdbSheet == sheet ) {
+						actions.push({
+							label : pa.name,
+							click : () -> c.sendCommand("menuAction", { action : pa, id : id }),
+						});
+					}
+				}
+			}
+		}
+		return actions;
+	}
+
+	/**
+		For hide-plugin: send console command to connected game instances
+	**/
 	public static function runInRemoteConsole( cmd : String ) {
 		rcmd?.sendCommand("runInConsole", { cmd : cmd });
 	}
 
-	// allow hide-plugin to add/modify game-specific hide command control
 	public static var commandViews = new Map<String, Class<RemoteConsoleCommand>>();
+	/**
+		For hide-plugin: add/modify game-specific hide command control
+	**/
 	public static function registerCommandView( name : String, cl : Class<RemoteConsoleCommand> ) {
 		commandViews.set(name, cl);
 		return null;

+ 21 - 2
hrt/impl/RemoteConsole.hx

@@ -1,5 +1,10 @@
 package hrt.impl;
 
+typedef RemoteMenuAction = {
+	name : String,
+	?cdbSheet : String,
+}
+
 /**
 	A simple socket-based local communication channel (plaintext and unsafe),
 	aim at communicate between 2 programs (e.g. Hide and a HL game).
@@ -316,11 +321,25 @@ class RemoteConsoleConnection {
 		return -1;
 	}
 
+	public var menuActions(default, null) : Array<RemoteMenuAction> = null;
+	@cmd function registerMenuActions( args : { actions : Array<RemoteMenuAction> } ) {
+		menuActions = args?.actions;
+	}
+
+	@cmd function menuAction( args : { action : RemoteMenuAction, id : String } ) : Int {
+		return onMenuAction(args?.action, args?.id);
+	}
+
+	public dynamic function onMenuAction( action : RemoteMenuAction, id : String ) : Int {
+		sendLogError('onMenuAction not implemented');
+		return -1;
+	}
+
 #if editor
 	// ----- Hide ------
 
 	var parser : hscript.Parser;
-	@cmd function open( args : { file : String, ?line : Int, ?column : Int, ?cdbsheet : String,
+	@cmd function open( args : { ?file : String, ?line : Int, ?column : Int, ?cdbsheet : String,
 								?selectExpr : String } ) {
 		if( args == null )
 			return;
@@ -348,7 +367,7 @@ class RemoteConsoleConnection {
 				}
 				Std.downcast(view, hide.view.CdbTable).goto(sheet, line, args.column ?? -1);
 			});
-		} else {
+		} else if( args.file != null ) {
 			hide.Ide.inst.showFileInResources(args.file);
 			hide.Ide.inst.openFile(args.file, null, function(view) {
 				hide.Ide.inst.focus();

+ 23 - 0
hrt/impl/RemoteTools.hx

@@ -11,6 +11,7 @@ class RemoteTools {
 	static var mainEvent : haxe.MainLoop.MainEvent;
 	static var lastUpdate : Float;
 	static var onConnected : Bool -> Void;
+	static var menuActions : Array<{ action : RemoteConsole.RemoteMenuAction, f : (id:String) -> Int}> = [];
 
 	public static function autoConnect( ?onConnected : Bool -> Void ) {
 		RemoteTools.onConnected = onConnected;
@@ -48,6 +49,25 @@ class RemoteTools {
 		return -1;
 	}
 
+	public static function registerCdbMenu( sheet : String, name : String, f : (id:String) -> Int ) {
+		menuActions.push({ action : { name : name, cdbSheet: sheet }, f : f});
+		// Send menuActions if already connected
+		if( rc != null && rc.isConnected() ) {
+			rc.sendCommand("registerMenuActions", { actions : [for( ma in menuActions ) ma.action] });
+		}
+	}
+
+	static function onMenuAction( action : RemoteConsole.RemoteMenuAction, id : String ) : Int {
+		if( action == null || id == null )
+			return -1;
+		for( ma in menuActions ) {
+			if( ma.action.name == action.name && ma.action.cdbSheet == action.cdbSheet ) {
+				return ma.f(id);
+			}
+		}
+		return -1;
+	}
+
 	static function update() {
 		if( rc == null || rc.isConnected() )
 			return;
@@ -62,7 +82,10 @@ class RemoteTools {
 				var c = rc.connections[0];
 				if( c != null ) {
 					c.onConsoleCommand = (cmd) -> onConsoleCommand(cmd);
+					c.onMenuAction = (action,id) -> onMenuAction(action,id);
 				}
+				// Send menuActions
+				rc.sendCommand("registerMenuActions", { actions : [for( ma in menuActions ) ma.action] });
 			}
 		});
 	}