1
0
forked from 0ad/0ad
0ad/binaries/data/mods/public/gui/session_new/input.js
2010-01-24 17:24:35 +00:00

113 lines
2.9 KiB
JavaScript

const SDL_BUTTON_LEFT = 1;
const SDL_BUTTON_MIDDLE = 2;
const SDL_BUTTON_RIGHT = 3;
// TODO: these constants should be defined somewhere else instead, in
// case any other code wants to use them too
var INPUT_NORMAL = 0;
var INPUT_DRAGGING = 1;
var INPUT_BUILDING_PLACEMENT = 2;
var inputState = INPUT_NORMAL;
var placementEntity = "";
function handleInputBeforeGui(ev)
{
return false;
}
/*
Selection methods: (not all currently implemented)
- Left-click on entity to select (always chooses the 'closest' one if the mouse is over several).
Includes non-controllable units (e.g. trees, enemy units).
- Double-left-click to select entity plus all of the same type on the screen.
- Triple-left-click to select entity plus all of the same type in the world.
- Left-click-and-drag to select all in region. Only includes controllable units.
- Left-click on empty space to deselect all.
- Hotkeys to select various groups.
- Shift plus left-click on entity to toggle selection of that unit. Only includes controllable.
- Shift plus any other selection method above, to add them to current selection.
*/
function handleInputAfterGui(ev)
{
switch (inputState)
{
case INPUT_NORMAL:
switch (ev.type)
{
case "mousebuttondown":
if (ev.button == SDL_BUTTON_LEFT)
{
var ents = Engine.PickEntitiesAtPoint(ev.x, ev.y);
if (!ents.length)
{
resetEntitySelection();
return true;
}
resetEntitySelection();
addEntitySelection([ents[0]]);
Engine.PostNetworkCommand({"type": "spin", "entities": [ents[0]]});
return true;
}
else if (ev.button == SDL_BUTTON_RIGHT)
{
var ents = getEntitySelection();
if (ents.length)
{
var target = Engine.GetTerrainAtPoint(ev.x, ev.y);
Engine.PostNetworkCommand({"type": "walk", "entities": ents, "x": target.x, "z": target.z});
return true;
}
}
}
break;
case INPUT_BUILDING_PLACEMENT:
switch (ev.type)
{
case "mousemotion":
var target = Engine.GetTerrainAtPoint(ev.x, ev.y);
var angle = Math.PI;
Engine.GuiInterfaceCall("SetBuildingPlacementPreview", {"template": placementEntity, "x": target.x, "z": target.z, "angle": angle});
return false; // continue processing mouse motion
case "mousebuttondown":
if (ev.button == SDL_BUTTON_LEFT)
{
var target = Engine.GetTerrainAtPoint(ev.x, ev.y);
var angle = Math.PI;
Engine.GuiInterfaceCall("SetBuildingPlacementPreview", {"template": ""});
Engine.PostNetworkCommand({"type": "construct", "template": placementEntity, "x": target.x, "z": target.z, "angle": angle});
inputState = INPUT_NORMAL;
return true;
}
else if (ev.button == SDL_BUTTON_RIGHT)
{
Engine.GuiInterfaceCall("SetBuildingPlacementPreview", {"template": ""});
inputState = INPUT_NORMAL;
return true;
}
}
break;
}
return false;
}
function testBuild(ent)
{
placementEntity = ent;
inputState = INPUT_BUILDING_PLACEMENT;
}