Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joystick support #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GameData/messages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -649,4 +649,4 @@ ColorSet_3 <Hair>
ColorSet_4 <Primary>
ColorSet_5 <Secondary>
ColorSet_6 <Details>

CONFIG_Prompt <Press the button on your controller that matches the displayed button. Hold any button to skip or press ESC to exit.>
113 changes: 112 additions & 1 deletion arenahq.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
interface

{$IFDEF SDLMODE}
uses sdlgfx;
uses sdlgfx {$IFDEF JOYSTICK_SUPPORT},SDL{$ENDIF};
{$ENDIF}

const
Expand All @@ -37,6 +37,9 @@ interface
{$IFDEF SDLMODE}
Procedure StartRPGCampaign( RD: RedrawProcedureType );
Procedure DesignDirBrowser( RD: RedrawProcedureType );
{$IFDEF JOYSTICK_SUPPORT}
Procedure ConfigureController(const RD: RedrawProcedureType);
{$ENDIF}
{$ELSE}
Procedure StartRPGCampaign;
Procedure DesignDirBrowser;
Expand Down Expand Up @@ -1397,6 +1400,114 @@ procedure HQMain( HQCamp: CampaignPtr );
CleanSpriteList();
end;

{$IFDEF JOYSTICK_SUPPORT}
Procedure ConfigRedraw(const RD: RedrawProcedureType; constref button: TButtonMapDesc; constref msg: String);
{ Draw the config UI }
{ Assumes that button is not null }
var
PromptRect, DisplayRect: TSDL_Rect;
S: String;
begin
PromptRect := ZONE_ConfigButtonPrompt.GetRect();
DisplayRect := ZONE_ConfigButton.GetRect();
S := 'Press ' + Replace(button.ConfigName, 'Button', '');
if button.MappedCmd <> NIL then S := S + ' (' + button.MappedCmd^.CmdName + ')';

RD;
InfoBox(PromptRect);
InfoBox(DisplayRect);
CMessage(S, DisplayRect, StdWhite);
CMessage(msg, PromptRect, StdWhite);
GHFlip;
end;

Procedure ConfigureController(const RD: RedrawProcedureType);
const
GHA_SKIP_DELAY = 300;
SDL_CARDINAL_DIRS = [SDL_HAT_UP, SDL_HAT_DOWN, SDL_HAT_LEFT, SDL_HAT_RIGHT];
var
i, num: Integer;
event: TSDL_Event;
prevTicks: LongWord;
loop, quit: Boolean;
kind: TButtonType;
msg: String[150];
dir: ShortInt;
begin
msg := MsgString('CONFIG_Prompt');
quit := false;

for i := ord(BUTTON_A) to ord(BUTTON_RIGHT) do begin
loop := true;
num := -1;
kind := TYPE_NONE;
prevTicks := 0;
dir := 0;
while loop do begin
ConfigRedraw(RD, ButtonMap[i], msg);

{Bootleg extra poll event loop, since we need the raw button indexes and not the RPGKey output}
if SDL_PollEvent(@event) = 1 then begin
case event.type_ of
SDL_JOYBUTTONDOWN: begin
prevTicks := SDL_GetTicks;
num := event.jbutton.button;
kind := TYPE_BUTTON;
end;
SDL_JOYBUTTONUP: if num = event.jbutton.button then loop := false;
SDL_JOYHATMOTION: begin
if event.jhat.value in SDL_CARDINAL_DIRS then begin
prevTicks := SDL_GetTicks;
num := event.jhat.hat;
dir := event.jhat.value;
kind := TYPE_HAT;
end else if event.jhat.value = SDL_HAT_CENTERED then begin
if num = event.jhat.hat then loop := false;
end;
end;
SDL_JOYAXISMOTION: begin
{special case around the main analog stick}
if (event.jaxis.axis <> JoyXIndex)
and (event.jaxis.axis <> JoyYIndex) then begin
if abs(event.jaxis.value) > JOY_AxisCutoff then begin
prevTicks := SDL_GetTicks;
num := event.jaxis.axis;
dir := Sgn(event.jaxis.value);
kind := TYPE_AXIS;
end else if num = event.jaxis.axis then loop := false;
end;
end;
SDL_VIDEORESIZE: ResizeScreen(event.resize.w, event.resize.h);
SDL_KEYDOWN: if event.key.keysym.sym = SDLK_ESCAPE then begin
quit := true;
break;
end;
end;
end else begin
{skip button if any button is held down}
if (num <> -1) and ((SDL_GetTicks - prevTicks) > GHA_SKIP_DELAY) then begin
num := -1;
kind := TYPE_BUTTON;
dir := 0;
loop := false;
end;

CheckAnimTicks;
end;
end;

if quit then break;

ButtonMap[i].BCode := num;
ButtonMap[i].BDir := dir;
ButtonMap[i].BType := kind;
end;

{here to keep from confusing the main event loop}
ButtonState := [];
end;
{$ENDIF}

{$ELSE}
Procedure BrowseDesignFile( List: GearPtr );
{ Choose one of the sibling gears from LIST and display its properties. }
Expand Down
2 changes: 1 addition & 1 deletion colormenu.pp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ implementation
ColorMenuRedraw;
GHFlip;

a := RPGKey;
a := RPGKey(CONTEXT_MENU);

if a = #8 then begin
a := #27;
Expand Down
2 changes: 1 addition & 1 deletion gflooker.pp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ implementation
IndicateTile( GB , X , Y );
{$ENDIF}

A := RPGKey;
A := RPGKey{$IFDEF SDLMODE}(CONTEXT_LOOKER){$ENDIF};

if A = KeyMap[ KMC_North ].KCode then begin
RepositionCursor( 6 );
Expand Down
8 changes: 7 additions & 1 deletion gharena.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
{$ENDIF}

const
Version = '1.310';
Version = '1.JOY';

var
RPM: RPGMenuPtr;
Expand Down Expand Up @@ -89,6 +89,9 @@
AddRPGMenuItem( RPM , 'View Design Files' , 7 );
{$IFDEF SDLMODE}
AddRPGMenuItem( RPM , 'View Color Selector' , 8 );
{$IFDEF JOYSTICK_SUPPORT}
if HasJoystick then AddRPGMenuItem( RPM , 'Configure Controller' , 9 );
{$ENDIF}
{$ENDIF}
AddRPGMenuItem( RPM , 'Quit Game' , -1 );

Expand Down Expand Up @@ -128,6 +131,9 @@
{$IFDEF SDLMODE}
7: DesignDirBrowser( @RedrawOpening );
8: DoCosplay;
{$IFDEF JOYSTICK_SUPPORT}
9: ConfigureController(@RedrawOpening);
{$ENDIF}
{$ELSE}
7: DesignDirBrowser;
{$ENDIF}
Expand Down
8 changes: 8 additions & 0 deletions history.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.JOY X X 2019
- Somewhat sketchy controller support can be compiled in with -dJOYSTICK_SUPPORT (a lot of files)
- Button bindings can be set during play, actions associated with buttons can be set in the config file.
- Resolution selector added to Play Options (mostly for fullscreen mode) (sdlgfx.pp, ui4gh.pp)
- NOKEYBOARD option in config file to use on-screen keyboard instead of typing (sdlgfx.pp, ui4gh.pp)
- Some string utility functions rewritten to use sets (texutil.pp)
- Fix issue preventing ADVANCEDCOLORS from being enabled [but not any issues with ADVANCEDCOLORS] (ui4gh.pp)

1.310 February 7 2019
- All old style walls have been replaced by thin walls (sdlmap.pp)
- Portraits may be assigned as unisex "por_n_*.png" (sdlinfo.pp)
Expand Down
69 changes: 63 additions & 6 deletions pcaction.pp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ implementation
SDLCombatDisplay( PCACTIONRD_GB );
end;

{$IFDEF JOYSTICK_SUPPORT}
Procedure PCActionRedrawWithJoystick;
{ Redraw the map and the PC's info, adding an indicator for joystick direction. }
begin
JoystickIndicatorForRedraw;
SDLCombatDisplay( PCACTIONRD_GB );
end;
{$ENDIF}

Procedure PhoneRedraw;
{ Redraw the map and the PC's info. }
begin
Expand Down Expand Up @@ -309,6 +318,30 @@ implementation
InsertInvCom( PC , NPC );
end;

{$IFDEF SDLMODE}
Procedure SetResolution();
var
RPM: RPGMenuPtr;
N: Integer;
begin
N := 0;
RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_CenterMenu );

for N := 0 to high(AvailableModes) do begin
AddRPGMenuItem( RPM , AvailableModes[N].Name , N );
end;

SetItemByValue( RPM , ModeIndex );

N := SelectMenu( RPM , @CenterMenuRedraw );

if N in [0 .. high(AvailableModes)] then begin
ResizeScreen(AvailableModes[N].Width, AvailableModes[N].Height);
ModeIndex := N;
end;
end;
{$ENDIF}

Procedure SetPlayOptions( GB: GameBoardPtr; Mek: GearPtr );
{ Allow the player to set control type, default burst value settings, }
{ and whatever other stuff you think is appropriate. }
Expand Down Expand Up @@ -350,6 +383,7 @@ implementation
end else begin
AddRPGMenuItem( RPM , 'Enable Name Display' , 9 );
end;
AddRPGMenuItem(RPM, 'Set Resolution', 11);
{$ELSE}
if Accessibility_On then begin
AddRPGMenuItem( RPM , 'Disable Accessibility+' , 10 );
Expand Down Expand Up @@ -404,6 +438,10 @@ implementation

end else if N = 10 then begin
Accessibility_On := Not Accessibility_On;
{$IFDEF SDLMODE}
end else if N = 11 then begin
SetResolution();
{$ENDIF}
end;

until N = -1;
Expand Down Expand Up @@ -912,7 +950,7 @@ implementation
if PropD < 0 then begin
DialogMsg( MsgString( 'PCUS_Prompt' ) );
{$IFDEF SDLMODE}
PropD := DirKey( @PCActionRedraw );
PropD := DirKey( @{$IFNDEF JOYSTICK_SUPPORT}PCActionRedraw{$ELSE}PCActionRedrawWithJoystick{$ENDIF} );
{$ELSE}
PropD := DirKey;
{$ENDIF}
Expand Down Expand Up @@ -943,7 +981,7 @@ implementation
P := GearCurrentLocation( PC );
DialogMsg( MsgString( 'PCUSOP_Prompt' ) );
{$IFDEF SDLMODE}
PropD := DirKey( @PCActionRedraw );
PropD := DirKey( @{$IFNDEF JOYSTICK_SUPPORT}PCActionRedraw{$ELSE}PCActionRedrawWithJoystick{$ENDIF} );
{$ELSE}
PropD := DirKey;
{$ENDIF}
Expand All @@ -968,7 +1006,7 @@ implementation
begin
DialogMsg( MsgString( 'PCREPAIR_Prompt' ) );
{$IFDEF SDLMODE}
D := DirKey( @PCActionRedraw );
D := DirKey( @{$IFNDEF JOYSTICK_SUPPORT}PCActionRedraw{$ELSE}PCActionRedrawWithJoystick{$ENDIF} );
{$ELSE}
D := DirKey;
{$ENDIF}
Expand Down Expand Up @@ -1115,7 +1153,7 @@ implementation
if D > 1 then begin
DialogMsg( MsgString( 'DOMINATE_Prompt' ) );
{$IFDEF SDLMODE}
D := DirKey( @PCActionRedraw );
D := DirKey( @{$IFNDEF JOYSTICK_SUPPORT}PCActionRedraw{$ELSE}PCActionRedrawWithJoystick{$ENDIF} );
{$ELSE}
D := DirKey;
{$ENDIF}
Expand Down Expand Up @@ -1232,7 +1270,7 @@ implementation
if D > 1 then begin
DialogMsg( MsgString( 'PICKPOCKET_Prompt' ) );
{$IFDEF SDLMODE}
D := DirKey( @PCActionRedraw );
D := DirKey( @{$IFNDEF JOYSTICK_SUPPORT}PCActionRedraw{$ELSE}PCActionRedrawWithJoystick{$ENDIF} );
{$ELSE}
D := DirKey;
{$ENDIF}
Expand Down Expand Up @@ -3039,6 +3077,7 @@ procedure ShiftGears( GB: GameBoardPtr; Mek: GearPtr );
{ Indicate the mek to get the action for, }
{ and prepare the display. }
{$IFDEF SDLMODE}
ClearOverlay;
IndicateTile( GB , Mek , True );
{$ELSE}
DisplayGearInfo( Mek , gb );
Expand Down Expand Up @@ -3239,6 +3278,9 @@ procedure ShiftGears( GB: GameBoardPtr; Mek: GearPtr );
GotMove: Boolean;
Mobile: Boolean;
P: Point;
{$IFDEF SDLMODE}
DidRefresh: Boolean;
{$ENDIF}
begin
{ The original comment said "Record where the mek currently is". }
{ But this is obviously checking to see if the mecha is mobile. }
Expand Down Expand Up @@ -3394,11 +3436,26 @@ procedure ShiftGears( GB: GameBoardPtr; Mek: GearPtr );
IndicateTile( Camp^.GB , Mek , True );

P := MouseMapPos;
if OnTheMap( P.X , P.Y ) and Mouse_Active then begin
DidRefresh := Mouse_Active and OnTheMap( P.X , P.Y );
if DidRefresh then begin
ClearOverlay;
MouseAtTile( Camp^.GB , P.X , P.Y );
end;

{$IFDEF JOYSTICK_SUPPORT}
{TODO: make the tile info thing pop up based on this?}
if not DidRefresh then ClearOverlay;
JoystickIndicate(Mek);

if not Mouse_Active then P := JoystickIndicatorPos(Mek);
{$ENDIF}

SDLCombatDisplay( Camp^.GB );
{$IFNDEF JOYSTICK_SUPPORT}
if OnTheMap( P.X , P.Y ) and Mouse_Active then begin
{$ELSE}
if (Mouse_Active or ((not Mouse_Active) and (JoyAxisDir <> []))) and OnTheMap( P.X , P.Y ) then begin
{$ENDIF}
DisplayTileInfo(Camp^.GB, P.X, P.Y, False );
end;
{ Everybody do the flip. }
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Open a terminal in the folder with the source code and type:

fpc -dSDLMODE gharena

For controller support in SDL mode, type:

fpc -dSDLMODE -dJOYSTICK_SUPPORT gharena

For the ASCII version, just type:

fpc gharena
Expand Down
Loading