<?php

// Functions to display and edit project-specific prefs
// (SETI@home/Astropulse version)
// See seti_boinc/client/sah_gfx.h for a description of the fields
//
// IF YOU MODIFY THIS FILE, COMMIT IT TO CVS
//
// We also use two "presets"
//  format_preset (SETI@home classic, etc.)
//    determines all prefs except colors, image filename
//  color_preset (rainbow, etc.)
//    determines color prefs
//  Setting either of these to "Custom" gives control of individual prefs

function project_specific_prefs_default() {
    return "<format_preset>SETI@home classic</format_preset>
        <color_preset>Rainbow</color_preset>
    ";
}

function option($name, $val) {
    if ($name == $val) {
        $x = "selected";
    } else {
        $x = "";
    }
    echo "<option name='$name' $x>$name\n";
}

function select_option($opt, $val) {
    if ($opt == $val) {
        return "<option name='$opt' selected>$opt\n";
    } else {
        return "<option name='$opt'>$opt\n";
    }
}

function select_str($name, $val, $opts) {
    $x = "<select name=$name>\n";
    foreach ($opts as $opt) {
        $x = $x.select_option($opt, $val);
    }
    $x = $x."</select>\n";
    return $x;
}

// The following MUST agree with the strings in sah_gfx.C
//
function text_style_options() {
    return array("Pillars", "Heads-up", "Panels");
}

function graph_style_options() {
    return array("Rectangles", "Surface", "Wireframe", "Planes");
}

define("FORMAT_PRESET_DESC", "Graphics preferences <br><font size=-2>Select Custom to edit individual parameters</font>");
define("TEXT_STYLE_DESC", "Text style");
define("GRAPH_STYLE_DESC", "Graph style");
define("MAX_FPS_DESC", "Maximum frames per second");
define("MAX_CPU_DESC", "Maximum CPU % for graphics<br><font size=-2>0 ... 100");
define("GROW_TIME_DESC", "Graph grow time<br><font size=-2>seconds");
define("HOLD_TIME_DESC", "Graph hold time<br><font size=-2>seconds");
define("GRAPH_ALPHA_DESC", "Transparency of surfaces<br><font size=-2>(0=transparent ... 1=opaque)");
define("ROLL_PERIOD_DESC", "Horizontal oscillation period<br><font size=-2>seconds");
define("ROLL_RANGE_DESC", "Horizontal oscillation range<br><font size=-2>degrees");
define("PITCH_PERIOD_DESC", "Vertical oscillation period<br><font size=-2>seconds");
define("PITCH_RANGE_DESC", "Vertical oscillation range<br><font size=-2>degrees");
define("STARFIELD_SIZE_DESC", "Number of stars");
define("MAX_FPS_DESC", "Maximum frames per second");
define("MAX_CPU_DESC", "Maximum CPU % for graphics<br><font size=-2>0 ... 100");
define("STARFIELD_SPEED_DESC", "Speed of stars<br><font size=-2>Try -1000 ... 1000");

define("START_HUE_DESC", "Start hue in graph<br><font size=-2>0 ... 1 (orange/yellow/green/blue/red/orange)");
define("HUE_CHANGE_DESC", "Hue change in graph<br><font size=-2>Usually -1 ... 1");
define("COLOR_PRESET_DESC", "Color preferences: <br><font size=-2>Select Custom to edit individual parameters</font>");
define("BACKGROUND_DESC", "URL of background image<br><font size=-2>This image (JPEG, Targa, BMP or PPM format) will be displayed in the background.");
define("USER_LOGO_DESC", "URL of logo image<br><font size=-2>This image (JPEG, Targa, BMP or PPM format) will be displayed in the lower corner.");

// given struct, show form for editing
//
function project_specific_prefs_edit($prefs) {
    row2(
        FORMAT_PRESET_DESC,
        select_str("format_preset", $prefs->format_preset, array("SETI@home classic", "Minimalist", "Panels", "Custom")));

    if ($prefs->format_preset == "Custom") {
        row2(TEXT_STYLE_DESC,
            select_str("text_style", $prefs->text_style, text_style_options())
        );
        row2(GRAPH_STYLE_DESC,
            select_str("graph_style", $prefs->graph_style, graph_style_options()));
        row2(MAX_FPS_DESC, "<input name=max_fps value='$prefs->max_fps'>");
        row2(MAX_CPU_DESC, "<input name=max_cpu value='$prefs->max_cpu'>");
        row2(GROW_TIME_DESC, "<input name=grow_time value='$prefs->grow_time'>");
        row2(HOLD_TIME_DESC, "<input name=hold_time value='$prefs->hold_time'>");
        row2(GRAPH_ALPHA_DESC, "<input name=graph_alpha value='$prefs->graph_alpha'>");
        row2(ROLL_PERIOD_DESC, "<input name=roll_period value='$prefs->roll_period'>");
        row2(ROLL_RANGE_DESC, "<input name=roll_range value='$prefs->roll_range'>");
        row2(PITCH_PERIOD_DESC, "<input name=pitch_period value='$prefs->pitch_period'>");
        row2(PITCH_RANGE_DESC, "<input name=pitch_range value='$prefs->pitch_range'>");
        row2(STARFIELD_SIZE_DESC, "<input name=starfield_size value='$prefs->starfield_size'>");
        row2(STARFIELD_SPEED_DESC, "<input name=starfield_speed value='$prefs->starfield_speed'>");
    }
    row2(COLOR_PRESET_DESC,
        select_str("color_preset", $prefs->color_preset, array("Rainbow", "Tahiti Sunset", "Desert Sands", "Water World", "Custom")));
    if ($prefs->color_preset == "Custom") {
        row2(START_HUE_DESC, "<input name=start_hue value='$prefs->start_hue'>");
        row2(HUE_CHANGE_DESC, "<input name=hue_change value='$prefs->hue_change'");
    }

    row2(
        BACKGROUND_DESC,
        "<input size=60 name=background value='$prefs->background'>"
    );
    row2(
        USER_LOGO_DESC,
        "<input size=60 name=user_logo value='$prefs->user_logo'>"
    );
}

// Return XML version of project-specific prefs from form vars
//
function project_specific_prefs_parse_form() {
    $format_preset = $_GET["format_preset"];
    $text_style = $_GET["text_style"];
    $graph_style = $_GET["graph_style"];
    $max_fps = $_GET["max_fps"];
    $max_cpu = $_GET["max_cpu"];
    $grow_time = $_GET["grow_time"];
    $hold_time = $_GET["hold_time"];
    $graph_alpha = $_GET["graph_alpha"];
    $roll_period = $_GET["roll_period"];
    $roll_range = $_GET["roll_range"];
    $pitch_period = $_GET["pitch_period"];
    $pitch_range = $_GET["pitch_range"];
    $starfield_size = $_GET["starfield_size"];
    $starfield_speed = $_GET["starfield_speed"];

    $color_preset = $_GET["color_preset"];
    $start_hue = $_GET["start_hue"];
    $hue_change = $_GET["hue_change"];
    $background = $_GET["background"];
    $user_logo = $_GET["user_logo"];

    switch ($format_preset) {
    case "SETI@home classic":
        $text_style = "Pillars";
        $graph_style = "Rectangles";
        $max_fps = 30;
        $max_cpu = 50;
        $grow_time = 10;
        $hold_time = 5;
        $graph_alpha = 0.7;
        $roll_period = 10;
        $roll_range = 20.;
        $pitch_period = 30;
        $pitch_range = 30.;
        $starfield_size = 2000;
        $starfield_speed = 40;
        break;
    case "Minimalist":
        $text_style = "Heads-up";
        $graph_style = "Wireframe";
        $max_fps = 30;
        $max_cpu = 50;
        $grow_time = 10;
        $hold_time = 5;
        $graph_alpha = 0.7;
        $roll_period = 10;
        $roll_range = 20.;
        $pitch_period = 30;
        $pitch_range = 30.;
        $starfield_size = 0;
        $starfield_speed = 0;
        break;
    case "Panels":
        $text_style = "Panels";
        $graph_style = "Surface";
        $max_fps = 30;
        $max_cpu = 50;
        $grow_time = 10;
        $hold_time = 5;
        $graph_alpha = 0.7;
        $roll_period = 10;
        $roll_range = 20.;
        $pitch_period = 30;
        $pitch_range = 30.;
        $starfield_size = 2000;
        $starfield_speed = 40;
        break;
    case "Custom":
        if ($text_style == null) $text_style = "Pillars";
        if ($graph_style == null) $graph_style = "Rectangles";
        if ($max_fps == null) $max_fps = 30;
        if ($max_cpu == null) $max_cpu = 50;
        if ($grow_time == null) $grow_time = 10;
        if ($hold_time == null) $hold_time = 5;
        if ($graph_alpha == null) $graph_alpha = 0.7;
        if ($roll_period == null) $roll_period = 10;
        if ($roll_range == null) $roll_range = 20.0;
        if ($pitch_period == null) $pitch_period = 30;
        if ($pitch_range == null) $pitch_range = 30.0;
        if ($starfield_size == null) $starfield_size = 2000;
        if ($starfield_speed == null) $starfield_speed = 40.0;
        break;
    }

    switch($color_preset) {
    case "Rainbow":
        $start_hue = 0.0;
        $hue_change = 1.0;
        break;
    case "Tahiti Sunset":
        $start_hue = 0.5;
        $hue_change = 0.5;
        break;
    case "Desert Sands":
        $start_hue = 0.0;
        $hue_change = 0.2;
        break;
    case "Water World":
        $start_hue = 0.2;
        $hue_change = 0.4;
        break;
    case "Custom":
        if ($start_hue == null) $start_hue = 0;
        if ($hue_change == null) $hue_change = 1;
        break;
    }
    $x = "<format_preset>$format_preset</format_preset>
        <text_style>$text_style</text_style>
        <graph_style>$graph_style</graph_style>
        <max_fps>$max_fps</max_fps>
        <max_cpu>$max_cpu</max_cpu>
        <grow_time>$grow_time</grow_time>
        <hold_time>$hold_time</hold_time>
        <graph_alpha>$graph_alpha</graph_alpha>
        <roll_period>$roll_period</roll_period>
        <roll_range>$roll_range</roll_range>
        <pitch_period>$pitch_period</pitch_period>
        <pitch_range>$pitch_range</pitch_range>
        <starfield_size>$starfield_size</starfield_size>
        <starfield_speed>$starfield_speed</starfield_speed>
        <color_preset>$color_preset</color_preset>
        <start_hue>$start_hue</start_hue>
        <hue_change>$hue_change</hue_change>
    ";
    if (strlen($background)) {
        $x = $x."    <app_file>
            <timestamp>".time()."</timestamp>
            <open_name>background</open_name>
<url>$background</url>
        </app_file>
        ";
    }
    if (strlen($user_logo)) {
        $x = $x."    <app_file>
            <timestamp>".time()."</timestamp>
            <open_name>user_logo</open_name>
<url>$user_logo</url>
        </app_file>
        ";
    }
    return $x;
}

// show non-editable version of prefs
//
function project_specific_prefs_show($prefs) {
    row2(FORMAT_PRESET_DESC, $prefs->format_preset);
    if ($prefs->format_preset == "Custom") {
        row2(TEXT_STYLE_DESC, $prefs->text_style);
        row2(GRAPH_STYLE_DESC, $prefs->graph_style);
        row2(MAX_FPS_DESC, $prefs->max_fps);
        row2(MAX_CPU_DESC, $prefs->max_cpu);
        row2(GROW_TIME_DESC, $prefs->grow_time);
        row2(HOLD_TIME_DESC, $prefs->hold_time);
        row2(GRAPH_ALPHA_DESC, $prefs->graph_alpha);
        row2(ROLL_PERIOD_DESC, $prefs->roll_period);
        row2(ROLL_RANGE_DESC, $prefs->roll_range);
        row2(PITCH_PERIOD_DESC, $prefs->pitch_period);
        row2(PITCH_RANGE_DESC, $prefs->pitch_range);
        row2(STARFIELD_SIZE_DESC, $prefs->starfield_size);
        row2(STARFIELD_SPEED_DESC, $prefs->starfield_speed);
    }
    row2(COLOR_PRESET_DESC, $prefs->color_preset);
    if ($prefs->color_preset == "Custom") {
        row2(START_HUE_DESC, $prefs->start_hue);
        row2(HUE_CHANGE_DESC, $prefs->hue_change);
    }

    if (strlen($prefs->background)) {
        row2(BACKGROUND_DESC, $prefs->background);
    } else {
        row2(BACKGROUND_DESC, "none");
    }
    if (strlen($prefs->user_logo)) {
        row2(USER_LOGO_DESC, $prefs->user_logo);
    } else {
        row2(USER_LOGO_DESC, "none");
    }
}

// parse XML, fill in struct
//
function project_specific_prefs_parse($prefs_xml) {
    $prefs->format_preset = parse_element($prefs_xml, "<format_preset>");
    $prefs->text_style = parse_element($prefs_xml, "<text_style>");
    $prefs->graph_style = parse_element($prefs_xml, "<graph_style>");
    $prefs->max_fps = parse_element($prefs_xml, "<max_fps>");
    $prefs->max_cpu = parse_element($prefs_xml, "<max_cpu>");
    $prefs->grow_time = parse_element($prefs_xml, "<grow_time>");
    $prefs->hold_time = parse_element($prefs_xml, "<hold_time>");
    $prefs->graph_alpha = parse_element($prefs_xml, "<graph_alpha>");
    $prefs->roll_period = parse_element($prefs_xml, "<roll_period>");
    $prefs->roll_range = parse_element($prefs_xml, "<roll_range>");
    $prefs->pitch_period = parse_element($prefs_xml, "<pitch_period>");
    $prefs->pitch_range = parse_element($prefs_xml, "<pitch_range>");
    $prefs->starfield_size = parse_element($prefs_xml, "<starfield_size>");
    $prefs->starfield_speed = parse_element($prefs_xml, "<starfield_speed>");
    $prefs->color_preset = parse_element($prefs_xml, "<color_preset>");
    $prefs->start_hue = parse_element($prefs_xml, "<start_hue>");
    $prefs->hue_change = parse_element($prefs_xml, "<hue_change>");
    $prefs->background = parse_element($prefs_xml, "background</open_name>\n<url>");
    $prefs->user_logo = parse_element($prefs_xml, "user_logo</open_name>\n<url>");
    return $prefs;
}
