The International Simutrans Forum

 

Author Topic: is_ctrl_pressed() for scripting tool  (Read 1431 times)

0 Members and 1 Guest are viewing this topic.

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
is_ctrl_pressed() for scripting tool
« on: August 18, 2021, 03:18:46 PM »
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?

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: is_ctrl_pressed() for scripting tool
« Reply #1 on: August 18, 2021, 03:59:08 PM »
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.
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?

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) 
« Last Edit: August 19, 2021, 12:35:05 AM by Yona-TYT »

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10905
  • Languages: De,EN,JP
Re: is_ctrl_pressed() for scripting tool
« Reply #2 on: August 19, 2021, 02:41:40 AM »
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.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4913
  • Languages: EN, DE, AT
Re: is_ctrl_pressed() for scripting tool
« Reply #3 on: August 23, 2021, 05:47:01 PM »
There are set_flags/get_flags methods in the command_x class. They can set the ctrl/shift state.

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: is_ctrl_pressed() for scripting tool
« Reply #4 on: August 24, 2021, 10:05:19 AM »
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.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4913
  • Languages: EN, DE, AT
Re: is_ctrl_pressed() for scripting tool
« Reply #5 on: August 24, 2021, 03:17:08 PM »
Where would this be useful? In is_work_allowed_here ??

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: is_ctrl_pressed() for scripting tool
« Reply #6 on: August 24, 2021, 04:56:54 PM »
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".

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: is_ctrl_pressed() for scripting tool
« Reply #7 on: August 29, 2021, 01:50:12 AM »
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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: is_ctrl_pressed() for scripting tool
« Reply #8 on: August 29, 2021, 02:48:06 PM »
I'll experiment to see how far I can go (remember that I barely understand c ++ :P).

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4913
  • Languages: EN, DE, AT
Re: is_ctrl_pressed() for scripting tool
« Reply #9 on: September 03, 2021, 01:08:43 PM »
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:
Code: [Select]
// 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.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4913
  • Languages: EN, DE, AT
Re: is_ctrl_pressed() for scripting tool
« Reply #10 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.

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: is_ctrl_pressed() for scripting tool
« Reply #11 on: September 04, 2021, 02:27:07 PM »
Very nice!. :D

Offline THLeaderH

  • Coder/patcher
  • Devotee
  • *
  • Posts: 437
  • Languages: JP,EN
Re: is_ctrl_pressed() for scripting tool
« Reply #12 on: September 05, 2021, 09:44:44 AM »
Fanstastic. Thank you!

Offline Yona-TYT

  • Devotee
  • *
  • Posts: 1768
    • Simutrans-BLOG
  • Languages: ES
Re: is_ctrl_pressed() for scripting tool
« Reply #13 on: September 05, 2021, 03:32:25 PM »

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.