summaryrefslogtreecommitdiff
path: root/demos/launcher-vala.vala
blob: ed772f4770b7971931ac1a559821f3c072fa7f2c (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
137
138
/*
 * 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
 */

using GLib;
using Clutter;
using Champlain;

public class Launcher : GLib.Object
{
  private const int PADDING = 10;
  private Champlain.View view;
  private Clutter.Stage stage;

  public Launcher ()
  {
    float width, total_width = 0;

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

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

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

    var button = make_button ("Zoom in");
    buttons.add_child (button);
    button.reactive = true;
    button.get_size (out width, null);
    total_width += width + PADDING;
    button.button_release_event.connect ((event) => {
        view.zoom_in ();
        return true;
      });

    button = make_button ("Zoom out");
    buttons.add_child (button);
    button.reactive = true;
    button.set_position (total_width, 0);
    button.get_size (out width, null);
    total_width += width + PADDING;
    button.button_release_event.connect ((event) => {
        view.zoom_out ();
        return true;
      });

    stage.add_child (buttons);

    /* Create the markers and marker layer */
    var layer = new  DemoLayer ();
    view.add_layer (layer);

    /* Connect to the click event */
    view.reactive = true;
    view.button_release_event.connect (button_release_cb);

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

  public void show ()
  {
    stage.show ();
  }

  private bool button_release_cb (Clutter.ButtonEvent event)
  {
    double lat, lon;

    if (event.button != 1 || event.click_count > 1)
      return false;
      
    lat = view.y_to_latitude (event.y);
    lon = view.x_to_longitude (event.x);

    GLib.print ("Map clicked at %f, %f \n", lat, lon);

    return true;
  }

  public Clutter.Actor make_button (string text)
  {
    Clutter.Color white = { 0xff, 0xff, 0xff, 0xff };
    Clutter.Color black = { 0x00, 0x00, 0x00, 0xff };
    float width, height;

    var button = new Clutter.Actor ();

    var button_bg = new Clutter.Actor ();
    button_bg.set_background_color (white);
    button.add_child (button_bg);
    button_bg.opacity = 0xcc;

    var button_text = new Clutter.Text.full ("Sans 10", text, black);
    button.add_child (button_text);
    button_text.get_size (out width, out height);

    button_bg.set_size (width + PADDING * 2, height + PADDING * 2);
    button_bg.set_position (0, 0);
    button_text.set_position (PADDING, PADDING);

    return button;
  }

  public static int main (string[] args)
  {
    if (Clutter.init (ref args) != InitError.SUCCESS)
      return 1;

    var launcher = new Launcher ();
    launcher.show ();
    Clutter.main ();
    return 0;
  }
}