Sometimes I have to implement a feature like customized drag & drop. The IP(imperative programming) style code may goes like this:
bool mouse_down = false;
Vec2 mouse_pos;
void on_mouse_down() {
mouse_down = true;
mouse_pos = cursor_pos();
}
void on_mouse_move() {
if(mouse_down) {
Vec2f c = cursor_pos();
Vec2f d = c - mouse_pos;
}
}
void on_mouse_up() {
mouse_down = false;
}
I know this works fine, but I donnot like redundant variables and separated procedures for something like this at all. The code above is just a simplest sample. Variables account would explode if I added more functions and limitations to the entire system, like left, right, middle, wheel, double clicking, different operations while cursor going over different objects... In IP maybe it's unavoidable; my question is, is there a better way to deal with this kinda thing in FP(functional programming) and how it looks like, how to make concinnity? Any ideas and suggestions are appreciated, no matter whatever programming languages, Lisp, Scheme, F#, Ruby, etc. or some better way to do this in IP?
Edited: You can treat the sample as something like C code.