PDA

View Full Version : Image.selectToken() during a Token.onClickDown()



celestian
April 20th, 2020, 07:41
I am attempting to have have the default token selection be the one that has "active" initiative if there is a pile of tokens.

I've setup Token.onClickDown = onClickDownADND; and that code is something alone these lines.



function onClickDownADND( target, button, image )
-- if host and not pressing control (targeting)
if not Input.isControlPressed() then
-- click left mouse on token
if button == 1 then
local ctwnd = nil;
local windowPath = nil
if User.isHost() then
ctwnd = Interface.findWindow("combattracker_host", "combattracker");
if ctwnd then windowPath = ctwnd.combatants.subwindow.list; end;
else
ctwnd = Interface.findWindow("combattracker_client", "combattracker");
if ctwnd then windowPath = ctwnd.list; end;
end
if ctwnd then
local nodeCT = CombatManager.getCTFromToken(target);
if nodeCT then
--
Debug.console("manager_token_adnd.lua","onClickDownADND","target",target);
for _, oToken in pairs(image.getTokens()) do
-- otoken is in a pile with selected token/target
if oToken.getPosition() == target.getPosition() then
Debug.console("manager_token_adnd.lua","onClickDownADND","oToken",oToken);
local nodeCT = CombatManager.getCTFromToken(oToken);
if nodeCT then
Debug.console("manager_token_adnd.lua","onClickDownADND","nodeCT",nodeCT);
local bActive = (DB.getValue(nodeCT, "active", 0) == 1);
Debug.console("manager_token_adnd.lua","onClickDownADND","bActive",bActive);
if bActive then
Debug.console("manager_token_adnd.lua","onClickDownADND","image.getSelectedTokens()",image.getSelectedTokens());
image.clearSelectedTokens();
image.selectToken(oToken, true);
Debug.console("manager_token_adnd.lua","onClickDownADND","image.getSelectedTokens()",image.getSelectedTokens());
return true;
end
end
end
end
--

end -- nodeCT entry didn't exist for token on map
end
end
end
end


When I use the above code the debug shows me the image.getSelectedTokens() is always the same, never removes "target" as selected and uses the selectToken(oToken,true).

What is the proper process to unselected/select a "selected" token programmatically ?

Moon Wizard
April 20th, 2020, 08:12
The mouse event calls on tokens do not work the same as controls (registered handlers can events); and multiple handlers can be assigned to each mouse action by different scripts.

This means that the default behavior is still called after the handlers are run. You won’t be able to override the default behavior with the current implementation.

Regards,
JPG

Diablobob
April 20th, 2020, 16:04
You will need to check if the token clicked on is the current “active” token

Then unselect all, run threw the list at the current position, and select the token wanted (but not the one clicked on), then lastly replace the token initially clicked on and lastly return true

Hope that helps and makes sense... that was the first version of token stack resolution I did... and it has become much more complicated now... but that is the source for making this all work

The real key is to “hold” the original token until you finish resolving the stack

celestian
April 21st, 2020, 17:59
With some help from @Diablobob I was able to resolve my issue... it's a end run around to get it to work but it works. I'd much prefer just being able to selectToken(). Hopefully down the road in FGU we can have more flexibility to do that.

Here is a code snippet that works. This version is restricted to just the host. You'll want to make sure it's only run when you want it (when clicked down, not control-clicked/etc).



--[[
Select a specific token in a pile of tokens.

Used to push a token to the top of a pile and set as selected.
]]--
function selectTokenInPile(tokenToSelect)
if User.isHost() then
local imageControl = ImageManager.getImageControl(tokenToSelect, true);
if imageControl then
local positionTarget = tokenToSelect.getPosition();
imageControl.clearSelectedTokens();
for _, oToken in pairs(imageControl.getTokens()) do
-- otoken is ontop of selected target
if oToken.getPosition() == positionTarget then
local nodeCT = CombatManager.getCTFromToken(oToken);
if nodeCT then
if tokenToSelect == oToken then
imageControl.selectToken(oToken, true);
else
CombatManager.replaceCombatantToken(nodeCT,nil);
end
end
end
end
return true;
end
end
return false;
end