From 0a4e556864b531e0cce150720af144c12027b4b3 Mon Sep 17 00:00:00 2001 From: thindil Date: Sat, 6 Jan 2024 05:03:05 +0000 Subject: [PATCH] refactor: started moving mapAccelerators to Nim FossilOrigin-Name: ecfa0c3e082e493d1daf2dc452cc00f3180ae45cdc09aca0e2534c7bbef50c5e --- nim/src/ui/mapsui.nim | 36 +++++++++++---------- src/ui/gameoptions.adb | 2 +- src/ui/maps-ui.adb | 72 ++++++++++++++++++++++++++++++++---------- src/ui/maps-ui.ads | 22 ++----------- 4 files changed, 79 insertions(+), 53 deletions(-) diff --git a/nim/src/ui/mapsui.nim b/nim/src/ui/mapsui.nim index fe640b4e7..456fb9f6f 100644 --- a/nim/src/ui/mapsui.nim +++ b/nim/src/ui/mapsui.nim @@ -24,6 +24,20 @@ var centerX*, centerY*: Positive ## Coordinates of the center point on the map generalAccelerators*: array[4, string] = ["Alt-a", "Alt-b", "Alt-c", "Alt-d"] ## The list of keyboard shortcuts used in some places + mapView = "" + menuAccelerators*: array[1 .. 11, string] = ["s", "o", "r", "m", "k", "w", + "g", "F1", "p", "q", "x"] + ## The game menu keyboard shortcuts + mapAccelerators*: array[1 .. 37, string] = ["e", "v", "plus", "minus", + "KP_Home", "KP_Up", "KP_Prior", "KP_Left", "KP_Begin", "KP_Right", + "KP_End", "KP_Down", "KP_Next", "KP_Divide", "Shift-Return", "Shift-h", + "Shift-KP_Home", "Shift-KP_Up", "Shift-KP_Prior", "Shift-KP_Left", + "Shift-KP_Right", "Shift-KP_End", "Shift-KP_Down", "Shift-KP_Next", + "Control-KP_Home", "Control-KP_Up", "Control-KP_Prior", "Control-KP_Left", + "Control-KP_Right", "Control-KP_End", "Control-KP_Down", + "Control-KP_Next", "Control-Return", "Control-a", "Control-b", + "Control-c", "Control-d"] ## The keyboard shortcuts used on the map + fullScreenAccel* = "Control-f" proc updateHeader*() {.sideEffect, raises: [], tags: [].} = ## Update in-game header with information about time, state of the crew @@ -349,22 +363,6 @@ proc showSkyMap*(clear: bool = false) {.sideEffect, raises: [], tags: [].} = res = "showstats") currentStory.showText = true -var - mapView = "" - menuAccelerators*: array[1 .. 11, string] = ["s", "o", "r", "m", "k", "w", - "g", "F1", "p", "q", "x"] - ## The game menu keyboard shortcuts - mapAccelerators*: array[1 .. 37, string] = ["e", "v", "plus", "minus", - "KP_Home", "KP_Up", "KP_Prior", "KP_Left", "KP_Begin", "KP_Right", - "KP_End", "KP_Down", "KP_Next", "KP_Divide", "Shift-Return", "Shift-h", - "Shift-KP_Home", "Shift-KP_Up", "Shift-KP_Prior", "Shift-KP_Left", - "Shift-KP_Right", "Shift-KP_End", "Shift-KP_Down", "Shift-KP_Next", - "Control-KP_Home", "Control-KP_Up", "Control-KP_Prior", "Control-KP_Left", - "Control-KP_Right", "Control-KP_End", "Control-KP_Down", - "Control-KP_Next", "Control-Return", "Control-a", "Control-b", - "Control-c", "Control-d"] ## The keyboard shortcuts used on the map - fullScreenAccel* = "Control-f" - proc createGameUi*() = let gameFrame = ".gameframe" @@ -525,3 +523,9 @@ proc getAdaGeneralAccelerator(index: cint): cstring {.raises: [], tags: [], expo proc setAdaGeneralAccelerator(index: cint; value: cstring) {.raises: [], tags: [], exportc.} = generalAccelerators[index - 1] = $value + +proc getAdaMenuAccelerator(index: cint): cstring {.raises: [], tags: [], exportc.} = + return menuAccelerators[index].cstring + +proc setAdaMenuAccelerator(index: cint; value: cstring) {.raises: [], tags: [], exportc.} = + generalAccelerators[index] = $value diff --git a/src/ui/gameoptions.adb b/src/ui/gameoptions.adb index a1d0f520a..03d4c4f00 100644 --- a/src/ui/gameoptions.adb +++ b/src/ui/gameoptions.adb @@ -74,7 +74,7 @@ package body GameOptions is -- SOURCE Accels: array(1 .. 53) of Accel_Data := (1 => - (Shortcut => Menu_Accelerators(1), + (Shortcut => To_Unbounded_String(Source => Get_Menu_Accelerator(Index => 1)), Entry_Name => To_Unbounded_String(Source => ".menu.shipinfo"), Config_Name => To_Unbounded_String(Source => "ShipInfo")), 2 => diff --git a/src/ui/maps-ui.adb b/src/ui/maps-ui.adb index ea565615b..360fd9557 100644 --- a/src/ui/maps-ui.adb +++ b/src/ui/maps-ui.adb @@ -1,4 +1,4 @@ --- Copyright (c) 2020-2023 Bartek thindil Jasicki +-- Copyright (c) 2020-2024 Bartek thindil Jasicki -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -752,33 +752,44 @@ package body Maps.UI is (Source => Raw_Data, Count => Length(Source => Raw_Data) - Equal_Index - 1); if Field_Name = To_Unbounded_String(Source => "ShipInfo") then - Menu_Accelerators(1) := Value; + Set_Menu_Accelerator + (Index => 1, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Orders") then - Menu_Accelerators(2) := Value; + Set_Menu_Accelerator + (Index => 2, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Crafting") then - Menu_Accelerators(3) := Value; + Set_Menu_Accelerator + (Index => 3, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "LastMessages") then - Menu_Accelerators(4) := Value; + Set_Menu_Accelerator + (Index => 4, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Knowledge") then - Menu_Accelerators(5) := Value; + Set_Menu_Accelerator + (Index => 5, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "WaitOrders") then - Menu_Accelerators(6) := Value; + Set_Menu_Accelerator + (Index => 6, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "GameStats") then - Menu_Accelerators(7) := Value; + Set_Menu_Accelerator + (Index => 7, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Help") then - Menu_Accelerators(8) := Value; + Set_Menu_Accelerator + (Index => 8, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "GameOptions") then - Menu_Accelerators(9) := Value; + Set_Menu_Accelerator + (Index => 9, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Quit") then - Menu_Accelerators(10) := Value; + Set_Menu_Accelerator + (Index => 10, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "Resign") then - Menu_Accelerators(11) := Value; + Set_Menu_Accelerator + (Index => 11, Value => To_String(Source => Value)); elsif Field_Name = To_Unbounded_String(Source => "GameMenu") then Map_Accelerators(1) := Value; @@ -1035,7 +1046,7 @@ package body Maps.UI is Action => "attributes", Options => "-fullscreen 1"); end if; Set_Accelerators_Loop : - for Accelerator of Menu_Accelerators loop + for Accelerator in 1 .. 11 loop Bind_To_Main_Window (Interp => Get_Context, Sequence => @@ -1043,15 +1054,23 @@ package body Maps.UI is To_String (Source => Insert - (Source => Accelerator, + (Source => + To_Unbounded_String + (Source => + Get_Menu_Accelerator(Index => Accelerator)), Before => Index - (Source => Accelerator, Pattern => "-", - Going => Backward) + + (Source => + To_Unbounded_String + (Source => + Get_Menu_Accelerator(Index => Accelerator)), + Pattern => "-", Going => Backward) + 1, New_Item => "KeyPress-")) & ">", - Script => "{InvokeMenu " & To_String(Source => Accelerator) & "}"); + Script => + "{InvokeMenu " & Get_Menu_Accelerator(Index => Accelerator) & + "}"); end loop Set_Accelerators_Loop; if Index (Source => @@ -1238,4 +1257,23 @@ package body Maps.UI is Set_Ada_General_Accelerator(I => Index, Val => New_String(Str => Value)); end Set_General_Accelerator; + function Get_Menu_Accelerator(Index: Positive) return String is + + function Get_Ada_Menu_Accelerator(I: Positive) return chars_ptr with + Import => True, + Convention => C, + External_Name => "getAdaMenuAccelerator"; + begin + return Value(Item => Get_Ada_Menu_Accelerator(I => Index)); + end Get_Menu_Accelerator; + + procedure Set_Menu_Accelerator(Index: Positive; Value: String) is + procedure Set_Ada_Menu_Accelerator(I: Positive; Val: chars_ptr) with + Import => True, + Convention => C, + External_Name => "setAdaMenuAccelerator"; + begin + Set_Ada_Menu_Accelerator(I => Index, Val => New_String(Str => Value)); + end Set_Menu_Accelerator; + end Maps.UI; diff --git a/src/ui/maps-ui.ads b/src/ui/maps-ui.ads index 70eb1688a..4697836d7 100644 --- a/src/ui/maps-ui.ads +++ b/src/ui/maps-ui.ads @@ -1,4 +1,4 @@ --- Copyright (c) 2020-2023 Bartek thindil Jasicki +-- Copyright (c) 2020-2024 Bartek thindil Jasicki -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -23,24 +23,6 @@ with Ships; use Ships; package Maps.UI is -- **** - -- ****v* MUI/MUI.MenuAccelerators - -- FUNCTION - -- Array with the game menu default accelerators - -- SOURCE - Menu_Accelerators: array(1 .. 11) of Unbounded_String := - (1 => To_Unbounded_String(Source => "s"), - 2 => To_Unbounded_String(Source => "o"), - 3 => To_Unbounded_String(Source => "r"), - 4 => To_Unbounded_String(Source => "m"), - 5 => To_Unbounded_String(Source => "k"), - 6 => To_Unbounded_String(Source => "w"), - 7 => To_Unbounded_String(Source => "g"), - 8 => To_Unbounded_String(Source => "F1"), - 9 => To_Unbounded_String(Source => "p"), - 10 => To_Unbounded_String(Source => "q"), - 11 => To_Unbounded_String(Source => "x")); - -- **** - -- ****v* MUI/MUI.Map_Accelerators -- FUNCTION -- Array with default map keyboard accelerators @@ -152,6 +134,8 @@ package Maps.UI is function Get_General_Accelerator(Index: Positive) return String; procedure Set_General_Accelerator(Index: Positive; Value: String); + function Get_Menu_Accelerator(Index: Positive) return String; + procedure Set_Menu_Accelerator(Index: Positive; Value: String); procedure Get_Center_Point(X, Y: out Positive) with Import => True, Convention => C,