Skip to content

Commit

Permalink
Add dropdown field to change an aircraft's livery (see #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jboecker committed Mar 8, 2014
1 parent 8f8bb2a commit a13c5ba
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
15 changes: 9 additions & 6 deletions admintool.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ <h2 class="filename-h2"></h2>
<tr><th>Mission File: </th><td id="save-file-input-td"><!-- input element will be moved here by JavaScript --></td></tr>
<tr><th></th><td><button id="save-mission-button">Save Mission</button></td></tr>
<tr><th></th><td>&nbsp;</td></tr>
<tr><th>Livery List:</th><td><input type="file" accept="text/plain" id="livery-list-input"></td></tr>
<tr><th></th><td><button id="upload_livery_list_button">Upload Livery List</button></td></tr>
<tr><th></th><td>&nbsp;</td></tr>
<tr><th>Mission State:</th><td><input type="file" accept="text/plain" id="state-input"></td></tr>
<tr><th></th><td><button id="upload_mission_state_button">Upload Mission State</button></td></tr>
</table>
Expand Down Expand Up @@ -290,11 +293,11 @@ <h2 class="filename-h2"></h2>

data = JSON:decode(ipc_get("data"))

local routes = {}
local route_ids = {}
local second_waypoints_by_group_name = {}
for _, obj in pairs(data.objects) do
if obj.type == "CLIENT_ACFT_ROUTE" then
routes[obj.group_name] = obj.id
route_ids[obj.group_name] = obj.id
local first_wpt = data.objects[obj.first_waypoint_id]
if first_wpt.next_waypoint_id ~= "" then
second_waypoints_by_group_name[obj.group_name] = data.objects[first_wpt.next_waypoint_id]
Expand All @@ -303,7 +306,7 @@ <h2 class="filename-h2"></h2>
end


function update_waypoints(group, route, data)
function update_waypoints(group, data)
-- delete existing waypoints
local points = group.route.points
for i = #points,2,-1 do
Expand Down Expand Up @@ -374,9 +377,9 @@ <h2 class="filename-h2"></h2>
for group_id=1,#mission.coalition[coalition_name].country[country_id][category].group do
local group = mission.coalition[coalition_name].country[country_id][category].group[group_id]

if routes[group.name] then
update_waypoints(group, routes[group.name], data)
if routes[group.name].livery_id then group.units[1].livery_id = routes[group.name].livery_id end
if route_ids[group.name] then
update_waypoints(group, data)
if data.objects[route_ids[group.name]].livery_id then group.units[1].livery_id = data.objects[route_ids[group.name]].livery_id end
end

local new_units = {}
Expand Down
35 changes: 34 additions & 1 deletion js/admintool.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ $(function() {
return;
}
if (data.md5hash != md5hash) {
alert('Instance "'+$("#instance_id-input").val()+'" was created from another mission file, which was named "'+data.filename+'".\nSaving the waypoint data using a different mission as a template may or may not work.');
alert('Instance "'+ADMIN_URI.query(true).instance_id+'" was created from another mission file, which was named "'+data.filename+'".\nSaving the waypoint data using a different mission as a template may or may not work.');
}
set_status("processing response [updaing mission data]...");
ipc.data = JSON.stringify(data.data);
Expand Down Expand Up @@ -274,4 +274,37 @@ $(function() {
}
});

$("#upload_livery_list_button").click(function(evt) {
var file = document.getElementById("livery-list-input").files[0];
var filename = document.getElementById("livery-list-input").files[0].name;
set_status("reading livery list...");
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(e) {
var text = reader.result;
var ws = new WebSocket(WEBSOCKET_URI.toString());
ws.onopen = function() {
set_status("uploading livery list...");
var request = { request_id: 1,
request: "set_liveries",
admin_pw: ADMIN_URI.password(),
instance_id: ADMIN_URI.query(true).instance_id,
liveries: JSON.parse(text),
};
ws.send(JSON.stringify(request));
}
ws.onmessage = function(e) {
var msg = JSON.parse(e.data);
set_status("processing response...");
if (!msg.success) {
set_status("error");
alert("Error: "+msg.error_msg);
} else {
set_status("uploaded livery list.");
}
}
}
});


});
1 change: 1 addition & 0 deletions js/planner_ng-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mp.Model = function(id_prefix, initial_data) {
function init() {
that.local_id_prefix = id_prefix;
that.objects = {};
that.liveries = initial_data.liveries || null;
that.data_version = 0;
that.selected_feature_group_id = "";

Expand Down
36 changes: 35 additions & 1 deletion js/planner_ng.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $(function() {
$("#activeroute").append($("<option>").attr("value", id_name_array[0]).text(id_name_array[1]));
$("#copyroute_from").append($("<option>").attr("value", id_name_array[0]).text(id_name_array[1]));
}

if (!routes_found) {
return abort_connect("Error: The "+coalition+" side has no routes to edit.");
}
Expand Down Expand Up @@ -157,7 +157,41 @@ $(function() {
if (mp.model) mp.model.setSelectedFeatureGroupId($("#activeroute").val());
});
$(document).on("update_selected_feature_group_id", function(e, old_id, new_id) {
var obj = mp.model.objects[new_id];
$("#activeroute").val(new_id);
$("#livery").empty();
var has_selected = false;
if (mp.model.liveries && mp.model.liveries[obj.unittype]) {
var livery_list = mp.model.liveries[obj.unittype];
$.each(livery_list, function(_, liv) {
console.log(liv);
var option = $("<option>").text(liv).attr("value", liv);
if (liv.toLowerCase() == obj.livery_id.toLowerCase()) {
option.attr("selected", true);
has_selected = true;
}
$("#livery").append(option);
});
}
if (!has_selected) {
$("#livery").append($("<option>").text(obj.livery_id).attr("value", obj.livery_id).attr("selected", true));
}
});
$("#livery").change(function(e) {
var obj = mp.model.objects[$("#activeroute").val()];
var new_livery = $("#livery").val();
$("#livery").val(obj.livery_id);

var obj_copy = JSON.parse(JSON.stringify(obj));
obj_copy.livery_id = new_livery;
mp.api.start_transaction({
objects: [obj_copy]
});
});
$(document).on("update_object", function(e, obj) {
if (obj.type == "CLIENT_ACFT_ROUTE" && $("#activeroute").val() == obj.id) {
$("#livery").val(obj.livery_id);
}
});

$("#center_on_wp0_button").click(function() {
Expand Down
3 changes: 2 additions & 1 deletion planner_client.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ <h2>Connection Lost</h2>
<div id="editmode_routes_controls" class="editmode control_section">
<input type="radio" name="editmode" id="editmode_routes" value="routes" checked="true"><label for="editmode_routes">Edit Routes</label><br>
<table>
<tr><td>Active Route: </td><td><select id="activeroute"></select></td>
<tr><td>Active Route: </td><td><select id="activeroute"></select></td></tr>
<tr><td></td><td><button id="center_on_wp0_button">Center on WP0</button></td></tr>
<tr><td>Livery: </td><td><select id="livery" style="width: 100%;"></select></td></tr>
</table>
</div>
<div id="editmode_annotations_controls" class="editmode control_section">
Expand Down

0 comments on commit a13c5ba

Please sign in to comment.