summaryrefslogtreecommitdiff
path: root/demos/launcher.js
blob: f2f9e30a370eb10670b52ba800c3040ed8f3d78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env gjs
/*
 * Copyright (C) 2010 Simon Wenner <simon@wenner.ch>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * Champlain launcher example in Javascript.
 *
 * Dependencies:
 *  * gobject-introspection
 *  * Build Champlain with '--enable-introspection'
 *  * Gjs: http://live.gnome.org/Gjs
 *
 * If you installed Champlain in /usr/local you have to run:
 * export GI_TYPELIB_PATH=$GI_TYPELIB_PATH:/usr/local/lib/girepository-1.0/
 */

const Lang = imports.lang;
const Clutter = imports.gi.Clutter;
const Champlain = imports.gi.Champlain;

Clutter.init (null);

const PADDING = 10;

function make_button (text)
{
  let white = new Clutter.Color ({red:0xff, blue:0xff, green:0xff, alpha:0xff});
  let black = new Clutter.Color ({red:0x00, blue:0x00, green:0x00, alpha:0xff});

  let button = new Clutter.Actor ();

  let button_bg = new Clutter.Actor ();
  button_bg.set_background_color (white)
  button.add_child (button_bg);
  button.set_opacity (0xcc);

  let button_text = new Clutter.Text ({font_name:"Sans 10", text:text, color:black});
  button.add_child (button_text);

  let [width, height] = button_text.get_size();
  button_bg.set_size (width + PADDING * 2, height + PADDING * 2);
  button_bg.set_position (0, 0);
  button_text.set_position (PADDING, PADDING);

  return button;
}

function map_view_button_release_cb (actor, event)
{
  // FIXME: it does not print and GLib.printf does not work.
  if (event.button != 1 || event.click_count > 1)
    return false;

  let lat = view.y_to_latitude (event.y);
  let lon = view.x_to_longitude (event.x);

  log ("Map clicked at %f, %f \n", lat, lon);

  return true;
}

let stage = new Clutter.Stage ();
stage.title = "Champlain Javascript Example";
stage.set_size (800, 600);

/* Create the map view */
let view = new Champlain.View();
view.set_size (800, 600);
stage.add_actor (view);

/* Create the buttons */
let buttons = new Clutter.Actor ();
let total_width = 0;
buttons.set_position (PADDING, PADDING);

let button = make_button ("Zoom in");
buttons.add_child (button);
button.set_reactive (true);

let width = button.width;
total_width += width + PADDING;
button.connect ("button-release-event", Lang.bind (view,
    function (actor, event)
      {
        view.zoom_in ();
        return true;
      }));

let button = make_button ("Zoom out");
buttons.add_child (button);
button.set_reactive (true);
button.set_position (total_width, 0);

let width = button.width;
total_width += width + PADDING;
button.connect ("button-release-event", Lang.bind (view,
    function (actor, event)
      {
        view.zoom_out ();
        return true;
      }));

stage.add_child (buttons);

/* Create the markers and marker layer */
// TODO

/* Connect to the click event */
view.set_reactive (true);
view.connect ("button-release-event", Lang.bind (view,
    map_view_button_release_cb));

/* Finish initialising the map view */
view.zoom_level = 12;
view.kinetic_mode = true;
view.center_on (45.466, -73.75);

stage.connect ("destroy", Clutter.main_quit);

stage.show ();
Clutter.main ();