| 1 |
# Initialize Canvas system |
| 2 |
# This depends on a special immat.ac model providing 2D-texture-planes to render on: |
| 3 |
# ImmatFuselageLeft, ImmatFuselageRight, ImmatWing, ImmatPanel |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
var ImmatCanvas = canvas.new({ |
| 8 |
"name": "liveryCustomRegistration", # The name is optional but allow for easier identification |
| 9 |
"size": [2048, 512], # Size of the underlying texture (should be a power of 2, required) [Resolution] |
| 10 |
"view": [2048, 512], # Virtual resolution (Defines the coordinate system of the canvas [Dimensions] |
| 11 |
# which will be stretched to the size of the texture, required) |
| 12 |
"mipmapping": 1 # Enable mipmapping (optional) |
| 13 |
}); |
| 14 |
ImmatCanvas.setColorBackground(0, 0, 0, 0.0); |
| 15 |
|
| 16 |
var immatCanvasGroup = ImmatCanvas.createGroup(); |
| 17 |
|
| 18 |
# Create text elements |
| 19 |
var canvas_immat_text = immatCanvasGroup.createChild("text", "fuselage.immat.left") |
| 20 |
.setTranslation(1024, 256) # The origin is in the top left corner |
| 21 |
.setAlignment("center-center") # All values from osgText are supported (see $FG_ROOT/Docs/README.osgtext) |
| 22 |
.show(); # Actual showing/hiding is toggled in the immat.xml |
| 23 |
|
| 24 |
|
| 25 |
# Initialize the canvas system for a given module instance (nil=local model) |
| 26 |
var initC172ImmatCanvas = func(target_module_id=nil) { |
| 27 |
# Place canvas on model |
| 28 |
|
| 29 |
if (target_module_id != nil) { |
| 30 |
# Multiplayer instance; var target_module_id is set in <load> of model xml |
| 31 |
print("C172 immat/registration: initialize multiplayer instance for target_module_id="~target_module_id); |
| 32 |
|
| 33 |
ImmatCanvas.addPlacement({"module-id": target_module_id, "type": "scenery-object", "node": "ImmatFuselageLeft"}); |
| 34 |
ImmatCanvas.addPlacement({"module-id": target_module_id, "type": "scenery-object", "node": "ImmatFuselageRight"}); |
| 35 |
ImmatCanvas.addPlacement({"module-id": target_module_id, "type": "scenery-object", "node": "ImmatWingLeft"}); |
| 36 |
} else { |
| 37 |
# Local instance |
| 38 |
print("C172 immat/registration: initialize local instance"); |
| 39 |
ImmatCanvas.addPlacement({"node": "ImmatFuselageLeft"}); |
| 40 |
ImmatCanvas.addPlacement({"node": "ImmatFuselageRight"}); |
| 41 |
ImmatCanvas.addPlacement({"node": "ImmatWingLeft"}); |
| 42 |
} |
| 43 |
|
| 44 |
# Debug window, shows generated registration in dialog window |
| 45 |
#var window = canvas.Window.new([512, 128],"dialog"); |
| 46 |
#window.setCanvas(ImmatCanvas); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
# Function to be called to update the displayed registration texture, |
| 52 |
# either for the local instance or a remote one |
| 53 |
var set_registration_number = func (namespace, immat) { |
| 54 |
#print("C172 immat/registration: set_registration_number"); |
| 55 |
var registrationCanvasUpdater = maketimer(0.5, func(){ |
| 56 |
# Should run deferred, so the livery has time to overload the properties |
| 57 |
var livery_font = namespace.getNode("sim/model/livery/immat/font_name",1).getValue(); # to be defined from livery xml |
| 58 |
var livery_size = namespace.getNode("sim/model/livery/immat/font_size",1).getValue(); |
| 59 |
var livery_font_r = namespace.getNode("sim/model/livery/immat/colour_r",1).getValue(); |
| 60 |
var livery_font_g = namespace.getNode("sim/model/livery/immat/colour_g",1).getValue(); |
| 61 |
var livery_font_b = namespace.getNode("sim/model/livery/immat/colour_b",1).getValue(); |
| 62 |
if (livery_font != nil) { |
| 63 |
# Update the canvas registration elements |
| 64 |
print("C172 immat/registration: update registration: "~immat); |
| 65 |
canvas_immat_text.setText(immat) |
| 66 |
.setFont(livery_font) # Fonts are loaded either from $AIRCRAFT_DIR/Fonts or $FG_ROOT/Fonts |
| 67 |
.setFontSize(livery_size, 1.0) # Set fontsize and optionally character aspect ratio |
| 68 |
.setColor(livery_font_r, livery_font_g, livery_font_b) # Text color |
| 69 |
.setText(immat); |
| 70 |
} else { |
| 71 |
print("C172 immat/registration: livery misses canvas immat properties: skipping canvas update."); |
| 72 |
} |
| 73 |
}); |
| 74 |
registrationCanvasUpdater.singleShot = 1; |
| 75 |
registrationCanvasUpdater.start(); |
| 76 |
|
| 77 |
|
| 78 |
# Old code using text-translation (buggy in multiplayer! see https://sourceforge.net/p/flightgear/codetickets/2130/) |
| 79 |
# This is still needed for the immat on panel |
| 80 |
# TODO: we probably should convert that to casnvas too |
| 81 |
if (immat == nil) |
| 82 |
return; |
| 83 |
|
| 84 |
var glyph = nil; |
| 85 |
var immat_size = size(immat); |
| 86 |
|
| 87 |
if (immat_size != 0) |
| 88 |
immat = string.uc(immat); |
| 89 |
|
| 90 |
for (var i = 0; i < 6; i += 1) { |
| 91 |
if (i >= immat_size) |
| 92 |
glyph = -1; |
| 93 |
elsif (string.isupper(immat[i])) |
| 94 |
glyph = immat[i] - `A`; |
| 95 |
elsif (string.isdigit(immat[i])) |
| 96 |
glyph = immat[i] - `0` + 26; |
| 97 |
else |
| 98 |
glyph = 36; |
| 99 |
namespace.getNode("sim/model/c172s/regnum"~(i+1), 1).setValue(glyph+1); |
| 100 |
} |
| 101 |
}; |