News:

Simutrans.com Portal
Our Simutrans site. You can find everything about Simutrans from here.

is_ctrl_pressed() for scripting tool

Started by THLeaderH, August 18, 2021, 03:18:46 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

THLeaderH

Some construction tools (for example, way construction tool) change their behavior by is_ctrl_pressed() status. Ctrl key is a very easy way to alter the tool behavior, so it should be useful too for scripting tools. Shift key pressing status should be also available for scripting tools.

I think world class should have is_ctrl_pressed() and is_shift_pressed() functions, but implementing a dedicated util class for these functions also sounds to be reasonable. How do you think?

Yona-TYT

#1
I understand that "tool.set_flags (nr)" is used to change the behavior of a tool, however there is no implementation to know which key is pressed.  :-[
Edit.
Quote from: THLeaderH on August 18, 2021, 03:18:46 PMI think world class should have is_ctrl_pressed() and is_shift_pressed() functions, but implementing a dedicated util class for these functions also sounds to be reasonable. How do you think?

I think the correct thing would be a special class to process keyboard events, so it would not be limited to ctrl and shift.  8) 

prissi

Tool do not process keypresses. The status of of teh cntrol/shift is just recorded at the time of the keypress and sent to the command queue when calling the tool.

Dwachs

There are set_flags/get_flags methods in the command_x class. They can set the ctrl/shift state.
Parsley, sage, rosemary, and maggikraut.

THLeaderH

Yes we can set ctrl/shift flags to the tool we call in the script, but we still need a way to get whether ctrl/shift key is pressed for the script.

Dwachs

Where would this be useful? In is_work_allowed_here ??
Parsley, sage, rosemary, and maggikraut.

Yona-TYT

It would be useful to me in "is_work_allowed_here", for example when building a tunnel entrance, if the player does not press Ctrl show a message "You must press the [Ctrl] key to build the tunnel entrance".

THLeaderH

What I want to do with ctrl key is changing the behavior of the scripted tool. For example, in the two_click scripted tool which removes all objects in the selected area, I can make this tool remove the objects which are in different heights when ctrl key is pressed. If ctrl key is not pressed, the scripted tool is supposed to remove the objects only in the selected height.

Yona-TYT

I'll experiment to see how far I can go (remember that I barely understand c ++ :P).

Dwachs

One way to implement this for scripted tools is: add another parameter to tab file that indicates whether work & friends take an additional flags parameter:

// old
needs_flags = 0
function work(player, pos) { .. }
// with flags
needs_flags = 1
function work(player, pos, flags) { .. }

This seems to me the best solution without breaking existing scripts.
Parsley, sage, rosemary, and maggikraut.

Dwachs

I submitted a change as r10077. The functions work, do_work, mark_tiles can have an additional parameter to receive the states of the keys. I added some basic compatibility script such that existing scripts do not break.
Parsley, sage, rosemary, and maggikraut.


THLeaderH


Yona-TYT


Quote from: Dwachs on September 04, 2021, 09:40:13 AM
I submitted a change as r10077. The functions work, do_work, mark_tiles can have an additional parameter to receive the states of the keys. I added some basic compatibility script such that existing scripts do not break.
I wonder if it can be used on scenario.