Skip to content

Commit

Permalink
Rework <dataselect> initially selected option
Browse files Browse the repository at this point in the history
Changes with the <dataselect> element:
- Use the event system ("change" event),
  instead of checking whether the selection changes each frame.
- Set the initially selection option directly instead of round tripping
  through the event queue.
- Fix data source "get" functions (only used by <dataselect>).

Fixes some bugs with the options menus:
- audio.al.device is no longer set to the empty string every time the
  cgame loads
- If you used a positive r_mode, opening the resolution menu will no
  longer set it to the equivalent resolution but using r_mode -1 and
  "custom" dimensions.
  • Loading branch information
slipher committed Oct 1, 2024
1 parent 118eb8e commit 39fbbbc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 47 deletions.
13 changes: 8 additions & 5 deletions src/cgame/cg_rocket_datasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,6 @@ static void CG_Rocket_BuildResolutionList( const char* )

static int CG_Rocket_GetResolutionListIndex( const char* )
{
if ( rocketInfo.data.resolutions.empty() )
{
CG_Rocket_BuildResolutionList( nullptr );
}
return rocketInfo.data.resolutionIndex;
}

Expand Down Expand Up @@ -624,6 +620,7 @@ static void CG_Rocket_BuildAlOutputs( const char* )
trap_Cvar_VariableStringBuffer( "audio.al.device", currentDevice, sizeof( currentDevice ) );
trap_Cvar_VariableStringBuffer( "audio.al.availableDevices", buf, sizeof( buf ) );
head = buf;
rocketInfo.data.alOutputIndex = -1;

while ( ( p = strchr( head, '\n' ) ) )
{
Expand All @@ -649,6 +646,11 @@ static void CG_Rocket_BuildAlOutputs( const char* )
}
}

static int CG_Rocket_GetAlOutputIndex( const char* )
{
return rocketInfo.data.alOutputIndex;
}

static void CG_Rocket_CleanUpAlOutputs( const char* )
{
int i;
Expand Down Expand Up @@ -990,6 +992,7 @@ static void CG_Rocket_BuildMapList( const char* )
Rocket_DSAddRow( "mapList", "default", buf );
}

rocketInfo.data.mapIndex = -1;
}

static void CG_Rocket_CleanUpMapList( const char* )
Expand Down Expand Up @@ -1485,7 +1488,7 @@ static const dataSourceCmd_t dataSourceCmdList[] =
{
{ "alienBuildList", &CG_Rocket_BuildAlienBuildList, &nullSortFunc, &nullCleanFunc, &nullSetFunc, &nullFilterFunc, &nullExecFunc, &nullGetFunc },
{ "alienEvolveList", &CG_Rocket_BuildAlienEvolveList, &nullSortFunc, &nullCleanFunc, &nullSetFunc, &nullFilterFunc, &nullExecFunc, &nullGetFunc },
{ "alOutputs", &CG_Rocket_BuildAlOutputs, &nullSortFunc, &CG_Rocket_CleanUpAlOutputs, &CG_Rocket_SetAlOutputsOutput, &nullFilterFunc, &nullExecFunc, &nullGetFunc },
{ "alOutputs", &CG_Rocket_BuildAlOutputs, &nullSortFunc, &CG_Rocket_CleanUpAlOutputs, &CG_Rocket_SetAlOutputsOutput, &nullFilterFunc, &nullExecFunc, &CG_Rocket_GetAlOutputIndex },
{ "armouryBuyList", &CG_Rocket_BuildArmouryBuyList, &nullSortFunc, &CG_Rocket_CleanUpArmouryBuyList, &nullSetFunc, &nullFilterFunc, &nullExecFunc, &nullGetFunc },
{ "beaconList", &CG_Rocket_BuildBeaconList, &nullSortFunc, &nullCleanFunc, &nullSetFunc, &nullFilterFunc, &nullExecFunc, &nullGetFunc },
{ "demoList", &CG_Rocket_BuildDemoList, &nullSortFunc, &CG_Rocket_CleanUpDemoList, &CG_Rocket_SetDemoListDemo, &nullFilterFunc, &CG_Rocket_ExecDemoList, &nullGetFunc },
Expand Down
19 changes: 0 additions & 19 deletions src/cgame/cg_rocket_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,6 @@ static void CG_Rocket_EventExecForm()
}
}

static void CG_Rocket_SetDataSelectValue()
{
char src[ 100 ];
char tbl[ 100 ];
int index;

Q_strncpyz( src, CG_Argv( 1 ), sizeof ( src ) );
Q_strncpyz( tbl, CG_Argv( 2 ), sizeof( tbl ) );

index = CG_Rocket_GetDataSourceIndex( src, tbl );

if ( index > -1 )
{
Rocket_SetDataSelectIndex( index );
}

}

static void CG_Rocket_EventPlay()
{
const char *track = nullptr;
Expand Down Expand Up @@ -311,7 +293,6 @@ static const eventCmd_t eventCmdList[] =
{ "play", &CG_Rocket_EventPlay },
{ "resetPings", &CG_Rocket_ResetPings },
{ "setChatCommand", &CG_Rocket_SetChatCommand },
{ "setDataSelectValue", &CG_Rocket_SetDataSelectValue },
{ "setDS", &CG_Rocket_SetDS },
{ "show", &CG_Rocket_EventShow },
{ "sortDS", &CG_Rocket_SortDS }
Expand Down
48 changes: 25 additions & 23 deletions src/cgame/rocket/rocketDataSelect.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Maryland 20850 USA.
class RocketDataSelect : public Rml::ElementFormControlDataSelect, public Rml::EventListener
{
public:
RocketDataSelect( const Rml::String &tag ) : Rml::ElementFormControlDataSelect( tag ), selection( -2 ), owner( nullptr) { }
RocketDataSelect( const Rml::String &tag )
: Rml::ElementFormControlDataSelect( tag ), selectionInit( false ), owner( nullptr ) { }
~RocketDataSelect() { }

virtual void OnChildAdd( Element *child )
Expand All @@ -51,19 +52,19 @@ class RocketDataSelect : public Rml::ElementFormControlDataSelect, public Rml::E
if ( child == this )
{
owner = GetOwnerDocument();
owner->AddEventListener( "show", this );
owner->AddEventListener( Rml::EventId::Show, this );
}
}

virtual void OnChildRemove( Element *child )
{
ElementFormControlDataSelect::OnChildRemove( child );

if ( child == this )
if ( child == this )
{
if ( owner )
{
owner->RemoveEventListener( "show", this );
owner->RemoveEventListener( Rml::EventId::Show, this );
}
}
}
Expand All @@ -85,35 +86,36 @@ class RocketDataSelect : public Rml::ElementFormControlDataSelect, public Rml::E
{
extern std::queue< RocketEvent_t * > eventQueue;

if ( event.GetTargetElement() == owner && event == "show" )
if ( event == Rml::EventId::Show )
{
eventQueue.push( new RocketEvent_t( this, va( "setDataSelectValue %s %s", dsName.c_str(), tableName.c_str() ) ) );
}
}
if ( event.GetTargetElement() != owner ) return;

void OnUpdate()
{
extern std::queue< RocketEvent_t * > eventQueue;
if ( !selectionInit )
{
selectionInit = true;
int index = CG_Rocket_GetDataSourceIndex( dsName.c_str(), tableName.c_str() );

ElementFormControlDataSelect::OnUpdate();
if ( index >= 0 )
{
SetSelection( index );
}

if ( GetSelection() != selection )
// only add this after initializing the selection to avoid a spurious setDS
this->AddEventListener( Rml::EventId::Change, this );
}
}
else if ( event == Rml::EventId::Change )
{
selection = GetSelection();
if ( event.GetTargetElement() != this ) return;

// dispatch event so cgame knows about it
eventQueue.push( new RocketEvent_t( Rml::String( va( "setDS %s %s %d", dsName.c_str(), tableName.c_str(), selection ) ) ) );

// dispatch event so rocket knows about it
Rml::Dictionary parameters;
parameters[ "index" ] = va( "%d", selection );
parameters[ "datasource" ] = dsName;
parameters[ "table" ] = tableName;
DispatchEvent( "rowselect", parameters );
eventQueue.push( new RocketEvent_t( Str::Format(
"setDS %s %s %d", dsName.c_str(), tableName.c_str(), GetSelection() ) ) );
}
}

private:
int selection;
bool selectionInit;
Rml::Element *owner;
Rml::String dsName;
Rml::String tableName;
Expand Down

0 comments on commit 39fbbbc

Please sign in to comment.