Plastic Gem #44: Custom Camera Zoom
by Anthony Rosenbaum · 08/21/2008 (6:59 am) · 2 comments
Download Code File

Custom Camera Zoom
Difficulty: Easy
This Gem will look at adding custom FOV zoom to the shapebaseImage. There are two parts the C side, which should be familiar to you and script side. The C side follows the regular pattern of adding a variable to the class, initializing it, exposing it, and networking it. The script side will take this fov and change the player's FOV based on datablocks value. Additionally I have included code to change the mouse sensitivity when zoomed in.
1) In Shapebase.h provide a new variable customZoom within ShapeBaseImageData Struct after
bool statesLoaded;
add
2) In shapeImage.cc initialize customZoom in the constructor ShapeBaseImageData::ShapeBaseImageData after
statesLoaded = false;
add
3) Expose customZoom in ShapeBaseImageData::initPersistFields at bottom add
4) Networking requires send customZoom in ShapeBaseImageData::packData after stream->write(shellVelocity); add
5) Networking requeies receiving customZoom in ShapeBaseImageData::unpackData after stream->read(&shellVelocity); add
Compile, now we will move into scripts. We must now add customZoom to a shapeBaseImageData datablock( we'll put this in a datablock called SniperImage)
6) Add to a shapeBaseImage datablock
The rest of the code is all within default.binds.cs, it can be added in one chunk, but I have broken it down into sections as to explain what is going on
7) Provide deafult global variales on the client for FOV and mouse sensitivity
8) Replace the default getMouseAdjustAmount to use our version which has the variable $mouseSens instead of the value 0.01
9) Replace the default toggleZoom with this function
Delete Prefs, load a mission, be sure you are holding the SniperImage and press E to see it zoom.

Custom Camera Zoom
Difficulty: Easy
This Gem will look at adding custom FOV zoom to the shapebaseImage. There are two parts the C side, which should be familiar to you and script side. The C side follows the regular pattern of adding a variable to the class, initializing it, exposing it, and networking it. The script side will take this fov and change the player's FOV based on datablocks value. Additionally I have included code to change the mouse sensitivity when zoomed in.
1) In Shapebase.h provide a new variable customZoom within ShapeBaseImageData Struct after
bool statesLoaded;
add
// > pg customZoom F32 customZoom; // so we can have a custom zoom value for each weapon, -1 means none // < pg customZoom
2) In shapeImage.cc initialize customZoom in the constructor ShapeBaseImageData::ShapeBaseImageData after
statesLoaded = false;
add
// > pg customZoom customZoom = -1.0f; // meaning none // < pg customZoom
3) Expose customZoom in ShapeBaseImageData::initPersistFields at bottom add
// > pg customzoom
addField("customzoom", TypeF32, Offset(customZoom, ShapeBaseImageData));
// < pg customzoom4) Networking requires send customZoom in ShapeBaseImageData::packData after stream->write(shellVelocity); add
// > pg customZoom stream->write(customZoom); // < pg customZoom
5) Networking requeies receiving customZoom in ShapeBaseImageData::unpackData after stream->read(&shellVelocity); add
// > pg customZoom stream->read(&customZoom); // < pg customZoom
Compile, now we will move into scripts. We must now add customZoom to a shapeBaseImageData datablock( we'll put this in a datablock called SniperImage)
6) Add to a shapeBaseImage datablock
// > pg customZoom customZoom = "15"; // < pg customZoom
The rest of the code is all within default.binds.cs, it can be added in one chunk, but I have broken it down into sections as to explain what is going on
7) Provide deafult global variales on the client for FOV and mouse sensitivity
// > pg customZoom //set up default FOV if($Pref::player::CurrentFOV $= "") $Pref::player::CurrentFOV = 45; //setup default mouse sensitivity $mouseSens = 0.01; $sniperMouseSense = 0.003; //mouse sensativity when zoomed in // < pg customZoom
8) Replace the default getMouseAdjustAmount to use our version which has the variable $mouseSens instead of the value 0.01
// > pg customZoom
//add
function getMouseAdjustAmount(%val)
{
// based on a default camera fov of 90'
return(%val * ($cameraFov / 90) * $mouseSens);
}
// < pg customZoom9) Replace the default toggleZoom with this function
// > pg customZoom
function toggleZoom( %val )
{
if ( %val ) // key pressed
{
%player = ServerConnection.getcontrolobject();
%weapon = %player.getMountedImage(0);
%zoom = %weapon.customZoom;
%type = %weapon.getName();
echo("zoom " SPC %zoom SPC %type);
if(%type $= "SniperImage"){
//add mask if you like here
$mouseSens = $sniperMouseSense; // change mouse sensitivity
}
if(%zoom $= -1)
%zoom = $Pref::player::DefaultFov ;
setFov( %zoom ); //change FOV
}
else // key released
{
$mouseSens = 0.01; //restore mouse aiming
//remove mask here
setFov( $Pref::player::DefaultFov ); // restore FOV
}
}
moveMap.bind(keyboard, e, toggleZoom);
// < pg customZoomDelete Prefs, load a mission, be sure you are holding the SniperImage and press E to see it zoom.
About the author

Associate Steve Acaster
[YorkshireRifles.com]