Skip to content

Commit

Permalink
refactor: started moving mapAccelerators to Nim
Browse files Browse the repository at this point in the history
FossilOrigin-Name: ecfa0c3e082e493d1daf2dc452cc00f3180ae45cdc09aca0e2534c7bbef50c5e
  • Loading branch information
thindil committed Jan 6, 2024
1 parent 6d6d58b commit 0a4e556
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 53 deletions.
36 changes: 20 additions & 16 deletions nim/src/ui/mapsui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/ui/gameoptions.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
72 changes: 55 additions & 17 deletions src/ui/maps-ui.adb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1035,23 +1046,31 @@ 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 =>
"<" &
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 =>
Expand Down Expand Up @@ -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;
22 changes: 3 additions & 19 deletions src/ui/maps-ui.ads
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0a4e556

Please sign in to comment.