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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
extern crate libscroll;
extern crate num;



#[macro_use]
extern crate ocaml;

static BAD_PTR: &str = "ERROR: null/invalid ptr passed as scrollview handle";

//use ocaml::core::memory;
use ocaml::{ToValue, Value};

caml!(libscroll_new() {
    //std::mem::transmute::<*mut libscroll::Scrollview, Value>(Box::into_raw(Box::new(libscroll::Scrollview::new())))

    caml_local!(result);

    //result = std::mem::transmute(Box::into_raw(Box::new(libscroll::Scrollview::new())));

    //return result;
    Value::ptr(Box::into_raw(Box::new(libscroll::Scrollview::new())))

});

caml!(libscroll_del(scrollview) {
    let _: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
    // drops

    Value::unit()
});

caml!(libscroll_set_geometry(
        scrollview,
        content_height,
        content_width,
        viewport_height,
        viewport_width
) {
    //let mut scrollview: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
    let content_height = content_height.int64_val() as u64;
    let content_width = content_width.int64_val() as u64;
    let viewport_width = viewport_width.int64_val() as u64;
    let viewport_height = viewport_height.int64_val() as u64;

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .set_geometry(
            content_height,
            content_width,
            viewport_height,
            viewport_width,
        );

    Value::unit()
});

caml!(libscroll_animating(scrollview) {
    //let scrollview: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();

    let result: isize = scrollview
        .as_mut()
        .expect(BAD_PTR)
        .animating()
        .into();

    //return scrollview.animating().bool_val()
    Value::nativeint(result)
});

caml!(libscroll_step_frame(scrollview) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .step_frame(None);


    Value::unit()
});

caml!(libscroll_set_avg_frametime(scrollview, milliseconds) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
    let milliseconds = milliseconds.f64_val();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .set_avg_frametime(milliseconds);

    Value::unit()
});

caml!(libscroll_set_next_frame_predict(scrollview, milliseconds) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
    let milliseconds = milliseconds.f64_val();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .set_next_frame_predict(milliseconds);

    Value::unit()
});

caml!(libscroll_get_position_absolute(scrollview) {
    let scrollview = scrollview.ptr_val::<libscroll::Scrollview>();

    let pos = scrollview
        .as_ref()
        .expect(BAD_PTR)
        .get_position_absolute();

    tuple!(pos.x, pos.y).into()
});

caml!(libscroll_push_pan(scrollview, axis, amount) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
    let axis = axis.usize_val();
    let axis = match axis {
        0 => libscroll::Axis::Horizontal,
        1 => libscroll::Axis::Vertical,
        other => panic!("Bad input for axis bounds {}, expected 0 (horizontal) or 1 (vertical)", other),
    };

    let amount = amount.f64_val();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .push_pan(axis, amount, None);

    Value::unit()
});

caml!(libscroll_push_fling(scrollview) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .push_fling(None);

    Value::unit()
});

caml!(libscroll_push_interrupt(scrollview) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .push_interrupt(None);

    Value::unit()
});

caml!(libscroll_set_source(scrollview, source) {
    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
    //let source: libscroll::Source = std::mem::transmute(source.nativeint_val());
    //let source: Option<libscroll::Source> = num::FromPrimitive::from_isize(source.nativeint_val());
    let source: libscroll::Source = std::mem::transmute(source.usize_val() as u8);

    scrollview
        .as_mut()
        .expect(BAD_PTR)
        .set_source(source);

    Value::unit()
});