forked from delphist2008/phys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.cpp
More file actions
121 lines (108 loc) · 2.37 KB
/
ui.cpp
File metadata and controls
121 lines (108 loc) · 2.37 KB
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
#include "ui.h"
void ui::init(simulator * s, renderer * r)
{
sim = s;
rend = r;
state = CREATE;
}
ui::ui()
{
click_count = 0;
}
void ui::onLMBD()
{
if (state == SELECT)
{
if (body_at_cursor )
{
body_at_click = sim->BodyAtPos(mouse_pos.x, mouse_pos.y);
state = MOVE;
dx = mouse_pos.x - body_at_click->centre_g.x;
dy = mouse_pos.y - body_at_click->centre_g.y;
}
}
else
if (state == MOVE)
state = SELECT;
if (state == CREATE || state == STATIC)
{
pol_to_create[click_count] = click_pos;
click_count++;
if (click_count == 3)
{
click_count = 0;
newbody = new pbody(pol_to_create, RGB(255, 10, 0), 3, sim);
}
}
}
void ui::onLMBU()
{
if (state == MOVE) state = SELECT;
}
void ui::onRMBD()
{
if (body_at_cursor )
{
body_at_click = sim->BodyAtPos(mouse_pos.x, mouse_pos.y);
oldstate = state;
state = RMBB;
body_at_click->inangl = body_at_click->angle;
}
}
float im;
void ui::onRMBU()
{
if (state == RMBB)
{
impulse_apply_vector.x = -mouse_pos.x + impulse_line_begin.x;
impulse_apply_vector.y = -mouse_pos.y + impulse_line_begin.y;
im = sqrt(impulse_apply_vector.x * impulse_apply_vector.x + impulse_apply_vector.y*impulse_apply_vector.y );
impulse_apply_vector.x /= im;
impulse_apply_vector.y /= im;
body_at_click->addimpulse(impulse_line_begin, impulse_apply_vector, im*3000.0);
state = oldstate;
}
}
void ui::onMM()
{
if (state == MOVE)
{
body_at_click->centre_g.x = mouse_pos.x -dx;
body_at_click->centre_g.y = mouse_pos.y -dy;
}
}
void ui::onKeyDown(int key)
{
if (key == 77 ) //m
if (state == SELECT)
state = CREATE;
else state = SELECT;
if (key == 67 ) //c
{
state = CREATE;
sim->bodies.clear();
}
if (key == 83)//s
{
if (state != STATIC)
{
oldstate = state;
state = STATIC;
}
else state = oldstate;
}
}
void ui::onKeyUp(int key)
{
}
void ui::update()
{
if (state == RMBB)
{
_X = (click_pos.x - body_at_click->centre_g.x);
_Y = (click_pos.y - body_at_click->centre_g.y);
impulse_line_begin.x = _X*cos(body_at_click->angle - body_at_click->inangl) - _Y*sin(body_at_click->angle - body_at_click->inangl) + body_at_click->centre_g.x;
impulse_line_begin.y = _X*sin(body_at_click->angle - body_at_click->inangl) + _Y*cos(body_at_click->angle - body_at_click->inangl) + body_at_click->centre_g.y;
}
sprintf(debugstr, "state= %s x= %f y= %f\0", statelist[state], mouse_pos.x, mouse_pos.y);
}