Skip to content

Instantly share code, notes, and snippets.

@knomepasi
Last active September 24, 2020 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knomepasi/cb9f47f24bfd2eae58ef3801239a3c67 to your computer and use it in GitHub Desktop.
Save knomepasi/cb9f47f24bfd2eae58ef3801239a3c67 to your computer and use it in GitHub Desktop.
OpenRCT2 plugin: A window for toggling transparency options easily
var transparencyOptionsWindow = function( ) {
if( ui.getWindow( 'transparency-options' ) ) {
ui.getWindow( 'transparency-options' ).bringToFront( );
return;
}
var windowOpts = {
classification: 'transparency-options',
width: 123,
height: 40,
title: 'Transparency...',
isSticky: true,
colours: [12, 12],
widgets: [
createButton( 'rides', 'See-Through Rides', 1, 1, 5187, function( ) { toggleTransparencyOption( 1, 'rides' ); } ),
createButton( 'scenery', 'See-Through Scenery', 2, 1, 5191, function( ) { toggleTransparencyOption( 2, 'scenery' ); } ),
createButton( 'paths', 'See-Through Paths', 3, 1, 29372, function( ) { toggleTransparencyOption( 16, 'paths' ); } ),
createButton( 'supports', 'Invisible Supports', 4, 1, 'S', function( ) { toggleTransparencyOption( 3, 'supports' ); } ),
createButton( 'people', 'Invisible People', 5, 1, 5193, function( ) { toggleTransparencyOption( 11, 'people' ); } ),
],
onClose: function( ) {
window = undefined;
}
}
var window = ui.openWindow( windowOpts );
// After opening the window, load visibility flags and toggle the buttons accordingly
setTransparencyOptions( );
}
var createButton = function( name, tooltip, column, row, content, onClick ) {
var button = {
type: 'button',
name: name,
tooltip: tooltip,
border: true,
y: 15 + ( ( row - 1 ) * 24 ),
x: 2 + ( ( column - 1 ) * 24 ),
width: 24,
height: 24,
onClick: onClick
}
if( typeof content == 'number' ) {
button.image = content;
} else {
button.text = content;
}
return button;
}
var toggleTransparencyOption = function( flag, button_name ) {
// Toggles viewport visibility flags based on the switch positions
if( ui.mainViewport.visibilityFlags & 1 << flag ) {
ui.mainViewport.visibilityFlags &= ~( 1 << flag );
ui.getWindow( 'transparency-options' ).findWidget( button_name ).isPressed = false;
context.sharedStorage.set( 'transparency-options.flag_' + flag, false );
} else {
ui.mainViewport.visibilityFlags |= 1 << flag;
ui.getWindow( 'transparency-options' ).findWidget( button_name ).isPressed = true;
context.sharedStorage.set( 'transparency-options.flag_' + flag, true );
}
}
var setTransparencyOptions = function( ) {
// Sets switches based on visibility flags
if( ui.mainViewport.visibilityFlags & 1 << 1 ) {
ui.getWindow( 'transparency-options' ).findWidget( 'rides' ).isPressed = true;
}
if( ui.mainViewport.visibilityFlags & 1 << 2 ) {
ui.getWindow( 'transparency-options' ).findWidget( 'scenery' ).isPressed = true;
}
if( ui.mainViewport.visibilityFlags & 1 << 16 ) {
ui.getWindow( 'transparency-options' ).findWidget( 'paths' ).isPressed = true;
}
if( ui.mainViewport.visibilityFlags & 1 << 3 ) {
ui.getWindow( 'transparency-options' ).findWidget( 'supports' ).isPressed = true;
}
if( ui.mainViewport.visibilityFlags & 1 << 11 ) {
ui.getWindow( 'transparency-options' ).findWidget( 'people' ).isPressed = true;
}
}
var forceTransparencyOptions = function( ) {
// Sets switchers based stored settings
const flags = [ 1, 2, 16, 3, 11 ];
flags.forEach( function( flag ) {
var flag_enabled = context.sharedStorage.get( 'transparency-options.flag_' + flag );
if( flag_enabled ) {
ui.mainViewport.visibilityFlags |= 1 << flag;
} else {
ui.mainViewport.visibilityFlags &= ~( 1 << flag );
}
} );
}
var main = function( ) {
if( typeof ui !== 'undefined' ) {
ui.registerMenuItem( 'Transparency Options', function( ) {
transparencyOptionsWindow( );
} );
}
context.subscribe( 'interval.tick', function( ) {
if( ui.getWindow( 'transparency-options' ) ) {
// Force the options to be kept as is when the option window is open, even when other tools are closed
forceTransparencyOptions( );
}
} );
/*
context.registerIntent( {
key: 'transparency-options.open-window',
title: 'Open Transparency Options Window',
shortcut: 'CTRL+X',
action: transparencyOptionsWindow
} );
*/
}
registerPlugin( {
name: 'Transparency Options',
version: '1.0',
authors: ['knome'],
type: 'local',
licence: 'MIT',
main: main
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment