From c8b851eda93836173bfbf7c3fed63e8176daa1c9 Mon Sep 17 00:00:00 2001 From: olsner Date: Wed, 18 May 2005 05:35:25 +0000 Subject: [PATCH] - JS functions to make use of new ordering and interaction APIs - selectLocation and selectEntity using the new interaction API - Implemented attack and patrol buttons using select{Location,Entity} This was SVN commit r2317. --- .../entities/template_entity_script.js | 8 +- .../official/gui/test/functions_session.js | 108 ++++++++++++++++++ .../gui/test/functions_session_status_orb.js | 52 ++++++--- 3 files changed, 147 insertions(+), 21 deletions(-) diff --git a/binaries/data/mods/official/entities/template_entity_script.js b/binaries/data/mods/official/entities/template_entity_script.js index 4d18ce7fa0..54ea0896e3 100644 --- a/binaries/data/mods/official/entities/template_entity_script.js +++ b/binaries/data/mods/official/entities/template_entity_script.js @@ -206,7 +206,7 @@ function entity_event_targetchanged( evt ) // yields false. ToString converts them to their actual values (i.e. the four character // string) first. - evt.defaultAction = ORDER_GOTO; + evt.defaultAction = NMT_Goto; evt.defaultCursor = "arrow-default"; if( evt.target ) { @@ -214,14 +214,14 @@ function entity_event_targetchanged( evt ) ( evt.target.player != gaiaPlayer ) && ( evt.target.player != this.player ) ) { - evt.defaultAction = ORDER_ATTACK; + evt.defaultAction = NMT_AttackMelee; evt.defaultCursor = "action-attack"; } if( this.actions.gather && evt.target.traits.supply && this.actions.gather[evt.target.traits.supply.type] && ( ( evt.target.traits.supply.curr > 0 ) || ( evt.target.traits.supply.max == 0 ) ) ) { - evt.defaultAction = ORDER_GATHER; + evt.defaultAction = NMT_Gather; // Set cursor (eg "action-gather-fruit"). evt.defaultCursor = "action-gather-" + evt.target.traits.supply.subtype; } @@ -433,4 +433,4 @@ function entity_CheckQueueReq (entry) return "true"; } -// ==================================================================== \ No newline at end of file +// ==================================================================== diff --git a/binaries/data/mods/official/gui/test/functions_session.js b/binaries/data/mods/official/gui/test/functions_session.js index b127d2d12a..320a2bd64e 100755 --- a/binaries/data/mods/official/gui/test/functions_session.js +++ b/binaries/data/mods/official/gui/test/functions_session.js @@ -314,3 +314,111 @@ function FlipGUI(NewGUIType) writeConsole("GUI flipped to " + GUIType + "."); } + +function selectEntity(handler) +{ + endSelection(); + startSelection(function (event) { + // Selection is performed when single-clicking the right mouse + // button. + if (event.button == 3 && event.clicks == 1) + { + handler(event.entity); + } + // End selection on first mouse-click + endSelection(); + }); +} + +function selectLocation(handler) +{ + endSelection(); + startSelection(function (event) { + // Selection is performed when single-clicking the right mouse + // button. + if (event.button == 3 && event.clicks == 1) + { + handler(event.x, event.y); + } + // End selection on first mouse-click + endSelection(); + }); +} + +function startSelection(handler) +{ + gameView.startCustomSelection(); + getGlobal().selectionWorldClickHandler=handler; + console.write("isSelecting(): "+isSelecting()); +} + +function endSelection() +{ + if (!isSelecting()) + return; + + gameView.endCustomSelection(); + getGlobal().selectionWorldClickHandler = null; +} + +function isSelecting() +{ + return getGlobal().selectionWorldClickHandler != null; +} + +// The world-click handler - called whenever the user clicks the terrain +function worldClickHandler(event) +{ + args=new Array(null, null); + + console.write("worldClickHandler: button "+event.button+", clicks "+event.clicks); + + if (isSelecting()) + { + getGlobal().selectionWorldClickHandler(event); + return; + } + + + // Right button single- or double-clicks + if (event.button == 3 && event.clicks <= 2) + { + if (event.clicks == 1) + cmd = event.command; + else if (event.clicks == 2) + { + console.write("Issuing secondary command"); + cmd = event.secondaryCommand; + } + } + else + return; + + switch (cmd) + { + // location target commands + case NMT_Goto: + case NMT_Patrol: + if (event.queued) + { + cmd = NMT_AddWaypoint; + } + case NMT_AddWaypoint: + args[0]=event.x; + args[1]=event.y; + break; + // entity target commands + case NMT_AttackMelee: + case NMT_Gather: + args[0]=event.entity; + args[1]=null; + break; + default: + console.write("worldClickHandler: Unknown command: "+cmd); + return; + } + + issueCommand(selection, cmd, args[0], args[1]); +} + +addGlobalHandler("worldClick", worldClickHandler); diff --git a/binaries/data/mods/official/gui/test/functions_session_status_orb.js b/binaries/data/mods/official/gui/test/functions_session_status_orb.js index 9d628baebc..8b0f8fac8a 100755 --- a/binaries/data/mods/official/gui/test/functions_session_status_orb.js +++ b/binaries/data/mods/official/gui/test/functions_session_status_orb.js @@ -437,25 +437,43 @@ function PressCommandButton(GUIObject, list, tab) { switch (list) { - case 1: - GUIObject.caption = ""; -// if (SN_STATUS_PANE_COMMAND[list][tab].type == "list") -// { + case 1: + GUIObject.caption = ""; + if (SN_STATUS_PANE_COMMAND[list][tab].type == "list") + { //console.write("Clicked [" + list + "," + tab + "]: tab of type " + SN_STATUS_PANE_COMMAND[list][tab].type + "; " + SN_STATUS_PANE_COMMAND[list][tab].last + "; " + SN_STATUS_PANE_COMMAND[list][tab].name); -// // Click the tab button to toggle visibility of its list (if it's of a list type). - GUIObjectToggle("SN_STATUS_PANE_COMMAND_" + tab + "_GROUP"); -// } -// else -// { -// // Perform appropriate actions for different command buttons. -//console.write("Clicked [" + list + "," + tab + "]: command of type " + SN_STATUS_PANE_COMMAND[list][tab].type + "; " + SN_STATUS_PANE_COMMAND[list][tab].last + "; " + SN_STATUS_PANE_COMMAND[list][tab].name); -// } + // Click the tab button to toggle visibility of its list (if it's of a list type). + GUIObjectToggle("SN_STATUS_PANE_COMMAND_" + tab + "_GROUP"); + } + else + { + // Perform appropriate actions for different command buttons. +console.write("Clicked [" + list + "," + tab + "]: command of type " + SN_STATUS_PANE_COMMAND[list][tab].type + "; " + SN_STATUS_PANE_COMMAND[list][tab].last + "; " + SN_STATUS_PANE_COMMAND[list][tab].name); + + switch (SN_STATUS_PANE_COMMAND[list][tab].name) + { + case action_patrol: + // setCursor(...) + selectLocation( + function (x, y) { + issueCommand(selection, NMT_Patrol, x, y); + }); + break; + case action_attack: + // setCursor(...) + selectEntity( + function (target) { + issueCommand(selection, NMT_AttackMelee, target); + }); + break; + } + } break; - default: - // Left-clicked list button. - console.write("Clicked [" + list + "," + tab + "]: list of type " + SN_STATUS_PANE_COMMAND[list][tab].type + "; " + SN_STATUS_PANE_COMMAND[list][tab].name); - // Attempt to add the entry to the queue. - attempt_add_to_build_queue( selection[0], selection[0].traits.id.civ_code + "_" + SN_STATUS_PANE_COMMAND[list][tab].name, list, tab); + default: + // Left-clicked list button. + console.write("Clicked [" + list + "," + tab + "]: list of type " + SN_STATUS_PANE_COMMAND[list][tab].type + "; " + SN_STATUS_PANE_COMMAND[list][tab].name); + // Attempt to add the entry to the queue. + attempt_add_to_build_queue( selection[0], selection[0].traits.id.civ_code + "_" + SN_STATUS_PANE_COMMAND[list][tab].name, list, tab); break; } }